Ticket #15574: josm-enable-large-jpeg-loading__if-memory-can-hold-scaled-version-only__by-using-jni-turbojpeg__r4.patch

File josm-enable-large-jpeg-loading__if-memory-can-hold-scaled-version-only__by-using-jni-turbojpeg__r4.patch, 3.5 KB (added by cmuelle8, 7 years ago)

use try-with-resources statement, patch against r13271; obsoletes first four patches of this ticket; applies in combination with josm-turbojpeg-decompressor-JNI-glue.patch

  • src/org/openstreetmap/josm/gui/layer/geoimage/ImageDisplay.java

     
    2424import java.awt.image.BufferedImage;
    2525import java.awt.image.ImageObserver;
    2626import java.io.File;
     27import java.io.FileInputStream;
     28import java.io.IOException;
    2729
    2830import javax.swing.JComponent;
    2931import javax.swing.SwingUtilities;
    3032
     33import org.libjpegturbo.turbojpeg.TJDecompressor;
     34import org.libjpegturbo.turbojpeg.TJException;
    3135import org.openstreetmap.josm.data.preferences.BooleanProperty;
    3236import org.openstreetmap.josm.data.preferences.DoubleProperty;
    3337import org.openstreetmap.josm.spi.preferences.Config;
     
    313317                img = null;
    314318            }
    315319
     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
    316368            synchronized (ImageDisplay.this) {
    317369                if (this.entry != ImageDisplay.this.entry) {
    318370                    // The file has changed