| 1 | /* $Header$ */ |
| 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_IMAGEDEPTH: |
| 147 | *va_arg(ap, uint32 *) = td->td_imagedepth; |
| 148 | return (1); |
| 149 | #ifdef YCBCR_SUPPORT |
| 150 | case TIFFTAG_YCBCRCOEFFICIENTS: |
| 151 | if (!td->td_ycbcrcoeffs) { |
| 152 | td->td_ycbcrcoeffs = (float *) |
| 153 | _TIFFmalloc(3*sizeof (float)); |
| 154 | /* defaults are from CCIR Recommendation 601-1 */ |
| 155 | td->td_ycbcrcoeffs[0] = 0.299f; |
| 156 | td->td_ycbcrcoeffs[1] = 0.587f; |
| 157 | td->td_ycbcrcoeffs[2] = 0.114f; |
| 158 | } |
| 159 | *va_arg(ap, float **) = td->td_ycbcrcoeffs; |
| 160 | return (1); |
| 161 | case TIFFTAG_YCBCRSUBSAMPLING: |
| 162 | *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0]; |
| 163 | *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1]; |
| 164 | return (1); |
| 165 | case TIFFTAG_YCBCRPOSITIONING: |
| 166 | *va_arg(ap, uint16 *) = td->td_ycbcrpositioning; |
| 167 | return (1); |
| 168 | #endif |
| 169 | #ifdef COLORIMETRY_SUPPORT |
| 170 | case TIFFTAG_TRANSFERFUNCTION: |
| 171 | if (!td->td_transferfunction[0]) |
| 172 | TIFFDefaultTransferFunction(td); |
| 173 | *va_arg(ap, uint16 **) = td->td_transferfunction[0]; |
| 174 | if (td->td_samplesperpixel - td->td_extrasamples > 1) { |
| 175 | *va_arg(ap, uint16 **) = td->td_transferfunction[1]; |
| 176 | *va_arg(ap, uint16 **) = td->td_transferfunction[2]; |
| 177 | } |
| 178 | return (1); |
| 179 | case TIFFTAG_REFERENCEBLACKWHITE: |
| 180 | if (!td->td_refblackwhite) |
| 181 | TIFFDefaultRefBlackWhite(td); |
| 182 | *va_arg(ap, float **) = td->td_refblackwhite; |
| 183 | return (1); |
| 184 | #endif |
| 185 | } |
| 186 | return (0); |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Like TIFFGetField, but return any default |
| 191 | * value if the tag is not present in the directory. |
| 192 | */ |
| 193 | int |
| 194 | TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...) |
| 195 | { |
| 196 | int ok; |
| 197 | va_list ap; |
| 198 | |
| 199 | va_start(ap, tag); |
| 200 | ok = TIFFVGetFieldDefaulted(tif, tag, ap); |
| 201 | va_end(ap); |
| 202 | return (ok); |
| 203 | } |