001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.imaging.formats.tiff.fieldtypes; 018 019import java.nio.ByteOrder; 020import java.util.Arrays; 021 022import org.apache.commons.imaging.ImagingException; 023import org.apache.commons.imaging.common.Allocator; 024import org.apache.commons.imaging.common.ByteConversions; 025import org.apache.commons.imaging.common.RationalNumber; 026import org.apache.commons.imaging.formats.tiff.TiffField; 027 028public class FieldTypeRational extends AbstractFieldType { 029 public FieldTypeRational(final int type, final String name) { 030 super(type, name, 8); 031 } 032 033 @Override 034 public Object getValue(final TiffField entry) { 035 final byte[] bytes = entry.getByteArrayValue(); 036 final boolean unsignedType = entry.getFieldType() != SRATIONAL; 037 if (entry.getCount() == 1) { 038 return ByteConversions.toRational(bytes, entry.getByteOrder(), unsignedType); 039 } 040 return ByteConversions.toRationals(bytes, entry.getByteOrder(), unsignedType); 041 } 042 043 @Override 044 public byte[] writeData(final Object o, final ByteOrder byteOrder) throws ImagingException { 045 if (o instanceof RationalNumber) { 046 return ByteConversions.toBytes((RationalNumber) o, byteOrder); 047 } 048 if (o instanceof RationalNumber[]) { 049 return ByteConversions.toBytes((RationalNumber[]) o, byteOrder); 050 } 051 if (o instanceof Number) { 052 final Number number = (Number) o; 053 return ByteConversions.toBytes(RationalNumber.valueOf(number.doubleValue()), byteOrder); 054 } 055 if (o instanceof Number[]) { 056 final Number[] numbers = (Number[]) o; 057 final RationalNumber[] rationalNumbers = Allocator.array(numbers.length, RationalNumber[]::new, RationalNumber.SHALLOW_SIZE); 058 Arrays.setAll(rationalNumbers, RationalNumber::valueOf); 059 return ByteConversions.toBytes(rationalNumbers, byteOrder); 060 } 061 if (!(o instanceof double[])) { 062 throw new ImagingException("Invalid data", o); 063 } 064 final double[] numbers = (double[]) o; 065 final RationalNumber[] rationalNumbers = Allocator.array(numbers.length, RationalNumber[]::new, RationalNumber.SHALLOW_SIZE); 066 Arrays.setAll(rationalNumbers, RationalNumber::valueOf); 067 return ByteConversions.toBytes(rationalNumbers, byteOrder); 068 } 069}