Skip to content
Browse files

Fixed transparency rgb data getting lost, addressing bug #33.

  • Loading branch information...
1 parent b20c547 commit 05b2fd6ef6317ad110ff35fc1fb9fe0bb44ead3f ismavatar committed
Showing with 25 additions and 31 deletions.
  1. +9 −11 org/lateralgm/file/GmStreamDecoder.java
  2. +16 −20 org/lateralgm/file/GmStreamEncoder.java
View
20 org/lateralgm/file/GmStreamDecoder.java
@@ -10,9 +10,8 @@
package org.lateralgm.file;
import java.awt.image.BufferedImage;
-import java.awt.image.DataBuffer;
-import java.awt.image.DataBufferByte;
-import java.awt.image.Raster;
+import java.awt.image.DataBufferInt;
+import java.awt.image.DirectColorModel;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -218,21 +217,20 @@ public BufferedImage readZlibImage() throws IOException,DataFormatException
public BufferedImage readBGRAImage(int w, int h) throws IOException
{
- WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,w,h,w * 4,4,
- new int[] { 2,1,0,3 },null); //2103 = RGBA ordering
+ DirectColorModel cm = new DirectColorModel(32,0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
+ WritableRaster raster = cm.createCompatibleWritableRaster(w,h);
- byte[] data = ((DataBufferByte) raster.getDataBuffer()).getData();
+ int[] data = ((DataBufferInt) raster.getDataBuffer()).getData();
int s = read4();
- if (s != data.length)
+ if (s != data.length * 4)
throw new IOException(Messages.format(
"GmStreamDecoder.IMAGE_SIZE_MISMATCH",s,data.length,getPosString())); //$NON-NLS-1$
- read(data);
+ for (int i = 0; i < data.length; i++)
+ data[i] = read4();
- BufferedImage dst = new BufferedImage(w,h,BufferedImage.TYPE_4BYTE_ABGR);
- dst.getRaster().setRect(raster);
- return dst;
+ return new BufferedImage(cm,raster,false,null);
}
/**
View
36 org/lateralgm/file/GmStreamEncoder.java
@@ -12,9 +12,10 @@
import static org.lateralgm.main.Util.deRef;
-import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
-import java.awt.image.ColorConvertOp;
+import java.awt.image.DataBufferInt;
+import java.awt.image.DirectColorModel;
+import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
@@ -173,15 +174,20 @@ public void endDeflate() throws IOException
public void writeZlibImage(BufferedImage image) throws IOException
{
- //Drop any alpha channel and convert to 3-byte to ensure that the image is bmp-compatible
- ColorConvertOp conv = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_sRGB),null);
- final BufferedImage dest = new BufferedImage(image.getWidth(),image.getHeight(),
- BufferedImage.TYPE_3BYTE_BGR);
- conv.filter(image,dest);
+ //original image pixels
+ int[] pixels = image.getRGB(0,0,image.getWidth(),image.getHeight(),null,0,image.getWidth());
+ //copy into 3-byte dest image with no alpha for bmp-compatible
+ DirectColorModel cm = new DirectColorModel(24,0x00FF0000,0x0000FF00,0x000000FF); //BGR
+ WritableRaster raster = cm.createCompatibleWritableRaster(image.getWidth(),image.getHeight());
+ int[] data = ((DataBufferInt) raster.getDataBuffer()).getData();
- ByteArrayOutputStream data = new ByteArrayOutputStream();
- ImageIO.write(dest,"bmp",data); //$NON-NLS-1$
- compress(data.toByteArray());
+ //assume the two buffers are the same size...
+ for (int i = 0; i < pixels.length; i++)
+ data[i] = pixels[i] & 0x00FFFFFF; //forcibly drop alpha channel
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ImageIO.write(new BufferedImage(cm,raster,false,null),"bmp",out); //$NON-NLS-1$
+ compress(out.toByteArray());
}
public void writeBGRAImage(BufferedImage image, boolean useTransp) throws IOException
@@ -205,16 +211,6 @@ public void writeBGRAImage(BufferedImage image, boolean useTransp) throws IOExce
else
write(pixels[p] >>> 24);
}
-
- /*int w = image.getWidth();
- int h = image.getHeight();
- WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,w,h,w * 4,4,
- new int[] { 2,1,0,3 },null); //2103 = RGBA ordering
- BufferedImage dst = new BufferedImage(w,h,BufferedImage.TYPE_4BYTE_ABGR);
- raster.setRect(dst.getRaster());
-
- byte[] buf = ((DataBufferByte) raster.getDataBuffer()).getData();
- out.write(buf);*/
}
/**

0 comments on commit 05b2fd6

Please sign in to comment.
Something went wrong with that request. Please try again.