1 /* $Header$ */
2 
3 /*
4  * Copyright (c) 1991-1997 Sam Leffler
5  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and
8  * its documentation for any purpose is hereby granted without fee, provided
9  * that (i) the above copyright notices and this permission notice appear in
10  * all copies of the software and related documentation, and (ii) the names of
11  * Sam Leffler and Silicon Graphics may not be used in any advertising or
12  * publicity relating to the software without the specific, prior written
13  * permission of Sam Leffler and Silicon Graphics.
14  *
15  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  */
26 
27 /*
28  * TIFF Library.
29  *
30  * Auxiliary Support Routines.
31  */
32 #include "tiffiop.h"
33 #include "tif_predict.h"
34 #include <math.h>
35 
36 static int
TIFFDefaultTransferFunction(TIFFDirectory * td)37 TIFFDefaultTransferFunction(TIFFDirectory* td)
38 {
39 	uint16 **tf = td->td_transferfunction;
40 	tsize_t i, n, nbytes;
41 
42 	tf[0] = tf[1] = tf[2] = 0;
43 	if (td->td_bitspersample >= sizeof(tsize_t) * 8 - 2)
44 		return 0;
45 
46 	n = 1<<td->td_bitspersample;
47 	nbytes = n * sizeof (uint16);
48 	if (!(tf[0] = (uint16 *)_TIFFmalloc(nbytes)))
49 		return 0;
50 	tf[0][0] = 0;
51 	for (i = 1; i < n; i++) {
52 		double t = (double)i/((double) n-1.);
53 		tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5);
54 	}
55 
56 	if (td->td_samplesperpixel - td->td_extrasamples > 1) {
57 		if (!(tf[1] = (uint16 *)_TIFFmalloc(nbytes)))
58 			goto bad;
59 		_TIFFmemcpy(tf[1], tf[0], nbytes);
60 		if (!(tf[2] = (uint16 *)_TIFFmalloc(nbytes)))
61 			goto bad;
62 		_TIFFmemcpy(tf[2], tf[0], nbytes);
63 	}
64 	return 1;
65 
66 bad:
67 	if (tf[0])
68 		_TIFFfree(tf[0]);
69 	if (tf[1])
70 		_TIFFfree(tf[1]);
71 	if (tf[2])
72 		_TIFFfree(tf[2]);
73 	tf[0] = tf[1] = tf[2] = 0;
74 	return 0;
75 }
76 
77 static int
TIFFDefaultRefBlackWhite(TIFFDirectory * td)78 TIFFDefaultRefBlackWhite(TIFFDirectory* td)
79 {
80 	int i;
81 
82 	if (!(td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float))))
83 		return 0;
84 	for (i = 0; i < 3; i++) {
85 	    td->td_refblackwhite[2*i+0] = 0;
86 	    td->td_refblackwhite[2*i+1] = (float)((1L<<td->td_bitspersample)-1L);
87 	}
88 	return 1;
89 }
90 
91 /*
92  * Like TIFFGetField, but return any default
93  * value if the tag is not present in the directory.
94  *
95  * NB:	We use the value in the directory, rather than
96  *	explcit values so that defaults exist only one
97  *	place in the library -- in TIFFDefaultDirectory.
98  */
99 int
TIFFVGetFieldDefaulted(TIFF * tif,ttag_t tag,va_list ap)100 TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
101 {
102 	TIFFDirectory *td = &tif->tif_dir;
103 
104 	if (TIFFVGetField(tif, tag, ap))
105 		return (1);
106 	switch (tag) {
107 	case TIFFTAG_SUBFILETYPE:
108 		*va_arg(ap, uint32 *) = td->td_subfiletype;
109 		return (1);
110 	case TIFFTAG_BITSPERSAMPLE:
111 		*va_arg(ap, uint16 *) = td->td_bitspersample;
112 		return (1);
113 	case TIFFTAG_THRESHHOLDING:
114 		*va_arg(ap, uint16 *) = td->td_threshholding;
115 		return (1);
116 	case TIFFTAG_FILLORDER:
117 		*va_arg(ap, uint16 *) = td->td_fillorder;
118 		return (1);
119 	case TIFFTAG_ORIENTATION:
120 		*va_arg(ap, uint16 *) = td->td_orientation;
121 		return (1);
122 	case TIFFTAG_SAMPLESPERPIXEL:
123 		*va_arg(ap, uint16 *) = td->td_samplesperpixel;
124 		return (1);
125 	case TIFFTAG_ROWSPERSTRIP:
126 		*va_arg(ap, uint32 *) = td->td_rowsperstrip;
127 		return (1);
128 	case TIFFTAG_MINSAMPLEVALUE:
129 		*va_arg(ap, uint16 *) = td->td_minsamplevalue;
130 		return (1);
131 	case TIFFTAG_MAXSAMPLEVALUE:
132 		*va_arg(ap, uint16 *) = td->td_maxsamplevalue;
133 		return (1);
134 	case TIFFTAG_PLANARCONFIG:
135 		*va_arg(ap, uint16 *) = td->td_planarconfig;
136 		return (1);
137 	case TIFFTAG_RESOLUTIONUNIT:
138 		*va_arg(ap, uint16 *) = td->td_resolutionunit;
139 		return (1);
140 	case TIFFTAG_PREDICTOR:
141                 {
142 			TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
143 			*va_arg(ap, uint16*) = (uint16) sp->predictor;
144 			return (1);
145                 }
146 	case TIFFTAG_DOTRANGE:
147 		*va_arg(ap, uint16 *) = 0;
148 		*va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
149 		return (1);
150 	case TIFFTAG_INKSET:
151 		*va_arg(ap, uint16 *) = td->td_inkset;
152 		return (1);
153 	case TIFFTAG_NUMBEROFINKS:
154 		*va_arg(ap, uint16 *) = td->td_ninks;
155 		return (1);
156 	case TIFFTAG_EXTRASAMPLES:
157 		*va_arg(ap, uint16 *) = td->td_extrasamples;
158 		*va_arg(ap, uint16 **) = td->td_sampleinfo;
159 		return (1);
160 	case TIFFTAG_MATTEING:
161 		*va_arg(ap, uint16 *) =
162 		    (td->td_extrasamples == 1 &&
163 		     td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
164 		return (1);
165 	case TIFFTAG_TILEDEPTH:
166 		*va_arg(ap, uint32 *) = td->td_tiledepth;
167 		return (1);
168 	case TIFFTAG_DATATYPE:
169 		*va_arg(ap, uint16 *) = td->td_sampleformat-1;
170 		return (1);
171 	case TIFFTAG_SAMPLEFORMAT:
172 		*va_arg(ap, uint16 *) = td->td_sampleformat;
173                 return(1);
174 	case TIFFTAG_IMAGEDEPTH:
175 		*va_arg(ap, uint32 *) = td->td_imagedepth;
176 		return (1);
177 	case TIFFTAG_YCBCRCOEFFICIENTS:
178 		if (!td->td_ycbcrcoeffs) {
179 			td->td_ycbcrcoeffs = (float *)
180 			    _TIFFmalloc(3*sizeof (float));
181 			if (!td->td_ycbcrcoeffs)
182 				return (0);
183 			/* defaults are from CCIR Recommendation 601-1 */
184 			td->td_ycbcrcoeffs[0] = 0.299f;
185 			td->td_ycbcrcoeffs[1] = 0.587f;
186 			td->td_ycbcrcoeffs[2] = 0.114f;
187 		}
188 		*va_arg(ap, float **) = td->td_ycbcrcoeffs;
189 		return (1);
190 	case TIFFTAG_YCBCRSUBSAMPLING:
191 		*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
192 		*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
193 		return (1);
194 	case TIFFTAG_YCBCRPOSITIONING:
195 		*va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
196 		return (1);
197 	case TIFFTAG_WHITEPOINT:
198 		if (!td->td_whitepoint) {
199 			td->td_whitepoint = (float *)
200 				_TIFFmalloc(2 * sizeof (float));
201 			if (!td->td_whitepoint)
202 				return (0);
203 			/* TIFF 6.0 specification says that it is no default
204 			   value for the WhitePoint, but AdobePhotoshop TIFF
205 			   Technical Note tells that it should be CIE D50. */
206 			td->td_whitepoint[0] =
207 				D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
208 			td->td_whitepoint[1] =
209 				D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
210 		}
211 		*va_arg(ap, float **) = td->td_whitepoint;
212 		return (1);
213 	case TIFFTAG_TRANSFERFUNCTION:
214 		if (!td->td_transferfunction[0] &&
215 		    !TIFFDefaultTransferFunction(td)) {
216 			TIFFError(tif->tif_name, "No space for \"TransferFunction\" tag");
217 			return (0);
218 		}
219 		*va_arg(ap, uint16 **) = td->td_transferfunction[0];
220 		if (td->td_samplesperpixel - td->td_extrasamples > 1) {
221 			*va_arg(ap, uint16 **) = td->td_transferfunction[1];
222 			*va_arg(ap, uint16 **) = td->td_transferfunction[2];
223 		}
224 		return (1);
225 	case TIFFTAG_REFERENCEBLACKWHITE:
226 		if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
227 			return (0);
228 		*va_arg(ap, float **) = td->td_refblackwhite;
229 		return (1);
230 	}
231 	return (0);
232 }
233 
234 /*
235  * Like TIFFGetField, but return any default
236  * value if the tag is not present in the directory.
237  */
238 int
TIFFGetFieldDefaulted(TIFF * tif,ttag_t tag,...)239 TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...)
240 {
241 	int ok;
242 	va_list ap;
243 
244 	va_start(ap, tag);
245 	ok =  TIFFVGetFieldDefaulted(tif, tag, ap);
246 	va_end(ap);
247 	return (ok);
248 }
249