Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * @(#)BitmapImage.java  1.5  2011-01-05
    *
    * Copyright (c) 2004-2011 Werner Randelshofer, Goldau, Switzerland.
    * All rights reserved.
    *
    * You may not use, copy or modify this file, except in compliance with the
    * license agreement you entered into with Werner Randelshofer.
    * For details see accompanying license terms.
   */
  package org.monte.media.image;
  
A BitmapImage is comprised of a ColorModel and an accessible byte array of image data.

The image data is expressed in several layers of rectangular regions called bit-planes. To determine the bits that form a single pixel one must combine all data-bits at the same x,y position in each bit-plane. This is known as a "planar" storage layout as it was used on Commodore Amiga Computers.

The bit-planes can be stored contiguously or can be interleaved at each scanline of the image.

Fig 1. A sample image:

 .+++..@...@.+..###...+++.     This sample uses 4 colors:
 +...+.@@.@@.+.#.....+...+     . = color 0 (all bits clear)
 +++++:@.@.@.+.#..##.+++++     + = color 1 (bit 0 set, bit 1 clear)
 +...+.@...@.+.#...#.+...+     

Author(s):
Werner Randelshofer, Hausmatt 10, CH-6405 Goldau, Switzerland
Version:
1.5 2011-01-05 Adds support for RGB555.
1.4 2011-01-03 Adds method setIntPixels().
1.3 2010-10-25 Removed suffixes in instance variable names.
1.2.1 2005-07-16 Setting a preferredColorModel is now better honoured.
1.2 2004-05-26 Improved performance of planar to chunky conversion routines.
1.1.1 2004-05-18 Fixed a bug, which caused an image to be all transparent, when it was of bitmap type indexed color, and when the desired bitmap type was true color, and the bitmap had a transparent color.
1.1 2003-04-01 BitmapImage can now convert bitmaps with IndexColorModel's into chunky pixels with DirectColorModel.
1.0 1999-10-19
:
= color 2 (bit 0 clear, bit 1 set) +...+.@...@.+..####.+...+ # = color 3 (all bits set)

Fig 2. Contiguous bit-plane storage layout.

 01110000 00001001 11000111 0.......     This is the first bit-plane.
 10001000 00001010 00001000 1.......     Each number represents a bit
 11111000 00001010 01101111 1.......     in the storage layout. Eight
 10001000 00001010 00101000 1.......     bits are grouped into one byte.
 10001000 00001001 11101000 1.......     Dots indicate unused bits.
 

00000010 00100001 11000000 0....... This is the second bit-plane. 00000011 01100010 00000000 0....... 00000010 10100010 01100000 0....... 00000010 00100010 00100000 0....... 00000010 00100001 11100000 0.......

Fig 3. Interleaved bit-plane storage layout.

 01110000 00001001 11000111 0.......     This is the first bit-plane.
 00000010 00100001 11000000 0.......     This is the second bit-plane.
 

10001000 00001010 00001000 1....... The bit-planes are interleaved 00000011 01100010 00000000 0....... at every scanline of the image.

11111000 00001010 01101111 1....... 00000010 10100010 01100000 0.......

10001000 00001010 00101000 1....... 00000010 00100010 00100000 0.......

10001000 00001001 11101000 1....... 00000010 00100001 11100000 0.......

For more details refer to "Amiga ROM Kernel Reference Manual: Libraries, Addison Wesley"

Responsibility

Gives clients direct access to the image data of the bitmap. Knows how to convert the bitmap into chunky image data according to the current color model. Supports indexed color model, direct color model, 6 and 8 bit HAM color model.

 
 public class BitmapImage
         implements Cloneable {

    
The bitmap data array.
 
     private byte[] bitmap;
    
The width of the image.
 
     private int width;
    
The height of the image.
 
     private int height;
    
The number of bits that form a single pixel.
 
     private int depth;
    
BitmapStride is the number of data array elements between two bits of the same image pixel.
 
     private int bitplaneStride;
    
ScanlineStride is the number of data array elements between a given pixel and the pixel in the same column of the next scanline.
 
     private int scanlineStride;
    
This ColorModel is used for the next conversion from planar bitmap data into chunky pixel data.
 
     private ColorModel planarColorModel;
    
This ColorModel represents the preferred color model for chunky pixel. If this value is null, then convertToChunky uses the planarColorModel_.
 
     private ColorModel preferredChunkyColorModel_;
    
This ColorModel represents the current color model for chunky pixel.
 
     private ColorModel currentChunkyColorModel_;
    
This ColorModel was used at the previous conversion from planar bitmap into chunky pixel data.
 
     private ColorModel lastPixelColorModel_;
    
Indicates availability of chunky pixel data.
 
     private int pixelType;
    
Tag for byte pixel data.
 
     public final static int BYTE_PIXEL = 1;
    
Tag for integer pixel data.
 
     public final static int INT_PIXEL = 2;
    
Tag for short pixel data.
 
     public final static int SHORT_PIXEL = 2;
    
Tag indicating that no pixel data is available.
 
     public final static int NO_PIXEL = 0;
    
Output array for byte pixel data.
 
     private byte[] bytePixels;
    
Output array for integer pixel data.
 
     private int[] intPixels;
    
Output array for short pixel data.
 
     private short[] shortPixels;
    
If this boolean is set to true, then convertToChunky always generates chunky pixels using a DirectColorModel.
 
     private boolean enforceDirectColors_ = false;

    
If you set this to true, then convertToChunky always generates chunky pixels using a DirectColorModel.
 
     public void setEnforceDirectColors(boolean b) {
          = b;
     }

    
If this returns true, then convertToChunky always generates chunky pixels using a DirectColorModel.
 
     public boolean isEnforceDirectColors() {
         return ;
     }

    
Construct an interleaved bitmap with the specified size, depth and color model. BitplaneStride and ScanlineStride are rounded up to the next even number of bytes.

Pre condition: -

Post condition: Interleaved bitmap constructed.

Obligation: -

Parameters:
width Width in pixels.
height Height in pixels.
depth Number of bits per pixel.
colorModel Color model to be used for conversions from/to chunky pixels.
 
     public BitmapImage(int widthint heightint depthColorModel colorModel) {
         this(widthheightdepthcolorModeltrue);
     }

    
Construct a bitmap with the specified size, depth and color model and with optional interleave. BitplaneStride and ScanlineStride are rounded up to the next even number of bytes.

Pre condition: -

Post condition: BitmapImage constructed.

Obligation: -

Parameters:
width Width in pixels.
height Height in pixels.
depth Number of bits per pixel.
colorModel Color model to be used for conversions from/to chunky pixels.
isInterleaved Indicator for contiguous or interleaved bit-planes.
 
     public BitmapImage(int widthint heightint depthColorModel colorModelboolean isInterleaved) {
         this. = width;
         this. = height;
         this. = depth;
         this. = colorModel;
         if (isInterleaved) {
              = (width + 15) / 16 * 2;
              =  * depth;
              = new byte[ * height];
         } else {
              = (width + 15) / 16 * 2;
              =  * depth;
              = new byte[ * height];
         }
          = ;
     }

    
Construct a bitmap with the specified size, depth, color model and interleave.

Pre condition: ScanlineStride must be a multiple of BitplaneStride or vice versa.

Post condition: BitmapImage constructed.

Obligation: -

Parameters:
width Width in pixels.
height Height in pixels.
depth Number of bits per pixel.
colorModel Color model to be used for conversions from/to chunky pixels.
bitStride Number of data array elements between two bits of the same image pixel.
scanlineStride Number of data array elements between a given pixel and the pixel in the same column of the next scanline.
 
     public BitmapImage(int widthint heightint depthColorModel colorModelint bitStrideint scanlineStride) {
         this. = width;
         this. = height;
         this. = depth;
         this. = colorModel;
         this. = bitStride;
         this. = scanlineStride;
         if ( < scanlineStride) {
              = new byte[scanlineStride * height];
         } else {
              = new byte[ * height];
         }
          = ;
     }

    
Returns the width of the image.

Pre condition: -

Post condition: -

Obligation: -

Returns:
The width in pixels.
 
     public int getWidth() {
         return ;
     }

    
Returns the height of the image.

Pre condition: -

Post condition: -

Obligation: -

Returns:
The height in pixels.
 
     public int getHeight() {
         return ;
     }

    
Returns the depth of the image.

The depth indicates how many bits are used to form a single pixel.

Pre condition: -

Post condition: -

Obligation: -

Returns:
The number of bitplanes used to form a single pixel.
 
     public int getDepth() {
         return ;
     }

    
Returns the numer of bytes you must add to a given address in the bitmap to advance to the next scanline of the image.

Pre condition: -

Post condition: -

Obligation: -

Returns:
The scansize.
 
     public int getScanlineStride() {
         return ;
     }

    
Returns the number of bytes that you must add to a bitmap address to advance to the next bit of a scanline.

Pre condition: -

Post condition: -

Obligation: -

Returns:
The interleave of the bitmap.
 
     public int getBitplaneStride() {
         return ;
     }

    
Replaces the color model used for conversions from/to chunky pixels.

Pre condition: The new color model must correspond with the depth of the bitmap.

Post condition: Color model changed.

Obligation: -

Parameters:
colorModel The new color model.
 
     public void setPlanarColorModel(ColorModel colorModel) {
          = colorModel;
     }

    
Returns the current color model of the planar image in this bitmap.

Pre condition: -

Post condition: -

Obligation: -

Returns:
The color model.
 
     public ColorModel getPlanarColorModel() {
         return ;
     }

    
Sets the preferred color model used for to chunky pixels.

Pre condition: -

Post condition: Color model changed.

Obligation: -

Parameters:
colorModel The new color model.
 
     public void setPreferredChunkyColorModel(ColorModel colorModel) {
          = colorModel;
     }

    
Returns the current color model of the chunky image in this bitmap.

Pre condition: -

Post condition: -

Obligation: -

Returns:
The color model.
 
     public ColorModel getChunkyColorModel() {
         if ( == null) {
             convertToChunky(0, 0, 0, 0);
         }
         return ;
     }

    
Gives you direct access to the bitmap data array.

Pre condition: -.

Post condition: -

Obligation: The bitmap data array remains property of the BitmapImage and will be used at the next conversion to chunky. You can access it as you like (even during conversion) since this class does never change the contents of the bitmap.

Returns:
A reference to the bitmap data.
 
     public byte[] getBitmap() {
         return ;
     }

    
Returns a reference to the byte pixel data that has been generated by a previous call to #converToChunky.

Pre condition: -

Post condition: -

Obligation: You may modify the contents of the array as you like to get some nice effects for the next call to #convertToChunky. Note whovewer that #convertToChunky will not reuse this array when the colorModel has been changed to a color format that requires pixels in integer format.

Returns:
byte array or NULL when no byte pixels have been generated by #convertToChunky.
 
     public byte[] getBytePixels() {
         if ( == ) {
             return ;
         } else {
             return null;
         }
     }

    
Returns a reference to the byte pixel data that has been generated by a previous call to #converToChunky.

Pre condition: -

Post condition: -

Obligation: You may modify the contents of the array as you like to get some nice effects for the next call to #convertToChunky. Note whovewer that #convertToChunky will not reuse this array when the colorModel has been changed to a color format that requires pixels in integer format.

Returns:
byte array or NULL when no byte pixels have been generated by #convertToChunky.
 
     public short[] getShortPixels() {
         if ( == ) {
             return ;
         } else {
             return null;
         }
     }

    
Returns a reference to the integer pixel data that has been generated by a previous call to #converToChunky.

Pre condition: -

Post condition: -

Obligation: You may modify the contents of the array as you like to get some nice effects for the next call to #convertToChunky. Note however that #convertToChunky will not reuse this array when the colorModel has been changed to a color format that requires pixels in byte format.

Returns:
byte array or NULL when no int pixels have been generated by #convertToChunky.
 
     public int[] getIntPixels() {
         if ( == ) {
             return ;
         } else {
             return null;
         }
     }

    
Returns the available type of pixel data.

Pre condition: -

Post condition: -

Obligation: -

Returns:
A constant that specifies the current type of pixel data.
 
     public int getPixelType() {
         return ;
     }

    
Creates a clone.

Pre condition: -

Post condition: Clone created.

Returns:
A clone.
 
     @Override
     public BitmapImage clone() {
         try {
             BitmapImage theClone = (BitmapImagesuper.clone();
             theClone.bitmap = (byte[]) .clone();
             if (getPixelType() == ) {
                 theClone.bytePixels = (byte[]) .clone();
             }
             if (getPixelType() == ) {
                 theClone.intPixels = (int[]) .clone();
             }
             return theClone;
         } catch (CloneNotSupportedException e) {
             throw new InternalError(e.toString());
         }
     }

    
Converts the planar image data into chunky pixel data.

This method will either generate byte pixel data or integer pixel data (depending on the color model).

The pixel array that resulted to a prior call to this method will be reused when the image dimension and the color model allows for it.

Pre condition: -

Post condition: Chunky pixels generated.

Obligation: -

Returns:
The type of generated pixel data.
 
     public int convertToChunky() {
         return convertToChunky(0, 0, getHeight() - 1, getWidth() - 1);
     }

    
Converts the indicated area of the bitmap data into pixel data.

This method will either generate byte pixel data or integer pixel data (depending on the color model).

Note that the size of the generated pixel data always corresponds to the size of the complete image. You do only specify a subset of the image to be converted not a subset to be extracted. Note also that the pixel data that resulted from prior calls to this method will be reused when the generated pixel array was of the same size and type.

Pre condition: -

Post condition: The indicated part of the bitmap has been converted into chunky pixels.

Obligation: -

Returns:
The type of generated pixel data.
 
     public int convertToChunky(int topint leftint bottomint right) {
          = ;
 
         /* Ensure pre conditions are met. */
         if (top < 0) {
             top = 0;
         }
         if (left < 0) {
             left = 0;
         }
         if (bottom > getHeight() - 1) {
             bottom = getHeight() - 1;
         }
         if (right > getWidth() - 1) {
             right = getWidth() - 1;
         }
 
         /* */
         if ( instanceof HAMColorModel) {
             if ( == null || . != getWidth() * getHeight()) {
                  = null;
                  = null;
                  = new int[getWidth() * getHeight()];
             }
              = ;
             if (((HAMColorModel).getHAMType() == .) {
                 ham6PlanesToDirectPixels(topleftbottomright);
             } else if (((HAMColorModel).getHAMType() == .) {
                 ham8PlanesToDirectPixels(topleftbottomright);
             } else {
                 throw new InternalError("unsupported ham model:" + );
             }
              = ;
 
         } else {
             if ( instanceof IndexColorModel) {
                 if ( ||  instanceof DirectColorModel) {
                     if ( != null && ((DirectColorModel).getPixelSize() == 16) {
                         if ( == null || . != getWidth() * getHeight()) {
                              = null;
                              = null;
                              = null;
                              = new short[getWidth() * getHeight()];
                         }
                          =
                                 ( != null && ( instanceof DirectColorModel))
                                 ? 
                                 : new DirectColorModel(16, 0x7c00, 0x3e0, 0x1f);
 
                         indexPlanesTo555(topleftbottomright);
                          = ;
                     } else {
                         if ( == null || . != getWidth() * getHeight()) {
                              = null;
                              = null;
                              = new int[getWidth() * getHeight()];
                         }
 
                          =
                                 ( != null && ( instanceof DirectColorModel))
                                 ? 
                                 : ColorModel.getRGBdefault();
 
                          = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
                         indexPlanesToDirectPixels(topleftbottomright);
                          = ;
                     }
                 } else {
                     if ( == null || . != getWidth() * getHeight()) {
                          = null;
                          = null;
                          = new byte[getWidth() * getHeight()];
                     }
                      = ;
                     indexPlanesToIndexPixels(topleftbottomright);
                      = ;
                 }
             } else if ( instanceof DirectColorModel) {
                 if (((DirectColorModel).getPixelSize() == 16) {
                     if ( == null || . != getWidth() * getHeight()) {
                          = null;
                          = null;
                          = null;
                          = new short[getWidth() * getHeight()];
                     }
                      = ;
                     directPlanesTo555(topleftbottomright);
                      = ;
                 } else {
                     if ( == null || . != getWidth() * getHeight()) {
                          = null;
                          = null;
                          = null;
                          = new int[getWidth() * getHeight()];
                     }
                      = ;
                     directPlanesToDirectPixels(topleftbottomright);
                      = ;
                 }
             } else {
                 throw new InternalError("unsupported color model:" + );
             }
         }
         return ;
     }

    
Converts the indicated area of the bitmap data into pixel data.

This method will either generate byte pixel data or integer pixel data (depending on the color model).

Note that the size of the generated pixel data always corresponds to the size of the complete image. You do only specify a subset of the image to be converted not a subset to be extracted. Note also that the pixel data that resulted from prior calls to this method will be reused when the generated pixel array was of the same size and type.

Pre condition: -

Post condition: The indicated part of the bitmap has been converted into chunky pixels.

Obligation: -

 
     public void convertFromChunky(BufferedImage image) {
         /* */
         if ( instanceof HAMColorModel) {
 
             throw new UnsupportedOperationException("HAM mode not implemented:");
 
 
         } else {
             if ( instanceof IndexColorModel) {
                 if (image.getType() == .) {
                     =image.getColorModel();
                     Raster raster=image.getRaster();
                     int dx=0,dy=0;
                     while (raster.getParent()!=null) {
                         dx+=raster.getMinX();
                         dy+=raster.getMinY();
                         raster=raster.getParent();
                     }
                    DataBufferByte dbuf= ((DataBufferByte)image.getRaster().getDataBuffer());
                   int inScanlineStride=raster.getWidth();
                     byte[] inb=dbuf.getData();
                     
                     if (==null||.!=*) {
                         =new byte[*];
                     }
                     
                     for (int y=0;y<;y++) {
                         System.arraycopy(inb,dx+(y+dy)*inScanlineStride,,y*,);
                     }
                     indexPixelsToIndexPlanes(0, 0, getHeight() - 1, getWidth() - 1);
                 } else {
                 
                 throw new UnsupportedOperationException("index color model not implemented:" + );
                 }
             } else if ( instanceof DirectColorModel) {
                 throw new UnsupportedOperationException("index color model not implemented:" + );
             } else {
                 throw new UnsupportedOperationException("unsupported color model:" + );
             }
         }
     }

    
Frees the memory allocated for the pixel data.

Pre condition: -

Post condition: The bitmap has given up all its references to the pixel data.

Obligation: The pixel data will not be reused at the next call to #convertToChunky.

 
     public void flushPixels() {
          = ;
          = null;
          = null;
          = null;
     }

    
Converts the planar image data into chunky pixels. After successful completion the chunky pixels can by used in conjunction with the IndexColorModel associated to this instance. Pre condition The color model must be an instance of java.awt.IndexColorModel. 0 <= topBound <= bottomBound <= height. 0 <= leftBound <= rightBound <= width. Post condition - Obligation -

Author(s):
Werner Randelshofer, Hausmatt 10, CH-6405 Goldau, Switzerland
Version:
1997-10-16 Created.
 
     private void indexPlanesToIndexPixels(int topint leftint bottomint right) {
 
         /* Add one to bottom and right to facilitate computations. */
         bottom++;
         right++;
 
         final int scanlineStride = getScanlineStride();
         final int bitplaneStride = getBitplaneStride();
         final int depth = getDepth();
         final int width = getWidth();
         final int pixelLineStride = width - right + left;
         final int bottomScanline = bottom * scanlineStride;
         //final int bitCorrection = depth - 8;
         //final int bitCorrection = 8 - depth;
         int x;
         int iPixel = top * width + left;
         int pixel = 0;
         //int bitShift;
         int iBitmap;
         int iScanline;
         int iDepth;
         int b0b1b2b3b4b5b6b7;
         b0 = b1 = b2 = b3 = b4 = b5 = b6 = b7 = 0;
         final int bitplaneStride1 = bitplaneStride;
         final int bitplaneStride2 = bitplaneStride * 2;
         final int bitplaneStride3 = bitplaneStride * 3;
         final int bitplaneStride4 = bitplaneStride * 4;
         final int bitplaneStride5 = bitplaneStride * 5;
         final int bitplaneStride6 = bitplaneStride * 6;
         final int bitplaneStride7 = bitplaneStride * 7;
 
         int iBit// the index of the bit inside the byte at the current x-position
         int bitMask// the mask for the bit inside the byte at the current x-position
 
         switch (depth) {
             case 1:
                 /*
                 for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
                 for (x = left; x < right; x++) {
                 bitShift = x % 8;
                 iBitmap = iScanline + x / 8;
                 bytePixels_[iPixel++] = (byte) (((bitmap_[iBitmap] << bitShift) & 128) >>> 7);
                 }
                 iPixel += pixelLineStride;
                 }
                  */
                 for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                     for (x = leftx < rightx++) {
                         [iPixel++] = (byte) ((([iScanline + (x >>> 3)] << (x & 7)) & 128) >>> 7);
                     }
                     iPixel += pixelLineStride;
                 }
                 break;
 
             case 2:
                 /*
                 for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
                 for (x = left; x < right; x++) {
                 bitShift = x & 7;
                 iBitmap = iScanline + x >>> 3;
                 bytePixels_[iPixel++] = (byte) (
                 ((bitmap_[iBitmap] << bitShift) & 128) >>> 7
                 | ((bitmap_[iBitmap+bitplaneStride1] << bitShift) & 128) >>> 6
                 );
                 }
                 iPixel += pixelLineStride;
                 }*/
                 for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                     for (x = leftx < rightx++) {
                         iBit = x & 7;
                         bitMask = 128 >>> (iBit);
                         iBitmap = iScanline + (x >>> 3);
 
                         [iPixel++] = (byte) ((([iBitmap] & bitMask)
                                 | ([iBitmap + bitplaneStride1] & bitMask) << 1) >>> (7 - iBit));
                     }
                     iPixel += pixelLineStride;
                 }
                 break;
 
             case 3:
                 /*
                 for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
                 for (x = left; x < right; x++) {
                 bitShift = x & 7;
                 iBitmap = iScanline + x >>> 3;
                 bytePixels_[iPixel++] = (byte) (
                 ((bitmap_[iBitmap] << bitShift) & 128) >>> 7
                 | ((bitmap_[iBitmap+bitplaneStride1] << bitShift) & 128) >>> 6
                 | ((bitmap_[iBitmap+bitplaneStride2] << bitShift) & 128) >>> 5
                 );
                 }
                 iPixel += pixelLineStride;
                 }*/
                 for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                     for (x = leftx < rightx++) {
                         iBit = x & 7;
                         bitMask = 128 >>> (iBit);
                         iBitmap = iScanline + (x >>> 3);
 
                         [iPixel++] = (byte) ((([iBitmap] & bitMask)
                                 | ([iBitmap + bitplaneStride1] & bitMask) << 1
                                 | ([iBitmap + bitplaneStride2] & bitMask) << 2) >>> (7 - iBit));
                     }
                     iPixel += pixelLineStride;
                 }
                 break;
 
             case 4:
                 /*
                 for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
                 for (x = left; x < right; x++) {
                 int bitShift = x % 8;
                 iBitmap = iScanline + x / 8;
                 bytePixels_[iPixel++] = (byte) (
                 ((bitmap[iBitmap] << bitShift) & 128) >>> 7
                 | ((bitmap[iBitmap+bitplaneStride1] << bitShift) & 128) >>> 6
                 | ((bitmap[iBitmap+bitplaneStride2] << bitShift) & 128) >>> 5
                 | ((bitmap[iBitmap+bitplaneStride3] << bitShift) & 128) >>> 4
                 );
                 }
                 iPixel += pixelLineStride;
                 }*/
 
                 for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                     for (x = leftx < rightx++) {
                         iBit = x & 7;
                         bitMask = 128 >>> (iBit);
                         iBitmap = iScanline + (x >>> 3);
 
                         [iPixel++] = (byte) ((([iBitmap] & bitMask)
                                 | ([iBitmap + bitplaneStride1] & bitMask) << 1
                                 | ([iBitmap + bitplaneStride2] & bitMask) << 2
                                 | ([iBitmap + bitplaneStride3] & bitMask) << 3) >>> (7 - iBit));
                     }
                     iPixel += pixelLineStride;
                 }
                 break;
 
             case 5:
                 /*
                 for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
                 for (x = left; x < right; x++) {
                 bitShift = x % 8;
                 iBitmap = iScanline + x / 8;
                 bytePixels_[iPixel++] = (byte) (
                 ((bitmap_[iBitmap] << bitShift) & 128) >>> 7
                 | ((bitmap_[iBitmap+bitplaneStride1] << bitShift) & 128) >>> 6
                 | ((bitmap_[iBitmap+bitplaneStride2] << bitShift) & 128) >>> 5
                 | ((bitmap_[iBitmap+bitplaneStride3] << bitShift) & 128) >>> 4
                 | ((bitmap_[iBitmap+bitplaneStride4] << bitShift) & 128) >>> 3
                 );
                 }
                 iPixel += pixelLineStride;
                 }*/
                 /*
                 for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
                 for (x = left; x < right; x++) {
                 iBit = x & 7;
                 bitMask = 128 >>> (iBit);
                 iBitmap = iScanline + (x >>> 3);
                 
                 bytePixels_[iPixel++] = (byte) ((
                 (bitmap_[iBitmap] & bitMask)
                 | (bitmap_[iBitmap+bitplaneStride1] & bitMask) << 1
                 | (bitmap_[iBitmap+bitplaneStride2] & bitMask) << 2
                 | (bitmap_[iBitmap+bitplaneStride3] & bitMask) << 3
                 | (bitmap_[iBitmap+bitplaneStride4] & bitMask) << 4
                 ) >>> (7 - iBit));
                 }
                 iPixel += pixelLineStride;
                 }
                 iPixel=0;
                  */
                 for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                     for (x = leftx < rightx++) {
                         iBit = x & 7;
                         bitMask = 128 >>> (iBit);
                         iBitmap = iScanline + (x >>> 3);
                         if (iBit == 0) {
                             b0 = [iBitmap];
                             b1 = [iBitmap + bitplaneStride];
                             b2 = [iBitmap + bitplaneStride2];
                             b3 = [iBitmap + bitplaneStride3];
                             b4 = [iBitmap + bitplaneStride4];
                         }
                         [iPixel++] = (byte) (((b0 & bitMask)
                                 | (b1 & bitMask) << 1
                                 | (b2 & bitMask) << 2
                                 | (b3 & bitMask) << 3
                                 | (b4 & bitMask) << 4) >>> (7 - iBit));
                     }
                     iPixel += pixelLineStride;
                 }
                 break;
 
             case 6:
                 /*
                 for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
                 for (x = left; x < right; x++) {
                bitShift = x % 8;
                iBitmap = iScanline + x / 8;
                bytePixels_[iPixel++] = (byte) (
                ((bitmap_[iBitmap] << bitShift) & 128) >>> 7
                | ((bitmap_[iBitmap+bitplaneStride1] << bitShift) & 128) >>> 6
                | ((bitmap_[iBitmap+bitplaneStride2] << bitShift) & 128) >>> 5
                | ((bitmap_[iBitmap+bitplaneStride3] << bitShift) & 128) >>> 4
                | ((bitmap_[iBitmap+bitplaneStride4] << bitShift) & 128) >>> 3
                | ((bitmap_[iBitmap+bitplaneStride5] << bitShift) & 128) >>> 2
                );
                }
                iPixel += pixelLineStride;
                }*/
                /*
                for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
                for (x = left; x < right; x++) {
                iBit = x & 7;
                bitMask = 128 >>> (iBit);
                iBitmap = iScanline + (x >>> 3);
                
                bytePixels_[iPixel++] = (byte) ((
                (bitmap_[iBitmap] & bitMask)
                | (bitmap_[iBitmap+bitplaneStride1] & bitMask) << 1
                | (bitmap_[iBitmap+bitplaneStride2] & bitMask) << 2
                | (bitmap_[iBitmap+bitplaneStride3] & bitMask) << 3
                | (bitmap_[iBitmap+bitplaneStride4] & bitMask) << 4
                | (bitmap_[iBitmap+bitplaneStride5] & bitMask) << 5
                ) >>> (7 - iBit));
                }
                iPixel += pixelLineStride;
                }*/
                for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                    for (x = leftx < rightx++) {
                        iBit = x & 7;
                        bitMask = 128 >>> (iBit);
                        iBitmap = iScanline + (x >>> 3);
                        if (iBit == 0) {
                            b0 = [iBitmap];
                            b1 = [iBitmap + bitplaneStride];
                            b2 = [iBitmap + bitplaneStride2];
                            b3 = [iBitmap + bitplaneStride3];
                            b4 = [iBitmap + bitplaneStride4];
                            b5 = [iBitmap + bitplaneStride5];
                        }
                        [iPixel++] = (byte) (((b0 & bitMask)
                                | (b1 & bitMask) << 1
                                | (b2 & bitMask) << 2
                                | (b3 & bitMask) << 3
                                | (b4 & bitMask) << 4
                                | (b5 & bitMask) << 5) >>> (7 - iBit));
                    }
                    iPixel += pixelLineStride;
                }
                break;
            case 7:
                /*
                for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
                for (x = left; x < right; x++) {
                bitShift = x % 8;
                iBitmap = iScanline + x / 8;
                bytePixels_[iPixel++] = (byte) (
                ((bitmap_[iBitmap] << bitShift) & 128) >>> 7
                | ((bitmap_[iBitmap+bitplaneStride1] << bitShift) & 128) >>> 6
                | ((bitmap_[iBitmap+bitplaneStride2] << bitShift) & 128) >>> 5
                | ((bitmap_[iBitmap+bitplaneStride3] << bitShift) & 128) >>> 4
                | ((bitmap_[iBitmap+bitplaneStride4] << bitShift) & 128) >>> 3
                | ((bitmap_[iBitmap+bitplaneStride5] << bitShift) & 128) >>> 2
                | ((bitmap_[iBitmap+bitplaneStride6] << bitShift) & 128) >>> 1
                );
                }
                iPixel += pixelLineStride;
                }*/
                /*
                for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
                for (x = left; x < right; x++) {
                iBit = x & 7;
                bitMask = 128 >>> (iBit);
                iBitmap = iScanline + (x >>> 3);
                
                bytePixels_[iPixel++] = (byte) ((
                (bitmap_[iBitmap] & bitMask)
                | (bitmap_[iBitmap+bitplaneStride1] & bitMask) << 1
                | (bitmap_[iBitmap+bitplaneStride2] & bitMask) << 2
                | (bitmap_[iBitmap+bitplaneStride3] & bitMask) << 3
                | (bitmap_[iBitmap+bitplaneStride4] & bitMask) << 4
                | (bitmap_[iBitmap+bitplaneStride5] & bitMask) << 5
                | (bitmap_[iBitmap+bitplaneStride6] & bitMask) << 6
                ) >>> (7 - iBit));
                }
                iPixel += pixelLineStride;
                }*/
                for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                    for (x = leftx < rightx++) {
                        iBit = x & 7;
                        bitMask = 128 >>> (iBit);
                        iBitmap = iScanline + (x >>> 3);
                        if (iBit == 0) {
                            b0 = [iBitmap];
                            b1 = [iBitmap + bitplaneStride];
                            b2 = [iBitmap + bitplaneStride2];
                            b3 = [iBitmap + bitplaneStride3];
                            b4 = [iBitmap + bitplaneStride4];
                            b5 = [iBitmap + bitplaneStride5];
                            b6 = [iBitmap + bitplaneStride6];
                        }
                        [iPixel++] = (byte) (((b0 & bitMask)
                                | (b1 & bitMask) << 1
                                | (b2 & bitMask) << 2
                                | (b3 & bitMask) << 3
                                | (b4 & bitMask) << 4
                                | (b5 & bitMask) << 5
                                | (b6 & bitMask) << 6) >>> (7 - iBit));
                    }
                    iPixel += pixelLineStride;
                }
                break;
            case 8:
                /*
                for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
                for (x = left; x < right; x++) {
                bitShift = x % 8;
                iBitmap = iScanline + x / 8;
                bytePixels_[iPixel++] = (byte) (
                ((bitmap_[iBitmap] << bitShift) & 128) >>> 7
                | ((bitmap_[iBitmap+bitplaneStride1] << bitShift) & 128) >>> 6
                | ((bitmap_[iBitmap+bitplaneStride2] << bitShift) & 128) >>> 5
                | ((bitmap_[iBitmap+bitplaneStride3] << bitShift) & 128) >>> 4
                | ((bitmap_[iBitmap+bitplaneStride4] << bitShift) & 128) >>> 3
                | ((bitmap_[iBitmap+bitplaneStride5] << bitShift) & 128) >>> 2
                | ((bitmap_[iBitmap+bitplaneStride6] << bitShift) & 128) >>> 1
                | ((bitmap_[iBitmap+bitplaneStride7] << bitShift) & 128)
                );
                }
                iPixel += pixelLineStride;
                }*/
                /*
                for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
                for (x = left; x < right; x++) {
                iBit = x & 7;
                bitMask = 128 >>> (iBit);
                iBitmap = iScanline + (x >>> 3);
                
                bytePixels_[iPixel++] = (byte) ((
                (bitmap_[iBitmap] & bitMask)
                | (bitmap_[iBitmap+bitplaneStride1] & bitMask) << 1
                | (bitmap_[iBitmap+bitplaneStride2] & bitMask) << 2
                | (bitmap_[iBitmap+bitplaneStride3] & bitMask) << 3
                | (bitmap_[iBitmap+bitplaneStride4] & bitMask) << 4
                | (bitmap_[iBitmap+bitplaneStride5] & bitMask) << 5
                | (bitmap_[iBitmap+bitplaneStride6] & bitMask) << 6
                | (bitmap_[iBitmap+bitplaneStride7] & bitMask) << 7
                ) >>> (7 - iBit));
                }
                iPixel += pixelLineStride;
                }*/
                for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                    for (x = leftx < rightx++) {
                        iBit = x & 7;
                        bitMask = 128 >>> (iBit);
                        iBitmap = iScanline + (x >>> 3);
                        if (iBit == 0) {
                            b0 = [iBitmap];
                            b1 = [iBitmap + bitplaneStride];
                            b2 = [iBitmap + bitplaneStride2];
                            b3 = [iBitmap + bitplaneStride3];
                            b4 = [iBitmap + bitplaneStride4];
                            b5 = [iBitmap + bitplaneStride5];
                            b6 = [iBitmap + bitplaneStride6];
                            b7 = [iBitmap + bitplaneStride7];
                        }
                        [iPixel++] = (byte) (((b0 & bitMask)
                                | (b1 & bitMask) << 1
                                | (b2 & bitMask) << 2
                                | (b3 & bitMask) << 3
                                | (b4 & bitMask) << 4
                                | (b5 & bitMask) << 5
                                | (b6 & bitMask) << 6
                                | (b7 & bitMask) << 7) >>> (7 - iBit));
                    }
                    iPixel += pixelLineStride;
                }
                break;
            default:
                /*
                for (iScanline = top * scanlineStride; iScanline < bottomScanline; iScanline += scanlineStride) {
                for (x = left; x < right; x++) {
                bitShift = x % 8;
                iBitmap = iScanline + x / 8;
                for (iDepth = depth; iDepth > 0; iDepth--) {
                pixel = (pixel >>> 1) | ((bitmap_[iBitmap] << bitShift)  & 128);
                iBitmap += bitplaneStride;
                }
                //bytePixels_[iPixel++] = (byte)(pixel >>> bitCorrection);
                bytePixels_[iPixel++] = (byte)(pixel);
                }
                iPixel += pixelLineStride;
                }*/
                for (iScanline = top * scanlineStride + scanlineStrideiScanline <= bottomScanlineiScanline += scanlineStride) {
                    for (x = leftx < rightx++) {
                        iBit = x & 7;
                        bitMask = 128 >>> (iBit);
                        iBitmap = iScanline + (x >>> 3);
                        pixel = 0;
                        for (iDepth = 0; iDepth < depthiDepth++) {
                            iBitmap -= bitplaneStride;
                            pixel = (pixel << 1) | [iBitmap] & bitMask;
                        }
                        [iPixel++] = (byte) (pixel >>> (7 - iBit));
                    }
                    iPixel += pixelLineStride;
                }
        }
    }
    private void indexPixelsToIndexPlanes(int topint leftint bottomint right) {
        /* Add one to bottom and right to facilitate computations. */
        bottom++;
        right++;
        final int scanlineStride = getScanlineStride();
        final int bitplaneStride = getBitplaneStride();
        final int depth = getDepth();
        final int width = getWidth();
        final int pixelLineStride = width - right + left;
        final int bottomScanline = bottom * scanlineStride;
        //final int bitCorrection = depth - 8;
        //final int bitCorrection = 8 - depth;
        int x;
        int iPixel = top * width + left;
        int pixel = 0;
        //int bitShift;
        int iBitmap;
        int iScanline;
        int iDepth;
        int b0b1b2b3b4b5b6b7;
        b0 = b1 = b2 = b3 = b4 = b5 = b6 = b7 = 0;
        final int bitplaneStride1 = bitplaneStride;
        final int bitplaneStride2 = bitplaneStride * 2;
        final int bitplaneStride3 = bitplaneStride * 3;
        final int bitplaneStride4 = bitplaneStride * 4;
        final int bitplaneStride5 = bitplaneStride * 5;
        final int bitplaneStride6 = bitplaneStride * 6;
        final int bitplaneStride7 = bitplaneStride * 7;
        int iBit// the index of the bit inside the byte at the current x-position
        int bitMask// the mask for the bit inside the byte at the current x-position
        switch (depth) {
            case 1:
                if (truethrow new UnsupportedOperationException(depth +" not yet implemented");
                for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                    for (x = leftx < rightx++) {
                        [iPixel++] = (byte) ((([iScanline + (x >>> 3)] << (x & 7)) & 128) >>> 7);
                    }
                    iPixel += pixelLineStride;
                }
                break;
            case 2:
                if (truethrow new UnsupportedOperationException(depth +" not yet implemented");
                for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                    for (x = leftx < rightx++) {
                        iBit = x & 7;
                        bitMask = 128 >>> (iBit);
                        iBitmap = iScanline + (x >>> 3);
                        [iPixel++] = (byte) ((([iBitmap] & bitMask)
                                | ([iBitmap + bitplaneStride1] & bitMask) << 1) >>> (7 - iBit));
                    }
                    iPixel += pixelLineStride;
                }
                break;
            case 3:
                if (truethrow new UnsupportedOperationException(depth +" not yet implemented");
                for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                    for (x = leftx < rightx++) {
                        iBit = x & 7;
                        bitMask = 128 >>> (iBit);
                        iBitmap = iScanline + (x >>> 3);
                        [iPixel++] = (byte) ((([iBitmap] & bitMask)
                                | ([iBitmap + bitplaneStride1] & bitMask) << 1
                                | ([iBitmap + bitplaneStride2] & bitMask) << 2) >>> (7 - iBit));
                    }
                    iPixel += pixelLineStride;
                }
                break;
            case 4:
                if (truethrow new UnsupportedOperationException(depth +" not yet implemented");
                for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                    for (x = leftx < rightx++) {
                        iBit = x & 7;
                        bitMask = 128 >>> (iBit);
                        iBitmap = iScanline + (x >>> 3);
                        [iPixel++] = (byte) ((([iBitmap] & bitMask)
                                | ([iBitmap + bitplaneStride1] & bitMask) << 1
                                | ([iBitmap + bitplaneStride2] & bitMask) << 2
                                | ([iBitmap + bitplaneStride3] & bitMask) << 3) >>> (7 - iBit));
                    }
                    iPixel += pixelLineStride;
                }
                break;
            case 5:
                if (truethrow new UnsupportedOperationException(depth +" not yet implemented");
                for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                    for (x = leftx < rightx++) {
                        iBit = x & 7;
                        bitMask = 128 >>> (iBit);
                        iBitmap = iScanline + (x >>> 3);
                        if (iBit == 0) {
                            b0 = [iBitmap];
                            b1 = [iBitmap + bitplaneStride];
                            b2 = [iBitmap + bitplaneStride2];
                            b3 = [iBitmap + bitplaneStride3];
                            b4 = [iBitmap + bitplaneStride4];
                        }
                        [iPixel++] = (byte) (((b0 & bitMask)
                                | (b1 & bitMask) << 1
                                | (b2 & bitMask) << 2
                                | (b3 & bitMask) << 3
                                | (b4 & bitMask) << 4) >>> (7 - iBit));
                    }
                    iPixel += pixelLineStride;
                }
                break;
            case 6:
                if (truethrow new UnsupportedOperationException(depth +" not yet implemented");
                for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                    for (x = leftx < rightx++) {
                        iBit = x & 7;
                        bitMask = 128 >>> (iBit);
                        iBitmap = iScanline + (x >>> 3);
                        if (iBit == 0) {
                            b0 = [iBitmap];
                            b1 = [iBitmap + bitplaneStride];
                            b2 = [iBitmap + bitplaneStride2];
                            b3 = [iBitmap + bitplaneStride3];
                            b4 = [iBitmap + bitplaneStride4];
                            b5 = [iBitmap + bitplaneStride5];
                        }
                        [iPixel++] = (byte) (((b0 & bitMask)
                                | (b1 & bitMask) << 1
                                | (b2 & bitMask) << 2
                                | (b3 & bitMask) << 3
                                | (b4 & bitMask) << 4
                                | (b5 & bitMask) << 5) >>> (7 - iBit));
                    }
                    iPixel += pixelLineStride;
                }
                break;
            case 7:
                if (truethrow new UnsupportedOperationException(depth +" not yet implemented");
                for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                    for (x = leftx < rightx++) {
                        iBit = x & 7;
                        bitMask = 128 >>> (iBit);
                        iBitmap = iScanline + (x >>> 3);
                        if (iBit == 0) {
                            b0 = [iBitmap];
                            b1 = [iBitmap + bitplaneStride];
                            b2 = [iBitmap + bitplaneStride2];
                            b3 = [iBitmap + bitplaneStride3];
                            b4 = [iBitmap + bitplaneStride4];
                            b5 = [iBitmap + bitplaneStride5];
                            b6 = [iBitmap + bitplaneStride6];
                        }
                        [iPixel++] = (byte) (((b0 & bitMask)
                                | (b1 & bitMask) << 1
                                | (b2 & bitMask) << 2
                                | (b3 & bitMask) << 3
                                | (b4 & bitMask) << 4
                                | (b5 & bitMask) << 5
                                | (b6 & bitMask) << 6) >>> (7 - iBit));
                    }
                    iPixel += pixelLineStride;
                }
                break;
            case 8:
                for (iScanline = top * scanlineStrideiScanline < bottomScanlineiScanline += scanlineStride) {
                    for (x = leftx < rightx++) {
                        iBit = x & 7;
                        bitMask = 128 >>> (iBit);
                        iBitmap = iScanline + (x >>> 3);
                        int px=[iPixel++];
                        b7=(b7<<1)|((px>>>7)&1);
                        b6=(b6<<1)|((px>>>6)&1);
                        b5=(b5<<1)|((px>>>5)&1);
                        b4=(b4<<1)|((px>>>4)&1);
                        b3=(b3<<1)|((px>>>3)&1);
                        b2=(b2<<1)|((px>>>2)&1);
                        b1=(b1<<1)|((px>>>1)&1);
                        b0=(b0<<1)|((px>>>0)&1);
                        
                        if (iBit == 7) {
                             [iBitmap]=(byte)b0;
                             [iBitmap + bitplaneStride]=(byte)b1;
                             [iBitmap + bitplaneStride2]=(byte)b2;
                             [iBitmap + bitplaneStride3]=(byte)b3;
                             [iBitmap + bitplaneStride4]=(byte)b4;
                             [iBitmap + bitplaneStride5]=(byte)b5;
                             [iBitmap + bitplaneStride6]=(byte)b6;
                             [iBitmap + bitplaneStride7]=(byte)b7;
                        }
                    }
                    // FIXME - Add special treatment here when width is not a multiple of 8
                    
                    iPixel += pixelLineStride
                }
                break;
            default:
                if (truethrow new UnsupportedOperationException(depth +" not yet implemented");
                for (iScanline = top * scanlineStride + scanlineStrideiScanline <= bottomScanlineiScanline += scanlineStride) {
                    for (x = leftx < rightx++) {
                        iBit = x & 7;
                        bitMask = 128 >>> (iBit);
                        iBitmap = iScanline + (x >>> 3);
                        pixel = 0;
                        for (iDepth = 0; iDepth < depthiDepth++) {
                            iBitmap -= bitplaneStride;
                            pixel = (pixel << 1) | [iBitmap] & bitMask;
                        }
                        [iPixel++] = (byte) (pixel >>> (7 - iBit));
                    }
                    iPixel += pixelLineStride;
                }
        }
    }
    
Converts the planar image data into chunky pixels. After successful completion the chunky pixels can by used in conjunction with the DirectColorModel associated to this instance. Pre condition The color model must be an instance of java.awt.IndexColorModel. 0 <= topBound <= bottomBound <= height. 0 <= leftBound <= rightBound <= width. Post condition - Obligation -
    private void indexPlanesToDirectPixels(int topint leftint bottomint right) {
        IndexColorModel colorModel = (IndexColorModel;
        final int[] clut = new int[colorModel.getMapSize()];
        //colorModel.getRGBs(clut);
        byte[] reds = new byte[clut.length];
        byte[] greens = new byte[clut.length];
        byte[] blues = new byte[clut.length];
        icm.getReds(reds);
        icm.getGreens(greens);
        icm.getBlues(blues);
        for (int i = 0; i < clut.lengthi++) {
            clut[i] = 0xff000000 | (reds[i] & 0xff) << 16 | (greens[i] & 0xff) << 8 | (blues[i] & 0xff);
        }
        if (clut.length < (1 << getDepth())) {
            throw new IndexOutOfBoundsException("Clut must not be smaller than depth");
        }
        /*
        int transparentPixel =