1 /* $Header: /usr/people/sam/tiff/libtiff/RCS/tif_aux.c,v 1.25 1994/09/28 00:54:41 sam Exp $ */
2 
3 /*
4  * Copyright (c) 1991, 1992, 1993, 1994 Sam Leffler
5  * Copyright (c) 1991, 1992, 1993, 1994 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 
34 #ifdef COLORIMETRY_SUPPORT
35 #include <math.h>
36 
37 static void
TIFFDefaultTransferFunction(TIFFDirectory * td)38 TIFFDefaultTransferFunction(TIFFDirectory* td)
39 {
40 	uint16 **tf = td->td_transferfunction;
41 	long i, n = 1<<td->td_bitspersample;
42 
43 	tf[0] = (uint16 *)_TIFFmalloc(n * sizeof (uint16));
44 	tf[0][0] = 0;
45 	for (i = 1; i < n; i++) {
46 		double t = (double)i/((double) n-1.);
47 		tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5);
48 	}
49 	if (td->td_samplesperpixel - td->td_extrasamples > 1) {
50 		tf[1] = (uint16 *)_TIFFmalloc(n * sizeof (uint16));
51 		_TIFFmemcpy(tf[1], tf[0], n * sizeof (uint16));
52 		tf[2] = (uint16 *)_TIFFmalloc(n * sizeof (uint16));
53 		_TIFFmemcpy(tf[2], tf[0], n * sizeof (uint16));
54 	}
55 }
56 
57 static void
TIFFDefaultRefBlackWhite(TIFFDirectory * td)58 TIFFDefaultRefBlackWhite(TIFFDirectory* td)
59 {
60 	int i;
61 
62 	td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float));
63 	for (i = 0; i < 3; i++) {
64 	    td->td_refblackwhite[2*i+0] = 0;
65 	    td->td_refblackwhite[2*i+1] = (float)(1L<<td->td_bitspersample);
66 	}
67 }
68 #endif
69 
70 /*
71  * Like TIFFGetField, but return any default
72  * value if the tag is not present in the directory.
73  *
74  * NB:	We use the value in the directory, rather than
75  *	explcit values so that defaults exist only one
76  *	place in the library -- in TIFFDefaultDirectory.
77  */
TIFFVGetFieldDefaulted(TIFF * tif,ttag_t tag,va_list ap)78 TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
79 {
80 	TIFFDirectory *td = &tif->tif_dir;
81 
82 	if (TIFFVGetField(tif, tag, ap))
83 		return (1);
84 	switch (tag) {
85 	case TIFFTAG_SUBFILETYPE:
86 		*va_arg(ap, uint32 *) = td->td_subfiletype;
87 		return (1);
88 	case TIFFTAG_BITSPERSAMPLE:
89 		*va_arg(ap, uint16 *) = td->td_bitspersample;
90 		return (1);
91 	case TIFFTAG_THRESHHOLDING:
92 		*va_arg(ap, uint16 *) = td->td_threshholding;
93 		return (1);
94 	case TIFFTAG_FILLORDER:
95 		*va_arg(ap, uint16 *) = td->td_fillorder;
96 		return (1);
97 	case TIFFTAG_ORIENTATION:
98 		*va_arg(ap, uint16 *) = td->td_orientation;
99 		return (1);
100 	case TIFFTAG_SAMPLESPERPIXEL:
101 		*va_arg(ap, uint16 *) = td->td_samplesperpixel;
102 		return (1);
103 	case TIFFTAG_ROWSPERSTRIP:
104 		*va_arg(ap, uint32 *) = td->td_rowsperstrip;
105 		return (1);
106 	case TIFFTAG_MINSAMPLEVALUE:
107 		*va_arg(ap, uint16 *) = td->td_minsamplevalue;
108 		return (1);
109 	case TIFFTAG_MAXSAMPLEVALUE:
110 		*va_arg(ap, uint16 *) = td->td_maxsamplevalue;
111 		return (1);
112 	case TIFFTAG_PLANARCONFIG:
113 		*va_arg(ap, uint16 *) = td->td_planarconfig;
114 		return (1);
115 	case TIFFTAG_GROUP4OPTIONS:
116 		*va_arg(ap, uint32 *) = td->td_group4options;
117 		return (1);
118 	case TIFFTAG_RESOLUTIONUNIT:
119 		*va_arg(ap, uint16 *) = td->td_resolutionunit;
120 		return (1);
121 	case TIFFTAG_PREDICTOR:
122 		*va_arg(ap, uint16 *) = td->td_predictor;
123 		return (1);
124 #ifdef CMYK_SUPPORT
125 	case TIFFTAG_DOTRANGE:
126 		*va_arg(ap, uint16 *) = 0;
127 		*va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
128 		return (1);
129 	case TIFFTAG_INKSET:
130 		*va_arg(ap, uint16 *) = td->td_inkset;
131 		return (1);
132 #endif
133 	case TIFFTAG_EXTRASAMPLES:
134 		*va_arg(ap, uint16 *) = td->td_extrasamples;
135 		*va_arg(ap, uint16 **) = td->td_sampleinfo;
136 		return (1);
137 	case TIFFTAG_MATTEING:
138 		*va_arg(ap, uint16 *) =
139 		    (td->td_extrasamples == 1 &&
140 		     td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
141 		return (1);
142 	case TIFFTAG_TILEDEPTH:
143 		*va_arg(ap, uint32 *) = td->td_tiledepth;
144 		return (1);
145 	case TIFFTAG_DATATYPE:
146 		*va_arg(ap, uint16 *) = td->td_sampleformat-1;
147 		return (1);
148 	case TIFFTAG_IMAGEDEPTH:
149 		*va_arg(ap, uint16 *) = td->td_imagedepth;
150 		return (1);
151 #ifdef YCBCR_SUPPORT
152 	case TIFFTAG_YCBCRCOEFFICIENTS:
153 		if (!td->td_ycbcrcoeffs) {
154 			td->td_ycbcrcoeffs = (float *)
155 			    _TIFFmalloc(3*sizeof (float));
156 			/* defaults are from CCIR Recommendation 601-1 */
157 			td->td_ycbcrcoeffs[0] = 0.299f;
158 			td->td_ycbcrcoeffs[1] = 0.587f;
159 			td->td_ycbcrcoeffs[2] = 0.114f;
160 		}
161 		*va_arg(ap, float **) = td->td_ycbcrcoeffs;
162 		return (1);
163 	case TIFFTAG_YCBCRSUBSAMPLING:
164 		*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
165 		*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
166 		return (1);
167 	case TIFFTAG_YCBCRPOSITIONING:
168 		*va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
169 		return (1);
170 #endif
171 #ifdef COLORIMETRY_SUPPORT
172 	case TIFFTAG_TRANSFERFUNCTION:
173 		if (!td->td_transferfunction[0])
174 			TIFFDefaultTransferFunction(td);
175 		*va_arg(ap, uint16 **) = td->td_transferfunction[0];
176 		if (td->td_samplesperpixel - td->td_extrasamples > 1) {
177 			*va_arg(ap, uint16 **) = td->td_transferfunction[1];
178 			*va_arg(ap, uint16 **) = td->td_transferfunction[2];
179 		}
180 		return (1);
181 	case TIFFTAG_REFERENCEBLACKWHITE:
182 		if (!td->td_refblackwhite)
183 			TIFFDefaultRefBlackWhite(td);
184 		*va_arg(ap, float **) = td->td_refblackwhite;
185 		return (1);
186 #endif
187 	}
188 	return (0);
189 }
190 
191 /*
192  * Like TIFFGetField, but return any default
193  * value if the tag is not present in the directory.
194  */
TIFFGetFieldDefaulted(TIFF * tif,ttag_t tag,...)195 TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...)
196 {
197 	int ok;
198 	va_list ap;
199 
200 	va_start(ap, tag);
201 	ok =  TIFFVGetFieldDefaulted(tif, tag, ap);
202 	va_end(ap);
203 	return (ok);
204 }
205