You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by ca...@apache.org on 2007/01/16 01:59:50 UTC

svn commit: r496556 [3/5] - in /xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics: image/ image/codec/png/ image/codec/tiff/ image/codec/util/ image/rendered/ image/writer/ image/writer/internal/ java2d/ util/ util/i18n/ util/io/

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFImage.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFImage.java?view=diff&rev=496556&r1=496555&r2=496556
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFImage.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFImage.java Mon Jan 15 16:59:48 2007
@@ -110,7 +110,7 @@
 
     // Endian-ness indicator
     boolean isBigEndian;
-    
+
     int imageType;
     boolean isWhiteZero = false;
     int dataType;
@@ -180,7 +180,7 @@
     private static SampleModel createPixelInterleavedSampleModel
         (int dataType, int tileWidth, int tileHeight, int bands) {
         int [] bandOffsets = new int[bands];
-        for (int i=0; i<bands; i++) 
+        for (int i=0; i<bands; i++)
             bandOffsets[i] = i;
         return new PixelInterleavedSampleModel
             (dataType, tileWidth, tileHeight, bands,
@@ -225,7 +225,7 @@
         this.stream = stream;
         if (param == null) {
             param = new TIFFDecodeParam();
-        } 
+        }
 
         decodePaletteAsShorts = param.getDecodePaletteAsShorts();
 
@@ -240,7 +240,7 @@
         int samplesPerPixel = sfield == null ? 1 : (int)sfield.getAsLong(0);
 
         // Read the TIFF_PLANAR_CONFIGURATION field
-        TIFFField planarConfigurationField = 
+        TIFFField planarConfigurationField =
             dir.getField(TIFFImageDecoder.TIFF_PLANAR_CONFIGURATION);
         char[] planarConfiguration = planarConfigurationField == null ?
             new char[] {1} :
@@ -252,7 +252,7 @@
             }
 
             // Read the TIFF_BITS_PER_SAMPLE field
-            TIFFField bitsField = 
+            TIFFField bitsField =
                 dir.getField(TIFFImageDecoder.TIFF_BITS_PER_SAMPLE);
             char[] bitsPerSample = null;
             if(bitsField != null) {
@@ -271,7 +271,7 @@
 
             // Read the TIFF_SAMPLE_FORMAT tag to see whether the data might be
             // signed or floating point
-            TIFFField sampleFormatField = 
+            TIFFField sampleFormatField =
                 dir.getField(TIFFImageDecoder.TIFF_SAMPLE_FORMAT);
 
             char[] sampleFormat = null;
@@ -424,13 +424,13 @@
             if (dir.getField(TIFFImageDecoder.TIFF_TILE_OFFSETS) != null) {
                 tiled = true;
                 // Image is in tiled format
-                tileWidth = 
+                tileWidth =
                     (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_TILE_WIDTH);
-                tileHeight = 
+                tileHeight =
                     (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_TILE_LENGTH);
-                tileOffsets = 
+                tileOffsets =
                     (dir.getField(TIFFImageDecoder.TIFF_TILE_OFFSETS)).getAsLongs();
-                tileByteCounts = 
+                tileByteCounts =
                     getFieldAsLongs(dir.getField(TIFFImageDecoder.TIFF_TILE_BYTE_COUNTS));
 
             } else {
@@ -449,7 +449,7 @@
                     dir.getField(TIFFImageDecoder.TIFF_ROWS_PER_STRIP);
                 if (field == null) {
                     // Default is infinity (2^32 -1), basically the entire image
-		
+
                     tileHeight =
                         dir.getField(TIFFImageDecoder.TIFF_TILE_LENGTH) != null ?
                         (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_TILE_LENGTH):
@@ -458,7 +458,7 @@
                     long l = field.getAsLong(0);
                     long infinity = 1;
                     infinity = (infinity << 32) - 1;
-                    if (l == infinity) {   
+                    if (l == infinity) {
                         // 2^32 - 1 (effectively infinity, entire image is 1 strip)
                         tileHeight = bounds.height;
                     } else {
@@ -466,7 +466,7 @@
                     }
                 }
 
-                TIFFField tileOffsetsField = 
+                TIFFField tileOffsetsField =
                     dir.getField(TIFFImageDecoder.TIFF_STRIP_OFFSETS);
                 if (tileOffsetsField == null) {
                     throw new RuntimeException("TIFFImage5");
@@ -474,7 +474,7 @@
                     tileOffsets = getFieldAsLongs(tileOffsetsField);
                 }
 
-                TIFFField tileByteCountsField = 
+                TIFFField tileByteCountsField =
                     dir.getField(TIFFImageDecoder.TIFF_STRIP_BYTE_COUNTS);
                 if (tileByteCountsField == null) {
                     throw new RuntimeException("TIFFImage6");
@@ -491,7 +491,7 @@
             // Check whether big endian or little endian format is used.
             isBigEndian = dir.isBigEndian();
 
-            TIFFField fillOrderField = 
+            TIFFField fillOrderField =
                 dir.getField(TIFFImageDecoder.TIFF_FILL_ORDER);
             if (fillOrderField != null) {
                 fillOrder = fillOrderField.getAsInt(0);
@@ -512,12 +512,12 @@
             case COMP_FAX_G3_2D:
             case COMP_FAX_G4_2D:
                 if(sampleSize != 1) {
-                    throw new RuntimeException("TIFFImage7"); 
+                    throw new RuntimeException("TIFFImage7");
                 }
 
                 // Fax T.4 compression options
                 if (compression == 3) {
-                    TIFFField t4OptionsField = 
+                    TIFFField t4OptionsField =
                         dir.getField(TIFFImageDecoder.TIFF_T4_OPTIONS);
                     if (t4OptionsField != null) {
                         tiffT4Options = t4OptionsField.getAsLong(0);
@@ -546,26 +546,26 @@
 
             case COMP_LZW:
                 // LZW compression used, need to create the LZW decoder.
-                TIFFField predictorField = 
+                TIFFField predictorField =
                     dir.getField(TIFFImageDecoder.TIFF_PREDICTOR);
 
                 if (predictorField == null) {
                     predictor = 1;
                 } else {
                     predictor = predictorField.getAsInt(0);
-		    
+
                     if (predictor != 1 && predictor != 2) {
-                        throw new RuntimeException("TIFFImage8"); 
+                        throw new RuntimeException("TIFFImage8");
                     }
-		    
+
                     if (predictor == 2 && sampleSize != 8) {
-                        throw new RuntimeException(sampleSize + 
+                        throw new RuntimeException(sampleSize +
                                                    "TIFFImage9");
                     }
                 }
-		
-                lzwDecoder = new TIFFLZWDecoder(tileWidth, predictor, 
-                                                samplesPerPixel); 
+
+                lzwDecoder = new TIFFLZWDecoder(tileWidth, predictor,
+                                                samplesPerPixel);
                 break;
 
             case COMP_JPEG_OLD:
@@ -601,9 +601,9 @@
             switch(imageType) {
             case TYPE_BILEVEL:
             case TYPE_GRAY_4BIT:
-                sampleModel = 
+                sampleModel =
                     new MultiPixelPackedSampleModel(dataType,
-                                                    tileWidth, 
+                                                    tileWidth,
                                                     tileHeight,
                                                     sampleSize);
                 if(imageType == TYPE_BILEVEL) {
@@ -640,15 +640,15 @@
                 if(imageType == TYPE_GRAY) {
                   colorModel = new ComponentColorModel
                     (ColorSpace.getInstance(ColorSpace.CS_GRAY),
-                     new int[] { sampleSize }, false, false, 
+                     new int[] { sampleSize }, false, false,
                      Transparency.OPAQUE, dataType);
                 } else if (imageType == TYPE_RGB) {
                   colorModel = new ComponentColorModel
                     (ColorSpace.getInstance(ColorSpace.CS_sRGB),
-                     new int[] { sampleSize, sampleSize, sampleSize }, 
+                     new int[] { sampleSize, sampleSize, sampleSize },
                      false, false, Transparency.OPAQUE, dataType);
                 } else { // hasAlpha
-                    // Transparency.OPAQUE signifies image data that is 
+                    // Transparency.OPAQUE signifies image data that is
                     // completely opaque, meaning that all pixels have an alpha
                     // value of 1.0. So the extra band gets ignored, which is
                     // what we want.
@@ -677,7 +677,7 @@
                 for (int i=0; i<numBands; i++) {
                     bandOffsets[i] = i;
                 }
-		
+
                 sampleModel = new PixelInterleavedSampleModel
                     (dataType, tileWidth, tileHeight,
                      numBands, numBands * tileWidth, bandOffsets);
@@ -698,10 +698,10 @@
                 if (decodePaletteAsShorts) {
                     numBands = 3;
 
-                    // If no SampleFormat tag was specified and if the 
-                    // sampleSize is less than or equal to 8, then the 
-                    // dataType was initially set to byte, but now we want to 
-                    // expand the palette as shorts, so the dataType should 
+                    // If no SampleFormat tag was specified and if the
+                    // sampleSize is less than or equal to 8, then the
+                    // dataType was initially set to byte, but now we want to
+                    // expand the palette as shorts, so the dataType should
                     // be ushort.
                     if (dataType == DataBuffer.TYPE_BYTE) {
                         dataType = DataBuffer.TYPE_USHORT;
@@ -715,22 +715,22 @@
 
                   colorModel = new ComponentColorModel
                     (ColorSpace.getInstance(ColorSpace.CS_sRGB),
-                     new int[] { 16, 16, 16 }, false, false, 
+                     new int[] { 16, 16, 16 }, false, false,
                      Transparency.OPAQUE, dataType);
 
                 } else {
 
                     numBands = 1;
-		
+
                     if (sampleSize == 4) {
                         // Pixel data will not be unpacked, will use
                         // MPPSM to store packed data and
                         // IndexColorModel to do the unpacking.
                         sampleModel = new MultiPixelPackedSampleModel
-                            (DataBuffer.TYPE_BYTE, tileWidth, tileHeight, 
+                            (DataBuffer.TYPE_BYTE, tileWidth, tileHeight,
                              sampleSize);
                     } else if (sampleSize == 8) {
-                        
+
                         sampleModel = createPixelInterleavedSampleModel
                             (DataBuffer.TYPE_BYTE, tileWidth, tileHeight,
                              numBands);
@@ -746,12 +746,12 @@
                             (DataBuffer.TYPE_USHORT, tileWidth, tileHeight,
                              numBands);
                     }
-		
+
                     int bandLength = colormap.length/3;
-                    byte r[] = new byte[bandLength];
-                    byte g[] = new byte[bandLength];
-                    byte b[] = new byte[bandLength];
-		
+                    byte[] r = new byte[bandLength];
+                    byte[] g = new byte[bandLength];
+                    byte[] b = new byte[bandLength];
+
                     int gIndex = bandLength;
                     int bIndex = bandLength * 2;
 
@@ -776,10 +776,10 @@
                             b[i] = param.decode16BitsTo8Bits
                                 (colormap[bIndex+i] & 0xffff);
                         }
-		    
+
                     }
 
-                    colorModel = new IndexColorModel(sampleSize, 
+                    colorModel = new IndexColorModel(sampleSize,
                                                      bandLength, r, g, b);
                 }
                 break;
@@ -794,7 +794,7 @@
 
         // System.out.println("Constructed TIFF");
 
-        init((CachableRed)null, bounds, colorModel, sampleModel, 
+        init((CachableRed)null, bounds, colorModel, sampleModel,
              0, 0, properties);
     }
 
@@ -827,9 +827,9 @@
 
 
         // Get the data array out of the DataBuffer
-        byte bdata[] = null;
-        short sdata[] = null;
-        int idata[] = null;
+        byte[] bdata = null;
+        short[] sdata = null;
+        int[] idata = null;
 
         SampleModel sampleModel = getSampleModel();
         WritableRaster tile = makeTile(tileX,tileY);
@@ -871,11 +871,11 @@
         else
             newRect = new Rectangle(tile.getMinX(), tile.getMinY(),
                                     tileWidth, tileHeight);
-            
+
         int unitsInThisTile = newRect.width * newRect.height * numBands;
 
         // Allocate read buffer if needed.
-        byte data[] = compression != COMP_NONE || imageType == TYPE_PALETTE ?
+        byte[] data = compression != COMP_NONE || imageType == TYPE_PALETTE ?
             new byte[byteCount] : null;
 
         // Read the data, uncompressing as needed. There are four cases:
@@ -885,7 +885,7 @@
                 if (compression == COMP_PACKBITS) {
                     stream.readFully(data, 0, byteCount);
 
-                    // Since the decompressed data will still be packed 
+                    // Since the decompressed data will still be packed
                     // 8 pixels into 1 byte, calculate bytesInThisTile
                     int bytesInThisTile;
                     if ((newRect.width % 8) == 0) {
@@ -924,40 +924,40 @@
             if (sampleSize == 16) {
 
                 if (decodePaletteAsShorts) {
-		    
-                    short tempData[]= null;
-		    		    
+
+                    short[] tempData= null;
+
                     // At this point the data is 1 banded and will
                     // become 3 banded only after we've done the palette
                     // lookup, since unitsInThisTile was calculated with
                     // 3 bands, we need to divide this by 3.
                     int unitsBeforeLookup = unitsInThisTile / 3;
-		    
-                    // Since unitsBeforeLookup is the number of shorts, 
-                    // but we do our decompression in terms of bytes, we 
+
+                    // Since unitsBeforeLookup is the number of shorts,
+                    // but we do our decompression in terms of bytes, we
                     // need to multiply it by 2 in order to figure out
                     // how many bytes we'll get after decompression.
                     int entries = unitsBeforeLookup * 2;
-		    
+
                     // Read the data, if compressed, decode it, reset the pointer
                     try {
-			
+
                         if (compression == COMP_PACKBITS) {
 
                             stream.readFully(data, 0, byteCount);
-			    
-                            byte byteArray[] = new byte[entries];
+
+                            byte[] byteArray = new byte[entries];
                             decodePackbits(data, entries, byteArray);
                             tempData = new short[unitsBeforeLookup];
                             interpretBytesAsShorts(byteArray, tempData,
                                                    unitsBeforeLookup);
-			    
+
                         }  else if (compression == COMP_LZW) {
 
                             // Read in all the compressed data for this tile
                             stream.readFully(data, 0, byteCount);
 
-                            byte byteArray[] = new byte[entries];
+                            byte[] byteArray = new byte[entries];
                             lzwDecoder.decode(data, byteArray, newRect.height);
                             tempData = new short[unitsBeforeLookup];
                             interpretBytesAsShorts(byteArray, tempData,
@@ -966,7 +966,7 @@
                         }  else if (compression == COMP_DEFLATE) {
 
                             stream.readFully(data, 0, byteCount);
-                            byte byteArray[] = new byte[entries];
+                            byte[] byteArray = new byte[entries];
                             inflate(data, byteArray);
                             tempData = new short[unitsBeforeLookup];
                             interpretBytesAsShorts(byteArray, tempData,
@@ -975,12 +975,12 @@
                         } else if (compression == COMP_NONE) {
 
                             // byteCount tells us how many bytes are there
-                            // in this tile, but we need to read in shorts, 
+                            // in this tile, but we need to read in shorts,
                             // which will take half the space, so while
                             // allocating we divide byteCount by 2.
                             tempData = new short[byteCount/2];
                             readShorts(byteCount/2, tempData);
-                        }			
+                        }
 
                         stream.seek(save_offset);
 
@@ -989,16 +989,16 @@
                     }
 
                     if (dataType == DataBuffer.TYPE_USHORT) {
-			
+
                         // Expand the palette image into an rgb image with ushort
                         // data type.
-                        int cmapValue; 
+                        int cmapValue;
                         int count = 0, lookup, len = colormap.length/3;
                         int len2 = len * 2;
                         for (int i=0; i<unitsBeforeLookup; i++) {
                             // Get the index into the colormap
                             lookup = tempData[i] & 0xffff;
-                            // Get the blue value 
+                            // Get the blue value
                             cmapValue = colormap[lookup+len2];
                             sdata[count++] = (short)(cmapValue & 0xffff);
                             // Get the green value
@@ -1011,15 +1011,15 @@
 
                     } else if (dataType == DataBuffer.TYPE_SHORT) {
 
-                        // Expand the palette image into an rgb image with 
+                        // Expand the palette image into an rgb image with
                         // short data type.
-                        int cmapValue; 
+                        int cmapValue;
                         int count = 0, lookup, len = colormap.length/3;
                         int len2 = len * 2;
                         for (int i=0; i<unitsBeforeLookup; i++) {
                             // Get the index into the colormap
                             lookup = tempData[i] & 0xffff;
-                            // Get the blue value 
+                            // Get the blue value
                             cmapValue = colormap[lookup+len2];
                             sdata[count++] = (short)cmapValue;
                             // Get the green value
@@ -1032,89 +1032,89 @@
                     }
 
                 } else {
-		    
+
                     // No lookup being done here, when RGB values are needed,
                     // the associated IndexColorModel can be used to get them.
-		    
+
                     try {
 
                         if (compression == COMP_PACKBITS) {
 
                             stream.readFully(data, 0, byteCount);
 
-                            // Since unitsInThisTile is the number of shorts, 
-                            // but we do our decompression in terms of bytes, we 
-                            // need to multiply unitsInThisTile by 2 in order to 
-                            // figure out how many bytes we'll get after 
+                            // Since unitsInThisTile is the number of shorts,
+                            // but we do our decompression in terms of bytes, we
+                            // need to multiply unitsInThisTile by 2 in order to
+                            // figure out how many bytes we'll get after
                             // decompression.
                             int bytesInThisTile = unitsInThisTile * 2;
 
-                            byte byteArray[] = new byte[bytesInThisTile];
+                            byte[] byteArray = new byte[bytesInThisTile];
                             decodePackbits(data, bytesInThisTile, byteArray);
                             interpretBytesAsShorts(byteArray, sdata,
                                                    unitsInThisTile);
-			    
+
                         } else if (compression == COMP_LZW) {
-			    
+
                             stream.readFully(data, 0, byteCount);
 
-                            // Since unitsInThisTile is the number of shorts, 
-                            // but we do our decompression in terms of bytes, we 
-                            // need to multiply unitsInThisTile by 2 in order to 
-                            // figure out how many bytes we'll get after 
+                            // Since unitsInThisTile is the number of shorts,
+                            // but we do our decompression in terms of bytes, we
+                            // need to multiply unitsInThisTile by 2 in order to
+                            // figure out how many bytes we'll get after
                             // decompression.
-                            byte byteArray[] = new byte[unitsInThisTile * 2];
+                            byte[] byteArray = new byte[unitsInThisTile * 2];
                             lzwDecoder.decode(data, byteArray, newRect.height);
-                            interpretBytesAsShorts(byteArray, sdata, 
+                            interpretBytesAsShorts(byteArray, sdata,
                                                    unitsInThisTile);
 
                         }  else if (compression == COMP_DEFLATE) {
 
                             stream.readFully(data, 0, byteCount);
-                            byte byteArray[] = new byte[unitsInThisTile * 2];
+                            byte[] byteArray = new byte[unitsInThisTile * 2];
                             inflate(data, byteArray);
-                            interpretBytesAsShorts(byteArray, sdata, 
+                            interpretBytesAsShorts(byteArray, sdata,
                                                    unitsInThisTile);
 
                         } else if (compression == COMP_NONE) {
 
                             readShorts(byteCount/2, sdata);
                         }
-			
+
                         stream.seek(save_offset);
 
                     } catch (IOException ioe) {
                         throw new RuntimeException("TIFFImage13");
-                    }		    
+                    }
                 }
-		
+
             } else if (sampleSize == 8) {
 
                 if (decodePaletteAsShorts) {
-		    
-                    byte tempData[]= null;
+
+                    byte[] tempData= null;
 
                     // At this point the data is 1 banded and will
                     // become 3 banded only after we've done the palette
                     // lookup, since unitsInThisTile was calculated with
                     // 3 bands, we need to divide this by 3.
                     int unitsBeforeLookup = unitsInThisTile / 3;
-		    
+
                     // Read the data, if compressed, decode it, reset the pointer
                     try {
-			
+
                         if (compression == COMP_PACKBITS) {
-			    
+
                             stream.readFully(data, 0, byteCount);
                             tempData = new byte[unitsBeforeLookup];
                             decodePackbits(data, unitsBeforeLookup, tempData);
-			    
+
                         }  else if (compression == COMP_LZW) {
-			    
+
                             stream.readFully(data, 0, byteCount);
                             tempData = new byte[unitsBeforeLookup];
                             lzwDecoder.decode(data, tempData, newRect.height);
-			    
+
                         } else if (compression == COMP_JPEG_TTN2) {
 
                             stream.readFully(data, 0, byteCount);
@@ -1144,7 +1144,7 @@
 
                             tempData = new byte[byteCount];
                             stream.readFully(tempData, 0, byteCount);
-                        }			
+                        }
 
                         stream.seek(save_offset);
 
@@ -1154,13 +1154,13 @@
 
                     // Expand the palette image into an rgb image with ushort
                     // data type.
-                    int cmapValue; 
+                    int cmapValue;
                     int count = 0, lookup, len = colormap.length/3;
                     int len2 = len * 2;
                     for (int i=0; i<unitsBeforeLookup; i++) {
                         // Get the index into the colormap
                         lookup = tempData[i] & 0xff;
-                        // Get the blue value 
+                        // Get the blue value
                         cmapValue = colormap[lookup+len2];
                         sdata[count++] = (short)(cmapValue & 0xffff);
                         // Get the green value
@@ -1171,22 +1171,22 @@
                         sdata[count++] = (short)(cmapValue & 0xffff);
                     }
                 } else {
-		    
+
                     // No lookup being done here, when RGB values are needed,
                     // the associated IndexColorModel can be used to get them.
-		    
+
                     try {
 
                         if (compression == COMP_PACKBITS) {
-			    
+
                             stream.readFully(data, 0, byteCount);
                             decodePackbits(data, unitsInThisTile, bdata);
-			    
+
                         } else if (compression == COMP_LZW) {
-			    
+
                             stream.readFully(data, 0, byteCount);
                             lzwDecoder.decode(data, bdata, newRect.height);
-			    
+
                         } else if (compression == COMP_JPEG_TTN2) {
 
                             stream.readFully(data, 0, byteCount);
@@ -1197,7 +1197,7 @@
                                                     tile.getMinY()));
 
                         }  else if (compression == COMP_DEFLATE) {
-                            
+
                             stream.readFully(data, 0, byteCount);
                             inflate(data, bdata);
 
@@ -1205,24 +1205,24 @@
 
                             stream.readFully(bdata, 0, byteCount);
                         }
-			
+
                         stream.seek(save_offset);
 
                     } catch (IOException ioe) {
                         throw new RuntimeException("TIFFImage13");
-                    }		    
+                    }
                 }
-		
+
             } else if (sampleSize == 4) {
-		
+
                 int padding = (newRect.width % 2 == 0) ? 0 : 1;
                 int bytesPostDecoding = ((newRect.width/2 + padding) *
                                          newRect.height);
-		
+
                 // Output short images
-                if (decodePaletteAsShorts) {		
-		    
-                    byte tempData[] = null;
+                if (decodePaletteAsShorts) {
+
+                    byte[] tempData = null;
 
                     try {
                         stream.readFully(data, 0, byteCount);
@@ -1230,48 +1230,48 @@
                     } catch (IOException ioe) {
                         throw new RuntimeException("TIFFImage13");
                     }
-		    
+
                     // If compressed, decode the data.
                     if (compression == COMP_PACKBITS) {
-			
+
                         tempData = new byte[bytesPostDecoding];
                         decodePackbits(data, bytesPostDecoding, tempData);
-			
+
                     }  else if (compression == COMP_LZW) {
-			
+
                         tempData = new byte[bytesPostDecoding];
                         lzwDecoder.decode(data, tempData, newRect.height);
-			
+
                     }  else if (compression == COMP_DEFLATE) {
 
                         tempData = new byte[bytesPostDecoding];
                         inflate(data, tempData);
-			
+
                     } else if (compression == COMP_NONE) {
-			
+
                         tempData = data;
-                    } 
-		    
+                    }
+
                     int bytes = unitsInThisTile / 3;
-		    
-                    // Unpack the 2 pixels packed into each byte.	   
+
+                    // Unpack the 2 pixels packed into each byte.
                     data = new byte[bytes];
-		    
+
                     int srcCount = 0, dstCount = 0;
                     for (int j=0; j<newRect.height; j++) {
                         for (int i=0; i<newRect.width/2; i++) {
-                            data[dstCount++] = 
+                            data[dstCount++] =
                                 (byte)((tempData[srcCount] & 0xf0) >> 4);
-                            data[dstCount++] = 
+                            data[dstCount++] =
                                 (byte)(tempData[srcCount++] & 0x0f);
                         }
-		       
+
                         if (padding == 1) {
-                            data[dstCount++] = 
+                            data[dstCount++] =
                                 (byte)((tempData[srcCount++] & 0xf0) >> 4);
                         }
                     }
-		    
+
                     int len = colormap.length/3;
                     int len2 = len*2;
                     int cmapValue, lookup;
@@ -1286,31 +1286,31 @@
                         sdata[count++] = (short)(cmapValue & 0xffff);
                     }
                 } else {
-		    
+
                     // Output byte values, use IndexColorModel for unpacking
                     try {
-			
+
                         // If compressed, decode the data.
                         if (compression == COMP_PACKBITS) {
-			    
+
                             stream.readFully(data, 0, byteCount);
                             decodePackbits(data, bytesPostDecoding, bdata);
-			    
+
                         }  else if (compression == COMP_LZW) {
-			    
+
                             stream.readFully(data, 0, byteCount);
                             lzwDecoder.decode(data, bdata, newRect.height);
-			    
+
                         }  else if (compression == COMP_DEFLATE) {
 
                             stream.readFully(data, 0, byteCount);
                             inflate(data, bdata);
 
                         } else if (compression == COMP_NONE) {
-			    
+
                             stream.readFully(bdata, 0, byteCount);
-                        } 
-			
+                        }
+
                         stream.seek(save_offset);
 
                     } catch (IOException ioe) {
@@ -1321,58 +1321,58 @@
         } else if(imageType == TYPE_GRAY_4BIT) { // 4-bit gray
             try {
                 if (compression == COMP_PACKBITS) {
-			
+
                     stream.readFully(data, 0, byteCount);
-			
-                    // Since the decompressed data will still be packed 
+
+                    // Since the decompressed data will still be packed
                     // 2 pixels into 1 byte, calculate bytesInThisTile
                     int bytesInThisTile;
                     if ((newRect.width % 8) == 0) {
                         bytesInThisTile = (newRect.width/2) * newRect.height;
                     } else {
-                        bytesInThisTile = (newRect.width/2 + 1) * 
+                        bytesInThisTile = (newRect.width/2 + 1) *
                             newRect.height;
                     }
-			
+
                     decodePackbits(data, bytesInThisTile, bdata);
-			
+
                 } else if (compression == COMP_LZW) {
-			
+
                     stream.readFully(data, 0, byteCount);
                     lzwDecoder.decode(data, bdata, newRect.height);
-			
+
                 }  else if (compression == COMP_DEFLATE) {
-			
+
                     stream.readFully(data, 0, byteCount);
                     inflate(data, bdata);
-			
+
                 } else {
 
                     stream.readFully(bdata, 0, byteCount);
                 }
-		
+
                 stream.seek(save_offset);
             } catch (IOException ioe) {
                 throw new RuntimeException("TIFFImage13");
             }
         } else { // everything else
             try {
-		
+
                 if (sampleSize == 8) {
-		    
+
                     if (compression == COMP_NONE) {
                         stream.readFully(bdata, 0, byteCount);
-			
+
                     } else if (compression == COMP_LZW) {
-			
+
                         stream.readFully(data, 0, byteCount);
                         lzwDecoder.decode(data, bdata, newRect.height);
-			
+
                     } else if (compression == COMP_PACKBITS) {
-			
+
                         stream.readFully(data, 0, byteCount);
                         decodePackbits(data, unitsInThisTile, bdata);
-			
+
                     } else if (compression == COMP_JPEG_TTN2) {
 
                         stream.readFully(data, 0, byteCount);
@@ -1386,92 +1386,92 @@
                         stream.readFully(data, 0, byteCount);
                         inflate(data, bdata);
                     }
-		    
+
                 } else if (sampleSize == 16) {
-		    
+
                     if (compression == COMP_NONE) {
-			
+
                         readShorts(byteCount/2, sdata);
-			
+
                     } else if (compression == COMP_LZW) {
-			
+
                         stream.readFully(data, 0, byteCount);
 
-                        // Since unitsInThisTile is the number of shorts, 
-                        // but we do our decompression in terms of bytes, we 
-                        // need to multiply unitsInThisTile by 2 in order to 
-                        // figure out how many bytes we'll get after 
+                        // Since unitsInThisTile is the number of shorts,
+                        // but we do our decompression in terms of bytes, we
+                        // need to multiply unitsInThisTile by 2 in order to
+                        // figure out how many bytes we'll get after
                         // decompression.
-                        byte byteArray[] = new byte[unitsInThisTile * 2];
+                        byte[] byteArray = new byte[unitsInThisTile * 2];
                         lzwDecoder.decode(data, byteArray, newRect.height);
-                        interpretBytesAsShorts(byteArray, sdata, 
+                        interpretBytesAsShorts(byteArray, sdata,
                                                unitsInThisTile);
-			
+
                     } else if (compression == COMP_PACKBITS) {
-			
+
                         stream.readFully(data, 0, byteCount);
 
-                        // Since unitsInThisTile is the number of shorts, 
-                        // but we do our decompression in terms of bytes, we 
-                        // need to multiply unitsInThisTile by 2 in order to 
-                        // figure out how many bytes we'll get after 
+                        // Since unitsInThisTile is the number of shorts,
+                        // but we do our decompression in terms of bytes, we
+                        // need to multiply unitsInThisTile by 2 in order to
+                        // figure out how many bytes we'll get after
                         // decompression.
                         int bytesInThisTile = unitsInThisTile * 2;
 
-                        byte byteArray[] = new byte[bytesInThisTile];
+                        byte[] byteArray = new byte[bytesInThisTile];
                         decodePackbits(data, bytesInThisTile, byteArray);
-                        interpretBytesAsShorts(byteArray, sdata, 
+                        interpretBytesAsShorts(byteArray, sdata,
                                                unitsInThisTile);
                     } else if (compression == COMP_DEFLATE) {
 
                         stream.readFully(data, 0, byteCount);
-                        byte byteArray[] = new byte[unitsInThisTile * 2];
+                        byte[] byteArray = new byte[unitsInThisTile * 2];
                         inflate(data, byteArray);
-                        interpretBytesAsShorts(byteArray, sdata, 
+                        interpretBytesAsShorts(byteArray, sdata,
                                                unitsInThisTile);
 
                     }
                 } else if (sampleSize == 32 &&
                            dataType == DataBuffer.TYPE_INT) { // redundant
                     if (compression == COMP_NONE) {
-			
+
                         readInts(byteCount/4, idata);
 
                     } else if (compression == COMP_LZW) {
-			
+
                         stream.readFully(data, 0, byteCount);
 
-                        // Since unitsInThisTile is the number of ints, 
-                        // but we do our decompression in terms of bytes, we 
-                        // need to multiply unitsInThisTile by 4 in order to 
-                        // figure out how many bytes we'll get after 
+                        // Since unitsInThisTile is the number of ints,
+                        // but we do our decompression in terms of bytes, we
+                        // need to multiply unitsInThisTile by 4 in order to
+                        // figure out how many bytes we'll get after
                         // decompression.
-                        byte byteArray[] = new byte[unitsInThisTile * 4];
+                        byte[] byteArray = new byte[unitsInThisTile * 4];
                         lzwDecoder.decode(data, byteArray, newRect.height);
-                        interpretBytesAsInts(byteArray, idata, 
+                        interpretBytesAsInts(byteArray, idata,
                                              unitsInThisTile);
-			
+
                     } else if (compression == COMP_PACKBITS) {
-			
+
                         stream.readFully(data, 0, byteCount);
 
-                        // Since unitsInThisTile is the number of ints, 
-                        // but we do our decompression in terms of bytes, we 
-                        // need to multiply unitsInThisTile by 4 in order to 
-                        // figure out how many bytes we'll get after 
+                        // Since unitsInThisTile is the number of ints,
+                        // but we do our decompression in terms of bytes, we
+                        // need to multiply unitsInThisTile by 4 in order to
+                        // figure out how many bytes we'll get after
                         // decompression.
                         int bytesInThisTile = unitsInThisTile * 4;
 
-                        byte byteArray[] = new byte[bytesInThisTile];
+                        byte[] byteArray = new byte[bytesInThisTile];
                         decodePackbits(data, bytesInThisTile, byteArray);
-                        interpretBytesAsInts(byteArray, idata, 
+                        interpretBytesAsInts(byteArray, idata,
                                              unitsInThisTile);
                     } else if (compression == COMP_DEFLATE) {
 
                         stream.readFully(data, 0, byteCount);
-                        byte byteArray[] = new byte[unitsInThisTile * 4];
+                        byte[] byteArray = new byte[unitsInThisTile * 4];
                         inflate(data, byteArray);
-                        interpretBytesAsInts(byteArray, idata, 
+                        interpretBytesAsInts(byteArray, idata,
                                              unitsInThisTile);
 
                     }
@@ -1511,7 +1511,7 @@
                         }
                     } else if (dataType == DataBuffer.TYPE_INT) {
 
-                        long uintMax = ((long)Integer.MAX_VALUE - 
+                        long uintMax = ((long)Integer.MAX_VALUE -
                                         (long)Integer.MIN_VALUE);
                         for (int l = 0; l < idata.length; l += numBands) {
                             idata[l] = (int)(uintMax - idata[l]);
@@ -1552,7 +1552,7 @@
                         bswap = bdata[i];
                         bdata[i] = bdata[i+3];
                         bdata[i+3] = bswap;
-			
+
                         // Swap G and B
                         bswap = bdata[i+1];
                         bdata[i+1] = bdata[i+2];
@@ -1626,18 +1626,18 @@
                 break;
             }
         }
-        
+
         return tile;
     }
 
-    private void readShorts(int shortCount, short shortArray[]) {
-	
-        // Since each short consists of 2 bytes, we need a 
+    private void readShorts(int shortCount, short[] shortArray) {
+
+        // Since each short consists of 2 bytes, we need a
         // byte array of double size
         int byteCount = 2 * shortCount;
-        byte byteArray[] = new byte[byteCount];
+        byte[] byteArray = new byte[byteCount];
 
-        try {	    
+        try {
             stream.readFully(byteArray, 0, byteCount);
         } catch (IOException ioe) {
             throw new RuntimeException("TIFFImage13");
@@ -1646,14 +1646,14 @@
         interpretBytesAsShorts(byteArray, shortArray, shortCount);
     }
 
-    private void readInts(int intCount, int intArray[]) {
-	
-        // Since each int consists of 4 bytes, we need a 
+    private void readInts(int intCount, int[] intArray) {
+
+        // Since each int consists of 4 bytes, we need a
         // byte array of quadruple size
         int byteCount = 4 * intCount;
-        byte byteArray[] = new byte[byteCount];
+        byte[] byteArray = new byte[byteCount];
 
-        try {	    
+        try {
             stream.readFully(byteArray, 0, byteCount);
         } catch (IOException ioe) {
             throw new RuntimeException("TIFFImage13");
@@ -1662,10 +1662,10 @@
         interpretBytesAsInts(byteArray, intArray, intCount);
     }
 
-    // Method to interpret a byte array to a short array, depending on 
+    // Method to interpret a byte array to a short array, depending on
     // whether the bytes are stored in a big endian or little endian format.
-    private void interpretBytesAsShorts(byte byteArray[], 
-                                        short shortArray[], 
+    private void interpretBytesAsShorts(byte[] byteArray,
+                                        short[] shortArray,
                                         int shortCount) {
 
         int j = 0;
@@ -1678,9 +1678,9 @@
                 secondByte = byteArray[j++] & 0xff;
                 shortArray[i] = (short)((firstByte << 8) + secondByte);
             }
-	    
+
         } else {
-	    
+
             for (int i=0; i<shortCount; i++) {
                 firstByte = byteArray[j++] & 0xff;
                 secondByte = byteArray[j++] & 0xff;
@@ -1689,25 +1689,25 @@
         }
     }
 
-    // Method to interpret a byte array to a int array, depending on 
+    // Method to interpret a byte array to a int array, depending on
     // whether the bytes are stored in a big endian or little endian format.
-    private void interpretBytesAsInts(byte byteArray[],
-                                      int intArray[], 
+    private void interpretBytesAsInts(byte[] byteArray,
+                                      int[] intArray,
                                       int intCount) {
 
         int j = 0;
 
         if (isBigEndian) {
-	    
+
             for (int i=0; i<intCount; i++) {
                 intArray[i] = (((byteArray[j++] & 0xff) << 24) |
                                ((byteArray[j++] & 0xff) << 16) |
                                ((byteArray[j++] & 0xff) << 8) |
                                ( byteArray[j++] & 0xff));
             }
-	    
+
         } else {
-	    
+
             for (int i=0; i<intCount; i++) {
                 intArray[i] = ((byteArray[j++] & 0xff) |
                               ((byteArray[j++] & 0xff) << 8) |
@@ -1718,7 +1718,7 @@
     }
 
     // Uncompress packbits compressed image data.
-    private byte[] decodePackbits(byte data[], int arraySize, byte[] dst) {
+    private byte[] decodePackbits(byte[] data, int arraySize, byte[] dst) {
 
         if (dst == null) {
             dst = new byte[arraySize];
@@ -1728,26 +1728,26 @@
         byte repeat, b;
 
         try {
-	    
+
             while (dstCount < arraySize) {
-		
+
                 b = data[srcCount++];
-		
+
                 if (b >= 0 && b <= 127) {
-		    
+
                     // literal run packet
                     for (int i=0; i<(b + 1); i++) {
                         dst[dstCount++] = data[srcCount++];
                     }
-		    		    
+
                 } else if (b <= -1 && b >= -127) {
-		    
+
                     // 2 byte encoded run packet
                     repeat = data[srcCount++];
                     for (int i=0; i<(-b + 1); i++) {
                         dst[dstCount++] = repeat;
                     }
-		    
+
                 } else {
                     // no-op packet. Do nothing
                     srcCount++;
@@ -1763,11 +1763,11 @@
     // Need a createColorModel().
     // Create ComponentColorModel for TYPE_RGB images
     private ComponentColorModel createAlphaComponentColorModel
-    (int dataType, int numBands, 
+    (int dataType, int numBands,
      boolean isAlphaPremultiplied, int transparency) {
-	
+
         ComponentColorModel ccm = null;
-        int RGBBits[] = null;
+        int[] RGBBits = null;
         ColorSpace cs = null;
         switch(numBands) {
             case 2: // gray+alpha
@@ -1779,7 +1779,7 @@
             default:
                 throw new IllegalArgumentException();
         }
-        
+
         int componentSize = 0;
         switch(dataType) {
             case DataBuffer.TYPE_BYTE:
@@ -1809,7 +1809,7 @@
                                       dataType);
 
 
-        return ccm;		
+        return ccm;
     }
-        
+
 }

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFImageEncoder.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFImageEncoder.java?view=diff&rev=496556&r1=496555&r2=496556
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFImageEncoder.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFImageEncoder.java Mon Jan 15 16:59:48 2007
@@ -42,6 +42,7 @@
 import java.util.Iterator;
 import java.util.SortedSet;
 import java.util.TreeSet;
+import java.util.List;
 import java.util.zip.Deflater;
 
 import org.apache.xmlgraphics.image.codec.util.ImageEncodeParam;
@@ -54,7 +55,7 @@
 /**
  * A baseline TIFF writer. The writer outputs TIFF images in either Bilevel,
  * Greyscale, Palette color or Full Color modes.
- * 
+ *
  */
 public class TIFFImageEncoder extends ImageEncoderImpl {
 
@@ -91,10 +92,10 @@
     private static final int DEFAULT_ROWS_PER_STRIP = 8;
 
     public TIFFImageEncoder(OutputStream output, ImageEncodeParam param) {
-    	super(output, param);
-    	if (this.param == null) {
-    	    this.param = new TIFFEncodeParam();
-    	}
+        super(output, param);
+        if (this.param == null) {
+            this.param = new TIFFEncodeParam();
+        }
     }
 
     /**
@@ -194,11 +195,11 @@
 
     private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
                        int ifdOffset, boolean isLast) throws IOException {
-    	// Currently all images are stored uncompressed.
-    	int compression = encodeParam.getCompression();
-    
-    	// Get tiled output preference.
-    	boolean isTiled = encodeParam.getWriteTiled();
+        // Currently all images are stored uncompressed.
+        int compression = encodeParam.getCompression();
+
+        // Get tiled output preference.
+        boolean isTiled = encodeParam.getWriteTiled();
 
         // Set bounds.
         int minX = im.getMinX();
@@ -210,7 +211,7 @@
         SampleModel sampleModel = im.getSampleModel();
 
         // Retrieve and verify sample size.
-        int sampleSize[] = sampleModel.getSampleSize();
+        int[] sampleSize = sampleModel.getSampleSize();
         for(int i = 1; i < sampleSize.length; i++) {
             if(sampleSize[i] != sampleSize[0]) {
                 throw new Error("TIFFImageEncoder0");
@@ -227,8 +228,8 @@
         int dataType = sampleModel.getDataType();
         switch(dataType) {
         case DataBuffer.TYPE_BYTE:
-            if(sampleSize[0] != 1 && sampleSize[0] == 4 &&
-               sampleSize[0] != 8) {
+            if(sampleSize[0] != 1 && sampleSize[0] == 4 &&    // todo does this make sense??
+               sampleSize[0] != 8) {                          // we get error only for 4
                 throw new Error("TIFFImageEncoder2");
             }
             break;
@@ -245,26 +246,26 @@
             }
             break;
         default:
-	    throw new Error("TIFFImageEncoder5");
-	}
+            throw new Error("TIFFImageEncoder5");
+        }
 
         boolean dataTypeIsShort =
             dataType == DataBuffer.TYPE_SHORT ||
             dataType == DataBuffer.TYPE_USHORT;
 
-	ColorModel colorModel = im.getColorModel();
+        ColorModel colorModel = im.getColorModel();
         if (colorModel != null &&
             colorModel instanceof IndexColorModel &&
             dataType != DataBuffer.TYPE_BYTE) {
             // Don't support (unsigned) short palette-color images.
-	    throw new Error("TIFFImageEncoder6");
+            throw new Error("TIFFImageEncoder6");
         }
-	IndexColorModel icm = null;
-	int sizeOfColormap = 0;	
-        char colormap[] = null;
+        IndexColorModel icm = null;
+        int sizeOfColormap = 0;
+        char[] colormap = null;
 
         // Set image type.
-	int imageType = TIFF_UNSUPPORTED;
+        int imageType = TIFF_UNSUPPORTED;
         int numExtraSamples = 0;
         int extraSampleType = EXTRA_SAMPLE_UNSPECIFIED;
         if(colorModel instanceof IndexColorModel) { // Bilevel or palette
@@ -273,45 +274,45 @@
 
             if(sampleSize[0] == 1 && numBands == 1) { // Bilevel image
 
-		if (mapSize != 2) {
-		    throw new IllegalArgumentException(
-					"TIFFImageEncoder7");
-		}
-
-		byte r[] = new byte[mapSize];
-		icm.getReds(r);
-		byte g[] = new byte[mapSize];
-		icm.getGreens(g);
-		byte b[] = new byte[mapSize];
-		icm.getBlues(b);
-
-		if ((r[0] & 0xff) == 0 && 
-		    (r[1] & 0xff) == 255 && 
-		    (g[0] & 0xff) == 0 && 
-		    (g[1] & 0xff) == 255 && 
-		    (b[0] & 0xff) == 0 && 
-		    (b[1] & 0xff) == 255) {
-
-		    imageType = TIFF_BILEVEL_BLACK_IS_ZERO;
-
-		} else if ((r[0] & 0xff) == 255 && 
-			   (r[1] & 0xff) == 0 && 
-			   (g[0] & 0xff) == 255 && 
-			   (g[1] & 0xff) == 0 && 
-			   (b[0] & 0xff) == 255 && 
-			   (b[1] & 0xff) == 0) {
-		    
-		    imageType = TIFF_BILEVEL_WHITE_IS_ZERO;
-
-		} else {
-		    imageType = TIFF_PALETTE;
-		}
-
-	    } else if(numBands == 1) { // Non-bilevel image.
-		// Palette color image.
-		imageType = TIFF_PALETTE;
-	    } 
-	} else if(colorModel == null) {
+                if (mapSize != 2) {
+                    throw new IllegalArgumentException(
+                                        "TIFFImageEncoder7");
+                }
+
+                byte[] r = new byte[mapSize];
+                icm.getReds(r);
+                byte[] g = new byte[mapSize];
+                icm.getGreens(g);
+                byte[] b = new byte[mapSize];
+                icm.getBlues(b);
+
+                if ((r[0] & 0xff) == 0 &&
+                    (r[1] & 0xff) == 255 &&
+                    (g[0] & 0xff) == 0 &&
+                    (g[1] & 0xff) == 255 &&
+                    (b[0] & 0xff) == 0 &&
+                    (b[1] & 0xff) == 255) {
+
+                    imageType = TIFF_BILEVEL_BLACK_IS_ZERO;
+
+                } else if ((r[0] & 0xff) == 255 &&
+                           (r[1] & 0xff) == 0 &&
+                           (g[0] & 0xff) == 255 &&
+                           (g[1] & 0xff) == 0 &&
+                           (b[0] & 0xff) == 255 &&
+                           (b[1] & 0xff) == 0) {
+
+                    imageType = TIFF_BILEVEL_WHITE_IS_ZERO;
+
+                } else {
+                    imageType = TIFF_PALETTE;
+                }
+
+            } else if(numBands == 1) { // Non-bilevel image.
+                // Palette color image.
+                imageType = TIFF_PALETTE;
+            }
+        } else if(colorModel == null) {
 
             if(sampleSize[0] == 1 && numBands == 1) { // bilevel
                 imageType = TIFF_BILEVEL_BLACK_IS_ZERO;
@@ -379,69 +380,72 @@
                 throw new Error("TIFFImageEncoder9");
             }
         }
-	
-	int photometricInterpretation = -1;
-	switch (imageType) {
-
-	case TIFF_BILEVEL_WHITE_IS_ZERO:
-	    photometricInterpretation = 0;
-	    break;
-
-	case TIFF_BILEVEL_BLACK_IS_ZERO:
-	    photometricInterpretation = 1;
-	    break;
 
-	case TIFF_GRAY:
+        int photometricInterpretation = -1;
+        switch (imageType) {
+
+        case TIFF_BILEVEL_WHITE_IS_ZERO:
+            photometricInterpretation = 0;
+            break;
+
+        case TIFF_BILEVEL_BLACK_IS_ZERO:
+            photometricInterpretation = 1;
+            break;
+
+        case TIFF_GRAY:
         case TIFF_GENERIC:
-	    // Since the CS_GRAY colorspace is always of type black_is_zero
-	    photometricInterpretation = 1;
-	    break;
-
-	case TIFF_PALETTE:
-	    photometricInterpretation = 3;
-
-	    icm = (IndexColorModel)colorModel;
-	    sizeOfColormap = icm.getMapSize();
-
-	    byte r[] = new byte[sizeOfColormap];
-	    icm.getReds(r);
-	    byte g[] = new byte[sizeOfColormap];
-	    icm.getGreens(g);
-	    byte b[] = new byte[sizeOfColormap];
-	    icm.getBlues(b);
+            // Since the CS_GRAY colorspace is always of type black_is_zero
+            photometricInterpretation = 1;
+            break;
 
-	    int redIndex = 0, greenIndex = sizeOfColormap;
-	    int blueIndex = 2 * sizeOfColormap;
+        case TIFF_PALETTE:
+            photometricInterpretation = 3;
+
+            icm = (IndexColorModel)colorModel;
+            sizeOfColormap = icm.getMapSize();
+
+            byte[] r = new byte[sizeOfColormap];
+            icm.getReds(r);
+            byte[] g = new byte[sizeOfColormap];
+            icm.getGreens(g);
+            byte[] b = new byte[sizeOfColormap];
+            icm.getBlues(b);
+
+            int redIndex = 0, greenIndex = sizeOfColormap;
+            int blueIndex = 2 * sizeOfColormap;
             colormap = new char[sizeOfColormap * 3];
-	    for (int i=0; i<sizeOfColormap; i++) {
-                colormap[redIndex++]   = (char)(((r[i] << 8) | r[i]) & 0xffff);
-                colormap[greenIndex++] = (char)(((g[i] << 8) | g[i]) & 0xffff);
-                colormap[blueIndex++]  = (char)(((b[i] << 8) | b[i]) & 0xffff);
-	    }
-
-	    sizeOfColormap *= 3;
-
-	    break;
-
-	case TIFF_RGB:
-	    photometricInterpretation = 2;
-	    break;
+            for (int i=0; i<sizeOfColormap; i++) {
+                int tmp = 0xff & r[i];   // beware of sign extended bytes
+                colormap[redIndex++]   = (char)(( tmp << 8) | tmp );
+                tmp = 0xff & g[i];
+                colormap[greenIndex++] = (char)(( tmp << 8) | tmp );
+                tmp = 0xff & b[i];
+                colormap[blueIndex++]  = (char)(( tmp << 8) | tmp );
+            }
+
+            sizeOfColormap *= 3;
+
+            break;
+
+        case TIFF_RGB:
+            photometricInterpretation = 2;
+            break;
 
         case TIFF_CMYK:
-	    photometricInterpretation = 5;
+            photometricInterpretation = 5;
             break;
 
         case TIFF_YCBCR:
-	    photometricInterpretation = 6;
+            photometricInterpretation = 6;
             break;
 
         case TIFF_CIELAB:
-	    photometricInterpretation = 8;
+            photometricInterpretation = 8;
             break;
 
         default:
             throw new Error("TIFFImageEncoder8");
-	}
+        }
 
         // Initialize tile dimensions.
         int tileWidth;
@@ -453,7 +457,7 @@
                 encodeParam.getTileHeight() : im.getTileHeight();
         } else {
             tileWidth = width;
-            
+
             tileHeight = encodeParam.getTileHeight() > 0 ?
                 encodeParam.getTileHeight() : DEFAULT_ROWS_PER_STRIP;
         }
@@ -505,16 +509,16 @@
             numTiles = (int)Math.ceil((double)height/(double)tileHeight);
         }
 
-	long tileByteCounts[] = new long[numTiles];
+        long[] tileByteCounts = new long[numTiles];
 
-	long bytesPerRow =
+        long bytesPerRow =
             (long)Math.ceil((sampleSize[0] / 8.0) * tileWidth * numBands);
 
-	long bytesPerTile = bytesPerRow * tileHeight;
+        long bytesPerTile = bytesPerRow * tileHeight;
 
-	for (int i=0; i<numTiles; i++) {
-	    tileByteCounts[i] = bytesPerTile;
-	}
+        for (int i=0; i<numTiles; i++) {
+            tileByteCounts[i] = bytesPerTile;
+        }
 
         if(!isTiled) {
             // Last strip may have lesser rows
@@ -522,96 +526,96 @@
             tileByteCounts[numTiles-1] = lastStripRows * bytesPerRow;
         }
 
-	long totalBytesOfData = bytesPerTile * (numTiles - 1) + 
-	    tileByteCounts[numTiles-1];
+        long totalBytesOfData = bytesPerTile * (numTiles - 1) +
+            tileByteCounts[numTiles-1];
 
         // The data will be written after the IFD: create the array here
         // but fill it in later.
-	long tileOffsets[] = new long[numTiles];
+        long[] tileOffsets = new long[numTiles];
 
-	// Basic fields - have to be in increasing numerical order.
-	// ImageWidth                     256
-	// ImageLength                    257
-	// BitsPerSample                  258
-	// Compression                    259
-	// PhotoMetricInterpretation      262
-	// StripOffsets                   273
-	// RowsPerStrip                   278
-	// StripByteCounts                279
-	// XResolution                    282
-	// YResolution                    283
-	// ResolutionUnit                 296	
-
-	// Create Directory
-	SortedSet fields = new TreeSet();
-
-	// Image Width
-	fields.add(new TIFFField(TIFFImageDecoder.TIFF_IMAGE_WIDTH, 
-                                 TIFFField.TIFF_LONG, 1, 
+        // Basic fields - have to be in increasing numerical order.
+        // ImageWidth                     256
+        // ImageLength                    257
+        // BitsPerSample                  258
+        // Compression                    259
+        // PhotoMetricInterpretation      262
+        // StripOffsets                   273
+        // RowsPerStrip                   278
+        // StripByteCounts                279
+        // XResolution                    282
+        // YResolution                    283
+        // ResolutionUnit                 296
+
+        // Create Directory
+        SortedSet fields = new TreeSet();
+
+        // Image Width
+        fields.add(new TIFFField(TIFFImageDecoder.TIFF_IMAGE_WIDTH,
+                                 TIFFField.TIFF_LONG, 1,
                                  new long[] {width}));
 
-	// Image Length
-	fields.add(new TIFFField(TIFFImageDecoder.TIFF_IMAGE_LENGTH, 
-                                 TIFFField.TIFF_LONG, 1, 
+        // Image Length
+        fields.add(new TIFFField(TIFFImageDecoder.TIFF_IMAGE_LENGTH,
+                                 TIFFField.TIFF_LONG, 1,
                                  new long[] {height}));
 
         char [] shortSampleSize = new char[numBands];
         for (int i=0; i<numBands; i++)
             shortSampleSize[i] = (char)sampleSize[i];
-	fields.add(new TIFFField(TIFFImageDecoder.TIFF_BITS_PER_SAMPLE,
+        fields.add(new TIFFField(TIFFImageDecoder.TIFF_BITS_PER_SAMPLE,
                                  TIFFField.TIFF_SHORT, numBands,
                                  shortSampleSize));
 
-	fields.add(new TIFFField(TIFFImageDecoder.TIFF_COMPRESSION,
-                                 TIFFField.TIFF_SHORT, 1, 
+        fields.add(new TIFFField(TIFFImageDecoder.TIFF_COMPRESSION,
+                                 TIFFField.TIFF_SHORT, 1,
                                  new char[] {(char)compression}));
 
-	fields.add(
-	    new TIFFField(TIFFImageDecoder.TIFF_PHOTOMETRIC_INTERPRETATION,
-                          TIFFField.TIFF_SHORT, 1, 
+        fields.add(
+            new TIFFField(TIFFImageDecoder.TIFF_PHOTOMETRIC_INTERPRETATION,
+                          TIFFField.TIFF_SHORT, 1,
                                  new char[] {(char)photometricInterpretation}));
 
         if(!isTiled) {
             fields.add(new TIFFField(TIFFImageDecoder.TIFF_STRIP_OFFSETS,
-                                     TIFFField.TIFF_LONG, numTiles, 
+                                     TIFFField.TIFF_LONG, numTiles,
                                      tileOffsets));
         }
-	
-	fields.add(new TIFFField(TIFFImageDecoder.TIFF_SAMPLES_PER_PIXEL,
-                                 TIFFField.TIFF_SHORT, 1, 
+
+        fields.add(new TIFFField(TIFFImageDecoder.TIFF_SAMPLES_PER_PIXEL,
+                                 TIFFField.TIFF_SHORT, 1,
                                  new char[] {(char)numBands}));
 
         if(!isTiled) {
-            fields.add(new TIFFField(TIFFImageDecoder.TIFF_ROWS_PER_STRIP, 
-                                     TIFFField.TIFF_LONG, 1, 
+            fields.add(new TIFFField(TIFFImageDecoder.TIFF_ROWS_PER_STRIP,
+                                     TIFFField.TIFF_LONG, 1,
                                      new long[] {tileHeight}));
 
             fields.add(new TIFFField(TIFFImageDecoder.TIFF_STRIP_BYTE_COUNTS,
-                                     TIFFField.TIFF_LONG, numTiles, 
+                                     TIFFField.TIFF_LONG, numTiles,
                                      tileByteCounts));
         }
 
-	if (colormap != null) {
-	    fields.add(new TIFFField(TIFFImageDecoder.TIFF_COLORMAP,
+        if (colormap != null) {
+            fields.add(new TIFFField(TIFFImageDecoder.TIFF_COLORMAP,
                                      TIFFField.TIFF_SHORT, sizeOfColormap,
                                      colormap));
-	}
+        }
 
         if(isTiled) {
-            fields.add(new TIFFField(TIFFImageDecoder.TIFF_TILE_WIDTH, 
-                                     TIFFField.TIFF_LONG, 1, 
+            fields.add(new TIFFField(TIFFImageDecoder.TIFF_TILE_WIDTH,
+                                     TIFFField.TIFF_LONG, 1,
                                      new long[] {tileWidth}));
 
-            fields.add(new TIFFField(TIFFImageDecoder.TIFF_TILE_LENGTH, 
-                                     TIFFField.TIFF_LONG, 1, 
+            fields.add(new TIFFField(TIFFImageDecoder.TIFF_TILE_LENGTH,
+                                     TIFFField.TIFF_LONG, 1,
                                      new long[] {tileHeight}));
 
             fields.add(new TIFFField(TIFFImageDecoder.TIFF_TILE_OFFSETS,
-                                     TIFFField.TIFF_LONG, numTiles, 
+                                     TIFFField.TIFF_LONG, numTiles,
                                      tileOffsets));
 
             fields.add(new TIFFField(TIFFImageDecoder.TIFF_TILE_BYTE_COUNTS,
-                                     TIFFField.TIFF_LONG, numTiles, 
+                                     TIFFField.TIFF_LONG, numTiles,
                                      tileByteCounts));
         }
 
@@ -621,7 +625,7 @@
                 extraSamples[i] = (char)extraSampleType;
             }
             fields.add(new TIFFField(TIFFImageDecoder.TIFF_EXTRA_SAMPLES,
-                                     TIFFField.TIFF_SHORT, numExtraSamples, 
+                                     TIFFField.TIFF_SHORT, numExtraSamples,
                                      extraSamples));
         }
 
@@ -639,7 +643,7 @@
             for(int b = 1; b < numBands; b++) {
                 sampleFormat[b] = sampleFormat[0];
             }
-	    fields.add(new TIFFField(TIFFImageDecoder.TIFF_SAMPLE_FORMAT,
+            fields.add(new TIFFField(TIFFImageDecoder.TIFF_SAMPLE_FORMAT,
                                      TIFFField.TIFF_SHORT, numBands,
                                      sampleFormat));
 
@@ -728,15 +732,15 @@
             }
 
             fields.add(new TIFFField(TIFF_YCBCR_SUBSAMPLING,
-                                     TIFFField.TIFF_SHORT, 2, 
+                                     TIFFField.TIFF_SHORT, 2,
                                      new char[] {subsampleH, subsampleV}));
 
 
             // YCbCr positioning.
             fields.add(new TIFFField(TIFF_YCBCR_POSITIONING,
-                                     TIFFField.TIFF_SHORT, 1, 
-                                     new int[] {compression == COMP_JPEG_TTN2 ?
-                                                1 : 2}));
+                                     TIFFField.TIFF_SHORT, 1,
+                                     new char[]
+                {(char)((compression == COMP_JPEG_TTN2)? 1 : 2)}));
 
             // Reference black/white.
             long[][] refbw;
@@ -752,7 +756,7 @@
                     };
             }
             fields.add(new TIFFField(TIFF_REF_BLACK_WHITE,
-                                     TIFFField.TIFF_RATIONAL, 6, 
+                                     TIFFField.TIFF_RATIONAL, 6,
                                      refbw));
         }
 
@@ -762,7 +766,7 @@
         // Add extra fields specified via the encoding parameters.
         TIFFField[] extraFields = encodeParam.getExtraFields();
         if(extraFields != null) {
-            ArrayList extantTags = new ArrayList(fields.size());
+            List extantTags = new ArrayList(fields.size());
             Iterator fieldIter = fields.iterator();
             while(fieldIter.hasNext()) {
                 TIFFField fld = (TIFFField)fieldIter.next();
@@ -789,7 +793,7 @@
 
         // The first data segment is written after the field overflow
         // following the IFD so initialize the first offset accordingly.
-	tileOffsets[0] = ifdOffset + dirSize;
+        tileOffsets[0] = ifdOffset + dirSize;
 
         // Branch here depending on whether data are being comrpressed.
         // If not, then the IFD is written immediately.
@@ -854,7 +858,7 @@
             // If compressing, the cannot be written yet as the size of the
             // data segments is unknown.
 
-            if((output instanceof SeekableOutputStream)) {
+            if( output instanceof SeekableOutputStream ) {
                 // Simply seek to the first data segment position.
                 ((SeekableOutputStream)output).seek(tileOffsets[0]);
             } else {
@@ -868,7 +872,7 @@
                     RandomAccessFile raFile =
                         new RandomAccessFile(tempFile, "rw");
                     output = new SeekableOutputStream(raFile);
-                    
+
                     // this method is exited!
                 } catch(Exception e) {
                     // Allocate memory for the entire image data (!).
@@ -907,7 +911,7 @@
 
         // ---- Writing of actual image data ----
 
-	// Buffer for up to tileHeight rows of pixels
+        // Buffer for up to tileHeight rows of pixels
         int[] pixels = null;
         float[] fpixels = null;
 
@@ -933,8 +937,8 @@
             }
         }
 
-	// Process tileHeight rows at a time
-	int lastRow = minY + height;
+        // Process tileHeight rows at a time
+        int lastRow = minY + height;
         int lastCol = minX + width;
         int tileNum = 0;
         for (int row = minY; row < lastRow; row += tileHeight) {
@@ -1035,7 +1039,7 @@
 
                             // Write number of pixels exactly divisible by 8
                             for (int j=0; j<tileWidth/8; j++) {
-			
+
                                 pixel =
                                     (pixels[index++] << 7) |
                                     (pixels[index++] << 6) |
@@ -1078,21 +1082,21 @@
                     break;
 
                 case 4:
-		
+
                     index = 0;
 
                     // For each of the rows in a strip
                     for (int i=0; i<rows; i++) {
-		    
-                        // Write  the number of pixels that will fit into an 
+
+                        // Write  the number of pixels that will fit into an
                         // even number of nibbles.
-                        for (int j=0; j<tileWidth/2; j++) {
+                        for (int j=0; j < tileWidth/2; j++) {
                             pixel = (pixels[index++] << 4) | pixels[index++];
                             bpixels[k++] = (byte)pixel;
                         }
 
                         // Last pixel for odd-length lines
-                        if ((tileWidth % 2) == 1) {
+                        if ((tileWidth & 1) == 1) {
                             pixel = pixels[index++] << 4;
                             bpixels[k++] = (byte)pixel;
                         }
@@ -1114,7 +1118,7 @@
                         output.write(compressBuf, 0, numCompressedBytes);
                     }
                     break;
- 
+
                 case 8:
 
                     if(compression != COMP_JPEG_TTN2) {
@@ -1213,14 +1217,14 @@
                         output.write(compressBuf, 0, numCompressedBytes);
                     }
                     break;
-		
+
                 case 16:
 
                     int ls = 0;
                     for (int i = 0; i < size; i++) {
-                        short value = (short)pixels[i];
+                        int value = pixels[i];
                         bpixels[ls++] = (byte)((value & 0xff00) >> 8);
-                        bpixels[ls++] = (byte)(value & 0x00ff);
+                        bpixels[ls++] = (byte) (value & 0x00ff);
                     }
 
                     if(compression == COMP_NONE) {
@@ -1245,19 +1249,19 @@
                         int li = 0;
                         for (int i = 0; i < size; i++) {
                             int value = pixels[i];
-                            bpixels[li++] = (byte)((value & 0xff000000) >> 24);
-                            bpixels[li++] = (byte)((value & 0x00ff0000) >> 16);
-                            bpixels[li++] = (byte)((value & 0x0000ff00) >> 8);
-                            bpixels[li++] = (byte)(value & 0x000000ff);
+                            bpixels[li++] = (byte)((value & 0xff000000) >>> 24);
+                            bpixels[li++] = (byte)((value & 0x00ff0000) >>> 16);
+                            bpixels[li++] = (byte)((value & 0x0000ff00) >>> 8);
+                            bpixels[li++] = (byte)( value & 0x000000ff);
                         }
                     } else { // DataBuffer.TYPE_FLOAT
                         int lf = 0;
                         for (int i = 0; i < size; i++) {
                             int value = Float.floatToIntBits(fpixels[i]);
-                            bpixels[lf++] = (byte)((value & 0xff000000) >> 24);
-                            bpixels[lf++] = (byte)((value & 0x00ff0000) >> 16);
-                            bpixels[lf++] = (byte)((value & 0x0000ff00) >> 8);
-                            bpixels[lf++] = (byte)(value & 0x000000ff);
+                            bpixels[lf++] = (byte)((value & 0xff000000) >>> 24);
+                            bpixels[lf++] = (byte)((value & 0x00ff0000) >>> 16);
+                            bpixels[lf++] = (byte)((value & 0x0000ff00) >>> 8);
+                            bpixels[lf++] = (byte)( value & 0x000000ff);
                         }
                     }
                     if(compression == COMP_NONE) {
@@ -1298,7 +1302,7 @@
 
             nextIFDOffset = isLast ?
                 0 : ifdOffset + dirSize + totalBytes;
-            if ((nextIFDOffset&0x01) != 0) {
+            if ((nextIFDOffset & 0x01) != 0) {   // make it even
                 nextIFDOffset++;
                 skipByte = true;
             }
@@ -1401,7 +1405,7 @@
         // Loop over fields adding the size of all values > 4 bytes.
         Iterator iter = fields.iterator();
         while(iter.hasNext()) {
-            // Get the field.	    
+            // Get the field.
             TIFFField field = (TIFFField)iter.next();
 
             // Determine the size of the field value.
@@ -1417,89 +1421,88 @@
     }
 
     private void writeFileHeader() throws IOException {
-    	// 8 byte image file header
-    	
-    	// Byte order used within the file - Big Endian
-    	output.write('M');
-    	output.write('M');
-    	
-    	// Magic value
-    	output.write(0);
-    	output.write(42);
-    	
-    	// Offset in bytes of the first IFD.
-    	writeLong(8);
+        // 8 byte image file header
+
+        // Byte order used within the file - Big Endian
+        output.write('M');
+        output.write('M');
+
+        // Magic value
+        output.write(0);
+        output.write(42);
+
+        // Offset in bytes of the first IFD.
+        writeLong(8);
     }
 
     private void writeDirectory(int thisIFDOffset, SortedSet fields,
-                                int nextIFDOffset) 
-            throws IOException {
-    
-    	// 2 byte count of number of directory entries (fields)
-    	int numEntries = fields.size();
-    
-    	long offsetBeyondIFD = thisIFDOffset + 12 * numEntries + 4 + 2;
-    	ArrayList tooBig = new ArrayList();
-    
-    	// Write number of fields in the IFD
-    	writeUnsignedShort(numEntries);
-    
+                                int nextIFDOffset)
+        throws IOException {
+
+        // 2 byte count of number of directory entries (fields)
+        int numEntries = fields.size();
+
+        long offsetBeyondIFD = thisIFDOffset + 12 * numEntries + 4 + 2;
+        List tooBig = new ArrayList();
+
+        // Write number of fields in the IFD
+        writeUnsignedShort(numEntries);
+
         Iterator iter = fields.iterator();
-    	while(iter.hasNext()) {
-    	    
-    	    // 12 byte field entry TIFFField	    
-    	    TIFFField field = (TIFFField)iter.next();
-    
-    	    // byte 0-1 Tag that identifies a field
-    	    int tag = field.getTag();
-    	    writeUnsignedShort(tag);
-    
-    	    // byte 2-3 The field type
-    	    int type = field.getType();
-    	    writeUnsignedShort(type);
-    	    
-    	    // bytes 4-7 the number of values of the indicated type except
-                // ASCII-valued fields which require the total number of bytes.
-    	    int count = field.getCount();
+        while(iter.hasNext()) {
+
+            // 12 byte field entry TIFFField
+            TIFFField field = (TIFFField)iter.next();
+
+            // byte 0-1 Tag that identifies a field
+            int tag = field.getTag();
+            writeUnsignedShort(tag);
+
+            // byte 2-3 The field type
+            int type = field.getType();
+            writeUnsignedShort(type);
+
+            // bytes 4-7 the number of values of the indicated type except
+            // ASCII-valued fields which require the total number of bytes.
+            int count = field.getCount();
             int valueSize = getValueSize(field);
-    	    writeLong(type == TIFFField.TIFF_ASCII ? valueSize : count);
-    
-    	    // bytes 8 - 11 the value or value offset
-    	    if (valueSize > 4) {
-    
-        		// We need an offset as data won't fit into 4 bytes
-        		writeLong(offsetBeyondIFD);
-        		offsetBeyondIFD += valueSize;
-        		tooBig.add(field);
-    
-    	    } else {
-    
-    	        writeValuesAsFourBytes(field);		
-    	    }
-    
-    	}
-    
-    	// Address of next IFD
-    	writeLong(nextIFDOffset);
-    
-    	// Write the tag values that did not fit into 4 bytes
-    	for (int i = 0; i < tooBig.size(); i++) {
-    	    writeValues((TIFFField)tooBig.get(i));
-    	} 
+            writeLong(type == TIFFField.TIFF_ASCII ? valueSize : count);
+
+            // bytes 8 - 11 the value or value offset
+            if (valueSize > 4) {
+
+                // We need an offset as data won't fit into 4 bytes
+                writeLong(offsetBeyondIFD);
+                offsetBeyondIFD += valueSize;
+                tooBig.add(field);
+
+            } else {
+                writeValuesAsFourBytes(field);
+            }
+
+        }
+
+        // Address of next IFD
+        writeLong(nextIFDOffset);
+
+        // Write the tag values that did not fit into 4 bytes
+        for (int i = 0; i < tooBig.size(); i++) {
+            writeValues((TIFFField)tooBig.get(i));
+        }
     }
 
     /**
      * Determine the number of bytes in the value portion of the field.
      */
-    private static final int getValueSize(TIFFField field) {
+    private static int getValueSize(TIFFField field) {
         int type = field.getType();
         int count = field.getCount();
         int valueSize = 0;
         if(type == TIFFField.TIFF_ASCII) {
             for(int i = 0; i < count; i++) {
-                byte[] stringBytes = field.getAsString(i).getBytes();
+                byte[] stringBytes = field.getAsString(i).getBytes();   // note: default encoding @work here!
                 valueSize += stringBytes.length;
-                if(stringBytes[stringBytes.length-1] != (byte)0) {
+                if( stringBytes[stringBytes.length-1] != 0 ) {
                     valueSize++;
                 }
             }
@@ -1508,7 +1511,7 @@
         }
         return valueSize;
     }
-    
+
     private static final int[] sizeOfType = {
         0, //  0 = n/a
         1, //  1 = byte
@@ -1522,118 +1525,118 @@
         4, //  9 = slong
         8, // 10 = srational
         4, // 11 = float
-        8  // 12 = double 
+        8  // 12 = double
     };
 
     private void writeValuesAsFourBytes(TIFFField field) throws IOException {
-	    
-	int dataType = field.getType();
-	int count = field.getCount();
+
+        int dataType = field.getType();
+        int count = field.getCount();
 
         switch (dataType) {
-	    
-	    // unsigned 8 bits
-	case TIFFField.TIFF_BYTE:
-	    byte bytes[] = field.getAsBytes();
+
+            // unsigned 8 bits
+        case TIFFField.TIFF_BYTE:
+            byte[] bytes = field.getAsBytes();
             if (count > 4) count =4;
             for (int i=0; i<count; i++)
-		output.write(bytes[i]);
+                output.write(bytes[i]);
 
             for (int i = 0; i < (4 - count); i++)
-		output.write(0);
-	    break;
-	    
-	    // unsigned 16 bits
-	case TIFFField.TIFF_SHORT:
-            char chars[] = field.getAsChars();
-        if (count > 2) count=2;
-        for (int i=0; i<count; i++)
-            writeUnsignedShort(chars[i]);
-        for (int i = 0; i < (2 - count); i++)
-            writeUnsignedShort(0);
-
-	    break;
-	    
-	    // unsigned 32 bits
-	case TIFFField.TIFF_LONG:
-	    long longs[] = field.getAsLongs();
-
-	    for (int i=0; i<count; i++) {
-		writeLong(longs[i]);
-	    }
-	    break;
-	}
-	
+                output.write(0);
+            break;
+
+            // unsigned 16 bits
+        case TIFFField.TIFF_SHORT:
+            char[] chars = field.getAsChars();
+            if (count > 2) count=2;
+            for (int i=0; i<count; i++)
+                writeUnsignedShort(chars[i]);
+            for (int i = 0; i < (2 - count); i++)
+                writeUnsignedShort(0);
+
+            break;
+
+            // unsigned 32 bits
+        case TIFFField.TIFF_LONG:
+            long[] longs = field.getAsLongs();
+
+            for (int i=0; i<count; i++) {
+                writeLong(longs[i]);
+            }
+            break;
+        }
+
     }
 
     private void writeValues(TIFFField field) throws IOException {
 
-	int dataType = field.getType();
-	int count = field.getCount();
+        int dataType = field.getType();
+        int count = field.getCount();
+
+        switch (dataType) {
+
+            // unsigned 8 bits
+        case TIFFField.TIFF_BYTE:
+        case TIFFField.TIFF_SBYTE:
+        case TIFFField.TIFF_UNDEFINED:
+            byte[] bytes = field.getAsBytes();
+            for (int i=0; i<count; i++) {
+                output.write(bytes[i]);
+            }
+            break;
+
+            // unsigned 16 bits
+        case TIFFField.TIFF_SHORT:
+            char[] chars = field.getAsChars();
+            for (int i=0; i<count; i++) {
+                writeUnsignedShort(chars[i]);
+            }
+            break;
+        case TIFFField.TIFF_SSHORT:
+            short[] shorts = field.getAsShorts();
+            for (int i=0; i<count; i++) {
+                writeUnsignedShort(shorts[i]);
+            }
+            break;
+
+            // unsigned 32 bits
+        case TIFFField.TIFF_LONG:
+        case TIFFField.TIFF_SLONG:
+            long[] longs = field.getAsLongs();
+            for (int i=0; i<count; i++) {
+                writeLong(longs[i]);
+            }
+            break;
 
-	switch (dataType) {
-	    
-	    // unsigned 8 bits
-	case TIFFField.TIFF_BYTE:
-	case TIFFField.TIFF_SBYTE:
-	case TIFFField.TIFF_UNDEFINED:
-	    byte bytes[] = field.getAsBytes();
-	    for (int i=0; i<count; i++) {
-		output.write(bytes[i]);
-	    }
-	    break;
-	    
-	    // unsigned 16 bits
-	case TIFFField.TIFF_SHORT:
-	    char chars[] = field.getAsChars();
-	    for (int i=0; i<count; i++) {
-            writeUnsignedShort(chars[i]);
-	    }
-	    break;
-	case TIFFField.TIFF_SSHORT:
-	    short shorts[] = field.getAsShorts();
-	    for (int i=0; i<count; i++) {
-		writeUnsignedShort(shorts[i]);
-	    }
-	    break;
-	    
-	    // unsigned 32 bits
-	case TIFFField.TIFF_LONG:
-	case TIFFField.TIFF_SLONG:
-	    long longs[] = field.getAsLongs();
-	    for (int i=0; i<count; i++) {
-		writeLong(longs[i]);
-	    }
-	    break;
-		    
         case TIFFField.TIFF_FLOAT:
             float[] floats = field.getAsFloats();
-	    for (int i=0; i<count; i++) {
+            for (int i=0; i<count; i++) {
                 int intBits = Float.floatToIntBits(floats[i]);
-		writeLong(intBits);
-	    }
+                writeLong(intBits);
+            }
             break;
 
         case TIFFField.TIFF_DOUBLE:
             double[] doubles = field.getAsDoubles();
-	    for (int i=0; i<count; i++) {
+            for (int i=0; i<count; i++) {
                 long longBits = Double.doubleToLongBits(doubles[i]);
-                writeLong(longBits >>> 32);
-		writeLong(longBits & 0xffffffff);
-	    }
+                writeLong(longBits >>> 32);           // write upper 32 bits
+                writeLong(longBits & 0xffffffffL);    // write lower 32 bits
+            }
             break;
 
-	case TIFFField.TIFF_RATIONAL:
-	case TIFFField.TIFF_SRATIONAL:
-	    long rationals[][] = field.getAsRationals();
-	    for (int i=0; i<count; i++) {
-		writeLong(rationals[i][0]);
-		writeLong(rationals[i][1]);
-	    }
-	    break;
+        case TIFFField.TIFF_RATIONAL:
+        case TIFFField.TIFF_SRATIONAL:
+            long[][] rationals = field.getAsRationals();
+            for (int i=0; i<count; i++) {
+                writeLong(rationals[i][0]);
+                writeLong(rationals[i][1]);
+            }
+            break;
 
         case TIFFField.TIFF_ASCII:
-	    for (int i=0; i<count; i++) {
+            for (int i=0; i<count; i++) {
                 byte[] stringBytes = field.getAsString(i).getBytes();
                 output.write(stringBytes);
                 if(stringBytes[stringBytes.length-1] != (byte)0) {
@@ -1645,22 +1648,27 @@
         default:
             throw new Error("TIFFImageEncoder10");
 
-	}
+        }
 
     }
 
-    // Here s is never expected to have value greater than what can be 
+    // Here s is never expected to have value greater than what can be
     // stored in 2 bytes.
     private void writeUnsignedShort(int s) throws IOException {
-	output.write((s & 0xff00) >>> 8);
-	output.write(s & 0x00ff);
+        output.write((s & 0xff00) >>> 8);
+        output.write( s & 0x00ff);
     }
 
+    /**
+     * despite its name, this method writes only 4 bytes to output.
+     * @param l 32bits of this are written as 4 bytes
+     * @throws IOException
+     */
     private void writeLong(long l) throws IOException {
         output.write( (int)((l & 0xff000000) >>> 24));
         output.write( (int)((l & 0x00ff0000) >>> 16));
         output.write( (int)((l & 0x0000ff00) >>> 8));
-        output.write( ((int)l & 0x000000ff));
+        output.write( (int) (l & 0x000000ff) );
     }
 
     /**
@@ -1771,4 +1779,3 @@
       }
     }
 }
-



---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org