diff options
Diffstat (limited to 'libjava/classpath/gnu/javax/imageio/bmp')
25 files changed, 3550 insertions, 0 deletions
diff --git a/libjava/classpath/gnu/javax/imageio/bmp/BMPDecoder.java b/libjava/classpath/gnu/javax/imageio/bmp/BMPDecoder.java new file mode 100644 index 000000000..108461931 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/BMPDecoder.java @@ -0,0 +1,168 @@ +/* BMPDecoder.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.imageio.bmp; + +import java.io.IOException; +import javax.imageio.stream.ImageInputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.awt.image.IndexColorModel; +import java.awt.image.BufferedImage; + +public abstract class BMPDecoder { + + protected BMPInfoHeader infoHeader; + protected BMPFileHeader fileHeader; + protected long offset; + + public BMPDecoder(BMPFileHeader fh, BMPInfoHeader ih){ + fileHeader = fh; + infoHeader = ih; + offset = BMPFileHeader.SIZE + BMPInfoHeader.SIZE; + } + + /** + * Determines the coding type of the bitmap and returns the corresponding + * decoder. + */ + public static BMPDecoder getDecoder(BMPFileHeader fh, BMPInfoHeader ih){ + switch(ih.getCompression()){ + case BMPInfoHeader.BI_RGB: // uncompressed RGB + switch(ih.getBitCount()){ + case 32: + return new DecodeBF32(fh, ih, true); + + case 24: + return new DecodeRGB24(fh, ih); + + case 16: + return new DecodeBF16(fh, ih, true); + + case 8: + return new DecodeRGB8(fh, ih); + + case 4: + return new DecodeRGB4(fh, ih); + + case 1: + return new DecodeRGB1(fh, ih); + + default: + return null; + } + + case BMPInfoHeader.BI_RLE8: + return new DecodeRLE8(fh, ih); + + case BMPInfoHeader.BI_RLE4: + return new DecodeRLE4(fh, ih); + + case BMPInfoHeader.BI_BITFIELDS: + switch(ih.getBitCount()){ + case 16: + return new DecodeBF16(fh, ih, false); + + case 32: + return new DecodeBF32(fh, ih, false); + + default: + return null; + } + + default: + return null; + } + } + + /** + * The image decoder. + */ + public abstract BufferedImage decode(ImageInputStream in) + throws IOException, BMPException; + + /** + * Reads r,g,b bit masks from an inputstream + */ + protected int[] readBitMasks(ImageInputStream in) throws IOException { + int[] bitmasks = new int[3]; + byte[] temp = new byte[12]; + if(in.read(temp) != 12) + throw new IOException("Couldn't read bit masks."); + offset += 12; + + ByteBuffer buf = ByteBuffer.wrap(temp); + buf.order(ByteOrder.LITTLE_ENDIAN); + bitmasks[0] = buf.getInt(); + bitmasks[1] = buf.getInt(); + bitmasks[2] = buf.getInt(); + return bitmasks; + } + + /** + * Reads an N-color palette from an inputstream in RGBQUAD format and + * returns an equivalent ColorModel object + */ + protected IndexColorModel readPalette(ImageInputStream in) throws IOException { + int N = infoHeader.getNumberOfPaletteEntries(); + byte[] r = new byte[N]; + byte[] g = new byte[N]; + byte[] b = new byte[N]; + for(int i=0;i<N;i++){ + byte[] RGBquad = new byte[4]; + if(in.read(RGBquad) != 4) + throw new IOException("Error reading palette information."); + // RGBQUAD structure is b,g,r,0 + r[i] = RGBquad[2]; + g[i] = RGBquad[1]; + b[i] = RGBquad[0]; + } + + offset += 4*N; + return new IndexColorModel(8, N, r, g, b); + } + + /** + * Read bytes to the start of the image data + */ + protected void skipToImage(ImageInputStream in) throws IOException { + byte[] d = new byte[1]; + long n = fileHeader.getOffset() - offset; + for(int i=0;i<n;i++) + in.read(d); + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/BMPEncoder.java b/libjava/classpath/gnu/javax/imageio/bmp/BMPEncoder.java new file mode 100644 index 000000000..d7c54c0d6 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/BMPEncoder.java @@ -0,0 +1,119 @@ +/* BMPEncoder.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.imageio.bmp; + +import java.io.IOException; + +import javax.imageio.IIOImage; +import javax.imageio.ImageWriteParam; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.stream.ImageOutputStream; + +public abstract class BMPEncoder +{ + + /** + * Constructs a new BMPEncoder. + */ + public BMPEncoder() + { + // Nothing to do here. + } + + /** + * Determines the coding type of the bitmap and returns the corresponding + * encoder. + * + * @param fh - the file header + * @param ih - the info header + * @return the appropriate encoder + */ + public static BMPEncoder getEncoder(BMPFileHeader fh, BMPInfoHeader ih) + { + switch (ih.getCompression()) + { + case BMPInfoHeader.BI_RGB: + switch (ih.getBitCount()) + { + case 32: + return new EncodeRGB32(fh, ih); + + case 24: + return new EncodeRGB24(fh, ih); + + case 16: + return new EncodeRGB16(fh, ih); + + case 8: + return new EncodeRGB8(fh, ih); + + case 4: + return new EncodeRGB4(fh, ih); + + case 1: + return new EncodeRGB1(fh, ih); + + default: + return null; + } + case BMPInfoHeader.BI_RLE4: + return new EncodeRLE4(fh, ih); + + case BMPInfoHeader.BI_RLE8: + return new EncodeRLE8(fh, ih); + default: + return null; + } + } + + /** + * The image encoder. + * + * @param o - the image output stream + * @param streamMetadata - metadata associated with this stream, or + * null + * @param image - an IIOImage containing image data, metadata and + * thumbnails to be written + * @param param - image writing parameters, or null + * @exception IOException if a write error occurs + */ + public abstract void encode(ImageOutputStream o, IIOMetadata streamMetadata, + IIOImage image, ImageWriteParam param) + throws IOException; +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/BMPException.java b/libjava/classpath/gnu/javax/imageio/bmp/BMPException.java new file mode 100644 index 000000000..0ba3c6c74 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/BMPException.java @@ -0,0 +1,47 @@ +/* BMPException.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.imageio.bmp; + +import javax.imageio.IIOException; + +public class BMPException extends IIOException { + + public BMPException(String message){ + super(message); + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/BMPFileHeader.java b/libjava/classpath/gnu/javax/imageio/bmp/BMPFileHeader.java new file mode 100644 index 000000000..0cfd72323 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/BMPFileHeader.java @@ -0,0 +1,151 @@ +/* BMPFileHeader.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.imageio.bmp; + +import java.awt.image.RenderedImage; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +import javax.imageio.IIOImage; +import javax.imageio.stream.ImageInputStream; +import javax.imageio.stream.ImageOutputStream; + +public class BMPFileHeader { + /** Header signature, always 'BM' */ + private final static short bfType = 0x424d; + + /** Bitmap file size, in bytes. */ + protected long bfSize; + + /** Offset from the beginning of the file to the bitmap data */ + protected long bfOffBits; + + /** BITMAPFILEHEADER is 14 bytes */ + public static final int SIZE = 14; + private static final int BITMAPINFOHEADER_SIZE = 40; + + /** + * Creates the header from an input stream, which is not closed. + * + * @throws IOException if an I/O error occured. + * @throws BMPException if the header was invalid + */ + public BMPFileHeader(ImageInputStream in) throws IOException, BMPException { + byte[] data = new byte[SIZE]; + + if (in.read(data) != SIZE) + throw new IOException("Couldn't read header."); + ByteBuffer buf = ByteBuffer.wrap(data); + + if(buf.getShort(0) != bfType) + throw new BMPException("Not a BMP file."); + + buf.order(ByteOrder.LITTLE_ENDIAN); + + // get size (keep unsigned) + bfSize = ((long)buf.getInt(2) & (0xFFFFFFFF)); + + // Two reserved shorts are here, and should be zero, + // perhaps they should be tested to be zero, but I don't + // feel this strictness is necessary. + + bfOffBits = ((long)buf.getInt(10) & (0xFFFFFFFF)); + } + + /** + * Creates the header from an output stream, which is not closed. + * + * @param out - the image output stream + * @param im - the image + * @throws IOException if an I/O error occured. + */ + public BMPFileHeader(ImageOutputStream out, IIOImage im) throws IOException + { + RenderedImage img = im.getRenderedImage(); + int w = img.getWidth(); + int h = img.getHeight(); + + bfOffBits = SIZE + BITMAPINFOHEADER_SIZE; + bfSize = ((w * h) * 3) + ((4 - ((w * 3) % 4)) * h) + bfOffBits; + + write(out); + } + + /** + * Writes the header to an output stream, which is not closed or flushed. + * + * @throws IOException if an I/O error occured. + */ + public void write(ImageOutputStream out) throws IOException { + ByteBuffer buf = ByteBuffer.allocate(SIZE); + buf.putShort(0, bfType); // ID + buf.putInt(2, (int)(bfSize & (0xFFFFFFFF))); // size + buf.putInt(6, 0); // 4 reserved bytes set to zero + buf.putInt(7, (int)(bfOffBits & (0xFFFFFFFF))); // size + out.write(buf.array()); + } + + /** + * Sets the file size + */ + public void setSize(long size){ + bfSize = size; + } + + /** + * Sets the bitmap offset within the file + */ + public void setOffset(long offset){ + bfOffBits = offset; + } + + /** + * Gets the file size + */ + public long getSize(){ + return bfSize; + } + + /** + * Gets the bitmap offset within the file + */ + public long getOffset(){ + return bfOffBits; + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/BMPImageReader.java b/libjava/classpath/gnu/javax/imageio/bmp/BMPImageReader.java new file mode 100644 index 000000000..7b136bdf5 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/BMPImageReader.java @@ -0,0 +1,148 @@ +/* BMPImageReader.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.imageio.bmp; + +import java.io.IOException; +import javax.imageio.*; +import javax.imageio.spi.*; +import javax.imageio.metadata.*; +import javax.imageio.stream.ImageInputStream; +import java.util.Iterator; +import java.awt.image.BufferedImage; + +public class BMPImageReader extends ImageReader { + private BMPInfoHeader infoHeader; + private BMPFileHeader fileHeader; + private BMPDecoder decoder; + + protected BMPImageReader(ImageReaderSpi originatingProvider){ + super(originatingProvider); + infoHeader = null; + fileHeader = null; + decoder = null; + } + + private void validateIndex(int imageIndex) + throws IndexOutOfBoundsException { + if (imageIndex != 0) + throw new IndexOutOfBoundsException("Invalid image index."); + } + + public void setInput(Object input) { + super.setInput(input); + } + + public void setInput(Object input, + boolean seekForwardOnly, + boolean ignoreMetadata) { + super.setInput(input, seekForwardOnly, ignoreMetadata); + } + + public void setInput(Object input, boolean isStreamable) { + super.setInput(input, isStreamable); + + if (!(input instanceof ImageInputStream)) + throw new IllegalArgumentException("Input not an ImageInputStream."); + } + + private void checkStream() throws IOException { + if (!(input instanceof ImageInputStream)) + throw new IllegalStateException("Input not an ImageInputStream."); + if(input == null) + throw new IllegalStateException("No input stream."); + + } + + private void readHeaders() throws IOException, IIOException { + if(fileHeader != null) + return; + + checkStream(); + + fileHeader = new BMPFileHeader((ImageInputStream)input); + infoHeader = new BMPInfoHeader((ImageInputStream)input); + decoder = BMPDecoder.getDecoder(fileHeader, infoHeader); + } + + public int getWidth(int imageIndex) throws IOException { + validateIndex(imageIndex); + readHeaders(); + return infoHeader.getWidth(); + } + + public int getHeight(int imageIndex) throws IOException { + validateIndex(imageIndex); + readHeaders(); + return infoHeader.getHeight(); + } + + public Iterator getImageTypes(int imageIndex){ + validateIndex(imageIndex); + return null; + } + + /** + * Returns the number of images. BMP files can only contain a single one. + */ + public int getNumImages(boolean allowSearch){ + return 1; + } + + + // FIXME: Support metadata + public IIOMetadata getImageMetadata(int imageIndex){ + validateIndex(imageIndex); + return null; + } + + // FIXME: Support metadata + public IIOMetadata getStreamMetadata(){ + return null; + } + + /** + * Reads the image indexed by imageIndex and returns it as + * a complete BufferedImage, using a supplied ImageReadParam. + */ + public BufferedImage read(int imageIndex, ImageReadParam param) + throws IOException, IIOException { + validateIndex(imageIndex); + readHeaders(); + return decoder.decode((ImageInputStream)input); + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/BMPImageReaderSpi.java b/libjava/classpath/gnu/javax/imageio/bmp/BMPImageReaderSpi.java new file mode 100644 index 000000000..5d027963a --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/BMPImageReaderSpi.java @@ -0,0 +1,116 @@ +/* BMPImageReaderSpi.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.imageio.bmp; + +import java.io.IOException; +import java.util.Locale; +import javax.imageio.ImageReader; +import javax.imageio.spi.ImageReaderSpi; +import javax.imageio.stream.ImageInputStream; + +public class BMPImageReaderSpi extends ImageReaderSpi { + + static final String vendorName = "GNU"; + static final String version = "0.1"; + static final String readerClassName = + "gnu.javax.imageio.bmp.BMPImageReader"; + static final String[] names = { "Microsoft Windows BMP" }; + static final String[] suffixes = { ".bmp", ".bm" }; + static final String[] MIMETypes = { + "image/bmp", + "image/x-windows-bmp"}; + static final String[] writerSpiNames = + { "gnu.javax.imageio.bmp.BMPImageWriterSpi" }; + + static final boolean supportsStandardStreamMetadataFormat = false; + static final String nativeStreamMetadataFormatName = null; + static final String nativeStreamMetadataFormatClassName = null; + static final String[] extraStreamMetadataFormatNames = null; + static final String[] extraStreamMetadataFormatClassNames = null; + static final boolean supportsStandardImageMetadataFormat = false; + static final String nativeImageMetadataFormatName = null; + static final String nativeImageMetadataFormatClassName = null; + static final String[] extraImageMetadataFormatNames = null; + static final String[] extraImageMetadataFormatClassNames = null; + + public BMPImageReaderSpi() { + super(vendorName, version, + names, suffixes, MIMETypes, + readerClassName, + STANDARD_INPUT_TYPE, // Accept ImageInputStreams + writerSpiNames, + supportsStandardStreamMetadataFormat, + nativeStreamMetadataFormatName, + nativeStreamMetadataFormatClassName, + extraStreamMetadataFormatNames, + extraStreamMetadataFormatClassNames, + supportsStandardImageMetadataFormat, + nativeImageMetadataFormatName, + nativeImageMetadataFormatClassName, + extraImageMetadataFormatNames, + extraImageMetadataFormatClassNames); + } + + public String getDescription(Locale locale) { + return "Microsoft BMP v3"; + } + + public boolean canDecodeInput(Object input) + throws IOException { + if (!(input instanceof ImageInputStream)) + return false; + + ImageInputStream in = (ImageInputStream)input; + boolean retval; + + in.mark(); + try { + new BMPFileHeader(in); + retval = true; + } catch(BMPException e){ + retval = false; + } + in.reset(); + + return retval; + } + + public ImageReader createReaderInstance(Object extension) { + return new BMPImageReader(this); + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/BMPImageWriter.java b/libjava/classpath/gnu/javax/imageio/bmp/BMPImageWriter.java new file mode 100644 index 000000000..407e66a72 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/BMPImageWriter.java @@ -0,0 +1,195 @@ +/* BMPImageWriter.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.imageio.bmp; + +import java.io.IOException; + +import javax.imageio.IIOImage; +import javax.imageio.ImageTypeSpecifier; +import javax.imageio.ImageWriteParam; +import javax.imageio.ImageWriter; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.spi.ImageWriterSpi; +import javax.imageio.stream.ImageOutputStream; + +public class BMPImageWriter + extends ImageWriter +{ + protected BMPEncoder encoder; + protected BMPFileHeader fileHeader; + protected BMPInfoHeader infoHeader; + + /** + * Construct an bmp image writer. + * + * @param originatingProvider - the provider that is constructing this image + * writer, or null + */ + protected BMPImageWriter(ImageWriterSpi originatingProvider) + { + super(originatingProvider); + encoder = null; + fileHeader = null; + infoHeader = null; + } + + /** + * Convert IIOMetadata from an input reader format, returning an IIOMetadata + * suitable for use by an image writer. The ImageTypeSpecifier specifies the + * destination image type. An optional ImageWriteParam argument is available + * in case the image writing parameters affect the metadata conversion. + * + * @param inData - the metadata coming from an image reader + * @param imageType - the output image type of the writer + * @param param - the image writing parameters or null + * @return the converted metadata that should be used by the image writer, or + * null if this ImageTranscoder has no knowledge of the input metadata + * @exception IllegalArgumentException if either inData or imageType is null + */ + public IIOMetadata convertImageMetadata(IIOMetadata inData, + ImageTypeSpecifier imageType, + ImageWriteParam param) + { + // FIXME: Support metadata. + if (inData == null || imageType == null) + throw new IllegalArgumentException("IIOMetadata and ImageTypeSpecifier cannot be null."); + return null; + } + + /** + * Convert IIOMetadata from an input stream format, returning an + * IIOMetadata suitable for use by an image writer. + * + * An optional ImageWriteParam argument is available in case the + * image writing parameters affect the metadata conversion. + * + * @param inData - the metadata coming from an input image stream + * @param param - the image writing parameters or null + * @return the converted metadata that should be used by the image + * writer, or null if this ImageTranscoder has no knowledge of the + * input metadata + * + * @exception IllegalArgumentException if inData is null + */ + public IIOMetadata convertStreamMetadata (IIOMetadata inData, + ImageWriteParam param) + { + // FIXME: Support metadata. + if (inData == null) + throw new IllegalArgumentException("IIOMetadata cannot be null."); + return null; + } + + /** + * Get a metadata object appropriate for encoding an image specified + * by the given image type specifier and optional image write + * parameters. + * + * @param imageType - an image type specifier + * @param param - image writing parameters, or null + * @return a metadata object appropriate for encoding an image of + * the given type with the given parameters + */ + public IIOMetadata getDefaultImageMetadata (ImageTypeSpecifier imageType, ImageWriteParam param) + { + // FIXME: Support metadata. + return null; + } + + /** + * Get a metadata object appropriate for encoding the default image + * type handled by this writer, optionally considering image write + * parameters. + * + * @param param - image writing parameters, or null + * @return a metadata object appropriate for encoding an image of + * the default type with the given parameters + */ + public IIOMetadata getDefaultStreamMetadata (ImageWriteParam param) + { + // FIXME: Support metadata. + return null; + } + + /** + * Write an image stream, including thumbnails and metadata to the + * output stream. The output must have been set prior to this + * method being called. Metadata associated with the stream may be + * supplied, or it can be left null. IIOImage may contain raster + * data if this writer supports rasters, or it will contain a + * rendered image. Thumbnails are resized if need be. Image + * writing parameters may be specified to affect writing, or may be + * left null. + * + * @param streamMetadata - metadata associated with this stream, or + * null + * @param image - an IIOImage containing image data, metadata and + * thumbnails to be written + * @param param - image writing parameters, or null + * @exception IOException if a write error occurs + * @throws BMPException if the encoder has not been initialized. + */ + public void write(IIOMetadata streamMetadata, IIOImage image, + ImageWriteParam param) throws IOException, BMPException + { + checkStream(); + ImageOutputStream out = (ImageOutputStream) output; + fileHeader = new BMPFileHeader(out, image); + infoHeader = new BMPInfoHeader(out, image, param); + encoder = BMPEncoder.getEncoder(fileHeader, infoHeader); + + if (encoder != null) + encoder.encode(out, streamMetadata, image, param); + else + throw new BMPException("Encoder has not been initialized."); + } + + /** + * Checks the output stream. + * + * @throws IOException if there is an error with the output stream + */ + private void checkStream() throws IOException + { + if (!(output instanceof ImageOutputStream)) + throw new IllegalStateException("Output not an ImageOutputStream."); + if (output == null) + throw new IllegalStateException("No output stream."); + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/BMPImageWriterSpi.java b/libjava/classpath/gnu/javax/imageio/bmp/BMPImageWriterSpi.java new file mode 100644 index 000000000..e158dcfd7 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/BMPImageWriterSpi.java @@ -0,0 +1,148 @@ +/* BMPImageWriterSpi.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.imageio.bmp; + +import java.util.Locale; + +import javax.imageio.ImageTypeSpecifier; +import javax.imageio.ImageWriter; +import javax.imageio.spi.ImageWriterSpi; + +public class BMPImageWriterSpi + extends ImageWriterSpi +{ + + static final String vendorName = "GNU"; + static final String version = "0.1"; + static final String writerClassName = "gnu.javax.imageio.bmp.BMPImageWriter"; + static final String[] names = { "bmp", "BMP", "Microsoft Windows BMP" }; + static final String[] suffixes = { ".bmp", ".bm" }; + static final String[] MIMETypes = { "image/bmp", "image/x-windows-bmp" }; + static final String[] readerSpiNames = { "gnu.javax.imageio.bmp.BMPImageReaderSpi" }; + + static final boolean supportsStandardStreamMetadataFormat = false; + static final String nativeStreamMetadataFormatName = null; + static final String nativeStreamMetadataFormatClassName = null; + static final String[] extraStreamMetadataFormatNames = null; + static final String[] extraStreamMetadataFormatClassNames = null; + static final boolean supportsStandardImageMetadataFormat = false; + static final String nativeImageMetadataFormatName = null; + static final String nativeImageMetadataFormatClassName = null; + static final String[] extraImageMetadataFormatNames = null; + static final String[] extraImageMetadataFormatClassNames = null; + + private BMPImageWriter writerInstance; + + public BMPImageWriterSpi() + { + super(vendorName, version, names, suffixes, MIMETypes, writerClassName, + STANDARD_OUTPUT_TYPE, readerSpiNames, supportsStandardStreamMetadataFormat, + nativeStreamMetadataFormatName, nativeStreamMetadataFormatClassName, + extraStreamMetadataFormatNames, extraStreamMetadataFormatClassNames, + supportsStandardImageMetadataFormat, nativeImageMetadataFormatName, + nativeImageMetadataFormatClassName, extraImageMetadataFormatNames, + extraImageMetadataFormatClassNames); + } + + /** + * Returns true if the image can be encoded. + * + * @param type - the image type specifier. + * @return true if image can be encoded, otherwise false. + */ + public boolean canEncodeImage(ImageTypeSpecifier type) + { + if (type == null) + return false; + + BMPInfoHeader ih = writerInstance.infoHeader; + if (ih != null) + { + int compressionType = ih.getCompression(); + int bytes = type.getColorModel().getPixelSize(); + if ((compressionType == BMPInfoHeader.BI_RLE4 && (bytes != 4 || bytes != 8)) + || (compressionType == BMPInfoHeader.BI_RGB && ((bytes != 1 + || bytes != 4 + || bytes != 8 + || bytes != 16 + || bytes != 24 + || bytes != 32)))) + return false; + } + return true; + } + + /** + * Creates an instance of ImageWriter using the given extension. + * + * @param extension - the provider that is constructing this image writer, or + * null + */ + public ImageWriter createWriterInstance(Object extension) + { + if (extension != null && extension instanceof ImageWriterSpi) + writerInstance = new BMPImageWriter((ImageWriterSpi) extension); + else + writerInstance = new BMPImageWriter(this); + return writerInstance; + } + + /** + * Gets the instance of ImageWriter, if already created. + */ + public BMPImageWriter getWriterInstance() + { + if (writerInstance != null) + return writerInstance; + return (BMPImageWriter) createWriterInstance(null); + } + + /** + * Returns a short description of this service provider that can be + * presented to a human user. + * + * @param locale - the locale for which the description string should + * be localized. + */ + public String getDescription(Locale locale) + { + return "Microsoft BMP v3"; + } + +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/BMPInfoHeader.java b/libjava/classpath/gnu/javax/imageio/bmp/BMPInfoHeader.java new file mode 100644 index 000000000..e14afdb04 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/BMPInfoHeader.java @@ -0,0 +1,317 @@ +/* BMPInfoHeader.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.imageio.bmp; + +import java.awt.Dimension; +import java.awt.image.ColorModel; +import java.awt.image.RenderedImage; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +import javax.imageio.IIOImage; +import javax.imageio.ImageWriteParam; +import javax.imageio.stream.ImageInputStream; +import javax.imageio.stream.ImageOutputStream; + +public class BMPInfoHeader +{ + /** Size of the bitmap info header */ + protected int biSize; + + /** Pixel width of the bitmap */ + protected int biWidth; + + /** Pixel height of the bitmap */ + protected int biHeight; + + /** Number of bitplanes = 1 */ + protected short biPlanes; + + /** Number of bpp = 1,4,8,24 */ + protected short biBitCount; + + /** Compression type, RGB8, RLE8, RLE4, BITFIELDS */ + protected int biCompression; + + /** Byte size of the uncompressed bitmap, can be 0. */ + protected int biSizeImage; + + /** X resolution, dots per meter */ + protected int biXPelsPerMeter; + + /** Y resolution, dots per meter */ + protected int biYPelsPerMeter; + + /** Number of colors used (palette only, can be 0 for all) */ + protected int biClrUsed; + + /** Number of 'important' colors, 0 for all */ + protected int biClrImportant; + + /** BITMAPINFOHEADER is 40 bytes */ + public static final int SIZE = 40; + + /** + * Compression types + */ + public static final int BI_RGB = 0; + public static final int BI_RLE8 = 1; + public static final int BI_RLE4 = 2; + public static final int BI_BITFIELDS = 3; + + /** + * Creates the header from an input stream, which is not closed. + * + * @param in - the image input stream + * @throws IOException if an I/O error occured. + * @throws BMPException if the header was invalid + */ + public BMPInfoHeader(ImageInputStream in) throws IOException, BMPException + { + byte[] data = new byte[SIZE]; + + if (in.read(data) != SIZE) + throw new IOException("Couldn't read header."); + ByteBuffer buf = ByteBuffer.wrap(data); + buf.order(ByteOrder.LITTLE_ENDIAN); + + int n; + if ((n = buf.getInt()) != SIZE) + throw new BMPException("Invalid BITMAPINFOHEADER size: " + n); + + biWidth = buf.getInt(); + biHeight = buf.getInt(); + biPlanes = buf.getShort(); + setBitCount(buf.getShort()); + setCompression(buf.getInt()); + biSizeImage = buf.getInt(); + biXPelsPerMeter = buf.getInt(); + biYPelsPerMeter = buf.getInt(); + biClrUsed = buf.getInt(); + biClrImportant = buf.getInt(); + } + + /** + * Creates the info header from an output stream, which is not closed. + * + * @param out - the image output stream + * @param im - the image + * @param param - the image write param. + * @throws IOException if an I/O error occured. + */ + public BMPInfoHeader(ImageOutputStream out, IIOImage im, ImageWriteParam param) throws IOException + { + RenderedImage img = im.getRenderedImage(); + ColorModel cMod = img.getColorModel(); + + biSize = SIZE; + biWidth = img.getWidth(); + biHeight = img.getHeight(); + biPlanes = 1; + + if (param != null && param.canWriteCompressed()) + { + String compType = param.getCompressionType(); + if (compType.equals("BI_RLE8")) + { + biCompression = BI_RLE8; + biBitCount = 8; + } + else if (compType.equals("BI_RLE4")) + { + biCompression = BI_RLE4; + biBitCount = 4; + } + else + { + biCompression = BI_RGB; + biBitCount = (short) cMod.getPixelSize(); + } + } + else + { + biBitCount = (short) cMod.getPixelSize(); + biCompression = BI_RGB; + } + + biXPelsPerMeter = 0x0; + biYPelsPerMeter = 0x0; + biClrUsed = 0; + biClrImportant = 0; + biSizeImage = ((biWidth * biHeight) * 3) + + ((4 - ((biWidth * 3) % 4)) * biHeight); + out.write(intToDWord(biSize)); + out.write(intToDWord(biWidth)); + out.write(intToDWord(biHeight)); + out.write(intToWord(biPlanes)); + out.write(intToWord(biBitCount)); + out.write(intToDWord(biCompression)); + out.write(intToDWord(biSizeImage)); + out.write(intToDWord(biXPelsPerMeter)); + out.write(intToDWord(biYPelsPerMeter)); + out.write(intToDWord(biClrUsed)); + out.write(intToDWord(biClrImportant)); + } + + /** + * Converts an int to a word, where the return value is stored in a + * 2-byte array. + * + * @param val - the value to convert + * @return the array + */ + private byte[] intToWord(int val) + { + byte b[] = new byte[2]; + b[0] = (byte) (val & 0x00FF); + b[1] = (byte) ((val >> 8) & 0x00FF); + return b; + } + + /** + * Converts an int to a double word, where the return value is + * stored in a 4-byte array. + * + * @param val - the value to convert + * @return the array + */ + private byte[] intToDWord(int val) + { + byte b[] = new byte[4]; + b[0] = (byte) (val & 0x00FF); + b[1] = (byte) ((val >> 8) & 0x000000FF); + b[2] = (byte) ((val >> 16) & 0x000000FF); + b[3] = (byte) ((val >> 24) & 0x000000FF); + return b; + } + + + public void setBitCount(short bitcount) throws BMPException + { + switch (bitcount) + { + case 1: + case 4: + case 8: + case 16: + case 24: + case 32: + biBitCount = bitcount; + break; + + default: + throw new BMPException("Invalid number of bits per pixel: " + bitcount); + } + } + + public short getBitCount() + { + return biBitCount; + } + + public void setCompression(int compression) throws BMPException + { + switch (compression) + { + case BI_RLE8: + if (getBitCount() != 8) + throw new BMPException("Invalid number of bits per pixel."); + biCompression = compression; + break; + case BI_RLE4: + if (getBitCount() != 4) + throw new BMPException("Invalid number of bits per pixel."); + biCompression = compression; + break; + + case BI_RGB: + case BI_BITFIELDS: + biCompression = compression; + break; + + default: + throw new BMPException("Unknown bitmap compression type."); + } + } + + public int getNumberOfPaletteEntries() + { + if (biClrUsed == 0) + switch (biBitCount) + { + case 1: + return 2; + case 4: + return 16; + case 8: + return 256; + + default: // should not happen + return 0; + } + + return biClrUsed; + } + + public int getCompression() + { + return biCompression; + } + + public Dimension getSize() + { + return new Dimension(biWidth, biHeight); + } + + public int getWidth() + { + return biWidth; + } + + public int getHeight() + { + return biHeight; + } + + public void setSize(Dimension d) + { + biWidth = (int) d.getWidth(); + biHeight = (int) d.getHeight(); + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/DecodeBF16.java b/libjava/classpath/gnu/javax/imageio/bmp/DecodeBF16.java new file mode 100644 index 000000000..2f94ac613 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/DecodeBF16.java @@ -0,0 +1,99 @@ +/* DecodeBF16.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.imageio.bmp; + +import java.io.IOException; +import javax.imageio.stream.ImageInputStream; +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.awt.image.DirectColorModel; +import java.awt.image.Raster; +import java.awt.image.WritableRaster; +import java.awt.image.DataBuffer; +import java.awt.image.DataBufferUShort; +import java.awt.image.SinglePixelPackedSampleModel; +import java.awt.image.SampleModel; +import java.awt.Dimension; + +public class DecodeBF16 extends BMPDecoder { + private int[] bitmasks; + private boolean useDefaultMasks; + + public DecodeBF16(BMPFileHeader fh, BMPInfoHeader ih, + boolean udm){ + super(fh,ih); + + useDefaultMasks = udm; + if(useDefaultMasks) // 5-6-5 mask, B,G,R + bitmasks = new int[] { 0x00F800, 0x0007E0, 0x00001F }; + } + + public BufferedImage decode(ImageInputStream in) throws IOException, BMPException { + if(!useDefaultMasks) + bitmasks = readBitMasks(in); + skipToImage(in); + + Dimension d = infoHeader.getSize(); + int h = (int)d.getHeight(); + int w = (int)d.getWidth(); + + // BMP scanlines are padded to dword offsets + int scansize = (w + (w&1)) << 1; + short[] data = new short[w*h]; + + for(int y=h-1;y>=0;y--){ + byte[] scanline = new byte[scansize]; + if(in.read(scanline) != scansize) + throw new IOException("Couldn't read image data."); + + for(int x=0;x<w;x++) + data[x + y*w] = (short)((scanline[x*2] & (0xFF)) | + ((scanline[x*2+1] & (0xFF)) << 8)); + } + + ColorModel cm = new DirectColorModel(16, + bitmasks[0], bitmasks[1], bitmasks[2]); + SampleModel sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_USHORT, + w, h, + bitmasks); + DataBuffer db = new DataBufferUShort(data, w*h, 0); + WritableRaster raster = Raster.createWritableRaster(sm, db, null); + return new BufferedImage(cm, raster, false, null); + } + +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/DecodeBF32.java b/libjava/classpath/gnu/javax/imageio/bmp/DecodeBF32.java new file mode 100644 index 000000000..bfa469488 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/DecodeBF32.java @@ -0,0 +1,103 @@ +/* DecodeBF32.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.imageio.bmp; + +import java.io.IOException; +import javax.imageio.stream.ImageInputStream; +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.awt.image.DirectColorModel; +import java.awt.image.Raster; +import java.awt.image.WritableRaster; +import java.awt.image.DataBuffer; +import java.awt.image.DataBufferInt; +import java.awt.image.SinglePixelPackedSampleModel; +import java.awt.image.SampleModel; +import java.awt.Dimension; + +public class DecodeBF32 extends BMPDecoder { + private int[] bitmasks; + private boolean useDefaultMasks; + + public DecodeBF32(BMPFileHeader fh, BMPInfoHeader ih, + boolean udm){ + super(fh,ih); + + useDefaultMasks = udm; + if(useDefaultMasks) + bitmasks = new int[] { 0x00FF0000, 0x0000FF00, 0x000000FF }; + } + + public BufferedImage decode(ImageInputStream in) throws IOException, BMPException { + if(!useDefaultMasks) + bitmasks = readBitMasks(in); + skipToImage(in); + + Dimension d = infoHeader.getSize(); + int h = (int)d.getHeight(); + int w = (int)d.getWidth(); + + // BMP scanlines are padded to dword offsets + int scansize = w << 2; + int[] data = new int[w*h]; + + + for(int y=h-1;y>=0;y--){ + byte[] scanline = new byte[scansize]; + if(in.read(scanline) != scansize) + throw new IOException("Couldn't read image data."); + + for(int x=0;x<w;x++) + data[x + y*w] = ((scanline[x<<2] & (0xFF)) | + ((scanline[(x<<2)+1] & (0xFF)) << 8) | + ((scanline[(x<<2)+2] & (0xFF)) << 16) | + ((scanline[(x<<2)+3] & (0xFF)) << 24)); + } + + ColorModel cm = new DirectColorModel(32, + bitmasks[0], bitmasks[1], bitmasks[2]); + SampleModel sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, + w, h, + bitmasks); + DataBuffer db = new DataBufferInt(data, w*h); + WritableRaster raster = Raster.createWritableRaster(sm, db, null); + + return new BufferedImage(cm, raster, false, null); + } + +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/DecodeRGB1.java b/libjava/classpath/gnu/javax/imageio/bmp/DecodeRGB1.java new file mode 100644 index 000000000..ca81bc433 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/DecodeRGB1.java @@ -0,0 +1,94 @@ +/* DecodeRGB1.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.imageio.bmp; + +import java.io.IOException; +import javax.imageio.stream.ImageInputStream; +import java.awt.image.BufferedImage; +import java.awt.image.IndexColorModel; +import java.awt.image.Raster; +import java.awt.image.WritableRaster; +import java.awt.image.DataBuffer; +import java.awt.image.DataBufferByte; +import java.awt.image.MultiPixelPackedSampleModel; +import java.awt.image.SampleModel; +import java.awt.Dimension; + +public class DecodeRGB1 extends BMPDecoder { + + public DecodeRGB1(BMPFileHeader fh, BMPInfoHeader ih){ + super(fh, ih); + } + + public BufferedImage decode(ImageInputStream in) + throws IOException, BMPException { + + IndexColorModel palette = readPalette(in); + skipToImage(in); + + Dimension d = infoHeader.getSize(); + int h = (int)d.getHeight(); + int w = (int)d.getWidth(); + int size = (w*h)>>3; + + int scansize = w>>3; + byte[] data = new byte[size]; + + for(int y=h-1;y>=0;y--){ + // Scanlines are padded to dword boundries + int readsize = scansize; + if((readsize & 3) != 0) readsize += (4 - (scansize & 3)); + + byte[] scanline = new byte[readsize]; + if(in.read(scanline) != readsize) + throw new IOException("Couldn't read image data."); + + for(int x=0;x<scansize;x++) + data[x + y*scansize] = scanline[x]; + } + + SampleModel sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, + w, h, 1); + + DataBuffer db = new DataBufferByte(data, size, 0); + WritableRaster raster = Raster.createWritableRaster(sm, db, null); + + return new BufferedImage(palette, raster, false, null); + } + +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/DecodeRGB24.java b/libjava/classpath/gnu/javax/imageio/bmp/DecodeRGB24.java new file mode 100644 index 000000000..0ddcc434e --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/DecodeRGB24.java @@ -0,0 +1,77 @@ +/* DecodeRGB24.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.imageio.bmp; + +import java.io.IOException; +import javax.imageio.stream.ImageInputStream; +import java.awt.image.BufferedImage; +import java.awt.Dimension; + +public class DecodeRGB24 extends BMPDecoder { + + public DecodeRGB24(BMPFileHeader fh, BMPInfoHeader ih){ + super(fh, ih); + } + + public BufferedImage decode(ImageInputStream in) throws IOException, BMPException { + skipToImage(in); + + Dimension d = infoHeader.getSize(); + int h = (int)d.getHeight(); + int w = (int)d.getWidth(); + BufferedImage image = new BufferedImage(w, h, + BufferedImage.TYPE_INT_RGB); + // BMP scanlines are padded to dword offsets + int scansize = ((w*3 & 3) != 0)? w*3 + 4 - (w*3 & 3): w*3; + int[] data = new int[w*h]; + + for(int y=h-1;y>=0;y--){ + byte[] scanline = new byte[scansize]; + if(in.read(scanline) != scansize) + throw new IOException("Couldn't read image data."); + + for(int x=0;x<w;x++) + data[x + y*w] = scanline[x*3] + + (scanline[x*3+1] << 8) + + (scanline[x*3+2] << 16); + } + image.setRGB(0, 0, w, h, data, 0, w); + return image; + } + +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/DecodeRGB4.java b/libjava/classpath/gnu/javax/imageio/bmp/DecodeRGB4.java new file mode 100644 index 000000000..a904ef745 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/DecodeRGB4.java @@ -0,0 +1,90 @@ +/* DecodeRGB4.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.imageio.bmp; + +import java.io.IOException; +import javax.imageio.stream.ImageInputStream; +import java.awt.image.BufferedImage; +import java.awt.image.IndexColorModel; +import java.awt.image.Raster; +import java.awt.image.WritableRaster; +import java.awt.image.DataBuffer; +import java.awt.image.DataBufferByte; +import java.awt.image.MultiPixelPackedSampleModel; +import java.awt.image.SampleModel; +import java.awt.Dimension; + +public class DecodeRGB4 extends BMPDecoder { + + public DecodeRGB4(BMPFileHeader fh, BMPInfoHeader ih){ + super(fh, ih); + } + + public BufferedImage decode(ImageInputStream in) throws IOException, BMPException { + IndexColorModel palette = readPalette(in); + skipToImage(in); + + Dimension d = infoHeader.getSize(); + int h = (int)d.getHeight(); + int w = (int)d.getWidth(); + int size = (w*h) >> 1; + + // Scanline padded to dword offsets + int wbytes = (w + (w & 1)) >> 1; + int scansize = ((wbytes & 3) != 0)? (wbytes + 4 - (wbytes&3)) : wbytes; + + byte[] data = new byte[wbytes*h]; + + for(int y=h-1;y>=0;y--){ + byte[] scanline = new byte[scansize]; + if(in.read(scanline) != scansize) + throw new IOException("Couldn't read image data."); + + for(int x=0;x<wbytes;x++) + data[x + y*wbytes] = scanline[x]; + } + SampleModel sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, + w, h, 4); + + DataBuffer db = new DataBufferByte(data, w*h, 0); + WritableRaster raster = Raster.createWritableRaster(sm, db, null); + + return new BufferedImage(palette, raster, false, null); + } + +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/DecodeRGB8.java b/libjava/classpath/gnu/javax/imageio/bmp/DecodeRGB8.java new file mode 100644 index 000000000..218047e5d --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/DecodeRGB8.java @@ -0,0 +1,88 @@ +/* DecodeRGB8.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.imageio.bmp; + +import java.io.IOException; +import javax.imageio.stream.ImageInputStream; +import java.awt.image.BufferedImage; +import java.awt.image.IndexColorModel; +import java.awt.image.Raster; +import java.awt.image.WritableRaster; +import java.awt.image.DataBuffer; +import java.awt.image.DataBufferByte; +import java.awt.image.SinglePixelPackedSampleModel; +import java.awt.image.SampleModel; +import java.awt.Dimension; + +public class DecodeRGB8 extends BMPDecoder { + + public DecodeRGB8(BMPFileHeader fh, BMPInfoHeader ih){ + super(fh, ih); + } + + public BufferedImage decode(ImageInputStream in) throws IOException, BMPException { + IndexColorModel palette = readPalette(in); + skipToImage(in); + + Dimension d = infoHeader.getSize(); + int h = (int)d.getHeight(); + int w = (int)d.getWidth(); + + // BMP scanlines are padded to dword offsets + int scansize = ((w & 3) != 0)? w + 4 - (w & 3): w; + byte[] data = new byte[w*h]; + + for(int y=h-1;y>=0;y--){ + byte[] scanline = new byte[scansize]; + if(in.read(scanline) != scansize) + throw new IOException("Couldn't read image data."); + + for(int x=0;x<w;x++) + data[x + y*w] = scanline[x]; + } + + SampleModel sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE, + w, h, + new int[] {0xFF}); + DataBuffer db = new DataBufferByte(data, w*h, 0); + WritableRaster raster = Raster.createWritableRaster(sm, db, null); + + return new BufferedImage(palette, raster, false, null); + } + +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/DecodeRLE4.java b/libjava/classpath/gnu/javax/imageio/bmp/DecodeRLE4.java new file mode 100644 index 000000000..1c116c247 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/DecodeRLE4.java @@ -0,0 +1,175 @@ +/* DecodeRLE4.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.imageio.bmp; + +import java.io.IOException; +import javax.imageio.stream.ImageInputStream; +import java.awt.image.BufferedImage; +import java.awt.image.IndexColorModel; +import java.awt.image.Raster; +import java.awt.image.WritableRaster; +import java.awt.image.DataBuffer; +import java.awt.image.DataBufferByte; +import java.awt.image.MultiPixelPackedSampleModel; +import java.awt.image.SampleModel; +import java.awt.Dimension; + +public class DecodeRLE4 extends BMPDecoder { + + public DecodeRLE4(BMPFileHeader fh, BMPInfoHeader ih){ + super(fh, ih); + } + + /** + * RLE control codes + */ + private static final byte ESCAPE = (byte)0; + private static final byte EOL = (byte)0; // end of line + private static final byte EOB = (byte)1; // end of bitmap + private static final byte DELTA = (byte)2; // delta + + public BufferedImage decode(ImageInputStream in) throws IOException, BMPException { + IndexColorModel palette = readPalette(in); + skipToImage(in); + + Dimension d = infoHeader.getSize(); + int h = (int)d.getHeight(); + int w = (int)d.getWidth(); + + byte[] data = uncompress(w, h, in); + SampleModel sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, + w, h, 4); + + DataBuffer db = new DataBufferByte(data, w*h, 0); + WritableRaster raster = Raster.createWritableRaster(sm, db, null); + + return new BufferedImage(palette, raster, false, null); + } + + private byte[] uncompress(int w, int h, ImageInputStream in) + throws BMPException, IOException { + byte[] cmd = new byte[2]; + byte[] data = new byte[w*h>>1]; + int offIn = 0; + int x=0,y=0; + + // width in bytes + w += (w&1); + w = w >> 1; + + try { + while(((x>>1) + y*w) < w*h){ + if(in.read(cmd) != 2) + throw new IOException("Error reading compressed data."); + + if(cmd[0] == ESCAPE){ + switch(cmd[1]){ + case EOB: // end of bitmap + return data; + case EOL: // end of line + x = 0; + y++; + break; + case DELTA: // delta + if(in.read(cmd) != 2) + throw new IOException("Error reading compressed data."); + int dx = cmd[0] & (0xFF); + int dy = cmd[1] & (0xFF); + x += dx; + y += dy; + break; + + default: + // decode a literal run + int length = cmd[1] & (0xFF); + + // size of run, which is word aligned. + int bytesize = length; + bytesize += (bytesize & 1); + bytesize >>= 1; + bytesize += (bytesize & 1); + + byte[] run = new byte[bytesize]; + if(in.read(run) != bytesize) + throw new IOException("Error reading compressed data."); + + if((x&1) == 0){ + length += (length&1); + length >>= 1; + System.arraycopy(run, 0, data, ((x>>1) + w*(h-y-1)), + length); + } else { + for(int i=0;i<length;i++){ + if((i&1) == 0) // copy high to low + data[((x+i)>>1) + w*(h-y-1)] + |= ((run[i>>1]&0xF0) >> 4); + else // copy low to high + data[((x+i)>>1) + w*(h-y-1)] + |= ((run[i>>1]&0x0F) << 4); + } + } + x += cmd[1] & (0xFF); + break; + } + } else { + // decode a byte run + int length = cmd[0] & (0xFF); + if((x&1) == 0){ + length += (length&1); + length >>= 1; + for(int i=0;i<length;i++) + data[(h-y-1)*w + i + (x >> 1)] = cmd[1]; + } else { + for(int i=0;i<length;i++){ + if((i&1) == 0) // copy high to low + data[((x+i)>>1) + w*(h-y-1)] + |= ((cmd[1]&0xF0) >> 4); + else // copy low to high + data[((x+i)>>1) + w*(h-y-1)] + |= ((cmd[1]&0x0F) << 4); + } + } + x += cmd[0] & (0xFF); + } + } + return data; + } catch(ArrayIndexOutOfBoundsException e){ + throw new BMPException("Invalid RLE data."); + } + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/DecodeRLE8.java b/libjava/classpath/gnu/javax/imageio/bmp/DecodeRLE8.java new file mode 100644 index 000000000..afc3da89e --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/DecodeRLE8.java @@ -0,0 +1,142 @@ +/* DecodeRLE8.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.javax.imageio.bmp; + +import java.io.IOException; +import javax.imageio.stream.ImageInputStream; +import java.awt.image.BufferedImage; +import java.awt.image.IndexColorModel; +import java.awt.image.Raster; +import java.awt.image.WritableRaster; +import java.awt.image.DataBuffer; +import java.awt.image.DataBufferByte; +import java.awt.image.SinglePixelPackedSampleModel; +import java.awt.image.SampleModel; +import java.awt.Dimension; + +public class DecodeRLE8 extends BMPDecoder { + + public DecodeRLE8(BMPFileHeader fh, BMPInfoHeader ih){ + super(fh, ih); + } + + /** + * RLE control codes + */ + private static final byte ESCAPE = (byte)0; + private static final byte EOL = (byte)0; // end of line + private static final byte EOB = (byte)1; // end of bitmap + private static final byte DELTA = (byte)2; // delta + + public BufferedImage decode(ImageInputStream in) throws IOException, BMPException { + IndexColorModel palette = readPalette(in); + skipToImage(in); + + Dimension d = infoHeader.getSize(); + int h = (int)d.getHeight(); + int w = (int)d.getWidth(); + + byte[] data = uncompress(w, h, in); + SampleModel sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE, + w, h, + new int[] {0xFF}); + DataBuffer db = new DataBufferByte(data, w*h, 0); + WritableRaster raster = Raster.createWritableRaster(sm, db, null); + + return new BufferedImage(palette, raster, false, null); + } + + private byte[] uncompress(int w, int h, ImageInputStream in) + throws BMPException, IOException { + byte[] cmd = new byte[2]; + byte[] data = new byte[w*h]; + int offIn = 0; + int x=0,y=0; + + try { + while((x + y*w) < w*h){ + if(in.read(cmd) != 2) + throw new IOException("Error reading compressed data."); + + if(cmd[0] == ESCAPE){ + switch(cmd[1]){ + case EOB: // end of bitmap + return data; + case EOL: // end of line + x = 0; + y++; + break; + case DELTA: // delta + if(in.read(cmd) != 2) + throw new IOException("Error reading compressed data."); + int dx = cmd[0] & (0xFF); + int dy = cmd[1] & (0xFF); + x += dx; + y += dy; + break; + + default: + // decode a literal run + int length = cmd[1] & (0xFF); + int copylength = length; + + // absolute mode must be word-aligned + length += (length & 1); + + byte[] run = new byte[length]; + if(in.read(run) != length) + throw new IOException("Error reading compressed data."); + + System.arraycopy(run, 0, data, (x+w*(h-y-1)), + copylength); + x += copylength; + break; + } + } else { + // decode a byte run + int length = cmd[0] & (0xFF); + for(int i=0;i<length;i++) + data[(h-y-1)*w + x++] = cmd[1]; + } + } + return data; + } catch(ArrayIndexOutOfBoundsException e){ + throw new BMPException("Invalid RLE data."); + } + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB1.java b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB1.java new file mode 100644 index 000000000..ffe671dc0 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB1.java @@ -0,0 +1,128 @@ +/* EncodeRGB1.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.imageio.bmp; + +import java.awt.image.BufferedImage; +import java.awt.image.PixelGrabber; +import java.io.IOException; + +import javax.imageio.IIOImage; +import javax.imageio.ImageWriteParam; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.stream.ImageOutputStream; + +public class EncodeRGB1 + extends BMPEncoder +{ + protected BMPInfoHeader infoHeader; + protected BMPFileHeader fileHeader; + protected long offset; + + /** + * Constructs an instance of this class. + * + * @param fh - the file header to use. + * @param ih - the info header to use. + */ + public EncodeRGB1(BMPFileHeader fh, BMPInfoHeader ih) + { + super(); + fileHeader = fh; + infoHeader = ih; + offset = BMPFileHeader.SIZE + BMPInfoHeader.SIZE; + } + + /** + * The image encoder. + * + * @param o - the image output stream + * @param streamMetadata - metadata associated with this stream, or + * null + * @param image - an IIOImage containing image data. + * @param param - image writing parameters, or null + * @exception IOException if a write error occurs + */ + public void encode(ImageOutputStream o, IIOMetadata streamMetadata, + IIOImage image, ImageWriteParam param) throws IOException + { + int size; + int value; + int j; + int rowCount; + int rowIndex; + int lastRowIndex; + int[] bitmap; + byte rgb[] = new byte[1]; + size = (infoHeader.biWidth * infoHeader.biHeight) - 1; + rowCount = 1; + rowIndex = size - infoHeader.biWidth; + lastRowIndex = rowIndex; + try + { + bitmap = new int[infoHeader.biWidth * infoHeader.biHeight]; + PixelGrabber pg = new PixelGrabber((BufferedImage) image.getRenderedImage(), + 0, 0, infoHeader.biWidth, + infoHeader.biHeight, bitmap, 0, + infoHeader.biWidth); + pg.grabPixels(); + + for (j = 0; j < size; j++) + { + value = bitmap[rowIndex]; + + rgb[0] = (byte) (value & 0xFF); + + o.write(rgb); + if (rowCount == infoHeader.biWidth) + { + rowCount = 1; + rowIndex = lastRowIndex - infoHeader.biWidth; + lastRowIndex = rowIndex; + } + else + rowCount++; + rowIndex++; + } + } + catch (Exception wb) + { + wb.printStackTrace(); + } + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB16.java b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB16.java new file mode 100644 index 000000000..a5450cc67 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB16.java @@ -0,0 +1,129 @@ +/* EncodeRGB16.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.imageio.bmp; + +import java.awt.image.BufferedImage; +import java.awt.image.PixelGrabber; +import java.io.IOException; + +import javax.imageio.IIOImage; +import javax.imageio.ImageWriteParam; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.stream.ImageOutputStream; + +public class EncodeRGB16 + extends BMPEncoder +{ + protected BMPInfoHeader infoHeader; + protected BMPFileHeader fileHeader; + protected long offset; + + /** + * Constructs an instance of this class. + * + * @param fh - the file header to use. + * @param ih - the info header to use. + */ + public EncodeRGB16(BMPFileHeader fh, BMPInfoHeader ih) + { + super(); + fileHeader = fh; + infoHeader = ih; + offset = BMPFileHeader.SIZE + BMPInfoHeader.SIZE; + } + + /** + * The image encoder. + * + * @param o - the image output stream + * @param streamMetadata - metadata associated with this stream, or + * null + * @param image - an IIOImage containing image data. + * @param param - image writing parameters, or null + * @exception IOException if a write error occurs + */ + public void encode(ImageOutputStream o, IIOMetadata streamMetadata, + IIOImage image, ImageWriteParam param) throws IOException + { + int size; + int value; + int j; + int rowCount; + int rowIndex; + int lastRowIndex; + int[] bitmap; + byte rgb[] = new byte[2]; + size = (infoHeader.biWidth * infoHeader.biHeight) - 1; + rowCount = 1; + rowIndex = size - infoHeader.biWidth; + lastRowIndex = rowIndex; + try + { + bitmap = new int[infoHeader.biWidth * infoHeader.biHeight]; + PixelGrabber pg = new PixelGrabber((BufferedImage) image.getRenderedImage(), + 0, 0, infoHeader.biWidth, + infoHeader.biHeight, bitmap, 0, + infoHeader.biWidth); + pg.grabPixels(); + + for (j = 0; j < size; j++) + { + value = bitmap[rowIndex]; + + rgb[0] = (byte) (value & 0xFF); + rgb[1] = (byte) (value >> 8 & 0xFF); + + o.write(rgb); + if (rowCount == infoHeader.biWidth) + { + rowCount = 1; + rowIndex = lastRowIndex - infoHeader.biWidth; + lastRowIndex = rowIndex; + } + else + rowCount++; + rowIndex++; + } + } + catch (Exception wb) + { + wb.printStackTrace(); + } + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB24.java b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB24.java new file mode 100644 index 000000000..7fbc83e2e --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB24.java @@ -0,0 +1,129 @@ +/* EncodeRGB24.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.imageio.bmp; + +import java.awt.image.BufferedImage; +import java.awt.image.PixelGrabber; +import java.io.IOException; + +import javax.imageio.IIOImage; +import javax.imageio.ImageWriteParam; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.stream.ImageOutputStream; + +public class EncodeRGB24 + extends BMPEncoder +{ + protected BMPInfoHeader infoHeader; + protected BMPFileHeader fileHeader; + protected long offset; + + /** + * Constructs an instance of this class. + * + * @param fh - the file header to use. + * @param ih - the info header to use. + */ + public EncodeRGB24(BMPFileHeader fh, BMPInfoHeader ih) + { + super(); + fileHeader = fh; + infoHeader = ih; + offset = BMPFileHeader.SIZE + BMPInfoHeader.SIZE; + } + + /** + * The image encoder. + * + * @param o - the image output stream + * @param streamMetadata - metadata associated with this stream, or + * null + * @param image - an IIOImage containing image data. + * @param param - image writing parameters, or null + * @exception IOException if a write error occurs + */ + public void encode(ImageOutputStream o, IIOMetadata streamMetadata, + IIOImage image, ImageWriteParam param) throws IOException + { + int size; + int value; + int j; + int rowCount; + int rowIndex; + int lastRowIndex; + int[] bitmap; + byte rgb[] = new byte[3]; + size = (infoHeader.biWidth * infoHeader.biHeight) - 1; + rowCount = 1; + rowIndex = size - infoHeader.biWidth; + lastRowIndex = rowIndex; + try + { + bitmap = new int[infoHeader.biWidth * infoHeader.biHeight]; + PixelGrabber pg = new PixelGrabber((BufferedImage) image.getRenderedImage(), + 0, 0, infoHeader.biWidth, + infoHeader.biHeight, bitmap, 0, + infoHeader.biWidth); + pg.grabPixels(); + + for (j = 0; j < size; j++) + { + value = bitmap[rowIndex]; + + rgb[0] = (byte) (value & 0xFF); + rgb[1] = (byte) ((value >> 8) & 0xFF); + rgb[2] = (byte) ((value >> 16) & 0xFF); + o.write(rgb); + if (rowCount == infoHeader.biWidth) + { + rowCount = 1; + rowIndex = lastRowIndex - infoHeader.biWidth; + lastRowIndex = rowIndex; + } + else + rowCount++; + rowIndex++; + } + } + catch (Exception wb) + { + wb.printStackTrace(); + } + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB32.java b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB32.java new file mode 100644 index 000000000..7deca3049 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB32.java @@ -0,0 +1,129 @@ +/* EncodeRGB32.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.imageio.bmp; + +import java.awt.image.BufferedImage; +import java.awt.image.PixelGrabber; +import java.io.IOException; + +import javax.imageio.IIOImage; +import javax.imageio.ImageWriteParam; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.stream.ImageOutputStream; + +public class EncodeRGB32 + extends BMPEncoder +{ + protected BMPInfoHeader infoHeader; + protected BMPFileHeader fileHeader; + protected long offset; + + /** + * Constructs an instance of this class. + * + * @param fh - the file header to use. + * @param ih - the info header to use. + */ + public EncodeRGB32(BMPFileHeader fh, BMPInfoHeader ih) + { + super(); + fileHeader = fh; + infoHeader = ih; + offset = BMPFileHeader.SIZE + BMPInfoHeader.SIZE; + } + + /** + * The image encoder. + * + * @param o - the image output stream + * @param streamMetadata - metadata associated with this stream, or null + * @param image - an IIOImage containing image data. + * @param param - image writing parameters, or null + * @exception IOException if a write error occurs + */ + public void encode(ImageOutputStream o, IIOMetadata streamMetadata, + IIOImage image, ImageWriteParam param) throws IOException + { + int size; + int value; + int j; + int rowCount; + int rowIndex; + int lastRowIndex; + int[] bitmap; + byte rgb[] = new byte[4]; + size = (infoHeader.biWidth * infoHeader.biHeight) - 1; + rowCount = 1; + rowIndex = size - infoHeader.biWidth; + lastRowIndex = rowIndex; + try + { + bitmap = new int[infoHeader.biWidth * infoHeader.biHeight]; + PixelGrabber pg = new PixelGrabber((BufferedImage) image.getRenderedImage(), + 0, 0, infoHeader.biWidth, + infoHeader.biHeight, bitmap, 0, + infoHeader.biWidth); + pg.grabPixels(); + + for (j = 0; j < size; j++) + { + value = bitmap[rowIndex]; + + rgb[0] = (byte) (value & 0xFF); + rgb[1] = (byte) ((value >> 8) & 0xFF); + rgb[2] = (byte) ((value >> 16) & 0xFF); + rgb[3] = (byte) ((value >> 24) & 0xFF); + o.write(rgb); + if (rowCount == infoHeader.biWidth) + { + rowCount = 1; + rowIndex = lastRowIndex - infoHeader.biWidth; + lastRowIndex = rowIndex; + } + else + rowCount++; + rowIndex++; + } + } + catch (Exception wb) + { + wb.printStackTrace(); + } + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB4.java b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB4.java new file mode 100644 index 000000000..b62283a7d --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB4.java @@ -0,0 +1,128 @@ +/* EncodeRGB4.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.imageio.bmp; + +import java.awt.image.BufferedImage; +import java.awt.image.PixelGrabber; +import java.io.IOException; + +import javax.imageio.IIOImage; +import javax.imageio.ImageWriteParam; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.stream.ImageOutputStream; + +public class EncodeRGB4 + extends BMPEncoder +{ + protected BMPInfoHeader infoHeader; + protected BMPFileHeader fileHeader; + protected long offset; + + /** + * Constructs an instance of this class. + * + * @param fh - the file header to use. + * @param ih - the info header to use. + */ + public EncodeRGB4(BMPFileHeader fh, BMPInfoHeader ih) + { + super(); + fileHeader = fh; + infoHeader = ih; + offset = BMPFileHeader.SIZE + BMPInfoHeader.SIZE; + } + + /** + * The image encoder. + * + * @param o - the image output stream + * @param streamMetadata - metadata associated with this stream, or + * null + * @param image - an IIOImage containing image data. + * @param param - image writing parameters, or null + * @exception IOException if a write error occurs + */ + public void encode(ImageOutputStream o, IIOMetadata streamMetadata, + IIOImage image, ImageWriteParam param) throws IOException + { + int size; + int value; + int j; + int rowCount; + int rowIndex; + int lastRowIndex; + int[] bitmap; + byte rgb[] = new byte[1]; + size = (infoHeader.biWidth * infoHeader.biHeight) - 1; + rowCount = 1; + rowIndex = size - infoHeader.biWidth; + lastRowIndex = rowIndex; + try + { + bitmap = new int[infoHeader.biWidth * infoHeader.biHeight]; + PixelGrabber pg = new PixelGrabber((BufferedImage) image.getRenderedImage(), + 0, 0, infoHeader.biWidth, + infoHeader.biHeight, bitmap, 0, + infoHeader.biWidth); + pg.grabPixels(); + + for (j = 0; j < size; j++) + { + value = bitmap[rowIndex]; + + rgb[0] = (byte) (value & 0xFF); + + o.write(rgb); + if (rowCount == infoHeader.biWidth) + { + rowCount = 1; + rowIndex = lastRowIndex - infoHeader.biWidth; + lastRowIndex = rowIndex; + } + else + rowCount++; + rowIndex++; + } + } + catch (Exception wb) + { + wb.printStackTrace(); + } + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB8.java b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB8.java new file mode 100644 index 000000000..ef0594a5f --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRGB8.java @@ -0,0 +1,127 @@ +/* EncodeRGB8.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.imageio.bmp; + +import java.awt.image.BufferedImage; +import java.awt.image.PixelGrabber; +import java.io.IOException; + +import javax.imageio.IIOImage; +import javax.imageio.ImageWriteParam; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.stream.ImageOutputStream; + +public class EncodeRGB8 + extends BMPEncoder +{ + protected BMPInfoHeader infoHeader; + protected BMPFileHeader fileHeader; + protected long offset; + + /** + * Constructs an instance of this class. + * + * @param fh - the file header to use. + * @param ih - the info header to use. + */ + public EncodeRGB8(BMPFileHeader fh, BMPInfoHeader ih) + { + super(); + fileHeader = fh; + infoHeader = ih; + offset = BMPFileHeader.SIZE + BMPInfoHeader.SIZE; + } + + /** + * The image encoder. + * + * @param o - the image output stream + * @param streamMetadata - metadata associated with this stream, or + * null + * @param image - an IIOImage containing image data. + * @param param - image writing parameters, or null + * @exception IOException if a write error occurs + */ + public void encode(ImageOutputStream o, IIOMetadata streamMetadata, + IIOImage image, ImageWriteParam param) throws IOException + { + int size; + int value; + int j; + int rowCount; + int rowIndex; + int lastRowIndex; + int[] bitmap; + byte rgb[] = new byte[1]; + size = (infoHeader.biWidth * infoHeader.biHeight) - 1; + rowCount = 1; + rowIndex = size - infoHeader.biWidth; + lastRowIndex = rowIndex; + try + { + bitmap = new int[infoHeader.biWidth * infoHeader.biHeight]; + PixelGrabber pg = new PixelGrabber((BufferedImage) image.getRenderedImage(), + 0, 0, infoHeader.biWidth, + infoHeader.biHeight, bitmap, 0, + infoHeader.biWidth); + pg.grabPixels(); + + for (j = 0; j < size; j++) + { + value = bitmap[rowIndex]; + + rgb[0] = (byte) (value & 0xFF); + o.write(rgb); + if (rowCount == infoHeader.biWidth) + { + rowCount = 1; + rowIndex = lastRowIndex - infoHeader.biWidth; + lastRowIndex = rowIndex; + } + else + rowCount++; + rowIndex++; + } + } + catch (Exception wb) + { + wb.printStackTrace(); + } + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/EncodeRLE4.java b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRLE4.java new file mode 100644 index 000000000..c54c3ca87 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRLE4.java @@ -0,0 +1,269 @@ +/* EncodeRLE4.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.imageio.bmp; + +import java.awt.image.BufferedImage; +import java.awt.image.PixelGrabber; +import java.io.IOException; +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; + +import javax.imageio.IIOImage; +import javax.imageio.ImageWriteParam; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.stream.ImageOutputStream; + +public class EncodeRLE4 + extends BMPEncoder +{ + protected BMPInfoHeader infoHeader; + protected BMPFileHeader fileHeader; + protected long offset; + + /** + * RLE control codes + */ + private static final byte ESCAPE = (byte)0; + private static final byte EOL = (byte)0; // end of line + private static final byte EOB = (byte)1; // end of bitmap + private static final byte DELTA = (byte)2; // delta + + /** + * Constructs an instance of this class. + * + * @param fh - the file header to use. + * @param ih - the info header to use. + */ + public EncodeRLE4(BMPFileHeader fh, BMPInfoHeader ih) + { + super(); + fileHeader = fh; + infoHeader = ih; + offset = BMPFileHeader.SIZE + BMPInfoHeader.SIZE; + } + + /** + * The image encoder. + * + * @param o - the image output stream + * @param streamMetadata - metadata associated with this stream, or + * null + * @param image - an IIOImage containing image data. + * @param param - image writing parameters, or null + * @exception IOException if a write error occurs + */ + public void encode(ImageOutputStream o, IIOMetadata streamMetadata, + IIOImage image, ImageWriteParam param) throws IOException + { + int size; + int value; + int j; + int rowCount; + int rowIndex; + int lastRowIndex; + int[] bitmap; + size = (infoHeader.biWidth * infoHeader.biHeight) - 1; + rowCount = 1; + rowIndex = size - infoHeader.biWidth; + lastRowIndex = rowIndex; + ByteBuffer buf = ByteBuffer.allocate(size); + try + { + bitmap = new int[infoHeader.biWidth * infoHeader.biHeight]; + PixelGrabber pg = new PixelGrabber((BufferedImage) image.getRenderedImage(), + 0, 0, infoHeader.biWidth, + infoHeader.biHeight, bitmap, 0, + infoHeader.biWidth); + pg.grabPixels(); + + for (j = 0; j < size; j++) + { + value = bitmap[rowIndex]; + buf.put((byte) (value & 0xFF)); + + if (rowCount == infoHeader.biWidth) + { + rowCount = 1; + rowIndex = lastRowIndex - infoHeader.biWidth; + lastRowIndex = rowIndex; + } + else + rowCount++; + rowIndex++; + } + + buf.flip(); + o.write(uncompress(infoHeader.biWidth, infoHeader.biHeight, buf)); + } + catch (Exception wb) + { + wb.printStackTrace(); + } + } + + /** + * Uncompresses the image stored in the buffer. + * + * @param w - the width of the image + * @param h - the height of the image + * @param buf - the ByteBuffer containing the pixel values. + * @return byte array containing the uncompressed image + * @throws IOException if an error is encountered while reading + * buffer. + */ + private byte[] uncompress(int w, int h, ByteBuffer buf) + throws IOException + { + byte[] cmd = new byte[2]; + byte[] data = new byte[w * h >> 1]; + int offIn = 0; + int x = 0, y = 0; + + w += (w & 1); + w = w >> 1; + + try + { + while (((x >> 1) + y * w) < w * h) + { + try + { + buf.get(cmd); + } + catch (BufferUnderflowException e) + { + throw new IOException("Error reading compressed data."); + } + + if (cmd[0] == ESCAPE) + { + switch (cmd[1]) + { + case EOB: + return data; + case EOL: + x = 0; + y++; + break; + case DELTA: + try + { + buf.get(cmd); + } + catch (BufferUnderflowException e) + { + throw new IOException("Error reading compressed data."); + } + + int dx = cmd[0] & (0xFF); + int dy = cmd[1] & (0xFF); + x += dx; + y += dy; + break; + + default: + int length = cmd[1] & (0xFF); + + int bytesize = length; + bytesize += (bytesize & 1); + bytesize >>= 1; + bytesize += (bytesize & 1); + + byte[] run = new byte[bytesize]; + try + { + buf.get(run); + } + catch (BufferUnderflowException e) + { + throw new IOException("Error reading compressed data."); + } + + if ((x & 1) == 0) + { + length += (length & 1); + length >>= 1; + System.arraycopy(run, 0, data, + ((x >> 1) + w * (h - y - 1)), length); + } + else + { + for (int i = 0; i < length; i++) + { + if ((i & 1) == 0) + data[((x + i) >> 1) + w * (h - y - 1)] |= ((run[i >> 1] & 0xF0) >> 4); + else + data[((x + i) >> 1) + w * (h - y - 1)] |= ((run[i >> 1] & 0x0F) << 4); + } + } + x += cmd[1] & (0xFF); + break; + } + } + else + { + int length = cmd[0] & (0xFF); + if ((x & 1) == 0) + { + length += (length & 1); + length >>= 1; + for (int i = 0; i < length; i++) + data[(h - y - 1) * w + i + (x >> 1)] = cmd[1]; + } + else + { + for (int i = 0; i < length; i++) + { + if ((i & 1) == 0) + data[((x + i) >> 1) + w * (h - y - 1)] |= ((cmd[1] & 0xF0) >> 4); + else + data[((x + i) >> 1) + w * (h - y - 1)] |= ((cmd[1] & 0x0F) << 4); + } + } + x += cmd[0] & (0xFF); + } + } + return data; + } + catch (ArrayIndexOutOfBoundsException e) + { + throw new BMPException("Invalid RLE data."); + } + } +} diff --git a/libjava/classpath/gnu/javax/imageio/bmp/EncodeRLE8.java b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRLE8.java new file mode 100644 index 000000000..62277ef90 --- /dev/null +++ b/libjava/classpath/gnu/javax/imageio/bmp/EncodeRLE8.java @@ -0,0 +1,234 @@ +/* EncodeRGB32.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.imageio.bmp; + +import java.awt.image.BufferedImage; +import java.awt.image.PixelGrabber; +import java.io.IOException; +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; + +import javax.imageio.IIOImage; +import javax.imageio.ImageWriteParam; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.stream.ImageOutputStream; + +public class EncodeRLE8 + extends BMPEncoder +{ + protected BMPInfoHeader infoHeader; + protected BMPFileHeader fileHeader; + protected long offset; + + /** + * RLE control codes + */ + private static final byte ESCAPE = (byte)0; + private static final byte EOL = (byte)0; // end of line + private static final byte EOB = (byte)1; // end of bitmap + private static final byte DELTA = (byte)2; // delta + + /** + * Constructs an instance of this class. + * + * @param fh - the file header to use. + * @param ih - the info header to use. + */ + public EncodeRLE8(BMPFileHeader fh, BMPInfoHeader ih) + { + super(); + fileHeader = fh; + infoHeader = ih; + offset = BMPFileHeader.SIZE + BMPInfoHeader.SIZE; + } + + /** + * The image encoder. + * + * @param o - the image output stream + * @param streamMetadata - metadata associated with this stream, or + * null + * @param image - an IIOImage containing image data. + * @param param - image writing parameters, or null + * @exception IOException if a write error occurs + */ + public void encode(ImageOutputStream o, IIOMetadata streamMetadata, + IIOImage image, ImageWriteParam param) throws IOException + { + int size; + int value; + int j; + int rowCount; + int rowIndex; + int lastRowIndex; + int[] bitmap; + size = (infoHeader.biWidth * infoHeader.biHeight) - 1; + rowCount = 1; + rowIndex = size - infoHeader.biWidth; + lastRowIndex = rowIndex; + ByteBuffer buf = ByteBuffer.allocate(size); + try + { + bitmap = new int[infoHeader.biWidth * infoHeader.biHeight]; + PixelGrabber pg = new PixelGrabber((BufferedImage) image.getRenderedImage(), + 0, 0, infoHeader.biWidth, + infoHeader.biHeight, bitmap, 0, + infoHeader.biWidth); + pg.grabPixels(); + + for (j = 0; j < size; j++) + { + value = bitmap[rowIndex]; + buf.put((byte) (value & 0xFF)); + + if (rowCount == infoHeader.biWidth) + { + rowCount = 1; + rowIndex = lastRowIndex - infoHeader.biWidth; + lastRowIndex = rowIndex; + } + else + rowCount++; + rowIndex++; + } + + buf.flip(); + o.write(uncompress(infoHeader.biWidth, infoHeader.biHeight, buf)); + } + catch (Exception wb) + { + wb.printStackTrace(); + } + } + + + /** + * Uncompresses the image stored in the buffer. + * + * @param w - the width of the image + * @param h - the height of the image + * @param buf - the ByteBuffer containing the pixel values. + * @return byte array containing the uncompressed image + * @throws IOException if an error is encountered while reading + * buffer. + */ + private byte[] uncompress(int w, int h, ByteBuffer buf) throws IOException + { + byte[] cmd = new byte[2]; + byte[] data = new byte[w * h]; + int offIn = 0; + int x = 0, y = 0; + + try + { + while ((x + y * w) < w * h) + { + try + { + buf.get(cmd); + } + catch (BufferUnderflowException e) + { + throw new IOException("Error reading compressed data."); + } + + if (cmd[0] == ESCAPE) + { + switch (cmd[1]) + { + case EOB: + return data; + case EOL: + x = 0; + y++; + break; + case DELTA: + try + { + buf.get(cmd); + } + catch (BufferUnderflowException e) + { + throw new IOException("Error reading compressed data."); + } + + int dx = cmd[0] & (0xFF); + int dy = cmd[1] & (0xFF); + x += dx; + y += dy; + break; + + default: + int length = cmd[1] & (0xFF); + int copylength = length; + + length += (length & 1); + + byte[] run = new byte[length]; + + try + { + buf.get(run); + } + catch (BufferUnderflowException e) + { + throw new IOException("Error reading compressed data."); + } + + System.arraycopy(run, 0, data, (x + w * (h - y - 1)), + copylength); + x += copylength; + break; + } + } + else + { + int length = cmd[0] & (0xFF); + for (int i = 0; i < length; i++) + data[(h - y - 1) * w + x++] = cmd[1]; + } + } + return data; + } + catch (ArrayIndexOutOfBoundsException e) + { + throw new BMPException("Invalid RLE data."); + } + } +} |