/src/freeimage-svn/FreeImage/trunk/Source/LibTIFF4/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 <math.h> |
149 | | #include <stdio.h> |
150 | | #include <stdlib.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 | | { |
160 | | int encoder_state; /* 1 if encoder correctly initialized */ |
161 | | int user_datafmt; /* user data format */ |
162 | | int encode_meth; /* encoding method */ |
163 | | int pixel_size; /* bytes per pixel */ |
164 | | |
165 | | uint8_t *tbuf; /* translation buffer */ |
166 | | tmsize_t tbuflen; /* buffer length */ |
167 | | void (*tfunc)(LogLuvState *, uint8_t *, tmsize_t); |
168 | | |
169 | | TIFFVSetMethod vgetparent; /* super-class method */ |
170 | | TIFFVSetMethod vsetparent; /* super-class method */ |
171 | | }; |
172 | | |
173 | 0 | #define DecoderState(tif) ((LogLuvState *)(tif)->tif_data) |
174 | 0 | #define EncoderState(tif) ((LogLuvState *)(tif)->tif_data) |
175 | | |
176 | 0 | #define SGILOGDATAFMT_UNKNOWN -1 |
177 | | |
178 | 0 | #define MINRUN 4 /* minimum run length */ |
179 | | |
180 | | /* |
181 | | * Decode a string of 16-bit gray pixels. |
182 | | */ |
183 | | static int 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 | { |
206 | 0 | if (sp->tbuflen < npixels) |
207 | 0 | { |
208 | 0 | TIFFErrorExtR(tif, module, "Translation buffer too short"); |
209 | 0 | return (0); |
210 | 0 | } |
211 | 0 | tp = (int16_t *)sp->tbuf; |
212 | 0 | } |
213 | 0 | _TIFFmemset((void *)tp, 0, npixels * sizeof(tp[0])); |
214 | |
|
215 | 0 | bp = (unsigned char *)tif->tif_rawcp; |
216 | 0 | cc = tif->tif_rawcc; |
217 | | /* get each byte string */ |
218 | 0 | for (shft = 8; shft >= 0; shft -= 8) |
219 | 0 | { |
220 | 0 | for (i = 0; i < npixels && cc > 0;) |
221 | 0 | { |
222 | 0 | if (*bp >= 128) |
223 | 0 | { /* run */ |
224 | 0 | if (cc < 2) |
225 | 0 | break; |
226 | 0 | rc = *bp++ + (2 - 128); |
227 | 0 | b = (int16_t)(*bp++ << shft); |
228 | 0 | cc -= 2; |
229 | 0 | while (rc-- && i < npixels) |
230 | 0 | tp[i++] |= b; |
231 | 0 | } |
232 | 0 | else |
233 | 0 | { /* non-run */ |
234 | 0 | rc = *bp++; /* nul is noop */ |
235 | 0 | while (--cc && rc-- && i < npixels) |
236 | 0 | tp[i++] |= (int16_t)*bp++ << shft; |
237 | 0 | } |
238 | 0 | } |
239 | 0 | if (i != npixels) |
240 | 0 | { |
241 | 0 | TIFFErrorExtR(tif, module, |
242 | 0 | "Not enough data at row %" PRIu32 |
243 | 0 | " (short %" TIFF_SSIZE_FORMAT " pixels)", |
244 | 0 | tif->tif_row, npixels - i); |
245 | 0 | tif->tif_rawcp = (uint8_t *)bp; |
246 | 0 | tif->tif_rawcc = cc; |
247 | 0 | return (0); |
248 | 0 | } |
249 | 0 | } |
250 | 0 | (*sp->tfunc)(sp, op, npixels); |
251 | 0 | tif->tif_rawcp = (uint8_t *)bp; |
252 | 0 | tif->tif_rawcc = cc; |
253 | 0 | return (1); |
254 | 0 | } |
255 | | |
256 | | /* |
257 | | * Decode a string of 24-bit pixels. |
258 | | */ |
259 | | static int LogLuvDecode24(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s) |
260 | 0 | { |
261 | 0 | static const char module[] = "LogLuvDecode24"; |
262 | 0 | LogLuvState *sp = DecoderState(tif); |
263 | 0 | tmsize_t cc; |
264 | 0 | tmsize_t i; |
265 | 0 | tmsize_t npixels; |
266 | 0 | unsigned char *bp; |
267 | 0 | uint32_t *tp; |
268 | |
|
269 | 0 | (void)s; |
270 | 0 | assert(s == 0); |
271 | 0 | assert(sp != NULL); |
272 | | |
273 | 0 | npixels = occ / sp->pixel_size; |
274 | |
|
275 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_RAW) |
276 | 0 | tp = (uint32_t *)op; |
277 | 0 | else |
278 | 0 | { |
279 | 0 | if (sp->tbuflen < npixels) |
280 | 0 | { |
281 | 0 | TIFFErrorExtR(tif, module, "Translation buffer too short"); |
282 | 0 | return (0); |
283 | 0 | } |
284 | 0 | tp = (uint32_t *)sp->tbuf; |
285 | 0 | } |
286 | | /* copy to array of uint32_t */ |
287 | 0 | bp = (unsigned char *)tif->tif_rawcp; |
288 | 0 | cc = tif->tif_rawcc; |
289 | 0 | for (i = 0; i < npixels && cc >= 3; i++) |
290 | 0 | { |
291 | 0 | tp[i] = bp[0] << 16 | bp[1] << 8 | bp[2]; |
292 | 0 | bp += 3; |
293 | 0 | cc -= 3; |
294 | 0 | } |
295 | 0 | tif->tif_rawcp = (uint8_t *)bp; |
296 | 0 | tif->tif_rawcc = cc; |
297 | 0 | if (i != npixels) |
298 | 0 | { |
299 | 0 | TIFFErrorExtR(tif, module, |
300 | 0 | "Not enough data at row %" PRIu32 |
301 | 0 | " (short %" TIFF_SSIZE_FORMAT " pixels)", |
302 | 0 | tif->tif_row, npixels - i); |
303 | 0 | return (0); |
304 | 0 | } |
305 | 0 | (*sp->tfunc)(sp, op, npixels); |
306 | 0 | return (1); |
307 | 0 | } |
308 | | |
309 | | /* |
310 | | * Decode a string of 32-bit pixels. |
311 | | */ |
312 | | static int LogLuvDecode32(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s) |
313 | 0 | { |
314 | 0 | static const char module[] = "LogLuvDecode32"; |
315 | 0 | LogLuvState *sp; |
316 | 0 | int shft; |
317 | 0 | tmsize_t i; |
318 | 0 | tmsize_t npixels; |
319 | 0 | unsigned char *bp; |
320 | 0 | uint32_t *tp; |
321 | 0 | uint32_t b; |
322 | 0 | tmsize_t cc; |
323 | 0 | int rc; |
324 | |
|
325 | 0 | (void)s; |
326 | 0 | assert(s == 0); |
327 | 0 | sp = DecoderState(tif); |
328 | 0 | assert(sp != NULL); |
329 | | |
330 | 0 | npixels = occ / sp->pixel_size; |
331 | |
|
332 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_RAW) |
333 | 0 | tp = (uint32_t *)op; |
334 | 0 | else |
335 | 0 | { |
336 | 0 | if (sp->tbuflen < npixels) |
337 | 0 | { |
338 | 0 | TIFFErrorExtR(tif, module, "Translation buffer too short"); |
339 | 0 | return (0); |
340 | 0 | } |
341 | 0 | tp = (uint32_t *)sp->tbuf; |
342 | 0 | } |
343 | 0 | _TIFFmemset((void *)tp, 0, npixels * sizeof(tp[0])); |
344 | |
|
345 | 0 | bp = (unsigned char *)tif->tif_rawcp; |
346 | 0 | cc = tif->tif_rawcc; |
347 | | /* get each byte string */ |
348 | 0 | for (shft = 24; shft >= 0; shft -= 8) |
349 | 0 | { |
350 | 0 | for (i = 0; i < npixels && cc > 0;) |
351 | 0 | { |
352 | 0 | if (*bp >= 128) |
353 | 0 | { /* run */ |
354 | 0 | if (cc < 2) |
355 | 0 | break; |
356 | 0 | rc = *bp++ + (2 - 128); |
357 | 0 | b = (uint32_t)*bp++ << shft; |
358 | 0 | cc -= 2; |
359 | 0 | while (rc-- && i < npixels) |
360 | 0 | tp[i++] |= b; |
361 | 0 | } |
362 | 0 | else |
363 | 0 | { /* non-run */ |
364 | 0 | rc = *bp++; /* nul is noop */ |
365 | 0 | while (--cc && rc-- && i < npixels) |
366 | 0 | tp[i++] |= (uint32_t)*bp++ << shft; |
367 | 0 | } |
368 | 0 | } |
369 | 0 | if (i != npixels) |
370 | 0 | { |
371 | 0 | TIFFErrorExtR(tif, module, |
372 | 0 | "Not enough data at row %" PRIu32 |
373 | 0 | " (short %" TIFF_SSIZE_FORMAT " pixels)", |
374 | 0 | tif->tif_row, npixels - i); |
375 | 0 | tif->tif_rawcp = (uint8_t *)bp; |
376 | 0 | tif->tif_rawcc = cc; |
377 | 0 | return (0); |
378 | 0 | } |
379 | 0 | } |
380 | 0 | (*sp->tfunc)(sp, op, npixels); |
381 | 0 | tif->tif_rawcp = (uint8_t *)bp; |
382 | 0 | tif->tif_rawcc = cc; |
383 | 0 | return (1); |
384 | 0 | } |
385 | | |
386 | | /* |
387 | | * Decode a strip of pixels. We break it into rows to |
388 | | * maintain synchrony with the encode algorithm, which |
389 | | * is row by row. |
390 | | */ |
391 | | static int LogLuvDecodeStrip(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) |
392 | 0 | { |
393 | 0 | tmsize_t rowlen = TIFFScanlineSize(tif); |
394 | |
|
395 | 0 | if (rowlen == 0) |
396 | 0 | return 0; |
397 | | |
398 | 0 | assert(cc % rowlen == 0); |
399 | 0 | while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s)) |
400 | 0 | { |
401 | 0 | bp += rowlen; |
402 | 0 | cc -= rowlen; |
403 | 0 | } |
404 | 0 | return (cc == 0); |
405 | 0 | } |
406 | | |
407 | | /* |
408 | | * Decode a tile of pixels. We break it into rows to |
409 | | * maintain synchrony with the encode algorithm, which |
410 | | * is row by row. |
411 | | */ |
412 | | static int LogLuvDecodeTile(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) |
413 | 0 | { |
414 | 0 | tmsize_t rowlen = TIFFTileRowSize(tif); |
415 | |
|
416 | 0 | if (rowlen == 0) |
417 | 0 | return 0; |
418 | | |
419 | 0 | assert(cc % rowlen == 0); |
420 | 0 | while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s)) |
421 | 0 | { |
422 | 0 | bp += rowlen; |
423 | 0 | cc -= rowlen; |
424 | 0 | } |
425 | 0 | return (cc == 0); |
426 | 0 | } |
427 | | |
428 | | /* |
429 | | * Encode a row of 16-bit pixels. |
430 | | */ |
431 | | static int LogL16Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) |
432 | 0 | { |
433 | 0 | static const char module[] = "LogL16Encode"; |
434 | 0 | LogLuvState *sp = EncoderState(tif); |
435 | 0 | int shft; |
436 | 0 | tmsize_t i; |
437 | 0 | tmsize_t j; |
438 | 0 | tmsize_t npixels; |
439 | 0 | uint8_t *op; |
440 | 0 | int16_t *tp; |
441 | 0 | int16_t b; |
442 | 0 | tmsize_t occ; |
443 | 0 | int rc = 0, mask; |
444 | 0 | tmsize_t beg; |
445 | |
|
446 | 0 | (void)s; |
447 | 0 | assert(s == 0); |
448 | 0 | assert(sp != NULL); |
449 | 0 | npixels = cc / sp->pixel_size; |
450 | |
|
451 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_16BIT) |
452 | 0 | tp = (int16_t *)bp; |
453 | 0 | else |
454 | 0 | { |
455 | 0 | tp = (int16_t *)sp->tbuf; |
456 | 0 | if (sp->tbuflen < npixels) |
457 | 0 | { |
458 | 0 | TIFFErrorExtR(tif, module, "Translation buffer too short"); |
459 | 0 | return (0); |
460 | 0 | } |
461 | 0 | (*sp->tfunc)(sp, bp, npixels); |
462 | 0 | } |
463 | | /* compress each byte string */ |
464 | 0 | op = tif->tif_rawcp; |
465 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
466 | 0 | for (shft = 8; shft >= 0; shft -= 8) |
467 | 0 | { |
468 | 0 | for (i = 0; i < npixels; i += rc) |
469 | 0 | { |
470 | 0 | if (occ < 4) |
471 | 0 | { |
472 | 0 | tif->tif_rawcp = op; |
473 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
474 | 0 | if (!TIFFFlushData1(tif)) |
475 | 0 | return (0); |
476 | 0 | op = tif->tif_rawcp; |
477 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
478 | 0 | } |
479 | 0 | mask = 0xff << shft; /* find next run */ |
480 | 0 | for (beg = i; beg < npixels; beg += rc) |
481 | 0 | { |
482 | 0 | b = (int16_t)(tp[beg] & mask); |
483 | 0 | rc = 1; |
484 | 0 | while (rc < 127 + 2 && beg + rc < npixels && |
485 | 0 | (tp[beg + rc] & mask) == b) |
486 | 0 | rc++; |
487 | 0 | if (rc >= MINRUN) |
488 | 0 | break; /* long enough */ |
489 | 0 | } |
490 | 0 | if (beg - i > 1 && beg - i < MINRUN) |
491 | 0 | { |
492 | 0 | b = (int16_t)(tp[i] & mask); /*check short run */ |
493 | 0 | j = i + 1; |
494 | 0 | while ((tp[j++] & mask) == b) |
495 | 0 | if (j == beg) |
496 | 0 | { |
497 | 0 | *op++ = (uint8_t)(128 - 2 + j - i); |
498 | 0 | *op++ = (uint8_t)(b >> shft); |
499 | 0 | occ -= 2; |
500 | 0 | i = beg; |
501 | 0 | break; |
502 | 0 | } |
503 | 0 | } |
504 | 0 | while (i < beg) |
505 | 0 | { /* write out non-run */ |
506 | 0 | if ((j = beg - i) > 127) |
507 | 0 | j = 127; |
508 | 0 | if (occ < j + 3) |
509 | 0 | { |
510 | 0 | tif->tif_rawcp = op; |
511 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
512 | 0 | if (!TIFFFlushData1(tif)) |
513 | 0 | return (0); |
514 | 0 | op = tif->tif_rawcp; |
515 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
516 | 0 | } |
517 | 0 | *op++ = (uint8_t)j; |
518 | 0 | occ--; |
519 | 0 | while (j--) |
520 | 0 | { |
521 | 0 | *op++ = (uint8_t)(tp[i++] >> shft & 0xff); |
522 | 0 | occ--; |
523 | 0 | } |
524 | 0 | } |
525 | 0 | if (rc >= MINRUN) |
526 | 0 | { /* write out run */ |
527 | 0 | *op++ = (uint8_t)(128 - 2 + rc); |
528 | 0 | *op++ = (uint8_t)(tp[beg] >> shft & 0xff); |
529 | 0 | occ -= 2; |
530 | 0 | } |
531 | 0 | else |
532 | 0 | rc = 0; |
533 | 0 | } |
534 | 0 | } |
535 | 0 | tif->tif_rawcp = op; |
536 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
537 | |
|
538 | 0 | return (1); |
539 | 0 | } |
540 | | |
541 | | /* |
542 | | * Encode a row of 24-bit pixels. |
543 | | */ |
544 | | static int LogLuvEncode24(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) |
545 | 0 | { |
546 | 0 | static const char module[] = "LogLuvEncode24"; |
547 | 0 | LogLuvState *sp = EncoderState(tif); |
548 | 0 | tmsize_t i; |
549 | 0 | tmsize_t npixels; |
550 | 0 | tmsize_t occ; |
551 | 0 | uint8_t *op; |
552 | 0 | uint32_t *tp; |
553 | |
|
554 | 0 | (void)s; |
555 | 0 | assert(s == 0); |
556 | 0 | assert(sp != NULL); |
557 | 0 | npixels = cc / sp->pixel_size; |
558 | |
|
559 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_RAW) |
560 | 0 | tp = (uint32_t *)bp; |
561 | 0 | else |
562 | 0 | { |
563 | 0 | tp = (uint32_t *)sp->tbuf; |
564 | 0 | if (sp->tbuflen < npixels) |
565 | 0 | { |
566 | 0 | TIFFErrorExtR(tif, module, "Translation buffer too short"); |
567 | 0 | return (0); |
568 | 0 | } |
569 | 0 | (*sp->tfunc)(sp, bp, npixels); |
570 | 0 | } |
571 | | /* write out encoded pixels */ |
572 | 0 | op = tif->tif_rawcp; |
573 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
574 | 0 | for (i = npixels; i--;) |
575 | 0 | { |
576 | 0 | if (occ < 3) |
577 | 0 | { |
578 | 0 | tif->tif_rawcp = op; |
579 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
580 | 0 | if (!TIFFFlushData1(tif)) |
581 | 0 | return (0); |
582 | 0 | op = tif->tif_rawcp; |
583 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
584 | 0 | } |
585 | 0 | *op++ = (uint8_t)(*tp >> 16); |
586 | 0 | *op++ = (uint8_t)(*tp >> 8 & 0xff); |
587 | 0 | *op++ = (uint8_t)(*tp++ & 0xff); |
588 | 0 | occ -= 3; |
589 | 0 | } |
590 | 0 | tif->tif_rawcp = op; |
591 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
592 | |
|
593 | 0 | return (1); |
594 | 0 | } |
595 | | |
596 | | /* |
597 | | * Encode a row of 32-bit pixels. |
598 | | */ |
599 | | static int LogLuvEncode32(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) |
600 | 0 | { |
601 | 0 | static const char module[] = "LogLuvEncode32"; |
602 | 0 | LogLuvState *sp = EncoderState(tif); |
603 | 0 | int shft; |
604 | 0 | tmsize_t i; |
605 | 0 | tmsize_t j; |
606 | 0 | tmsize_t npixels; |
607 | 0 | uint8_t *op; |
608 | 0 | uint32_t *tp; |
609 | 0 | uint32_t b; |
610 | 0 | tmsize_t occ; |
611 | 0 | int rc = 0; |
612 | 0 | tmsize_t beg; |
613 | |
|
614 | 0 | (void)s; |
615 | 0 | assert(s == 0); |
616 | 0 | assert(sp != NULL); |
617 | | |
618 | 0 | npixels = cc / sp->pixel_size; |
619 | |
|
620 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_RAW) |
621 | 0 | tp = (uint32_t *)bp; |
622 | 0 | else |
623 | 0 | { |
624 | 0 | tp = (uint32_t *)sp->tbuf; |
625 | 0 | if (sp->tbuflen < npixels) |
626 | 0 | { |
627 | 0 | TIFFErrorExtR(tif, module, "Translation buffer too short"); |
628 | 0 | return (0); |
629 | 0 | } |
630 | 0 | (*sp->tfunc)(sp, bp, npixels); |
631 | 0 | } |
632 | | /* compress each byte string */ |
633 | 0 | op = tif->tif_rawcp; |
634 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
635 | 0 | for (shft = 24; shft >= 0; shft -= 8) |
636 | 0 | { |
637 | 0 | const uint32_t mask = 0xffU << shft; /* find next run */ |
638 | 0 | for (i = 0; i < npixels; i += rc) |
639 | 0 | { |
640 | 0 | if (occ < 4) |
641 | 0 | { |
642 | 0 | tif->tif_rawcp = op; |
643 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
644 | 0 | if (!TIFFFlushData1(tif)) |
645 | 0 | return (0); |
646 | 0 | op = tif->tif_rawcp; |
647 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
648 | 0 | } |
649 | 0 | for (beg = i; beg < npixels; beg += rc) |
650 | 0 | { |
651 | 0 | b = tp[beg] & mask; |
652 | 0 | rc = 1; |
653 | 0 | while (rc < 127 + 2 && beg + rc < npixels && |
654 | 0 | (tp[beg + rc] & mask) == b) |
655 | 0 | rc++; |
656 | 0 | if (rc >= MINRUN) |
657 | 0 | break; /* long enough */ |
658 | 0 | } |
659 | 0 | if (beg - i > 1 && beg - i < MINRUN) |
660 | 0 | { |
661 | 0 | b = tp[i] & mask; /* check short run */ |
662 | 0 | j = i + 1; |
663 | 0 | while ((tp[j++] & mask) == b) |
664 | 0 | if (j == beg) |
665 | 0 | { |
666 | 0 | *op++ = (uint8_t)(128 - 2 + j - i); |
667 | 0 | *op++ = (uint8_t)(b >> shft); |
668 | 0 | occ -= 2; |
669 | 0 | i = beg; |
670 | 0 | break; |
671 | 0 | } |
672 | 0 | } |
673 | 0 | while (i < beg) |
674 | 0 | { /* write out non-run */ |
675 | 0 | if ((j = beg - i) > 127) |
676 | 0 | j = 127; |
677 | 0 | if (occ < j + 3) |
678 | 0 | { |
679 | 0 | tif->tif_rawcp = op; |
680 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
681 | 0 | if (!TIFFFlushData1(tif)) |
682 | 0 | return (0); |
683 | 0 | op = tif->tif_rawcp; |
684 | 0 | occ = tif->tif_rawdatasize - tif->tif_rawcc; |
685 | 0 | } |
686 | 0 | *op++ = (uint8_t)j; |
687 | 0 | occ--; |
688 | 0 | while (j--) |
689 | 0 | { |
690 | 0 | *op++ = (uint8_t)(tp[i++] >> shft & 0xff); |
691 | 0 | occ--; |
692 | 0 | } |
693 | 0 | } |
694 | 0 | if (rc >= MINRUN) |
695 | 0 | { /* write out run */ |
696 | 0 | *op++ = (uint8_t)(128 - 2 + rc); |
697 | 0 | *op++ = (uint8_t)(tp[beg] >> shft & 0xff); |
698 | 0 | occ -= 2; |
699 | 0 | } |
700 | 0 | else |
701 | 0 | rc = 0; |
702 | 0 | } |
703 | 0 | } |
704 | 0 | tif->tif_rawcp = op; |
705 | 0 | tif->tif_rawcc = tif->tif_rawdatasize - occ; |
706 | |
|
707 | 0 | return (1); |
708 | 0 | } |
709 | | |
710 | | /* |
711 | | * Encode a strip of pixels. We break it into rows to |
712 | | * avoid encoding runs across row boundaries. |
713 | | */ |
714 | | static int LogLuvEncodeStrip(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) |
715 | 0 | { |
716 | 0 | tmsize_t rowlen = TIFFScanlineSize(tif); |
717 | |
|
718 | 0 | if (rowlen == 0) |
719 | 0 | return 0; |
720 | | |
721 | 0 | assert(cc % rowlen == 0); |
722 | 0 | while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1) |
723 | 0 | { |
724 | 0 | bp += rowlen; |
725 | 0 | cc -= rowlen; |
726 | 0 | } |
727 | 0 | return (cc == 0); |
728 | 0 | } |
729 | | |
730 | | /* |
731 | | * Encode a tile of pixels. We break it into rows to |
732 | | * avoid encoding runs across row boundaries. |
733 | | */ |
734 | | static int LogLuvEncodeTile(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) |
735 | 0 | { |
736 | 0 | tmsize_t rowlen = TIFFTileRowSize(tif); |
737 | |
|
738 | 0 | if (rowlen == 0) |
739 | 0 | return 0; |
740 | | |
741 | 0 | assert(cc % rowlen == 0); |
742 | 0 | while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1) |
743 | 0 | { |
744 | 0 | bp += rowlen; |
745 | 0 | cc -= rowlen; |
746 | 0 | } |
747 | 0 | return (cc == 0); |
748 | 0 | } |
749 | | |
750 | | /* |
751 | | * Encode/Decode functions for converting to and from user formats. |
752 | | */ |
753 | | |
754 | | #include "uvcode.h" |
755 | | |
756 | | #ifndef UVSCALE |
757 | | #define U_NEU 0.210526316 |
758 | | #define V_NEU 0.473684211 |
759 | | #define UVSCALE 410. |
760 | | #endif |
761 | | |
762 | | #ifndef M_LN2 |
763 | | #define M_LN2 0.69314718055994530942 |
764 | | #endif |
765 | | #ifndef M_PI |
766 | | #define M_PI 3.14159265358979323846 |
767 | | #endif |
768 | | #undef log2 /* Conflict with C'99 function */ |
769 | 0 | #define log2(x) ((1. / M_LN2) * log(x)) |
770 | | #undef exp2 /* Conflict with C'99 function */ |
771 | | #define exp2(x) exp(M_LN2 *(x)) |
772 | | |
773 | | static int tiff_itrunc(double x, int m) |
774 | 0 | { |
775 | 0 | if (m == SGILOGENCODE_NODITHER) |
776 | 0 | return (int)x; |
777 | | /* Silence CoverityScan warning about bad crypto function */ |
778 | | /* coverity[dont_call] */ |
779 | 0 | return (int)(x + rand() * (1. / RAND_MAX) - .5); |
780 | 0 | } |
781 | | |
782 | | #if !LOGLUV_PUBLIC |
783 | | static |
784 | | #endif |
785 | | double |
786 | | LogL16toY(int p16) /* compute luminance from 16-bit LogL */ |
787 | 0 | { |
788 | 0 | int Le = p16 & 0x7fff; |
789 | 0 | double Y; |
790 | |
|
791 | 0 | if (!Le) |
792 | 0 | return (0.); |
793 | 0 | Y = exp(M_LN2 / 256. * (Le + .5) - M_LN2 * 64.); |
794 | 0 | return (!(p16 & 0x8000) ? Y : -Y); |
795 | 0 | } |
796 | | |
797 | | #if !LOGLUV_PUBLIC |
798 | | static |
799 | | #endif |
800 | | int |
801 | | LogL16fromY(double Y, int em) /* get 16-bit LogL from Y */ |
802 | 0 | { |
803 | 0 | if (Y >= 1.8371976e19) |
804 | 0 | return (0x7fff); |
805 | 0 | if (Y <= -1.8371976e19) |
806 | 0 | return (0xffff); |
807 | 0 | if (Y > 5.4136769e-20) |
808 | 0 | return tiff_itrunc(256. * (log2(Y) + 64.), em); |
809 | 0 | if (Y < -5.4136769e-20) |
810 | 0 | return (~0x7fff | tiff_itrunc(256. * (log2(-Y) + 64.), em)); |
811 | 0 | return (0); |
812 | 0 | } |
813 | | |
814 | | static void L16toY(LogLuvState *sp, uint8_t *op, tmsize_t n) |
815 | 0 | { |
816 | 0 | int16_t *l16 = (int16_t *)sp->tbuf; |
817 | 0 | float *yp = (float *)op; |
818 | |
|
819 | 0 | while (n-- > 0) |
820 | 0 | *yp++ = (float)LogL16toY(*l16++); |
821 | 0 | } |
822 | | |
823 | | static void L16toGry(LogLuvState *sp, uint8_t *op, tmsize_t n) |
824 | 0 | { |
825 | 0 | int16_t *l16 = (int16_t *)sp->tbuf; |
826 | 0 | uint8_t *gp = (uint8_t *)op; |
827 | |
|
828 | 0 | while (n-- > 0) |
829 | 0 | { |
830 | 0 | double Y = LogL16toY(*l16++); |
831 | 0 | *gp++ = (uint8_t)((Y <= 0.) ? 0 |
832 | 0 | : (Y >= 1.) ? 255 |
833 | 0 | : (int)(256. * sqrt(Y))); |
834 | 0 | } |
835 | 0 | } |
836 | | |
837 | | static void L16fromY(LogLuvState *sp, uint8_t *op, tmsize_t n) |
838 | 0 | { |
839 | 0 | int16_t *l16 = (int16_t *)sp->tbuf; |
840 | 0 | float *yp = (float *)op; |
841 | |
|
842 | 0 | while (n-- > 0) |
843 | 0 | *l16++ = (int16_t)(LogL16fromY(*yp++, sp->encode_meth)); |
844 | 0 | } |
845 | | |
846 | | #if !LOGLUV_PUBLIC |
847 | | static |
848 | | #endif |
849 | | void |
850 | | XYZtoRGB24(float *xyz, uint8_t *rgb) |
851 | 0 | { |
852 | 0 | double r, g, b; |
853 | | /* assume CCIR-709 primaries */ |
854 | 0 | r = 2.690 * xyz[0] + -1.276 * xyz[1] + -0.414 * xyz[2]; |
855 | 0 | g = -1.022 * xyz[0] + 1.978 * xyz[1] + 0.044 * xyz[2]; |
856 | 0 | b = 0.061 * xyz[0] + -0.224 * xyz[1] + 1.163 * xyz[2]; |
857 | | /* assume 2.0 gamma for speed */ |
858 | | /* could use integer sqrt approx., but this is probably faster */ |
859 | 0 | rgb[0] = (uint8_t)((r <= 0.) ? 0 : (r >= 1.) ? 255 : (int)(256. * sqrt(r))); |
860 | 0 | rgb[1] = (uint8_t)((g <= 0.) ? 0 : (g >= 1.) ? 255 : (int)(256. * sqrt(g))); |
861 | 0 | rgb[2] = (uint8_t)((b <= 0.) ? 0 : (b >= 1.) ? 255 : (int)(256. * sqrt(b))); |
862 | 0 | } |
863 | | |
864 | | #if !LOGLUV_PUBLIC |
865 | | static |
866 | | #endif |
867 | | double |
868 | | LogL10toY(int p10) /* compute luminance from 10-bit LogL */ |
869 | 0 | { |
870 | 0 | if (p10 == 0) |
871 | 0 | return (0.); |
872 | 0 | return (exp(M_LN2 / 64. * (p10 + .5) - M_LN2 * 12.)); |
873 | 0 | } |
874 | | |
875 | | #if !LOGLUV_PUBLIC |
876 | | static |
877 | | #endif |
878 | | int |
879 | | LogL10fromY(double Y, int em) /* get 10-bit LogL from Y */ |
880 | 0 | { |
881 | 0 | if (Y >= 15.742) |
882 | 0 | return (0x3ff); |
883 | 0 | else if (Y <= .00024283) |
884 | 0 | return (0); |
885 | 0 | else |
886 | 0 | return tiff_itrunc(64. * (log2(Y) + 12.), em); |
887 | 0 | } |
888 | | |
889 | 0 | #define NANGLES 100 |
890 | | #define uv2ang(u, v) \ |
891 | 0 | ((NANGLES * .499999999 / M_PI) * atan2((v)-V_NEU, (u)-U_NEU) + .5 * NANGLES) |
892 | | |
893 | | static int oog_encode(double u, double v) /* encode out-of-gamut chroma */ |
894 | 0 | { |
895 | 0 | static int oog_table[NANGLES]; |
896 | 0 | static int initialized = 0; |
897 | 0 | register int i; |
898 | |
|
899 | 0 | if (!initialized) |
900 | 0 | { /* set up perimeter table */ |
901 | 0 | double eps[NANGLES], ua, va, ang, epsa; |
902 | 0 | int ui, vi, ustep; |
903 | 0 | for (i = NANGLES; i--;) |
904 | 0 | eps[i] = 2.; |
905 | 0 | for (vi = UV_NVS; vi--;) |
906 | 0 | { |
907 | 0 | va = UV_VSTART + (vi + .5) * UV_SQSIZ; |
908 | 0 | ustep = uv_row[vi].nus - 1; |
909 | 0 | if (vi == UV_NVS - 1 || vi == 0 || ustep <= 0) |
910 | 0 | ustep = 1; |
911 | 0 | for (ui = uv_row[vi].nus - 1; ui >= 0; ui -= ustep) |
912 | 0 | { |
913 | 0 | ua = uv_row[vi].ustart + (ui + .5) * UV_SQSIZ; |
914 | 0 | ang = uv2ang(ua, va); |
915 | 0 | i = (int)ang; |
916 | 0 | epsa = fabs(ang - (i + .5)); |
917 | 0 | if (epsa < eps[i]) |
918 | 0 | { |
919 | 0 | oog_table[i] = uv_row[vi].ncum + ui; |
920 | 0 | eps[i] = epsa; |
921 | 0 | } |
922 | 0 | } |
923 | 0 | } |
924 | 0 | for (i = NANGLES; i--;) /* fill any holes */ |
925 | 0 | if (eps[i] > 1.5) |
926 | 0 | { |
927 | 0 | int i1, i2; |
928 | 0 | for (i1 = 1; i1 < NANGLES / 2; i1++) |
929 | 0 | if (eps[(i + i1) % NANGLES] < 1.5) |
930 | 0 | break; |
931 | 0 | for (i2 = 1; i2 < NANGLES / 2; i2++) |
932 | 0 | if (eps[(i + NANGLES - i2) % NANGLES] < 1.5) |
933 | 0 | break; |
934 | 0 | if (i1 < i2) |
935 | 0 | oog_table[i] = oog_table[(i + i1) % NANGLES]; |
936 | 0 | else |
937 | 0 | oog_table[i] = oog_table[(i + NANGLES - i2) % NANGLES]; |
938 | 0 | } |
939 | 0 | initialized = 1; |
940 | 0 | } |
941 | 0 | i = (int)uv2ang(u, v); /* look up hue angle */ |
942 | 0 | return (oog_table[i]); |
943 | 0 | } |
944 | | |
945 | | #undef uv2ang |
946 | | #undef NANGLES |
947 | | |
948 | | #if !LOGLUV_PUBLIC |
949 | | static |
950 | | #endif |
951 | | int |
952 | | uv_encode(double u, double v, int em) /* encode (u',v') coordinates */ |
953 | 0 | { |
954 | 0 | register int vi, ui; |
955 | | |
956 | | /* check for NaN */ |
957 | 0 | if (u != u || v != v) |
958 | 0 | { |
959 | 0 | u = U_NEU; |
960 | 0 | v = V_NEU; |
961 | 0 | } |
962 | |
|
963 | 0 | if (v < UV_VSTART) |
964 | 0 | return oog_encode(u, v); |
965 | 0 | vi = tiff_itrunc((v - UV_VSTART) * (1. / UV_SQSIZ), em); |
966 | 0 | if (vi >= UV_NVS) |
967 | 0 | return oog_encode(u, v); |
968 | 0 | if (u < uv_row[vi].ustart) |
969 | 0 | return oog_encode(u, v); |
970 | 0 | ui = tiff_itrunc((u - uv_row[vi].ustart) * (1. / UV_SQSIZ), em); |
971 | 0 | if (ui >= uv_row[vi].nus) |
972 | 0 | return oog_encode(u, v); |
973 | | |
974 | 0 | return (uv_row[vi].ncum + ui); |
975 | 0 | } |
976 | | |
977 | | #if !LOGLUV_PUBLIC |
978 | | static |
979 | | #endif |
980 | | int |
981 | | uv_decode(double *up, double *vp, int c) /* decode (u',v') index */ |
982 | 0 | { |
983 | 0 | int upper, lower; |
984 | 0 | register int ui, vi; |
985 | |
|
986 | 0 | if (c < 0 || c >= UV_NDIVS) |
987 | 0 | return (-1); |
988 | 0 | lower = 0; /* binary search */ |
989 | 0 | upper = UV_NVS; |
990 | 0 | while (upper - lower > 1) |
991 | 0 | { |
992 | 0 | vi = (lower + upper) >> 1; |
993 | 0 | ui = c - uv_row[vi].ncum; |
994 | 0 | if (ui > 0) |
995 | 0 | lower = vi; |
996 | 0 | else if (ui < 0) |
997 | 0 | upper = vi; |
998 | 0 | else |
999 | 0 | { |
1000 | 0 | lower = vi; |
1001 | 0 | break; |
1002 | 0 | } |
1003 | 0 | } |
1004 | 0 | vi = lower; |
1005 | 0 | ui = c - uv_row[vi].ncum; |
1006 | 0 | *up = uv_row[vi].ustart + (ui + .5) * UV_SQSIZ; |
1007 | 0 | *vp = UV_VSTART + (vi + .5) * UV_SQSIZ; |
1008 | 0 | return (0); |
1009 | 0 | } |
1010 | | |
1011 | | #if !LOGLUV_PUBLIC |
1012 | | static |
1013 | | #endif |
1014 | | void |
1015 | | LogLuv24toXYZ(uint32_t p, float *XYZ) |
1016 | 0 | { |
1017 | 0 | int Ce; |
1018 | 0 | double L, u, v, s, x, y; |
1019 | | /* decode luminance */ |
1020 | 0 | L = LogL10toY(p >> 14 & 0x3ff); |
1021 | 0 | if (L <= 0.) |
1022 | 0 | { |
1023 | 0 | XYZ[0] = XYZ[1] = XYZ[2] = 0.; |
1024 | 0 | return; |
1025 | 0 | } |
1026 | | /* decode color */ |
1027 | 0 | Ce = p & 0x3fff; |
1028 | 0 | if (uv_decode(&u, &v, Ce) < 0) |
1029 | 0 | { |
1030 | 0 | u = U_NEU; |
1031 | 0 | v = V_NEU; |
1032 | 0 | } |
1033 | 0 | s = 1. / (6. * u - 16. * v + 12.); |
1034 | 0 | x = 9. * u * s; |
1035 | 0 | y = 4. * v * s; |
1036 | | /* convert to XYZ */ |
1037 | 0 | XYZ[0] = (float)(x / y * L); |
1038 | 0 | XYZ[1] = (float)L; |
1039 | 0 | XYZ[2] = (float)((1. - x - y) / y * L); |
1040 | 0 | } |
1041 | | |
1042 | | #if !LOGLUV_PUBLIC |
1043 | | static |
1044 | | #endif |
1045 | | uint32_t |
1046 | | LogLuv24fromXYZ(float *XYZ, int em) |
1047 | 0 | { |
1048 | 0 | int Le, Ce; |
1049 | 0 | double u, v, s; |
1050 | | /* encode luminance */ |
1051 | 0 | Le = LogL10fromY(XYZ[1], em); |
1052 | | /* encode color */ |
1053 | 0 | s = XYZ[0] + 15. * XYZ[1] + 3. * XYZ[2]; |
1054 | 0 | if (!Le || s <= 0.) |
1055 | 0 | { |
1056 | 0 | u = U_NEU; |
1057 | 0 | v = V_NEU; |
1058 | 0 | } |
1059 | 0 | else |
1060 | 0 | { |
1061 | 0 | u = 4. * XYZ[0] / s; |
1062 | 0 | v = 9. * XYZ[1] / s; |
1063 | 0 | } |
1064 | 0 | Ce = uv_encode(u, v, em); |
1065 | 0 | if (Ce < 0) /* never happens */ |
1066 | 0 | Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER); |
1067 | | /* combine encodings */ |
1068 | 0 | return (Le << 14 | Ce); |
1069 | 0 | } |
1070 | | |
1071 | | static void Luv24toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n) |
1072 | 0 | { |
1073 | 0 | uint32_t *luv = (uint32_t *)sp->tbuf; |
1074 | 0 | float *xyz = (float *)op; |
1075 | |
|
1076 | 0 | while (n-- > 0) |
1077 | 0 | { |
1078 | 0 | LogLuv24toXYZ(*luv, xyz); |
1079 | 0 | xyz += 3; |
1080 | 0 | luv++; |
1081 | 0 | } |
1082 | 0 | } |
1083 | | |
1084 | | static void Luv24toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n) |
1085 | 0 | { |
1086 | 0 | uint32_t *luv = (uint32_t *)sp->tbuf; |
1087 | 0 | int16_t *luv3 = (int16_t *)op; |
1088 | |
|
1089 | 0 | while (n-- > 0) |
1090 | 0 | { |
1091 | 0 | double u, v; |
1092 | |
|
1093 | 0 | *luv3++ = (int16_t)((*luv >> 12 & 0xffd) + 13314); |
1094 | 0 | if (uv_decode(&u, &v, *luv & 0x3fff) < 0) |
1095 | 0 | { |
1096 | 0 | u = U_NEU; |
1097 | 0 | v = V_NEU; |
1098 | 0 | } |
1099 | 0 | *luv3++ = (int16_t)(u * (1L << 15)); |
1100 | 0 | *luv3++ = (int16_t)(v * (1L << 15)); |
1101 | 0 | luv++; |
1102 | 0 | } |
1103 | 0 | } |
1104 | | |
1105 | | static void Luv24toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n) |
1106 | 0 | { |
1107 | 0 | uint32_t *luv = (uint32_t *)sp->tbuf; |
1108 | 0 | uint8_t *rgb = (uint8_t *)op; |
1109 | |
|
1110 | 0 | while (n-- > 0) |
1111 | 0 | { |
1112 | 0 | float xyz[3]; |
1113 | |
|
1114 | 0 | LogLuv24toXYZ(*luv++, xyz); |
1115 | 0 | XYZtoRGB24(xyz, rgb); |
1116 | 0 | rgb += 3; |
1117 | 0 | } |
1118 | 0 | } |
1119 | | |
1120 | | static void Luv24fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n) |
1121 | 0 | { |
1122 | 0 | uint32_t *luv = (uint32_t *)sp->tbuf; |
1123 | 0 | float *xyz = (float *)op; |
1124 | |
|
1125 | 0 | while (n-- > 0) |
1126 | 0 | { |
1127 | 0 | *luv++ = LogLuv24fromXYZ(xyz, sp->encode_meth); |
1128 | 0 | xyz += 3; |
1129 | 0 | } |
1130 | 0 | } |
1131 | | |
1132 | | static void Luv24fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n) |
1133 | 0 | { |
1134 | 0 | uint32_t *luv = (uint32_t *)sp->tbuf; |
1135 | 0 | int16_t *luv3 = (int16_t *)op; |
1136 | |
|
1137 | 0 | while (n-- > 0) |
1138 | 0 | { |
1139 | 0 | int Le, Ce; |
1140 | |
|
1141 | 0 | if (luv3[0] <= 0) |
1142 | 0 | Le = 0; |
1143 | 0 | else if (luv3[0] >= (1 << 12) + 3314) |
1144 | 0 | Le = (1 << 10) - 1; |
1145 | 0 | else if (sp->encode_meth == SGILOGENCODE_NODITHER) |
1146 | 0 | Le = (luv3[0] - 3314) >> 2; |
1147 | 0 | else |
1148 | 0 | Le = tiff_itrunc(.25 * (luv3[0] - 3314.), sp->encode_meth); |
1149 | |
|
1150 | 0 | Ce = uv_encode((luv3[1] + .5) / (1 << 15), (luv3[2] + .5) / (1 << 15), |
1151 | 0 | sp->encode_meth); |
1152 | 0 | if (Ce < 0) /* never happens */ |
1153 | 0 | Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER); |
1154 | 0 | *luv++ = (uint32_t)Le << 14 | Ce; |
1155 | 0 | luv3 += 3; |
1156 | 0 | } |
1157 | 0 | } |
1158 | | |
1159 | | #if !LOGLUV_PUBLIC |
1160 | | static |
1161 | | #endif |
1162 | | void |
1163 | | LogLuv32toXYZ(uint32_t p, float *XYZ) |
1164 | 0 | { |
1165 | 0 | double L, u, v, s, x, y; |
1166 | | /* decode luminance */ |
1167 | 0 | L = LogL16toY((int)p >> 16); |
1168 | 0 | if (L <= 0.) |
1169 | 0 | { |
1170 | 0 | XYZ[0] = XYZ[1] = XYZ[2] = 0.; |
1171 | 0 | return; |
1172 | 0 | } |
1173 | | /* decode color */ |
1174 | 0 | u = 1. / UVSCALE * ((p >> 8 & 0xff) + .5); |
1175 | 0 | v = 1. / UVSCALE * ((p & 0xff) + .5); |
1176 | 0 | s = 1. / (6. * u - 16. * v + 12.); |
1177 | 0 | x = 9. * u * s; |
1178 | 0 | y = 4. * v * s; |
1179 | | /* convert to XYZ */ |
1180 | 0 | XYZ[0] = (float)(x / y * L); |
1181 | 0 | XYZ[1] = (float)L; |
1182 | 0 | XYZ[2] = (float)((1. - x - y) / y * L); |
1183 | 0 | } |
1184 | | |
1185 | | #if !LOGLUV_PUBLIC |
1186 | | static |
1187 | | #endif |
1188 | | uint32_t |
1189 | | LogLuv32fromXYZ(float *XYZ, int em) |
1190 | 0 | { |
1191 | 0 | unsigned int Le, ue, ve; |
1192 | 0 | double u, v, s; |
1193 | | /* encode luminance */ |
1194 | 0 | Le = (unsigned int)LogL16fromY(XYZ[1], em); |
1195 | | /* encode color */ |
1196 | 0 | s = XYZ[0] + 15. * XYZ[1] + 3. * XYZ[2]; |
1197 | 0 | if (!Le || s <= 0.) |
1198 | 0 | { |
1199 | 0 | u = U_NEU; |
1200 | 0 | v = V_NEU; |
1201 | 0 | } |
1202 | 0 | else |
1203 | 0 | { |
1204 | 0 | u = 4. * XYZ[0] / s; |
1205 | 0 | v = 9. * XYZ[1] / s; |
1206 | 0 | } |
1207 | 0 | if (u <= 0.) |
1208 | 0 | ue = 0; |
1209 | 0 | else |
1210 | 0 | ue = tiff_itrunc(UVSCALE * u, em); |
1211 | 0 | if (ue > 255) |
1212 | 0 | ue = 255; |
1213 | 0 | if (v <= 0.) |
1214 | 0 | ve = 0; |
1215 | 0 | else |
1216 | 0 | ve = tiff_itrunc(UVSCALE * v, em); |
1217 | 0 | if (ve > 255) |
1218 | 0 | ve = 255; |
1219 | | /* combine encodings */ |
1220 | 0 | return (Le << 16 | ue << 8 | ve); |
1221 | 0 | } |
1222 | | |
1223 | | static void Luv32toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n) |
1224 | 0 | { |
1225 | 0 | uint32_t *luv = (uint32_t *)sp->tbuf; |
1226 | 0 | float *xyz = (float *)op; |
1227 | |
|
1228 | 0 | while (n-- > 0) |
1229 | 0 | { |
1230 | 0 | LogLuv32toXYZ(*luv++, xyz); |
1231 | 0 | xyz += 3; |
1232 | 0 | } |
1233 | 0 | } |
1234 | | |
1235 | | static void Luv32toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n) |
1236 | 0 | { |
1237 | 0 | uint32_t *luv = (uint32_t *)sp->tbuf; |
1238 | 0 | int16_t *luv3 = (int16_t *)op; |
1239 | |
|
1240 | 0 | while (n-- > 0) |
1241 | 0 | { |
1242 | 0 | double u, v; |
1243 | |
|
1244 | 0 | *luv3++ = (int16_t)(*luv >> 16); |
1245 | 0 | u = 1. / UVSCALE * ((*luv >> 8 & 0xff) + .5); |
1246 | 0 | v = 1. / UVSCALE * ((*luv & 0xff) + .5); |
1247 | 0 | *luv3++ = (int16_t)(u * (1L << 15)); |
1248 | 0 | *luv3++ = (int16_t)(v * (1L << 15)); |
1249 | 0 | luv++; |
1250 | 0 | } |
1251 | 0 | } |
1252 | | |
1253 | | static void Luv32toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n) |
1254 | 0 | { |
1255 | 0 | uint32_t *luv = (uint32_t *)sp->tbuf; |
1256 | 0 | uint8_t *rgb = (uint8_t *)op; |
1257 | |
|
1258 | 0 | while (n-- > 0) |
1259 | 0 | { |
1260 | 0 | float xyz[3]; |
1261 | |
|
1262 | 0 | LogLuv32toXYZ(*luv++, xyz); |
1263 | 0 | XYZtoRGB24(xyz, rgb); |
1264 | 0 | rgb += 3; |
1265 | 0 | } |
1266 | 0 | } |
1267 | | |
1268 | | static void Luv32fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n) |
1269 | 0 | { |
1270 | 0 | uint32_t *luv = (uint32_t *)sp->tbuf; |
1271 | 0 | float *xyz = (float *)op; |
1272 | |
|
1273 | 0 | while (n-- > 0) |
1274 | 0 | { |
1275 | 0 | *luv++ = LogLuv32fromXYZ(xyz, sp->encode_meth); |
1276 | 0 | xyz += 3; |
1277 | 0 | } |
1278 | 0 | } |
1279 | | |
1280 | | static void Luv32fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n) |
1281 | 0 | { |
1282 | 0 | uint32_t *luv = (uint32_t *)sp->tbuf; |
1283 | 0 | int16_t *luv3 = (int16_t *)op; |
1284 | |
|
1285 | 0 | if (sp->encode_meth == SGILOGENCODE_NODITHER) |
1286 | 0 | { |
1287 | 0 | while (n-- > 0) |
1288 | 0 | { |
1289 | 0 | *luv++ = (uint32_t)luv3[0] << 16 | |
1290 | 0 | (luv3[1] * (uint32_t)(UVSCALE + .5) >> 7 & 0xff00) | |
1291 | 0 | (luv3[2] * (uint32_t)(UVSCALE + .5) >> 15 & 0xff); |
1292 | 0 | luv3 += 3; |
1293 | 0 | } |
1294 | 0 | return; |
1295 | 0 | } |
1296 | 0 | while (n-- > 0) |
1297 | 0 | { |
1298 | 0 | *luv++ = |
1299 | 0 | (uint32_t)luv3[0] << 16 | |
1300 | 0 | (tiff_itrunc(luv3[1] * (UVSCALE / (1 << 15)), sp->encode_meth) |
1301 | 0 | << 8 & |
1302 | 0 | 0xff00) | |
1303 | 0 | (tiff_itrunc(luv3[2] * (UVSCALE / (1 << 15)), sp->encode_meth) & |
1304 | 0 | 0xff); |
1305 | 0 | luv3 += 3; |
1306 | 0 | } |
1307 | 0 | } |
1308 | | |
1309 | | static void _logLuvNop(LogLuvState *sp, uint8_t *op, tmsize_t n) |
1310 | 0 | { |
1311 | 0 | (void)sp; |
1312 | 0 | (void)op; |
1313 | 0 | (void)n; |
1314 | 0 | } |
1315 | | |
1316 | | static int LogL16GuessDataFmt(TIFFDirectory *td) |
1317 | 0 | { |
1318 | 0 | #define PACK(s, b, f) (((b) << 6) | ((s) << 3) | (f)) |
1319 | 0 | switch ( |
1320 | 0 | PACK(td->td_samplesperpixel, td->td_bitspersample, td->td_sampleformat)) |
1321 | 0 | { |
1322 | 0 | case PACK(1, 32, SAMPLEFORMAT_IEEEFP): |
1323 | 0 | return (SGILOGDATAFMT_FLOAT); |
1324 | 0 | case PACK(1, 16, SAMPLEFORMAT_VOID): |
1325 | 0 | case PACK(1, 16, SAMPLEFORMAT_INT): |
1326 | 0 | case PACK(1, 16, SAMPLEFORMAT_UINT): |
1327 | 0 | return (SGILOGDATAFMT_16BIT); |
1328 | 0 | case PACK(1, 8, SAMPLEFORMAT_VOID): |
1329 | 0 | case PACK(1, 8, SAMPLEFORMAT_UINT): |
1330 | 0 | return (SGILOGDATAFMT_8BIT); |
1331 | 0 | } |
1332 | 0 | #undef PACK |
1333 | 0 | return (SGILOGDATAFMT_UNKNOWN); |
1334 | 0 | } |
1335 | | |
1336 | | static tmsize_t multiply_ms(tmsize_t m1, tmsize_t m2) |
1337 | 0 | { |
1338 | 0 | return _TIFFMultiplySSize(NULL, m1, m2, NULL); |
1339 | 0 | } |
1340 | | |
1341 | | static int LogL16InitState(TIFF *tif) |
1342 | 0 | { |
1343 | 0 | static const char module[] = "LogL16InitState"; |
1344 | 0 | TIFFDirectory *td = &tif->tif_dir; |
1345 | 0 | LogLuvState *sp = DecoderState(tif); |
1346 | |
|
1347 | 0 | assert(sp != NULL); |
1348 | 0 | assert(td->td_photometric == PHOTOMETRIC_LOGL); |
1349 | | |
1350 | 0 | if (td->td_samplesperpixel != 1) |
1351 | 0 | { |
1352 | 0 | TIFFErrorExtR(tif, module, |
1353 | 0 | "Sorry, can not handle LogL image with %s=%" PRIu16, |
1354 | 0 | "Samples/pixel", td->td_samplesperpixel); |
1355 | 0 | return 0; |
1356 | 0 | } |
1357 | | |
1358 | | /* for some reason, we can't do this in TIFFInitLogL16 */ |
1359 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN) |
1360 | 0 | sp->user_datafmt = LogL16GuessDataFmt(td); |
1361 | 0 | switch (sp->user_datafmt) |
1362 | 0 | { |
1363 | 0 | case SGILOGDATAFMT_FLOAT: |
1364 | 0 | sp->pixel_size = sizeof(float); |
1365 | 0 | break; |
1366 | 0 | case SGILOGDATAFMT_16BIT: |
1367 | 0 | sp->pixel_size = sizeof(int16_t); |
1368 | 0 | break; |
1369 | 0 | case SGILOGDATAFMT_8BIT: |
1370 | 0 | sp->pixel_size = sizeof(uint8_t); |
1371 | 0 | break; |
1372 | 0 | default: |
1373 | 0 | TIFFErrorExtR(tif, module, |
1374 | 0 | "No support for converting user data format to LogL"); |
1375 | 0 | return (0); |
1376 | 0 | } |
1377 | 0 | if (isTiled(tif)) |
1378 | 0 | sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength); |
1379 | 0 | else if (td->td_rowsperstrip < td->td_imagelength) |
1380 | 0 | sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip); |
1381 | 0 | else |
1382 | 0 | sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength); |
1383 | 0 | if (multiply_ms(sp->tbuflen, sizeof(int16_t)) == 0 || |
1384 | 0 | (sp->tbuf = (uint8_t *)_TIFFmallocExt( |
1385 | 0 | tif, sp->tbuflen * sizeof(int16_t))) == NULL) |
1386 | 0 | { |
1387 | 0 | TIFFErrorExtR(tif, module, "No space for SGILog translation buffer"); |
1388 | 0 | return (0); |
1389 | 0 | } |
1390 | 0 | return (1); |
1391 | 0 | } |
1392 | | |
1393 | | static int LogLuvGuessDataFmt(TIFFDirectory *td) |
1394 | 0 | { |
1395 | 0 | int guess; |
1396 | | |
1397 | | /* |
1398 | | * If the user didn't tell us their datafmt, |
1399 | | * take our best guess from the bitspersample. |
1400 | | */ |
1401 | 0 | #define PACK(a, b) (((a) << 3) | (b)) |
1402 | 0 | switch (PACK(td->td_bitspersample, td->td_sampleformat)) |
1403 | 0 | { |
1404 | 0 | case PACK(32, SAMPLEFORMAT_IEEEFP): |
1405 | 0 | guess = SGILOGDATAFMT_FLOAT; |
1406 | 0 | break; |
1407 | 0 | case PACK(32, SAMPLEFORMAT_VOID): |
1408 | 0 | case PACK(32, SAMPLEFORMAT_UINT): |
1409 | 0 | case PACK(32, SAMPLEFORMAT_INT): |
1410 | 0 | guess = SGILOGDATAFMT_RAW; |
1411 | 0 | break; |
1412 | 0 | case PACK(16, SAMPLEFORMAT_VOID): |
1413 | 0 | case PACK(16, SAMPLEFORMAT_INT): |
1414 | 0 | case PACK(16, SAMPLEFORMAT_UINT): |
1415 | 0 | guess = SGILOGDATAFMT_16BIT; |
1416 | 0 | break; |
1417 | 0 | case PACK(8, SAMPLEFORMAT_VOID): |
1418 | 0 | case PACK(8, SAMPLEFORMAT_UINT): |
1419 | 0 | guess = SGILOGDATAFMT_8BIT; |
1420 | 0 | break; |
1421 | 0 | default: |
1422 | 0 | guess = SGILOGDATAFMT_UNKNOWN; |
1423 | 0 | break; |
1424 | 0 | #undef PACK |
1425 | 0 | } |
1426 | | /* |
1427 | | * Double-check samples per pixel. |
1428 | | */ |
1429 | 0 | switch (td->td_samplesperpixel) |
1430 | 0 | { |
1431 | 0 | case 1: |
1432 | 0 | if (guess != SGILOGDATAFMT_RAW) |
1433 | 0 | guess = SGILOGDATAFMT_UNKNOWN; |
1434 | 0 | break; |
1435 | 0 | case 3: |
1436 | 0 | if (guess == SGILOGDATAFMT_RAW) |
1437 | 0 | guess = SGILOGDATAFMT_UNKNOWN; |
1438 | 0 | break; |
1439 | 0 | default: |
1440 | 0 | guess = SGILOGDATAFMT_UNKNOWN; |
1441 | 0 | break; |
1442 | 0 | } |
1443 | 0 | return (guess); |
1444 | 0 | } |
1445 | | |
1446 | | static int LogLuvInitState(TIFF *tif) |
1447 | 0 | { |
1448 | 0 | static const char module[] = "LogLuvInitState"; |
1449 | 0 | TIFFDirectory *td = &tif->tif_dir; |
1450 | 0 | LogLuvState *sp = DecoderState(tif); |
1451 | |
|
1452 | 0 | assert(sp != NULL); |
1453 | 0 | assert(td->td_photometric == PHOTOMETRIC_LOGLUV); |
1454 | | |
1455 | | /* for some reason, we can't do this in TIFFInitLogLuv */ |
1456 | 0 | if (td->td_planarconfig != PLANARCONFIG_CONTIG) |
1457 | 0 | { |
1458 | 0 | TIFFErrorExtR(tif, module, |
1459 | 0 | "SGILog compression cannot handle non-contiguous data"); |
1460 | 0 | return (0); |
1461 | 0 | } |
1462 | 0 | if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN) |
1463 | 0 | sp->user_datafmt = LogLuvGuessDataFmt(td); |
1464 | 0 | switch (sp->user_datafmt) |
1465 | 0 | { |
1466 | 0 | case SGILOGDATAFMT_FLOAT: |
1467 | 0 | sp->pixel_size = 3 * sizeof(float); |
1468 | 0 | break; |
1469 | 0 | case SGILOGDATAFMT_16BIT: |
1470 | 0 | sp->pixel_size = 3 * sizeof(int16_t); |
1471 | 0 | break; |
1472 | 0 | case SGILOGDATAFMT_RAW: |
1473 | 0 | sp->pixel_size = sizeof(uint32_t); |
1474 | 0 | break; |
1475 | 0 | case SGILOGDATAFMT_8BIT: |
1476 | 0 | sp->pixel_size = 3 * sizeof(uint8_t); |
1477 | 0 | break; |
1478 | 0 | default: |
1479 | 0 | TIFFErrorExtR( |
1480 | 0 | tif, module, |
1481 | 0 | "No support for converting user data format to LogLuv"); |
1482 | 0 | return (0); |
1483 | 0 | } |
1484 | 0 | if (isTiled(tif)) |
1485 | 0 | sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength); |
1486 | 0 | else if (td->td_rowsperstrip < td->td_imagelength) |
1487 | 0 | sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip); |
1488 | 0 | else |
1489 | 0 | sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength); |
1490 | 0 | if (multiply_ms(sp->tbuflen, sizeof(uint32_t)) == 0 || |
1491 | 0 | (sp->tbuf = (uint8_t *)_TIFFmallocExt( |
1492 | 0 | tif, sp->tbuflen * sizeof(uint32_t))) == NULL) |
1493 | 0 | { |
1494 | 0 | TIFFErrorExtR(tif, module, "No space for SGILog translation buffer"); |
1495 | 0 | return (0); |
1496 | 0 | } |
1497 | 0 | return (1); |
1498 | 0 | } |
1499 | | |
1500 | | static int LogLuvFixupTags(TIFF *tif) |
1501 | 0 | { |
1502 | 0 | (void)tif; |
1503 | 0 | return (1); |
1504 | 0 | } |
1505 | | |
1506 | | static int LogLuvSetupDecode(TIFF *tif) |
1507 | 0 | { |
1508 | 0 | static const char module[] = "LogLuvSetupDecode"; |
1509 | 0 | LogLuvState *sp = DecoderState(tif); |
1510 | 0 | TIFFDirectory *td = &tif->tif_dir; |
1511 | |
|
1512 | 0 | tif->tif_postdecode = _TIFFNoPostDecode; |
1513 | 0 | switch (td->td_photometric) |
1514 | 0 | { |
1515 | 0 | case PHOTOMETRIC_LOGLUV: |
1516 | 0 | if (!LogLuvInitState(tif)) |
1517 | 0 | break; |
1518 | 0 | if (td->td_compression == COMPRESSION_SGILOG24) |
1519 | 0 | { |
1520 | 0 | tif->tif_decoderow = LogLuvDecode24; |
1521 | 0 | switch (sp->user_datafmt) |
1522 | 0 | { |
1523 | 0 | case SGILOGDATAFMT_FLOAT: |
1524 | 0 | sp->tfunc = Luv24toXYZ; |
1525 | 0 | break; |
1526 | 0 | case SGILOGDATAFMT_16BIT: |
1527 | 0 | sp->tfunc = Luv24toLuv48; |
1528 | 0 | break; |
1529 | 0 | case SGILOGDATAFMT_8BIT: |
1530 | 0 | sp->tfunc = Luv24toRGB; |
1531 | 0 | break; |
1532 | 0 | } |
1533 | 0 | } |
1534 | 0 | else |
1535 | 0 | { |
1536 | 0 | tif->tif_decoderow = LogLuvDecode32; |
1537 | 0 | switch (sp->user_datafmt) |
1538 | 0 | { |
1539 | 0 | case SGILOGDATAFMT_FLOAT: |
1540 | 0 | sp->tfunc = Luv32toXYZ; |
1541 | 0 | break; |
1542 | 0 | case SGILOGDATAFMT_16BIT: |
1543 | 0 | sp->tfunc = Luv32toLuv48; |
1544 | 0 | break; |
1545 | 0 | case SGILOGDATAFMT_8BIT: |
1546 | 0 | sp->tfunc = Luv32toRGB; |
1547 | 0 | break; |
1548 | 0 | } |
1549 | 0 | } |
1550 | 0 | return (1); |
1551 | 0 | case PHOTOMETRIC_LOGL: |
1552 | 0 | if (!LogL16InitState(tif)) |
1553 | 0 | break; |
1554 | 0 | tif->tif_decoderow = LogL16Decode; |
1555 | 0 | switch (sp->user_datafmt) |
1556 | 0 | { |
1557 | 0 | case SGILOGDATAFMT_FLOAT: |
1558 | 0 | sp->tfunc = L16toY; |
1559 | 0 | break; |
1560 | 0 | case SGILOGDATAFMT_8BIT: |
1561 | 0 | sp->tfunc = L16toGry; |
1562 | 0 | break; |
1563 | 0 | } |
1564 | 0 | return (1); |
1565 | 0 | default: |
1566 | 0 | TIFFErrorExtR(tif, module, |
1567 | 0 | "Inappropriate photometric interpretation %" PRIu16 |
1568 | 0 | " for SGILog compression; %s", |
1569 | 0 | td->td_photometric, "must be either LogLUV or LogL"); |
1570 | 0 | break; |
1571 | 0 | } |
1572 | 0 | return (0); |
1573 | 0 | } |
1574 | | |
1575 | | static int LogLuvSetupEncode(TIFF *tif) |
1576 | 0 | { |
1577 | 0 | static const char module[] = "LogLuvSetupEncode"; |
1578 | 0 | LogLuvState *sp = EncoderState(tif); |
1579 | 0 | TIFFDirectory *td = &tif->tif_dir; |
1580 | |
|
1581 | 0 | switch (td->td_photometric) |
1582 | 0 | { |
1583 | 0 | case PHOTOMETRIC_LOGLUV: |
1584 | 0 | if (!LogLuvInitState(tif)) |
1585 | 0 | return (0); |
1586 | 0 | if (td->td_compression == COMPRESSION_SGILOG24) |
1587 | 0 | { |
1588 | 0 | tif->tif_encoderow = LogLuvEncode24; |
1589 | 0 | switch (sp->user_datafmt) |
1590 | 0 | { |
1591 | 0 | case SGILOGDATAFMT_FLOAT: |
1592 | 0 | sp->tfunc = Luv24fromXYZ; |
1593 | 0 | break; |
1594 | 0 | case SGILOGDATAFMT_16BIT: |
1595 | 0 | sp->tfunc = Luv24fromLuv48; |
1596 | 0 | break; |
1597 | 0 | case SGILOGDATAFMT_RAW: |
1598 | 0 | break; |
1599 | 0 | default: |
1600 | 0 | goto notsupported; |
1601 | 0 | } |
1602 | 0 | } |
1603 | 0 | else |
1604 | 0 | { |
1605 | 0 | tif->tif_encoderow = LogLuvEncode32; |
1606 | 0 | switch (sp->user_datafmt) |
1607 | 0 | { |
1608 | 0 | case SGILOGDATAFMT_FLOAT: |
1609 | 0 | sp->tfunc = Luv32fromXYZ; |
1610 | 0 | break; |
1611 | 0 | case SGILOGDATAFMT_16BIT: |
1612 | 0 | sp->tfunc = Luv32fromLuv48; |
1613 | 0 | break; |
1614 | 0 | case SGILOGDATAFMT_RAW: |
1615 | 0 | break; |
1616 | 0 | default: |
1617 | 0 | goto notsupported; |
1618 | 0 | } |
1619 | 0 | } |
1620 | 0 | break; |
1621 | 0 | case PHOTOMETRIC_LOGL: |
1622 | 0 | if (!LogL16InitState(tif)) |
1623 | 0 | return (0); |
1624 | 0 | tif->tif_encoderow = LogL16Encode; |
1625 | 0 | switch (sp->user_datafmt) |
1626 | 0 | { |
1627 | 0 | case SGILOGDATAFMT_FLOAT: |
1628 | 0 | sp->tfunc = L16fromY; |
1629 | 0 | break; |
1630 | 0 | case SGILOGDATAFMT_16BIT: |
1631 | 0 | break; |
1632 | 0 | default: |
1633 | 0 | goto notsupported; |
1634 | 0 | } |
1635 | 0 | break; |
1636 | 0 | default: |
1637 | 0 | TIFFErrorExtR(tif, module, |
1638 | 0 | "Inappropriate photometric interpretation %" PRIu16 |
1639 | 0 | " for SGILog compression; %s", |
1640 | 0 | td->td_photometric, "must be either LogLUV or LogL"); |
1641 | 0 | return (0); |
1642 | 0 | } |
1643 | 0 | sp->encoder_state = 1; |
1644 | 0 | return (1); |
1645 | 0 | notsupported: |
1646 | 0 | TIFFErrorExtR(tif, module, |
1647 | 0 | "SGILog compression supported only for %s, or raw data", |
1648 | 0 | td->td_photometric == PHOTOMETRIC_LOGL ? "Y, L" : "XYZ, Luv"); |
1649 | 0 | return (0); |
1650 | 0 | } |
1651 | | |
1652 | | static void LogLuvClose(TIFF *tif) |
1653 | 0 | { |
1654 | 0 | LogLuvState *sp = (LogLuvState *)tif->tif_data; |
1655 | 0 | TIFFDirectory *td = &tif->tif_dir; |
1656 | |
|
1657 | 0 | assert(sp != 0); |
1658 | | /* |
1659 | | * For consistency, we always want to write out the same |
1660 | | * bitspersample and sampleformat for our TIFF file, |
1661 | | * regardless of the data format being used by the application. |
1662 | | * Since this routine is called after tags have been set but |
1663 | | * before they have been recorded in the file, we reset them here. |
1664 | | * Note: this is really a nasty approach. See PixarLogClose |
1665 | | */ |
1666 | 0 | if (sp->encoder_state) |
1667 | 0 | { |
1668 | | /* See PixarLogClose. Might avoid issues with tags whose size depends |
1669 | | * on those below, but not completely sure this is enough. */ |
1670 | 0 | td->td_samplesperpixel = |
1671 | 0 | (td->td_photometric == PHOTOMETRIC_LOGL) ? 1 : 3; |
1672 | 0 | td->td_bitspersample = 16; |
1673 | 0 | td->td_sampleformat = SAMPLEFORMAT_INT; |
1674 | 0 | } |
1675 | 0 | } |
1676 | | |
1677 | | static void LogLuvCleanup(TIFF *tif) |
1678 | 0 | { |
1679 | 0 | LogLuvState *sp = (LogLuvState *)tif->tif_data; |
1680 | |
|
1681 | 0 | assert(sp != 0); |
1682 | | |
1683 | 0 | tif->tif_tagmethods.vgetfield = sp->vgetparent; |
1684 | 0 | tif->tif_tagmethods.vsetfield = sp->vsetparent; |
1685 | |
|
1686 | 0 | if (sp->tbuf) |
1687 | 0 | _TIFFfreeExt(tif, sp->tbuf); |
1688 | 0 | _TIFFfreeExt(tif, sp); |
1689 | 0 | tif->tif_data = NULL; |
1690 | |
|
1691 | 0 | _TIFFSetDefaultCompressionState(tif); |
1692 | 0 | } |
1693 | | |
1694 | | static int LogLuvVSetField(TIFF *tif, uint32_t tag, va_list ap) |
1695 | 0 | { |
1696 | 0 | static const char module[] = "LogLuvVSetField"; |
1697 | 0 | LogLuvState *sp = DecoderState(tif); |
1698 | 0 | int bps, fmt; |
1699 | |
|
1700 | 0 | switch (tag) |
1701 | 0 | { |
1702 | 0 | case TIFFTAG_SGILOGDATAFMT: |
1703 | 0 | sp->user_datafmt = (int)va_arg(ap, int); |
1704 | | /* |
1705 | | * Tweak the TIFF header so that the rest of libtiff knows what |
1706 | | * size of data will be passed between app and library, and |
1707 | | * assume that the app knows what it is doing and is not |
1708 | | * confused by these header manipulations... |
1709 | | */ |
1710 | 0 | switch (sp->user_datafmt) |
1711 | 0 | { |
1712 | 0 | case SGILOGDATAFMT_FLOAT: |
1713 | 0 | bps = 32; |
1714 | 0 | fmt = SAMPLEFORMAT_IEEEFP; |
1715 | 0 | break; |
1716 | 0 | case SGILOGDATAFMT_16BIT: |
1717 | 0 | bps = 16; |
1718 | 0 | fmt = SAMPLEFORMAT_INT; |
1719 | 0 | break; |
1720 | 0 | case SGILOGDATAFMT_RAW: |
1721 | 0 | bps = 32; |
1722 | 0 | fmt = SAMPLEFORMAT_UINT; |
1723 | 0 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); |
1724 | 0 | break; |
1725 | 0 | case SGILOGDATAFMT_8BIT: |
1726 | 0 | bps = 8; |
1727 | 0 | fmt = SAMPLEFORMAT_UINT; |
1728 | 0 | break; |
1729 | 0 | default: |
1730 | 0 | TIFFErrorExtR( |
1731 | 0 | tif, tif->tif_name, |
1732 | 0 | "Unknown data format %d for LogLuv compression", |
1733 | 0 | sp->user_datafmt); |
1734 | 0 | return (0); |
1735 | 0 | } |
1736 | 0 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps); |
1737 | 0 | TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, fmt); |
1738 | | /* |
1739 | | * Must recalculate sizes should bits/sample change. |
1740 | | */ |
1741 | 0 | tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)-1; |
1742 | 0 | tif->tif_scanlinesize = TIFFScanlineSize(tif); |
1743 | 0 | return (1); |
1744 | 0 | case TIFFTAG_SGILOGENCODE: |
1745 | 0 | sp->encode_meth = (int)va_arg(ap, int); |
1746 | 0 | if (sp->encode_meth != SGILOGENCODE_NODITHER && |
1747 | 0 | sp->encode_meth != SGILOGENCODE_RANDITHER) |
1748 | 0 | { |
1749 | 0 | TIFFErrorExtR(tif, module, |
1750 | 0 | "Unknown encoding %d for LogLuv compression", |
1751 | 0 | sp->encode_meth); |
1752 | 0 | return (0); |
1753 | 0 | } |
1754 | 0 | return (1); |
1755 | 0 | default: |
1756 | 0 | return (*sp->vsetparent)(tif, tag, ap); |
1757 | 0 | } |
1758 | 0 | } |
1759 | | |
1760 | | static int LogLuvVGetField(TIFF *tif, uint32_t tag, va_list ap) |
1761 | 0 | { |
1762 | 0 | LogLuvState *sp = (LogLuvState *)tif->tif_data; |
1763 | |
|
1764 | 0 | switch (tag) |
1765 | 0 | { |
1766 | 0 | case TIFFTAG_SGILOGDATAFMT: |
1767 | 0 | *va_arg(ap, int *) = sp->user_datafmt; |
1768 | 0 | return (1); |
1769 | 0 | default: |
1770 | 0 | return (*sp->vgetparent)(tif, tag, ap); |
1771 | 0 | } |
1772 | 0 | } |
1773 | | |
1774 | | static const TIFFField LogLuvFields[] = { |
1775 | | {TIFFTAG_SGILOGDATAFMT, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT, |
1776 | | TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "SGILogDataFmt", NULL}, |
1777 | | {TIFFTAG_SGILOGENCODE, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT, |
1778 | | TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "SGILogEncode", NULL}}; |
1779 | | |
1780 | | int TIFFInitSGILog(TIFF *tif, int scheme) |
1781 | 0 | { |
1782 | 0 | static const char module[] = "TIFFInitSGILog"; |
1783 | 0 | LogLuvState *sp; |
1784 | |
|
1785 | 0 | assert(scheme == COMPRESSION_SGILOG24 || scheme == COMPRESSION_SGILOG); |
1786 | | |
1787 | | /* |
1788 | | * Merge codec-specific tag information. |
1789 | | */ |
1790 | 0 | if (!_TIFFMergeFields(tif, LogLuvFields, TIFFArrayCount(LogLuvFields))) |
1791 | 0 | { |
1792 | 0 | TIFFErrorExtR(tif, module, "Merging SGILog codec-specific tags failed"); |
1793 | 0 | return 0; |
1794 | 0 | } |
1795 | | |
1796 | | /* |
1797 | | * Allocate state block so tag methods have storage to record values. |
1798 | | */ |
1799 | 0 | tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LogLuvState)); |
1800 | 0 | if (tif->tif_data == NULL) |
1801 | 0 | goto bad; |
1802 | 0 | sp = (LogLuvState *)tif->tif_data; |
1803 | 0 | _TIFFmemset((void *)sp, 0, sizeof(*sp)); |
1804 | 0 | sp->user_datafmt = SGILOGDATAFMT_UNKNOWN; |
1805 | 0 | sp->encode_meth = (scheme == COMPRESSION_SGILOG24) ? SGILOGENCODE_RANDITHER |
1806 | 0 | : SGILOGENCODE_NODITHER; |
1807 | 0 | sp->tfunc = _logLuvNop; |
1808 | | |
1809 | | /* |
1810 | | * Install codec methods. |
1811 | | * NB: tif_decoderow & tif_encoderow are filled |
1812 | | * in at setup time. |
1813 | | */ |
1814 | 0 | tif->tif_fixuptags = LogLuvFixupTags; |
1815 | 0 | tif->tif_setupdecode = LogLuvSetupDecode; |
1816 | 0 | tif->tif_decodestrip = LogLuvDecodeStrip; |
1817 | 0 | tif->tif_decodetile = LogLuvDecodeTile; |
1818 | 0 | tif->tif_setupencode = LogLuvSetupEncode; |
1819 | 0 | tif->tif_encodestrip = LogLuvEncodeStrip; |
1820 | 0 | tif->tif_encodetile = LogLuvEncodeTile; |
1821 | 0 | tif->tif_close = LogLuvClose; |
1822 | 0 | tif->tif_cleanup = LogLuvCleanup; |
1823 | | |
1824 | | /* |
1825 | | * Override parent get/set field methods. |
1826 | | */ |
1827 | 0 | sp->vgetparent = tif->tif_tagmethods.vgetfield; |
1828 | 0 | tif->tif_tagmethods.vgetfield = LogLuvVGetField; /* hook for codec tags */ |
1829 | 0 | sp->vsetparent = tif->tif_tagmethods.vsetfield; |
1830 | 0 | tif->tif_tagmethods.vsetfield = LogLuvVSetField; /* hook for codec tags */ |
1831 | |
|
1832 | 0 | return (1); |
1833 | 0 | bad: |
1834 | 0 | TIFFErrorExtR(tif, module, "%s: No space for LogLuv state block", |
1835 | 0 | tif->tif_name); |
1836 | 0 | return (0); |
1837 | 0 | } |
1838 | | #endif /* LOGLUV_SUPPORT */ |