Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygentif_aux.c
Go to the documentation of this file.
00001 /* $Id: tif_aux.c,v 1.20.2.3 2010-06-09 21:15:27 bfriesen Exp $ */ 00002 00003 /* 00004 * Copyright (c) 1991-1997 Sam Leffler 00005 * Copyright (c) 1991-1997 Silicon Graphics, Inc. 00006 * 00007 * Permission to use, copy, modify, distribute, and sell this software and 00008 * its documentation for any purpose is hereby granted without fee, provided 00009 * that (i) the above copyright notices and this permission notice appear in 00010 * all copies of the software and related documentation, and (ii) the names of 00011 * Sam Leffler and Silicon Graphics may not be used in any advertising or 00012 * publicity relating to the software without the specific, prior written 00013 * permission of Sam Leffler and Silicon Graphics. 00014 * 00015 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 00016 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 00017 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 00018 * 00019 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR 00020 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, 00021 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 00022 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 00023 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 00024 * OF THIS SOFTWARE. 00025 */ 00026 00027 /* 00028 * TIFF Library. 00029 * 00030 * Auxiliary Support Routines. 00031 */ 00032 #include "tiffiop.h" 00033 #include "tif_predict.h" 00034 #include <math.h> 00035 00036 tdata_t 00037 _TIFFCheckRealloc(TIFF* tif, tdata_t buffer, 00038 size_t nmemb, size_t elem_size, const char* what) 00039 { 00040 tdata_t cp = NULL; 00041 tsize_t bytes = nmemb * elem_size; 00042 00043 /* 00044 * XXX: Check for integer overflow. 00045 */ 00046 if (nmemb && elem_size && bytes / elem_size == nmemb) 00047 cp = _TIFFrealloc(buffer, bytes); 00048 00049 if (cp == NULL) 00050 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 00051 "Failed to allocate memory for %s " 00052 "(%ld elements of %ld bytes each)", 00053 what,(long) nmemb, (long) elem_size); 00054 00055 return cp; 00056 } 00057 00058 tdata_t 00059 _TIFFCheckMalloc(TIFF* tif, size_t nmemb, size_t elem_size, const char* what) 00060 { 00061 return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what); 00062 } 00063 00064 static int 00065 TIFFDefaultTransferFunction(TIFFDirectory* td) 00066 { 00067 uint16 **tf = td->td_transferfunction; 00068 tsize_t i, n, nbytes; 00069 00070 tf[0] = tf[1] = tf[2] = 0; 00071 if (td->td_bitspersample >= sizeof(tsize_t) * 8 - 2) 00072 return 0; 00073 00074 n = 1<<td->td_bitspersample; 00075 nbytes = n * sizeof (uint16); 00076 if (!(tf[0] = (uint16 *)_TIFFmalloc(nbytes))) 00077 return 0; 00078 tf[0][0] = 0; 00079 for (i = 1; i < n; i++) { 00080 double t = (double)i/((double) n-1.); 00081 tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5); 00082 } 00083 00084 if (td->td_samplesperpixel - td->td_extrasamples > 1) { 00085 if (!(tf[1] = (uint16 *)_TIFFmalloc(nbytes))) 00086 goto bad; 00087 _TIFFmemcpy(tf[1], tf[0], nbytes); 00088 if (!(tf[2] = (uint16 *)_TIFFmalloc(nbytes))) 00089 goto bad; 00090 _TIFFmemcpy(tf[2], tf[0], nbytes); 00091 } 00092 return 1; 00093 00094 bad: 00095 if (tf[0]) 00096 _TIFFfree(tf[0]); 00097 if (tf[1]) 00098 _TIFFfree(tf[1]); 00099 if (tf[2]) 00100 _TIFFfree(tf[2]); 00101 tf[0] = tf[1] = tf[2] = 0; 00102 return 0; 00103 } 00104 00105 static int 00106 TIFFDefaultRefBlackWhite(TIFFDirectory* td) 00107 { 00108 int i; 00109 00110 if (!(td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float)))) 00111 return 0; 00112 if (td->td_photometric == PHOTOMETRIC_YCBCR) { 00113 /* 00114 * YCbCr (Class Y) images must have the ReferenceBlackWhite 00115 * tag set. Fix the broken images, which lacks that tag. 00116 */ 00117 td->td_refblackwhite[0] = 0.0F; 00118 td->td_refblackwhite[1] = td->td_refblackwhite[3] = 00119 td->td_refblackwhite[5] = 255.0F; 00120 td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F; 00121 } else { 00122 /* 00123 * Assume RGB (Class R) 00124 */ 00125 for (i = 0; i < 3; i++) { 00126 td->td_refblackwhite[2*i+0] = 0; 00127 td->td_refblackwhite[2*i+1] = 00128 (float)((1L<<td->td_bitspersample)-1L); 00129 } 00130 } 00131 return 1; 00132 } 00133 00134 /* 00135 * Like TIFFGetField, but return any default 00136 * value if the tag is not present in the directory. 00137 * 00138 * NB: We use the value in the directory, rather than 00139 * explcit values so that defaults exist only one 00140 * place in the library -- in TIFFDefaultDirectory. 00141 */ 00142 int 00143 TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap) 00144 { 00145 TIFFDirectory *td = &tif->tif_dir; 00146 00147 if (TIFFVGetField(tif, tag, ap)) 00148 return (1); 00149 switch (tag) { 00150 case TIFFTAG_SUBFILETYPE: 00151 *va_arg(ap, uint32 *) = td->td_subfiletype; 00152 return (1); 00153 case TIFFTAG_BITSPERSAMPLE: 00154 *va_arg(ap, uint16 *) = td->td_bitspersample; 00155 return (1); 00156 case TIFFTAG_THRESHHOLDING: 00157 *va_arg(ap, uint16 *) = td->td_threshholding; 00158 return (1); 00159 case TIFFTAG_FILLORDER: 00160 *va_arg(ap, uint16 *) = td->td_fillorder; 00161 return (1); 00162 case TIFFTAG_ORIENTATION: 00163 *va_arg(ap, uint16 *) = td->td_orientation; 00164 return (1); 00165 case TIFFTAG_SAMPLESPERPIXEL: 00166 *va_arg(ap, uint16 *) = td->td_samplesperpixel; 00167 return (1); 00168 case TIFFTAG_ROWSPERSTRIP: 00169 *va_arg(ap, uint32 *) = td->td_rowsperstrip; 00170 return (1); 00171 case TIFFTAG_MINSAMPLEVALUE: 00172 *va_arg(ap, uint16 *) = td->td_minsamplevalue; 00173 return (1); 00174 case TIFFTAG_MAXSAMPLEVALUE: 00175 *va_arg(ap, uint16 *) = td->td_maxsamplevalue; 00176 return (1); 00177 case TIFFTAG_PLANARCONFIG: 00178 *va_arg(ap, uint16 *) = td->td_planarconfig; 00179 return (1); 00180 case TIFFTAG_RESOLUTIONUNIT: 00181 *va_arg(ap, uint16 *) = td->td_resolutionunit; 00182 return (1); 00183 case TIFFTAG_PREDICTOR: 00184 { 00185 TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data; 00186 *va_arg(ap, uint16*) = (uint16) sp->predictor; 00187 return 1; 00188 } 00189 case TIFFTAG_DOTRANGE: 00190 *va_arg(ap, uint16 *) = 0; 00191 *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1; 00192 return (1); 00193 case TIFFTAG_INKSET: 00194 *va_arg(ap, uint16 *) = INKSET_CMYK; 00195 return 1; 00196 case TIFFTAG_NUMBEROFINKS: 00197 *va_arg(ap, uint16 *) = 4; 00198 return (1); 00199 case TIFFTAG_EXTRASAMPLES: 00200 *va_arg(ap, uint16 *) = td->td_extrasamples; 00201 *va_arg(ap, uint16 **) = td->td_sampleinfo; 00202 return (1); 00203 case TIFFTAG_MATTEING: 00204 *va_arg(ap, uint16 *) = 00205 (td->td_extrasamples == 1 && 00206 td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA); 00207 return (1); 00208 case TIFFTAG_TILEDEPTH: 00209 *va_arg(ap, uint32 *) = td->td_tiledepth; 00210 return (1); 00211 case TIFFTAG_DATATYPE: 00212 *va_arg(ap, uint16 *) = td->td_sampleformat-1; 00213 return (1); 00214 case TIFFTAG_SAMPLEFORMAT: 00215 *va_arg(ap, uint16 *) = td->td_sampleformat; 00216 return(1); 00217 case TIFFTAG_IMAGEDEPTH: 00218 *va_arg(ap, uint32 *) = td->td_imagedepth; 00219 return (1); 00220 case TIFFTAG_YCBCRCOEFFICIENTS: 00221 { 00222 /* defaults are from CCIR Recommendation 601-1 */ 00223 static float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f }; 00224 *va_arg(ap, float **) = ycbcrcoeffs; 00225 return 1; 00226 } 00227 case TIFFTAG_YCBCRSUBSAMPLING: 00228 *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0]; 00229 *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1]; 00230 return (1); 00231 case TIFFTAG_YCBCRPOSITIONING: 00232 *va_arg(ap, uint16 *) = td->td_ycbcrpositioning; 00233 return (1); 00234 case TIFFTAG_WHITEPOINT: 00235 { 00236 static float whitepoint[2]; 00237 00238 /* TIFF 6.0 specification tells that it is no default 00239 value for the WhitePoint, but AdobePhotoshop TIFF 00240 Technical Note tells that it should be CIE D50. */ 00241 whitepoint[0] = D50_X0 / (D50_X0 + D50_Y0 + D50_Z0); 00242 whitepoint[1] = D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0); 00243 *va_arg(ap, float **) = whitepoint; 00244 return 1; 00245 } 00246 case TIFFTAG_TRANSFERFUNCTION: 00247 if (!td->td_transferfunction[0] && 00248 !TIFFDefaultTransferFunction(td)) { 00249 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \"TransferFunction\" tag"); 00250 return (0); 00251 } 00252 *va_arg(ap, uint16 **) = td->td_transferfunction[0]; 00253 if (td->td_samplesperpixel - td->td_extrasamples > 1) { 00254 *va_arg(ap, uint16 **) = td->td_transferfunction[1]; 00255 *va_arg(ap, uint16 **) = td->td_transferfunction[2]; 00256 } 00257 return (1); 00258 case TIFFTAG_REFERENCEBLACKWHITE: 00259 if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td)) 00260 return (0); 00261 *va_arg(ap, float **) = td->td_refblackwhite; 00262 return (1); 00263 } 00264 return 0; 00265 } 00266 00267 /* 00268 * Like TIFFGetField, but return any default 00269 * value if the tag is not present in the directory. 00270 */ 00271 int 00272 TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...) 00273 { 00274 int ok; 00275 va_list ap; 00276 00277 va_start(ap, tag); 00278 ok = TIFFVGetFieldDefaulted(tif, tag, ap); 00279 va_end(ap); 00280 return (ok); 00281 } 00282 00283 /* vim: set ts=8 sts=8 sw=8 noet: */ 00284 /* 00285 * Local Variables: 00286 * mode: c 00287 * c-basic-offset: 8 00288 * fill-column: 78 00289 * End: 00290 */ Generated on Sun May 27 2012 04:19:32 for ReactOS by
1.7.6.1
|