org.apache.commons.imaging.ImageWriteException Java Examples

The following examples show how to use org.apache.commons.imaging.ImageWriteException. 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: ImageUtil.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Save image into a file
 * @param image Image
 * @param fileName Output image file name
 * @throws IOException 
 * @throws ImageWriteException 
 */
public static void imageSave(BufferedImage image, String fileName) throws IOException, ImageWriteException{
    String extension = fileName.substring(fileName.lastIndexOf('.') + 1); 
    ImageFormat format = getImageFormat(extension);
    if (format == ImageFormats.JPEG){
        ImageIO.write(image, extension, new File(fileName));
    } else {
        Imaging.writeImage(image, new File(fileName), format, null);       
    }
}
 
Example #2
Source File: TextFieldTest.java    From commons-imaging with Apache License 2.0 6 votes vote down vote up
@Override
protected void checkField(final File imageFile, final TiffField field)
        throws IOException, ImageReadException, ImageWriteException {
    if (field.getTag() == ExifTagConstants.EXIF_TAG_USER_COMMENT.tag) {
        // do nothing
    } else if (field.getTag() == GpsTagConstants.GPS_TAG_GPS_PROCESSING_METHOD.tag
            && field.getDirectoryType() == TiffDirectoryType.EXIF_DIRECTORY_GPS.directoryType) {
            // do nothing
    } else if (field.getTag() == GpsTagConstants.GPS_TAG_GPS_AREA_INFORMATION.tag
            && field.getDirectoryType() == TiffDirectoryType.EXIF_DIRECTORY_GPS.directoryType) {
            // do nothing
    } else {
        return;
    }

    try {
        final Object textFieldValue = field.getValue();
        Assertions.assertNotNull(textFieldValue);
        // TODO what else to assert?
    } catch (final ImageReadException e) {
        Debug.debug("imageFile", imageFile.getAbsoluteFile());
        Debug.debug(e);
        throw e;
    }

}
 
Example #3
Source File: PnmImageParserTest.java    From commons-imaging with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteImageRaw_happyCase() throws ImageWriteException,
                                                 ImageReadException, IOException {
    final BufferedImage srcImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
    final Map<String, Object> params = new HashMap<>();
    params.put(PnmImageParser.PARAM_KEY_PNM_RAWBITS, PnmImageParser.PARAM_VALUE_PNM_RAWBITS_YES);

    final byte[] dstBytes = Imaging.writeImageToBytes(srcImage, ImageFormats.PNM, params);
    final BufferedImage dstImage = Imaging.getBufferedImage(dstBytes);

    assertTrue(srcImage.getWidth() == dstImage.getWidth());
    assertTrue(srcImage.getHeight() == dstImage.getHeight());

    final DataBufferInt srcData = (DataBufferInt) srcImage.getRaster().getDataBuffer();
    final DataBufferInt dstData = (DataBufferInt) dstImage.getRaster().getDataBuffer();

    for (int bank = 0; bank < srcData.getNumBanks(); bank++) {
        final int[] actual = srcData.getData(bank);
        final int[] expected = dstData.getData(bank);

        assertArrayEquals(actual, expected);
    }
}
 
Example #4
Source File: ImageWriteExample.java    From commons-imaging with Apache License 2.0 6 votes vote down vote up
public static byte[] imageWriteExample(final File file)
        throws ImageReadException, ImageWriteException, IOException {
    // read image
    final BufferedImage image = Imaging.getBufferedImage(file);

    final ImageFormat format = ImageFormats.TIFF;
    final Map<String, Object> params = new HashMap<>();

    // set optional parameters if you like
    params.put(ImagingConstants.PARAM_KEY_COMPRESSION, Integer.valueOf(
            TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED));

    final byte[] bytes = Imaging.writeImageToBytes(image, format, params);

    return bytes;
}
 
Example #5
Source File: JpegRewriter.java    From commons-imaging with Apache License 2.0 6 votes vote down vote up
protected <T extends JFIFPiece, U extends JFIFPiece> List<JFIFPiece> insertBeforeFirstAppSegments(
        final List<T> segments, final List<U> newSegments) throws ImageWriteException {
    int firstAppIndex = -1;
    for (int i = 0; i < segments.size(); i++) {
        final JFIFPiece piece = segments.get(i);
        if (!(piece instanceof JFIFPieceSegment)) {
            continue;
        }

        final JFIFPieceSegment segment = (JFIFPieceSegment) piece;
        if (segment.isAppSegment()) {
            if (firstAppIndex == -1) {
                firstAppIndex = i;
            }
        }
    }

    final List<JFIFPiece> result = new ArrayList<>(segments);
    if (firstAppIndex == -1) {
        throw new ImageWriteException("JPEG file has no APP segments.");
    }
    result.addAll(firstAppIndex, newSegments);
    return result;
}
 
Example #6
Source File: JpegXmpRewriter.java    From commons-imaging with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a Jpeg image, replaces the XMP XML and writes the result to a
 * stream.
 *
 * @param byteSource
 *            ByteSource containing Jpeg image data.
 * @param os
 *            OutputStream to write the image to.
 * @param xmpXml
 *            String containing XMP XML.
 * @throws ImageReadException if it fails to read the JFIF segments
 * @throws IOException if it fails to read or write the data from the segments
 * @throws ImageWriteException if it fails to write the JFIF segments
 */
public void updateXmpXml(final ByteSource byteSource, final OutputStream os,
        final String xmpXml) throws ImageReadException, IOException,
        ImageWriteException {
    final JFIFPieces jfifPieces = analyzeJFIF(byteSource);
    List<JFIFPiece> pieces = jfifPieces.pieces;
    pieces = removeXmpSegments(pieces);

    final List<JFIFPieceSegment> newPieces = new ArrayList<>();
    final byte[] xmpXmlBytes = xmpXml.getBytes(StandardCharsets.UTF_8);
    int index = 0;
    while (index < xmpXmlBytes.length) {
        final int segmentSize = Math.min(xmpXmlBytes.length, JpegConstants.MAX_SEGMENT_SIZE);
        final byte[] segmentData = writeXmpSegment(xmpXmlBytes, index,
                segmentSize);
        newPieces.add(new JFIFPieceSegment(JpegConstants.JPEG_APP1_MARKER, segmentData));
        index += segmentSize;
    }

    pieces = insertAfterLastAppSegments(pieces, newPieces);

    writeSegments(os, pieces);
}
 
Example #7
Source File: TiffImageMetadata.java    From commons-imaging with Apache License 2.0 6 votes vote down vote up
public TiffOutputSet getOutputSet() throws ImageWriteException {
    final ByteOrder byteOrder = contents.header.byteOrder;
    final TiffOutputSet result = new TiffOutputSet(byteOrder);

    final List<? extends ImageMetadataItem> srcDirs = getDirectories();
    for (final ImageMetadataItem srcDir1 : srcDirs) {
        final Directory srcDir = (Directory) srcDir1;

        if (null != result.findDirectory(srcDir.type)) {
            // Certain cameras right directories more than once.
            // This is a bug.
            // Ignore second directory of a given type.
            continue;
        }

        final TiffOutputDirectory outputDirectory = srcDir.getOutputDirectory(byteOrder);
        result.addDirectory(outputDirectory);
    }

    return result;
}
 
Example #8
Source File: TiffOutputDirectory.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public void add(final TagInfoFloat tagInfo, final float value)
        throws ImageWriteException {
    if (tagInfo.length != 1) {
        throw new ImageWriteException("Tag expects " + tagInfo.length
                + " value(s), not 1");
    }
    final byte[] bytes = tagInfo.encodeValue(byteOrder, value);
    final TiffOutputField tiffOutputField = new TiffOutputField(tagInfo.tag,
            tagInfo, FieldType.FLOAT, 1, bytes);
    add(tiffOutputField);
}
 
Example #9
Source File: TiffOutputDirectory.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public void add(final TagInfoShortOrLongOrRational tagInfo, final int... values)
        throws ImageWriteException {
    if (tagInfo.length > 0 && tagInfo.length != values.length) {
        throw new ImageWriteException("Tag expects " + tagInfo.length
                + " value(s), not " + values.length);
    }
    final byte[] bytes = tagInfo.encodeValue(byteOrder, values);
    final TiffOutputField tiffOutputField = new TiffOutputField(tagInfo.tag,
            tagInfo, FieldType.LONG, values.length,
            bytes);
    add(tiffOutputField);
}
 
Example #10
Source File: TiffOutputDirectory.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public void add(final TagInfoRationals tagInfo, final RationalNumber... values)
        throws ImageWriteException {
    if (tagInfo.length > 0 && tagInfo.length != values.length) {
        throw new ImageWriteException("Tag expects " + tagInfo.length
                + " value(s), not " + values.length);
    }
    final byte[] bytes = tagInfo.encodeValue(byteOrder, values);
    final TiffOutputField tiffOutputField = new TiffOutputField(tagInfo.tag,
            tagInfo, FieldType.RATIONAL,
            values.length, bytes);
    add(tiffOutputField);
}
 
Example #11
Source File: TiffOutputDirectory.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public void add(final TagInfoSByte tagInfo, final byte value)
        throws ImageWriteException {
    if (tagInfo.length != 1) {
        throw new ImageWriteException("Tag expects " + tagInfo.length
                + " value(s), not 1");
    }
    final byte[] bytes = tagInfo.encodeValue(byteOrder, value);
    final TiffOutputField tiffOutputField = new TiffOutputField(tagInfo.tag,
            tagInfo, FieldType.SBYTE, 1, bytes);
    add(tiffOutputField);
}
 
Example #12
Source File: IcoRoundtripTest.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
@Override
       public byte[] generateBitmap(final int foreground, final int background,
final int paletteSize)
               throws IOException, ImageWriteException {
           try (final ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
                   final BinaryOutputStream bos = new BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
               // Palette
               bos.write3Bytes(background);
               bos.write(0);
               bos.write3Bytes(foreground);
               bos.write(0);
               for (int i = 2; i < paletteSize; i++) {
                   bos.write4Bytes(0);
               }
               // Image
               for (int y = 15; y >= 0; y--) {
                   for (int x = 0; x < 16; x += 8) {
                       bos.write(((0x1 & IMAGE[y][x]) << 7) | ((0x1 & IMAGE[y][x + 1]) << 6)
                               | ((0x1 & IMAGE[y][x + 2]) << 5) | ((0x1 & IMAGE[y][x + 3]) << 4)
                               | ((0x1 & IMAGE[y][x + 4]) << 3) | ((0x1 & IMAGE[y][x + 5]) << 2)
                               | ((0x1 & IMAGE[y][x + 6]) << 1) | ((0x1 & IMAGE[y][x + 7]) << 0));
                   }
                   // Pad to multiple of 32 bytes
                   bos.write(0);
                   bos.write(0);
               }
               // Mask
               for (int y = IMAGE.length - 1; y >= 0; y--) {
                   bos.write(0);
                   bos.write(0);
                   // Pad to 4 bytes:
                   bos.write(0);
                   bos.write(0);
               }
               bos.flush();
               return byteArrayStream.toByteArray();
           }
       }
 
Example #13
Source File: DitheringTest.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
@Test
public void testApplyFloydSteinbergDitheringWithNonNullTwo() throws ImageWriteException {
    BufferedImage bufferedImage = new BufferedImage(3, 3, 3);
    bufferedImage.setRGB(1, 2, 4);
    List<ColorSpaceSubset> linkedList = new LinkedList<>();
    ColorSpaceSubset colorSpaceSubset = new ColorSpaceSubset((-234), (-352));
    linkedList.add(colorSpaceSubset);
    QuantizedPalette quantizedPalette = new QuantizedPalette(linkedList, 3);
    Dithering.applyFloydSteinbergDithering(bufferedImage, quantizedPalette);

    assertEquals(-1, bufferedImage.getRGB(0,0) );
    assertEquals(-1, bufferedImage.getRGB(1,1) );
    assertEquals(-1, bufferedImage.getRGB(2,1) );
    assertEquals(-1, bufferedImage.getRGB(2,2) );
}
 
Example #14
Source File: IcoRoundtripTest.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] generateBitmap(final int foreground, final int background, final int paletteSize)
        throws IOException, ImageWriteException {
    try (final ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
            final BinaryOutputStream bos = new BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
        // Palette
        bos.write3Bytes(background);
        bos.write(0);
        bos.write3Bytes(foreground);
        bos.write(0);
        for (int i = 2; i < paletteSize; i++) {
            bos.write4Bytes(0);
        }
        // Image
        for (int y = 15; y >= 0; y--) {
            for (int x = 0; x < 16; x++) {
                bos.write(IMAGE[y][x]);
            }
        }
        // Mask
        for (int y = IMAGE.length - 1; y >= 0; y--) {
            bos.write(0);
            bos.write(0);
            // Pad to 4 bytes:
            bos.write(0);
            bos.write(0);
        }
        bos.flush();
        return byteArrayStream.toByteArray();
    }
}
 
Example #15
Source File: FieldTypeByteTest.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteDataWithNull() throws ImageWriteException {
    final FieldTypeByte fieldTypeByte = FieldType.UNDEFINED;
    final ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;

    Assertions.assertThrows(ImageWriteException.class, () -> {
        fieldTypeByte.writeData( null, byteOrder);          
    });
}
 
Example #16
Source File: TiffOutputDirectory.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public void add(final TagInfoByteOrShort tagInfo, final byte... values)
        throws ImageWriteException {
    if (tagInfo.length > 0 && tagInfo.length != values.length) {
        throw new ImageWriteException("Tag expects " + tagInfo.length
                + " value(s), not " + values.length);
    }
    final byte[] bytes = tagInfo.encodeValue(byteOrder, values);
    final TiffOutputField tiffOutputField = new TiffOutputField(tagInfo.tag,
            tagInfo, FieldType.BYTE, values.length,
            bytes);
    add(tiffOutputField);
}
 
Example #17
Source File: FieldTypeRationalTest.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteDataWithNonNull() throws ImageWriteException {
    FieldTypeRational fieldTypeRational = new FieldTypeRational((-922), "z_AX");
    ByteOrder byteOrder = ByteOrder.nativeOrder();
    Assertions.assertThrows(ImageWriteException.class, () -> {
        fieldTypeRational.writeData("z_AX", byteOrder);
    });
}
 
Example #18
Source File: TiffOutputDirectory.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public void add(final TagInfoShortOrRational tagInfo, final short... values)
        throws ImageWriteException {
    if (tagInfo.length > 0 && tagInfo.length != values.length) {
        throw new ImageWriteException("Tag expects " + tagInfo.length
                + " value(s), not " + values.length);
    }
    final byte[] bytes = tagInfo.encodeValue(byteOrder, values);
    final TiffOutputField tiffOutputField = new TiffOutputField(tagInfo.tag,
            tagInfo, FieldType.SHORT,
            values.length, bytes);
    add(tiffOutputField);
}
 
Example #19
Source File: TiffOutputDirectory.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public void add(final TagInfoSLongs tagInfo, final int... values)
        throws ImageWriteException {
    if (tagInfo.length > 0 && tagInfo.length != values.length) {
        throw new ImageWriteException("Tag expects " + tagInfo.length
                + " value(s), not " + values.length);
    }
    final byte[] bytes = tagInfo.encodeValue(byteOrder, values);
    final TiffOutputField tiffOutputField = new TiffOutputField(tagInfo.tag,
            tagInfo, FieldType.SLONG,
            values.length, bytes);
    add(tiffOutputField);
}
 
Example #20
Source File: TiffOutputDirectory.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public void add(final TagInfoLong tagInfo, final int value)
        throws ImageWriteException {
    if (tagInfo.length != 1) {
        throw new ImageWriteException("Tag expects " + tagInfo.length
                + " value(s), not 1");
    }
    final byte[] bytes = tagInfo.encodeValue(byteOrder, value);
    final TiffOutputField tiffOutputField = new TiffOutputField(tagInfo.tag,
            tagInfo, FieldType.LONG, 1, bytes);
    add(tiffOutputField);
}
 
Example #21
Source File: TiffOutputDirectory.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public void add(final TagInfoShort tagInfo, final short value)
        throws ImageWriteException {
    if (tagInfo.length != 1) {
        throw new ImageWriteException("Tag expects " + tagInfo.length
                + " value(s), not 1");
    }
    final byte[] bytes = tagInfo.encodeValue(byteOrder, value);
    final TiffOutputField tiffOutputField = new TiffOutputField(tagInfo.tag,
            tagInfo, FieldType.SHORT, 1, bytes);
    add(tiffOutputField);
}
 
Example #22
Source File: TiffOutputDirectory.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public void add(final TagInfoAscii tagInfo, final String... values)
        throws ImageWriteException {
    final byte[] bytes = tagInfo.encodeValue(byteOrder, values);
    if (tagInfo.length > 0 && tagInfo.length != bytes.length) {
        throw new ImageWriteException("Tag expects " + tagInfo.length
                + " byte(s), not " + values.length);
    }
    final TiffOutputField tiffOutputField = new TiffOutputField(tagInfo.tag,
            tagInfo, FieldType.ASCII, bytes.length,
            bytes);
    add(tiffOutputField);
}
 
Example #23
Source File: TiffOutputSet.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public TiffOutputDirectory addInteroperabilityDirectory()
        throws ImageWriteException {
    getOrCreateExifDirectory();

    final TiffOutputDirectory result = new TiffOutputDirectory(
            TiffDirectoryConstants.DIRECTORY_TYPE_INTEROPERABILITY, byteOrder);
    addDirectory(result);
    return result;
}
 
Example #24
Source File: TiffOutputSet.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public TiffOutputDirectory getOrCreateRootDirectory()
        throws ImageWriteException {
    final TiffOutputDirectory result = findDirectory(TiffDirectoryConstants.DIRECTORY_TYPE_ROOT);
    if (null != result) {
        return result;
    }
    return addRootDirectory();
}
 
Example #25
Source File: TiffOutputDirectory.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public void add(final TagInfoAsciiOrRational tagInfo, final String... values)
        throws ImageWriteException {
    final byte[] bytes = tagInfo.encodeValue(
            FieldType.ASCII, values, byteOrder);
    if (tagInfo.length > 0 && tagInfo.length != bytes.length) {
        throw new ImageWriteException("Tag expects " + tagInfo.length
                + " byte(s), not " + values.length);
    }
    final TiffOutputField tiffOutputField = new TiffOutputField(tagInfo.tag,
            tagInfo, FieldType.ASCII, bytes.length,
            bytes);
    add(tiffOutputField);
}
 
Example #26
Source File: ZlibDeflate.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
/**
 * Compress the byte[] using ZLIB deflate compression.
 *
 * @param bytes The bytes to compress
 *
 * @return The compressed bytes.
 * @throws ImageWriteException if the bytes could not be compressed.
 * @see DeflaterOutputStream
 */
public static byte[] compress(final byte[] bytes) throws ImageWriteException {
    ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length / 2);
    try (DeflaterOutputStream compressOut = new DeflaterOutputStream(out)) {
        compressOut.write(bytes);
    } catch (final IOException e) {
        throw new ImageWriteException("Unable to compress image", e);
    }
    return out.toByteArray();
}
 
Example #27
Source File: SpecificExifTagTest.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
private void checkImage(final File imageFile) throws IOException,
        ImageReadException, ImageWriteException {
    // Debug.debug("imageFile", imageFile.getAbsoluteFile());

    final Map<String, Object> params = new HashMap<>();
    final boolean ignoreImageData = isPhilHarveyTestImage(imageFile);
    params.put(ImagingConstants.PARAM_KEY_READ_THUMBNAILS, Boolean.valueOf(!ignoreImageData));

    // note that metadata might be null if no metadata is found.
    final ImageMetadata metadata = Imaging.getMetadata(imageFile, params);
    if (null == metadata) {
        return;
    }
    final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;

    // note that exif might be null if no Exif metadata is found.
    final TiffImageMetadata exif = jpegMetadata.getExif();
    if (null == exif) {
        return;
    }

    final List<TiffField> fields = exif.getAllFields();
    for (final TiffField field : fields) {
        checkField(imageFile, field);
    }

}
 
Example #28
Source File: TiffOutputDirectory.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public void add(final TagInfoShortOrLong tagInfo, final int... values)
        throws ImageWriteException {
    if (tagInfo.length > 0 && tagInfo.length != values.length) {
        throw new ImageWriteException("Tag expects " + tagInfo.length
                + " value(s), not " + values.length);
    }
    final byte[] bytes = tagInfo.encodeValue(byteOrder, values);
    final TiffOutputField tiffOutputField = new TiffOutputField(tagInfo.tag,
            tagInfo, FieldType.LONG, values.length,
            bytes);
    add(tiffOutputField);
}
 
Example #29
Source File: PgmWriter.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
@Override
public void writeImage(final BufferedImage src, final OutputStream os, final Map<String, Object> params)
        throws ImageWriteException, IOException {
    // System.out.println
    // (b1 == 0x50 && b2 == 0x36)
    os.write(0x50);
    os.write(rawbits ? 0x35 : 0x32);
    os.write(PnmConstants.PNM_SEPARATOR);

    final int width = src.getWidth();
    final int height = src.getHeight();

    os.write(Integer.toString(width).getBytes(StandardCharsets.US_ASCII));
    os.write(PnmConstants.PNM_SEPARATOR);

    os.write(Integer.toString(height).getBytes(StandardCharsets.US_ASCII));
    os.write(PnmConstants.PNM_SEPARATOR);

    os.write(Integer.toString(255).getBytes(StandardCharsets.US_ASCII)); // max component value
    os.write(PnmConstants.PNM_NEWLINE);

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            final int argb = src.getRGB(x, y);
            final int red = 0xff & (argb >> 16);
            final int green = 0xff & (argb >> 8);
            final int blue = 0xff & (argb >> 0);
            final int sample = (red + green + blue) / 3;

            if (rawbits) {
                os.write((byte) sample);
            } else {
                os.write(Integer.toString(sample).getBytes(StandardCharsets.US_ASCII)); // max component value
                os.write(PnmConstants.PNM_SEPARATOR);
            }
        }
    }
}
 
Example #30
Source File: TiffOutputDirectory.java    From commons-imaging with Apache License 2.0 5 votes vote down vote up
public void add(final TagInfoByteOrShort tagInfo, final short... values)
        throws ImageWriteException {
    if (tagInfo.length > 0 && tagInfo.length != values.length) {
        throw new ImageWriteException("Tag expects " + tagInfo.length
                + " value(s), not " + values.length);
    }
    final byte[] bytes = tagInfo.encodeValue(byteOrder, values);
    final TiffOutputField tiffOutputField = new TiffOutputField(tagInfo.tag,
            tagInfo, FieldType.SHORT,
            values.length, bytes);
    add(tiffOutputField);
}