View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.formats.tiff;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.commons.imaging.FormatCompliance;
25  import org.apache.commons.imaging.ImagingException;
26  import org.apache.commons.imaging.bytesource.ByteSource;
27  import org.apache.commons.imaging.common.RationalNumber;
28  import org.apache.commons.imaging.formats.tiff.constants.ExifTagConstants;
29  import org.apache.commons.imaging.formats.tiff.constants.GeoTiffTagConstants;
30  import org.apache.commons.imaging.formats.tiff.constants.GpsTagConstants;
31  import org.apache.commons.imaging.formats.tiff.constants.MicrosoftHdPhotoTagConstants;
32  import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
33  import org.apache.commons.imaging.formats.tiff.write.TiffImageWriterLossy;
34  import org.apache.commons.imaging.formats.tiff.write.TiffOutputDirectory;
35  import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;
36  import org.junit.jupiter.api.Test;
37  
38  public class TiffReadWriteTagsTest extends TiffBaseTest {
39  
40      @Test
41      public void testReadWriteTags() throws ImagingException, ImagingException, IOException {
42          final String description = "A pretty picture";
43          final short page = 1;
44          final RationalNumber twoThirds = new RationalNumber(2, 3);
45          final int t4Options = 0;
46          final int width = 10;
47          final short height = 10;
48          final String area = "A good area";
49          final float widthRes = 2.2f;
50          final double geoDoubleParams = -8.4;
51          final RationalNumber exposureCompensation = new RationalNumber(-17, 10);
52          final RationalNumber[] latitude = new RationalNumber[3];
53          latitude[0] = new RationalNumber(38, 1, true);
54          latitude[1] = new RationalNumber(36, 1, true);
55          latitude[2] = new RationalNumber(0xF5937B1E, 70_000_000, true);
56  
57          final TiffOutputSet set = new TiffOutputSet();
58          final TiffOutputDirectory dir = set.getOrCreateRootDirectory();
59          dir.add(TiffTagConstants.TIFF_TAG_IMAGE_DESCRIPTION, description);
60          dir.add(TiffTagConstants.TIFF_TAG_PAGE_NUMBER, page, page);
61          dir.add(TiffTagConstants.TIFF_TAG_YRESOLUTION, twoThirds);
62          dir.add(TiffTagConstants.TIFF_TAG_T4_OPTIONS, t4Options);
63          dir.add(TiffTagConstants.TIFF_TAG_IMAGE_WIDTH, width);
64          dir.add(TiffTagConstants.TIFF_TAG_IMAGE_LENGTH, height);
65          dir.add(GpsTagConstants.GPS_TAG_GPS_AREA_INFORMATION, area);
66          dir.add(MicrosoftHdPhotoTagConstants.EXIF_TAG_WIDTH_RESOLUTION, widthRes);
67          dir.add(GeoTiffTagConstants.EXIF_TAG_GEO_DOUBLE_PARAMS_TAG, geoDoubleParams);
68          dir.add(ExifTagConstants.EXIF_TAG_EXPOSURE_COMPENSATION, exposureCompensation);
69          dir.add(GpsTagConstants.GPS_TAG_GPS_LATITUDE, latitude);
70  
71          final TiffImageWriterLossy writer = new TiffImageWriterLossy();
72          final ByteArrayOutputStream tiff = new ByteArrayOutputStream();
73          writer.write(tiff, set);
74  
75          final TiffReader reader = new TiffReader(true);
76          final FormatCompliance formatCompliance = new FormatCompliance("");
77          final TiffContents contents = reader.readDirectories(ByteSource.array(tiff.toByteArray()), true, formatCompliance);
78          final TiffDirectory rootDir = contents.directories.get(0);
79          assertEquals(description, rootDir.getSingleFieldValue(TiffTagConstants.TIFF_TAG_IMAGE_DESCRIPTION));
80          assertEquals(page, rootDir.getFieldValue(TiffTagConstants.TIFF_TAG_PAGE_NUMBER, true)[0]);
81          final RationalNumber yRes = rootDir.getFieldValue(TiffTagConstants.TIFF_TAG_YRESOLUTION);
82          assertEquals(twoThirds.numerator, yRes.numerator);
83          assertEquals(twoThirds.divisor, yRes.divisor);
84          assertEquals(t4Options, rootDir.getFieldValue(TiffTagConstants.TIFF_TAG_T4_OPTIONS));
85          assertEquals(width, rootDir.getSingleFieldValue(TiffTagConstants.TIFF_TAG_IMAGE_WIDTH));
86          assertEquals(width, rootDir.getSingleFieldValue(TiffTagConstants.TIFF_TAG_IMAGE_LENGTH));
87          assertEquals(area, rootDir.getFieldValue(GpsTagConstants.GPS_TAG_GPS_AREA_INFORMATION, true));
88          assertEquals(widthRes, rootDir.getFieldValue(MicrosoftHdPhotoTagConstants.EXIF_TAG_WIDTH_RESOLUTION), 0.0);
89          assertEquals(geoDoubleParams, rootDir.getFieldValue(GeoTiffTagConstants.EXIF_TAG_GEO_DOUBLE_PARAMS_TAG, true)[0], 0.0);
90          assertEquals(exposureCompensation.doubleValue(), rootDir.getFieldValue(ExifTagConstants.EXIF_TAG_EXPOSURE_COMPENSATION).doubleValue(), 0.0);
91          final RationalNumber[] testLat = rootDir.getFieldValue(GpsTagConstants.GPS_TAG_GPS_LATITUDE, true);
92          for (int i = 0; i < 3; i++) {
93              assertEquals(latitude[i].doubleValue(), testLat[i].doubleValue(), 0.0);
94          }
95      }
96  }