下面列出了javax.imageio.plugins.tiff.TIFFField#getAsInt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Sets the value of the {@code metadata} field.
*
* <p> The implementation in this class also sets local options
* from the T4_OPTIONS field if it exists, and if it doesn't, adds
* it with default values.</p>
*
* @param metadata the {@code IIOMetadata} object for the
* image being written.
*
* @see #getMetadata()
*/
public void setMetadata(IIOMetadata metadata) {
super.setMetadata(metadata);
if (metadata instanceof TIFFImageMetadata) {
TIFFImageMetadata tim = (TIFFImageMetadata)metadata;
TIFFField f = tim.getTIFFField(BaselineTIFFTagSet.TAG_T4_OPTIONS);
if (f != null) {
int options = f.getAsInt(0);
is1DMode = (options & 0x1) == 0;
isEOLAligned = (options & 0x4) == 0x4;
} else {
long[] oarray = new long[1];
oarray[0] = (isEOLAligned ? 0x4 : 0x0) |
(is1DMode ? 0x0 : 0x1);
BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
TIFFField T4Options =
new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_T4_OPTIONS),
TIFFTag.TIFF_LONG,
1,
oarray);
tim.rootIFD.addTIFFField(T4Options);
}
}
}
/**
* Sets the value of the {@code metadata} field.
*
* <p> The implementation in this class also sets local options
* from the T4_OPTIONS field if it exists, and if it doesn't, adds
* it with default values.</p>
*
* @param metadata the {@code IIOMetadata} object for the
* image being written.
*
* @see #getMetadata()
*/
public void setMetadata(IIOMetadata metadata) {
super.setMetadata(metadata);
if (metadata instanceof TIFFImageMetadata) {
TIFFImageMetadata tim = (TIFFImageMetadata)metadata;
TIFFField f = tim.getTIFFField(BaselineTIFFTagSet.TAG_T4_OPTIONS);
if (f != null) {
int options = f.getAsInt(0);
is1DMode = (options & 0x1) == 0;
isEOLAligned = (options & 0x4) == 0x4;
} else {
long[] oarray = new long[1];
oarray[0] = (isEOLAligned ? 0x4 : 0x0) |
(is1DMode ? 0x0 : 0x1);
BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
TIFFField T4Options =
new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_T4_OPTIONS),
TIFFTag.TIFF_LONG,
1,
oarray);
tim.rootIFD.addTIFFField(T4Options);
}
}
}
public boolean canReplacePixels(int imageIndex) throws IOException {
if (getOutput() == null) {
throw new IllegalStateException("getOutput() == null!");
}
TIFFIFD rootIFD = readIFD(imageIndex);
TIFFField f = rootIFD.getTIFFField(BaselineTIFFTagSet.TAG_COMPRESSION);
int compression = f.getAsInt(0);
return compression == BaselineTIFFTagSet.COMPRESSION_NONE;
}
/**
* Invokes the superclass method and then sets instance variables on
* the basis of the metadata set on this decompressor.
*/
public void beginDecoding() {
super.beginDecoding();
if(metadata instanceof TIFFImageMetadata) {
TIFFImageMetadata tmetadata = (TIFFImageMetadata)metadata;
TIFFField f;
f = tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_FILL_ORDER);
this.fillOrder = f == null ?
BaselineTIFFTagSet.FILL_ORDER_LEFT_TO_RIGHT : f.getAsInt(0);
f = tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_COMPRESSION);
this.compression = f == null ?
BaselineTIFFTagSet.COMPRESSION_CCITT_RLE : f.getAsInt(0);
f = tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_T4_OPTIONS);
this.t4Options = f == null ? 0 : f.getAsInt(0);
this.oneD = (t4Options & 0x01);
// uncompressedMode - haven't dealt with this yet.
this.uncompressedMode = ((t4Options & 0x02) >> 1);
this.fillBits = ((t4Options & 0x04) >> 2);
f = tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_T6_OPTIONS);
this.t6Options = f == null ? 0 : f.getAsInt(0);
} else {
this.fillOrder = BaselineTIFFTagSet.FILL_ORDER_LEFT_TO_RIGHT;
this.compression = BaselineTIFFTagSet.COMPRESSION_CCITT_RLE; // RLE
this.t4Options = 0; // Irrelevant as applies to T.4 only
this.oneD = 0; // One-dimensional
this.uncompressedMode = 0; // Not uncompressed mode
this.fillBits = 0; // No fill bits
this.t6Options = 0;
}
}
private int getTileOrStripHeight() {
TIFFField f
= imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_TILE_LENGTH);
if (f != null) {
return f.getAsInt(0);
}
f = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_ROWS_PER_STRIP);
// Default for ROWS_PER_STRIP is 2^32 - 1, i.e., infinity
int h = (f == null) ? -1 : f.getAsInt(0);
return (h == -1) ? getHeight() : h;
}
private int getCompression() {
TIFFField f
= imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_COMPRESSION);
if (f == null) {
return BaselineTIFFTagSet.COMPRESSION_NONE;
} else {
return f.getAsInt(0);
}
}
public boolean canReplacePixels(int imageIndex) throws IOException {
if (getOutput() == null) {
throw new IllegalStateException("getOutput() == null!");
}
TIFFIFD rootIFD = readIFD(imageIndex);
TIFFField f = rootIFD.getTIFFField(BaselineTIFFTagSet.TAG_COMPRESSION);
int compression = f.getAsInt(0);
return compression == BaselineTIFFTagSet.COMPRESSION_NONE;
}
/**
* Invokes the superclass method and then sets instance variables on
* the basis of the metadata set on this decompressor.
*/
public void beginDecoding() {
super.beginDecoding();
if(metadata instanceof TIFFImageMetadata) {
TIFFImageMetadata tmetadata = (TIFFImageMetadata)metadata;
TIFFField f;
f = tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_FILL_ORDER);
this.fillOrder = f == null ?
BaselineTIFFTagSet.FILL_ORDER_LEFT_TO_RIGHT : f.getAsInt(0);
f = tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_COMPRESSION);
this.compression = f == null ?
BaselineTIFFTagSet.COMPRESSION_CCITT_RLE : f.getAsInt(0);
f = tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_T4_OPTIONS);
this.t4Options = f == null ? 0 : f.getAsInt(0);
this.oneD = (t4Options & 0x01);
// uncompressedMode - haven't dealt with this yet.
this.uncompressedMode = ((t4Options & 0x02) >> 1);
this.fillBits = ((t4Options & 0x04) >> 2);
f = tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_T6_OPTIONS);
this.t6Options = f == null ? 0 : f.getAsInt(0);
} else {
this.fillOrder = BaselineTIFFTagSet.FILL_ORDER_LEFT_TO_RIGHT;
this.compression = BaselineTIFFTagSet.COMPRESSION_CCITT_RLE; // RLE
this.t4Options = 0; // Irrelevant as applies to T.4 only
this.oneD = 0; // One-dimensional
this.uncompressedMode = 0; // Not uncompressed mode
this.fillBits = 0; // No fill bits
this.t6Options = 0;
}
}
private int getTileOrStripHeight() {
TIFFField f
= imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_TILE_LENGTH);
if (f != null) {
return f.getAsInt(0);
}
f = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_ROWS_PER_STRIP);
// Default for ROWS_PER_STRIP is 2^32 - 1, i.e., infinity
int h = (f == null) ? -1 : f.getAsInt(0);
return (h == -1) ? getHeight() : h;
}
private int getCompression() {
TIFFField f
= imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_COMPRESSION);
if (f == null) {
return BaselineTIFFTagSet.COMPRESSION_NONE;
} else {
return f.getAsInt(0);
}
}
public void prepareReplacePixels(int imageIndex,
Rectangle region) throws IOException {
synchronized(replacePixelsLock) {
// Check state and parameters vis-a-vis ImageWriter specification.
if (stream == null) {
throw new IllegalStateException("Output not set!");
}
if (region == null) {
throw new IllegalArgumentException("region == null!");
}
if (region.getWidth() < 1) {
throw new IllegalArgumentException("region.getWidth() < 1!");
}
if (region.getHeight() < 1) {
throw new IllegalArgumentException("region.getHeight() < 1!");
}
if (inReplacePixelsNest) {
throw new IllegalStateException
("In nested call to prepareReplacePixels!");
}
// Read the IFD for the pixel replacement index.
TIFFIFD replacePixelsIFD = readIFD(imageIndex);
// Ensure that compression is "none".
TIFFField f =
replacePixelsIFD.getTIFFField(BaselineTIFFTagSet.TAG_COMPRESSION);
int compression = f.getAsInt(0);
if (compression != BaselineTIFFTagSet.COMPRESSION_NONE) {
throw new UnsupportedOperationException
("canReplacePixels(imageIndex) == false!");
}
// Get the image dimensions.
f =
replacePixelsIFD.getTIFFField(BaselineTIFFTagSet.TAG_IMAGE_WIDTH);
if(f == null) {
throw new IIOException("Cannot read ImageWidth field.");
}
int w = f.getAsInt(0);
f =
replacePixelsIFD.getTIFFField(BaselineTIFFTagSet.TAG_IMAGE_LENGTH);
if(f == null) {
throw new IIOException("Cannot read ImageHeight field.");
}
int h = f.getAsInt(0);
// Create image bounds.
Rectangle bounds = new Rectangle(0, 0, w, h);
// Intersect region with bounds.
region = region.intersection(bounds);
// Check for empty intersection.
if(region.isEmpty()) {
throw new IIOException("Region does not intersect image bounds");
}
// Save the region.
replacePixelsRegion = region;
// Get the tile offsets.
f = replacePixelsIFD.getTIFFField(BaselineTIFFTagSet.TAG_TILE_OFFSETS);
if(f == null) {
f = replacePixelsIFD.getTIFFField(BaselineTIFFTagSet.TAG_STRIP_OFFSETS);
}
replacePixelsTileOffsets = f.getAsLongs();
// Get the byte counts.
f = replacePixelsIFD.getTIFFField(BaselineTIFFTagSet.TAG_TILE_BYTE_COUNTS);
if(f == null) {
f = replacePixelsIFD.getTIFFField(BaselineTIFFTagSet.TAG_STRIP_BYTE_COUNTS);
}
replacePixelsByteCounts = f.getAsLongs();
replacePixelsOffsetsPosition =
replacePixelsIFD.getStripOrTileOffsetsPosition();
replacePixelsByteCountsPosition =
replacePixelsIFD.getStripOrTileByteCountsPosition();
// Get the image metadata.
replacePixelsMetadata = new TIFFImageMetadata(replacePixelsIFD);
// Save the image index.
replacePixelsIndex = imageIndex;
// Set the pixel replacement flag.
inReplacePixelsNest = true;
}
}
private int getFieldAsInt(int tagNumber) {
TIFFField f = getTIFFField(tagNumber);
return f == null ? -1 : f.getAsInt(0);
}
public void beginDecoding() {
if(decompressor != null) {
decompressor.beginDecoding();
}
TIFFImageMetadata tmetadata = (TIFFImageMetadata)metadata;
TIFFField f;
f = tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_Y_CB_CR_SUBSAMPLING);
if (f != null) {
if (f.getCount() == 2) {
this.chromaSubsampleH = f.getAsInt(0);
this.chromaSubsampleV = f.getAsInt(1);
if (chromaSubsampleH != 1 && chromaSubsampleH != 2 &&
chromaSubsampleH != 4) {
warning("Y_CB_CR_SUBSAMPLING[0] has illegal value " +
chromaSubsampleH +
" (should be 1, 2, or 4), setting to 1");
chromaSubsampleH = 1;
}
if (chromaSubsampleV != 1 && chromaSubsampleV != 2 &&
chromaSubsampleV != 4) {
warning("Y_CB_CR_SUBSAMPLING[1] has illegal value " +
chromaSubsampleV +
" (should be 1, 2, or 4), setting to 1");
chromaSubsampleV = 1;
}
} else {
warning("Y_CB_CR_SUBSAMPLING count != 2, " +
"assuming no subsampling");
}
}
f =
tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_Y_CB_CR_COEFFICIENTS);
if (f != null) {
if (f.getCount() == 3) {
this.lumaRed = f.getAsFloat(0);
this.lumaGreen = f.getAsFloat(1);
this.lumaBlue = f.getAsFloat(2);
} else {
warning("Y_CB_CR_COEFFICIENTS count != 3, " +
"assuming default values for CCIR 601-1");
}
}
f =
tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_REFERENCE_BLACK_WHITE);
if (f != null) {
if (f.getCount() == 6) {
this.referenceBlackY = f.getAsFloat(0);
this.referenceWhiteY = f.getAsFloat(1);
this.referenceBlackCb = f.getAsFloat(2);
this.referenceWhiteCb = f.getAsFloat(3);
this.referenceBlackCr = f.getAsFloat(4);
this.referenceWhiteCr = f.getAsFloat(5);
} else {
warning("REFERENCE_BLACK_WHITE count != 6, ignoring it");
}
} else {
warning("REFERENCE_BLACK_WHITE not found, assuming 0-255/128-255/128-255");
}
this.colorConvert = true;
float BCb = (2.0f - 2.0f*lumaBlue);
float RCr = (2.0f - 2.0f*lumaRed);
float GY = (1.0f - lumaBlue - lumaRed)/lumaGreen;
float GCb = 2.0f*lumaBlue*(lumaBlue - 1.0f)/lumaGreen;
float GCr = 2.0f*lumaRed*(lumaRed - 1.0f)/lumaGreen;
for (int i = 0; i < 256; i++) {
float fY = (i - referenceBlackY)*codingRangeY/
(referenceWhiteY - referenceBlackY);
float fCb = (i - referenceBlackCb)*127.0f/
(referenceWhiteCb - referenceBlackCb);
float fCr = (i - referenceBlackCr)*127.0f/
(referenceWhiteCr - referenceBlackCr);
iYTab[i] = (int)(fY*FRAC_SCALE);
iCbTab[i] = (int)(fCb*BCb*FRAC_SCALE);
iCrTab[i] = (int)(fCr*RCr*FRAC_SCALE);
iGYTab[i] = (int)(fY*GY*FRAC_SCALE);
iGCbTab[i] = (int)(fCb*GCb*FRAC_SCALE);
iGCrTab[i] = (int)(fCr*GCr*FRAC_SCALE);
}
}
private int getTileOrStripWidth() {
TIFFField f
= imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_TILE_WIDTH);
return (f == null) ? getWidth() : f.getAsInt(0);
}
public void prepareReplacePixels(int imageIndex,
Rectangle region) throws IOException {
synchronized(replacePixelsLock) {
// Check state and parameters vis-a-vis ImageWriter specification.
if (stream == null) {
throw new IllegalStateException("Output not set!");
}
if (region == null) {
throw new IllegalArgumentException("region == null!");
}
if (region.getWidth() < 1) {
throw new IllegalArgumentException("region.getWidth() < 1!");
}
if (region.getHeight() < 1) {
throw new IllegalArgumentException("region.getHeight() < 1!");
}
if (inReplacePixelsNest) {
throw new IllegalStateException
("In nested call to prepareReplacePixels!");
}
// Read the IFD for the pixel replacement index.
TIFFIFD replacePixelsIFD = readIFD(imageIndex);
// Ensure that compression is "none".
TIFFField f =
replacePixelsIFD.getTIFFField(BaselineTIFFTagSet.TAG_COMPRESSION);
int compression = f.getAsInt(0);
if (compression != BaselineTIFFTagSet.COMPRESSION_NONE) {
throw new UnsupportedOperationException
("canReplacePixels(imageIndex) == false!");
}
// Get the image dimensions.
f =
replacePixelsIFD.getTIFFField(BaselineTIFFTagSet.TAG_IMAGE_WIDTH);
if(f == null) {
throw new IIOException("Cannot read ImageWidth field.");
}
int w = f.getAsInt(0);
f =
replacePixelsIFD.getTIFFField(BaselineTIFFTagSet.TAG_IMAGE_LENGTH);
if(f == null) {
throw new IIOException("Cannot read ImageHeight field.");
}
int h = f.getAsInt(0);
// Create image bounds.
Rectangle bounds = new Rectangle(0, 0, w, h);
// Intersect region with bounds.
region = region.intersection(bounds);
// Check for empty intersection.
if(region.isEmpty()) {
throw new IIOException("Region does not intersect image bounds");
}
// Save the region.
replacePixelsRegion = region;
// Get the tile offsets.
f = replacePixelsIFD.getTIFFField(BaselineTIFFTagSet.TAG_TILE_OFFSETS);
if(f == null) {
f = replacePixelsIFD.getTIFFField(BaselineTIFFTagSet.TAG_STRIP_OFFSETS);
}
replacePixelsTileOffsets = f.getAsLongs();
// Get the byte counts.
f = replacePixelsIFD.getTIFFField(BaselineTIFFTagSet.TAG_TILE_BYTE_COUNTS);
if(f == null) {
f = replacePixelsIFD.getTIFFField(BaselineTIFFTagSet.TAG_STRIP_BYTE_COUNTS);
}
replacePixelsByteCounts = f.getAsLongs();
replacePixelsOffsetsPosition =
replacePixelsIFD.getStripOrTileOffsetsPosition();
replacePixelsByteCountsPosition =
replacePixelsIFD.getStripOrTileByteCountsPosition();
// Get the image metadata.
replacePixelsMetadata = new TIFFImageMetadata(replacePixelsIFD);
// Save the image index.
replacePixelsIndex = imageIndex;
// Set the pixel replacement flag.
inReplacePixelsNest = true;
}
}
private int getFieldAsInt(int tagNumber) {
TIFFField f = getTIFFField(tagNumber);
return f == null ? -1 : f.getAsInt(0);
}
public void beginDecoding() {
if(decompressor != null) {
decompressor.beginDecoding();
}
TIFFImageMetadata tmetadata = (TIFFImageMetadata)metadata;
TIFFField f;
f = tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_Y_CB_CR_SUBSAMPLING);
if (f != null) {
if (f.getCount() == 2) {
this.chromaSubsampleH = f.getAsInt(0);
this.chromaSubsampleV = f.getAsInt(1);
if (chromaSubsampleH != 1 && chromaSubsampleH != 2 &&
chromaSubsampleH != 4) {
warning("Y_CB_CR_SUBSAMPLING[0] has illegal value " +
chromaSubsampleH +
" (should be 1, 2, or 4), setting to 1");
chromaSubsampleH = 1;
}
if (chromaSubsampleV != 1 && chromaSubsampleV != 2 &&
chromaSubsampleV != 4) {
warning("Y_CB_CR_SUBSAMPLING[1] has illegal value " +
chromaSubsampleV +
" (should be 1, 2, or 4), setting to 1");
chromaSubsampleV = 1;
}
} else {
warning("Y_CB_CR_SUBSAMPLING count != 2, " +
"assuming no subsampling");
}
}
f =
tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_Y_CB_CR_COEFFICIENTS);
if (f != null) {
if (f.getCount() == 3) {
this.lumaRed = f.getAsFloat(0);
this.lumaGreen = f.getAsFloat(1);
this.lumaBlue = f.getAsFloat(2);
} else {
warning("Y_CB_CR_COEFFICIENTS count != 3, " +
"assuming default values for CCIR 601-1");
}
}
f =
tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_REFERENCE_BLACK_WHITE);
if (f != null) {
if (f.getCount() == 6) {
this.referenceBlackY = f.getAsFloat(0);
this.referenceWhiteY = f.getAsFloat(1);
this.referenceBlackCb = f.getAsFloat(2);
this.referenceWhiteCb = f.getAsFloat(3);
this.referenceBlackCr = f.getAsFloat(4);
this.referenceWhiteCr = f.getAsFloat(5);
} else {
warning("REFERENCE_BLACK_WHITE count != 6, ignoring it");
}
} else {
warning("REFERENCE_BLACK_WHITE not found, assuming 0-255/128-255/128-255");
}
this.colorConvert = true;
float BCb = (2.0f - 2.0f*lumaBlue);
float RCr = (2.0f - 2.0f*lumaRed);
float GY = (1.0f - lumaBlue - lumaRed)/lumaGreen;
float GCb = 2.0f*lumaBlue*(lumaBlue - 1.0f)/lumaGreen;
float GCr = 2.0f*lumaRed*(lumaRed - 1.0f)/lumaGreen;
for (int i = 0; i < 256; i++) {
float fY = (i - referenceBlackY)*codingRangeY/
(referenceWhiteY - referenceBlackY);
float fCb = (i - referenceBlackCb)*127.0f/
(referenceWhiteCb - referenceBlackCb);
float fCr = (i - referenceBlackCr)*127.0f/
(referenceWhiteCr - referenceBlackCr);
iYTab[i] = (int)(fY*FRAC_SCALE);
iCbTab[i] = (int)(fCb*BCb*FRAC_SCALE);
iCrTab[i] = (int)(fCr*RCr*FRAC_SCALE);
iGYTab[i] = (int)(fY*GY*FRAC_SCALE);
iGCbTab[i] = (int)(fCb*GCb*FRAC_SCALE);
iGCrTab[i] = (int)(fCr*GCr*FRAC_SCALE);
}
}
private int getTileOrStripWidth() {
TIFFField f
= imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_TILE_WIDTH);
return (f == null) ? getWidth() : f.getAsInt(0);
}
/**
* Sets the value of the {@code metadata} field.
*
* <p> The implementation in this class also sets local options
* from the FILL_ORDER field if it exists.</p>
*
* @param metadata the {@code IIOMetadata} object for the
* image being written.
*
* @see #getMetadata()
*/
public void setMetadata(IIOMetadata metadata) {
super.setMetadata(metadata);
if (metadata instanceof TIFFImageMetadata) {
TIFFImageMetadata tim = (TIFFImageMetadata)metadata;
TIFFField f = tim.getTIFFField(BaselineTIFFTagSet.TAG_FILL_ORDER);
inverseFill = (f != null && f.getAsInt(0) == 2);
}
}
/**
* Sets the value of the {@code metadata} field.
*
* <p> The implementation in this class also sets local options
* from the FILL_ORDER field if it exists.</p>
*
* @param metadata the {@code IIOMetadata} object for the
* image being written.
*
* @see #getMetadata()
*/
public void setMetadata(IIOMetadata metadata) {
super.setMetadata(metadata);
if (metadata instanceof TIFFImageMetadata) {
TIFFImageMetadata tim = (TIFFImageMetadata)metadata;
TIFFField f = tim.getTIFFField(BaselineTIFFTagSet.TAG_FILL_ORDER);
inverseFill = (f != null && f.getAsInt(0) == 2);
}
}