/src/ghostpdl/tiff/libtiff/tif_luv.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 1997 Greg Ward Larson |
3 | | * Copyright (c) 1997 Silicon Graphics, Inc. |
4 | | * |
5 | | * Permission to use, copy, modify, distribute, and sell this software and |
6 | | * its documentation for any purpose is hereby granted without fee, provided |
7 | | * that (i) the above copyright notices and this permission notice appear in |
8 | | * all copies of the software and related documentation, and (ii) the names of |
9 | | * Sam Leffler, Greg Larson and Silicon Graphics may not be used in any |
10 | | * advertising or publicity relating to the software without the specific, |
11 | | * prior written permission of Sam Leffler, Greg Larson and Silicon Graphics. |
12 | | * |
13 | | * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
14 | | * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
15 | | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
16 | | * |
17 | | * IN NO EVENT SHALL SAM LEFFLER, GREG LARSON OR SILICON GRAPHICS BE LIABLE |
18 | | * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
19 | | * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
20 | | * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
21 | | * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
22 | | * OF THIS SOFTWARE. |
23 | | */ |
24 | | |
25 | | #include "tiffiop.h" |
26 | | #ifdef LOGLUV_SUPPORT |
27 | | |
28 | | /* |
29 | | * TIFF Library. |
30 | | * LogLuv compression support for high dynamic range images. |
31 | | * |
32 | | * Contributed by Greg Larson. |
33 | | * |
34 | | * LogLuv image support uses the TIFF library to store 16 or 10-bit |
35 | | * log luminance values with 8 bits each of u and v or a 14-bit index. |
36 | | * |
37 | | * The codec can take as input and produce as output 32-bit IEEE float values |
38 | | * as well as 16-bit integer values. A 16-bit luminance is interpreted |
39 | | * as a sign bit followed by a 15-bit integer that is converted |
40 | | * to and from a linear magnitude using the transformation: |
41 | | * |
42 | | * L = 2^( (Le+.5)/256 - 64 ) # real from 15-bit |
43 | | * |
44 | | * Le = floor( 256*(log2(L) + 64) ) # 15-bit from real |
45 | | * |
46 | | * The actual conversion to world luminance units in candelas per sq. meter |
47 | | * requires an additional multiplier, which is stored in the TIFFTAG_STONITS. |
48 | | * This value is usually set such that a reasonable exposure comes from |
49 | | * clamping decoded luminances above 1 to 1 in the displayed image. |
50 | | * |
51 | | * The 16-bit values for u and v may be converted to real values by dividing |
52 | | * each by 32768. (This allows for negative values, which aren't useful as |
53 | | * far as we know, but are left in case of future improvements in human |
54 | | * color vision.) |
55 | | * |
56 | | * Conversion from (u,v), which is actually the CIE (u',v') system for |
57 | | * you color scientists, is accomplished by the following transformation: |
58 | | * |
59 | | * u = 4*x / (-2*x + 12*y + 3) |
60 | | * v = 9*y / (-2*x + 12*y + 3) |
61 | | * |
62 | | * x = 9*u / (6*u - 16*v + 12) |
63 | | * y = 4*v / (6*u - 16*v + 12) |
64 | | * |
65 | | * This process is greatly simplified by passing 32-bit IEEE floats |
66 | | * for each of three CIE XYZ coordinates. The codec then takes care |
67 | | * of conversion to and from LogLuv, though the application is still |
68 | | * responsible for interpreting the TIFFTAG_STONITS calibration factor. |
69 | | * |
70 | | * By definition, a CIE XYZ vector of [1 1 1] corresponds to a neutral white |
71 | | * point of (x,y)=(1/3,1/3). However, most color systems assume some other |
72 | | * white point, such as D65, and an absolute color conversion to XYZ then |
73 | | * to another color space with a different white point may introduce an |
74 | | * unwanted color cast to the image. It is often desirable, therefore, to |
75 | | * perform a white point conversion that maps the input white to [1 1 1] |
76 | | * in XYZ, then record the original white point using the TIFFTAG_WHITEPOINT |
77 | | * tag value. A decoder that demands absolute color calibration may use |
78 | | * this white point tag to get back the original colors, but usually it |
79 | | * will be ignored and the new white point will be used instead that |
80 | | * matches the output color space. |
81 | | * |
82 | | * Pixel information is compressed into one of two basic encodings, depending |
83 | | * on the setting of the compression tag, which is one of COMPRESSION_SGILOG |
84 | | * or COMPRESSION_SGILOG24. For COMPRESSION_SGILOG, greyscale data is |
85 | | * stored as: |
86 | | * |
87 | | * 1 15 |
88 | | * |-+---------------| |
89 | | * |
90 | | * COMPRESSION_SGILOG color data is stored as: |
91 | | * |
92 | | * 1 15 8 8 |
93 | | * |-+---------------|--------+--------| |
94 | | * S Le ue ve |
95 | | * |
96 | | * For the 24-bit COMPRESSION_SGILOG24 color format, the data is stored as: |
97 | | * |
98 | | * 10 14 |
99 | | * |----------|--------------| |
100 | | * Le' Ce |
101 | | * |
102 | | * There is no sign bit in the 24-bit case, and the (u,v) chromaticity is |
103 | | * encoded as an index for optimal color resolution. The 10 log bits are |
104 | | * defined by the following conversions: |
105 | | * |
106 | | * L = 2^((Le'+.5)/64 - 12) # real from 10-bit |
107 | | * |
108 | | * Le' = floor( 64*(log2(L) + 12) ) # 10-bit from real |
109 | | * |
110 | | * The 10 bits of the smaller format may be converted into the 15 bits of |
111 | | * the larger format by multiplying by 4 and adding 13314. Obviously, |
112 | | * a smaller range of magnitudes is covered (about 5 orders of magnitude |
113 | | * instead of 38), and the lack of a sign bit means that negative luminances |
114 | | * are not allowed. (Well, they aren't allowed in the real world, either, |
115 | | * but they are useful for certain types of image processing.) |
116 | | * |
117 | | * The desired user format is controlled by the setting the internal |
118 | | * pseudo tag TIFFTAG_SGILOGDATAFMT to one of: |
119 | | * SGILOGDATAFMT_FLOAT = IEEE 32-bit float XYZ values |
120 | | * SGILOGDATAFMT_16BIT = 16-bit integer encodings of logL, u and v |
121 | | * Raw data i/o is also possible using: |
122 | | * SGILOGDATAFMT_RAW = 32-bit unsigned integer with encoded pixel |
123 | | * In addition, the following decoding is provided for ease of display: |
124 | | * SGILOGDATAFMT_8BIT = 8-bit default RGB gamma-corrected values |
125 | | * |
126 | | * For grayscale images, we provide the following data formats: |
127 | | * SGILOGDATAFMT_FLOAT = IEEE 32-bit float Y values |
128 | | * SGILOGDATAFMT_16BIT = 16-bit integer w/ encoded luminance |
129 | | * SGILOGDATAFMT_8BIT = 8-bit gray monitor values |
130 | | * |
131 | | * Note that the COMPRESSION_SGILOG applies a simple run-length encoding |
132 | | * scheme by separating the logL, u and v bytes for each row and applying |
133 | | * a PackBits type of compression. Since the 24-bit encoding is not |
134 | | * adaptive, the 32-bit color format takes less space in many cases. |
135 | | * |
136 | | * Further control is provided over the conversion from higher-resolution |
137 | | * formats to final encoded values through the pseudo tag |
138 | | * TIFFTAG_SGILOGENCODE: |
139 | | * SGILOGENCODE_NODITHER = do not dither encoded values |
140 | | * SGILOGENCODE_RANDITHER = apply random dithering during encoding |
141 | | * |
142 | | * The default value of this tag is SGILOGENCODE_NODITHER for |
143 | | * COMPRESSION_SGILOG to maximize run-length encoding and |
144 | | * SGILOGENCODE_RANDITHER for COMPRESSION_SGILOG24 to turn |
145 | | * quantization errors into noise. |
146 | | */ |
147 | | |
148 | | #include <stdio.h> |
149 | | #include <stdlib.h> |
150 | | #include <math.h> |
151 | | |
152 | | /* |
153 | | * State block for each open TIFF |
154 | | * file using LogLuv compression/decompression. |
155 | | */ |
156 | | typedef struct logLuvState LogLuvState; |
157 | | |
158 | | struct logLuvState { |
159 | | int encoder_state; /* 1 if encoder correctly initialized */ |
160 | | int user_datafmt; /* user data format */ |
161 | | int encode_meth; /* encoding method */ |
162 | | int pixel_size; /* bytes per pixel */ |
163 | | |
164 | | uint8_t* tbuf; /* translation buffer */ |
165 | | tmsize_t tbuflen; /* buffer length */ |
166 | | void (*tfunc)(LogLuvState*, uint8_t*, tmsize_t); |
167 | | |
168 | | TIFFVSetMethod vgetparent; /* super-class method */ |
169 | | TIFFVSetMethod vsetparent; /* super-class method */ |
170 | | }; |
171 | | |
172 | 0 | #define DecoderState(tif) ((LogLuvState*) (tif)->tif_data) |
173 | 0 | #define EncoderState(tif) ((LogLuvState*) (tif)->tif_data) |
174 | | |
175 | 0 | #define SGILOGDATAFMT_UNKNOWN -1 |
176 | | |
177 | 0 | #define MINRUN 4 /* minimum run length */ |
178 | | |
179 | | /* |
180 | | * Decode a string of 16-bit gray pixels. |
181 | | */ |
182 | | static int |
183 | | LogL16Decode(TIFF* tif, uint8_t* op, tmsize_t occ, uint16_t s) |
184 | 0 | { |
185 | 0 | static const char module[] = "LogL16Decode"; |
186 | 0 | LogLuvState* sp = DecoderState(tif); |
187 | 0 | int shft; |
188 | 0 | tmsize_t i; |
189 | 0 | tmsize_t npixels; |
190 | 0 | unsigned char* bp; |
191 | 0 | int16_t* tp; |
192 | 0 | int16_t b; |
193 | 0 | tmsize_t cc; |
194 | 0 | int rc; |
195 | |
|
196 | 0 | (void)s; |
197 | 0 | assert(s == 0); |
198 | 0 | assert(sp != NULL); |
199 | |
|
200 | 0 | npixels = occ / sp->pixel_size; |
201 | |
|
202 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_16BIT) |
203 | 0 | tp = (int16_t*) op; |
204 | 0 | else { |
205 | 0 | if(sp->tbuflen < npixels) { |
206 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
207 | 0 | "Translation buffer too short"); |
208 | 0 | return (0); |
209 | 0 | } |
210 | 0 | tp = (int16_t*) sp->tbuf; |
211 | 0 | } |
212 | 0 | _TIFFmemset((void*) tp, 0, npixels*sizeof (tp[0])); |
213 | |
|
214 | 0 | bp = (unsigned char*) tif->tif_rawcp; |
215 | 0 | cc = tif->tif_rawcc; |
216 | | /* get each byte string */ |
217 | 0 | for (shft = 8; shft >= 0; shft -=8) { |
218 | 0 | for (i = 0; i < npixels && cc > 0; ) { |
219 | 0 | if (*bp >= 128) { /* run */ |
220 | 0 | if( cc < 2 ) |
221 | 0 | break; |
222 | 0 | rc = *bp++ + (2-128); |
223 | 0 | b = (int16_t)(*bp++ << shft); |
224 | 0 | cc -= 2; |
225 | 0 | while (rc-- && i < npixels) |
226 | 0 | tp[i++] |= b; |
227 | 0 | } else { /* non-run */ |
228 | 0 | rc = *bp++; /* nul is noop */ |
229 | 0 | while (--cc && rc-- && i < npixels) |
230 | 0 | tp[i++] |= (int16_t)*bp++ << shft; |
231 | 0 | } |
232 | 0 | } |
233 | 0 | if (i != npixels) { |
234 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
235 | 0 | "Not enough data at row %"PRIu32" (short %"TIFF_SSIZE_FORMAT" pixels)", |
236 | 0 | tif->tif_row, |
237 | 0 | npixels - i); |
238 | 0 | tif->tif_rawcp = (uint8_t*) bp; |
239 | 0 | tif->tif_rawcc = cc; |
240 | 0 | return (0); |
241 | 0 | } |
242 | 0 | } |
243 | 0 | (*sp->tfunc)(sp, op, npixels); |
244 | 0 | tif->tif_rawcp = (uint8_t*) bp; |
245 | 0 | tif->tif_rawcc = cc; |
246 | 0 | return (1); |
247 | 0 | } |
248 | | |
249 | | /* |
250 | | * Decode a string of 24-bit pixels. |
251 | | */ |
252 | | static int |
253 | | LogLuvDecode24(TIFF* tif, uint8_t* op, tmsize_t occ, uint16_t s) |
254 | 0 | { |
255 | 0 | static const char module[] = "LogLuvDecode24"; |
256 | 0 | LogLuvState* sp = DecoderState(tif); |
257 | 0 | tmsize_t cc; |
258 | 0 | tmsize_t i; |
259 | 0 | tmsize_t npixels; |
260 | 0 | unsigned char* bp; |
261 | 0 | uint32_t* tp; |
262 | |
|
263 | 0 | (void)s; |
264 | 0 | assert(s == 0); |
265 | 0 | assert(sp != NULL); |
266 | |
|
267 | 0 | npixels = occ / sp->pixel_size; |
268 | |
|
269 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_RAW) |
270 | 0 | tp = (uint32_t *)op; |
271 | 0 | else { |
272 | 0 | if(sp->tbuflen < npixels) { |
273 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
274 | 0 | "Translation buffer too short"); |
275 | 0 | return (0); |
276 | 0 | } |
277 | 0 | tp = (uint32_t *) sp->tbuf; |
278 | 0 | } |
279 | | /* copy to array of uint32_t */ |
280 | 0 | bp = (unsigned char*) tif->tif_rawcp; |
281 | 0 | cc = tif->tif_rawcc; |
282 | 0 | for (i = 0; i < npixels && cc >= 3; i++) { |
283 | 0 | tp[i] = bp[0] << 16 | bp[1] << 8 | bp[2]; |
284 | 0 | bp += 3; |
285 | 0 | cc -= 3; |
286 | 0 | } |
287 | 0 | tif->tif_rawcp = (uint8_t*) bp; |
288 | 0 | tif->tif_rawcc = cc; |
289 | 0 | if (i != npixels) { |
290 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
291 | 0 | "Not enough data at row %"PRIu32" (short %"TIFF_SSIZE_FORMAT" pixels)", |
292 | 0 | tif->tif_row, |
293 | 0 | npixels - i); |
294 | 0 | return (0); |
295 | 0 | } |
296 | 0 | (*sp->tfunc)(sp, op, npixels); |
297 | 0 | return (1); |
298 | 0 | } |
299 | | |
300 | | /* |
301 | | * Decode a string of 32-bit pixels. |
302 | | */ |
303 | | static int |
304 | | LogLuvDecode32(TIFF* tif, uint8_t* op, tmsize_t occ, uint16_t s) |
305 | 0 | { |
306 | 0 | static const char module[] = "LogLuvDecode32"; |
307 | 0 | LogLuvState* sp; |
308 | 0 | int shft; |
309 | 0 | tmsize_t i; |
310 | 0 | tmsize_t npixels; |
311 | 0 | unsigned char* bp; |
312 | 0 | uint32_t* tp; |
313 | 0 | uint32_t b; |
314 | 0 | tmsize_t cc; |
315 | 0 | int rc; |
316 | |
|
317 | 0 | (void)s; |
318 | 0 | assert(s == 0); |
319 | 0 | sp = DecoderState(tif); |
320 | 0 | assert(sp != NULL); |
321 | |
|
322 | 0 | npixels = occ / sp->pixel_size; |
323 | |
|
324 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_RAW) |
325 | 0 | tp = (uint32_t*) op; |
326 | 0 | else { |
327 | 0 | if(sp->tbuflen < npixels) { |
328 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
329 | 0 | "Translation buffer too short"); |
330 | 0 | return (0); |
331 | 0 | } |
332 | 0 | tp = (uint32_t*) sp->tbuf; |
333 | 0 | } |
334 | 0 | _TIFFmemset((void*) tp, 0, npixels*sizeof (tp[0])); |
335 | |
|
336 | 0 | bp = (unsigned char*) tif->tif_rawcp; |
337 | 0 | cc = tif->tif_rawcc; |
338 | | /* get each byte string */ |
339 | 0 | for (shft = 24; shft >= 0; shft -=8) { |
340 | 0 | for (i = 0; i < npixels && cc > 0; ) { |
341 | 0 | if (*bp >= 128) { /* run */ |
342 | 0 | if( cc < 2 ) |
343 | 0 | break; |
344 | 0 | rc = *bp++ + (2-128); |
345 | 0 | b = (uint32_t)*bp++ << shft; |
346 | 0 | cc -= 2; |
347 | 0 | while (rc-- && i < npixels) |
348 | 0 | tp[i++] |= b; |
349 | 0 | } else { /* non-run */ |
350 | 0 | rc = *bp++; /* nul is noop */ |
351 | 0 | while (--cc && rc-- && i < npixels) |
352 | 0 | tp[i++] |= (uint32_t)*bp++ << shft; |
353 | 0 | } |
354 | 0 | } |
355 | 0 | if (i != npixels) { |
356 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
357 | 0 | "Not enough data at row %"PRIu32" (short %"TIFF_SSIZE_FORMAT" pixels)", |
358 | 0 | tif->tif_row, |
359 | 0 | npixels - i); |
360 | 0 | tif->tif_rawcp = (uint8_t*) bp; |
361 | 0 | tif->tif_rawcc = cc; |
362 | 0 | return (0); |
363 | 0 | } |
364 | 0 | } |
365 | 0 | (*sp->tfunc)(sp, op, npixels); |
366 | 0 | tif->tif_rawcp = (uint8_t*) bp; |
367 | 0 | tif->tif_rawcc = cc; |
368 | 0 | return (1); |
369 | 0 | } |
370 | | |
371 | | /* |
372 | | * Decode a strip of pixels. We break it into rows to |
373 | | * maintain synchrony with the encode algorithm, which |
374 | | * is row by row. |
375 | | */ |
376 | | static int |
377 | | LogLuvDecodeStrip(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s) |
378 | 0 | { |
379 | 0 | tmsize_t rowlen = TIFFScanlineSize(tif); |
380 | |
|
381 | 0 | if (rowlen == 0) |
382 | 0 | return 0; |
383 | | |
384 | 0 | assert(cc%rowlen == 0); |
385 | 0 | while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s)) { |
386 | 0 | bp += rowlen; |
387 | 0 | cc -= rowlen; |
388 | 0 | } |
389 | 0 | return (cc == 0); |
390 | 0 | } |
391 | | |
392 | | /* |
393 | | * Decode a tile of pixels. We break it into rows to |
394 | | * maintain synchrony with the encode algorithm, which |
395 | | * is row by row. |
396 | | */ |
397 | | static int |
398 | | LogLuvDecodeTile(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s) |
399 | 0 | { |
400 | 0 | tmsize_t rowlen = TIFFTileRowSize(tif); |
401 | |
|
402 | 0 | if (rowlen == 0) |
403 | 0 | return 0; |
404 | | |
405 | 0 | assert(cc%rowlen == 0); |
406 | 0 | while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s)) { |
407 | 0 | bp += rowlen; |
408 | 0 | cc -= rowlen; |
409 | 0 | } |
410 | 0 | return (cc == 0); |
411 | 0 | } |
412 | | |
413 | | /* |
414 | | * Encode a row of 16-bit pixels. |
415 | | */ |
416 | | static int |
417 | | LogL16Encode(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s) |
418 | 0 | { |
419 | 0 | static const char module[] = "LogL16Encode"; |
420 | 0 | LogLuvState* sp = EncoderState(tif); |
421 | 0 | int shft; |
422 | 0 | tmsize_t i; |
423 | 0 | tmsize_t j; |
424 | 0 | tmsize_t npixels; |
425 | 0 | uint8_t* op; |
426 | 0 | int16_t* tp; |
427 | 0 | int16_t b; |
428 | 0 | tmsize_t occ; |
429 | 0 | int rc=0, mask; |
430 | 0 | tmsize_t beg; |
431 | |
|
432 | 0 | (void)s; |
433 | 0 | assert(s == 0); |
434 | 0 | assert(sp != NULL); |
435 | 0 | npixels = cc / sp->pixel_size; |
436 | |
|
437 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_16BIT) |
438 | 0 | tp = (int16_t*) bp; |
439 | 0 | else { |
440 | 0 | tp = (int16_t*) sp->tbuf; |
441 | 0 | if(sp->tbuflen < npixels) { |
442 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
443 | 0 | "Translation buffer too short"); |
444 | 0 | return (0); |
445 | 0 | } |
446 | 0 | (*sp->tfunc)(sp, bp, npixels); |
447 | 0 | } |
448 | | /* compress each byte string */ |
449 | 0 | op = tif->tif_rawcp; |
450 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
451 | 0 | for (shft = 8; shft >= 0; shft -=8) { |
452 | 0 | for (i = 0; i < npixels; i += rc) { |
453 | 0 | if (occ < 4) { |
454 | 0 | tif->tif_rawcp = op; |
455 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
456 | 0 | if (!TIFFFlushData1(tif)) |
457 | 0 | return (0); |
458 | 0 | op = tif->tif_rawcp; |
459 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
460 | 0 | } |
461 | 0 | mask = 0xff << shft; /* find next run */ |
462 | 0 | for (beg = i; beg < npixels; beg += rc) { |
463 | 0 | b = (int16_t) (tp[beg] & mask); |
464 | 0 | rc = 1; |
465 | 0 | while (rc < 127+2 && beg+rc < npixels && |
466 | 0 | (tp[beg+rc] & mask) == b) |
467 | 0 | rc++; |
468 | 0 | if (rc >= MINRUN) |
469 | 0 | break; /* long enough */ |
470 | 0 | } |
471 | 0 | if (beg-i > 1 && beg-i < MINRUN) { |
472 | 0 | b = (int16_t) (tp[i] & mask);/*check short run */ |
473 | 0 | j = i+1; |
474 | 0 | while ((tp[j++] & mask) == b) |
475 | 0 | if (j == beg) { |
476 | 0 | *op++ = (uint8_t)(128 - 2 + j - i); |
477 | 0 | *op++ = (uint8_t)(b >> shft); |
478 | 0 | occ -= 2; |
479 | 0 | i = beg; |
480 | 0 | break; |
481 | 0 | } |
482 | 0 | } |
483 | 0 | while (i < beg) { /* write out non-run */ |
484 | 0 | if ((j = beg-i) > 127) j = 127; |
485 | 0 | if (occ < j+3) { |
486 | 0 | tif->tif_rawcp = op; |
487 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
488 | 0 | if (!TIFFFlushData1(tif)) |
489 | 0 | return (0); |
490 | 0 | op = tif->tif_rawcp; |
491 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
492 | 0 | } |
493 | 0 | *op++ = (uint8_t) j; occ--; |
494 | 0 | while (j--) { |
495 | 0 | *op++ = (uint8_t) (tp[i++] >> shft & 0xff); |
496 | 0 | occ--; |
497 | 0 | } |
498 | 0 | } |
499 | 0 | if (rc >= MINRUN) { /* write out run */ |
500 | 0 | *op++ = (uint8_t) (128 - 2 + rc); |
501 | 0 | *op++ = (uint8_t) (tp[beg] >> shft & 0xff); |
502 | 0 | occ -= 2; |
503 | 0 | } else |
504 | 0 | rc = 0; |
505 | 0 | } |
506 | 0 | } |
507 | 0 | tif->tif_rawcp = op; |
508 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
509 | |
|
510 | 0 | return (1); |
511 | 0 | } |
512 | | |
513 | | /* |
514 | | * Encode a row of 24-bit pixels. |
515 | | */ |
516 | | static int |
517 | | LogLuvEncode24(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s) |
518 | 0 | { |
519 | 0 | static const char module[] = "LogLuvEncode24"; |
520 | 0 | LogLuvState* sp = EncoderState(tif); |
521 | 0 | tmsize_t i; |
522 | 0 | tmsize_t npixels; |
523 | 0 | tmsize_t occ; |
524 | 0 | uint8_t* op; |
525 | 0 | uint32_t* tp; |
526 | |
|
527 | 0 | (void)s; |
528 | 0 | assert(s == 0); |
529 | 0 | assert(sp != NULL); |
530 | 0 | npixels = cc / sp->pixel_size; |
531 | |
|
532 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_RAW) |
533 | 0 | tp = (uint32_t*) bp; |
534 | 0 | else { |
535 | 0 | tp = (uint32_t*) sp->tbuf; |
536 | 0 | if(sp->tbuflen < npixels) { |
537 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
538 | 0 | "Translation buffer too short"); |
539 | 0 | return (0); |
540 | 0 | } |
541 | 0 | (*sp->tfunc)(sp, bp, npixels); |
542 | 0 | } |
543 | | /* write out encoded pixels */ |
544 | 0 | op = tif->tif_rawcp; |
545 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
546 | 0 | for (i = npixels; i--; ) { |
547 | 0 | if (occ < 3) { |
548 | 0 | tif->tif_rawcp = op; |
549 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
550 | 0 | if (!TIFFFlushData1(tif)) |
551 | 0 | return (0); |
552 | 0 | op = tif->tif_rawcp; |
553 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
554 | 0 | } |
555 | 0 | *op++ = (uint8_t)(*tp >> 16); |
556 | 0 | *op++ = (uint8_t)(*tp >> 8 & 0xff); |
557 | 0 | *op++ = (uint8_t)(*tp++ & 0xff); |
558 | 0 | occ -= 3; |
559 | 0 | } |
560 | 0 | tif->tif_rawcp = op; |
561 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
562 | |
|
563 | 0 | return (1); |
564 | 0 | } |
565 | | |
566 | | /* |
567 | | * Encode a row of 32-bit pixels. |
568 | | */ |
569 | | static int |
570 | | LogLuvEncode32(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s) |
571 | 0 | { |
572 | 0 | static const char module[] = "LogLuvEncode32"; |
573 | 0 | LogLuvState* sp = EncoderState(tif); |
574 | 0 | int shft; |
575 | 0 | tmsize_t i; |
576 | 0 | tmsize_t j; |
577 | 0 | tmsize_t npixels; |
578 | 0 | uint8_t* op; |
579 | 0 | uint32_t* tp; |
580 | 0 | uint32_t b; |
581 | 0 | tmsize_t occ; |
582 | 0 | int rc=0, mask; |
583 | 0 | tmsize_t beg; |
584 | |
|
585 | 0 | (void)s; |
586 | 0 | assert(s == 0); |
587 | 0 | assert(sp != NULL); |
588 | |
|
589 | 0 | npixels = cc / sp->pixel_size; |
590 | |
|
591 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_RAW) |
592 | 0 | tp = (uint32_t*) bp; |
593 | 0 | else { |
594 | 0 | tp = (uint32_t*) sp->tbuf; |
595 | 0 | if(sp->tbuflen < npixels) { |
596 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
597 | 0 | "Translation buffer too short"); |
598 | 0 | return (0); |
599 | 0 | } |
600 | 0 | (*sp->tfunc)(sp, bp, npixels); |
601 | 0 | } |
602 | | /* compress each byte string */ |
603 | 0 | op = tif->tif_rawcp; |
604 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
605 | 0 | for (shft = 24; shft >= 0; shft -=8) { |
606 | 0 | for (i = 0; i < npixels; i += rc) { |
607 | 0 | if (occ < 4) { |
608 | 0 | tif->tif_rawcp = op; |
609 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
610 | 0 | if (!TIFFFlushData1(tif)) |
611 | 0 | return (0); |
612 | 0 | op = tif->tif_rawcp; |
613 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
614 | 0 | } |
615 | 0 | mask = 0xff << shft; /* find next run */ |
616 | 0 | for (beg = i; beg < npixels; beg += rc) { |
617 | 0 | b = tp[beg] & mask; |
618 | 0 | rc = 1; |
619 | 0 | while (rc < 127+2 && beg+rc < npixels && |
620 | 0 | (tp[beg+rc] & mask) == b) |
621 | 0 | rc++; |
622 | 0 | if (rc >= MINRUN) |
623 | 0 | break; /* long enough */ |
624 | 0 | } |
625 | 0 | if (beg-i > 1 && beg-i < MINRUN) { |
626 | 0 | b = tp[i] & mask; /* check short run */ |
627 | 0 | j = i+1; |
628 | 0 | while ((tp[j++] & mask) == b) |
629 | 0 | if (j == beg) { |
630 | 0 | *op++ = (uint8_t)(128 - 2 + j - i); |
631 | 0 | *op++ = (uint8_t)(b >> shft); |
632 | 0 | occ -= 2; |
633 | 0 | i = beg; |
634 | 0 | break; |
635 | 0 | } |
636 | 0 | } |
637 | 0 | while (i < beg) { /* write out non-run */ |
638 | 0 | if ((j = beg-i) > 127) j = 127; |
639 | 0 | if (occ < j+3) { |
640 | 0 | tif->tif_rawcp = op; |
641 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
642 | 0 | if (!TIFFFlushData1(tif)) |
643 | 0 | return (0); |
644 | 0 | op = tif->tif_rawcp; |
645 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
646 | 0 | } |
647 | 0 | *op++ = (uint8_t) j; occ--; |
648 | 0 | while (j--) { |
649 | 0 | *op++ = (uint8_t)(tp[i++] >> shft & 0xff); |
650 | 0 | occ--; |
651 | 0 | } |
652 | 0 | } |
653 | 0 | if (rc >= MINRUN) { /* write out run */ |
654 | 0 | *op++ = (uint8_t) (128 - 2 + rc); |
655 | 0 | *op++ = (uint8_t)(tp[beg] >> shft & 0xff); |
656 | 0 | occ -= 2; |
657 | 0 | } else |
658 | 0 | rc = 0; |
659 | 0 | } |
660 | 0 | } |
661 | 0 | tif->tif_rawcp = op; |
662 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
663 | |
|
664 | 0 | return (1); |
665 | 0 | } |
666 | | |
667 | | /* |
668 | | * Encode a strip of pixels. We break it into rows to |
669 | | * avoid encoding runs across row boundaries. |
670 | | */ |
671 | | static int |
672 | | LogLuvEncodeStrip(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s) |
673 | 0 | { |
674 | 0 | tmsize_t rowlen = TIFFScanlineSize(tif); |
675 | |
|
676 | 0 | if (rowlen == 0) |
677 | 0 | return 0; |
678 | | |
679 | 0 | assert(cc%rowlen == 0); |
680 | 0 | while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1) { |
681 | 0 | bp += rowlen; |
682 | 0 | cc -= rowlen; |
683 | 0 | } |
684 | 0 | return (cc == 0); |
685 | 0 | } |
686 | | |
687 | | /* |
688 | | * Encode a tile of pixels. We break it into rows to |
689 | | * avoid encoding runs across row boundaries. |
690 | | */ |
691 | | static int |
692 | | LogLuvEncodeTile(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s) |
693 | 0 | { |
694 | 0 | tmsize_t rowlen = TIFFTileRowSize(tif); |
695 | |
|
696 | 0 | if (rowlen == 0) |
697 | 0 | return 0; |
698 | | |
699 | 0 | assert(cc%rowlen == 0); |
700 | 0 | while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1) { |
701 | 0 | bp += rowlen; |
702 | 0 | cc -= rowlen; |
703 | 0 | } |
704 | 0 | return (cc == 0); |
705 | 0 | } |
706 | | |
707 | | /* |
708 | | * Encode/Decode functions for converting to and from user formats. |
709 | | */ |
710 | | |
711 | | #include "uvcode.h" |
712 | | |
713 | | #ifndef UVSCALE |
714 | | #define U_NEU 0.210526316 |
715 | | #define V_NEU 0.473684211 |
716 | | #define UVSCALE 410. |
717 | | #endif |
718 | | |
719 | | #ifndef M_LN2 |
720 | | #define M_LN2 0.69314718055994530942 |
721 | | #endif |
722 | | #ifndef M_PI |
723 | | #define M_PI 3.14159265358979323846 |
724 | | #endif |
725 | | #undef log2 /* Conflict with C'99 function */ |
726 | 0 | #define log2(x) ((1./M_LN2)*log(x)) |
727 | | #undef exp2 /* Conflict with C'99 function */ |
728 | | #define exp2(x) exp(M_LN2*(x)) |
729 | | |
730 | | static int tiff_itrunc(double x, int m) |
731 | 0 | { |
732 | 0 | if( m == SGILOGENCODE_NODITHER ) |
733 | 0 | return (int)x; |
734 | | /* Silence CoverityScan warning about bad crypto function */ |
735 | | /* coverity[dont_call] */ |
736 | 0 | return (int)(x + rand()*(1./RAND_MAX) - .5); |
737 | 0 | } |
738 | | |
739 | | #if !LOGLUV_PUBLIC |
740 | | static |
741 | | #endif |
742 | | double |
743 | | LogL16toY(int p16) /* compute luminance from 16-bit LogL */ |
744 | 0 | { |
745 | 0 | int Le = p16 & 0x7fff; |
746 | 0 | double Y; |
747 | |
|
748 | 0 | if (!Le) |
749 | 0 | return (0.); |
750 | 0 | Y = exp(M_LN2/256.*(Le+.5) - M_LN2*64.); |
751 | 0 | return (!(p16 & 0x8000) ? Y : -Y); |
752 | 0 | } |
753 | | |
754 | | #if !LOGLUV_PUBLIC |
755 | | static |
756 | | #endif |
757 | | int |
758 | | LogL16fromY(double Y, int em) /* get 16-bit LogL from Y */ |
759 | 0 | { |
760 | 0 | if (Y >= 1.8371976e19) |
761 | 0 | return (0x7fff); |
762 | 0 | if (Y <= -1.8371976e19) |
763 | 0 | return (0xffff); |
764 | 0 | if (Y > 5.4136769e-20) |
765 | 0 | return tiff_itrunc(256.*(log2(Y) + 64.), em); |
766 | 0 | if (Y < -5.4136769e-20) |
767 | 0 | return (~0x7fff | tiff_itrunc(256.*(log2(-Y) + 64.), em)); |
768 | 0 | return (0); |
769 | 0 | } |
770 | | |
771 | | static void |
772 | | L16toY(LogLuvState* sp, uint8_t* op, tmsize_t n) |
773 | 0 | { |
774 | 0 | int16_t* l16 = (int16_t*) sp->tbuf; |
775 | 0 | float* yp = (float*) op; |
776 | |
|
777 | 0 | while (n-- > 0) |
778 | 0 | *yp++ = (float)LogL16toY(*l16++); |
779 | 0 | } |
780 | | |
781 | | static void |
782 | | L16toGry(LogLuvState* sp, uint8_t* op, tmsize_t n) |
783 | 0 | { |
784 | 0 | int16_t* l16 = (int16_t*) sp->tbuf; |
785 | 0 | uint8_t* gp = (uint8_t*) op; |
786 | |
|
787 | 0 | while (n-- > 0) { |
788 | 0 | double Y = LogL16toY(*l16++); |
789 | 0 | *gp++ = (uint8_t) ((Y <= 0.) ? 0 : (Y >= 1.) ? 255 : (int)(256. * sqrt(Y))); |
790 | 0 | } |
791 | 0 | } |
792 | | |
793 | | static void |
794 | | L16fromY(LogLuvState* sp, uint8_t* op, tmsize_t n) |
795 | 0 | { |
796 | 0 | int16_t* l16 = (int16_t*) sp->tbuf; |
797 | 0 | float* yp = (float*) op; |
798 | |
|
799 | 0 | while (n-- > 0) |
800 | 0 | *l16++ = (int16_t) (LogL16fromY(*yp++, sp->encode_meth)); |
801 | 0 | } |
802 | | |
803 | | #if !LOGLUV_PUBLIC |
804 | | static |
805 | | #endif |
806 | | void |
807 | | XYZtoRGB24(float xyz[3], uint8_t rgb[3]) |
808 | 0 | { |
809 | 0 | double r, g, b; |
810 | | /* assume CCIR-709 primaries */ |
811 | 0 | r = 2.690*xyz[0] + -1.276*xyz[1] + -0.414*xyz[2]; |
812 | 0 | g = -1.022*xyz[0] + 1.978*xyz[1] + 0.044*xyz[2]; |
813 | 0 | b = 0.061*xyz[0] + -0.224*xyz[1] + 1.163*xyz[2]; |
814 | | /* assume 2.0 gamma for speed */ |
815 | | /* could use integer sqrt approx., but this is probably faster */ |
816 | 0 | rgb[0] = (uint8_t)((r <= 0.) ? 0 : (r >= 1.) ? 255 : (int)(256. * sqrt(r))); |
817 | 0 | rgb[1] = (uint8_t)((g <= 0.) ? 0 : (g >= 1.) ? 255 : (int)(256. * sqrt(g))); |
818 | 0 | rgb[2] = (uint8_t)((b <= 0.) ? 0 : (b >= 1.) ? 255 : (int)(256. * sqrt(b))); |
819 | 0 | } |
820 | | |
821 | | #if !LOGLUV_PUBLIC |
822 | | static |
823 | | #endif |
824 | | double |
825 | | LogL10toY(int p10) /* compute luminance from 10-bit LogL */ |
826 | 0 | { |
827 | 0 | if (p10 == 0) |
828 | 0 | return (0.); |
829 | 0 | return (exp(M_LN2/64.*(p10+.5) - M_LN2*12.)); |
830 | 0 | } |
831 | | |
832 | | #if !LOGLUV_PUBLIC |
833 | | static |
834 | | #endif |
835 | | int |
836 | | LogL10fromY(double Y, int em) /* get 10-bit LogL from Y */ |
837 | 0 | { |
838 | 0 | if (Y >= 15.742) |
839 | 0 | return (0x3ff); |
840 | 0 | else if (Y <= .00024283) |
841 | 0 | return (0); |
842 | 0 | else |
843 | 0 | return tiff_itrunc(64.*(log2(Y) + 12.), em); |
844 | 0 | } |
845 | | |
846 | 0 | #define NANGLES 100 |
847 | 0 | #define uv2ang(u, v) ( (NANGLES*.499999999/M_PI) \ |
848 | 0 | * atan2((v)-V_NEU,(u)-U_NEU) + .5*NANGLES ) |
849 | | |
850 | | static int |
851 | | oog_encode(double u, double v) /* encode out-of-gamut chroma */ |
852 | 0 | { |
853 | 0 | static int oog_table[NANGLES]; |
854 | 0 | static int initialized = 0; |
855 | 0 | register int i; |
856 | |
|
857 | 0 | if (!initialized) { /* set up perimeter table */ |
858 | 0 | double eps[NANGLES], ua, va, ang, epsa; |
859 | 0 | int ui, vi, ustep; |
860 | 0 | for (i = NANGLES; i--; ) |
861 | 0 | eps[i] = 2.; |
862 | 0 | for (vi = UV_NVS; vi--; ) { |
863 | 0 | va = UV_VSTART + (vi+.5)*UV_SQSIZ; |
864 | 0 | ustep = uv_row[vi].nus-1; |
865 | 0 | if (vi == UV_NVS-1 || vi == 0 || ustep <= 0) |
866 | 0 | ustep = 1; |
867 | 0 | for (ui = uv_row[vi].nus-1; ui >= 0; ui -= ustep) { |
868 | 0 | ua = uv_row[vi].ustart + (ui+.5)*UV_SQSIZ; |
869 | 0 | ang = uv2ang(ua, va); |
870 | 0 | i = (int) ang; |
871 | 0 | epsa = fabs(ang - (i+.5)); |
872 | 0 | if (epsa < eps[i]) { |
873 | 0 | oog_table[i] = uv_row[vi].ncum + ui; |
874 | 0 | eps[i] = epsa; |
875 | 0 | } |
876 | 0 | } |
877 | 0 | } |
878 | 0 | for (i = NANGLES; i--; ) /* fill any holes */ |
879 | 0 | if (eps[i] > 1.5) { |
880 | 0 | int i1, i2; |
881 | 0 | for (i1 = 1; i1 < NANGLES/2; i1++) |
882 | 0 | if (eps[(i+i1)%NANGLES] < 1.5) |
883 | 0 | break; |
884 | 0 | for (i2 = 1; i2 < NANGLES/2; i2++) |
885 | 0 | if (eps[(i+NANGLES-i2)%NANGLES] < 1.5) |
886 | 0 | break; |
887 | 0 | if (i1 < i2) |
888 | 0 | oog_table[i] = |
889 | 0 | oog_table[(i+i1)%NANGLES]; |
890 | 0 | else |
891 | 0 | oog_table[i] = |
892 | 0 | oog_table[(i+NANGLES-i2)%NANGLES]; |
893 | 0 | } |
894 | 0 | initialized = 1; |
895 | 0 | } |
896 | 0 | i = (int) uv2ang(u, v); /* look up hue angle */ |
897 | 0 | return (oog_table[i]); |
898 | 0 | } |
899 | | |
900 | | #undef uv2ang |
901 | | #undef NANGLES |
902 | | |
903 | | #if !LOGLUV_PUBLIC |
904 | | static |
905 | | #endif |
906 | | int |
907 | | uv_encode(double u, double v, int em) /* encode (u',v') coordinates */ |
908 | 0 | { |
909 | 0 | register int vi, ui; |
910 | |
|
911 | 0 | if (v < UV_VSTART) |
912 | 0 | return oog_encode(u, v); |
913 | 0 | vi = tiff_itrunc((v - UV_VSTART)*(1./UV_SQSIZ), em); |
914 | 0 | if (vi >= UV_NVS) |
915 | 0 | return oog_encode(u, v); |
916 | 0 | if (u < uv_row[vi].ustart) |
917 | 0 | return oog_encode(u, v); |
918 | 0 | ui = tiff_itrunc((u - uv_row[vi].ustart)*(1./UV_SQSIZ), em); |
919 | 0 | if (ui >= uv_row[vi].nus) |
920 | 0 | return oog_encode(u, v); |
921 | | |
922 | 0 | return (uv_row[vi].ncum + ui); |
923 | 0 | } |
924 | | |
925 | | #if !LOGLUV_PUBLIC |
926 | | static |
927 | | #endif |
928 | | int |
929 | | uv_decode(double *up, double *vp, int c) /* decode (u',v') index */ |
930 | 0 | { |
931 | 0 | int upper, lower; |
932 | 0 | register int ui, vi; |
933 | |
|
934 | 0 | if (c < 0 || c >= UV_NDIVS) |
935 | 0 | return (-1); |
936 | 0 | lower = 0; /* binary search */ |
937 | 0 | upper = UV_NVS; |
938 | 0 | while (upper - lower > 1) { |
939 | 0 | vi = (lower + upper) >> 1; |
940 | 0 | ui = c - uv_row[vi].ncum; |
941 | 0 | if (ui > 0) |
942 | 0 | lower = vi; |
943 | 0 | else if (ui < 0) |
944 | 0 | upper = vi; |
945 | 0 | else { |
946 | 0 | lower = vi; |
947 | 0 | break; |
948 | 0 | } |
949 | 0 | } |
950 | 0 | vi = lower; |
951 | 0 | ui = c - uv_row[vi].ncum; |
952 | 0 | *up = uv_row[vi].ustart + (ui+.5)*UV_SQSIZ; |
953 | 0 | *vp = UV_VSTART + (vi+.5)*UV_SQSIZ; |
954 | 0 | return (0); |
955 | 0 | } |
956 | | |
957 | | #if !LOGLUV_PUBLIC |
958 | | static |
959 | | #endif |
960 | | void |
961 | | LogLuv24toXYZ(uint32_t p, float XYZ[3]) |
962 | 0 | { |
963 | 0 | int Ce; |
964 | 0 | double L, u, v, s, x, y; |
965 | | /* decode luminance */ |
966 | 0 | L = LogL10toY(p>>14 & 0x3ff); |
967 | 0 | if (L <= 0.) { |
968 | 0 | XYZ[0] = XYZ[1] = XYZ[2] = 0.; |
969 | 0 | return; |
970 | 0 | } |
971 | | /* decode color */ |
972 | 0 | Ce = p & 0x3fff; |
973 | 0 | if (uv_decode(&u, &v, Ce) < 0) { |
974 | 0 | u = U_NEU; v = V_NEU; |
975 | 0 | } |
976 | 0 | s = 1./(6.*u - 16.*v + 12.); |
977 | 0 | x = 9.*u * s; |
978 | 0 | y = 4.*v * s; |
979 | | /* convert to XYZ */ |
980 | 0 | XYZ[0] = (float)(x/y * L); |
981 | 0 | XYZ[1] = (float)L; |
982 | 0 | XYZ[2] = (float)((1.-x-y)/y * L); |
983 | 0 | } |
984 | | |
985 | | #if !LOGLUV_PUBLIC |
986 | | static |
987 | | #endif |
988 | | uint32_t |
989 | | LogLuv24fromXYZ(float XYZ[3], int em) |
990 | 0 | { |
991 | 0 | int Le, Ce; |
992 | 0 | double u, v, s; |
993 | | /* encode luminance */ |
994 | 0 | Le = LogL10fromY(XYZ[1], em); |
995 | | /* encode color */ |
996 | 0 | s = XYZ[0] + 15.*XYZ[1] + 3.*XYZ[2]; |
997 | 0 | if (!Le || s <= 0.) { |
998 | 0 | u = U_NEU; |
999 | 0 | v = V_NEU; |
1000 | 0 | } else { |
1001 | 0 | u = 4.*XYZ[0] / s; |
1002 | 0 | v = 9.*XYZ[1] / s; |
1003 | 0 | } |
1004 | 0 | Ce = uv_encode(u, v, em); |
1005 | 0 | if (Ce < 0) /* never happens */ |
1006 | 0 | Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER); |
1007 | | /* combine encodings */ |
1008 | 0 | return (Le << 14 | Ce); |
1009 | 0 | } |
1010 | | |
1011 | | static void |
1012 | | Luv24toXYZ(LogLuvState* sp, uint8_t* op, tmsize_t n) |
1013 | 0 | { |
1014 | 0 | uint32_t* luv = (uint32_t*) sp->tbuf; |
1015 | 0 | float* xyz = (float*) op; |
1016 | |
|
1017 | 0 | while (n-- > 0) { |
1018 | 0 | LogLuv24toXYZ(*luv, xyz); |
1019 | 0 | xyz += 3; |
1020 | 0 | luv++; |
1021 | 0 | } |
1022 | 0 | } |
1023 | | |
1024 | | static void |
1025 | | Luv24toLuv48(LogLuvState* sp, uint8_t* op, tmsize_t n) |
1026 | 0 | { |
1027 | 0 | uint32_t* luv = (uint32_t*) sp->tbuf; |
1028 | 0 | int16_t* luv3 = (int16_t*) op; |
1029 | |
|
1030 | 0 | while (n-- > 0) { |
1031 | 0 | double u, v; |
1032 | |
|
1033 | 0 | *luv3++ = (int16_t)((*luv >> 12 & 0xffd) + 13314); |
1034 | 0 | if (uv_decode(&u, &v, *luv&0x3fff) < 0) { |
1035 | 0 | u = U_NEU; |
1036 | 0 | v = V_NEU; |
1037 | 0 | } |
1038 | 0 | *luv3++ = (int16_t)(u * (1L << 15)); |
1039 | 0 | *luv3++ = (int16_t)(v * (1L << 15)); |
1040 | 0 | luv++; |
1041 | 0 | } |
1042 | 0 | } |
1043 | | |
1044 | | static void |
1045 | | Luv24toRGB(LogLuvState* sp, uint8_t* op, tmsize_t n) |
1046 | 0 | { |
1047 | 0 | uint32_t* luv = (uint32_t*) sp->tbuf; |
1048 | 0 | uint8_t* rgb = (uint8_t*) op; |
1049 | |
|
1050 | 0 | while (n-- > 0) { |
1051 | 0 | float xyz[3]; |
1052 | |
|
1053 | 0 | LogLuv24toXYZ(*luv++, xyz); |
1054 | 0 | XYZtoRGB24(xyz, rgb); |
1055 | 0 | rgb += 3; |
1056 | 0 | } |
1057 | 0 | } |
1058 | | |
1059 | | static void |
1060 | | Luv24fromXYZ(LogLuvState* sp, uint8_t* op, tmsize_t n) |
1061 | 0 | { |
1062 | 0 | uint32_t* luv = (uint32_t*) sp->tbuf; |
1063 | 0 | float* xyz = (float*) op; |
1064 | |
|
1065 | 0 | while (n-- > 0) { |
1066 | 0 | *luv++ = LogLuv24fromXYZ(xyz, sp->encode_meth); |
1067 | 0 | xyz += 3; |
1068 | 0 | } |
1069 | 0 | } |
1070 | | |
1071 | | static void |
1072 | | Luv24fromLuv48(LogLuvState* sp, uint8_t* op, tmsize_t n) |
1073 | 0 | { |
1074 | 0 | uint32_t* luv = (uint32_t*) sp->tbuf; |
1075 | 0 | int16_t* luv3 = (int16_t*) op; |
1076 | |
|
1077 | 0 | while (n-- > 0) { |
1078 | 0 | int Le, Ce; |
1079 | |
|
1080 | 0 | if (luv3[0] <= 0) |
1081 | 0 | Le = 0; |
1082 | 0 | else if (luv3[0] >= (1<<12)+3314) |
1083 | 0 | Le = (1<<10) - 1; |
1084 | 0 | else if (sp->encode_meth == SGILOGENCODE_NODITHER) |
1085 | 0 | Le = (luv3[0]-3314) >> 2; |
1086 | 0 | else |
1087 | 0 | Le = tiff_itrunc(.25*(luv3[0]-3314.), sp->encode_meth); |
1088 | |
|
1089 | 0 | Ce = uv_encode((luv3[1]+.5)/(1<<15), (luv3[2]+.5)/(1<<15), |
1090 | 0 | sp->encode_meth); |
1091 | 0 | if (Ce < 0) /* never happens */ |
1092 | 0 | Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER); |
1093 | 0 | *luv++ = (uint32_t)Le << 14 | Ce; |
1094 | 0 | luv3 += 3; |
1095 | 0 | } |
1096 | 0 | } |
1097 | | |
1098 | | #if !LOGLUV_PUBLIC |
1099 | | static |
1100 | | #endif |
1101 | | void |
1102 | | LogLuv32toXYZ(uint32_t p, float XYZ[3]) |
1103 | 0 | { |
1104 | 0 | double L, u, v, s, x, y; |
1105 | | /* decode luminance */ |
1106 | 0 | L = LogL16toY((int)p >> 16); |
1107 | 0 | if (L <= 0.) { |
1108 | 0 | XYZ[0] = XYZ[1] = XYZ[2] = 0.; |
1109 | 0 | return; |
1110 | 0 | } |
1111 | | /* decode color */ |
1112 | 0 | u = 1./UVSCALE * ((p>>8 & 0xff) + .5); |
1113 | 0 | v = 1./UVSCALE * ((p & 0xff) + .5); |
1114 | 0 | s = 1./(6.*u - 16.*v + 12.); |
1115 | 0 | x = 9.*u * s; |
1116 | 0 | y = 4.*v * s; |
1117 | | /* convert to XYZ */ |
1118 | 0 | XYZ[0] = (float)(x/y * L); |
1119 | 0 | XYZ[1] = (float)L; |
1120 | 0 | XYZ[2] = (float)((1.-x-y)/y * L); |
1121 | 0 | } |
1122 | | |
1123 | | #if !LOGLUV_PUBLIC |
1124 | | static |
1125 | | #endif |
1126 | | uint32_t |
1127 | | LogLuv32fromXYZ(float XYZ[3], int em) |
1128 | 0 | { |
1129 | 0 | unsigned int Le, ue, ve; |
1130 | 0 | double u, v, s; |
1131 | | /* encode luminance */ |
1132 | 0 | Le = (unsigned int)LogL16fromY(XYZ[1], em); |
1133 | | /* encode color */ |
1134 | 0 | s = XYZ[0] + 15.*XYZ[1] + 3.*XYZ[2]; |
1135 | 0 | if (!Le || s <= 0.) { |
1136 | 0 | u = U_NEU; |
1137 | 0 | v = V_NEU; |
1138 | 0 | } else { |
1139 | 0 | u = 4.*XYZ[0] / s; |
1140 | 0 | v = 9.*XYZ[1] / s; |
1141 | 0 | } |
1142 | 0 | if (u <= 0.) ue = 0; |
1143 | 0 | else ue = tiff_itrunc(UVSCALE*u, em); |
1144 | 0 | if (ue > 255) ue = 255; |
1145 | 0 | if (v <= 0.) ve = 0; |
1146 | 0 | else ve = tiff_itrunc(UVSCALE*v, em); |
1147 | 0 | if (ve > 255) ve = 255; |
1148 | | /* combine encodings */ |
1149 | 0 | return (Le << 16 | ue << 8 | ve); |
1150 | 0 | } |
1151 | | |
1152 | | static void |
1153 | | Luv32toXYZ(LogLuvState* sp, uint8_t* op, tmsize_t n) |
1154 | 0 | { |
1155 | 0 | uint32_t* luv = (uint32_t*) sp->tbuf; |
1156 | 0 | float* xyz = (float*) op; |
1157 | |
|
1158 | 0 | while (n-- > 0) { |
1159 | 0 | LogLuv32toXYZ(*luv++, xyz); |
1160 | 0 | xyz += 3; |
1161 | 0 | } |
1162 | 0 | } |
1163 | | |
1164 | | static void |
1165 | | Luv32toLuv48(LogLuvState* sp, uint8_t* op, tmsize_t n) |
1166 | 0 | { |
1167 | 0 | uint32_t* luv = (uint32_t*) sp->tbuf; |
1168 | 0 | int16_t* luv3 = (int16_t*) op; |
1169 | |
|
1170 | 0 | while (n-- > 0) { |
1171 | 0 | double u, v; |
1172 | |
|
1173 | 0 | *luv3++ = (int16_t)(*luv >> 16); |
1174 | 0 | u = 1./UVSCALE * ((*luv>>8 & 0xff) + .5); |
1175 | 0 | v = 1./UVSCALE * ((*luv & 0xff) + .5); |
1176 | 0 | *luv3++ = (int16_t)(u * (1L << 15)); |
1177 | 0 | *luv3++ = (int16_t)(v * (1L << 15)); |
1178 | 0 | luv++; |
1179 | 0 | } |
1180 | 0 | } |
1181 | | |
1182 | | static void |
1183 | | Luv32toRGB(LogLuvState* sp, uint8_t* op, tmsize_t n) |
1184 | 0 | { |
1185 | 0 | uint32_t* luv = (uint32_t*) sp->tbuf; |
1186 | 0 | uint8_t* rgb = (uint8_t*) op; |
1187 | |
|
1188 | 0 | while (n-- > 0) { |
1189 | 0 | float xyz[3]; |
1190 | |
|
1191 | 0 | LogLuv32toXYZ(*luv++, xyz); |
1192 | 0 | XYZtoRGB24(xyz, rgb); |
1193 | 0 | rgb += 3; |
1194 | 0 | } |
1195 | 0 | } |
1196 | | |
1197 | | static void |
1198 | | Luv32fromXYZ(LogLuvState* sp, uint8_t* op, tmsize_t n) |
1199 | 0 | { |
1200 | 0 | uint32_t* luv = (uint32_t*) sp->tbuf; |
1201 | 0 | float* xyz = (float*) op; |
1202 | |
|
1203 | 0 | while (n-- > 0) { |
1204 | 0 | *luv++ = LogLuv32fromXYZ(xyz, sp->encode_meth); |
1205 | 0 | xyz += 3; |
1206 | 0 | } |
1207 | 0 | } |
1208 | | |
1209 | | static void |
1210 | | Luv32fromLuv48(LogLuvState* sp, uint8_t* op, tmsize_t n) |
1211 | 0 | { |
1212 | 0 | uint32_t* luv = (uint32_t*) sp->tbuf; |
1213 | 0 | int16_t* luv3 = (int16_t*) op; |
1214 | |
|
1215 | 0 | if (sp->encode_meth == SGILOGENCODE_NODITHER) { |
1216 | 0 | while (n-- > 0) { |
1217 | 0 | *luv++ = (uint32_t)luv3[0] << 16 | |
1218 | 0 | (luv3[1]*(uint32_t)(UVSCALE + .5) >> 7 & 0xff00) | |
1219 | 0 | (luv3[2]*(uint32_t)(UVSCALE + .5) >> 15 & 0xff); |
1220 | 0 | luv3 += 3; |
1221 | 0 | } |
1222 | 0 | return; |
1223 | 0 | } |
1224 | 0 | while (n-- > 0) { |
1225 | 0 | *luv++ = (uint32_t)luv3[0] << 16 | |
1226 | 0 | (tiff_itrunc(luv3[1]*(UVSCALE/(1<<15)), sp->encode_meth) << 8 & 0xff00) | |
1227 | 0 | (tiff_itrunc(luv3[2]*(UVSCALE/(1<<15)), sp->encode_meth) & 0xff); |
1228 | 0 | luv3 += 3; |
1229 | 0 | } |
1230 | 0 | } |
1231 | | |
1232 | | static void |
1233 | | _logLuvNop(LogLuvState* sp, uint8_t* op, tmsize_t n) |
1234 | 0 | { |
1235 | 0 | (void) sp; (void) op; (void) n; |
1236 | 0 | } |
1237 | | |
1238 | | static int |
1239 | | LogL16GuessDataFmt(TIFFDirectory *td) |
1240 | 0 | { |
1241 | 0 | #define PACK(s,b,f) (((b)<<6)|((s)<<3)|(f)) |
1242 | 0 | switch (PACK(td->td_samplesperpixel, td->td_bitspersample, td->td_sampleformat)) { |
1243 | 0 | case PACK(1, 32, SAMPLEFORMAT_IEEEFP): |
1244 | 0 | return (SGILOGDATAFMT_FLOAT); |
1245 | 0 | case PACK(1, 16, SAMPLEFORMAT_VOID): |
1246 | 0 | case PACK(1, 16, SAMPLEFORMAT_INT): |
1247 | 0 | case PACK(1, 16, SAMPLEFORMAT_UINT): |
1248 | 0 | return (SGILOGDATAFMT_16BIT); |
1249 | 0 | case PACK(1, 8, SAMPLEFORMAT_VOID): |
1250 | 0 | case PACK(1, 8, SAMPLEFORMAT_UINT): |
1251 | 0 | return (SGILOGDATAFMT_8BIT); |
1252 | 0 | } |
1253 | 0 | #undef PACK |
1254 | 0 | return (SGILOGDATAFMT_UNKNOWN); |
1255 | 0 | } |
1256 | | |
1257 | | static tmsize_t |
1258 | | multiply_ms(tmsize_t m1, tmsize_t m2) |
1259 | 0 | { |
1260 | 0 | return _TIFFMultiplySSize(NULL, m1, m2, NULL); |
1261 | 0 | } |
1262 | | |
1263 | | static int |
1264 | | LogL16InitState(TIFF* tif) |
1265 | 0 | { |
1266 | 0 | static const char module[] = "LogL16InitState"; |
1267 | 0 | TIFFDirectory *td = &tif->tif_dir; |
1268 | 0 | LogLuvState* sp = DecoderState(tif); |
1269 | |
|
1270 | 0 | assert(sp != NULL); |
1271 | 0 | assert(td->td_photometric == PHOTOMETRIC_LOGL); |
1272 | |
|
1273 | 0 | if( td->td_samplesperpixel != 1 ) |
1274 | 0 | { |
1275 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
1276 | 0 | "Sorry, can not handle LogL image with %s=%"PRIu16, |
1277 | 0 | "Samples/pixel", td->td_samplesperpixel); |
1278 | 0 | return 0; |
1279 | 0 | } |
1280 | | |
1281 | | /* for some reason, we can't do this in TIFFInitLogL16 */ |
1282 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN) |
1283 | 0 | sp->user_datafmt = LogL16GuessDataFmt(td); |
1284 | 0 | switch (sp->user_datafmt) { |
1285 | 0 | case SGILOGDATAFMT_FLOAT: |
1286 | 0 | sp->pixel_size = sizeof (float); |
1287 | 0 | break; |
1288 | 0 | case SGILOGDATAFMT_16BIT: |
1289 | 0 | sp->pixel_size = sizeof (int16_t); |
1290 | 0 | break; |
1291 | 0 | case SGILOGDATAFMT_8BIT: |
1292 | 0 | sp->pixel_size = sizeof (uint8_t); |
1293 | 0 | break; |
1294 | 0 | default: |
1295 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
1296 | 0 | "No support for converting user data format to LogL"); |
1297 | 0 | return (0); |
1298 | 0 | } |
1299 | 0 | if( isTiled(tif) ) |
1300 | 0 | sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength); |
1301 | 0 | else if( td->td_rowsperstrip < td->td_imagelength ) |
1302 | 0 | sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip); |
1303 | 0 | else |
1304 | 0 | sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength); |
1305 | 0 | if (multiply_ms(sp->tbuflen, sizeof (int16_t)) == 0 || |
1306 | 0 | (sp->tbuf = (uint8_t*) _TIFFmalloc(sp->tbuflen * sizeof (int16_t))) == NULL) { |
1307 | 0 | TIFFErrorExt(tif->tif_clientdata, module, "No space for SGILog translation buffer"); |
1308 | 0 | return (0); |
1309 | 0 | } |
1310 | 0 | return (1); |
1311 | 0 | } |
1312 | | |
1313 | | static int |
1314 | | LogLuvGuessDataFmt(TIFFDirectory *td) |
1315 | 0 | { |
1316 | 0 | int guess; |
1317 | | |
1318 | | /* |
1319 | | * If the user didn't tell us their datafmt, |
1320 | | * take our best guess from the bitspersample. |
1321 | | */ |
1322 | 0 | #define PACK(a,b) (((a)<<3)|(b)) |
1323 | 0 | switch (PACK(td->td_bitspersample, td->td_sampleformat)) { |
1324 | 0 | case PACK(32, SAMPLEFORMAT_IEEEFP): |
1325 | 0 | guess = SGILOGDATAFMT_FLOAT; |
1326 | 0 | break; |
1327 | 0 | case PACK(32, SAMPLEFORMAT_VOID): |
1328 | 0 | case PACK(32, SAMPLEFORMAT_UINT): |
1329 | 0 | case PACK(32, SAMPLEFORMAT_INT): |
1330 | 0 | guess = SGILOGDATAFMT_RAW; |
1331 | 0 | break; |
1332 | 0 | case PACK(16, SAMPLEFORMAT_VOID): |
1333 | 0 | case PACK(16, SAMPLEFORMAT_INT): |
1334 | 0 | case PACK(16, SAMPLEFORMAT_UINT): |
1335 | 0 | guess = SGILOGDATAFMT_16BIT; |
1336 | 0 | break; |
1337 | 0 | case PACK( 8, SAMPLEFORMAT_VOID): |
1338 | 0 | case PACK( 8, SAMPLEFORMAT_UINT): |
1339 | 0 | guess = SGILOGDATAFMT_8BIT; |
1340 | 0 | break; |
1341 | 0 | default: |
1342 | 0 | guess = SGILOGDATAFMT_UNKNOWN; |
1343 | 0 | break; |
1344 | 0 | #undef PACK |
1345 | 0 | } |
1346 | | /* |
1347 | | * Double-check samples per pixel. |
1348 | | */ |
1349 | 0 | switch (td->td_samplesperpixel) { |
1350 | 0 | case 1: |
1351 | 0 | if (guess != SGILOGDATAFMT_RAW) |
1352 | 0 | guess = SGILOGDATAFMT_UNKNOWN; |
1353 | 0 | break; |
1354 | 0 | case 3: |
1355 | 0 | if (guess == SGILOGDATAFMT_RAW) |
1356 | 0 | guess = SGILOGDATAFMT_UNKNOWN; |
1357 | 0 | break; |
1358 | 0 | default: |
1359 | 0 | guess = SGILOGDATAFMT_UNKNOWN; |
1360 | 0 | break; |
1361 | 0 | } |
1362 | 0 | return (guess); |
1363 | 0 | } |
1364 | | |
1365 | | static int |
1366 | | LogLuvInitState(TIFF* tif) |
1367 | 0 | { |
1368 | 0 | static const char module[] = "LogLuvInitState"; |
1369 | 0 | TIFFDirectory* td = &tif->tif_dir; |
1370 | 0 | LogLuvState* sp = DecoderState(tif); |
1371 | |
|
1372 | 0 | assert(sp != NULL); |
1373 | 0 | assert(td->td_photometric == PHOTOMETRIC_LOGLUV); |
1374 | | |
1375 | | /* for some reason, we can't do this in TIFFInitLogLuv */ |
1376 | 0 | if (td->td_planarconfig != PLANARCONFIG_CONTIG) { |
1377 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
1378 | 0 | "SGILog compression cannot handle non-contiguous data"); |
1379 | 0 | return (0); |
1380 | 0 | } |
1381 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN) |
1382 | 0 | sp->user_datafmt = LogLuvGuessDataFmt(td); |
1383 | 0 | switch (sp->user_datafmt) { |
1384 | 0 | case SGILOGDATAFMT_FLOAT: |
1385 | 0 | sp->pixel_size = 3*sizeof (float); |
1386 | 0 | break; |
1387 | 0 | case SGILOGDATAFMT_16BIT: |
1388 | 0 | sp->pixel_size = 3*sizeof (int16_t); |
1389 | 0 | break; |
1390 | 0 | case SGILOGDATAFMT_RAW: |
1391 | 0 | sp->pixel_size = sizeof (uint32_t); |
1392 | 0 | break; |
1393 | 0 | case SGILOGDATAFMT_8BIT: |
1394 | 0 | sp->pixel_size = 3*sizeof (uint8_t); |
1395 | 0 | break; |
1396 | 0 | default: |
1397 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
1398 | 0 | "No support for converting user data format to LogLuv"); |
1399 | 0 | return (0); |
1400 | 0 | } |
1401 | 0 | if( isTiled(tif) ) |
1402 | 0 | sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength); |
1403 | 0 | else if( td->td_rowsperstrip < td->td_imagelength ) |
1404 | 0 | sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip); |
1405 | 0 | else |
1406 | 0 | sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength); |
1407 | 0 | if (multiply_ms(sp->tbuflen, sizeof (uint32_t)) == 0 || |
1408 | 0 | (sp->tbuf = (uint8_t*) _TIFFmalloc(sp->tbuflen * sizeof (uint32_t))) == NULL) { |
1409 | 0 | TIFFErrorExt(tif->tif_clientdata, module, "No space for SGILog translation buffer"); |
1410 | 0 | return (0); |
1411 | 0 | } |
1412 | 0 | return (1); |
1413 | 0 | } |
1414 | | |
1415 | | static int |
1416 | | LogLuvFixupTags(TIFF* tif) |
1417 | 0 | { |
1418 | 0 | (void) tif; |
1419 | 0 | return (1); |
1420 | 0 | } |
1421 | | |
1422 | | static int |
1423 | | LogLuvSetupDecode(TIFF* tif) |
1424 | 0 | { |
1425 | 0 | static const char module[] = "LogLuvSetupDecode"; |
1426 | 0 | LogLuvState* sp = DecoderState(tif); |
1427 | 0 | TIFFDirectory* td = &tif->tif_dir; |
1428 | |
|
1429 | 0 | tif->tif_postdecode = _TIFFNoPostDecode; |
1430 | 0 | switch (td->td_photometric) { |
1431 | 0 | case PHOTOMETRIC_LOGLUV: |
1432 | 0 | if (!LogLuvInitState(tif)) |
1433 | 0 | break; |
1434 | 0 | if (td->td_compression == COMPRESSION_SGILOG24) { |
1435 | 0 | tif->tif_decoderow = LogLuvDecode24; |
1436 | 0 | switch (sp->user_datafmt) { |
1437 | 0 | case SGILOGDATAFMT_FLOAT: |
1438 | 0 | sp->tfunc = Luv24toXYZ; |
1439 | 0 | break; |
1440 | 0 | case SGILOGDATAFMT_16BIT: |
1441 | 0 | sp->tfunc = Luv24toLuv48; |
1442 | 0 | break; |
1443 | 0 | case SGILOGDATAFMT_8BIT: |
1444 | 0 | sp->tfunc = Luv24toRGB; |
1445 | 0 | break; |
1446 | 0 | } |
1447 | 0 | } else { |
1448 | 0 | tif->tif_decoderow = LogLuvDecode32; |
1449 | 0 | switch (sp->user_datafmt) { |
1450 | 0 | case SGILOGDATAFMT_FLOAT: |
1451 | 0 | sp->tfunc = Luv32toXYZ; |
1452 | 0 | break; |
1453 | 0 | case SGILOGDATAFMT_16BIT: |
1454 | 0 | sp->tfunc = Luv32toLuv48; |
1455 | 0 | break; |
1456 | 0 | case SGILOGDATAFMT_8BIT: |
1457 | 0 | sp->tfunc = Luv32toRGB; |
1458 | 0 | break; |
1459 | 0 | } |
1460 | 0 | } |
1461 | 0 | return (1); |
1462 | 0 | case PHOTOMETRIC_LOGL: |
1463 | 0 | if (!LogL16InitState(tif)) |
1464 | 0 | break; |
1465 | 0 | tif->tif_decoderow = LogL16Decode; |
1466 | 0 | switch (sp->user_datafmt) { |
1467 | 0 | case SGILOGDATAFMT_FLOAT: |
1468 | 0 | sp->tfunc = L16toY; |
1469 | 0 | break; |
1470 | 0 | case SGILOGDATAFMT_8BIT: |
1471 | 0 | sp->tfunc = L16toGry; |
1472 | 0 | break; |
1473 | 0 | } |
1474 | 0 | return (1); |
1475 | 0 | default: |
1476 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
1477 | 0 | "Inappropriate photometric interpretation %"PRIu16" for SGILog compression; %s", |
1478 | 0 | td->td_photometric, "must be either LogLUV or LogL"); |
1479 | 0 | break; |
1480 | 0 | } |
1481 | 0 | return (0); |
1482 | 0 | } |
1483 | | |
1484 | | static int |
1485 | | LogLuvSetupEncode(TIFF* tif) |
1486 | 0 | { |
1487 | 0 | static const char module[] = "LogLuvSetupEncode"; |
1488 | 0 | LogLuvState* sp = EncoderState(tif); |
1489 | 0 | TIFFDirectory* td = &tif->tif_dir; |
1490 | |
|
1491 | 0 | switch (td->td_photometric) { |
1492 | 0 | case PHOTOMETRIC_LOGLUV: |
1493 | 0 | if (!LogLuvInitState(tif)) |
1494 | 0 | return (0); |
1495 | 0 | if (td->td_compression == COMPRESSION_SGILOG24) { |
1496 | 0 | tif->tif_encoderow = LogLuvEncode24; |
1497 | 0 | switch (sp->user_datafmt) { |
1498 | 0 | case SGILOGDATAFMT_FLOAT: |
1499 | 0 | sp->tfunc = Luv24fromXYZ; |
1500 | 0 | break; |
1501 | 0 | case SGILOGDATAFMT_16BIT: |
1502 | 0 | sp->tfunc = Luv24fromLuv48; |
1503 | 0 | break; |
1504 | 0 | case SGILOGDATAFMT_RAW: |
1505 | 0 | break; |
1506 | 0 | default: |
1507 | 0 | goto notsupported; |
1508 | 0 | } |
1509 | 0 | } else { |
1510 | 0 | tif->tif_encoderow = LogLuvEncode32; |
1511 | 0 | switch (sp->user_datafmt) { |
1512 | 0 | case SGILOGDATAFMT_FLOAT: |
1513 | 0 | sp->tfunc = Luv32fromXYZ; |
1514 | 0 | break; |
1515 | 0 | case SGILOGDATAFMT_16BIT: |
1516 | 0 | sp->tfunc = Luv32fromLuv48; |
1517 | 0 | break; |
1518 | 0 | case SGILOGDATAFMT_RAW: |
1519 | 0 | break; |
1520 | 0 | default: |
1521 | 0 | goto notsupported; |
1522 | 0 | } |
1523 | 0 | } |
1524 | 0 | break; |
1525 | 0 | case PHOTOMETRIC_LOGL: |
1526 | 0 | if (!LogL16InitState(tif)) |
1527 | 0 | return (0); |
1528 | 0 | tif->tif_encoderow = LogL16Encode; |
1529 | 0 | switch (sp->user_datafmt) { |
1530 | 0 | case SGILOGDATAFMT_FLOAT: |
1531 | 0 | sp->tfunc = L16fromY; |
1532 | 0 | break; |
1533 | 0 | case SGILOGDATAFMT_16BIT: |
1534 | 0 | break; |
1535 | 0 | default: |
1536 | 0 | goto notsupported; |
1537 | 0 | } |
1538 | 0 | break; |
1539 | 0 | default: |
1540 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
1541 | 0 | "Inappropriate photometric interpretation %"PRIu16" for SGILog compression; %s", |
1542 | 0 | td->td_photometric, "must be either LogLUV or LogL"); |
1543 | 0 | return (0); |
1544 | 0 | } |
1545 | 0 | sp->encoder_state = 1; |
1546 | 0 | return (1); |
1547 | 0 | notsupported: |
1548 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
1549 | 0 | "SGILog compression supported only for %s, or raw data", |
1550 | 0 | td->td_photometric == PHOTOMETRIC_LOGL ? "Y, L" : "XYZ, Luv"); |
1551 | 0 | return (0); |
1552 | 0 | } |
1553 | | |
1554 | | static void |
1555 | | LogLuvClose(TIFF* tif) |
1556 | 0 | { |
1557 | 0 | LogLuvState* sp = (LogLuvState*) tif->tif_data; |
1558 | 0 | TIFFDirectory *td = &tif->tif_dir; |
1559 | |
|
1560 | 0 | assert(sp != 0); |
1561 | | /* |
1562 | | * For consistency, we always want to write out the same |
1563 | | * bitspersample and sampleformat for our TIFF file, |
1564 | | * regardless of the data format being used by the application. |
1565 | | * Since this routine is called after tags have been set but |
1566 | | * before they have been recorded in the file, we reset them here. |
1567 | | * Note: this is really a nasty approach. See PixarLogClose |
1568 | | */ |
1569 | 0 | if( sp->encoder_state ) |
1570 | 0 | { |
1571 | | /* See PixarLogClose. Might avoid issues with tags whose size depends |
1572 | | * on those below, but not completely sure this is enough. */ |
1573 | 0 | td->td_samplesperpixel = |
1574 | 0 | (td->td_photometric == PHOTOMETRIC_LOGL) ? 1 : 3; |
1575 | 0 | td->td_bitspersample = 16; |
1576 | 0 | td->td_sampleformat = SAMPLEFORMAT_INT; |
1577 | 0 | } |
1578 | 0 | } |
1579 | | |
1580 | | static void |
1581 | | LogLuvCleanup(TIFF* tif) |
1582 | 0 | { |
1583 | 0 | LogLuvState* sp = (LogLuvState *)tif->tif_data; |
1584 | |
|
1585 | 0 | assert(sp != 0); |
1586 | |
|
1587 | 0 | tif->tif_tagmethods.vgetfield = sp->vgetparent; |
1588 | 0 | tif->tif_tagmethods.vsetfield = sp->vsetparent; |
1589 | |
|
1590 | 0 | if (sp->tbuf) |
1591 | 0 | _TIFFfree(sp->tbuf); |
1592 | 0 | _TIFFfree(sp); |
1593 | 0 | tif->tif_data = NULL; |
1594 | |
|
1595 | 0 | _TIFFSetDefaultCompressionState(tif); |
1596 | 0 | } |
1597 | | |
1598 | | static int |
1599 | | LogLuvVSetField(TIFF* tif, uint32_t tag, va_list ap) |
1600 | 0 | { |
1601 | 0 | static const char module[] = "LogLuvVSetField"; |
1602 | 0 | LogLuvState* sp = DecoderState(tif); |
1603 | 0 | int bps, fmt; |
1604 | |
|
1605 | 0 | switch (tag) { |
1606 | 0 | case TIFFTAG_SGILOGDATAFMT: |
1607 | 0 | sp->user_datafmt = (int) va_arg(ap, int); |
1608 | | /* |
1609 | | * Tweak the TIFF header so that the rest of libtiff knows what |
1610 | | * size of data will be passed between app and library, and |
1611 | | * assume that the app knows what it is doing and is not |
1612 | | * confused by these header manipulations... |
1613 | | */ |
1614 | 0 | switch (sp->user_datafmt) { |
1615 | 0 | case SGILOGDATAFMT_FLOAT: |
1616 | 0 | bps = 32; |
1617 | 0 | fmt = SAMPLEFORMAT_IEEEFP; |
1618 | 0 | break; |
1619 | 0 | case SGILOGDATAFMT_16BIT: |
1620 | 0 | bps = 16; |
1621 | 0 | fmt = SAMPLEFORMAT_INT; |
1622 | 0 | break; |
1623 | 0 | case SGILOGDATAFMT_RAW: |
1624 | 0 | bps = 32; |
1625 | 0 | fmt = SAMPLEFORMAT_UINT; |
1626 | 0 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); |
1627 | 0 | break; |
1628 | 0 | case SGILOGDATAFMT_8BIT: |
1629 | 0 | bps = 8; |
1630 | 0 | fmt = SAMPLEFORMAT_UINT; |
1631 | 0 | break; |
1632 | 0 | default: |
1633 | 0 | TIFFErrorExt(tif->tif_clientdata, tif->tif_name, |
1634 | 0 | "Unknown data format %d for LogLuv compression", |
1635 | 0 | sp->user_datafmt); |
1636 | 0 | return (0); |
1637 | 0 | } |
1638 | 0 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps); |
1639 | 0 | TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, fmt); |
1640 | | /* |
1641 | | * Must recalculate sizes should bits/sample change. |
1642 | | */ |
1643 | 0 | tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t) -1; |
1644 | 0 | tif->tif_scanlinesize = TIFFScanlineSize(tif); |
1645 | 0 | return (1); |
1646 | 0 | case TIFFTAG_SGILOGENCODE: |
1647 | 0 | sp->encode_meth = (int) va_arg(ap, int); |
1648 | 0 | if (sp->encode_meth != SGILOGENCODE_NODITHER && |
1649 | 0 | sp->encode_meth != SGILOGENCODE_RANDITHER) { |
1650 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
1651 | 0 | "Unknown encoding %d for LogLuv compression", |
1652 | 0 | sp->encode_meth); |
1653 | 0 | return (0); |
1654 | 0 | } |
1655 | 0 | return (1); |
1656 | 0 | default: |
1657 | 0 | return (*sp->vsetparent)(tif, tag, ap); |
1658 | 0 | } |
1659 | 0 | } |
1660 | | |
1661 | | static int |
1662 | | LogLuvVGetField(TIFF* tif, uint32_t tag, va_list ap) |
1663 | 0 | { |
1664 | 0 | LogLuvState *sp = (LogLuvState *)tif->tif_data; |
1665 | |
|
1666 | 0 | switch (tag) { |
1667 | 0 | case TIFFTAG_SGILOGDATAFMT: |
1668 | 0 | *va_arg(ap, int*) = sp->user_datafmt; |
1669 | 0 | return (1); |
1670 | 0 | default: |
1671 | 0 | return (*sp->vgetparent)(tif, tag, ap); |
1672 | 0 | } |
1673 | 0 | } |
1674 | | |
1675 | | static const TIFFField LogLuvFields[] = { |
1676 | | { TIFFTAG_SGILOGDATAFMT, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "SGILogDataFmt", NULL}, |
1677 | | { TIFFTAG_SGILOGENCODE, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "SGILogEncode", NULL} |
1678 | | }; |
1679 | | |
1680 | | int |
1681 | | TIFFInitSGILog(TIFF* tif, int scheme) |
1682 | 0 | { |
1683 | 0 | static const char module[] = "TIFFInitSGILog"; |
1684 | 0 | LogLuvState* sp; |
1685 | |
|
1686 | 0 | assert(scheme == COMPRESSION_SGILOG24 || scheme == COMPRESSION_SGILOG); |
1687 | | |
1688 | | /* |
1689 | | * Merge codec-specific tag information. |
1690 | | */ |
1691 | 0 | if (!_TIFFMergeFields(tif, LogLuvFields, |
1692 | 0 | TIFFArrayCount(LogLuvFields))) { |
1693 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
1694 | 0 | "Merging SGILog codec-specific tags failed"); |
1695 | 0 | return 0; |
1696 | 0 | } |
1697 | | |
1698 | | /* |
1699 | | * Allocate state block so tag methods have storage to record values. |
1700 | | */ |
1701 | 0 | tif->tif_data = (uint8_t*) _TIFFmalloc(sizeof (LogLuvState)); |
1702 | 0 | if (tif->tif_data == NULL) |
1703 | 0 | goto bad; |
1704 | 0 | sp = (LogLuvState*) tif->tif_data; |
1705 | 0 | _TIFFmemset((void*)sp, 0, sizeof (*sp)); |
1706 | 0 | sp->user_datafmt = SGILOGDATAFMT_UNKNOWN; |
1707 | 0 | sp->encode_meth = (scheme == COMPRESSION_SGILOG24) ? |
1708 | 0 | SGILOGENCODE_RANDITHER : SGILOGENCODE_NODITHER; |
1709 | 0 | sp->tfunc = _logLuvNop; |
1710 | | |
1711 | | /* |
1712 | | * Install codec methods. |
1713 | | * NB: tif_decoderow & tif_encoderow are filled |
1714 | | * in at setup time. |
1715 | | */ |
1716 | 0 | tif->tif_fixuptags = LogLuvFixupTags; |
1717 | 0 | tif->tif_setupdecode = LogLuvSetupDecode; |
1718 | 0 | tif->tif_decodestrip = LogLuvDecodeStrip; |
1719 | 0 | tif->tif_decodetile = LogLuvDecodeTile; |
1720 | 0 | tif->tif_setupencode = LogLuvSetupEncode; |
1721 | 0 | tif->tif_encodestrip = LogLuvEncodeStrip; |
1722 | 0 | tif->tif_encodetile = LogLuvEncodeTile; |
1723 | 0 | tif->tif_close = LogLuvClose; |
1724 | 0 | tif->tif_cleanup = LogLuvCleanup; |
1725 | | |
1726 | | /* |
1727 | | * Override parent get/set field methods. |
1728 | | */ |
1729 | 0 | sp->vgetparent = tif->tif_tagmethods.vgetfield; |
1730 | 0 | tif->tif_tagmethods.vgetfield = LogLuvVGetField; /* hook for codec tags */ |
1731 | 0 | sp->vsetparent = tif->tif_tagmethods.vsetfield; |
1732 | 0 | tif->tif_tagmethods.vsetfield = LogLuvVSetField; /* hook for codec tags */ |
1733 | |
|
1734 | 0 | return (1); |
1735 | 0 | bad: |
1736 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
1737 | 0 | "%s: No space for LogLuv state block", tif->tif_name); |
1738 | 0 | return (0); |
1739 | 0 | } |
1740 | | #endif /* LOGLUV_SUPPORT */ |
1741 | | |
1742 | | /* vim: set ts=8 sts=8 sw=8 noet: */ |
1743 | | /* |
1744 | | * Local Variables: |
1745 | | * mode: c |
1746 | | * c-basic-offset: 8 |
1747 | | * fill-column: 78 |
1748 | | * End: |
1749 | | */ |