You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2010/09/10 18:33:42 UTC
svn commit: r995859 [8/30] - in
/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan: ./ color/
common/ common/byteSources/ common/mylzw/ formats/bmp/
formats/bmp/pixelparsers/ formats/bmp/writers/ formats/gif/ formats/ico/
formats/jpeg/ fo...
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/BmpImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/BmpImageParser.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/BmpImageParser.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/BmpImageParser.java Fri Sep 10 16:33:35 2010
@@ -52,670 +52,670 @@ import org.apache.sanselan.util.ParamMap
public class BmpImageParser extends ImageParser
{
- public BmpImageParser()
- {
- super.setByteOrder(BYTE_ORDER_INTEL);
- }
-
- public String getName()
- {
- return "Bmp-Custom";
- }
-
- public String getDefaultExtension()
- {
- return DEFAULT_EXTENSION;
- }
-
- private static final String DEFAULT_EXTENSION = ".bmp";
-
- private static final String ACCEPTED_EXTENSIONS[] = { DEFAULT_EXTENSION, };
-
- protected String[] getAcceptedExtensions()
- {
- return ACCEPTED_EXTENSIONS;
- }
-
- protected ImageFormat[] getAcceptedTypes()
- {
- return new ImageFormat[] { ImageFormat.IMAGE_FORMAT_BMP, //
- };
- }
-
- private static final byte BMP_HEADER_SIGNATURE[] = { 0x42, 0x4d, };
-
- private BmpHeaderInfo readBmpHeaderInfo(InputStream is,
- FormatCompliance formatCompliance, boolean verbose)
- throws ImageReadException, IOException
- {
- byte identifier1 = readByte("Identifier1", is, "Not a Valid BMP File");
- byte identifier2 = readByte("Identifier2", is, "Not a Valid BMP File");
-
- if (formatCompliance != null)
- {
- formatCompliance.compare_bytes("Signature", BMP_HEADER_SIGNATURE,
- new byte[] { identifier1, identifier2, });
- }
-
- int fileSize = read4Bytes("File Size", is, "Not a Valid BMP File");
- int reserved = read4Bytes("Reserved", is, "Not a Valid BMP File");
- int bitmapDataOffset = read4Bytes("Bitmap Data Offset", is,
- "Not a Valid BMP File");
-
- int bitmapHeaderSize = read4Bytes("Bitmap Header Size", is,
- "Not a Valid BMP File");
- int width = read4Bytes("Width", is, "Not a Valid BMP File");
- int height = read4Bytes("Height", is, "Not a Valid BMP File");
- int planes = read2Bytes("Planes", is, "Not a Valid BMP File");
- int bitsPerPixel = read2Bytes("Bits Per Pixel", is,
- "Not a Valid BMP File");
- int compression = read4Bytes("Compression", is, "Not a Valid BMP File");
- int bitmapDataSize = read4Bytes("Bitmap Data Size", is,
- "Not a Valid BMP File");
- int hResolution = read4Bytes("HResolution", is, "Not a Valid BMP File");
- int vResolution = read4Bytes("VResolution", is, "Not a Valid BMP File");
- int colorsUsed = read4Bytes("ColorsUsed", is, "Not a Valid BMP File");
- int colorsImportant = read4Bytes("ColorsImportant", is,
- "Not a Valid BMP File");
-
- if (verbose)
- {
- this.debugNumber("identifier1", identifier1, 1);
- this.debugNumber("identifier2", identifier2, 1);
- this.debugNumber("fileSize", fileSize, 4);
- this.debugNumber("reserved", reserved, 4);
- this.debugNumber("bitmapDataOffset", bitmapDataOffset, 4);
- this.debugNumber("bitmapHeaderSize", bitmapHeaderSize, 4);
- this.debugNumber("width", width, 4);
- this.debugNumber("height", height, 4);
- this.debugNumber("planes", planes, 2);
- this.debugNumber("bitsPerPixel", bitsPerPixel, 2);
- this.debugNumber("compression", compression, 4);
- this.debugNumber("bitmapDataSize", bitmapDataSize, 4);
- this.debugNumber("hResolution", hResolution, 4);
- this.debugNumber("vResolution", vResolution, 4);
- this.debugNumber("colorsUsed", colorsUsed, 4);
- this.debugNumber("colorsImportant", colorsImportant, 4);
- }
-
- BmpHeaderInfo result = new BmpHeaderInfo(identifier1, identifier2,
- fileSize, reserved, bitmapDataOffset, bitmapHeaderSize, width,
- height, planes, bitsPerPixel, compression, bitmapDataSize,
- hResolution, vResolution, colorsUsed, colorsImportant);
- return result;
- }
-
- private static final int BI_RGB = 0;
- private static final int BI_RLE4 = 2;
- private static final int BI_RLE8 = 1;
- private static final int BI_BITFIELDS = 3;
-
- private byte[] getRLEBytes(InputStream is, int RLESamplesPerByte)
- throws ImageReadException, IOException
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- // this.setDebug(true);
-
- boolean done = false;
- while (!done)
- {
- int a = 0xff & this.readByte("RLE a", is, "BMP: Bad RLE");
- baos.write(a);
- int b = 0xff & this.readByte("RLE b", is, "BMP: Bad RLE");
- baos.write(b);
-
- if (a == 0)
- {
- switch (b)
- {
- case 0: // EOL
- break;
- case 1: // EOF
- // System.out.println("xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
- // );
- done = true;
- break;
- case 2: {
- // System.out.println("xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
- // );
- int c = 0xff & this.readByte("RLE c", is, "BMP: Bad RLE");
- baos.write(c);
- int d = 0xff & this.readByte("RLE d", is, "BMP: Bad RLE");
- baos.write(d);
-
- }
- break;
- default: {
- int size = b / RLESamplesPerByte;
- if ((b % RLESamplesPerByte) > 0)
- size++;
- if ((size % 2) != 0)
- size++;
-
- // System.out.println("b: " + b);
- // System.out.println("size: " + size);
- // System.out.println("RLESamplesPerByte: " +
- // RLESamplesPerByte);
- // System.out.println("xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
- // );
- byte bytes[] = this.readByteArray("bytes", size, is,
- "RLE: Absolute Mode");
- baos.write(bytes);
- }
- break;
- }
- }
- }
-
- return baos.toByteArray();
- }
-
- private ImageContents readImageContents(InputStream is,
- FormatCompliance formatCompliance, boolean verbose)
- throws ImageReadException, IOException
- {
- BmpHeaderInfo bhi = readBmpHeaderInfo(is, formatCompliance, verbose);
-
- int colorTableSize = bhi.colorsUsed;
- if (colorTableSize == 0)
- colorTableSize = (1 << bhi.bitsPerPixel);
-
- if (verbose)
- {
- this.debugNumber("ColorsUsed", bhi.colorsUsed, 4);
- this.debugNumber("BitsPerPixel", bhi.bitsPerPixel, 4);
- this.debugNumber("ColorTableSize", colorTableSize, 4);
- this.debugNumber("bhi.colorsUsed", bhi.colorsUsed, 4);
- this.debugNumber("Compression", bhi.compression, 4);
- }
-
- int paletteLength;
- int rleSamplesPerByte = 0;
- boolean rle = false;
-
- switch (bhi.compression)
- {
- case BI_RGB:
- if (verbose)
- System.out.println("Compression: BI_RGB");
- if (bhi.bitsPerPixel <= 8)
- paletteLength = 4 * colorTableSize;
- else
- paletteLength = 0;
- // BytesPerPaletteEntry = 0;
- // System.out.println("Compression: BI_RGBx2: " + bhi.BitsPerPixel);
- // System.out.println("Compression: BI_RGBx2: " + (bhi.BitsPerPixel
- // <= 16));
- break;
-
- case BI_RLE4:
- if (verbose)
- System.out.println("Compression: BI_RLE4");
- paletteLength = 4 * colorTableSize;
- rleSamplesPerByte = 2;
- // ExtraBitsPerPixel = 4;
- rle = true;
- // // BytesPerPixel = 2;
- // // BytesPerPaletteEntry = 0;
- break;
- //
- case BI_RLE8:
- if (verbose)
- System.out.println("Compression: BI_RLE8");
- paletteLength = 4 * colorTableSize;
- rleSamplesPerByte = 1;
- // ExtraBitsPerPixel = 8;
- rle = true;
- // BytesPerPixel = 2;
- // BytesPerPaletteEntry = 0;
- break;
- //
- case BI_BITFIELDS:
- if (verbose)
- System.out.println("Compression: BI_BITFIELDS");
- paletteLength = 3 * 4; // TODO: is this right? are the masks always
- // LONGs?
- // BytesPerPixel = 2;
- // BytesPerPaletteEntry = 4;
- break;
-
- default:
- throw new ImageReadException("BMP: Unknown Compression: "
- + bhi.compression);
- }
-
- byte colorTable[] = null;
- if (paletteLength > 0)
- colorTable = this.readByteArray("ColorTable", paletteLength, is,
- "Not a Valid BMP File");
-
- if (verbose)
- {
- this.debugNumber("paletteLength", paletteLength, 4);
- System.out.println("ColorTable: "
- + ((colorTable == null) ? "null" : "" + colorTable.length));
- }
-
- int pixelCount = bhi.width * bhi.height;
-
- int imageLineLength = ((((bhi.bitsPerPixel) * bhi.width) + 7) / 8);
-
- if (verbose)
- {
- // this.debugNumber("Total BitsPerPixel",
- // (ExtraBitsPerPixel + bhi.BitsPerPixel), 4);
- // this.debugNumber("Total Bit Per Line",
- // ((ExtraBitsPerPixel + bhi.BitsPerPixel) * bhi.Width), 4);
- // this.debugNumber("ExtraBitsPerPixel", ExtraBitsPerPixel, 4);
- this.debugNumber("bhi.Width", bhi.width, 4);
- this.debugNumber("bhi.Height", bhi.height, 4);
- this.debugNumber("ImageLineLength", imageLineLength, 4);
- // this.debugNumber("imageDataSize", imageDataSize, 4);
- this.debugNumber("PixelCount", pixelCount, 4);
- }
- // int ImageLineLength = BytesPerPixel * bhi.Width;
- while ((imageLineLength % 4) != 0)
- imageLineLength++;
-
- final int headerSize = BITMAP_FILE_HEADER_SIZE
- + BITMAP_INFO_HEADER_SIZE;
- int expectedDataOffset = headerSize + paletteLength;
-
- if (verbose)
- {
- this.debugNumber("bhi.BitmapDataOffset", bhi.bitmapDataOffset, 4);
- this.debugNumber("expectedDataOffset", expectedDataOffset, 4);
- }
- int extraBytes = bhi.bitmapDataOffset - expectedDataOffset;
- if (extraBytes < 0)
- throw new ImageReadException("BMP has invalid image data offset: "
- + bhi.bitmapDataOffset + " (expected: "
- + expectedDataOffset + ", paletteLength: " + paletteLength
- + ", headerSize: " + headerSize + ")");
- else if (extraBytes > 0)
- this.readByteArray("BitmapDataOffset", extraBytes, is,
- "Not a Valid BMP File");
-
- int imageDataSize = bhi.height * imageLineLength;
-
- if (verbose)
- this.debugNumber("imageDataSize", imageDataSize, 4);
-
- byte imageData[];
- if (rle)
- imageData = getRLEBytes(is, rleSamplesPerByte);
- else
- imageData = this.readByteArray("ImageData", imageDataSize, is,
- "Not a Valid BMP File");
-
- if (verbose)
- this.debugNumber("ImageData.length", imageData.length, 4);
-
- PixelParser pixelParser;
-
- switch (bhi.compression)
- {
- case BI_RLE4:
- case BI_RLE8:
- pixelParser = new PixelParserRle(bhi, colorTable, imageData);
- break;
- case BI_RGB:
- pixelParser = new PixelParserRgb(bhi, colorTable, imageData);
- break;
- case BI_BITFIELDS:
- pixelParser = new PixelParserBitFields(bhi, colorTable, imageData);
- break;
- default:
- throw new ImageReadException("BMP: Unknown Compression: "
- + bhi.compression);
- }
-
- return new ImageContents(bhi, colorTable, imageData, pixelParser);
- }
-
- private BmpHeaderInfo readBmpHeaderInfo(ByteSource byteSource,
- boolean verbose) throws ImageReadException, IOException
- {
- InputStream is = null;
- try
- {
- is = byteSource.getInputStream();
-
- // readSignature(is);
- return readBmpHeaderInfo(is, null, verbose);
- } finally
- {
- try
- {
- if (is != null) {
- is.close();
- }
- } catch (Exception e)
- {
- Debug.debug(e);
- }
-
- }
- }
-
- public byte[] getICCProfileBytes(ByteSource byteSource, Map params)
- throws ImageReadException, IOException
- {
- return null;
- }
-
- public Dimension getImageSize(ByteSource byteSource, Map params)
- throws ImageReadException, IOException
- {
- // make copy of params; we'll clear keys as we consume them.
- params = (params == null) ? new HashMap() : new HashMap(params);
-
- boolean verbose = ParamMap.getParamBoolean(params, PARAM_KEY_VERBOSE,
- false);
-
- if (params.containsKey(PARAM_KEY_VERBOSE))
- params.remove(PARAM_KEY_VERBOSE);
-
- if (params.size() > 0)
- {
- Object firstKey = params.keySet().iterator().next();
- throw new ImageReadException("Unknown parameter: " + firstKey);
- }
-
- BmpHeaderInfo bhi = readBmpHeaderInfo(byteSource, verbose);
-
- if (bhi == null)
- throw new ImageReadException("BMP: couldn't read header");
-
- return new Dimension(bhi.width, bhi.height);
-
- }
-
- public byte[] embedICCProfile(byte image[], byte profile[])
- {
- return null;
- }
-
- public boolean embedICCProfile(File src, File dst, byte profile[])
- {
- return false;
- }
-
- public IImageMetadata getMetadata(ByteSource byteSource, Map params)
- throws ImageReadException, IOException
- {
- return null;
- }
-
- private String getBmpTypeDescription(int Identifier1, int Identifier2)
- {
- if ((Identifier1 == 'B') && (Identifier2 == 'M'))
- return "Windows 3.1x, 95, NT,";
- if ((Identifier1 == 'B') && (Identifier2 == 'A'))
- return "OS/2 Bitmap Array";
- if ((Identifier1 == 'C') && (Identifier2 == 'I'))
- return "OS/2 Color Icon";
- if ((Identifier1 == 'C') && (Identifier2 == 'P'))
- return "OS/2 Color Pointer";
- if ((Identifier1 == 'I') && (Identifier2 == 'C'))
- return "OS/2 Icon";
- if ((Identifier1 == 'P') && (Identifier2 == 'T'))
- return "OS/2 Pointer";
-
- return "Unknown";
- }
-
- public ImageInfo getImageInfo(ByteSource byteSource, Map params)
- throws ImageReadException, IOException
- {
- // make copy of params; we'll clear keys as we consume them.
- params = (params == null) ? new HashMap() : new HashMap(params);
-
- boolean verbose = ParamMap.getParamBoolean(params, PARAM_KEY_VERBOSE,
- false);
-
- if (params.containsKey(PARAM_KEY_VERBOSE))
- params.remove(PARAM_KEY_VERBOSE);
-
- if (params.size() > 0)
- {
- Object firstKey = params.keySet().iterator().next();
- throw new ImageReadException("Unknown parameter: " + firstKey);
- }
-
- ImageContents ic = readImageContents(byteSource.getInputStream(),
- FormatCompliance.getDefault(), verbose);
-
- if (ic == null)
- throw new ImageReadException("Couldn't read BMP Data");
-
- BmpHeaderInfo bhi = ic.bhi;
- byte colorTable[] = ic.colorTable;
-
- if (bhi == null)
- throw new ImageReadException("BMP: couldn't read header");
-
- int height = bhi.height;
- int width = bhi.width;
-
- ArrayList comments = new ArrayList();
- // TODO: comments...
-
- int bitsPerPixel = bhi.bitsPerPixel;
- ImageFormat format = ImageFormat.IMAGE_FORMAT_BMP;
- String name = "BMP Windows Bitmap";
- String mimeType = "image/x-ms-bmp";
- // we ought to count images, but don't yet.
- int numberOfImages = -1;
- // not accurate ... only reflects first
- boolean isProgressive = false;
- // boolean isProgressive = (fPNGChunkIHDR.InterlaceMethod != 0);
- //
- // pixels per meter
- int physicalWidthDpi = (int) (bhi.hResolution * 1000.0 / 2.54);
- float physicalWidthInch = (float) ((double) width / (double) physicalWidthDpi);
- // int physicalHeightDpi = 72;
- int physicalHeightDpi = (int) (bhi.vResolution * 1000.0 / 2.54);
- float physicalHeightInch = (float) ((double) height / (double) physicalHeightDpi);
-
- String formatDetails = "Bmp (" + (char) bhi.identifier1
- + (char) bhi.identifier2 + ": "
- + getBmpTypeDescription(bhi.identifier1, bhi.identifier2) + ")";
-
- boolean isTransparent = false;
-
- boolean usesPalette = colorTable != null;
- int colorType = ImageInfo.COLOR_TYPE_RGB;
- String compressionAlgorithm = ImageInfo.COMPRESSION_ALGORITHM_RLE;
-
- ImageInfo result = new ImageInfo(formatDetails, bitsPerPixel, comments,
- format, name, height, mimeType, numberOfImages,
- physicalHeightDpi, physicalHeightInch, physicalWidthDpi,
- physicalWidthInch, width, isProgressive, isTransparent,
- usesPalette, colorType, compressionAlgorithm);
-
- return result;
- }
-
- public boolean dumpImageFile(PrintWriter pw, ByteSource byteSource)
- throws ImageReadException, IOException
- {
- pw.println("bmp.dumpImageFile");
-
- ImageInfo imageData = getImageInfo(byteSource, null);
- if (imageData == null)
- return false;
-
- imageData.toString(pw, "");
-
- pw.println("");
-
- return true;
- }
-
- public FormatCompliance getFormatCompliance(ByteSource byteSource)
- throws ImageReadException, IOException
- {
- boolean verbose = false;
-
- FormatCompliance result = new FormatCompliance(byteSource
- .getDescription());
-
- readImageContents(byteSource.getInputStream(), result, verbose);
-
- return result;
- }
-
- public BufferedImage getBufferedImage(ByteSource byteSource, Map params)
- throws ImageReadException, IOException
- {
- return getBufferedImage(byteSource.getInputStream(), params);
- }
-
- public BufferedImage getBufferedImage(InputStream inputStream, Map params)
- throws ImageReadException, IOException
- {
- // make copy of params; we'll clear keys as we consume them.
- params = (params == null) ? new HashMap() : new HashMap(params);
-
- boolean verbose = ParamMap.getParamBoolean(params, PARAM_KEY_VERBOSE,
- false);
-
- if (params.containsKey(PARAM_KEY_VERBOSE))
- params.remove(PARAM_KEY_VERBOSE);
- if (params.containsKey(BUFFERED_IMAGE_FACTORY))
- params.remove(BUFFERED_IMAGE_FACTORY);
-
- if (params.size() > 0)
- {
- Object firstKey = params.keySet().iterator().next();
- throw new ImageReadException("Unknown parameter: " + firstKey);
- }
-
- ImageContents ic = readImageContents(inputStream,
- FormatCompliance.getDefault(), verbose);
- if (ic == null)
- throw new ImageReadException("Couldn't read BMP Data");
-
- BmpHeaderInfo bhi = ic.bhi;
- // byte colorTable[] = ic.colorTable;
- // byte imageData[] = ic.imageData;
-
- int width = bhi.width;
- int height = bhi.height;
-
- boolean hasAlpha = false;
- BufferedImage result = getBufferedImageFactory(params)
- .getColorBufferedImage(width, height, hasAlpha);
-
- if (verbose)
- {
- System.out.println("width: " + width);
- System.out.println("height: " + height);
- System.out.println("width*height: " + width * height);
- System.out.println("width*height*4: " + width * height * 4);
- }
-
- PixelParser pixelParser = ic.pixelParser;
-
- pixelParser.processImage(result);
-
- return result;
-
- }
-
- private static final int BITMAP_FILE_HEADER_SIZE = 14;
- private static final int BITMAP_INFO_HEADER_SIZE = 40;
-
- public void writeImage(BufferedImage src, OutputStream os, Map params)
- throws ImageWriteException, IOException
- {
- // make copy of params; we'll clear keys as we consume them.
- params = (params == null) ? new HashMap() : new HashMap(params);
-
- // clear format key.
- if (params.containsKey(PARAM_KEY_FORMAT))
- params.remove(PARAM_KEY_FORMAT);
-
- if (params.size() > 0)
- {
- Object firstKey = params.keySet().iterator().next();
- throw new ImageWriteException("Unknown parameter: " + firstKey);
- }
-
- final SimplePalette palette = new PaletteFactory().makePaletteSimple(
- src, 256);
-
- BMPWriter writer = null;
- if (palette == null)
- writer = new BMPWriterRGB();
- else
- writer = new BMPWriterPalette(palette);
-
- byte imagedata[] = writer.getImageData(src);
- BinaryOutputStream bos = new BinaryOutputStream(os, BYTE_ORDER_INTEL);
-
- {
- // write BitmapFileHeader
- os.write(0x42); // B, Windows 3.1x, 95, NT, Bitmap
- os.write(0x4d); // M
-
- int filesize = BITMAP_FILE_HEADER_SIZE + BITMAP_INFO_HEADER_SIZE + // header
- // size
- 4 * writer.getPaletteSize() + // palette size in bytes
- imagedata.length;
- bos.write4Bytes(filesize);
-
- bos.write4Bytes(0); // reserved
- bos.write4Bytes(BITMAP_FILE_HEADER_SIZE + BITMAP_INFO_HEADER_SIZE
- + 4 * writer.getPaletteSize()); // Bitmap Data Offset
- }
-
- int width = src.getWidth();
- int height = src.getHeight();
-
- { // write BitmapInfoHeader
- bos.write4Bytes(BITMAP_INFO_HEADER_SIZE); // Bitmap Info Header Size
- bos.write4Bytes(width); // width
- bos.write4Bytes(height); // height
- bos.write2Bytes(1); // Number of Planes
- bos.write2Bytes(writer.getBitsPerPixel()); // Bits Per Pixel
-
- bos.write4Bytes(BI_RGB); // Compression
- bos.write4Bytes(imagedata.length); // Bitmap Data Size
- bos.write4Bytes(0); // HResolution
- bos.write4Bytes(0); // VResolution
- if (palette == null)
- bos.write4Bytes(0); // Colors
- else
- bos.write4Bytes(palette.length()); // Colors
- bos.write4Bytes(0); // Important Colors
- // bos.write_4_bytes(0); // Compression
- }
-
- { // write Palette
- writer.writePalette(bos);
- }
- { // write Image Data
- bos.writeByteArray(imagedata);
- }
- }
-
- /**
- * Extracts embedded XML metadata as XML string.
- * <p>
- *
- * @param byteSource
- * File containing image data.
- * @param params
- * Map of optional parameters, defined in SanselanConstants.
- * @return Xmp Xml as String, if present. Otherwise, returns null.
- */
- public String getXmpXml(ByteSource byteSource, Map params)
- throws ImageReadException, IOException
- {
- return null;
- }
+ public BmpImageParser()
+ {
+ super.setByteOrder(BYTE_ORDER_INTEL);
+ }
+
+ public String getName()
+ {
+ return "Bmp-Custom";
+ }
+
+ public String getDefaultExtension()
+ {
+ return DEFAULT_EXTENSION;
+ }
+
+ private static final String DEFAULT_EXTENSION = ".bmp";
+
+ private static final String ACCEPTED_EXTENSIONS[] = { DEFAULT_EXTENSION, };
+
+ protected String[] getAcceptedExtensions()
+ {
+ return ACCEPTED_EXTENSIONS;
+ }
+
+ protected ImageFormat[] getAcceptedTypes()
+ {
+ return new ImageFormat[] { ImageFormat.IMAGE_FORMAT_BMP, //
+ };
+ }
+
+ private static final byte BMP_HEADER_SIGNATURE[] = { 0x42, 0x4d, };
+
+ private BmpHeaderInfo readBmpHeaderInfo(InputStream is,
+ FormatCompliance formatCompliance, boolean verbose)
+ throws ImageReadException, IOException
+ {
+ byte identifier1 = readByte("Identifier1", is, "Not a Valid BMP File");
+ byte identifier2 = readByte("Identifier2", is, "Not a Valid BMP File");
+
+ if (formatCompliance != null)
+ {
+ formatCompliance.compare_bytes("Signature", BMP_HEADER_SIGNATURE,
+ new byte[] { identifier1, identifier2, });
+ }
+
+ int fileSize = read4Bytes("File Size", is, "Not a Valid BMP File");
+ int reserved = read4Bytes("Reserved", is, "Not a Valid BMP File");
+ int bitmapDataOffset = read4Bytes("Bitmap Data Offset", is,
+ "Not a Valid BMP File");
+
+ int bitmapHeaderSize = read4Bytes("Bitmap Header Size", is,
+ "Not a Valid BMP File");
+ int width = read4Bytes("Width", is, "Not a Valid BMP File");
+ int height = read4Bytes("Height", is, "Not a Valid BMP File");
+ int planes = read2Bytes("Planes", is, "Not a Valid BMP File");
+ int bitsPerPixel = read2Bytes("Bits Per Pixel", is,
+ "Not a Valid BMP File");
+ int compression = read4Bytes("Compression", is, "Not a Valid BMP File");
+ int bitmapDataSize = read4Bytes("Bitmap Data Size", is,
+ "Not a Valid BMP File");
+ int hResolution = read4Bytes("HResolution", is, "Not a Valid BMP File");
+ int vResolution = read4Bytes("VResolution", is, "Not a Valid BMP File");
+ int colorsUsed = read4Bytes("ColorsUsed", is, "Not a Valid BMP File");
+ int colorsImportant = read4Bytes("ColorsImportant", is,
+ "Not a Valid BMP File");
+
+ if (verbose)
+ {
+ this.debugNumber("identifier1", identifier1, 1);
+ this.debugNumber("identifier2", identifier2, 1);
+ this.debugNumber("fileSize", fileSize, 4);
+ this.debugNumber("reserved", reserved, 4);
+ this.debugNumber("bitmapDataOffset", bitmapDataOffset, 4);
+ this.debugNumber("bitmapHeaderSize", bitmapHeaderSize, 4);
+ this.debugNumber("width", width, 4);
+ this.debugNumber("height", height, 4);
+ this.debugNumber("planes", planes, 2);
+ this.debugNumber("bitsPerPixel", bitsPerPixel, 2);
+ this.debugNumber("compression", compression, 4);
+ this.debugNumber("bitmapDataSize", bitmapDataSize, 4);
+ this.debugNumber("hResolution", hResolution, 4);
+ this.debugNumber("vResolution", vResolution, 4);
+ this.debugNumber("colorsUsed", colorsUsed, 4);
+ this.debugNumber("colorsImportant", colorsImportant, 4);
+ }
+
+ BmpHeaderInfo result = new BmpHeaderInfo(identifier1, identifier2,
+ fileSize, reserved, bitmapDataOffset, bitmapHeaderSize, width,
+ height, planes, bitsPerPixel, compression, bitmapDataSize,
+ hResolution, vResolution, colorsUsed, colorsImportant);
+ return result;
+ }
+
+ private static final int BI_RGB = 0;
+ private static final int BI_RLE4 = 2;
+ private static final int BI_RLE8 = 1;
+ private static final int BI_BITFIELDS = 3;
+
+ private byte[] getRLEBytes(InputStream is, int RLESamplesPerByte)
+ throws ImageReadException, IOException
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ // this.setDebug(true);
+
+ boolean done = false;
+ while (!done)
+ {
+ int a = 0xff & this.readByte("RLE a", is, "BMP: Bad RLE");
+ baos.write(a);
+ int b = 0xff & this.readByte("RLE b", is, "BMP: Bad RLE");
+ baos.write(b);
+
+ if (a == 0)
+ {
+ switch (b)
+ {
+ case 0: // EOL
+ break;
+ case 1: // EOF
+ // System.out.println("xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
+ // );
+ done = true;
+ break;
+ case 2: {
+ // System.out.println("xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
+ // );
+ int c = 0xff & this.readByte("RLE c", is, "BMP: Bad RLE");
+ baos.write(c);
+ int d = 0xff & this.readByte("RLE d", is, "BMP: Bad RLE");
+ baos.write(d);
+
+ }
+ break;
+ default: {
+ int size = b / RLESamplesPerByte;
+ if ((b % RLESamplesPerByte) > 0)
+ size++;
+ if ((size % 2) != 0)
+ size++;
+
+ // System.out.println("b: " + b);
+ // System.out.println("size: " + size);
+ // System.out.println("RLESamplesPerByte: " +
+ // RLESamplesPerByte);
+ // System.out.println("xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
+ // );
+ byte bytes[] = this.readByteArray("bytes", size, is,
+ "RLE: Absolute Mode");
+ baos.write(bytes);
+ }
+ break;
+ }
+ }
+ }
+
+ return baos.toByteArray();
+ }
+
+ private ImageContents readImageContents(InputStream is,
+ FormatCompliance formatCompliance, boolean verbose)
+ throws ImageReadException, IOException
+ {
+ BmpHeaderInfo bhi = readBmpHeaderInfo(is, formatCompliance, verbose);
+
+ int colorTableSize = bhi.colorsUsed;
+ if (colorTableSize == 0)
+ colorTableSize = (1 << bhi.bitsPerPixel);
+
+ if (verbose)
+ {
+ this.debugNumber("ColorsUsed", bhi.colorsUsed, 4);
+ this.debugNumber("BitsPerPixel", bhi.bitsPerPixel, 4);
+ this.debugNumber("ColorTableSize", colorTableSize, 4);
+ this.debugNumber("bhi.colorsUsed", bhi.colorsUsed, 4);
+ this.debugNumber("Compression", bhi.compression, 4);
+ }
+
+ int paletteLength;
+ int rleSamplesPerByte = 0;
+ boolean rle = false;
+
+ switch (bhi.compression)
+ {
+ case BI_RGB:
+ if (verbose)
+ System.out.println("Compression: BI_RGB");
+ if (bhi.bitsPerPixel <= 8)
+ paletteLength = 4 * colorTableSize;
+ else
+ paletteLength = 0;
+ // BytesPerPaletteEntry = 0;
+ // System.out.println("Compression: BI_RGBx2: " + bhi.BitsPerPixel);
+ // System.out.println("Compression: BI_RGBx2: " + (bhi.BitsPerPixel
+ // <= 16));
+ break;
+
+ case BI_RLE4:
+ if (verbose)
+ System.out.println("Compression: BI_RLE4");
+ paletteLength = 4 * colorTableSize;
+ rleSamplesPerByte = 2;
+ // ExtraBitsPerPixel = 4;
+ rle = true;
+ // // BytesPerPixel = 2;
+ // // BytesPerPaletteEntry = 0;
+ break;
+ //
+ case BI_RLE8:
+ if (verbose)
+ System.out.println("Compression: BI_RLE8");
+ paletteLength = 4 * colorTableSize;
+ rleSamplesPerByte = 1;
+ // ExtraBitsPerPixel = 8;
+ rle = true;
+ // BytesPerPixel = 2;
+ // BytesPerPaletteEntry = 0;
+ break;
+ //
+ case BI_BITFIELDS:
+ if (verbose)
+ System.out.println("Compression: BI_BITFIELDS");
+ paletteLength = 3 * 4; // TODO: is this right? are the masks always
+ // LONGs?
+ // BytesPerPixel = 2;
+ // BytesPerPaletteEntry = 4;
+ break;
+
+ default:
+ throw new ImageReadException("BMP: Unknown Compression: "
+ + bhi.compression);
+ }
+
+ byte colorTable[] = null;
+ if (paletteLength > 0)
+ colorTable = this.readByteArray("ColorTable", paletteLength, is,
+ "Not a Valid BMP File");
+
+ if (verbose)
+ {
+ this.debugNumber("paletteLength", paletteLength, 4);
+ System.out.println("ColorTable: "
+ + ((colorTable == null) ? "null" : "" + colorTable.length));
+ }
+
+ int pixelCount = bhi.width * bhi.height;
+
+ int imageLineLength = ((((bhi.bitsPerPixel) * bhi.width) + 7) / 8);
+
+ if (verbose)
+ {
+ // this.debugNumber("Total BitsPerPixel",
+ // (ExtraBitsPerPixel + bhi.BitsPerPixel), 4);
+ // this.debugNumber("Total Bit Per Line",
+ // ((ExtraBitsPerPixel + bhi.BitsPerPixel) * bhi.Width), 4);
+ // this.debugNumber("ExtraBitsPerPixel", ExtraBitsPerPixel, 4);
+ this.debugNumber("bhi.Width", bhi.width, 4);
+ this.debugNumber("bhi.Height", bhi.height, 4);
+ this.debugNumber("ImageLineLength", imageLineLength, 4);
+ // this.debugNumber("imageDataSize", imageDataSize, 4);
+ this.debugNumber("PixelCount", pixelCount, 4);
+ }
+ // int ImageLineLength = BytesPerPixel * bhi.Width;
+ while ((imageLineLength % 4) != 0)
+ imageLineLength++;
+
+ final int headerSize = BITMAP_FILE_HEADER_SIZE
+ + BITMAP_INFO_HEADER_SIZE;
+ int expectedDataOffset = headerSize + paletteLength;
+
+ if (verbose)
+ {
+ this.debugNumber("bhi.BitmapDataOffset", bhi.bitmapDataOffset, 4);
+ this.debugNumber("expectedDataOffset", expectedDataOffset, 4);
+ }
+ int extraBytes = bhi.bitmapDataOffset - expectedDataOffset;
+ if (extraBytes < 0)
+ throw new ImageReadException("BMP has invalid image data offset: "
+ + bhi.bitmapDataOffset + " (expected: "
+ + expectedDataOffset + ", paletteLength: " + paletteLength
+ + ", headerSize: " + headerSize + ")");
+ else if (extraBytes > 0)
+ this.readByteArray("BitmapDataOffset", extraBytes, is,
+ "Not a Valid BMP File");
+
+ int imageDataSize = bhi.height * imageLineLength;
+
+ if (verbose)
+ this.debugNumber("imageDataSize", imageDataSize, 4);
+
+ byte imageData[];
+ if (rle)
+ imageData = getRLEBytes(is, rleSamplesPerByte);
+ else
+ imageData = this.readByteArray("ImageData", imageDataSize, is,
+ "Not a Valid BMP File");
+
+ if (verbose)
+ this.debugNumber("ImageData.length", imageData.length, 4);
+
+ PixelParser pixelParser;
+
+ switch (bhi.compression)
+ {
+ case BI_RLE4:
+ case BI_RLE8:
+ pixelParser = new PixelParserRle(bhi, colorTable, imageData);
+ break;
+ case BI_RGB:
+ pixelParser = new PixelParserRgb(bhi, colorTable, imageData);
+ break;
+ case BI_BITFIELDS:
+ pixelParser = new PixelParserBitFields(bhi, colorTable, imageData);
+ break;
+ default:
+ throw new ImageReadException("BMP: Unknown Compression: "
+ + bhi.compression);
+ }
+
+ return new ImageContents(bhi, colorTable, imageData, pixelParser);
+ }
+
+ private BmpHeaderInfo readBmpHeaderInfo(ByteSource byteSource,
+ boolean verbose) throws ImageReadException, IOException
+ {
+ InputStream is = null;
+ try
+ {
+ is = byteSource.getInputStream();
+
+ // readSignature(is);
+ return readBmpHeaderInfo(is, null, verbose);
+ } finally
+ {
+ try
+ {
+ if (is != null) {
+ is.close();
+ }
+ } catch (Exception e)
+ {
+ Debug.debug(e);
+ }
+
+ }
+ }
+
+ public byte[] getICCProfileBytes(ByteSource byteSource, Map params)
+ throws ImageReadException, IOException
+ {
+ return null;
+ }
+
+ public Dimension getImageSize(ByteSource byteSource, Map params)
+ throws ImageReadException, IOException
+ {
+ // make copy of params; we'll clear keys as we consume them.
+ params = (params == null) ? new HashMap() : new HashMap(params);
+
+ boolean verbose = ParamMap.getParamBoolean(params, PARAM_KEY_VERBOSE,
+ false);
+
+ if (params.containsKey(PARAM_KEY_VERBOSE))
+ params.remove(PARAM_KEY_VERBOSE);
+
+ if (params.size() > 0)
+ {
+ Object firstKey = params.keySet().iterator().next();
+ throw new ImageReadException("Unknown parameter: " + firstKey);
+ }
+
+ BmpHeaderInfo bhi = readBmpHeaderInfo(byteSource, verbose);
+
+ if (bhi == null)
+ throw new ImageReadException("BMP: couldn't read header");
+
+ return new Dimension(bhi.width, bhi.height);
+
+ }
+
+ public byte[] embedICCProfile(byte image[], byte profile[])
+ {
+ return null;
+ }
+
+ public boolean embedICCProfile(File src, File dst, byte profile[])
+ {
+ return false;
+ }
+
+ public IImageMetadata getMetadata(ByteSource byteSource, Map params)
+ throws ImageReadException, IOException
+ {
+ return null;
+ }
+
+ private String getBmpTypeDescription(int Identifier1, int Identifier2)
+ {
+ if ((Identifier1 == 'B') && (Identifier2 == 'M'))
+ return "Windows 3.1x, 95, NT,";
+ if ((Identifier1 == 'B') && (Identifier2 == 'A'))
+ return "OS/2 Bitmap Array";
+ if ((Identifier1 == 'C') && (Identifier2 == 'I'))
+ return "OS/2 Color Icon";
+ if ((Identifier1 == 'C') && (Identifier2 == 'P'))
+ return "OS/2 Color Pointer";
+ if ((Identifier1 == 'I') && (Identifier2 == 'C'))
+ return "OS/2 Icon";
+ if ((Identifier1 == 'P') && (Identifier2 == 'T'))
+ return "OS/2 Pointer";
+
+ return "Unknown";
+ }
+
+ public ImageInfo getImageInfo(ByteSource byteSource, Map params)
+ throws ImageReadException, IOException
+ {
+ // make copy of params; we'll clear keys as we consume them.
+ params = (params == null) ? new HashMap() : new HashMap(params);
+
+ boolean verbose = ParamMap.getParamBoolean(params, PARAM_KEY_VERBOSE,
+ false);
+
+ if (params.containsKey(PARAM_KEY_VERBOSE))
+ params.remove(PARAM_KEY_VERBOSE);
+
+ if (params.size() > 0)
+ {
+ Object firstKey = params.keySet().iterator().next();
+ throw new ImageReadException("Unknown parameter: " + firstKey);
+ }
+
+ ImageContents ic = readImageContents(byteSource.getInputStream(),
+ FormatCompliance.getDefault(), verbose);
+
+ if (ic == null)
+ throw new ImageReadException("Couldn't read BMP Data");
+
+ BmpHeaderInfo bhi = ic.bhi;
+ byte colorTable[] = ic.colorTable;
+
+ if (bhi == null)
+ throw new ImageReadException("BMP: couldn't read header");
+
+ int height = bhi.height;
+ int width = bhi.width;
+
+ ArrayList comments = new ArrayList();
+ // TODO: comments...
+
+ int bitsPerPixel = bhi.bitsPerPixel;
+ ImageFormat format = ImageFormat.IMAGE_FORMAT_BMP;
+ String name = "BMP Windows Bitmap";
+ String mimeType = "image/x-ms-bmp";
+ // we ought to count images, but don't yet.
+ int numberOfImages = -1;
+ // not accurate ... only reflects first
+ boolean isProgressive = false;
+ // boolean isProgressive = (fPNGChunkIHDR.InterlaceMethod != 0);
+ //
+ // pixels per meter
+ int physicalWidthDpi = (int) (bhi.hResolution * 1000.0 / 2.54);
+ float physicalWidthInch = (float) ((double) width / (double) physicalWidthDpi);
+ // int physicalHeightDpi = 72;
+ int physicalHeightDpi = (int) (bhi.vResolution * 1000.0 / 2.54);
+ float physicalHeightInch = (float) ((double) height / (double) physicalHeightDpi);
+
+ String formatDetails = "Bmp (" + (char) bhi.identifier1
+ + (char) bhi.identifier2 + ": "
+ + getBmpTypeDescription(bhi.identifier1, bhi.identifier2) + ")";
+
+ boolean isTransparent = false;
+
+ boolean usesPalette = colorTable != null;
+ int colorType = ImageInfo.COLOR_TYPE_RGB;
+ String compressionAlgorithm = ImageInfo.COMPRESSION_ALGORITHM_RLE;
+
+ ImageInfo result = new ImageInfo(formatDetails, bitsPerPixel, comments,
+ format, name, height, mimeType, numberOfImages,
+ physicalHeightDpi, physicalHeightInch, physicalWidthDpi,
+ physicalWidthInch, width, isProgressive, isTransparent,
+ usesPalette, colorType, compressionAlgorithm);
+
+ return result;
+ }
+
+ public boolean dumpImageFile(PrintWriter pw, ByteSource byteSource)
+ throws ImageReadException, IOException
+ {
+ pw.println("bmp.dumpImageFile");
+
+ ImageInfo imageData = getImageInfo(byteSource, null);
+ if (imageData == null)
+ return false;
+
+ imageData.toString(pw, "");
+
+ pw.println("");
+
+ return true;
+ }
+
+ public FormatCompliance getFormatCompliance(ByteSource byteSource)
+ throws ImageReadException, IOException
+ {
+ boolean verbose = false;
+
+ FormatCompliance result = new FormatCompliance(byteSource
+ .getDescription());
+
+ readImageContents(byteSource.getInputStream(), result, verbose);
+
+ return result;
+ }
+
+ public BufferedImage getBufferedImage(ByteSource byteSource, Map params)
+ throws ImageReadException, IOException
+ {
+ return getBufferedImage(byteSource.getInputStream(), params);
+ }
+
+ public BufferedImage getBufferedImage(InputStream inputStream, Map params)
+ throws ImageReadException, IOException
+ {
+ // make copy of params; we'll clear keys as we consume them.
+ params = (params == null) ? new HashMap() : new HashMap(params);
+
+ boolean verbose = ParamMap.getParamBoolean(params, PARAM_KEY_VERBOSE,
+ false);
+
+ if (params.containsKey(PARAM_KEY_VERBOSE))
+ params.remove(PARAM_KEY_VERBOSE);
+ if (params.containsKey(BUFFERED_IMAGE_FACTORY))
+ params.remove(BUFFERED_IMAGE_FACTORY);
+
+ if (params.size() > 0)
+ {
+ Object firstKey = params.keySet().iterator().next();
+ throw new ImageReadException("Unknown parameter: " + firstKey);
+ }
+
+ ImageContents ic = readImageContents(inputStream,
+ FormatCompliance.getDefault(), verbose);
+ if (ic == null)
+ throw new ImageReadException("Couldn't read BMP Data");
+
+ BmpHeaderInfo bhi = ic.bhi;
+ // byte colorTable[] = ic.colorTable;
+ // byte imageData[] = ic.imageData;
+
+ int width = bhi.width;
+ int height = bhi.height;
+
+ boolean hasAlpha = false;
+ BufferedImage result = getBufferedImageFactory(params)
+ .getColorBufferedImage(width, height, hasAlpha);
+
+ if (verbose)
+ {
+ System.out.println("width: " + width);
+ System.out.println("height: " + height);
+ System.out.println("width*height: " + width * height);
+ System.out.println("width*height*4: " + width * height * 4);
+ }
+
+ PixelParser pixelParser = ic.pixelParser;
+
+ pixelParser.processImage(result);
+
+ return result;
+
+ }
+
+ private static final int BITMAP_FILE_HEADER_SIZE = 14;
+ private static final int BITMAP_INFO_HEADER_SIZE = 40;
+
+ public void writeImage(BufferedImage src, OutputStream os, Map params)
+ throws ImageWriteException, IOException
+ {
+ // make copy of params; we'll clear keys as we consume them.
+ params = (params == null) ? new HashMap() : new HashMap(params);
+
+ // clear format key.
+ if (params.containsKey(PARAM_KEY_FORMAT))
+ params.remove(PARAM_KEY_FORMAT);
+
+ if (params.size() > 0)
+ {
+ Object firstKey = params.keySet().iterator().next();
+ throw new ImageWriteException("Unknown parameter: " + firstKey);
+ }
+
+ final SimplePalette palette = new PaletteFactory().makePaletteSimple(
+ src, 256);
+
+ BMPWriter writer = null;
+ if (palette == null)
+ writer = new BMPWriterRGB();
+ else
+ writer = new BMPWriterPalette(palette);
+
+ byte imagedata[] = writer.getImageData(src);
+ BinaryOutputStream bos = new BinaryOutputStream(os, BYTE_ORDER_INTEL);
+
+ {
+ // write BitmapFileHeader
+ os.write(0x42); // B, Windows 3.1x, 95, NT, Bitmap
+ os.write(0x4d); // M
+
+ int filesize = BITMAP_FILE_HEADER_SIZE + BITMAP_INFO_HEADER_SIZE + // header
+ // size
+ 4 * writer.getPaletteSize() + // palette size in bytes
+ imagedata.length;
+ bos.write4Bytes(filesize);
+
+ bos.write4Bytes(0); // reserved
+ bos.write4Bytes(BITMAP_FILE_HEADER_SIZE + BITMAP_INFO_HEADER_SIZE
+ + 4 * writer.getPaletteSize()); // Bitmap Data Offset
+ }
+
+ int width = src.getWidth();
+ int height = src.getHeight();
+
+ { // write BitmapInfoHeader
+ bos.write4Bytes(BITMAP_INFO_HEADER_SIZE); // Bitmap Info Header Size
+ bos.write4Bytes(width); // width
+ bos.write4Bytes(height); // height
+ bos.write2Bytes(1); // Number of Planes
+ bos.write2Bytes(writer.getBitsPerPixel()); // Bits Per Pixel
+
+ bos.write4Bytes(BI_RGB); // Compression
+ bos.write4Bytes(imagedata.length); // Bitmap Data Size
+ bos.write4Bytes(0); // HResolution
+ bos.write4Bytes(0); // VResolution
+ if (palette == null)
+ bos.write4Bytes(0); // Colors
+ else
+ bos.write4Bytes(palette.length()); // Colors
+ bos.write4Bytes(0); // Important Colors
+ // bos.write_4_bytes(0); // Compression
+ }
+
+ { // write Palette
+ writer.writePalette(bos);
+ }
+ { // write Image Data
+ bos.writeByteArray(imagedata);
+ }
+ }
+
+ /**
+ * Extracts embedded XML metadata as XML string.
+ * <p>
+ *
+ * @param byteSource
+ * File containing image data.
+ * @param params
+ * Map of optional parameters, defined in SanselanConstants.
+ * @return Xmp Xml as String, if present. Otherwise, returns null.
+ */
+ public String getXmpXml(ByteSource byteSource, Map params)
+ throws ImageReadException, IOException
+ {
+ return null;
+ }
}
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/ImageContents.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/ImageContents.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/ImageContents.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/ImageContents.java Fri Sep 10 16:33:35 2010
@@ -20,18 +20,18 @@ import org.apache.sanselan.formats.bmp.p
class ImageContents
{
- public final BmpHeaderInfo bhi;
- public final byte colorTable[];
- public final byte imageData[];
- public final PixelParser pixelParser;
+ public final BmpHeaderInfo bhi;
+ public final byte colorTable[];
+ public final byte imageData[];
+ public final PixelParser pixelParser;
- public ImageContents(BmpHeaderInfo bhi, byte ColorTable[],
- byte ImageData[], PixelParser fPixelParser)
- {
- this.bhi = bhi;
- this.colorTable = ColorTable;
- this.imageData = ImageData;
- this.pixelParser = fPixelParser;
- }
+ public ImageContents(BmpHeaderInfo bhi, byte ColorTable[],
+ byte ImageData[], PixelParser fPixelParser)
+ {
+ this.bhi = bhi;
+ this.colorTable = ColorTable;
+ this.imageData = ImageData;
+ this.pixelParser = fPixelParser;
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParser.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParser.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParser.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParser.java Fri Sep 10 16:33:35 2010
@@ -26,36 +26,36 @@ import org.apache.sanselan.formats.bmp.B
public abstract class PixelParser
{
- public final BmpHeaderInfo bhi;
- public final byte colorTable[];
- public final byte imageData[];
-
- protected final BinaryFileParser bfp;
- protected final ByteArrayInputStream is;
-
- public PixelParser(BmpHeaderInfo bhi, byte ColorTable[], byte ImageData[])
- {
- this.bhi = bhi;
- this.colorTable = ColorTable;
- this.imageData = ImageData;
-
- bfp = new BinaryFileParser(BinaryFileParser.BYTE_ORDER_INTEL);
- is = new ByteArrayInputStream(ImageData);
- }
-
- public abstract void processImage(BufferedImage bi)
- throws ImageReadException, IOException;
-
- protected int getColorTableRGB(int index)
- {
- index *= 4;
- int blue = 0xff & colorTable[index + 0];
- int green = 0xff & colorTable[index + 1];
- int red = 0xff & colorTable[index + 2];
- int alpha = 0xff;
-
- int rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
- return rgb;
- }
+ public final BmpHeaderInfo bhi;
+ public final byte colorTable[];
+ public final byte imageData[];
+
+ protected final BinaryFileParser bfp;
+ protected final ByteArrayInputStream is;
+
+ public PixelParser(BmpHeaderInfo bhi, byte ColorTable[], byte ImageData[])
+ {
+ this.bhi = bhi;
+ this.colorTable = ColorTable;
+ this.imageData = ImageData;
+
+ bfp = new BinaryFileParser(BinaryFileParser.BYTE_ORDER_INTEL);
+ is = new ByteArrayInputStream(ImageData);
+ }
+
+ public abstract void processImage(BufferedImage bi)
+ throws ImageReadException, IOException;
+
+ protected int getColorTableRGB(int index)
+ {
+ index *= 4;
+ int blue = 0xff & colorTable[index + 0];
+ int green = 0xff & colorTable[index + 1];
+ int red = 0xff & colorTable[index + 2];
+ int alpha = 0xff;
+
+ int rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
+ return rgb;
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserBitFields.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserBitFields.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserBitFields.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserBitFields.java Fri Sep 10 16:33:35 2010
@@ -26,104 +26,104 @@ import org.apache.sanselan.formats.bmp.B
public class PixelParserBitFields extends PixelParserSimple
{
- private final int redShift;
- private final int greenShift;
- private final int blueShift;
-
- private final int redMask;
- private final int greenMask;
- private final int blueMask;
-
- public PixelParserBitFields(BmpHeaderInfo bhi, byte ColorTable[],
- byte ImageData[]) throws ImageReadException, IOException
- {
- super(bhi, ColorTable, ImageData);
-
- InputStream bais = new ByteArrayInputStream(ColorTable);
-
- redMask = bfp.read4Bytes("redMask", bais,
- "BMP BI_BITFIELDS Bad Color Table");
- greenMask = bfp.read4Bytes("greenMask", bais,
- "BMP BI_BITFIELDS Bad Color Table");
- blueMask = bfp.read4Bytes("blueMask", bais,
- "BMP BI_BITFIELDS Bad Color Table");
-
- redShift = getMaskShift(redMask);
- greenShift = getMaskShift(greenMask);
- blueShift = getMaskShift(blueMask);
- }
-
- private int getMaskShift(int mask)
- {
- int trailingZeroes = 0;
-
- while ((0x1 & mask) == 0)
- {
- mask = 0x7fffffff & (mask >> 1);
- trailingZeroes++;
- }
-
- int maskLength = 0;
-
- while ((0x1 & mask) == 1)
- {
- mask = 0x7fffffff & (mask >> 1);
- maskLength++;
- }
-
- return (trailingZeroes - (8 - maskLength));
- }
- private int bytecount = 0;
-
- public int getNextRGB() throws ImageReadException, IOException
- {
- int data;
-
- if (bhi.bitsPerPixel == 8)
- {
- data = 0xff & imageData[bytecount + 0];
- bytecount += 1;
- }
- else if (bhi.bitsPerPixel == 24)
- {
- data = bfp.read3Bytes("Pixel", is, "BMP Image Data");
- bytecount += 3;
- }
- else if (bhi.bitsPerPixel == 32)
- {
- data = bfp.read4Bytes("Pixel", is, "BMP Image Data");
- bytecount += 4;
- }
- else if (bhi.bitsPerPixel == 16)
- {
- data = bfp.read2Bytes("Pixel", is, "BMP Image Data");
- bytecount += 2;
- }
- else
- throw new ImageReadException("Unknown BitsPerPixel: "
- + bhi.bitsPerPixel);
-
- int red = (redMask & data);
- int green = (greenMask & data);
- int blue = (blueMask & data);
-
- red = (redShift >= 0) ? red >> redShift : red << -redShift;
- green = (greenShift >= 0) ? green >> greenShift : green << -greenShift;
- blue = (blueShift >= 0) ? blue >> blueShift : blue << -blueShift;
-
- int alpha = 0xff;
-
- int rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
-
- return rgb;
- }
-
- public void newline() throws ImageReadException, IOException
- {
- while (((bytecount) % 4) != 0)
- {
- bfp.readByte("Pixel", is, "BMP Image Data");
- bytecount++;
- }
- }
+ private final int redShift;
+ private final int greenShift;
+ private final int blueShift;
+
+ private final int redMask;
+ private final int greenMask;
+ private final int blueMask;
+
+ public PixelParserBitFields(BmpHeaderInfo bhi, byte ColorTable[],
+ byte ImageData[]) throws ImageReadException, IOException
+ {
+ super(bhi, ColorTable, ImageData);
+
+ InputStream bais = new ByteArrayInputStream(ColorTable);
+
+ redMask = bfp.read4Bytes("redMask", bais,
+ "BMP BI_BITFIELDS Bad Color Table");
+ greenMask = bfp.read4Bytes("greenMask", bais,
+ "BMP BI_BITFIELDS Bad Color Table");
+ blueMask = bfp.read4Bytes("blueMask", bais,
+ "BMP BI_BITFIELDS Bad Color Table");
+
+ redShift = getMaskShift(redMask);
+ greenShift = getMaskShift(greenMask);
+ blueShift = getMaskShift(blueMask);
+ }
+
+ private int getMaskShift(int mask)
+ {
+ int trailingZeroes = 0;
+
+ while ((0x1 & mask) == 0)
+ {
+ mask = 0x7fffffff & (mask >> 1);
+ trailingZeroes++;
+ }
+
+ int maskLength = 0;
+
+ while ((0x1 & mask) == 1)
+ {
+ mask = 0x7fffffff & (mask >> 1);
+ maskLength++;
+ }
+
+ return (trailingZeroes - (8 - maskLength));
+ }
+ private int bytecount = 0;
+
+ public int getNextRGB() throws ImageReadException, IOException
+ {
+ int data;
+
+ if (bhi.bitsPerPixel == 8)
+ {
+ data = 0xff & imageData[bytecount + 0];
+ bytecount += 1;
+ }
+ else if (bhi.bitsPerPixel == 24)
+ {
+ data = bfp.read3Bytes("Pixel", is, "BMP Image Data");
+ bytecount += 3;
+ }
+ else if (bhi.bitsPerPixel == 32)
+ {
+ data = bfp.read4Bytes("Pixel", is, "BMP Image Data");
+ bytecount += 4;
+ }
+ else if (bhi.bitsPerPixel == 16)
+ {
+ data = bfp.read2Bytes("Pixel", is, "BMP Image Data");
+ bytecount += 2;
+ }
+ else
+ throw new ImageReadException("Unknown BitsPerPixel: "
+ + bhi.bitsPerPixel);
+
+ int red = (redMask & data);
+ int green = (greenMask & data);
+ int blue = (blueMask & data);
+
+ red = (redShift >= 0) ? red >> redShift : red << -redShift;
+ green = (greenShift >= 0) ? green >> greenShift : green << -greenShift;
+ blue = (blueShift >= 0) ? blue >> blueShift : blue << -blueShift;
+
+ int alpha = 0xff;
+
+ int rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
+
+ return rgb;
+ }
+
+ public void newline() throws ImageReadException, IOException
+ {
+ while (((bytecount) % 4) != 0)
+ {
+ bfp.readByte("Pixel", is, "BMP Image Data");
+ bytecount++;
+ }
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserRgb.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserRgb.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserRgb.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserRgb.java Fri Sep 10 16:33:35 2010
@@ -23,106 +23,106 @@ import org.apache.sanselan.formats.bmp.B
public class PixelParserRgb extends PixelParserSimple
{
- public PixelParserRgb(BmpHeaderInfo bhi, byte ColorTable[],
- byte ImageData[])
- {
- super(bhi, ColorTable, ImageData);
-
- }
-
- private int bytecount = 0;
- private int cached_bit_count = 0;
- private int cached_byte = 0;
-
- int pixelCount = 0;
-
- public int getNextRGB() throws ImageReadException, IOException
- {
- pixelCount++;
-
- if ((bhi.bitsPerPixel == 1) // always grayscale?
- || (bhi.bitsPerPixel == 4)) // always grayscale?
- {
- if (cached_bit_count < bhi.bitsPerPixel)
- {
- if (cached_bit_count != 0)
- throw new ImageReadException("Unexpected leftover bits: "
- + cached_bit_count + "/" + bhi.bitsPerPixel);
-
- cached_bit_count += 8;
- cached_byte =
- (0xff & imageData[bytecount]);
- bytecount++;
- }
- int cache_mask = (1 << bhi.bitsPerPixel) - 1;
- int sample = cache_mask & (cached_byte >> (8 - bhi.bitsPerPixel));
- cached_byte = 0xff & (cached_byte << bhi.bitsPerPixel);
- cached_bit_count -= bhi.bitsPerPixel;
-
- int rgb = getColorTableRGB(sample);
-
- return rgb;
- } else if (bhi.bitsPerPixel == 8) // always grayscale?
- {
- int sample = 0xff & imageData[bytecount + 0];
-
- int rgb = getColorTableRGB(sample);
-
- bytecount += 1;
-
- return rgb;
- } else if (bhi.bitsPerPixel == 16)
- {
- int data = bfp.read2Bytes("Pixel", is, "BMP Image Data");
-
- int blue = (0x1f & (data >> 0)) << 3;
- int green = (0x1f & (data >> 5)) << 3;
- int red = (0x1f & (data >> 10)) << 3;
- int alpha = 0xff;
-
- int rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
-
- bytecount += 2;
-
- return rgb;
- } else if (bhi.bitsPerPixel == 24)
- {
- int blue = 0xff & imageData[bytecount + 0];
- int green = 0xff & imageData[bytecount + 1];
- int red = 0xff & imageData[bytecount + 2];
- int alpha = 0xff;
-
- int rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
-
- bytecount += 3;
-
- return rgb;
- } else if (bhi.bitsPerPixel == 32)
- {
- int blue = 0xff & imageData[bytecount + 0];
- int green = 0xff & imageData[bytecount + 1];
- int red = 0xff & imageData[bytecount + 2];
- int alpha = 0xff;
-
- int rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
-
- bytecount += 4;
-
- return rgb;
- }
-
- throw new ImageReadException("Unknown BitsPerPixel: "
- + bhi.bitsPerPixel);
- }
-
- public void newline() throws ImageReadException, IOException
- {
- cached_bit_count = 0;
-
- while (((bytecount) % 4) != 0)
- {
- bfp.readByte("Pixel", is, "BMP Image Data");
- bytecount++;
- }
- }
+ public PixelParserRgb(BmpHeaderInfo bhi, byte ColorTable[],
+ byte ImageData[])
+ {
+ super(bhi, ColorTable, ImageData);
+
+ }
+
+ private int bytecount = 0;
+ private int cached_bit_count = 0;
+ private int cached_byte = 0;
+
+ int pixelCount = 0;
+
+ public int getNextRGB() throws ImageReadException, IOException
+ {
+ pixelCount++;
+
+ if ((bhi.bitsPerPixel == 1) // always grayscale?
+ || (bhi.bitsPerPixel == 4)) // always grayscale?
+ {
+ if (cached_bit_count < bhi.bitsPerPixel)
+ {
+ if (cached_bit_count != 0)
+ throw new ImageReadException("Unexpected leftover bits: "
+ + cached_bit_count + "/" + bhi.bitsPerPixel);
+
+ cached_bit_count += 8;
+ cached_byte =
+ (0xff & imageData[bytecount]);
+ bytecount++;
+ }
+ int cache_mask = (1 << bhi.bitsPerPixel) - 1;
+ int sample = cache_mask & (cached_byte >> (8 - bhi.bitsPerPixel));
+ cached_byte = 0xff & (cached_byte << bhi.bitsPerPixel);
+ cached_bit_count -= bhi.bitsPerPixel;
+
+ int rgb = getColorTableRGB(sample);
+
+ return rgb;
+ } else if (bhi.bitsPerPixel == 8) // always grayscale?
+ {
+ int sample = 0xff & imageData[bytecount + 0];
+
+ int rgb = getColorTableRGB(sample);
+
+ bytecount += 1;
+
+ return rgb;
+ } else if (bhi.bitsPerPixel == 16)
+ {
+ int data = bfp.read2Bytes("Pixel", is, "BMP Image Data");
+
+ int blue = (0x1f & (data >> 0)) << 3;
+ int green = (0x1f & (data >> 5)) << 3;
+ int red = (0x1f & (data >> 10)) << 3;
+ int alpha = 0xff;
+
+ int rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
+
+ bytecount += 2;
+
+ return rgb;
+ } else if (bhi.bitsPerPixel == 24)
+ {
+ int blue = 0xff & imageData[bytecount + 0];
+ int green = 0xff & imageData[bytecount + 1];
+ int red = 0xff & imageData[bytecount + 2];
+ int alpha = 0xff;
+
+ int rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
+
+ bytecount += 3;
+
+ return rgb;
+ } else if (bhi.bitsPerPixel == 32)
+ {
+ int blue = 0xff & imageData[bytecount + 0];
+ int green = 0xff & imageData[bytecount + 1];
+ int red = 0xff & imageData[bytecount + 2];
+ int alpha = 0xff;
+
+ int rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
+
+ bytecount += 4;
+
+ return rgb;
+ }
+
+ throw new ImageReadException("Unknown BitsPerPixel: "
+ + bhi.bitsPerPixel);
+ }
+
+ public void newline() throws ImageReadException, IOException
+ {
+ cached_bit_count = 0;
+
+ while (((bytecount) % 4) != 0)
+ {
+ bfp.readByte("Pixel", is, "BMP Image Data");
+ bytecount++;
+ }
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserRle.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserRle.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserRle.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserRle.java Fri Sep 10 16:33:35 2010
@@ -26,185 +26,185 @@ import org.apache.sanselan.formats.bmp.B
public class PixelParserRle extends PixelParser
{
- public PixelParserRle(BmpHeaderInfo bhi, byte ColorTable[],
- byte ImageData[])
- {
- super(bhi, ColorTable, ImageData);
-
- }
-
- private int getSamplesPerByte() throws ImageReadException, IOException
- {
- if (bhi.bitsPerPixel == 8)
- return 1;
- else if (bhi.bitsPerPixel == 4)
- return 2;
- else
- throw new ImageReadException("BMP RLE: bad BitsPerPixel: "
- + bhi.bitsPerPixel);
- }
-
- private int[] convertDataToSamples(int data) throws ImageReadException,
- IOException
- {
- int rgbs[];
- if (bhi.bitsPerPixel == 8)
- {
- rgbs = new int[1];
- rgbs[0] = getColorTableRGB(data);
- // pixels_written = 1;
- }
- else if (bhi.bitsPerPixel == 4)
- {
- rgbs = new int[2];
- int sample1 = data >> 4;
- int sample2 = 0x0f & data;
- rgbs[0] = getColorTableRGB(sample1);
- rgbs[1] = getColorTableRGB(sample2);
- // pixels_written = 2;
- }
- else
- throw new ImageReadException("BMP RLE: bad BitsPerPixel: "
- + bhi.bitsPerPixel);
-
- return rgbs;
- }
-
- private int processByteOfData(int rgbs[], int repeat, int x, int y,
- int width, int height, DataBuffer db, BufferedImage bi)
- throws ImageReadException
- {
- // int rbg
- int pixels_written = 0;
- for (int i = 0; i < repeat; i++)
- {
-
- if ((x >= 0) && (x < width) && (y >= 0) && (y < height))
- {
- // int rgb = 0xff000000;
- // rgb = getNextRGB();
- int rgb = rgbs[i % rgbs.length];
- // bi.setRGB(x, y, rgb);
- db.setElem(y * bhi.width + x, rgb);
- // bi.setRGB(x, y, 0xff00ff00);
- }
- else
- {
- System.out.println("skipping bad pixel (" + x + "," + y + ")");
- }
-
- x++;
- pixels_written++;
- }
-
- return pixels_written;
- }
-
- public void processImage(BufferedImage bi) throws ImageReadException,
- IOException
- {
- DataBuffer db = bi.getRaster().getDataBuffer();
-
- int count = 0;
- int width = bhi.width;
- int height = bhi.height;
- int x = 0, y = height - 1;
-
- // bfp.setDebug(true);
-
- boolean done = false;
- while (!done)
- {
- count++;
- // if (count > 100)
- // return;
-
- int a = 0xff & bfp.readByte("RLE (" + x + "," + y + ") a", is,
- "BMP: Bad RLE");
- // baos.write(a);
- int b = 0xff & bfp.readByte("RLE (" + x + "," + y + ") b", is,
- "BMP: Bad RLE");
- // baos.write(b);
-
- if (a == 0)
- {
- switch (b)
- {
- case 0 : // EOL
- {
- // System.out.println("EOL");
- y--;
- x = 0;
- }
- break;
- case 1 : // EOF
- // System.out
- // .println("xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
- done = true;
- break;
- case 2 : {
- // System.out
- // .println("xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
- int c = 0xff & bfp
- .readByte("RLE c", is, "BMP: Bad RLE");
- // baos.write(c);
- int d = 0xff & bfp
- .readByte("RLE d", is, "BMP: Bad RLE");
- // baos.write(d);
-
- }
- break;
- default : {
- // System.out
- // .println("xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
-
- int SamplesPerByte = getSamplesPerByte();
- int size = b / SamplesPerByte;
- if ((b % SamplesPerByte) > 0)
- size++;
- if ((size % 2) != 0)
- size++;
-
- // System.out.println("b: " + b);
- // System.out.println("size: " + size);
- // System.out.println("SamplesPerByte: " + SamplesPerByte);
-
- byte bytes[] = bfp.readByteArray("bytes", size, is,
- "RLE: Absolute Mode");
-
- int remaining = b;
-
- // while(true)
- for (int i = 0; remaining > 0; i++)
- // for (int i = 0; i < bytes.length; i++)
- {
- int samples[] = convertDataToSamples(0xff & bytes[i]);
- int towrite = Math.min(remaining, SamplesPerByte);
- // System.out.println("remaining: " + remaining);
- // System.out.println("SamplesPerByte: "
- // + SamplesPerByte);
- // System.out.println("towrite: " + towrite);
- int written = processByteOfData(samples, towrite,
- x, y, width, height, db, bi);
- // System.out.println("written: " + written);
- // System.out.println("");
- x += written;
- remaining -= written;
- }
- // baos.write(bytes);
- }
- break;
- }
- }
- else
- {
- int rgbs[] = convertDataToSamples(b);
+ public PixelParserRle(BmpHeaderInfo bhi, byte ColorTable[],
+ byte ImageData[])
+ {
+ super(bhi, ColorTable, ImageData);
+
+ }
+
+ private int getSamplesPerByte() throws ImageReadException, IOException
+ {
+ if (bhi.bitsPerPixel == 8)
+ return 1;
+ else if (bhi.bitsPerPixel == 4)
+ return 2;
+ else
+ throw new ImageReadException("BMP RLE: bad BitsPerPixel: "
+ + bhi.bitsPerPixel);
+ }
+
+ private int[] convertDataToSamples(int data) throws ImageReadException,
+ IOException
+ {
+ int rgbs[];
+ if (bhi.bitsPerPixel == 8)
+ {
+ rgbs = new int[1];
+ rgbs[0] = getColorTableRGB(data);
+ // pixels_written = 1;
+ }
+ else if (bhi.bitsPerPixel == 4)
+ {
+ rgbs = new int[2];
+ int sample1 = data >> 4;
+ int sample2 = 0x0f & data;
+ rgbs[0] = getColorTableRGB(sample1);
+ rgbs[1] = getColorTableRGB(sample2);
+ // pixels_written = 2;
+ }
+ else
+ throw new ImageReadException("BMP RLE: bad BitsPerPixel: "
+ + bhi.bitsPerPixel);
+
+ return rgbs;
+ }
+
+ private int processByteOfData(int rgbs[], int repeat, int x, int y,
+ int width, int height, DataBuffer db, BufferedImage bi)
+ throws ImageReadException
+ {
+ // int rbg
+ int pixels_written = 0;
+ for (int i = 0; i < repeat; i++)
+ {
+
+ if ((x >= 0) && (x < width) && (y >= 0) && (y < height))
+ {
+ // int rgb = 0xff000000;
+ // rgb = getNextRGB();
+ int rgb = rgbs[i % rgbs.length];
+ // bi.setRGB(x, y, rgb);
+ db.setElem(y * bhi.width + x, rgb);
+ // bi.setRGB(x, y, 0xff00ff00);
+ }
+ else
+ {
+ System.out.println("skipping bad pixel (" + x + "," + y + ")");
+ }
+
+ x++;
+ pixels_written++;
+ }
+
+ return pixels_written;
+ }
+
+ public void processImage(BufferedImage bi) throws ImageReadException,
+ IOException
+ {
+ DataBuffer db = bi.getRaster().getDataBuffer();
+
+ int count = 0;
+ int width = bhi.width;
+ int height = bhi.height;
+ int x = 0, y = height - 1;
+
+ // bfp.setDebug(true);
+
+ boolean done = false;
+ while (!done)
+ {
+ count++;
+ // if (count > 100)
+ // return;
+
+ int a = 0xff & bfp.readByte("RLE (" + x + "," + y + ") a", is,
+ "BMP: Bad RLE");
+ // baos.write(a);
+ int b = 0xff & bfp.readByte("RLE (" + x + "," + y + ") b", is,
+ "BMP: Bad RLE");
+ // baos.write(b);
+
+ if (a == 0)
+ {
+ switch (b)
+ {
+ case 0 : // EOL
+ {
+ // System.out.println("EOL");
+ y--;
+ x = 0;
+ }
+ break;
+ case 1 : // EOF
+ // System.out
+ // .println("xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
+ done = true;
+ break;
+ case 2 : {
+ // System.out
+ // .println("xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
+ int c = 0xff & bfp
+ .readByte("RLE c", is, "BMP: Bad RLE");
+ // baos.write(c);
+ int d = 0xff & bfp
+ .readByte("RLE d", is, "BMP: Bad RLE");
+ // baos.write(d);
+
+ }
+ break;
+ default : {
+ // System.out
+ // .println("xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
+
+ int SamplesPerByte = getSamplesPerByte();
+ int size = b / SamplesPerByte;
+ if ((b % SamplesPerByte) > 0)
+ size++;
+ if ((size % 2) != 0)
+ size++;
+
+ // System.out.println("b: " + b);
+ // System.out.println("size: " + size);
+ // System.out.println("SamplesPerByte: " + SamplesPerByte);
+
+ byte bytes[] = bfp.readByteArray("bytes", size, is,
+ "RLE: Absolute Mode");
+
+ int remaining = b;
+
+ // while(true)
+ for (int i = 0; remaining > 0; i++)
+ // for (int i = 0; i < bytes.length; i++)
+ {
+ int samples[] = convertDataToSamples(0xff & bytes[i]);
+ int towrite = Math.min(remaining, SamplesPerByte);
+ // System.out.println("remaining: " + remaining);
+ // System.out.println("SamplesPerByte: "
+ // + SamplesPerByte);
+ // System.out.println("towrite: " + towrite);
+ int written = processByteOfData(samples, towrite,
+ x, y, width, height, db, bi);
+ // System.out.println("written: " + written);
+ // System.out.println("");
+ x += written;
+ remaining -= written;
+ }
+ // baos.write(bytes);
+ }
+ break;
+ }
+ }
+ else
+ {
+ int rgbs[] = convertDataToSamples(b);
- x += processByteOfData(rgbs, a, x, y, width, height, db, bi);
- // x += processByteOfData(b, a, x, y, width, height, bi);
+ x += processByteOfData(rgbs, a, x, y, width, height, db, bi);
+ // x += processByteOfData(b, a, x, y, width, height, bi);
- }
- }
+ }
+ }
- }
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserSimple.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserSimple.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserSimple.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/pixelparsers/PixelParserSimple.java Fri Sep 10 16:33:35 2010
@@ -24,31 +24,31 @@ import org.apache.sanselan.formats.bmp.B
public abstract class PixelParserSimple extends PixelParser
{
- public PixelParserSimple(BmpHeaderInfo bhi, byte ColorTable[],
- byte ImageData[])
- {
- super(bhi, ColorTable, ImageData);
- }
-
- public abstract int getNextRGB() throws ImageReadException, IOException;
-
- public abstract void newline() throws ImageReadException, IOException;
-
- public void processImage(BufferedImage bi) throws ImageReadException,
- IOException
- {
-// DataBuffer db = bi.getRaster().getDataBuffer();
-
- for (int y = bhi.height - 1; y >= 0; y--)
- {
- for (int x = 0; x < bhi.width; x++)
- {
- int rgb = getNextRGB();
-
- bi.setRGB(x, y, rgb);
-// db.setElem(y * bhi.width + x, rgb);
- }
- newline();
- }
- }
+ public PixelParserSimple(BmpHeaderInfo bhi, byte ColorTable[],
+ byte ImageData[])
+ {
+ super(bhi, ColorTable, ImageData);
+ }
+
+ public abstract int getNextRGB() throws ImageReadException, IOException;
+
+ public abstract void newline() throws ImageReadException, IOException;
+
+ public void processImage(BufferedImage bi) throws ImageReadException,
+ IOException
+ {
+// DataBuffer db = bi.getRaster().getDataBuffer();
+
+ for (int y = bhi.height - 1; y >= 0; y--)
+ {
+ for (int x = 0; x < bhi.width; x++)
+ {
+ int rgb = getNextRGB();
+
+ bi.setRGB(x, y, rgb);
+// db.setElem(y * bhi.width + x, rgb);
+ }
+ newline();
+ }
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/writers/BMPWriter.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/writers/BMPWriter.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/writers/BMPWriter.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/writers/BMPWriter.java Fri Sep 10 16:33:35 2010
@@ -23,12 +23,12 @@ import org.apache.sanselan.common.Binary
public abstract class BMPWriter
{
- public abstract int getPaletteSize();
+ public abstract int getPaletteSize();
- public abstract int getBitsPerPixel();
+ public abstract int getBitsPerPixel();
- public abstract void writePalette(BinaryOutputStream bos)
- throws IOException;
+ public abstract void writePalette(BinaryOutputStream bos)
+ throws IOException;
- public abstract byte[] getImageData(BufferedImage src);
+ public abstract byte[] getImageData(BufferedImage src);
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/writers/BMPWriterPalette.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/writers/BMPWriterPalette.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/writers/BMPWriterPalette.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/writers/BMPWriterPalette.java Fri Sep 10 16:33:35 2010
@@ -25,104 +25,104 @@ import org.apache.sanselan.palette.Simpl
public class BMPWriterPalette extends BMPWriter
{
- private final SimplePalette palette;
- private final int bitsPerSample;
+ private final SimplePalette palette;
+ private final int bitsPerSample;
- public BMPWriterPalette(SimplePalette palette)
- {
- this.palette = palette;
-
- if (palette.length() <= 2)
- bitsPerSample = 1;
- else if (palette.length() <= 16)
- bitsPerSample = 4;
- else
- bitsPerSample = 8;
- }
-
- public int getPaletteSize()
- {
- return palette.length();
- }
-
- public int getBitsPerPixel()
- {
- return bitsPerSample;
- }
-
- public void writePalette(BinaryOutputStream bos) throws IOException
- {
- for (int i = 0; i < palette.length(); i++)
- {
- int rgb = palette.getEntry(i);
-
- int red = 0xff & (rgb >> 16);
- int green = 0xff & (rgb >> 8);
- int blue = 0xff & (rgb >> 0);
-
- bos.write(blue);
- bos.write(green);
- bos.write(red);
- bos.write(0);
- }
- }
-
- public byte[] getImageData(BufferedImage src)
- {
- int width = src.getWidth();
- int height = src.getHeight();
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- int bit_cache = 0;
- int bits_in_cache = 0;
-
- int bytecount = 0;
- for (int y = height - 1; y >= 0; y--)
- {
- for (int x = 0; x < width; x++)
- {
- int argb = src.getRGB(x, y);
- int rgb = 0xffffff & argb;
-
- int index = palette.getPaletteIndex(rgb);
-
- if (bitsPerSample == 8)
- {
- baos.write(0xff & index);
- bytecount++;
- } else
- // 4 or 1
- {
- bit_cache = (bit_cache << bitsPerSample) | index;
- bits_in_cache += bitsPerSample;
- if (bits_in_cache >= 8)
- {
- baos.write(0xff & bit_cache);
- bytecount++;
- bit_cache = 0;
- bits_in_cache = 0;
- }
- }
- }
-
- if (bits_in_cache > 0)
- {
- bit_cache = (bit_cache << (8 - bits_in_cache));
-
- baos.write(0xff & bit_cache);
- bytecount++;
- bit_cache = 0;
- bits_in_cache = 0;
- }
-
- while ((bytecount % 4) != 0)
- {
- baos.write(0);
- bytecount++;
- }
- }
+ public BMPWriterPalette(SimplePalette palette)
+ {
+ this.palette = palette;
+
+ if (palette.length() <= 2)
+ bitsPerSample = 1;
+ else if (palette.length() <= 16)
+ bitsPerSample = 4;
+ else
+ bitsPerSample = 8;
+ }
+
+ public int getPaletteSize()
+ {
+ return palette.length();
+ }
+
+ public int getBitsPerPixel()
+ {
+ return bitsPerSample;
+ }
+
+ public void writePalette(BinaryOutputStream bos) throws IOException
+ {
+ for (int i = 0; i < palette.length(); i++)
+ {
+ int rgb = palette.getEntry(i);
+
+ int red = 0xff & (rgb >> 16);
+ int green = 0xff & (rgb >> 8);
+ int blue = 0xff & (rgb >> 0);
+
+ bos.write(blue);
+ bos.write(green);
+ bos.write(red);
+ bos.write(0);
+ }
+ }
+
+ public byte[] getImageData(BufferedImage src)
+ {
+ int width = src.getWidth();
+ int height = src.getHeight();
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ int bit_cache = 0;
+ int bits_in_cache = 0;
+
+ int bytecount = 0;
+ for (int y = height - 1; y >= 0; y--)
+ {
+ for (int x = 0; x < width; x++)
+ {
+ int argb = src.getRGB(x, y);
+ int rgb = 0xffffff & argb;
+
+ int index = palette.getPaletteIndex(rgb);
+
+ if (bitsPerSample == 8)
+ {
+ baos.write(0xff & index);
+ bytecount++;
+ } else
+ // 4 or 1
+ {
+ bit_cache = (bit_cache << bitsPerSample) | index;
+ bits_in_cache += bitsPerSample;
+ if (bits_in_cache >= 8)
+ {
+ baos.write(0xff & bit_cache);
+ bytecount++;
+ bit_cache = 0;
+ bits_in_cache = 0;
+ }
+ }
+ }
+
+ if (bits_in_cache > 0)
+ {
+ bit_cache = (bit_cache << (8 - bits_in_cache));
+
+ baos.write(0xff & bit_cache);
+ bytecount++;
+ bit_cache = 0;
+ bits_in_cache = 0;
+ }
+
+ while ((bytecount % 4) != 0)
+ {
+ baos.write(0);
+ bytecount++;
+ }
+ }
- return baos.toByteArray();
- }
+ return baos.toByteArray();
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/writers/BMPWriterRGB.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/writers/BMPWriterRGB.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/writers/BMPWriterRGB.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/writers/BMPWriterRGB.java Fri Sep 10 16:33:35 2010
@@ -24,61 +24,61 @@ import org.apache.sanselan.common.Binary
public class BMPWriterRGB extends BMPWriter
{
- // private final boolean alpha;
- //
- // public BMPWriterRGB(boolean alpha)
- // {
- // this.alpha = alpha;
- // }
-
- public int getPaletteSize()
- {
- return 0;
- }
-
- public int getBitsPerPixel()
- {
- // return alpha ? 32 : 24;
- return 24;
- }
-
- public void writePalette(BinaryOutputStream bos) throws IOException
- {
- }
-
- public byte[] getImageData(BufferedImage src)
- {
- int width = src.getWidth();
- int height = src.getHeight();
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- // BinaryOutputStream bos = new BinaryOutputStream(baos, BYTE_ORDER_Network);
-
- int bytecount = 0;
- for (int y = height - 1; y >= 0; y--)
- {
- // for (int y = 0; y < height; y++)
- for (int x = 0; x < width; x++)
- {
- int argb = src.getRGB(x, y);
- int rgb = 0xffffff & argb;
-
- int red = 0xff & (rgb >> 16);
- int green = 0xff & (rgb >> 8);
- int blue = 0xff & (rgb >> 0);
-
- baos.write(blue);
- baos.write(green);
- baos.write(red);
- bytecount += 3;
- }
- while ((bytecount % 4) != 0)
- {
- baos.write(0);
- bytecount++;
- }
- }
+ // private final boolean alpha;
+ //
+ // public BMPWriterRGB(boolean alpha)
+ // {
+ // this.alpha = alpha;
+ // }
+
+ public int getPaletteSize()
+ {
+ return 0;
+ }
+
+ public int getBitsPerPixel()
+ {
+ // return alpha ? 32 : 24;
+ return 24;
+ }
+
+ public void writePalette(BinaryOutputStream bos) throws IOException
+ {
+ }
+
+ public byte[] getImageData(BufferedImage src)
+ {
+ int width = src.getWidth();
+ int height = src.getHeight();
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ // BinaryOutputStream bos = new BinaryOutputStream(baos, BYTE_ORDER_Network);
+
+ int bytecount = 0;
+ for (int y = height - 1; y >= 0; y--)
+ {
+ // for (int y = 0; y < height; y++)
+ for (int x = 0; x < width; x++)
+ {
+ int argb = src.getRGB(x, y);
+ int rgb = 0xffffff & argb;
+
+ int red = 0xff & (rgb >> 16);
+ int green = 0xff & (rgb >> 8);
+ int blue = 0xff & (rgb >> 0);
+
+ baos.write(blue);
+ baos.write(green);
+ baos.write(red);
+ bytecount += 3;
+ }
+ while ((bytecount % 4) != 0)
+ {
+ baos.write(0);
+ bytecount++;
+ }
+ }
- return baos.toByteArray();
- }
+ return baos.toByteArray();
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/gif/GIFBlock.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/gif/GIFBlock.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/gif/GIFBlock.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/gif/GIFBlock.java Fri Sep 10 16:33:35 2010
@@ -18,10 +18,10 @@ package org.apache.sanselan.formats.gif;
class GIFBlock
{
- public final int blockCode;
+ public final int blockCode;
- public GIFBlock(int blockCode)
- {
- this.blockCode = blockCode;
- }
+ public GIFBlock(int blockCode)
+ {
+ this.blockCode = blockCode;
+ }
}
\ No newline at end of file