Revision: 6643
          http://sourceforge.net/p/jump-pilot/code/6643
Author:   michaudm
Date:     2020-11-25 10:32:59 +0000 (Wed, 25 Nov 2020)
Log Message:
-----------
Fix related to #515 + remove duplicate code which could cause OOME

Modified Paths:
--------------
    core/trunk/src/org/openjump/core/rasterimage/RasterImageLayer.java

Modified: core/trunk/src/org/openjump/core/rasterimage/RasterImageLayer.java
===================================================================
--- core/trunk/src/org/openjump/core/rasterimage/RasterImageLayer.java  
2020-11-23 15:57:54 UTC (rev 6642)
+++ core/trunk/src/org/openjump/core/rasterimage/RasterImageLayer.java  
2020-11-25 10:32:59 UTC (rev 6643)
@@ -25,6 +25,7 @@
 import javax.media.jai.JAI;
 
 import com.vividsolutions.jump.workbench.model.Disposable;
+import org.apache.commons.imaging.Imaging;
 import org.openjump.core.ccordsys.utils.SRSInfo;
 import org.openjump.util.metaData.MetaDataMap;
 import org.openjump.util.metaData.ObjectContainingMetaInformation;
@@ -129,6 +130,7 @@
     //-- end
 
     private Metadata metadata;
+    private int bitsPerPixel = -1;
     
     private Stats stats;
     
@@ -275,7 +277,6 @@
         pb.addSource(im);
         pb.add(xScale);
         pb.add(yScale);
-        JAI.create("Scale", pb, null).getAsBufferedImage();
         // @TODO Try to replace JAI by Graphics2D after migration to 2.0
         //BufferedImage resizedImage =
         //        new BufferedImage((int)(im.getWidth()*xScale), 
(int)(im.getHeight()*yScale), im.getType());
@@ -347,11 +348,21 @@
                     
                 symbologyChanged = false;
                 this.setNeedToKeepImage(false);
+                if (bitsPerPixel == -1) {
+                    bitsPerPixel = Imaging.getImageInfo(new 
File(imageFileName)).getBitsPerPixel();
+                }
                 clearImageAndRaster(true);
-                if (getAvailRAM()-getCommittedMemory() < 
origImageWidth*origImageHeight*4 + getAvailRAM()*0.01) {
+                // Check that there is enough free memory for the image + 1% 
of available memory + 10Mb
+                if (getAvailRAM()-getCommittedMemory() <
+                        origImageWidth*origImageHeight*bitsPerPixel/8 + 
getAvailRAM()*0.01 + 10*1024*1024) {
                     layerViewPanel.getContext().warnUser("Low Memory : image " 
+
                             imageFileName + " will not be displayed");
+                    System.out.println("" + 
(getAvailRAM()-getCommittedMemory())/1024 + "kb < " +
+                            
(origImageWidth*origImageHeight*bitsPerPixel/8)/1024 + "kb " +
+                            getAvailRAM()*0.01/1024 + "kb + 10240");
                     return null;
+                } else {
+                    System.out.println("Reload image");
                 }
                 // Load the part of the image intersecting the viewport and 
setting this.image
                 reLoadImage();
@@ -368,8 +379,8 @@
                 double scaledHeight = upperLeftCornerOfImage.getY() - 
lowerRightCornerOfImage.getY();
 
                 // Apply symbology to this.image
-                setImage(stretchImageValuesForDisplay());
                 imageToDraw = stretchImageValuesForDisplay();
+                setImage(imageToDraw);
 
                 //if(getCommittedMemory() + minRamToKeepFree < availRAM){
                     //setNeedToKeepImage(true); //so small images are not 
reloaded every time
@@ -497,7 +508,7 @@
         actualImageEnvelope = 
imageAndMetadata.getMetadata().getActualEnvelope();
         originalCellSize = 
imageAndMetadata.getMetadata().getOriginalCellSize();        
         actualCellSize = imageAndMetadata.getMetadata().getActualCellSize();
-        
+
         if(image != null) {
             setImage(image);
         }



_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel

Reply via email to