Java Code Examples for javax.imageio.plugins.tiff.TIFFField#getCount()

The following examples show how to use javax.imageio.plugins.tiff.TIFFField#getCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TIFFYCbCrColorConverter.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public TIFFYCbCrColorConverter(TIFFImageMetadata metadata) {
    TIFFImageMetadata tmetadata = metadata;

    TIFFField f =
       tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_Y_CB_CR_COEFFICIENTS);
    if (f != null && f.getCount() == 3) {
        this.lumaRed = f.getAsFloat(0);
        this.lumaGreen = f.getAsFloat(1);
        this.lumaBlue = f.getAsFloat(2);
    }

    f =
      tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_REFERENCE_BLACK_WHITE);
    if (f != null && 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);
    }
}
 
Example 2
Source File: TIFFYCbCrColorConverter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public TIFFYCbCrColorConverter(TIFFImageMetadata metadata) {
    TIFFImageMetadata tmetadata = metadata;

    TIFFField f =
       tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_Y_CB_CR_COEFFICIENTS);
    if (f != null && f.getCount() == 3) {
        this.lumaRed = f.getAsFloat(0);
        this.lumaGreen = f.getAsFloat(1);
        this.lumaBlue = f.getAsFloat(2);
    }

    f =
      tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_REFERENCE_BLACK_WHITE);
    if (f != null && 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);
    }
}
 
Example 3
Source File: TIFFIFD.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private static void writeTIFFFieldToStream(TIFFField field,
                                           ImageOutputStream stream)
    throws IOException {
    int count = field.getCount();
    Object data = field.getData();

    switch (field.getType()) {
    case TIFFTag.TIFF_ASCII:
        for (int i = 0; i < count; i++) {
            String s = ((String[])data)[i];
            int length = s.length();
            for (int j = 0; j < length; j++) {
                stream.writeByte(s.charAt(j) & 0xff);
            }
            stream.writeByte(0);
        }
        break;
    case TIFFTag.TIFF_UNDEFINED:
    case TIFFTag.TIFF_BYTE:
    case TIFFTag.TIFF_SBYTE:
        stream.write((byte[])data);
        break;
    case TIFFTag.TIFF_SHORT:
        stream.writeChars((char[])data, 0, ((char[])data).length);
        break;
    case TIFFTag.TIFF_SSHORT:
        stream.writeShorts((short[])data, 0, ((short[])data).length);
        break;
    case TIFFTag.TIFF_SLONG:
        stream.writeInts((int[])data, 0, ((int[])data).length);
        break;
    case TIFFTag.TIFF_LONG:
        for (int i = 0; i < count; i++) {
            stream.writeInt((int)(((long[])data)[i]));
        }
        break;
    case TIFFTag.TIFF_IFD_POINTER:
        stream.writeInt(0); // will need to be backpatched
        break;
    case TIFFTag.TIFF_FLOAT:
        stream.writeFloats((float[])data, 0, ((float[])data).length);
        break;
    case TIFFTag.TIFF_DOUBLE:
        stream.writeDoubles((double[])data, 0, ((double[])data).length);
        break;
    case TIFFTag.TIFF_SRATIONAL:
        for (int i = 0; i < count; i++) {
            stream.writeInt(((int[][])data)[i][0]);
            stream.writeInt(((int[][])data)[i][1]);
        }
        break;
    case TIFFTag.TIFF_RATIONAL:
        for (int i = 0; i < count; i++) {
            long num = ((long[][])data)[i][0];
            long den = ((long[][])data)[i][1];
            stream.writeInt((int)num);
            stream.writeInt((int)den);
        }
        break;
    default:
        // error
    }
}
 
Example 4
Source File: TIFFIFD.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public void writeToStream(ImageOutputStream stream)
    throws IOException {

    int numFields = getNumTIFFFields();
    stream.writeShort(numFields);

    long nextSpace = stream.getStreamPosition() + 12*numFields + 4;

    Iterator<TIFFField> iter = iterator();
    while (iter.hasNext()) {
        TIFFField f = iter.next();

        TIFFTag tag = f.getTag();

        int type = f.getType();
        int count = f.getCount();

        // Deal with unknown tags
        if (type == 0) {
            type = TIFFTag.TIFF_UNDEFINED;
        }
        int size = count*TIFFTag.getSizeOfType(type);

        if (type == TIFFTag.TIFF_ASCII) {
            int chars = 0;
            for (int i = 0; i < count; i++) {
                chars += f.getAsString(i).length() + 1;
            }
            count = chars;
            size = count;
        }

        int tagNumber = f.getTagNumber();
        stream.writeShort(tagNumber);
        stream.writeShort(type);
        stream.writeInt(count);

        // Write a dummy value to fill space
        stream.writeInt(0);
        stream.mark(); // Mark beginning of next field
        stream.skipBytes(-4);

        long pos;

        if (size > 4 || tag.isIFDPointer()) {
            // Ensure IFD or value is written on a word boundary
            nextSpace = (nextSpace + 3) & ~0x3;

            stream.writeInt((int)nextSpace);
            stream.seek(nextSpace);
            pos = nextSpace;

            if (tag.isIFDPointer() && f.hasDirectory()) {
                TIFFIFD subIFD = getDirectoryAsIFD(f.getDirectory());
                subIFD.writeToStream(stream);
                nextSpace = subIFD.lastPosition;
            } else {
                writeTIFFFieldToStream(f, stream);
                nextSpace = stream.getStreamPosition();
            }
        } else {
            pos = stream.getStreamPosition();
            writeTIFFFieldToStream(f, stream);
        }

        // If we are writing the data for the
        // StripByteCounts, TileByteCounts, StripOffsets,
        // TileOffsets, JPEGInterchangeFormat, or
        // JPEGInterchangeFormatLength fields, record the current stream
        // position for backpatching
        if (tagNumber ==
            BaselineTIFFTagSet.TAG_STRIP_BYTE_COUNTS ||
            tagNumber == BaselineTIFFTagSet.TAG_TILE_BYTE_COUNTS ||
            tagNumber == BaselineTIFFTagSet.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH) {
            this.stripOrTileByteCountsPosition = pos;
        } else if (tagNumber ==
                   BaselineTIFFTagSet.TAG_STRIP_OFFSETS ||
                   tagNumber ==
                   BaselineTIFFTagSet.TAG_TILE_OFFSETS ||
                   tagNumber ==
                   BaselineTIFFTagSet.TAG_JPEG_INTERCHANGE_FORMAT) {
            this.stripOrTileOffsetsPosition = pos;
        }

        stream.reset(); // Go to marked position of next field
    }

    this.lastPosition = nextSpace;
}
 
Example 5
Source File: TIFFYCbCrDecompressor.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
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);
    }
}
 
Example 6
Source File: TIFFIFD.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void writeTIFFFieldToStream(TIFFField field,
                                           ImageOutputStream stream)
    throws IOException {
    int count = field.getCount();
    Object data = field.getData();

    switch (field.getType()) {
    case TIFFTag.TIFF_ASCII:
        for (int i = 0; i < count; i++) {
            String s = ((String[])data)[i];
            int length = s.length();
            for (int j = 0; j < length; j++) {
                stream.writeByte(s.charAt(j) & 0xff);
            }
            stream.writeByte(0);
        }
        break;
    case TIFFTag.TIFF_UNDEFINED:
    case TIFFTag.TIFF_BYTE:
    case TIFFTag.TIFF_SBYTE:
        stream.write((byte[])data);
        break;
    case TIFFTag.TIFF_SHORT:
        stream.writeChars((char[])data, 0, ((char[])data).length);
        break;
    case TIFFTag.TIFF_SSHORT:
        stream.writeShorts((short[])data, 0, ((short[])data).length);
        break;
    case TIFFTag.TIFF_SLONG:
        stream.writeInts((int[])data, 0, ((int[])data).length);
        break;
    case TIFFTag.TIFF_LONG:
        for (int i = 0; i < count; i++) {
            stream.writeInt((int)(((long[])data)[i]));
        }
        break;
    case TIFFTag.TIFF_IFD_POINTER:
        stream.writeInt(0); // will need to be backpatched
        break;
    case TIFFTag.TIFF_FLOAT:
        stream.writeFloats((float[])data, 0, ((float[])data).length);
        break;
    case TIFFTag.TIFF_DOUBLE:
        stream.writeDoubles((double[])data, 0, ((double[])data).length);
        break;
    case TIFFTag.TIFF_SRATIONAL:
        for (int i = 0; i < count; i++) {
            stream.writeInt(((int[][])data)[i][0]);
            stream.writeInt(((int[][])data)[i][1]);
        }
        break;
    case TIFFTag.TIFF_RATIONAL:
        for (int i = 0; i < count; i++) {
            long num = ((long[][])data)[i][0];
            long den = ((long[][])data)[i][1];
            stream.writeInt((int)num);
            stream.writeInt((int)den);
        }
        break;
    default:
        // error
    }
}
 
Example 7
Source File: TIFFIFD.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void writeToStream(ImageOutputStream stream)
    throws IOException {

    int numFields = getNumTIFFFields();
    stream.writeShort(numFields);

    long nextSpace = stream.getStreamPosition() + 12*numFields + 4;

    Iterator<TIFFField> iter = iterator();
    while (iter.hasNext()) {
        TIFFField f = iter.next();

        TIFFTag tag = f.getTag();

        int type = f.getType();
        int count = f.getCount();

        // Deal with unknown tags
        if (type == 0) {
            type = TIFFTag.TIFF_UNDEFINED;
        }
        int size = count*TIFFTag.getSizeOfType(type);

        if (type == TIFFTag.TIFF_ASCII) {
            int chars = 0;
            for (int i = 0; i < count; i++) {
                chars += f.getAsString(i).length() + 1;
            }
            count = chars;
            size = count;
        }

        int tagNumber = f.getTagNumber();
        stream.writeShort(tagNumber);
        stream.writeShort(type);
        stream.writeInt(count);

        // Write a dummy value to fill space
        stream.writeInt(0);
        stream.mark(); // Mark beginning of next field
        stream.skipBytes(-4);

        long pos;

        if (size > 4 || tag.isIFDPointer()) {
            // Ensure IFD or value is written on a word boundary
            nextSpace = (nextSpace + 3) & ~0x3;

            stream.writeInt((int)nextSpace);
            stream.seek(nextSpace);
            pos = nextSpace;

            if (tag.isIFDPointer() && f.hasDirectory()) {
                TIFFIFD subIFD = getDirectoryAsIFD(f.getDirectory());
                subIFD.writeToStream(stream);
                nextSpace = subIFD.lastPosition;
            } else {
                writeTIFFFieldToStream(f, stream);
                nextSpace = stream.getStreamPosition();
            }
        } else {
            pos = stream.getStreamPosition();
            writeTIFFFieldToStream(f, stream);
        }

        // If we are writing the data for the
        // StripByteCounts, TileByteCounts, StripOffsets,
        // TileOffsets, JPEGInterchangeFormat, or
        // JPEGInterchangeFormatLength fields, record the current stream
        // position for backpatching
        if (tagNumber ==
            BaselineTIFFTagSet.TAG_STRIP_BYTE_COUNTS ||
            tagNumber == BaselineTIFFTagSet.TAG_TILE_BYTE_COUNTS ||
            tagNumber == BaselineTIFFTagSet.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH) {
            this.stripOrTileByteCountsPosition = pos;
        } else if (tagNumber ==
                   BaselineTIFFTagSet.TAG_STRIP_OFFSETS ||
                   tagNumber ==
                   BaselineTIFFTagSet.TAG_TILE_OFFSETS ||
                   tagNumber ==
                   BaselineTIFFTagSet.TAG_JPEG_INTERCHANGE_FORMAT) {
            this.stripOrTileOffsetsPosition = pos;
        }

        stream.reset(); // Go to marked position of next field
    }

    this.lastPosition = nextSpace;
}
 
Example 8
Source File: TIFFYCbCrDecompressor.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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);
    }
}