| 327 | if (img == null && file.getPath().matches(".*\\.[jJ][pP][eE]?[gG]$")) { |
| 328 | TJDecompressor tjd = null; |
| 329 | FileInputStream fis = null; |
| 330 | try { |
| 331 | tjd = new TJDecompressor(); |
| 332 | fis = new FileInputStream(file); |
| 333 | |
| 334 | Logging.info("Loading {0} ({1}x{2}) using turbojpeg", file.getPath(), width, height); |
| 335 | final byte[] buf = new byte[(int) file.length()]; |
| 336 | int off = 0; |
| 337 | while (fis.available() > 0) { |
| 338 | off += fis.read(buf, off, Math.min(512*1024, buf.length-off)); |
| 339 | if (this.entry != ImageDisplay.this.entry) |
| 340 | return; |
| 341 | } |
| 342 | tjd.setSourceImage(buf, buf.length); |
| 343 | |
| 344 | while (width > 0 && height > 0) { |
| 345 | if (mayFitMemory(((long) width)*height*4*2)) { |
| 346 | BufferedImage bi = new BufferedImage( |
| 347 | tjd.getScaledWidth(width, height), |
| 348 | tjd.getScaledHeight(width, height), |
| 349 | BufferedImage.TYPE_INT_RGB); |
| 350 | if (this.entry != ImageDisplay.this.entry) |
| 351 | return; |
| 352 | try { |
| 353 | tjd.decompress(bi, 0); |
| 354 | width = bi.getWidth(null); |
| 355 | height = bi.getHeight(null); |
| 356 | img = bi; |
| 357 | break; |
| 358 | } catch (OutOfMemoryError oom) { |
| 359 | bi = null; |
| 360 | } |
| 361 | } |
| 362 | width = (width*4)/5; |
| 363 | height = (height*4)/5; |
| 364 | } |
| 365 | } catch (UnsatisfiedLinkError ule) { |
| 366 | Logging.warn("turbojpeg not found in {0}", System.getProperty("java.library.path")); |
| 367 | } catch (TJException e) { |
| 368 | Logging.warn("turbojpeg unusable or error decoding {0}", file.getPath()); |
| 369 | } catch (IOException e) { |
| 370 | Logging.warn("error reading file {0} for turbojpeg decoding", file.getPath()); |
| 371 | } finally { |
| 372 | try { |
| 373 | if (tjd != null) { |
| 374 | tjd.close(); |
| 375 | } |
| 376 | if (fis != null) { |
| 377 | fis.close(); |
| 378 | } |
| 379 | } catch (Exception e) { |
| 380 | e.printStackTrace(); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |