1 | /* $Header: /afs/dev.mit.edu/source/repository/third/tiff/libtiff/tif_aux.c,v 1.1.1.1 2002-12-26 02:38:12 ghudson Exp $ */ |
---|
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 | |
---|
34 | #ifdef COLORIMETRY_SUPPORT |
---|
35 | #include <math.h> |
---|
36 | |
---|
37 | static void |
---|
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 |
---|
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)-1L); |
---|
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 | */ |
---|
78 | int |
---|
79 | TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap) |
---|
80 | { |
---|
81 | TIFFDirectory *td = &tif->tif_dir; |
---|
82 | |
---|
83 | if (TIFFVGetField(tif, tag, ap)) |
---|
84 | return (1); |
---|
85 | switch (tag) { |
---|
86 | case TIFFTAG_SUBFILETYPE: |
---|
87 | *va_arg(ap, uint32 *) = td->td_subfiletype; |
---|
88 | return (1); |
---|
89 | case TIFFTAG_BITSPERSAMPLE: |
---|
90 | *va_arg(ap, uint16 *) = td->td_bitspersample; |
---|
91 | return (1); |
---|
92 | case TIFFTAG_THRESHHOLDING: |
---|
93 | *va_arg(ap, uint16 *) = td->td_threshholding; |
---|
94 | return (1); |
---|
95 | case TIFFTAG_FILLORDER: |
---|
96 | *va_arg(ap, uint16 *) = td->td_fillorder; |
---|
97 | return (1); |
---|
98 | case TIFFTAG_ORIENTATION: |
---|
99 | *va_arg(ap, uint16 *) = td->td_orientation; |
---|
100 | return (1); |
---|
101 | case TIFFTAG_SAMPLESPERPIXEL: |
---|
102 | *va_arg(ap, uint16 *) = td->td_samplesperpixel; |
---|
103 | return (1); |
---|
104 | case TIFFTAG_ROWSPERSTRIP: |
---|
105 | *va_arg(ap, uint32 *) = td->td_rowsperstrip; |
---|
106 | return (1); |
---|
107 | case TIFFTAG_MINSAMPLEVALUE: |
---|
108 | *va_arg(ap, uint16 *) = td->td_minsamplevalue; |
---|
109 | return (1); |
---|
110 | case TIFFTAG_MAXSAMPLEVALUE: |
---|
111 | *va_arg(ap, uint16 *) = td->td_maxsamplevalue; |
---|
112 | return (1); |
---|
113 | case TIFFTAG_PLANARCONFIG: |
---|
114 | *va_arg(ap, uint16 *) = td->td_planarconfig; |
---|
115 | return (1); |
---|
116 | case TIFFTAG_RESOLUTIONUNIT: |
---|
117 | *va_arg(ap, uint16 *) = td->td_resolutionunit; |
---|
118 | return (1); |
---|
119 | #ifdef CMYK_SUPPORT |
---|
120 | case TIFFTAG_DOTRANGE: |
---|
121 | *va_arg(ap, uint16 *) = 0; |
---|
122 | *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1; |
---|
123 | return (1); |
---|
124 | case TIFFTAG_INKSET: |
---|
125 | *va_arg(ap, uint16 *) = td->td_inkset; |
---|
126 | return (1); |
---|
127 | case TIFFTAG_NUMBEROFINKS: |
---|
128 | *va_arg(ap, uint16 *) = td->td_ninks; |
---|
129 | return (1); |
---|
130 | #endif |
---|
131 | case TIFFTAG_EXTRASAMPLES: |
---|
132 | *va_arg(ap, uint16 *) = td->td_extrasamples; |
---|
133 | *va_arg(ap, uint16 **) = td->td_sampleinfo; |
---|
134 | return (1); |
---|
135 | case TIFFTAG_MATTEING: |
---|
136 | *va_arg(ap, uint16 *) = |
---|
137 | (td->td_extrasamples == 1 && |
---|
138 | td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA); |
---|
139 | return (1); |
---|
140 | case TIFFTAG_TILEDEPTH: |
---|
141 | *va_arg(ap, uint32 *) = td->td_tiledepth; |
---|
142 | return (1); |
---|
143 | case TIFFTAG_DATATYPE: |
---|
144 | *va_arg(ap, uint16 *) = td->td_sampleformat-1; |
---|
145 | return (1); |
---|
146 | case TIFFTAG_SAMPLEFORMAT: |
---|
147 | *va_arg(ap, uint16 *) = td->td_sampleformat; |
---|
148 | return(1); |
---|
149 | case TIFFTAG_IMAGEDEPTH: |
---|
150 | *va_arg(ap, uint32 *) = td->td_imagedepth; |
---|
151 | return (1); |
---|
152 | #ifdef YCBCR_SUPPORT |
---|
153 | case TIFFTAG_YCBCRCOEFFICIENTS: |
---|
154 | if (!td->td_ycbcrcoeffs) { |
---|
155 | td->td_ycbcrcoeffs = (float *) |
---|
156 | _TIFFmalloc(3*sizeof (float)); |
---|
157 | /* defaults are from CCIR Recommendation 601-1 */ |
---|
158 | td->td_ycbcrcoeffs[0] = 0.299f; |
---|
159 | td->td_ycbcrcoeffs[1] = 0.587f; |
---|
160 | td->td_ycbcrcoeffs[2] = 0.114f; |
---|
161 | } |
---|
162 | *va_arg(ap, float **) = td->td_ycbcrcoeffs; |
---|
163 | return (1); |
---|
164 | case TIFFTAG_YCBCRSUBSAMPLING: |
---|
165 | *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0]; |
---|
166 | *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1]; |
---|
167 | return (1); |
---|
168 | case TIFFTAG_YCBCRPOSITIONING: |
---|
169 | *va_arg(ap, uint16 *) = td->td_ycbcrpositioning; |
---|
170 | return (1); |
---|
171 | #endif |
---|
172 | #ifdef COLORIMETRY_SUPPORT |
---|
173 | case TIFFTAG_TRANSFERFUNCTION: |
---|
174 | if (!td->td_transferfunction[0]) |
---|
175 | TIFFDefaultTransferFunction(td); |
---|
176 | *va_arg(ap, uint16 **) = td->td_transferfunction[0]; |
---|
177 | if (td->td_samplesperpixel - td->td_extrasamples > 1) { |
---|
178 | *va_arg(ap, uint16 **) = td->td_transferfunction[1]; |
---|
179 | *va_arg(ap, uint16 **) = td->td_transferfunction[2]; |
---|
180 | } |
---|
181 | return (1); |
---|
182 | case TIFFTAG_REFERENCEBLACKWHITE: |
---|
183 | if (!td->td_refblackwhite) |
---|
184 | TIFFDefaultRefBlackWhite(td); |
---|
185 | *va_arg(ap, float **) = td->td_refblackwhite; |
---|
186 | return (1); |
---|
187 | #endif |
---|
188 | } |
---|
189 | return (0); |
---|
190 | } |
---|
191 | |
---|
192 | /* |
---|
193 | * Like TIFFGetField, but return any default |
---|
194 | * value if the tag is not present in the directory. |
---|
195 | */ |
---|
196 | int |
---|
197 | TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...) |
---|
198 | { |
---|
199 | int ok; |
---|
200 | va_list ap; |
---|
201 | |
---|
202 | va_start(ap, tag); |
---|
203 | ok = TIFFVGetFieldDefaulted(tif, tag, ap); |
---|
204 | va_end(ap); |
---|
205 | return (ok); |
---|
206 | } |
---|