Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed dependency on com.sun classes.

Removed JPEG compression support from internal TIFF codec.
Removed JPEGImageWriter which was writing JPEG using the com.sun classes.

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/commons/trunk@1028385 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit a902f9e09e4ec843d51bcbefaf886e955859ee6c 1 parent 74d5e8a
Jeremias Märki jmaerki authored
19 build.xml
View
@@ -126,23 +126,8 @@
<available property="jdk14.present" classname="java.lang.CharSequence"/>
<fail message="${Name} requires at least Java 1.4" unless="jdk14.present"/>
- <available property="sun.jpeg.present" classname="com.sun.image.codec.jpeg.JPEGCodec"/>
- <condition property="sun.jpeg.message" value="Sun-private JPEG Support PRESENT">
- <equals arg1="${sun.jpeg.present}" arg2="true"/>
- </condition>
- <condition property="sun.jpeg.message" value="Sun-private JPEG Support NOT Present">
- <not>
- <equals arg1="${sun.jpeg.present}" arg2="true"/>
- </not>
- </condition>
- <echo message="${sun.jpeg.message}"/>
<condition property="internal-codecs.eff.disabled">
- <or>
- <not>
- <equals arg1="${sun.jpeg.present}" arg2="true"/>
- </not>
- <equals arg1="${internal-codecs.disabled}" arg2="true"/>
- </or>
+ <equals arg1="${internal-codecs.disabled}" arg2="true"/>
</condition>
<available property="junit.present" classname="junit.framework.TestCase"
@@ -209,7 +194,6 @@
<patternset id="compile-pattern">
<include name="**/*.java"/>
<exclude name="**/image/codec/tiff/**/*.java"/>
- <exclude name="**/image/writer/internal/JPEGImageWriter*.java"/>
<exclude name="**/image/writer/internal/TIFFImageWriter*.java"/>
</patternset>
</target>
@@ -336,7 +320,6 @@
<fileset dir="${basedir}/test/java">
<include name="**/*Test*.java"/>
<exclude name="**/*TestSuite.java"/>
- <exclude name="**/image/writer/internal/JPEGImageWriter*.java" if="internal-codecs.eff.disabled"/>
<exclude name="**/image/writer/internal/TIFFImageWriter*.java" if="internal-codecs.eff.disabled"/>
</fileset>
</batchtest>
52 src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFEncodeParam.java
View
@@ -24,8 +24,6 @@
import org.apache.xmlgraphics.image.codec.util.ImageEncodeParam;
-import com.sun.image.codec.jpeg.JPEGEncodeParam;
-
/**
* An instance of <code>ImageEncodeParam</code> for encoding images in
* the TIFF format.
@@ -107,7 +105,6 @@
private TIFFField[] extraFields;
private boolean convertJPEGRGBToYCbCr = true;
- private JPEGEncodeParam jpegEncodeParam = null;
private int deflateLevel = Deflater.DEFAULT_COMPRESSION;
@@ -115,7 +112,9 @@
* Constructs a TIFFEncodeParam object with default values for
* all parameters.
*/
- public TIFFEncodeParam() {}
+ public TIFFEncodeParam() {
+ //nop
+ }
/**
* Returns the value of the compression parameter.
@@ -146,7 +145,6 @@ public void setCompression(int compression) {
switch(compression) {
case COMPRESSION_NONE:
case COMPRESSION_PACKBITS:
- case COMPRESSION_JPEG_TTN2:
case COMPRESSION_DEFLATE:
// Do nothing.
break;
@@ -189,9 +187,6 @@ public void setWriteTiled(boolean writeTiled) {
* of each strip is always the width of the image and the default
* number of rows per strip is 8.
*
- * <p> If JPEG compession is being used, the dimensions of the strips or
- * tiles may be modified to conform to the JPEG-in-TIFF specification.
- *
* @param tileWidth The tile width; ignored if strips are used.
* @param tileHeight The tile height or number of rows per strip.
*/
@@ -252,8 +247,9 @@ public synchronized Iterator getExtraImages() {
* setting is ignored if the compression type is not DEFLATE.
*/
public void setDeflateLevel(int deflateLevel) {
- if(deflateLevel < 1 && deflateLevel > 9 &&
- deflateLevel != Deflater.DEFAULT_COMPRESSION) {
+ if (deflateLevel < 1
+ && deflateLevel > 9
+ && deflateLevel != Deflater.DEFAULT_COMPRESSION) {
throw new Error("TIFFEncodeParam1");
}
@@ -284,42 +280,6 @@ public boolean getJPEGCompressRGBToYCbCr() {
}
/**
- * Sets the JPEG compression parameters. These parameters are ignored
- * if the compression type is not JPEG. The argument may be
- * <code>null</code> to indicate that default compression parameters
- * are to be used. For maximum conformance with the specification it
- * is recommended in most cases that only the quality compression
- * parameter be set.
- *
- * <p> The <code>writeTablesOnly</code> and <code>JFIFHeader</code>
- * flags of the <code>JPEGEncodeParam</code> are ignored. The
- * <code>writeImageOnly</code> flag is used to determine whether the
- * JPEGTables field will be written to the TIFF stream: if
- * <code>writeImageOnly</code> is <code>true</code>, then the JPEGTables
- * field will be written and will contain a valid JPEG abbreviated
- * table specification datastream. In this case the data in each data
- * segment (strip or tile) will contain an abbreviated JPEG image
- * datastream. If the <code>writeImageOnly</code> flag is
- * <code>false</code>, then the JPEGTables field will not be written and
- * each data segment will contain a complete JPEG interchange datastream.
- */
- public void setJPEGEncodeParam(JPEGEncodeParam jpegEncodeParam) {
- if(jpegEncodeParam != null) {
- jpegEncodeParam = (JPEGEncodeParam)jpegEncodeParam.clone();
- jpegEncodeParam.setTableInfoValid(false);
- jpegEncodeParam.setImageInfoValid(true);
- }
- this.jpegEncodeParam = jpegEncodeParam;
- }
-
- /**
- * Retrieves the JPEG compression parameters.
- */
- public JPEGEncodeParam getJPEGEncodeParam() {
- return jpegEncodeParam;
- }
-
- /**
* Sets an array of extra fields to be written to the TIFF Image File
* Directory (IFD). Fields with tags equal to the tag of any
* automatically generated fields are ignored. No error checking is
419 src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFImage.java
View
@@ -35,7 +35,6 @@
import java.awt.image.Raster;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@@ -46,10 +45,6 @@
import org.apache.xmlgraphics.image.rendered.AbstractRed;
import org.apache.xmlgraphics.image.rendered.CachableRed;
-import com.sun.image.codec.jpeg.JPEGCodec;
-import com.sun.image.codec.jpeg.JPEGDecodeParam;
-import com.sun.image.codec.jpeg.JPEGImageDecoder;
-
public class TIFFImage extends AbstractRed {
// Compression types
@@ -101,10 +96,6 @@
// LZW compression related variable
int predictor;
- // TTN2 JPEG related variables
- JPEGDecodeParam decodeParam = null;
- boolean colorConvertJPEG = false;
-
// DEFLATE variables
Inflater inflater = null;
@@ -123,46 +114,6 @@
private TIFFLZWDecoder lzwDecoder = null;
/**
- * Decode a buffer of data into a Raster with the specified location.
- *
- * @param data buffer contain an interchange or abbreviated datastream.
- * @param decodeParam decoding parameters; may be null unless the
- * data buffer contains an abbreviated datastream in which case
- * it may not be null or an error will occur.
- * @param colorConvert whether to perform color conversion; in this
- * case that would be limited to YCbCr-to-RGB.
- * @param minX the X position of the returned Raster.
- * @param minY the Y position of the returned Raster.
- */
- private static final Raster decodeJPEG(byte[] data,
- JPEGDecodeParam decodeParam,
- boolean colorConvert,
- int minX,
- int minY) {
- // Create an InputStream from the compressed data array.
- ByteArrayInputStream jpegStream = new ByteArrayInputStream(data);
-
- // Create a decoder.
- JPEGImageDecoder decoder = decodeParam == null ?
- JPEGCodec.createJPEGDecoder(jpegStream) :
- JPEGCodec.createJPEGDecoder(jpegStream,
- decodeParam);
-
- // Decode the compressed data into a Raster.
- Raster jpegRaster;
- try {
- jpegRaster = colorConvert ?
- decoder.decodeAsBufferedImage().getWritableTile(0, 0) :
- decoder.decodeAsRaster();
- } catch (IOException ioe) {
- throw new RuntimeException("TIFFImage13");
- }
-
- // Translate the decoded Raster to the specified location and return.
- return jpegRaster.createTranslatedChild(minX, minY);
- }
-
- /**
* Inflates <code>deflated</code> into <code>inflated</code> using the
* <code>Inflater</code> constructed during class instantiation.
*/
@@ -170,8 +121,8 @@ private final void inflate(byte[] deflated, byte[] inflated) {
inflater.setInput(deflated);
try {
inflater.inflate(inflated);
- } catch(DataFormatException dfe) {
- throw new RuntimeException("TIFFImage17"+": "+
+ } catch (DataFormatException dfe) {
+ throw new RuntimeException("TIFFImage17" + ": " +
dfe.getMessage());
}
inflater.reset();
@@ -180,11 +131,12 @@ private final void inflate(byte[] deflated, byte[] inflated) {
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,
- tileWidth*bands, bandOffsets);
+ tileWidth * bands, bandOffsets);
}
/**
@@ -193,13 +145,13 @@ private final void inflate(byte[] deflated, byte[] inflated) {
private final long[] getFieldAsLongs(TIFFField field) {
long[] value = null;
- if(field.getType() == TIFFField.TIFF_SHORT) {
+ if (field.getType() == TIFFField.TIFF_SHORT) {
char[] charValue = field.getAsChars();
value = new long[charValue.length];
- for(int i = 0; i < charValue.length; i++) {
- value[i] = charValue[i] & 0xffff;
+ for (int i = 0; i < charValue.length; i++) {
+ value[i] = charValue[i] & 0xffff;
}
- } else if(field.getType() == TIFFField.TIFF_LONG) {
+ } else if (field.getType() == TIFFField.TIFF_LONG) {
value = field.getAsLongs();
} else {
throw new RuntimeException();
@@ -255,7 +207,7 @@ public TIFFImage(SeekableStream stream,
TIFFField bitsField =
dir.getField(TIFFImageDecoder.TIFF_BITS_PER_SAMPLE);
char[] bitsPerSample = null;
- if(bitsField != null) {
+ if (bitsField != null) {
bitsPerSample = bitsField.getAsChars();
} else {
bitsPerSample = new char[] {1};
@@ -279,7 +231,7 @@ public TIFFImage(SeekableStream stream,
sampleFormat = sampleFormatField.getAsChars();
// Check that all the samples have the same format
- for (int l=1; l<sampleFormat.length; l++) {
+ for (int l = 1; l < sampleFormat.length; l++) {
if (sampleFormat[l] != sampleFormat[0]) {
throw new RuntimeException("TIFFImage2");
}
@@ -291,34 +243,34 @@ public TIFFImage(SeekableStream stream,
// Set the data type based on the sample size and format.
boolean isValidDataFormat = false;
- switch(sampleSize) {
+ switch (sampleSize) {
case 1:
case 4:
case 8:
- if(sampleFormat[0] != 3) {
+ if (sampleFormat[0] != 3) {
// Ignore whether signed or unsigned: treat all as unsigned.
dataType = DataBuffer.TYPE_BYTE;
isValidDataFormat = true;
}
break;
case 16:
- if(sampleFormat[0] != 3) {
+ if (sampleFormat[0] != 3) {
dataType = sampleFormat[0] == 2 ?
DataBuffer.TYPE_SHORT : DataBuffer.TYPE_USHORT;
isValidDataFormat = true;
}
break;
case 32:
- if (sampleFormat[0] == 3)
+ if (sampleFormat[0] == 3) {
isValidDataFormat = false;
- else {
+ } else {
dataType = DataBuffer.TYPE_INT;
isValidDataFormat = true;
}
break;
}
- if(!isValidDataFormat) {
+ if (!isValidDataFormat) {
throw new RuntimeException("TIFFImage3");
}
@@ -342,11 +294,11 @@ public TIFFImage(SeekableStream stream,
case 0: // WhiteIsZero
isWhiteZero = true;
case 1: // BlackIsZero
- if(sampleSize == 1 && samplesPerPixel == 1) {
+ if (sampleSize == 1 && samplesPerPixel == 1) {
imageType = TYPE_BILEVEL;
- } else if(sampleSize == 4 && samplesPerPixel == 1) {
+ } else if (sampleSize == 4 && samplesPerPixel == 1) {
imageType = TYPE_GRAY_4BIT;
- } else if(sampleSize % 8 == 0) {
+ } else if (sampleSize % 8 == 0) {
if(samplesPerPixel == 1) {
imageType = TYPE_GRAY;
} else if(samplesPerPixel == 2) {
@@ -357,10 +309,10 @@ public TIFFImage(SeekableStream stream,
}
break;
case 2: // RGB
- if(sampleSize % 8 == 0) {
- if(samplesPerPixel == 3) {
+ if (sampleSize % 8 == 0) {
+ if (samplesPerPixel == 3) {
imageType = TYPE_RGB;
- } else if(samplesPerPixel == 4) {
+ } else if (samplesPerPixel == 4) {
imageType = TYPE_RGB_ALPHA;
} else {
imageType = TYPE_GENERIC;
@@ -368,48 +320,24 @@ public TIFFImage(SeekableStream stream,
}
break;
case 3: // RGB Palette
- if(samplesPerPixel == 1 &&
+ if (samplesPerPixel == 1 &&
(sampleSize == 4 || sampleSize == 8 || sampleSize == 16)) {
imageType = TYPE_PALETTE;
}
break;
case 4: // Transparency mask
- if(sampleSize == 1 && samplesPerPixel == 1) {
+ if (sampleSize == 1 && samplesPerPixel == 1) {
imageType = TYPE_BILEVEL;
}
break;
- case 6: // YCbCr
- if(compression == COMP_JPEG_TTN2 &&
- sampleSize == 8 && samplesPerPixel == 3) {
- // Set color conversion flag.
- colorConvertJPEG = param.getJPEGDecompressYCbCrToRGB();
-
- // Set type to RGB if color converting.
- imageType = colorConvertJPEG ? TYPE_RGB : TYPE_GENERIC;
- } else {
- TIFFField chromaField = dir.getField(TIFF_YCBCR_SUBSAMPLING);
- if(chromaField != null) {
- chromaSubH = chromaField.getAsInt(0);
- chromaSubV = chromaField.getAsInt(1);
- } else {
- chromaSubH = chromaSubV = 2;
- }
-
- if(chromaSubH*chromaSubV == 1) {
- imageType = TYPE_GENERIC;
- } else if(sampleSize == 8 && samplesPerPixel == 3) {
- imageType = TYPE_YCBCR_SUB;
- }
- }
- break;
default: // Other including CMYK, CIE L*a*b*, unknown.
- if(sampleSize % 8 == 0) {
+ if (sampleSize % 8 == 0) {
imageType = TYPE_GENERIC;
}
}
// Bail out if not one of the supported types.
- if(imageType == TYPE_UNSUPPORTED) {
+ if (imageType == TYPE_UNSUPPORTED) {
throw new RuntimeException("TIFFImage4");
}
@@ -490,8 +418,8 @@ public TIFFImage(SeekableStream stream,
}
// Calculate number of tiles and the tileSize in bytes
- tilesX = (bounds.width + tileWidth - 1)/tileWidth;
- tilesY = (bounds.height + tileHeight - 1)/tileHeight;
+ tilesX = (bounds.width + tileWidth - 1) / tileWidth;
+ tilesY = (bounds.height + tileHeight - 1) / tileHeight;
tileSize = tileWidth * tileHeight * numBands;
// Check whether big endian or little endian format is used.
@@ -517,7 +445,7 @@ public TIFFImage(SeekableStream stream,
case COMP_FAX_G3_1D:
case COMP_FAX_G3_2D:
case COMP_FAX_G4_2D:
- if(sampleSize != 1) {
+ if (sampleSize != 1) {
throw new RuntimeException("TIFFImage7");
}
@@ -577,29 +505,9 @@ public TIFFImage(SeekableStream stream,
case COMP_JPEG_OLD:
throw new RuntimeException("TIFFImage15");
- case COMP_JPEG_TTN2:
- if(!(sampleSize == 8 &&
- ((imageType == TYPE_GRAY && samplesPerPixel == 1) ||
- (imageType == TYPE_PALETTE && samplesPerPixel == 1) ||
- (imageType == TYPE_RGB && samplesPerPixel == 3)))) {
- throw new RuntimeException("TIFFImage16");
- }
-
- // Create decodeParam from JPEGTables field if present.
- if(dir.isTagPresent(TIFF_JPEG_TABLES)) {
- TIFFField jpegTableField = dir.getField(TIFF_JPEG_TABLES);
- byte[] jpegTable = jpegTableField.getAsBytes();
- ByteArrayInputStream tableStream =
- new ByteArrayInputStream(jpegTable);
- JPEGImageDecoder decoder =
- JPEGCodec.createJPEGDecoder(tableStream);
- decoder.decodeAsRaster();
- decodeParam = decoder.getJPEGDecodeParam();
- }
-
- break;
default:
- throw new RuntimeException("TIFFImage10");
+ throw new RuntimeException("Compression not supported: " + compression);
+ //throw new RuntimeException("TIFFImage10");
}
ColorModel colorModel = null;
@@ -612,18 +520,20 @@ public TIFFImage(SeekableStream stream,
tileWidth,
tileHeight,
sampleSize);
- if(imageType == TYPE_BILEVEL) {
+ if (imageType == TYPE_BILEVEL) {
byte[] map = new byte[] {(byte)(isWhiteZero ? 255 : 0),
(byte)(isWhiteZero ? 0 : 255)};
colorModel = new IndexColorModel(1, 2, map, map, map);
} else {
byte [] map = new byte[16];
if (isWhiteZero) {
- for (int i=0; i<map.length; i++)
- map[i] = (byte)(255-(16*i));
+ for (int i = 0; i < map.length; i++) {
+ map[i] = (byte)(255 - (16 * i));
+ }
} else {
- for (int i=0; i<map.length; i++)
- map[i] = (byte)(16*i);
+ for (int i = 0; i < map.length; i++) {
+ map[i] = (byte)(16 * i);
+ }
}
colorModel = new IndexColorModel(4, 16, map, map, map);
}
@@ -636,22 +546,22 @@ public TIFFImage(SeekableStream stream,
// Create a pixel interleaved SampleModel with decreasing
// band offsets.
int[] reverseOffsets = new int[numBands];
- for (int i=0; i<numBands; i++) {
+ for (int i = 0; i < numBands; i++) {
reverseOffsets[i] = numBands - 1 - i;
}
sampleModel = new PixelInterleavedSampleModel
(dataType, tileWidth, tileHeight,
- numBands, numBands*tileWidth, reverseOffsets);
+ numBands, numBands * tileWidth, reverseOffsets);
- if(imageType == TYPE_GRAY) {
+ 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
@@ -659,9 +569,9 @@ public TIFFImage(SeekableStream stream,
// value of 1.0. So the extra band gets ignored, which is
// what we want.
int transparency = Transparency.OPAQUE;
- if(extraSamples == 1) { // associated (premultiplied) alpha
+ if (extraSamples == 1) { // associated (premultiplied) alpha
transparency = Transparency.TRANSLUCENT;
- } else if(extraSamples == 2) { // unassociated alpha
+ } else if (extraSamples == 2) { // unassociated alpha
transparency = Transparency.BITMASK;
}
@@ -680,7 +590,7 @@ public TIFFImage(SeekableStream stream,
// ColorModel as null, as there is no appropriate ColorModel.
int[] bandOffsets = new int[numBands];
- for (int i=0; i<numBands; i++) {
+ for (int i = 0; i < numBands; i++) {
bandOffsets[i] = i;
}
@@ -721,7 +631,7 @@ public TIFFImage(SeekableStream stream,
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 {
@@ -753,7 +663,7 @@ public TIFFImage(SeekableStream stream,
numBands);
}
- int bandLength = colormap.length/3;
+ int bandLength = colormap.length / 3;
byte[] r = new byte[bandLength];
byte[] g = new byte[bandLength];
byte[] b = new byte[bandLength];
@@ -763,24 +673,24 @@ public TIFFImage(SeekableStream stream,
if (dataType == DataBuffer.TYPE_SHORT) {
- for (int i=0; i<bandLength; i++) {
+ for (int i = 0; i < bandLength; i++) {
r[i] = param.decodeSigned16BitsTo8Bits
((short)colormap[i]);
g[i] = param.decodeSigned16BitsTo8Bits
- ((short)colormap[gIndex+i]);
+ ((short)colormap[gIndex + i]);
b[i] = param.decodeSigned16BitsTo8Bits
- ((short)colormap[bIndex+i]);
+ ((short)colormap[bIndex + i]);
}
} else {
- for (int i=0; i<bandLength; i++) {
+ for (int i = 0; i < bandLength; i++) {
r[i] = param.decode16BitsTo8Bits
(colormap[i] & 0xffff);
g[i] = param.decode16BitsTo8Bits
- (colormap[gIndex+i] & 0xffff);
+ (colormap[gIndex + i] & 0xffff);
b[i] = param.decode16BitsTo8Bits
- (colormap[bIndex+i] & 0xffff);
+ (colormap[bIndex + i] & 0xffff);
}
}
@@ -838,7 +748,7 @@ public synchronized Raster getTile(int tileX, int tileY) {
int[] idata = null;
SampleModel sampleModel = getSampleModel();
- WritableRaster tile = makeTile(tileX,tileY);
+ WritableRaster tile = makeTile(tileX, tileY);
DataBuffer buffer = tile.getDataBuffer();
@@ -862,21 +772,22 @@ public synchronized Raster getTile(int tileX, int tileY) {
long save_offset = 0;
try {
save_offset = stream.getFilePointer();
- stream.seek(tileOffsets[tileY*tilesX + tileX]);
+ stream.seek(tileOffsets[tileY * tilesX + tileX]);
} catch (IOException ioe) {
throw new RuntimeException("TIFFImage13");
}
// Number of bytes in this tile (strip) after compression.
- int byteCount = (int)tileByteCounts[tileY*tilesX + tileX];
+ int byteCount = (int)tileByteCounts[tileY * tilesX + tileX];
// Find out the number of bytes in the current tile
Rectangle newRect;
- if (!tiled)
+ if (!tiled) {
newRect = tile.getBounds();
- else
+ } else {
newRect = new Rectangle(tile.getMinX(), tile.getMinY(),
tileWidth, tileHeight);
+ }
int unitsInThisTile = newRect.width * newRect.height * numBands;
@@ -886,7 +797,7 @@ public synchronized Raster getTile(int tileX, int tileY) {
// Read the data, uncompressing as needed. There are four cases:
// bilevel, palette-RGB, 4-bit grayscale, and everything else.
- if(imageType == TYPE_BILEVEL) { // bilevel
+ if (imageType == TYPE_BILEVEL) { // bilevel
try {
if (compression == COMP_PACKBITS) {
stream.readFully(data, 0, byteCount);
@@ -895,10 +806,10 @@ public synchronized Raster getTile(int tileX, int tileY) {
// 8 pixels into 1 byte, calculate bytesInThisTile
int bytesInThisTile;
if ((newRect.width % 8) == 0) {
- bytesInThisTile = (newRect.width/8) * newRect.height;
+ bytesInThisTile = (newRect.width / 8) * newRect.height;
} else {
bytesInThisTile =
- (newRect.width/8 + 1) * newRect.height;
+ (newRect.width / 8 + 1) * newRect.height;
}
decodePackbits(data, bytesInThisTile, bdata);
} else if (compression == COMP_LZW) {
@@ -926,12 +837,12 @@ public synchronized Raster getTile(int tileX, int tileY) {
} catch (IOException ioe) {
throw new RuntimeException("TIFFImage13");
}
- } else if(imageType == TYPE_PALETTE) { // palette-RGB
+ } else if (imageType == TYPE_PALETTE) { // palette-RGB
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
@@ -984,8 +895,8 @@ public synchronized Raster getTile(int tileX, int tileY) {
// 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);
+ tempData = new short[byteCount / 2];
+ readShorts(byteCount / 2, tempData);
}
stream.seek(save_offset);
@@ -999,16 +910,16 @@ public synchronized Raster getTile(int tileX, int tileY) {
// Expand the palette image into an rgb image with ushort
// data type.
int cmapValue;
- int count = 0, lookup, len = colormap.length/3;
+ int count = 0, lookup, len = colormap.length / 3;
int len2 = len * 2;
- for (int i=0; i<unitsBeforeLookup; i++) {
+ for (int i = 0; i < unitsBeforeLookup; i++) {
// Get the index into the colormap
lookup = tempData[i] & 0xffff;
// Get the blue value
- cmapValue = colormap[lookup+len2];
+ cmapValue = colormap[lookup + len2];
sdata[count++] = (short)(cmapValue & 0xffff);
// Get the green value
- cmapValue = colormap[lookup+len];
+ cmapValue = colormap[lookup + len];
sdata[count++] = (short)(cmapValue & 0xffff);
// Get the red value
cmapValue = colormap[lookup];
@@ -1020,16 +931,16 @@ public synchronized Raster getTile(int tileX, int tileY) {
// Expand the palette image into an rgb image with
// short data type.
int cmapValue;
- int count = 0, lookup, len = colormap.length/3;
+ int count = 0, lookup, len = colormap.length / 3;
int len2 = len * 2;
- for (int i=0; i<unitsBeforeLookup; i++) {
+ for (int i = 0; i < unitsBeforeLookup; i++) {
// Get the index into the colormap
lookup = tempData[i] & 0xffff;
// Get the blue value
- cmapValue = colormap[lookup+len2];
+ cmapValue = colormap[lookup + len2];
sdata[count++] = (short)cmapValue;
// Get the green value
- cmapValue = colormap[lookup+len];
+ cmapValue = colormap[lookup + len];
sdata[count++] = (short)cmapValue;
// Get the red value
cmapValue = colormap[lookup];
@@ -1084,7 +995,7 @@ public synchronized Raster getTile(int tileX, int tileY) {
} else if (compression == COMP_NONE) {
- readShorts(byteCount/2, sdata);
+ readShorts(byteCount / 2, sdata);
}
stream.seek(save_offset);
@@ -1098,7 +1009,7 @@ public synchronized Raster getTile(int tileX, int tileY) {
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
@@ -1121,25 +1032,6 @@ public synchronized Raster getTile(int tileX, int tileY) {
tempData = new byte[unitsBeforeLookup];
lzwDecoder.decode(data, tempData, newRect.height);
- } else if (compression == COMP_JPEG_TTN2) {
-
- stream.readFully(data, 0, byteCount);
- Raster tempTile = decodeJPEG(data,
- decodeParam,
- colorConvertJPEG,
- tile.getMinX(),
- tile.getMinY());
- int[] tempPixels = new int[unitsBeforeLookup];
- tempTile.getPixels(tile.getMinX(),
- tile.getMinY(),
- tile.getWidth(),
- tile.getHeight(),
- tempPixels);
- tempData = new byte[unitsBeforeLookup];
- for(int i = 0; i < unitsBeforeLookup; i++) {
- tempData[i] = (byte)tempPixels[i];
- }
-
} else if (compression == COMP_DEFLATE) {
stream.readFully(data, 0, byteCount);
@@ -1150,6 +1042,9 @@ public synchronized Raster getTile(int tileX, int tileY) {
tempData = new byte[byteCount];
stream.readFully(tempData, 0, byteCount);
+ } else {
+ throw new RuntimeException(
+ "Compression type not supported: " + compression);
}
stream.seek(save_offset);
@@ -1161,16 +1056,16 @@ public synchronized Raster getTile(int tileX, int tileY) {
// Expand the palette image into an rgb image with ushort
// data type.
int cmapValue;
- int count = 0, lookup, len = colormap.length/3;
+ int count = 0, lookup, len = colormap.length / 3;
int len2 = len * 2;
- for (int i=0; i<unitsBeforeLookup; i++) {
+ for (int i = 0; i < unitsBeforeLookup; i++) {
// Get the index into the colormap
lookup = tempData[i] & 0xff;
// Get the blue value
- cmapValue = colormap[lookup+len2];
+ cmapValue = colormap[lookup + len2];
sdata[count++] = (short)(cmapValue & 0xffff);
// Get the green value
- cmapValue = colormap[lookup+len];
+ cmapValue = colormap[lookup + len];
sdata[count++] = (short)(cmapValue & 0xffff);
// Get the red value
cmapValue = colormap[lookup];
@@ -1193,15 +1088,6 @@ public synchronized Raster getTile(int tileX, int tileY) {
stream.readFully(data, 0, byteCount);
lzwDecoder.decode(data, bdata, newRect.height);
- } else if (compression == COMP_JPEG_TTN2) {
-
- stream.readFully(data, 0, byteCount);
- tile.setRect(decodeJPEG(data,
- decodeParam,
- colorConvertJPEG,
- tile.getMinX(),
- tile.getMinY()));
-
} else if (compression == COMP_DEFLATE) {
stream.readFully(data, 0, byteCount);
@@ -1210,6 +1096,10 @@ public synchronized Raster getTile(int tileX, int tileY) {
} else if (compression == COMP_NONE) {
stream.readFully(bdata, 0, byteCount);
+
+ } else {
+ throw new RuntimeException(
+ "Compression type not supported: " + compression);
}
stream.seek(save_offset);
@@ -1222,8 +1112,7 @@ public synchronized Raster getTile(int tileX, int tileY) {
} else if (sampleSize == 4) {
int padding = (newRect.width % 2 == 0) ? 0 : 1;
- int bytesPostDecoding = ((newRect.width/2 + padding) *
- newRect.height);
+ int bytesPostDecoding = ((newRect.width / 2 + padding) * newRect.height);
// Output short images
if (decodePaletteAsShorts) {
@@ -1264,8 +1153,8 @@ public synchronized Raster getTile(int tileX, int tileY) {
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++) {
+ for (int j = 0; j < newRect.height; j++) {
+ for (int i = 0; i < newRect.width / 2; i++) {
data[dstCount++] =
(byte)((tempData[srcCount] & 0xf0) >> 4);
data[dstCount++] =
@@ -1278,15 +1167,15 @@ public synchronized Raster getTile(int tileX, int tileY) {
}
}
- int len = colormap.length/3;
- int len2 = len*2;
+ int len = colormap.length / 3;
+ int len2 = len * 2;
int cmapValue, lookup;
int count = 0;
- for (int i=0; i<bytes; i++) {
+ for (int i = 0; i < bytes; i++) {
lookup = data[i] & 0xff;
- cmapValue = colormap[lookup+len2];
+ cmapValue = colormap[lookup + len2];
sdata[count++] = (short)(cmapValue & 0xffff);
- cmapValue = colormap[lookup+len];
+ cmapValue = colormap[lookup + len];
sdata[count++] = (short)(cmapValue & 0xffff);
cmapValue = colormap[lookup];
sdata[count++] = (short)(cmapValue & 0xffff);
@@ -1324,7 +1213,7 @@ public synchronized Raster getTile(int tileX, int tileY) {
}
}
}
- } else if(imageType == TYPE_GRAY_4BIT) { // 4-bit gray
+ } else if (imageType == TYPE_GRAY_4BIT) { // 4-bit gray
try {
if (compression == COMP_PACKBITS) {
@@ -1334,9 +1223,9 @@ public synchronized Raster getTile(int tileX, int tileY) {
// 2 pixels into 1 byte, calculate bytesInThisTile
int bytesInThisTile;
if ((newRect.width % 8) == 0) {
- bytesInThisTile = (newRect.width/2) * newRect.height;
+ bytesInThisTile = (newRect.width / 2) * newRect.height;
} else {
- bytesInThisTile = (newRect.width/2 + 1) *
+ bytesInThisTile = (newRect.width / 2 + 1) *
newRect.height;
}
@@ -1379,25 +1268,21 @@ public synchronized Raster getTile(int tileX, int tileY) {
stream.readFully(data, 0, byteCount);
decodePackbits(data, unitsInThisTile, bdata);
- } else if (compression == COMP_JPEG_TTN2) {
-
- stream.readFully(data, 0, byteCount);
- tile.setRect(decodeJPEG(data,
- decodeParam,
- colorConvertJPEG,
- tile.getMinX(),
- tile.getMinY()));
} else if (compression == COMP_DEFLATE) {
stream.readFully(data, 0, byteCount);
inflate(data, bdata);
+
+ } else {
+ throw new RuntimeException(
+ "Compression type not supported: " + compression);
}
} else if (sampleSize == 16) {
if (compression == COMP_NONE) {
- readShorts(byteCount/2, sdata);
+ readShorts(byteCount / 2, sdata);
} else if (compression == COMP_LZW) {
@@ -1441,7 +1326,7 @@ public synchronized Raster getTile(int tileX, int tileY) {
dataType == DataBuffer.TYPE_INT) { // redundant
if (compression == COMP_NONE) {
- readInts(byteCount/4, idata);
+ readInts(byteCount / 4, idata);
} else if (compression == COMP_LZW) {
@@ -1490,10 +1375,10 @@ public synchronized Raster getTile(int tileX, int tileY) {
}
// Modify the data for certain special cases.
- switch(imageType) {
+ switch (imageType) {
case TYPE_GRAY:
case TYPE_GRAY_ALPHA:
- if(isWhiteZero) {
+ if (isWhiteZero) {
// Since we are using a ComponentColorModel with this
// image, we need to change the WhiteIsZero data to
// BlackIsZero data so it will display properly.
@@ -1529,23 +1414,23 @@ public synchronized Raster getTile(int tileX, int tileY) {
// Change RGB to BGR order, as Java2D displays that faster.
// Unnecessary for JPEG-in-TIFF as the decoder handles it.
if (sampleSize == 8 && compression != COMP_JPEG_TTN2) {
- for (int i=0; i<unitsInThisTile; i+=3) {
+ for (int i = 0; i < unitsInThisTile; i += 3) {
bswap = bdata[i];
- bdata[i] = bdata[i+2];
- bdata[i+2] = bswap;
+ bdata[i] = bdata[i + 2];
+ bdata[i + 2] = bswap;
}
} else if (sampleSize == 16) {
- for (int i=0; i<unitsInThisTile; i+=3) {
+ for (int i = 0; i < unitsInThisTile; i += 3) {
sswap = sdata[i];
- sdata[i] = sdata[i+2];
- sdata[i+2] = sswap;
+ sdata[i] = sdata[i + 2];
+ sdata[i + 2] = sswap;
}
} else if (sampleSize == 32) {
- if(dataType == DataBuffer.TYPE_INT) {
- for (int i=0; i<unitsInThisTile; i+=3) {
+ if (dataType == DataBuffer.TYPE_INT) {
+ for (int i = 0; i < unitsInThisTile; i += 3) {
iswap = idata[i];
- idata[i] = idata[i+2];
- idata[i+2] = iswap;
+ idata[i] = idata[i + 2];
+ idata[i + 2] = iswap;
}
}
}
@@ -1553,41 +1438,41 @@ public synchronized Raster getTile(int tileX, int tileY) {
case TYPE_RGB_ALPHA:
// Convert from RGBA to ABGR for Java2D
if (sampleSize == 8) {
- for (int i=0; i<unitsInThisTile; i+=4) {
+ for (int i = 0; i < unitsInThisTile; i += 4) {
// Swap R and A
bswap = bdata[i];
- bdata[i] = bdata[i+3];
- bdata[i+3] = bswap;
+ bdata[i] = bdata[i + 3];
+ bdata[i + 3] = bswap;
// Swap G and B
- bswap = bdata[i+1];
- bdata[i+1] = bdata[i+2];
- bdata[i+2] = bswap;
+ bswap = bdata[i + 1];
+ bdata[i + 1] = bdata[i + 2];
+ bdata[i + 2] = bswap;
}
} else if (sampleSize == 16) {
- for (int i=0; i<unitsInThisTile; i+=4) {
+ for (int i = 0; i < unitsInThisTile; i += 4) {
// Swap R and A
sswap = sdata[i];
- sdata[i] = sdata[i+3];
- sdata[i+3] = sswap;
+ sdata[i] = sdata[i + 3];
+ sdata[i + 3] = sswap;
// Swap G and B
- sswap = sdata[i+1];
- sdata[i+1] = sdata[i+2];
- sdata[i+2] = sswap;
+ sswap = sdata[i + 1];
+ sdata[i + 1] = sdata[i + 2];
+ sdata[i + 2] = sswap;
}
} else if (sampleSize == 32) {
- if(dataType == DataBuffer.TYPE_INT) {
- for (int i=0; i<unitsInThisTile; i+=4) {
+ if (dataType == DataBuffer.TYPE_INT) {
+ for (int i = 0; i < unitsInThisTile; i += 4) {
// Swap R and A
iswap = idata[i];
- idata[i] = idata[i+3];
- idata[i+3] = iswap;
+ idata[i] = idata[i + 3];
+ idata[i + 3] = iswap;
// Swap G and B
- iswap = idata[i+1];
- idata[i+1] = idata[i+2];
- idata[i+2] = iswap;
+ iswap = idata[i + 1];
+ idata[i + 1] = idata[i + 2];
+ idata[i + 2] = iswap;
}
}
}
@@ -1595,15 +1480,15 @@ public synchronized Raster getTile(int tileX, int tileY) {
case TYPE_YCBCR_SUB:
// Post-processing for YCbCr with subsampled chrominance:
// simply replicate the chroma channels for displayability.
- int pixelsPerDataUnit = chromaSubH*chromaSubV;
+ int pixelsPerDataUnit = chromaSubH * chromaSubV;
- int numH = newRect.width/chromaSubH;
- int numV = newRect.height/chromaSubV;
+ int numH = newRect.width / chromaSubH;
+ int numV = newRect.height / chromaSubV;
- byte[] tempData = new byte[numH*numV*(pixelsPerDataUnit + 2)];
+ byte[] tempData = new byte[numH * numV * (pixelsPerDataUnit + 2)];
System.arraycopy(bdata, 0, tempData, 0, tempData.length);
- int samplesPerDataUnit = pixelsPerDataUnit*3;
+ int samplesPerDataUnit = pixelsPerDataUnit * 3;
int[] pixels = new int[samplesPerDataUnit];
int bOffset = 0;
@@ -1611,13 +1496,13 @@ public synchronized Raster getTile(int tileX, int tileY) {
int offsetCr = offsetCb + 1;
int y = newRect.y;
- for(int j = 0; j < numV; j++) {
+ for (int j = 0; j < numV; j++) {
int x = newRect.x;
- for(int i = 0; i < numH; i++) {
+ for (int i = 0; i < numH; i++) {
int Cb = tempData[bOffset + offsetCb];
int Cr = tempData[bOffset + offsetCr];
int k = 0;
- while(k < samplesPerDataUnit) {
+ while (k < samplesPerDataUnit) {
pixels[k++] = tempData[bOffset++];
pixels[k++] = Cb;
pixels[k++] = Cr;
@@ -1679,7 +1564,7 @@ private void interpretBytesAsShorts(byte[] byteArray,
if (isBigEndian) {
- for (int i=0; i<shortCount; i++) {
+ for (int i = 0; i < shortCount; i++) {
firstByte = byteArray[j++] & 0xff;
secondByte = byteArray[j++] & 0xff;
shortArray[i] = (short)((firstByte << 8) + secondByte);
@@ -1687,7 +1572,7 @@ private void interpretBytesAsShorts(byte[] byteArray,
} else {
- for (int i=0; i<shortCount; i++) {
+ for (int i = 0; i < shortCount; i++) {
firstByte = byteArray[j++] & 0xff;
secondByte = byteArray[j++] & 0xff;
shortArray[i] = (short)((secondByte << 8) + firstByte);
@@ -1705,7 +1590,7 @@ private void interpretBytesAsInts(byte[] byteArray,
if (isBigEndian) {
- for (int i=0; i<intCount; i++) {
+ for (int i = 0; i < intCount; i++) {
intArray[i] = (((byteArray[j++] & 0xff) << 24) |
((byteArray[j++] & 0xff) << 16) |
((byteArray[j++] & 0xff) << 8) |
@@ -1714,7 +1599,7 @@ private void interpretBytesAsInts(byte[] byteArray,
} else {
- for (int i=0; i<intCount; i++) {
+ for (int i = 0; i < intCount; i++) {
intArray[i] = ((byteArray[j++] & 0xff) |
((byteArray[j++] & 0xff) << 8) |
((byteArray[j++] & 0xff) << 16) |
@@ -1742,7 +1627,7 @@ private void interpretBytesAsInts(byte[] byteArray,
if (b >= 0 && b <= 127) {
// literal run packet
- for (int i=0; i<(b + 1); i++) {
+ for (int i = 0; i < (b + 1); i++) {
dst[dstCount++] = data[srcCount++];
}
@@ -1750,7 +1635,7 @@ private void interpretBytesAsInts(byte[] byteArray,
// 2 byte encoded run packet
repeat = data[srcCount++];
- for (int i=0; i<(-b + 1); i++) {
+ for (int i = 0; i < (-b + 1); i++) {
dst[dstCount++] = repeat;
}
@@ -1803,7 +1688,7 @@ private void interpretBytesAsInts(byte[] byteArray,
}
RGBBits = new int[numBands];
- for(int i = 0; i < numBands; i++) {
+ for (int i = 0; i < numBands; i++) {
RGBBits[i] = componentSize;
}
551 src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFImageEncoder.java
View
@@ -21,7 +21,6 @@
import java.awt.Rectangle;
import java.awt.color.ColorSpace;
-import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentSampleModel;
import java.awt.image.DataBuffer;
@@ -31,7 +30,6 @@
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.SampleModel;
-import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
@@ -49,9 +47,6 @@
import org.apache.xmlgraphics.image.codec.util.ImageEncoderImpl;
import org.apache.xmlgraphics.image.codec.util.SeekableOutputStream;
-import com.sun.image.codec.jpeg.JPEGEncodeParam;
-import com.sun.image.codec.jpeg.JPEGQTable;
-
/**
* A baseline TIFF writer. The writer outputs TIFF images in either Bilevel,
* Greyscale, Palette color or Full Color modes.
@@ -118,12 +113,12 @@ public void encode(RenderedImage im) throws IOException {
do {
hasNext = iter.hasNext();
ifdOffset = encode(nextImage, nextParam, ifdOffset, !hasNext);
- if(hasNext) {
+ if (hasNext) {
Object obj = iter.next();
- if(obj instanceof RenderedImage) {
+ if (obj instanceof RenderedImage) {
nextImage = (RenderedImage)obj;
nextParam = encodeParam;
- } else if(obj instanceof Object[]) {
+ } else if (obj instanceof Object[]) {
Object[] o = (Object[])obj;
nextImage = (RenderedImage)o[0];
nextParam = (TIFFEncodeParam)o[1];
@@ -198,6 +193,10 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
// Currently all images are stored uncompressed.
int compression = encodeParam.getCompression();
+ if (compression == COMP_JPEG_TTN2) {
+ throw new IllegalArgumentException("JPEG compression is not supported");
+ }
+
// Get tiled output preference.
boolean isTiled = encodeParam.getWriteTiled();
@@ -212,15 +211,15 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
// Retrieve and verify sample size.
int[] sampleSize = sampleModel.getSampleSize();
- for(int i = 1; i < sampleSize.length; i++) {
- if(sampleSize[i] != sampleSize[0]) {
+ for (int i = 1; i < sampleSize.length; i++) {
+ if (sampleSize[i] != sampleSize[0]) {
throw new Error("TIFFImageEncoder0");
}
}
// Check low bit limits.
int numBands = sampleModel.getNumBands();
- if((sampleSize[0] == 1 || sampleSize[0] == 4) && numBands != 1) {
+ if ((sampleSize[0] == 1 || sampleSize[0] == 4) && numBands != 1) {
throw new Error("TIFFImageEncoder1");
}
@@ -228,20 +227,20 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
int dataType = sampleModel.getDataType();
switch(dataType) {
case DataBuffer.TYPE_BYTE:
- if(sampleSize[0] != 1 && sampleSize[0] == 4 && // todo does this make sense??
+ 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;
case DataBuffer.TYPE_SHORT:
case DataBuffer.TYPE_USHORT:
- if(sampleSize[0] != 16) {
+ if (sampleSize[0] != 16) {
throw new Error("TIFFImageEncoder3");
}
break;
case DataBuffer.TYPE_INT:
case DataBuffer.TYPE_FLOAT:
- if(sampleSize[0] != 32) {
+ if (sampleSize[0] != 32) {
throw new Error("TIFFImageEncoder4");
}
break;
@@ -268,11 +267,11 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
int imageType = TIFF_UNSUPPORTED;
int numExtraSamples = 0;
int extraSampleType = EXTRA_SAMPLE_UNSPECIFIED;
- if(colorModel instanceof IndexColorModel) { // Bilevel or palette
+ if (colorModel instanceof IndexColorModel) { // Bilevel or palette
icm = (IndexColorModel)colorModel;
int mapSize = icm.getMapSize();
- if(sampleSize[0] == 1 && numBands == 1) { // Bilevel image
+ if (sampleSize[0] == 1 && numBands == 1) { // Bilevel image
if (mapSize != 2) {
throw new IllegalArgumentException(
@@ -308,17 +307,17 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
imageType = TIFF_PALETTE;
}
- } else if(numBands == 1) { // Non-bilevel image.
+ } else if (numBands == 1) { // Non-bilevel image.
// Palette color image.
imageType = TIFF_PALETTE;
}
- } else if(colorModel == null) {
+ } else if (colorModel == null) {
- if(sampleSize[0] == 1 && numBands == 1) { // bilevel
+ if (sampleSize[0] == 1 && numBands == 1) { // bilevel
imageType = TIFF_BILEVEL_BLACK_IS_ZERO;
} else { // generic image
imageType = TIFF_GENERIC;
- if(numBands > 1) {
+ if (numBands > 1) {
numExtraSamples = numBands - 1;
}
}
@@ -337,8 +336,8 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
imageType = TIFF_CIELAB;
break;
case ColorSpace.TYPE_RGB:
- if(compression == COMP_JPEG_TTN2 &&
- encodeParam.getJPEGCompressRGBToYCbCr()) {
+ if (compression == COMP_JPEG_TTN2
+ && encodeParam.getJPEGCompressRGBToYCbCr()) {
imageType = TIFF_YCBCR;
} else {
imageType = TIFF_RGB;
@@ -352,35 +351,23 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
break;
}
- if(imageType == TIFF_GENERIC) {
+ if (imageType == TIFF_GENERIC) {
numExtraSamples = numBands - 1;
- } else if(numBands > 1) {
+ } else if (numBands > 1) {
numExtraSamples = numBands - colorSpace.getNumComponents();
}
- if(numExtraSamples == 1 && colorModel.hasAlpha()) {
+ if (numExtraSamples == 1 && colorModel.hasAlpha()) {
extraSampleType = colorModel.isAlphaPremultiplied() ?
EXTRA_SAMPLE_ASSOCIATED_ALPHA :
EXTRA_SAMPLE_UNASSOCIATED_ALPHA;
}
}
- if(imageType == TIFF_UNSUPPORTED) {
+ if (imageType == TIFF_UNSUPPORTED) {
throw new Error("TIFFImageEncoder8");
}
- // Check JPEG compatibility.
- if(compression == COMP_JPEG_TTN2) {
- if(imageType == TIFF_PALETTE) {
- throw new Error("TIFFImageEncoder11");
- } else if(!(sampleSize[0] == 8 &&
- (imageType == TIFF_GRAY ||
- imageType == TIFF_RGB ||
- imageType == TIFF_YCBCR))) {
- throw new Error("TIFFImageEncoder9");
- }
- }
-
int photometricInterpretation = -1;
switch (imageType) {
@@ -414,7 +401,7 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
int redIndex = 0, greenIndex = sizeOfColormap;
int blueIndex = 2 * sizeOfColormap;
colormap = new char[sizeOfColormap * 3];
- for (int i=0; i<sizeOfColormap; i++) {
+ 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];
@@ -450,7 +437,7 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
// Initialize tile dimensions.
int tileWidth;
int tileHeight;
- if(isTiled) {
+ if (isTiled) {
tileWidth = encodeParam.getTileWidth() > 0 ?
encodeParam.getTileWidth() : im.getTileWidth();
tileHeight = encodeParam.getTileHeight() > 0 ?
@@ -462,51 +449,14 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
encodeParam.getTileHeight() : DEFAULT_ROWS_PER_STRIP;
}
- // Re-tile for JPEG conformance if needed.
- JPEGEncodeParam jep = null;
- if(compression == COMP_JPEG_TTN2) {
- // Get JPEGEncodeParam from encodeParam.
- jep = encodeParam.getJPEGEncodeParam();
-
- // Determine maximum subsampling.
- int maxSubH = jep.getHorizontalSubsampling(0);
- int maxSubV = jep.getVerticalSubsampling(0);
- for(int i = 1; i < numBands; i++) {
- int subH = jep.getHorizontalSubsampling(i);
- if(subH > maxSubH) {
- maxSubH = subH;
- }
- int subV = jep.getVerticalSubsampling(i);
- if(subV > maxSubV) {
- maxSubV = subV;
- }
- }
-
- int factorV = 8*maxSubV;
- tileHeight =
- (int)((float)tileHeight/(float)factorV + 0.5F)*factorV;
- if(tileHeight < factorV) {
- tileHeight = factorV;
- }
-
- if(isTiled) {
- int factorH = 8*maxSubH;
- tileWidth =
- (int)((float)tileWidth/(float)factorH + 0.5F)*factorH;
- if(tileWidth < factorH) {
- tileWidth = factorH;
- }
- }
- }
-
int numTiles;
- if(isTiled) {
+ if (isTiled) {
// NB: Parentheses are used in this statement for correct rounding.
numTiles =
- ((width + tileWidth - 1)/tileWidth) *
- ((height + tileHeight - 1)/tileHeight);
+ ((width + tileWidth - 1) / tileWidth) *
+ ((height + tileHeight - 1) / tileHeight);
} else {
- numTiles = (int)Math.ceil((double)height/(double)tileHeight);
+ numTiles = (int)Math.ceil((double)height / (double)tileHeight);
}
long[] tileByteCounts = new long[numTiles];
@@ -516,18 +466,18 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
long bytesPerTile = bytesPerRow * tileHeight;
- for (int i=0; i<numTiles; i++) {
+ for (int i = 0; i < numTiles; i++) {
tileByteCounts[i] = bytesPerTile;
}
- if(!isTiled) {
+ if (!isTiled) {
// Last strip may have lesser rows
- long lastStripRows = height - (tileHeight * (numTiles-1));
- tileByteCounts[numTiles-1] = lastStripRows * bytesPerRow;
+ long lastStripRows = height - (tileHeight * (numTiles - 1));
+ tileByteCounts[numTiles - 1] = lastStripRows * bytesPerRow;
}
long totalBytesOfData = bytesPerTile * (numTiles - 1) +
- tileByteCounts[numTiles-1];
+ tileByteCounts[numTiles - 1];
// The data will be written after the IFD: create the array here
// but fill it in later.
@@ -560,8 +510,9 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
new long[] {height}));
char [] shortSampleSize = new char[numBands];
- for (int i=0; i<numBands; i++)
+ for (int i = 0; i < numBands; i++) {
shortSampleSize[i] = (char)sampleSize[i];
+ }
fields.add(new TIFFField(TIFFImageDecoder.TIFF_BITS_PER_SAMPLE,
TIFFField.TIFF_SHORT, numBands,
shortSampleSize));
@@ -575,7 +526,7 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
TIFFField.TIFF_SHORT, 1,
new char[] {(char)photometricInterpretation}));
- if(!isTiled) {
+ if (!isTiled) {
fields.add(new TIFFField(TIFFImageDecoder.TIFF_STRIP_OFFSETS,
TIFFField.TIFF_LONG, numTiles,
tileOffsets));
@@ -585,7 +536,7 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
TIFFField.TIFF_SHORT, 1,
new char[] {(char)numBands}));
- if(!isTiled) {
+ if (!isTiled) {
fields.add(new TIFFField(TIFFImageDecoder.TIFF_ROWS_PER_STRIP,
TIFFField.TIFF_LONG, 1,
new long[] {tileHeight}));
@@ -601,7 +552,7 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
colormap));
}
- if(isTiled) {
+ if (isTiled) {
fields.add(new TIFFField(TIFFImageDecoder.TIFF_TILE_WIDTH,
TIFFField.TIFF_LONG, 1,
new long[] {tileWidth}));
@@ -619,9 +570,9 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
tileByteCounts));
}
- if(numExtraSamples > 0) {
+ if (numExtraSamples > 0) {
char[] extraSamples = new char[numExtraSamples];
- for(int i = 0; i < numExtraSamples; i++) {
+ for (int i = 0; i < numExtraSamples; i++) {
extraSamples[i] = (char)extraSampleType;
}
fields.add(new TIFFField(TIFFImageDecoder.TIFF_EXTRA_SAMPLES,
@@ -630,17 +581,17 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
}
// Data Sample Format Extension fields.
- if(dataType != DataBuffer.TYPE_BYTE) {
+ if (dataType != DataBuffer.TYPE_BYTE) {
// SampleFormat
char[] sampleFormat = new char[numBands];
- if(dataType == DataBuffer.TYPE_FLOAT) {
+ if (dataType == DataBuffer.TYPE_FLOAT) {
sampleFormat[0] = 3;
- } else if(dataType == DataBuffer.TYPE_USHORT) {
+ } else if (dataType == DataBuffer.TYPE_USHORT) {
sampleFormat[0] = 1;
} else {
sampleFormat[0] = 2;
}
- for(int b = 1; b < numBands; b++) {
+ for (int b = 1; b < numBands; b++) {
sampleFormat[b] = sampleFormat[0];
}
fields.add(new TIFFField(TIFFImageDecoder.TIFF_SAMPLE_FORMAT,
@@ -654,89 +605,12 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
// use it if available.
}
- // Initialize some JPEG variables.
- com.sun.image.codec.jpeg.JPEGEncodeParam jpegEncodeParam = null;
- com.sun.image.codec.jpeg.JPEGImageEncoder jpegEncoder = null;
- int jpegColorID = 0;
-
- if(compression == COMP_JPEG_TTN2) {
-
- // Initialize JPEG color ID.
- jpegColorID =
- com.sun.image.codec.jpeg.JPEGDecodeParam.COLOR_ID_UNKNOWN;
- switch(imageType) {
- case TIFF_GRAY:
- case TIFF_PALETTE:
- jpegColorID =
- com.sun.image.codec.jpeg.JPEGDecodeParam.COLOR_ID_GRAY;
- break;
- case TIFF_RGB:
- if (colorModel.hasAlpha()) {
- jpegColorID = com.sun.image.codec.jpeg.JPEGDecodeParam.COLOR_ID_RGBA;
- } else {
- jpegColorID = com.sun.image.codec.jpeg.JPEGDecodeParam.COLOR_ID_RGB;
- }
- break;
- case TIFF_YCBCR:
- if (colorModel.hasAlpha()) {
- jpegColorID = com.sun.image.codec.jpeg.JPEGDecodeParam.COLOR_ID_YCbCrA;
- } else {
- jpegColorID = com.sun.image.codec.jpeg.JPEGDecodeParam.COLOR_ID_YCbCr;
- }
- break;
- }
-
- // Get the JDK encoding parameters.
- Raster tile00 = im.getTile(0, 0);
- jpegEncodeParam =
- com.sun.image.codec.jpeg.JPEGCodec.getDefaultJPEGEncodeParam(
- tile00, jpegColorID);
-
- modifyEncodeParam(jep, jpegEncodeParam, numBands);
-
- // Write an abbreviated tables-only stream to JPEGTables field.
- jpegEncodeParam.setImageInfoValid(false);
- jpegEncodeParam.setTableInfoValid(true);
- ByteArrayOutputStream tableStream =
- new ByteArrayOutputStream();
- jpegEncoder =
- com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(
- tableStream,
- jpegEncodeParam);
- jpegEncoder.encode(tile00);
- byte[] tableData = tableStream.toByteArray();
- fields.add(new TIFFField(TIFF_JPEG_TABLES,
- TIFFField.TIFF_UNDEFINED,
- tableData.length,
- tableData));
-
- // Reset encoder so it's recreated below.
- jpegEncoder = null;
- }
-
- if(imageType == TIFF_YCBCR) {
+ if (imageType == TIFF_YCBCR) {
// YCbCrSubSampling: 2 is the default so we must write 1 as
// we do not (yet) do any subsampling.
char subsampleH = 1;
char subsampleV = 1;
- // If JPEG, update values.
- if(compression == COMP_JPEG_TTN2) {
- // Determine maximum subsampling.
- subsampleH = (char)jep.getHorizontalSubsampling(0);
- subsampleV = (char)jep.getVerticalSubsampling(0);
- for(int i = 1; i < numBands; i++) {
- char subH = (char)jep.getHorizontalSubsampling(i);
- if(subH > subsampleH) {
- subsampleH = subH;
- }
- char subV = (char)jep.getVerticalSubsampling(i);
- if(subV > subsampleV) {
- subsampleV = subV;
- }
- }
- }
-
fields.add(new TIFFField(TIFF_YCBCR_SUBSAMPLING,
TIFFField.TIFF_SHORT, 2,
new char[] {subsampleH, subsampleV}));
@@ -746,21 +620,13 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
fields.add(new TIFFField(TIFF_YCBCR_POSITIONING,
TIFFField.TIFF_SHORT, 1,
new char[]
- {(char)((compression == COMP_JPEG_TTN2)? 1 : 2)}));
+ {(char)((compression == COMP_JPEG_TTN2) ? 1 : 2)}));
// Reference black/white.
long[][] refbw;
- if(compression == COMP_JPEG_TTN2) {
- refbw =
- new long[][] { // no headroon/footroom
- {0, 1}, {255, 1}, {128, 1}, {255, 1}, {128, 1}, {255, 1}
- };
- } else {
- refbw =
- new long[][] { // CCIR 601.1 headroom/footroom (presumptive)
- {15, 1}, {235, 1}, {128, 1}, {240, 1}, {128, 1}, {240, 1}
- };
- }
+ refbw = new long[][] // CCIR 601.1 headroom/footroom (presumptive)
+ {{15, 1}, {235, 1}, {128, 1}, {240, 1}, {128, 1}, {240, 1}};
+
fields.add(new TIFFField(TIFF_REF_BLACK_WHITE,
TIFFField.TIFF_RATIONAL, 6,
refbw));
@@ -771,19 +637,19 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
// Add extra fields specified via the encoding parameters.
TIFFField[] extraFields = encodeParam.getExtraFields();
- if(extraFields != null) {
+ if (extraFields != null) {
List extantTags = new ArrayList(fields.size());
Iterator fieldIter = fields.iterator();
- while(fieldIter.hasNext()) {
+ while (fieldIter.hasNext()) {
TIFFField fld = (TIFFField)fieldIter.next();
extantTags.add(new Integer(fld.getTag()));
}
int numExtraFields = extraFields.length;
- for(int i = 0; i < numExtraFields; i++) {
+ for (int i = 0; i < numExtraFields; i++) {
TIFFField fld = extraFields[i];
Integer tagValue = new Integer(fld.getTag());
- if(!extantTags.contains(tagValue)) {
+ if (!extantTags.contains(tagValue)) {
fields.add(fld);
extantTags.add(tagValue);
}
@@ -801,7 +667,7 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
// following the IFD so initialize the first offset accordingly.
tileOffsets[0] = ifdOffset + dirSize;
- // Branch here depending on whether data are being comrpressed.
+ // Branch here depending on whether data are being compressed.
// If not, then the IFD is written immediately.
// If so then there are three possibilities:
// A) the OutputStream is a SeekableOutputStream (outCache null);
@@ -820,31 +686,31 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
Deflater deflater = null;
boolean jpegRGBToYCbCr = false;
- if(compression == COMP_NONE) {
+ if (compression == COMP_NONE) {
// Determine the number of bytes of padding necessary between
// the end of the IFD and the first data segment such that the
// alignment of the data conforms to the specification (required
// for uncompressed data only).
int numBytesPadding = 0;
- if(sampleSize[0] == 16 && tileOffsets[0] % 2 != 0) {
+ if (sampleSize[0] == 16 && tileOffsets[0] % 2 != 0) {
numBytesPadding = 1;
tileOffsets[0]++;
- } else if(sampleSize[0] == 32 && tileOffsets[0] % 4 != 0) {
+ } else if (sampleSize[0] == 32 && tileOffsets[0] % 4 != 0) {
numBytesPadding = (int)(4 - tileOffsets[0] % 4);
tileOffsets[0] += numBytesPadding;
}
// Update the data offsets (which TIFFField stores by reference).
for (int i = 1; i < numTiles; i++) {
- tileOffsets[i] = tileOffsets[i-1] + tileByteCounts[i-1];
+ tileOffsets[i] = tileOffsets[i - 1] + tileByteCounts[i - 1];
}
- if(!isLast) {
+ if (!isLast) {
// Determine the offset of the next IFD.
nextIFDOffset = (int)(tileOffsets[0] + totalBytesOfData);
// IFD offsets must be on a word boundary.
- if ((nextIFDOffset&0x01) != 0) {
+ if ((nextIFDOffset & 0x01) != 0) {
nextIFDOffset++;
skipByte = true;
}
@@ -855,8 +721,8 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
// Write any padding bytes needed between the end of the IFD
// and the start of the actual image data.
- if(numBytesPadding != 0) {
- for(int padding = 0; padding < numBytesPadding; padding++) {
+ if (numBytesPadding != 0) {
+ for (int padding = 0; padding < numBytesPadding; padding++) {
output.write((byte)0);
}
}
@@ -875,12 +741,11 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
// Attempt to create a temporary file.
tempFile = File.createTempFile("jai-SOS-", ".tmp");
tempFile.deleteOnExit();
- RandomAccessFile raFile =
- new RandomAccessFile(tempFile, "rw");
+ RandomAccessFile raFile = new RandomAccessFile(tempFile, "rw");
output = new SeekableOutputStream(raFile);
// this method is exited!
- } catch(Exception e) {
+ } catch (Exception e) {
// Allocate memory for the entire image data (!).
output = new ByteArrayOutputStream((int)totalBytesOfData);
}
@@ -889,19 +754,7 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
int bufSize = 0;
switch(compression) {
case COMP_PACKBITS:
- bufSize = (int)(bytesPerTile +
- ((bytesPerRow+127)/128)*tileHeight);
- break;
- case COMP_JPEG_TTN2:
- bufSize = 0;
-
- // Set color conversion flag.
- if(imageType == TIFF_YCBCR &&
- colorModel != null &&
- colorModel.getColorSpace().getType() ==
- ColorSpace.TYPE_RGB) {
- jpegRGBToYCbCr = true;
- }
+ bufSize = (int)(bytesPerTile + ((bytesPerRow + 127) / 128) * tileHeight);
break;
case COMP_DEFLATE:
bufSize = (int)bytesPerTile;
@@ -910,7 +763,7 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
default:
bufSize = 0;
}
- if(bufSize != 0) {
+ if (bufSize != 0) {
compressBuf = new byte[bufSize];
}
}
@@ -932,12 +785,12 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
// Also create a buffer to hold tileHeight lines of the
// data to be written to the file, so we can use array writes.
byte[] bpixels = null;
- if(compression != COMP_JPEG_TTN2) {
- if(dataType == DataBuffer.TYPE_BYTE) {
+ if (compression != COMP_JPEG_TTN2) {
+ if (dataType == DataBuffer.TYPE_BYTE) {
bpixels = new byte[tileHeight * tileWidth * numBands];
- } else if(dataTypeIsShort) {
+ } else if (dataTypeIsShort) {
bpixels = new byte[2 * tileHeight * tileWidth * numBands];
- } else if(dataType == DataBuffer.TYPE_INT ||
+ } else if (dataType == DataBuffer.TYPE_INT ||
dataType == DataBuffer.TYPE_FLOAT) {
bpixels = new byte[4 * tileHeight * tileWidth * numBands];
}
@@ -952,15 +805,15 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
tileHeight : Math.min(tileHeight, lastRow - row);
int size = rows * tileWidth * numBands;
- for(int col = minX; col < lastCol; col += tileWidth) {
+ for (int col = minX; col < lastCol; col += tileWidth) {
// Grab the pixels
Raster src =
im.getData(new Rectangle(col, row, tileWidth, rows));
boolean useDataBuffer = false;
- if(compression != COMP_JPEG_TTN2) { // JPEG access Raster
- if(checkContiguous) {
- if(sampleSize[0] == 8) { // 8-bit
+ if (compression != COMP_JPEG_TTN2) { // JPEG access Raster
+ if (checkContiguous) {
+ if (sampleSize[0] == 8) { // 8-bit
ComponentSampleModel csm =
(ComponentSampleModel)src.getSampleModel();
int[] bankIndices = csm.getBankIndices();
@@ -968,15 +821,15 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
int pixelStride = csm.getPixelStride();
int lineStride = csm.getScanlineStride();
- if(pixelStride != numBands ||
+ if (pixelStride != numBands ||
lineStride != bytesPerRow) {
useDataBuffer = false;
} else {
useDataBuffer = true;
- for(int i = 0;
+ for (int i = 0;
useDataBuffer && i < numBands;
i++) {
- if(bankIndices[i] != 0 ||
+ if (bankIndices[i] != 0 ||
bandOffsets[i] != i) {
useDataBuffer = false;
}
@@ -985,7 +838,7 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
} else { // 1-bit
MultiPixelPackedSampleModel mpp =
(MultiPixelPackedSampleModel)src.getSampleModel();
- if(mpp.getNumBands() == 1 &&
+ if (mpp.getNumBands() == 1 &&
mpp.getDataBitOffset() == 0 &&
mpp.getPixelBitStride() == 1) {
useDataBuffer = true;
@@ -993,8 +846,8 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
}
}
- if(!useDataBuffer) {
- if(dataType == DataBuffer.TYPE_FLOAT) {
+ if (!useDataBuffer) {
+ if (dataType == DataBuffer.TYPE_FLOAT) {
fpixels = src.getPixels(col, row, tileWidth, rows,
fpixels);
} else {
@@ -1008,11 +861,11 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
int pixel = 0;
int k = 0;
- switch(sampleSize[0]) {
+ switch (sampleSize[0]) {
case 1:
- if(useDataBuffer) {
+ if (useDataBuffer) {
byte[] btmp =
((DataBufferByte)src.getDataBuffer()).getData();
MultiPixelPackedSampleModel mpp =
@@ -1023,13 +876,13 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
src.getSampleModelTranslateX(),
row -
src.getSampleModelTranslateY());
- if(lineStride == (int)bytesPerRow) {
+ if (lineStride == (int)bytesPerRow) {
System.arraycopy(btmp, inOffset,
bpixels, 0,
- (int)bytesPerRow*rows);
+ (int)bytesPerRow * rows);
} else {
int outOffset = 0;
- for(int j = 0; j < rows; j++) {
+ for (int j = 0; j < rows; j++) {
System.arraycopy(btmp, inOffset,
bpixels, outOffset,
(int)bytesPerRow);
@@ -1041,10 +894,10 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
index = 0;
// For each of the rows in a strip
- for (int i=0; i<rows; i++) {
+ for (int i = 0; i < rows; i++) {
// Write number of pixels exactly divisible by 8
- for (int j=0; j<tileWidth/8; j++) {
+ for (int j = 0; j < tileWidth / 8; j++) {
pixel =
(pixels[index++] << 7) |
@@ -1059,9 +912,9 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
}
// Write the pixels remaining after division by 8
- if (tileWidth%8 > 0) {
+ if (tileWidth % 8 > 0) {
pixel = 0;
- for (int j=0; j<tileWidth%8; j++) {
+ for (int j = 0; j < tileWidth % 8; j++) {
pixel |= (pixels[index++] << (7 - j));
}
bpixels[k++] = (byte)pixel;
@@ -1069,16 +922,16 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
}
}
- if(compression == COMP_NONE) {
- output.write(bpixels, 0, rows * ((tileWidth+7)/8));
- } else if(compression == COMP_PACKBITS) {
+ if (compression == COMP_NONE) {
+ output.write(bpixels, 0, rows * ((tileWidth + 7) / 8));
+ } else if (compression == COMP_PACKBITS) {
int numCompressedBytes =
compressPackBits(bpixels, rows,
(int)bytesPerRow,
compressBuf);
tileByteCounts[tileNum++] = numCompressedBytes;
output.write(compressBuf, 0, numCompressedBytes);
- } else if(compression == COMP_DEFLATE) {
+ } else if (compression == COMP_DEFLATE) {
int numCompressedBytes =
deflate(deflater, bpixels, compressBuf);
tileByteCounts[tileNum++] = numCompressedBytes;
@@ -1092,11 +945,11 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
index = 0;
// For each of the rows in a strip
- for (int i=0; i<rows; i++) {
+ for (int i = 0; i < rows; i++) {
// 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;
}
@@ -1108,16 +961,16 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
}
}
- if(compression == COMP_NONE) {
- output.write(bpixels, 0, rows * ((tileWidth+1)/2));
- } else if(compression == COMP_PACKBITS) {
+ if (compression == COMP_NONE) {
+ output.write(bpixels, 0, rows * ((tileWidth + 1) / 2));
+ } else if (compression == COMP_PACKBITS) {
int numCompressedBytes =
compressPackBits(bpixels, rows,
(int)bytesPerRow,
compressBuf);
tileByteCounts[tileNum++] = numCompressedBytes;
output.write(compressBuf, 0, numCompressedBytes);
- } else if(compression == COMP_DEFLATE) {
+ } else if (compression == COMP_DEFLATE) {
int numCompressedBytes =
deflate(deflater, bpixels, compressBuf);
tileByteCounts[tileNum++] = numCompressedBytes;
@@ -1127,8 +980,8 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
case 8:
- if(compression != COMP_JPEG_TTN2) {
- if(useDataBuffer) {
+ if (compression != COMP_JPEG_TTN2) {
+ if (useDataBuffer) {
byte[] btmp =
((DataBufferByte)src.getDataBuffer()).getData();
ComponentSampleModel csm =
@@ -1139,14 +992,14 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
row -
src.getSampleModelTranslateY());
int lineStride = csm.getScanlineStride();
- if(lineStride == (int)bytesPerRow) {
+ if (lineStride == (int)bytesPerRow) {
System.arraycopy(btmp,
inOffset,
bpixels, 0,
- (int)bytesPerRow*rows);
+ (int)bytesPerRow * rows);
} else {
int outOffset = 0;
- for(int j = 0; j < rows; j++) {
+ for (int j = 0; j < rows; j++) {
System.arraycopy(btmp, inOffset,
bpixels, outOffset,
(int)bytesPerRow);
@@ -1161,62 +1014,16 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
}
}
- if(compression == COMP_NONE) {
+ if (compression == COMP_NONE) {
output.write(bpixels, 0, size);
- } else if(compression == COMP_PACKBITS) {
+ } else if (compression == COMP_PACKBITS) {
int numCompressedBytes =
compressPackBits(bpixels, rows,
(int)bytesPerRow,
compressBuf);
tileByteCounts[tileNum++] = numCompressedBytes;
output.write(compressBuf, 0, numCompressedBytes);
- } else if(compression == COMP_JPEG_TTN2) {
- long startPos = getOffset(output);
-
- // Recreate encoder and parameters if the encoder
- // is null (first data segment) or if its size
- // doesn't match the current data segment.
- if(jpegEncoder == null ||
- jpegEncodeParam.getWidth() != src.getWidth() ||
- jpegEncodeParam.getHeight() != src.getHeight()) {
-
- jpegEncodeParam =
- com.sun.image.codec.jpeg.JPEGCodec.
- getDefaultJPEGEncodeParam(src, jpegColorID);
-
- modifyEncodeParam(jep, jpegEncodeParam,
- numBands);
-
- jpegEncoder =
- com.sun.image.codec.jpeg.JPEGCodec.
- createJPEGEncoder(output, jpegEncodeParam);
- }
-
- if(jpegRGBToYCbCr) {
- WritableRaster wRas = null;
- if(src instanceof WritableRaster) {
- wRas = (WritableRaster)src;
- } else {
- wRas = src.createCompatibleWritableRaster();
- wRas.setRect(src);
- }
-
- if (wRas.getMinX() != 0 || wRas.getMinY() != 0) {
- wRas =
- wRas.createWritableTranslatedChild(0, 0);
- }
- BufferedImage bi =
- new BufferedImage(colorModel, wRas,
- false, null);
- jpegEncoder.encode(bi);
- } else {
- jpegEncoder.encode(src.createTranslatedChild(0,
- 0));
- }
-
- long endPos = getOffset(output);
- tileByteCounts[tileNum++] = (int)(endPos - startPos);
- } else if(compression == COMP_DEFLATE) {
+ } else if (compression == COMP_DEFLATE) {
int numCompressedBytes =
deflate(deflater, bpixels, compressBuf);
tileByteCounts[tileNum++] = numCompressedBytes;
@@ -1233,16 +1040,16 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
bpixels[ls++] = (byte) (value & 0x00ff);
}
- if(compression == COMP_NONE) {
+ if (compression == COMP_NONE) {
output.write(bpixels, 0, size*2);
- } else if(compression == COMP_PACKBITS) {
+ } else if (compression == COMP_PACKBITS) {
int numCompressedBytes =
compressPackBits(bpixels, rows,
(int)bytesPerRow,
compressBuf);
tileByteCounts[tileNum++] = numCompressedBytes;
output.write(compressBuf, 0, numCompressedBytes);
- } else if(compression == COMP_DEFLATE) {
+ } else if (compression == COMP_DEFLATE) {
int numCompressedBytes =
deflate(deflater, bpixels, compressBuf);
tileByteCounts[tileNum++] = numCompressedBytes;
@@ -1251,7 +1058,7 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
break;
case 32:
- if(dataType == DataBuffer.TYPE_INT) {
+ if (dataType == DataBuffer.TYPE_INT) {
int li = 0;
for (int i = 0; i < size; i++) {
int value = pixels[i];
@@ -1270,16 +1077,16 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
bpixels[lf++] = (byte)( value & 0x000000ff);
}
}
- if(compression == COMP_NONE) {
+ if (compression == COMP_NONE) {
output.write(bpixels, 0, size*4);
- } else if(compression == COMP_PACKBITS) {
+ } else if (compression == COMP_PACKBITS) {
int numCompressedBytes =
compressPackBits(bpixels, rows,
(int)bytesPerRow,
compressBuf);
tileByteCounts[tileNum++] = numCompressedBytes;
output.write(compressBuf, 0, numCompressedBytes);
- } else if(compression == COMP_DEFLATE) {
+ } else if (compression == COMP_DEFLATE) {
int numCompressedBytes =
deflate(deflater, bpixels, compressBuf);
tileByteCounts[tileNum++] = numCompressedBytes;
@@ -1291,20 +1098,20 @@ private int encode(RenderedImage im, TIFFEncodeParam encodeParam,
}
}
- if(compression == COMP_NONE) {
+ if (compression == COMP_NONE) {