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