javax.imageio.plugins.tiff.TIFFTag Java Examples

The following examples show how to use javax.imageio.plugins.tiff.TIFFTag. 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: 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 #2
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 #3
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 #4
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 #5
Source File: TIFFIFD.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static TIFFTag getTag(String tagName, List<TIFFTagSet> tagSets) {
    Iterator<TIFFTagSet> iter = tagSets.iterator();
    while (iter.hasNext()) {
        TIFFTagSet tagSet = iter.next();
        TIFFTag tag = tagSet.getTag(tagName);
        if (tag != null) {
            return tag;
        }
    }

    return null;
}
 
Example #6
Source File: TIFFIFD.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static TIFFTag getTag(int tagNumber, List<TIFFTagSet> tagSets) {
    Iterator<TIFFTagSet> iter = tagSets.iterator();
    while (iter.hasNext()) {
        TIFFTagSet tagSet = iter.next();
        TIFFTag tag = tagSet.getTag(tagNumber);
        if (tag != null) {
            return tag;
        }
    }

    return null;
}
 
Example #7
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 #8
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 #9
Source File: TIFFIFD.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public static TIFFTag getTag(String tagName, List<TIFFTagSet> tagSets) {
    Iterator<TIFFTagSet> iter = tagSets.iterator();
    while (iter.hasNext()) {
        TIFFTagSet tagSet = iter.next();
        TIFFTag tag = tagSet.getTag(tagName);
        if (tag != null) {
            return tag;
        }
    }

    return null;
}
 
Example #10
Source File: TIFFIFD.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public static TIFFTag getTag(int tagNumber, List<TIFFTagSet> tagSets) {
    Iterator<TIFFTagSet> iter = tagSets.iterator();
    while (iter.hasNext()) {
        TIFFTagSet tagSet = iter.next();
        TIFFTag tag = tagSet.getTag(tagNumber);
        if (tag != null) {
            return tag;
        }
    }

    return null;
}
 
Example #11
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 #12
Source File: TIFFFieldNode.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private static boolean isIFD(TIFFField f) {
    int type = f.getType();
    return f.hasDirectory() &&
        (type == TIFFTag.TIFF_LONG || type == TIFFTag.TIFF_IFD_POINTER);
}
 
Example #13
Source File: TIFFIFD.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
TIFFIFDEntry(TIFFTag tag, int type, int count, long offset) {
    this.tag = tag;
    this.type = type;
    this.count = count;
    this.offset = offset;
}
 
Example #14
Source File: TIFFIFD.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public TIFFIFD(List<TIFFTagSet> tagSets, TIFFTag parentTag) {
    super(tagSets.toArray(new TIFFTagSet[tagSets.size()]),
          parentTag);
}
 
Example #15
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 #16
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 #17
Source File: TIFFIFD.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public TIFFIFD(List<TIFFTagSet> tagSets, TIFFTag parentTag) {
    super(tagSets.toArray(new TIFFTagSet[tagSets.size()]),
          parentTag);
}
 
Example #18
Source File: TIFFIFD.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
TIFFIFDEntry(TIFFTag tag, int type, int count, long offset) {
    this.tag = tag;
    this.type = type;
    this.count = count;
    this.offset = offset;
}
 
Example #19
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 #20
Source File: TIFFFieldNode.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isIFD(TIFFField f) {
    int type = f.getType();
    return f.hasDirectory() &&
        (type == TIFFTag.TIFF_LONG || type == TIFFTag.TIFF_IFD_POINTER);
}
 
Example #21
Source File: ImageTiffFile.java    From MyBox with Apache License 2.0 4 votes vote down vote up
public static IIOMetadata getWriterMeta(ImageAttributes attributes, BufferedImage image,
        ImageWriter writer, ImageWriteParam param) {
    try {
        IIOMetadata metaData;
        if (attributes != null && attributes.getColorType() != null) {
            ImageTypeSpecifier imageTypeSpecifier;
            switch (attributes.getColorType()) {
                case ARGB:
                    imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB);
                    break;
                case RGB:
                    imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
                    break;
                case BINARY:
                    imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_BYTE_BINARY);
                    break;
                case GRAY:
                    imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_BYTE_GRAY);
                    break;
                default:
                    imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
                    break;
            }
            metaData = writer.getDefaultImageMetadata(imageTypeSpecifier, param);
        } else if (image != null) {
            metaData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(image), param);
        } else {
            metaData = writer.getDefaultImageMetadata(
                    ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB), param);
        }

        if (metaData == null || metaData.isReadOnly() || attributes == null) {
            return metaData;
        }
        if (attributes.getDensity() > 0) {
            String nativeName = metaData.getNativeMetadataFormatName();  // javax_imageio_tiff_image_1.0
            Node nativeTree = metaData.getAsTree(nativeName);
            long[] xRes = new long[]{attributes.getDensity(), 1};
            long[] yRes = new long[]{attributes.getDensity(), 1};
            TIFFField fieldXRes = new TIFFField(
                    BaselineTIFFTagSet.getInstance().getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION),
                    TIFFTag.TIFF_RATIONAL, 1, new long[][]{xRes});
            TIFFField fieldYRes = new TIFFField(
                    BaselineTIFFTagSet.getInstance().getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION),
                    TIFFTag.TIFF_RATIONAL, 1, new long[][]{yRes});
            nativeTree.getFirstChild().appendChild(fieldXRes.getAsNativeNode());
            nativeTree.getFirstChild().appendChild(fieldYRes.getAsNativeNode());
            char[] fieldUnit = new char[]{BaselineTIFFTagSet.RESOLUTION_UNIT_INCH};
            TIFFField fieldResUnit = new TIFFField(
                    BaselineTIFFTagSet.getInstance().getTag(BaselineTIFFTagSet.TAG_RESOLUTION_UNIT),
                    TIFFTag.TIFF_SHORT, 1, fieldUnit);
            nativeTree.getFirstChild().appendChild(fieldResUnit.getAsNativeNode());
            metaData.mergeTree(nativeName, nativeTree);
        }

        return metaData;

    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}