Removed the libs directory containing win32 compiled versions of libpng, libtiff...
[openjpeg.git] / thirdparty / libtiff / tif_aux.c
1 /* $Id: tif_aux.c,v 1.20.2.3 2010-06-09 21:15:27 bfriesen 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 tdata_t
37 _TIFFCheckRealloc(TIFF* tif, tdata_t buffer,
38                   size_t nmemb, size_t elem_size, const char* what)
39 {
40         tdata_t cp = NULL;
41         tsize_t bytes = nmemb * elem_size;
42
43         /*
44          * XXX: Check for integer overflow.
45          */
46         if (nmemb && elem_size && bytes / elem_size == nmemb)
47                 cp = _TIFFrealloc(buffer, bytes);
48
49         if (cp == NULL)
50                 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
51                              "Failed to allocate memory for %s "
52                              "(%ld elements of %ld bytes each)",
53                              what,(long) nmemb, (long) elem_size);
54
55         return cp;
56 }
57
58 tdata_t
59 _TIFFCheckMalloc(TIFF* tif, size_t nmemb, size_t elem_size, const char* what)
60 {
61         return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);
62 }
63
64 static int
65 TIFFDefaultTransferFunction(TIFFDirectory* td)
66 {
67         uint16 **tf = td->td_transferfunction;
68         tsize_t i, n, nbytes;
69
70         tf[0] = tf[1] = tf[2] = 0;
71         if (td->td_bitspersample >= sizeof(tsize_t) * 8 - 2)
72                 return 0;
73
74         n = 1<<td->td_bitspersample;
75         nbytes = n * sizeof (uint16);
76         if (!(tf[0] = (uint16 *)_TIFFmalloc(nbytes)))
77                 return 0;
78         tf[0][0] = 0;
79         for (i = 1; i < n; i++) {
80                 double t = (double)i/((double) n-1.);
81                 tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5);
82         }
83
84         if (td->td_samplesperpixel - td->td_extrasamples > 1) {
85                 if (!(tf[1] = (uint16 *)_TIFFmalloc(nbytes)))
86                         goto bad;
87                 _TIFFmemcpy(tf[1], tf[0], nbytes);
88                 if (!(tf[2] = (uint16 *)_TIFFmalloc(nbytes)))
89                         goto bad;
90                 _TIFFmemcpy(tf[2], tf[0], nbytes);
91         }
92         return 1;
93
94 bad:
95         if (tf[0])
96                 _TIFFfree(tf[0]);
97         if (tf[1])
98                 _TIFFfree(tf[1]);
99         if (tf[2])
100                 _TIFFfree(tf[2]);
101         tf[0] = tf[1] = tf[2] = 0;
102         return 0;
103 }
104
105 static int
106 TIFFDefaultRefBlackWhite(TIFFDirectory* td)
107 {
108         int i;
109
110         if (!(td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float))))
111                 return 0;
112         if (td->td_photometric == PHOTOMETRIC_YCBCR) {
113                 /*
114                  * YCbCr (Class Y) images must have the ReferenceBlackWhite
115                  * tag set. Fix the broken images, which lacks that tag.
116                  */
117                 td->td_refblackwhite[0] = 0.0F;
118                 td->td_refblackwhite[1] = td->td_refblackwhite[3] =
119                         td->td_refblackwhite[5] = 255.0F;
120                 td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
121         } else {
122                 /*
123                  * Assume RGB (Class R)
124                  */
125                 for (i = 0; i < 3; i++) {
126                     td->td_refblackwhite[2*i+0] = 0;
127                     td->td_refblackwhite[2*i+1] =
128                             (float)((1L<<td->td_bitspersample)-1L);
129                 }
130         }
131         return 1;
132 }
133
134 /*
135  * Like TIFFGetField, but return any default
136  * value if the tag is not present in the directory.
137  *
138  * NB:  We use the value in the directory, rather than
139  *      explcit values so that defaults exist only one
140  *      place in the library -- in TIFFDefaultDirectory.
141  */
142 int
143 TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
144 {
145         TIFFDirectory *td = &tif->tif_dir;
146
147         if (TIFFVGetField(tif, tag, ap))
148                 return (1);
149         switch (tag) {
150         case TIFFTAG_SUBFILETYPE:
151                 *va_arg(ap, uint32 *) = td->td_subfiletype;
152                 return (1);
153         case TIFFTAG_BITSPERSAMPLE:
154                 *va_arg(ap, uint16 *) = td->td_bitspersample;
155                 return (1);
156         case TIFFTAG_THRESHHOLDING:
157                 *va_arg(ap, uint16 *) = td->td_threshholding;
158                 return (1);
159         case TIFFTAG_FILLORDER:
160                 *va_arg(ap, uint16 *) = td->td_fillorder;
161                 return (1);
162         case TIFFTAG_ORIENTATION:
163                 *va_arg(ap, uint16 *) = td->td_orientation;
164                 return (1);
165         case TIFFTAG_SAMPLESPERPIXEL:
166                 *va_arg(ap, uint16 *) = td->td_samplesperpixel;
167                 return (1);
168         case TIFFTAG_ROWSPERSTRIP:
169                 *va_arg(ap, uint32 *) = td->td_rowsperstrip;
170                 return (1);
171         case TIFFTAG_MINSAMPLEVALUE:
172                 *va_arg(ap, uint16 *) = td->td_minsamplevalue;
173                 return (1);
174         case TIFFTAG_MAXSAMPLEVALUE:
175                 *va_arg(ap, uint16 *) = td->td_maxsamplevalue;
176                 return (1);
177         case TIFFTAG_PLANARCONFIG:
178                 *va_arg(ap, uint16 *) = td->td_planarconfig;
179                 return (1);
180         case TIFFTAG_RESOLUTIONUNIT:
181                 *va_arg(ap, uint16 *) = td->td_resolutionunit;
182                 return (1);
183         case TIFFTAG_PREDICTOR:
184                 {
185                         TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
186                         *va_arg(ap, uint16*) = (uint16) sp->predictor;
187                         return 1;
188                 }
189         case TIFFTAG_DOTRANGE:
190                 *va_arg(ap, uint16 *) = 0;
191                 *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
192                 return (1);
193         case TIFFTAG_INKSET:
194                 *va_arg(ap, uint16 *) = INKSET_CMYK;
195                 return 1;
196         case TIFFTAG_NUMBEROFINKS:
197                 *va_arg(ap, uint16 *) = 4;
198                 return (1);
199         case TIFFTAG_EXTRASAMPLES:
200                 *va_arg(ap, uint16 *) = td->td_extrasamples;
201                 *va_arg(ap, uint16 **) = td->td_sampleinfo;
202                 return (1);
203         case TIFFTAG_MATTEING:
204                 *va_arg(ap, uint16 *) =
205                     (td->td_extrasamples == 1 &&
206                      td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
207                 return (1);
208         case TIFFTAG_TILEDEPTH:
209                 *va_arg(ap, uint32 *) = td->td_tiledepth;
210                 return (1);
211         case TIFFTAG_DATATYPE:
212                 *va_arg(ap, uint16 *) = td->td_sampleformat-1;
213                 return (1);
214         case TIFFTAG_SAMPLEFORMAT:
215                 *va_arg(ap, uint16 *) = td->td_sampleformat;
216                 return(1);
217         case TIFFTAG_IMAGEDEPTH:
218                 *va_arg(ap, uint32 *) = td->td_imagedepth;
219                 return (1);
220         case TIFFTAG_YCBCRCOEFFICIENTS:
221                 {
222                         /* defaults are from CCIR Recommendation 601-1 */
223                         static float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };
224                         *va_arg(ap, float **) = ycbcrcoeffs;
225                         return 1;
226                 }
227         case TIFFTAG_YCBCRSUBSAMPLING:
228                 *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
229                 *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
230                 return (1);
231         case TIFFTAG_YCBCRPOSITIONING:
232                 *va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
233                 return (1);
234         case TIFFTAG_WHITEPOINT:
235                 {
236                         static float whitepoint[2];
237
238                         /* TIFF 6.0 specification tells that it is no default
239                            value for the WhitePoint, but AdobePhotoshop TIFF
240                            Technical Note tells that it should be CIE D50. */
241                         whitepoint[0] = D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
242                         whitepoint[1] = D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
243                         *va_arg(ap, float **) = whitepoint;
244                         return 1;
245                 }
246         case TIFFTAG_TRANSFERFUNCTION:
247                 if (!td->td_transferfunction[0] &&
248                     !TIFFDefaultTransferFunction(td)) {
249                         TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \"TransferFunction\" tag");
250                         return (0);
251                 }
252                 *va_arg(ap, uint16 **) = td->td_transferfunction[0];
253                 if (td->td_samplesperpixel - td->td_extrasamples > 1) {
254                         *va_arg(ap, uint16 **) = td->td_transferfunction[1];
255                         *va_arg(ap, uint16 **) = td->td_transferfunction[2];
256                 }
257                 return (1);
258         case TIFFTAG_REFERENCEBLACKWHITE:
259                 if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
260                         return (0);
261                 *va_arg(ap, float **) = td->td_refblackwhite;
262                 return (1);
263         }
264         return 0;
265 }
266
267 /*
268  * Like TIFFGetField, but return any default
269  * value if the tag is not present in the directory.
270  */
271 int
272 TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...)
273 {
274         int ok;
275         va_list ap;
276
277         va_start(ap, tag);
278         ok =  TIFFVGetFieldDefaulted(tif, tag, ap);
279         va_end(ap);
280         return (ok);
281 }
282
283 /* vim: set ts=8 sts=8 sw=8 noet: */
284 /*
285  * Local Variables:
286  * mode: c
287  * c-basic-offset: 8
288  * fill-column: 78
289  * End:
290  */