Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / libtiff / tif_aux.c
1 /* $Id: tif_aux.c,v 1.1 2005-06-17 13:54:52 vp153 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 #include "tif_predict.h"
34 #include <math.h>
35
36 static int
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
78 TIFFDefaultRefBlackWhite(TIFFDirectory* td)
79 {
80         int i;
81
82         if (!(td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float))))
83                 return 0;
84         if (td->td_photometric == PHOTOMETRIC_YCBCR) {
85                 /*
86                  * YCbCr (Class Y) images must have the ReferenceBlackWhite
87                  * tag set. Fix the broken images, which lacks that tag.
88                  */
89                 td->td_refblackwhite[0] = 0.0F;
90                 td->td_refblackwhite[1] = td->td_refblackwhite[3] =
91                         td->td_refblackwhite[5] = 255.0F;
92                 td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
93         } else {
94                 /*
95                  * Assume RGB (Class R)
96                  */
97                 for (i = 0; i < 3; i++) {
98                     td->td_refblackwhite[2*i+0] = 0;
99                     td->td_refblackwhite[2*i+1] =
100                             (float)((1L<<td->td_bitspersample)-1L);
101                 }
102         }
103         return 1;
104 }
105
106 /*
107  * Like TIFFGetField, but return any default
108  * value if the tag is not present in the directory.
109  *
110  * NB:  We use the value in the directory, rather than
111  *      explcit values so that defaults exist only one
112  *      place in the library -- in TIFFDefaultDirectory.
113  */
114 int
115 TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
116 {
117         TIFFDirectory *td = &tif->tif_dir;
118
119         if (TIFFVGetField(tif, tag, ap))
120                 return (1);
121         switch (tag) {
122         case TIFFTAG_SUBFILETYPE:
123                 *va_arg(ap, uint32 *) = td->td_subfiletype;
124                 return (1);
125         case TIFFTAG_BITSPERSAMPLE:
126                 *va_arg(ap, uint16 *) = td->td_bitspersample;
127                 return (1);
128         case TIFFTAG_THRESHHOLDING:
129                 *va_arg(ap, uint16 *) = td->td_threshholding;
130                 return (1);
131         case TIFFTAG_FILLORDER:
132                 *va_arg(ap, uint16 *) = td->td_fillorder;
133                 return (1);
134         case TIFFTAG_ORIENTATION:
135                 *va_arg(ap, uint16 *) = td->td_orientation;
136                 return (1);
137         case TIFFTAG_SAMPLESPERPIXEL:
138                 *va_arg(ap, uint16 *) = td->td_samplesperpixel;
139                 return (1);
140         case TIFFTAG_ROWSPERSTRIP:
141                 *va_arg(ap, uint32 *) = td->td_rowsperstrip;
142                 return (1);
143         case TIFFTAG_MINSAMPLEVALUE:
144                 *va_arg(ap, uint16 *) = td->td_minsamplevalue;
145                 return (1);
146         case TIFFTAG_MAXSAMPLEVALUE:
147                 *va_arg(ap, uint16 *) = td->td_maxsamplevalue;
148                 return (1);
149         case TIFFTAG_PLANARCONFIG:
150                 *va_arg(ap, uint16 *) = td->td_planarconfig;
151                 return (1);
152         case TIFFTAG_RESOLUTIONUNIT:
153                 *va_arg(ap, uint16 *) = td->td_resolutionunit;
154                 return (1);
155         case TIFFTAG_PREDICTOR:
156                 {
157                         TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
158                         *va_arg(ap, uint16*) = (uint16) sp->predictor;
159                         return (1);
160                 }
161         case TIFFTAG_DOTRANGE:
162                 *va_arg(ap, uint16 *) = 0;
163                 *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
164                 return (1);
165         case TIFFTAG_INKSET:
166                 *va_arg(ap, uint16 *) = td->td_inkset;
167                 return (1);
168         case TIFFTAG_NUMBEROFINKS:
169                 *va_arg(ap, uint16 *) = td->td_ninks;
170                 return (1);
171         case TIFFTAG_EXTRASAMPLES:
172                 *va_arg(ap, uint16 *) = td->td_extrasamples;
173                 *va_arg(ap, uint16 **) = td->td_sampleinfo;
174                 return (1);
175         case TIFFTAG_MATTEING:
176                 *va_arg(ap, uint16 *) =
177                     (td->td_extrasamples == 1 &&
178                      td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
179                 return (1);
180         case TIFFTAG_TILEDEPTH:
181                 *va_arg(ap, uint32 *) = td->td_tiledepth;
182                 return (1);
183         case TIFFTAG_DATATYPE:
184                 *va_arg(ap, uint16 *) = td->td_sampleformat-1;
185                 return (1);
186         case TIFFTAG_SAMPLEFORMAT:
187                 *va_arg(ap, uint16 *) = td->td_sampleformat;
188                 return(1);
189         case TIFFTAG_IMAGEDEPTH:
190                 *va_arg(ap, uint32 *) = td->td_imagedepth;
191                 return (1);
192         case TIFFTAG_YCBCRCOEFFICIENTS:
193                 if (!td->td_ycbcrcoeffs) {
194                         td->td_ycbcrcoeffs = (float *)
195                             _TIFFmalloc(3*sizeof (float));
196                         if (!td->td_ycbcrcoeffs)
197                                 return (0);
198                         /* defaults are from CCIR Recommendation 601-1 */
199                         td->td_ycbcrcoeffs[0] = 0.299f;
200                         td->td_ycbcrcoeffs[1] = 0.587f;
201                         td->td_ycbcrcoeffs[2] = 0.114f;
202                 }
203                 *va_arg(ap, float **) = td->td_ycbcrcoeffs;
204                 return (1);
205         case TIFFTAG_YCBCRSUBSAMPLING:
206                 *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
207                 *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
208                 return (1);
209         case TIFFTAG_YCBCRPOSITIONING:
210                 *va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
211                 return (1);
212         case TIFFTAG_WHITEPOINT:
213                 if (!td->td_whitepoint) {
214                         td->td_whitepoint = (float *)
215                                 _TIFFmalloc(2 * sizeof (float));
216                         if (!td->td_whitepoint)
217                                 return (0);
218                         /* TIFF 6.0 specification says that it is no default
219                            value for the WhitePoint, but AdobePhotoshop TIFF
220                            Technical Note tells that it should be CIE D50. */
221                         td->td_whitepoint[0] =
222                                 D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
223                         td->td_whitepoint[1] =
224                                 D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
225                 }
226                 *va_arg(ap, float **) = td->td_whitepoint;
227                 return (1);
228         case TIFFTAG_TRANSFERFUNCTION:
229                 if (!td->td_transferfunction[0] &&
230                     !TIFFDefaultTransferFunction(td)) {
231                         TIFFError(tif->tif_name, "No space for \"TransferFunction\" tag");
232                         return (0);
233                 }
234                 *va_arg(ap, uint16 **) = td->td_transferfunction[0];
235                 if (td->td_samplesperpixel - td->td_extrasamples > 1) {
236                         *va_arg(ap, uint16 **) = td->td_transferfunction[1];
237                         *va_arg(ap, uint16 **) = td->td_transferfunction[2];
238                 }
239                 return (1);
240         case TIFFTAG_REFERENCEBLACKWHITE:
241                 if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
242                         return (0);
243                 *va_arg(ap, float **) = td->td_refblackwhite;
244                 return (1);
245         }
246         return (0);
247 }
248
249 /*
250  * Like TIFFGetField, but return any default
251  * value if the tag is not present in the directory.
252  */
253 int
254 TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...)
255 {
256         int ok;
257         va_list ap;
258
259         va_start(ap, tag);
260         ok =  TIFFVGetFieldDefaulted(tif, tag, ap);
261         va_end(ap);
262         return (ok);
263 }
264
265 /* vim: set ts=8 sts=8 sw=8 noet: */