1 /*
2 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
550 * {@code TIFF_LONG}, {@code TIFF_RATIONAL}, or {@code TIFF_IFD_POINTER}
551 * and any of the elements is negative or greater than {@code 0xffffffff}.
552 */
553 public TIFFField(TIFFTag tag, int type, int count, Object data) {
554 if(tag == null) {
555 throw new NullPointerException("tag == null!");
556 } else if(type < TIFFTag.MIN_DATATYPE || type > TIFFTag.MAX_DATATYPE) {
557 throw new IllegalArgumentException("Unknown data type "+type);
558 } else if(!tag.isDataTypeOK(type)) {
559 throw new IllegalArgumentException("Illegal data type " + type
560 + " for " + tag.getName() + " tag");
561 } else if(count < 0) {
562 throw new IllegalArgumentException("count < 0!");
563 } else if((type == TIFFTag.TIFF_RATIONAL
564 || type == TIFFTag.TIFF_SRATIONAL)
565 && count < 1) {
566 throw new IllegalArgumentException
567 ("Type is TIFF_RATIONAL or TIFF_SRATIONAL and count < 1");
568 } else if (type == TIFFTag.TIFF_IFD_POINTER && count != 1) {
569 throw new IllegalArgumentException
570 ("Type is TIFF_IFD_POINTER count != 1");
571 } else if(data == null) {
572 throw new NullPointerException("data == null!");
573 }
574
575 boolean isDataArrayCorrect = false;
576
577 switch (type) {
578 case TIFFTag.TIFF_BYTE:
579 case TIFFTag.TIFF_SBYTE:
580 case TIFFTag.TIFF_UNDEFINED:
581 isDataArrayCorrect = data instanceof byte[]
582 && ((byte[])data).length == count;
583 break;
584 case TIFFTag.TIFF_ASCII:
585 isDataArrayCorrect = data instanceof String[]
586 && ((String[])data).length == count;
587 break;
588 case TIFFTag.TIFF_SHORT:
589 isDataArrayCorrect = data instanceof char[]
590 && ((char[])data).length == count;
681 this.data = data;
682 }
683
684 /**
685 * Constructs a data array using {@link #createArrayForType
686 * createArrayForType()} and invokes
687 * {@link #TIFFField(TIFFTag,int,int,Object)} with the supplied
688 * parameters and the created array.
689 *
690 * @param tag The tag to associated with this field.
691 * @param type One of the {@code TIFFTag.TIFF_*} constants
692 * indicating the data type of the field as written to the TIFF stream.
693 * @param count The number of data values.
694 * @throws NullPointerException if {@code tag == null}.
695 * @throws IllegalArgumentException if {@code type} is not
696 * one of the {@code TIFFTag.TIFF_*} data type constants.
697 * @throws IllegalArgumentException if {@code type} is an unacceptable
698 * data type for the supplied {@code TIFFTag}.
699 * @throws IllegalArgumentException if {@code count < 0}.
700 * @see #TIFFField(TIFFTag,int,int,Object)
701 */
702 public TIFFField(TIFFTag tag, int type, int count) {
703 this(tag, type, count, createArrayForType(type, count));
704 }
705
706 /**
707 * Constructs a {@code TIFFField} with a single non-negative integral
708 * value. The field will have type {@link TIFFTag#TIFF_SHORT TIFF_SHORT}
709 * if {@code value} is in {@code [0,0xffff]}, and type
710 * {@link TIFFTag#TIFF_LONG TIFF_LONG} if {@code value} is in
711 * {@code [0x10000,0xffffffff]}. The count of the field will be unity.
712 *
713 * @param tag The tag to associate with this field.
714 * @param value The value to associate with this field.
715 * @throws NullPointerException if {@code tag == null}.
716 * @throws IllegalArgumentException if {@code value} is not in
717 * {@code [0,0xffffffff]}.
718 * @throws IllegalArgumentException if {@code value} is in
719 * {@code [0,0xffff]} and {@code TIFF_SHORT} is an unacceptable type
720 * for the {@code TIFFTag}, or if {@code value} is in
868 for (int i = TIFFTag.MIN_DATATYPE; i <= TIFFTag.MAX_DATATYPE; i++) {
869 if (typeName.equals(TYPE_NAMES[i])) {
870 return i;
871 }
872 }
873
874 return -1;
875 }
876
877 /**
878 * Creates an array appropriate for the indicated data type.
879 *
880 * @param dataType One of the {@code TIFFTag.TIFF_*} data type
881 * constants.
882 * @param count The number of values in the array.
883 * @return An array appropriate for the specified data type.
884 *
885 * @throws IllegalArgumentException if {@code dataType} is not
886 * one of the {@code TIFFTag.TIFF_*} data type constants.
887 * @throws IllegalArgumentException if {@code count < 0}.
888 */
889 public static Object createArrayForType(int dataType, int count) {
890 if(count < 0) {
891 throw new IllegalArgumentException("count < 0!");
892 }
893 switch (dataType) {
894 case TIFFTag.TIFF_BYTE:
895 case TIFFTag.TIFF_SBYTE:
896 case TIFFTag.TIFF_UNDEFINED:
897 return new byte[count];
898 case TIFFTag.TIFF_ASCII:
899 return new String[count];
900 case TIFFTag.TIFF_SHORT:
901 return new char[count];
902 case TIFFTag.TIFF_LONG:
903 case TIFFTag.TIFF_IFD_POINTER:
904 return new long[count];
905 case TIFFTag.TIFF_RATIONAL:
906 return new long[count][2];
907 case TIFFTag.TIFF_SSHORT:
908 return new short[count];
909 case TIFFTag.TIFF_SLONG:
910 return new int[count];
911 case TIFFTag.TIFF_SRATIONAL:
912 return new int[count][2];
|
1 /*
2 * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
550 * {@code TIFF_LONG}, {@code TIFF_RATIONAL}, or {@code TIFF_IFD_POINTER}
551 * and any of the elements is negative or greater than {@code 0xffffffff}.
552 */
553 public TIFFField(TIFFTag tag, int type, int count, Object data) {
554 if(tag == null) {
555 throw new NullPointerException("tag == null!");
556 } else if(type < TIFFTag.MIN_DATATYPE || type > TIFFTag.MAX_DATATYPE) {
557 throw new IllegalArgumentException("Unknown data type "+type);
558 } else if(!tag.isDataTypeOK(type)) {
559 throw new IllegalArgumentException("Illegal data type " + type
560 + " for " + tag.getName() + " tag");
561 } else if(count < 0) {
562 throw new IllegalArgumentException("count < 0!");
563 } else if((type == TIFFTag.TIFF_RATIONAL
564 || type == TIFFTag.TIFF_SRATIONAL)
565 && count < 1) {
566 throw new IllegalArgumentException
567 ("Type is TIFF_RATIONAL or TIFF_SRATIONAL and count < 1");
568 } else if (type == TIFFTag.TIFF_IFD_POINTER && count != 1) {
569 throw new IllegalArgumentException
570 ("Type is TIFF_IFD_POINTER and count != 1");
571 } else if(data == null) {
572 throw new NullPointerException("data == null!");
573 }
574
575 boolean isDataArrayCorrect = false;
576
577 switch (type) {
578 case TIFFTag.TIFF_BYTE:
579 case TIFFTag.TIFF_SBYTE:
580 case TIFFTag.TIFF_UNDEFINED:
581 isDataArrayCorrect = data instanceof byte[]
582 && ((byte[])data).length == count;
583 break;
584 case TIFFTag.TIFF_ASCII:
585 isDataArrayCorrect = data instanceof String[]
586 && ((String[])data).length == count;
587 break;
588 case TIFFTag.TIFF_SHORT:
589 isDataArrayCorrect = data instanceof char[]
590 && ((char[])data).length == count;
681 this.data = data;
682 }
683
684 /**
685 * Constructs a data array using {@link #createArrayForType
686 * createArrayForType()} and invokes
687 * {@link #TIFFField(TIFFTag,int,int,Object)} with the supplied
688 * parameters and the created array.
689 *
690 * @param tag The tag to associated with this field.
691 * @param type One of the {@code TIFFTag.TIFF_*} constants
692 * indicating the data type of the field as written to the TIFF stream.
693 * @param count The number of data values.
694 * @throws NullPointerException if {@code tag == null}.
695 * @throws IllegalArgumentException if {@code type} is not
696 * one of the {@code TIFFTag.TIFF_*} data type constants.
697 * @throws IllegalArgumentException if {@code type} is an unacceptable
698 * data type for the supplied {@code TIFFTag}.
699 * @throws IllegalArgumentException if {@code count < 0}.
700 * @see #TIFFField(TIFFTag,int,int,Object)
701 * @throws IllegalArgumentException if {@code count < 1}
702 * and {@code type} is {@code TIFF_RATIONAL} or
703 * {@code TIFF_SRATIONAL}.
704 * @throws IllegalArgumentException if {@code count != 1}
705 * and {@code type} is {@code TIFF_IFD_POINTER}.
706 */
707 public TIFFField(TIFFTag tag, int type, int count) {
708 this(tag, type, count, createArrayForType(type, count));
709 }
710
711 /**
712 * Constructs a {@code TIFFField} with a single non-negative integral
713 * value. The field will have type {@link TIFFTag#TIFF_SHORT TIFF_SHORT}
714 * if {@code value} is in {@code [0,0xffff]}, and type
715 * {@link TIFFTag#TIFF_LONG TIFF_LONG} if {@code value} is in
716 * {@code [0x10000,0xffffffff]}. The count of the field will be unity.
717 *
718 * @param tag The tag to associate with this field.
719 * @param value The value to associate with this field.
720 * @throws NullPointerException if {@code tag == null}.
721 * @throws IllegalArgumentException if {@code value} is not in
722 * {@code [0,0xffffffff]}.
723 * @throws IllegalArgumentException if {@code value} is in
724 * {@code [0,0xffff]} and {@code TIFF_SHORT} is an unacceptable type
725 * for the {@code TIFFTag}, or if {@code value} is in
873 for (int i = TIFFTag.MIN_DATATYPE; i <= TIFFTag.MAX_DATATYPE; i++) {
874 if (typeName.equals(TYPE_NAMES[i])) {
875 return i;
876 }
877 }
878
879 return -1;
880 }
881
882 /**
883 * Creates an array appropriate for the indicated data type.
884 *
885 * @param dataType One of the {@code TIFFTag.TIFF_*} data type
886 * constants.
887 * @param count The number of values in the array.
888 * @return An array appropriate for the specified data type.
889 *
890 * @throws IllegalArgumentException if {@code dataType} is not
891 * one of the {@code TIFFTag.TIFF_*} data type constants.
892 * @throws IllegalArgumentException if {@code count < 0}.
893 * @throws IllegalArgumentException if {@code count < 1}
894 * and {@code type} is {@code TIFF_RATIONAL} or
895 * {@code TIFF_SRATIONAL}.
896 * @throws IllegalArgumentException if {@code count != 1}
897 * and {@code type} is {@code TIFF_IFD_POINTER}.
898 */
899 public static Object createArrayForType(int dataType, int count) {
900
901 if(count < 0) {
902 throw new IllegalArgumentException("count < 0!");
903 } else if ((dataType == TIFFTag.TIFF_RATIONAL
904 || dataType == TIFFTag.TIFF_SRATIONAL)
905 && count < 1) {
906 throw new IllegalArgumentException
907 ("Type is TIFF_RATIONAL or TIFF_SRATIONAL and count < 1");
908 } else if (dataType == TIFFTag.TIFF_IFD_POINTER && count != 1) {
909 throw new IllegalArgumentException
910 ("Type is TIFF_IFD_POINTER and count != 1");
911 }
912
913 switch (dataType) {
914 case TIFFTag.TIFF_BYTE:
915 case TIFFTag.TIFF_SBYTE:
916 case TIFFTag.TIFF_UNDEFINED:
917 return new byte[count];
918 case TIFFTag.TIFF_ASCII:
919 return new String[count];
920 case TIFFTag.TIFF_SHORT:
921 return new char[count];
922 case TIFFTag.TIFF_LONG:
923 case TIFFTag.TIFF_IFD_POINTER:
924 return new long[count];
925 case TIFFTag.TIFF_RATIONAL:
926 return new long[count][2];
927 case TIFFTag.TIFF_SSHORT:
928 return new short[count];
929 case TIFFTag.TIFF_SLONG:
930 return new int[count];
931 case TIFFTag.TIFF_SRATIONAL:
932 return new int[count][2];
|