9ece460dfaa7 — Quintillus tip 2 years ago
GodPaint can now export BufferedImages.

Also have a JImageWriter interface.  I might switch it up before release; in particular I'm considering switching from an export method that takes both a BufferedImage and a File, to having a constructor that takes a BufferedImage, and an export that simply takes the File to export to.
M src/main/java/com/ajtjp/jImageReader/reader/GodPaintReader.java +40 -6
@@ 1,18 1,18 @@ 
 
 package com.ajtjp.jImageReader.reader;
 
-import com.ajtjp.jImageReader.utils.LittleEndianDataInputStream;
-import com.ajtjp.jImageReader.exception.IllegalDataException;
-import com.ajtjp.jImageReader.exception.UnsupportedVariantException;
 import com.ajtjp.jImageReader.bitmap.Bitmap;
 import com.ajtjp.jImageReader.utils.BitUtils16;
+import com.ajtjp.jImageReader.utils.JImageWriter;
 import java.awt.Color;
 import java.awt.image.BufferedImage;
-import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
 import java.io.ByteArrayInputStream;
 import java.io.DataInputStream;
+import java.io.DataOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.nio.ByteOrder;
 import javax.imageio.spi.ImageReaderSpi;

          
@@ 29,7 29,7 @@ import org.apache.log4j.Logger;
  * @author Andrew
  * @since 1.2.0
  */
-public class GodPaintReader extends BaseImageReader implements JImageReader {
+public class GodPaintReader extends BaseImageReader implements JImageReader, JImageWriter {
     
     Logger logger = Logger.getLogger(GodPaintReader.class);
     

          
@@ 113,4 113,38 @@ public class GodPaintReader extends Base
             }
         }
     }
+
+    public void exportImage(BufferedImage image, String fileName) {
+        File outFile = new File(fileName);
+        DataOutputStream fos = null;
+        try {
+            fos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
+        }
+        catch(FileNotFoundException e) {
+            logger.error("File not found", e);
+        }
+        try {
+            fos.writeBytes("G4");
+            fos.writeShort(image.getWidth());
+            fos.writeShort(image.getHeight());
+            
+            
+            for (int y = 0; y < image.getHeight(); y++) {
+                for (int x = 0; x < image.getWidth(); x++) {
+                    Color c = new Color(image.getRGB(x,y));
+                    byte red = (byte)(c.getRed() >>> 3);
+                    byte green = (byte)(c.getGreen() >>> 2);
+                    byte blue = (byte)(c.getBlue() >>> 3);
+                    
+                    short rgb16 = (short)(red << 11 | green << 5 | blue);
+                    fos.writeShort(rgb16);
+                }
+            }
+            
+            fos.close();
+        }
+        catch(IOException e) {
+            logger.error("", e);
+        }
+    }
 }

          
A => src/main/java/com/ajtjp/jImageReader/utils/JImageWriter.java +20 -0
@@ 0,0 1,20 @@ 
+package com.ajtjp.jImageReader.utils;
+
+import java.awt.image.BufferedImage;
+
+/**
+ *
+ * @author Andrew
+ */
+public interface JImageWriter {
+    /**
+     * Exports a BufferedImage to a file of the format this writer corresponds to.
+     * 
+     * It will automatically choose the most convenient format, which should be
+     * documented by the implementor.
+     * @param image The image to be written.
+     * @param fileName The name of the file to which it should be written.  Should
+     * include the file extension.
+     */
+    public void exportImage(BufferedImage image, String fileName);
+}