javax.imageio.plugins.tiff.TIFFField Java Examples

The following examples show how to use javax.imageio.plugins.tiff.TIFFField. 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: TIFFImageReader.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private long getTileOrStripOffset(int tileIndex) throws IIOException {
    TIFFField f
            = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_TILE_OFFSETS);
    if (f == null) {
        f = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_STRIP_OFFSETS);
    }
    if (f == null) {
        f = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_JPEG_INTERCHANGE_FORMAT);
    }

    if (f == null) {
        throw new IIOException("Missing required strip or tile offsets field.");
    }

    return f.getAsLong(tileIndex);
}
 
Example #2
Source File: TIFFT4Compressor.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example #3
Source File: TIFFImageReader.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private long getTileOrStripOffset(int tileIndex) throws IIOException {
    TIFFField f
            = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_TILE_OFFSETS);
    if (f == null) {
        f = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_STRIP_OFFSETS);
    }
    if (f == null) {
        f = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_JPEG_INTERCHANGE_FORMAT);
    }

    if (f == null) {
        throw new IIOException("Missing required strip or tile offsets field.");
    }

    return f.getAsLong(tileIndex);
}
 
Example #4
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 #5
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 #6
Source File: TIFFT4Compressor.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example #7
Source File: TIFFImageMetadata.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void mergeNativeTree(Node root) throws IIOInvalidTreeException {
    Node node = root;
    if (!node.getNodeName().equals(nativeMetadataFormatName)) {
        fatal(node, "Root must be " + nativeMetadataFormatName);
    }

    node = node.getFirstChild();
    if (node == null || !node.getNodeName().equals("TIFFIFD")) {
        fatal(root, "Root must have \"TIFFIFD\" child");
    }
    TIFFIFD ifd = parseIFD(node);

    List<TIFFTagSet> rootIFDTagSets = rootIFD.getTagSetList();
    Iterator<TIFFTagSet> tagSetIter = ifd.getTagSetList().iterator();
    while(tagSetIter.hasNext()) {
        Object o = tagSetIter.next();
        if(o instanceof TIFFTagSet && !rootIFDTagSets.contains(o)) {
            rootIFD.addTagSet((TIFFTagSet)o);
        }
    }

    Iterator<TIFFField> ifdIter = ifd.iterator();
    while(ifdIter.hasNext()) {
        TIFFField field = ifdIter.next();
        rootIFD.addTIFFField(field);
    }
}
 
Example #8
Source File: TIFFImageReader.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #9
Source File: TIFFImageReader.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private long getTileOrStripByteCount(int tileIndex) throws IOException {
    TIFFField f
            = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_TILE_BYTE_COUNTS);
    if (f == null) {
        f
                = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_STRIP_BYTE_COUNTS);
    }
    if (f == null) {
        f = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
    }

    long tileOrStripByteCount;
    if (f != null) {
        tileOrStripByteCount = f.getAsLong(tileIndex);
    } else {
        processWarningOccurred("TIFF directory contains neither StripByteCounts nor TileByteCounts field: attempting to calculate from strip or tile width and height.");

        // Initialize to number of bytes per strip or tile assuming
        // no compression.
        int bitsPerPixel = bitsPerSample[0];
        for (int i = 1; i < samplesPerPixel; i++) {
            bitsPerPixel += bitsPerSample[i];
        }
        int bytesPerRow = (getTileOrStripWidth() * bitsPerPixel + 7) / 8;
        tileOrStripByteCount = bytesPerRow * getTileOrStripHeight();

        // Clamp to end of stream if possible.
        long streamLength = stream.length();
        if (streamLength != -1) {
            tileOrStripByteCount
                    = Math.min(tileOrStripByteCount,
                            streamLength - getTileOrStripOffset(tileIndex));
        } else {
            processWarningOccurred("Stream length is unknown: cannot clamp estimated strip or tile byte count to EOF.");
        }
    }

    return tileOrStripByteCount;
}
 
Example #10
Source File: TIFFImageReader.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private int getCompression() {
    TIFFField f
            = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_COMPRESSION);
    if (f == null) {
        return BaselineTIFFTagSet.COMPRESSION_NONE;
    } else {
        return f.getAsInt(0);
    }
}
 
Example #11
Source File: TIFFImageWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #12
Source File: TIFFJPEGDecompressor.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void beginDecoding() {
    // Initialize the JPEG reader if needed.
    if(this.JPEGReader == null) {
        // Get all JPEG readers.
        Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("jpeg");

        if(!iter.hasNext()) {
            throw new IllegalStateException("No JPEG readers found!");
        }

        // Initialize reader to the first one.
        this.JPEGReader = iter.next();

        this.JPEGParam = JPEGReader.getDefaultReadParam();
    }

    // Get the JPEGTables field.
    TIFFImageMetadata tmetadata = (TIFFImageMetadata)metadata;
    TIFFField f =
        tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_JPEG_TABLES);

    if (f != null) {
        this.hasJPEGTables = true;
        this.tables = f.getAsBytes();
    } else {
        this.hasJPEGTables = false;
    }
}
 
Example #13
Source File: TIFFIFD.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns an {@code Iterator} over the TIFF fields. The
 * traversal is in the order of increasing tag number.
 */
// Note: the sort is guaranteed for low fields by the use of an
// array wherein the index corresponds to the tag number and for
// the high fields by the use of a TreeMap with tag number keys.
public Iterator<TIFFField> iterator() {
    return Arrays.asList(getTIFFFields()).iterator();
}
 
Example #14
Source File: TIFFFaxDecompressor.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example #15
Source File: TIFFImageReader.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public boolean isImageTiled(int imageIndex) throws IOException {
    seekToImage(imageIndex);

    TIFFField f
            = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_TILE_WIDTH);
    return f != null;
}
 
Example #16
Source File: TIFFT6Compressor.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public int encode(byte[] b, int off,
                  int width, int height,
                  int[] bitsPerSample,
                  int scanlineStride) throws IOException {
    if (bitsPerSample.length != 1 || bitsPerSample[0] != 1) {
        throw new IIOException(
                         "Bits per sample must be 1 for T6 compression!");
    }


    if (metadata instanceof TIFFImageMetadata) {
        TIFFImageMetadata tim = (TIFFImageMetadata)metadata;

        long[] options = new long[1];
        options[0] = 0;

        BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
        TIFFField T6Options =
            new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_T6_OPTIONS),
                          TIFFTag.TIFF_LONG,
                          1,
                          options);
        tim.rootIFD.addTIFFField(T6Options);
    }

    // See comment in TIFFT4Compressor
    int maxBits = 9*((width + 1)/2) + 2;
    int bufSize = (maxBits + 7)/8;
    bufSize = height*(bufSize + 2) + 12;

    byte[] compData = new byte[bufSize];
    int bytes = encodeT6(b, scanlineStride, 8*off, width, height,
                         compData);
    stream.write(compData, 0, bytes);
    return bytes;
}
 
Example #17
Source File: TIFFFieldNode.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public TIFFFieldNode(TIFFField field) {
    super(getNodeName(field));

    isIFD = isIFD(field);

    this.field = field;

    TIFFTag tag = field.getTag();
    int tagNumber = tag.getNumber();
    String tagName = tag.getName();

    if(isIFD) {
        if(tagNumber != 0) {
            setAttribute("parentTagNumber", Integer.toString(tagNumber));
        }
        if(tagName != null) {
            setAttribute("parentTagName", tagName);
        }

        TIFFDirectory dir = field.hasDirectory() ?
            field.getDirectory() : (TIFFDirectory)field.getData();
        TIFFTagSet[] tagSets = dir.getTagSets();
        if(tagSets != null) {
            StringBuilder tagSetNames = new StringBuilder();
            for(int i = 0; i < tagSets.length; i++) {
                tagSetNames.append(tagSets[i].getClass().getName());
                if(i != tagSets.length - 1) {
                    tagSetNames.append(",");
                }
            }
            setAttribute("tagSets", tagSetNames.toString());
        }
    } else {
        setAttribute("number", Integer.toString(tagNumber));
        setAttribute("name", tagName);
    }
}
 
Example #18
Source File: TIFFImageReader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #19
Source File: TIFFImageReader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private long getTileOrStripByteCount(int tileIndex) throws IOException {
    TIFFField f
            = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_TILE_BYTE_COUNTS);
    if (f == null) {
        f
                = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_STRIP_BYTE_COUNTS);
    }
    if (f == null) {
        f = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
    }

    long tileOrStripByteCount;
    if (f != null) {
        tileOrStripByteCount = f.getAsLong(tileIndex);
    } else {
        processWarningOccurred("TIFF directory contains neither StripByteCounts nor TileByteCounts field: attempting to calculate from strip or tile width and height.");

        // Initialize to number of bytes per strip or tile assuming
        // no compression.
        int bitsPerPixel = bitsPerSample[0];
        for (int i = 1; i < samplesPerPixel; i++) {
            bitsPerPixel += bitsPerSample[i];
        }
        int bytesPerRow = (getTileOrStripWidth() * bitsPerPixel + 7) / 8;
        tileOrStripByteCount = bytesPerRow * getTileOrStripHeight();

        // Clamp to end of stream if possible.
        long streamLength = stream.length();
        if (streamLength != -1) {
            tileOrStripByteCount
                    = Math.min(tileOrStripByteCount,
                            streamLength - getTileOrStripOffset(tileIndex));
        } else {
            processWarningOccurred("Stream length is unknown: cannot clamp estimated strip or tile byte count to EOF.");
        }
    }

    return tileOrStripByteCount;
}
 
Example #20
Source File: TIFFImageReader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private int getCompression() {
    TIFFField f
            = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_COMPRESSION);
    if (f == null) {
        return BaselineTIFFTagSet.COMPRESSION_NONE;
    } else {
        return f.getAsInt(0);
    }
}
 
Example #21
Source File: TIFFImageReader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public boolean isImageTiled(int imageIndex) throws IOException {
    seekToImage(imageIndex);

    TIFFField f
            = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_TILE_WIDTH);
    return f != null;
}
 
Example #22
Source File: TIFFFaxDecompressor.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example #23
Source File: TIFFImageWriter.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #24
Source File: TIFFJPEGDecompressor.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public void beginDecoding() {
    // Initialize the JPEG reader if needed.
    if(this.JPEGReader == null) {
        // Get all JPEG readers.
        Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("jpeg");

        if(!iter.hasNext()) {
            throw new IllegalStateException("No JPEG readers found!");
        }

        // Initialize reader to the first one.
        this.JPEGReader = iter.next();

        this.JPEGParam = JPEGReader.getDefaultReadParam();
    }

    // Get the JPEGTables field.
    TIFFImageMetadata tmetadata = (TIFFImageMetadata)metadata;
    TIFFField f =
        tmetadata.getTIFFField(BaselineTIFFTagSet.TAG_JPEG_TABLES);

    if (f != null) {
        this.hasJPEGTables = true;
        this.tables = f.getAsBytes();
    } else {
        this.hasJPEGTables = false;
    }
}
 
Example #25
Source File: TIFFIFD.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an {@code Iterator} over the TIFF fields. The
 * traversal is in the order of increasing tag number.
 */
// Note: the sort is guaranteed for low fields by the use of an
// array wherein the index corresponds to the tag number and for
// the high fields by the use of a TreeMap with tag number keys.
public Iterator<TIFFField> iterator() {
    return Arrays.asList(getTIFFFields()).iterator();
}
 
Example #26
Source File: TIFFImageMetadata.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private void mergeNativeTree(Node root) throws IIOInvalidTreeException {
    Node node = root;
    if (!node.getNodeName().equals(nativeMetadataFormatName)) {
        fatal(node, "Root must be " + nativeMetadataFormatName);
    }

    node = node.getFirstChild();
    if (node == null || !node.getNodeName().equals("TIFFIFD")) {
        fatal(root, "Root must have \"TIFFIFD\" child");
    }
    TIFFIFD ifd = parseIFD(node);

    List<TIFFTagSet> rootIFDTagSets = rootIFD.getTagSetList();
    Iterator<TIFFTagSet> tagSetIter = ifd.getTagSetList().iterator();
    while(tagSetIter.hasNext()) {
        Object o = tagSetIter.next();
        if(o instanceof TIFFTagSet && !rootIFDTagSets.contains(o)) {
            rootIFD.addTagSet((TIFFTagSet)o);
        }
    }

    Iterator<TIFFField> ifdIter = ifd.iterator();
    while(ifdIter.hasNext()) {
        TIFFField field = ifdIter.next();
        rootIFD.addTIFFField(field);
    }
}
 
Example #27
Source File: TIFFFieldNode.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public TIFFFieldNode(TIFFField field) {
    super(getNodeName(field));

    isIFD = isIFD(field);

    this.field = field;

    TIFFTag tag = field.getTag();
    int tagNumber = tag.getNumber();
    String tagName = tag.getName();

    if(isIFD) {
        if(tagNumber != 0) {
            setAttribute("parentTagNumber", Integer.toString(tagNumber));
        }
        if(tagName != null) {
            setAttribute("parentTagName", tagName);
        }

        TIFFDirectory dir = field.hasDirectory() ?
            field.getDirectory() : (TIFFDirectory)field.getData();
        TIFFTagSet[] tagSets = dir.getTagSets();
        if(tagSets != null) {
            StringBuilder tagSetNames = new StringBuilder();
            for(int i = 0; i < tagSets.length; i++) {
                tagSetNames.append(tagSets[i].getClass().getName());
                if(i != tagSets.length - 1) {
                    tagSetNames.append(",");
                }
            }
            setAttribute("tagSets", tagSetNames.toString());
        }
    } else {
        setAttribute("number", Integer.toString(tagNumber));
        setAttribute("name", tagName);
    }
}
 
Example #28
Source File: TIFFT6Compressor.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public int encode(byte[] b, int off,
                  int width, int height,
                  int[] bitsPerSample,
                  int scanlineStride) throws IOException {
    if (bitsPerSample.length != 1 || bitsPerSample[0] != 1) {
        throw new IIOException(
                         "Bits per sample must be 1 for T6 compression!");
    }


    if (metadata instanceof TIFFImageMetadata) {
        TIFFImageMetadata tim = (TIFFImageMetadata)metadata;

        long[] options = new long[1];
        options[0] = 0;

        BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
        TIFFField T6Options =
            new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_T6_OPTIONS),
                          TIFFTag.TIFF_LONG,
                          1,
                          options);
        tim.rootIFD.addTIFFField(T6Options);
    }

    // See comment in TIFFT4Compressor
    int maxBits = 9*((width + 1)/2) + 2;
    int bufSize = (maxBits + 7)/8;
    bufSize = height*(bufSize + 2) + 12;

    byte[] compData = new byte[bufSize];
    int bytes = encodeT6(b, scanlineStride, 8*off, width, height,
                         compData);
    stream.write(compData, 0, bytes);
    return bytes;
}
 
Example #29
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 #30
Source File: TIFFImageWriter.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
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;
    }
}