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.fieldtypes;
18  
19  import java.nio.ByteOrder;
20  import java.util.Arrays;
21  
22  import org.apache.commons.imaging.ImagingException;
23  import org.apache.commons.imaging.common.Allocator;
24  import org.apache.commons.imaging.common.ByteConversions;
25  import org.apache.commons.imaging.common.RationalNumber;
26  import org.apache.commons.imaging.formats.tiff.TiffField;
27  
28  public class FieldTypeRational extends AbstractFieldType {
29      public FieldTypeRational(final int type, final String name) {
30          super(type, name, 8);
31      }
32  
33      @Override
34      public Object getValue(final TiffField entry) {
35          final byte[] bytes = entry.getByteArrayValue();
36          final boolean unsignedType = entry.getFieldType() != SRATIONAL;
37          if (entry.getCount() == 1) {
38              return ByteConversions.toRational(bytes, entry.getByteOrder(), unsignedType);
39          }
40          return ByteConversions.toRationals(bytes, entry.getByteOrder(), unsignedType);
41      }
42  
43      @Override
44      public byte[] writeData(final Object o, final ByteOrder byteOrder) throws ImagingException {
45          if (o instanceof RationalNumber) {
46              return ByteConversions.toBytes((RationalNumber) o, byteOrder);
47          }
48          if (o instanceof RationalNumber[]) {
49              return ByteConversions.toBytes((RationalNumber[]) o, byteOrder);
50          }
51          if (o instanceof Number) {
52              final Number number = (Number) o;
53              return ByteConversions.toBytes(RationalNumber.valueOf(number.doubleValue()), byteOrder);
54          }
55          if (o instanceof Number[]) {
56              final Number[] numbers = (Number[]) o;
57              final RationalNumber[] rationalNumbers = Allocator.array(numbers.length, RationalNumber[]::new, RationalNumber.SHALLOW_SIZE);
58              Arrays.setAll(rationalNumbers, RationalNumber::valueOf);
59              return ByteConversions.toBytes(rationalNumbers, byteOrder);
60          }
61          if (!(o instanceof double[])) {
62              throw new ImagingException("Invalid data", o);
63          }
64          final double[] numbers = (double[]) o;
65          final RationalNumber[] rationalNumbers = Allocator.array(numbers.length, RationalNumber[]::new, RationalNumber.SHALLOW_SIZE);
66          Arrays.setAll(rationalNumbers, RationalNumber::valueOf);
67          return ByteConversions.toBytes(rationalNumbers, byteOrder);
68      }
69  }