If no writer has been found with for ARGB format, retry with RGB image. 70/16170/3
Vincent COUVERT [Wed, 18 Mar 2015 09:46:41 +0000 (10:46 +0100)]
Since JoGL 2.2.4, depending on Java version and hardware configuration, writers are sometimes not available.

Change-Id: I27d06acd9fe23b4e847ca047f6f8c4abad152246

scilab/modules/graphic_export/src/java/org/scilab/modules/graphic_export/Export.java
scilab/modules/graphic_export/src/java/org/scilab/modules/graphic_export/ExportBitmap.java
scilab/modules/scirenderer/src/org/scilab/forge/scirenderer/implementation/jogl/JoGLCanvas.java

index bf38ca1..68a1cf9 100644 (file)
@@ -79,6 +79,7 @@ public class Export {
     public static final int MEMORY_ERROR = 3;
     public static final int UNKNOWN_ERROR = 4;
     public static final int FILENOTFOUND_ERROR = 5;
+    public static final int NOWRITER_ERROR = 5;
 
     private static final float DEFAULT_JPEG_COMPRESSION = 0.95f;
 
@@ -365,11 +366,21 @@ public class Export {
 
             if (joglCanvas != null) {
                 BufferedImage image = joglCanvas.getImage();
-                //joglCanvas.destroy();
                 PNGExporter exporter = (PNGExporter) getExporter(type);
                 exporter.setImage(file, image, params);
-                exporter.write();
+                int exportStatus = exporter.write();
                 exporter.dispose();
+                if (isBitmapFormat(type) && exportStatus == Export.NOWRITER_ERROR) {
+                    // If export fails because no writer was found for bitmap format
+                    // ==> Retry without Alpha channel in image
+                    // Needed after JoGL 2.2.4 version
+                    image = joglCanvas.getImage(false);
+                    exporter = (PNGExporter) getExporter(type);
+                    exporter.setImage(file, image, params);
+                    exporter.write();
+                    exporter.dispose();
+                }
+
             }
         }
     }
@@ -498,7 +509,7 @@ public class Export {
         /**
          * Write the file
          */
-        abstract void write() throws IOException;
+        abstract int write() throws IOException;
 
         abstract void dispose();
     }
@@ -531,8 +542,8 @@ public class Export {
         }
 
         @Override
-        public void write() throws IOException {
-            ExportBitmap.writeFile(image, "png", file);
+        public int write() throws IOException {
+            return ExportBitmap.writeFile(image, "png", file);
         }
 
         @Override
@@ -551,8 +562,8 @@ public class Export {
         public GIFExporter() { }
 
         @Override
-        public void write() throws IOException {
-            ExportBitmap.writeFile(image, "gif", file);
+        public int write() throws IOException {
+            return ExportBitmap.writeFile(image, "gif", file);
         }
     }
 
@@ -574,8 +585,8 @@ public class Export {
         }
 
         @Override
-        public void write() throws IOException {
-            ExportBitmap.writeFile(image, "bmp", file);
+        public int write() throws IOException {
+            return ExportBitmap.writeFile(image, "bmp", file);
         }
     }
 
@@ -587,11 +598,11 @@ public class Export {
         public JPEGExporter() { }
 
         @Override
-        public void write() throws IOException {
+        public int write() throws IOException {
             if (params.compressionQuality == -1) {
-                ExportBitmap.writeJPEG(image, DEFAULT_JPEG_COMPRESSION, file);
+                return ExportBitmap.writeJPEG(image, DEFAULT_JPEG_COMPRESSION, file);
             } else {
-                ExportBitmap.writeJPEG(image, params.compressionQuality, file);
+                return ExportBitmap.writeJPEG(image, params.compressionQuality, file);
             }
         }
     }
@@ -604,7 +615,7 @@ public class Export {
         public PPMExporter() { }
 
         @Override
-        public void write() throws IOException {
+        public int write() throws IOException {
             OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
             PPMEncoder encoder = new PPMEncoder(image, out);
             int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
@@ -612,6 +623,7 @@ public class Export {
             encoder.encodePixels(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
             out.flush();
             out.close();
+            return Export.SUCCESS;
         }
     }
 
@@ -662,13 +674,14 @@ public class Export {
         }
 
         @Override
-        public void write() throws IOException {
+        public int write() throws IOException {
             boolean useCSS = true;
             OutputStream svgs = new BufferedOutputStream(new FileOutputStream(file));
             Writer out = new OutputStreamWriter(svgs, "UTF-8");
             g2d.stream(out, useCSS);
             svgs.flush();
             svgs.close();
+            return Export.SUCCESS;
         }
 
         @Override
@@ -723,7 +736,7 @@ public class Export {
         }
 
         @Override
-        public void write() throws IOException {
+        public int write() throws IOException {
             if (g2d != null) {
                 g2d.finish();
             }
@@ -737,6 +750,7 @@ public class Export {
             if (out != null) {
                 out.close();
             }
+            return Export.SUCCESS;
         }
 
         @Override
@@ -895,7 +909,7 @@ public class Export {
         }
 
         @Override
-        public void write() throws IOException {
+        public int write() throws IOException {
             if (g2d != null) {
                 g2d.finish();
             }
@@ -909,6 +923,7 @@ public class Export {
             if (out != null) {
                 out.close();
             }
+            return Export.SUCCESS;
         }
 
         @Override
@@ -1139,7 +1154,7 @@ public class Export {
         }
 
         @Override
-        public void write() throws IOException {
+        public int write() throws IOException {
             if (g2d != null) {
                 try {
                     g2dClass.getMethod("endExport").invoke(g2d);
@@ -1161,6 +1176,7 @@ public class Export {
             if (out != null) {
                 out.close();
             }
+            return Export.SUCCESS;
         }
 
         @Override
index 3cf36b2..abbb689 100644 (file)
@@ -50,7 +50,10 @@ public class ExportBitmap {
             for (int i = 0; i < MAX_ATTEMPT && ret != Export.SUCCESS; i++) {
                 try {
                     fos = new FileOutputStream(file);
-                    ImageIO.write(image, ext, fos);
+                    boolean writerFound = ImageIO.write(image, ext, fos);
+                    if (writerFound == false) {
+                        return Export.NOWRITER_ERROR;
+                    }
                     fos.close();
                     ret = Export.SUCCESS;
                 } catch (FileNotFoundException e) {
@@ -71,22 +74,26 @@ public class ExportBitmap {
      * @param compressionQuality the compression rate
      * @param file the output file
      */
-    public static boolean writeJPEG(BufferedImage image, float compressionQuality, File file) throws IOException {
+    public static int writeJPEG(BufferedImage image, float compressionQuality, File file) throws IOException {
         Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
         ImageWriter writer;
         if (iter.hasNext()) {
             writer = (ImageWriter) iter.next();
         } else {
-            return false;
+            return Export.NOWRITER_ERROR;
         }
         ImageWriteParam param = writer.getDefaultWriteParam();
         param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
         param.setCompressionQuality(compressionQuality);
         FileImageOutputStream output = new FileImageOutputStream(file);
         writer.setOutput(output);
-        writer.write(null, new IIOImage(image, null, null), param);
+        try {
+            writer.write(null, new IIOImage(image, null, null), param);
+        } catch (IOException e) {
+            return Export.NOWRITER_ERROR;
+        }
         writer.dispose();
         output.close();
-        return true;
+        return Export.SUCCESS;
     }
 }
index 169067f..f048402 100644 (file)
 
 package org.scilab.forge.scirenderer.implementation.jogl;
 
-import org.scilab.forge.scirenderer.Canvas;
-import org.scilab.forge.scirenderer.Drawer;
-import org.scilab.forge.scirenderer.implementation.jogl.buffers.JoGLBuffersManager;
-import org.scilab.forge.scirenderer.implementation.jogl.picking.JoGLPickingManager;
-import org.scilab.forge.scirenderer.implementation.jogl.renderer.JoGLRendererManager;
-import org.scilab.forge.scirenderer.implementation.jogl.texture.JoGLTextureManager;
-import org.scilab.forge.scirenderer.picking.PickingManager;
-
-import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil;
-import com.jogamp.opengl.util.awt.ImageUtil;
-
 import java.awt.Dimension;
 import java.awt.image.BufferedImage;
 import java.lang.reflect.InvocationTargetException;
@@ -39,6 +28,16 @@ import javax.media.opengl.GLOffscreenAutoDrawable;
 import javax.media.opengl.GLProfile;
 import javax.swing.SwingUtilities;
 
+import org.scilab.forge.scirenderer.Canvas;
+import org.scilab.forge.scirenderer.Drawer;
+import org.scilab.forge.scirenderer.implementation.jogl.buffers.JoGLBuffersManager;
+import org.scilab.forge.scirenderer.implementation.jogl.picking.JoGLPickingManager;
+import org.scilab.forge.scirenderer.implementation.jogl.renderer.JoGLRendererManager;
+import org.scilab.forge.scirenderer.implementation.jogl.texture.JoGLTextureManager;
+import org.scilab.forge.scirenderer.picking.PickingManager;
+
+import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil;
+
 /**
  * JoGL implementation of a Canvas.
  *
@@ -233,6 +232,14 @@ public final class JoGLCanvas implements Canvas, GLEventListener {
      * @return an image
      */
     public BufferedImage getImage() {
+        return getImage(true);
+    }
+
+    /**
+     * Get an image from the autoDrawable
+     * @return an image
+     */
+    public BufferedImage getImage(final boolean alpha) {
         while (!canvasAnimator.isDrawFinished() || !displayFinished) {
             try {
                 Thread.sleep(10);
@@ -246,7 +253,7 @@ public final class JoGLCanvas implements Canvas, GLEventListener {
 
         if (SwingUtilities.isEventDispatchThread()) {
             context.makeCurrent();
-            AWTGLReadBufferUtil buffer = new AWTGLReadBufferUtil(GLProfile.getDefault(), true);
+            AWTGLReadBufferUtil buffer = new AWTGLReadBufferUtil(GLProfile.getDefault(), alpha);
             image[0] = buffer.readPixelsToBufferedImage(getGl(), 0, 0, autoDrawable.getSurfaceWidth(), autoDrawable.getSurfaceHeight(), true);
             context.release();
         } else {
@@ -254,7 +261,7 @@ public final class JoGLCanvas implements Canvas, GLEventListener {
                 SwingUtilities.invokeAndWait(new Runnable() {
                     public void run() {
                         context.makeCurrent();
-                        AWTGLReadBufferUtil buffer = new AWTGLReadBufferUtil(GLProfile.getDefault(), true);
+                        AWTGLReadBufferUtil buffer = new AWTGLReadBufferUtil(GLProfile.getDefault(), alpha);
                         image[0] = buffer.readPixelsToBufferedImage(getGl(), 0, 0, autoDrawable.getSurfaceWidth(), autoDrawable.getSurfaceHeight(), true);
                         context.release();
                     }