/src/libtiff/libtiff/tif_getimage.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 1991-1997 Sam Leffler |
3 | | * Copyright (c) 1991-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 and Silicon Graphics may not be used in any advertising or |
10 | | * publicity relating to the software without the specific, prior written |
11 | | * permission of Sam Leffler 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 OR SILICON GRAPHICS BE LIABLE FOR |
18 | | * 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 | | /* |
26 | | * TIFF Library |
27 | | * |
28 | | * Read and return a packed RGBA image. |
29 | | */ |
30 | | #include "tiffiop.h" |
31 | | #include <limits.h> |
32 | | #include <stdio.h> |
33 | | |
34 | | static int gtTileContig(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t); |
35 | | static int gtTileSeparate(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t); |
36 | | static int gtStripContig(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t); |
37 | | static int gtStripSeparate(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t); |
38 | | static int PickContigCase(TIFFRGBAImage *); |
39 | | static int PickSeparateCase(TIFFRGBAImage *); |
40 | | |
41 | | static int BuildMapUaToAa(TIFFRGBAImage *img); |
42 | | static int BuildMapBitdepth16To8(TIFFRGBAImage *img); |
43 | | |
44 | | static const char photoTag[] = "PhotometricInterpretation"; |
45 | | |
46 | | /* |
47 | | * Helper constants used in Orientation tag handling |
48 | | */ |
49 | 780k | #define FLIP_VERTICALLY 0x01 |
50 | 319k | #define FLIP_HORIZONTALLY 0x02 |
51 | | |
52 | 270 | #define EMSG_BUF_SIZE 1024 |
53 | | |
54 | | /* |
55 | | * Color conversion constants. We will define display types here. |
56 | | */ |
57 | | |
58 | | static const TIFFDisplay display_sRGB = { |
59 | | {/* XYZ -> luminance matrix */ |
60 | | {3.2410F, -1.5374F, -0.4986F}, |
61 | | {-0.9692F, 1.8760F, 0.0416F}, |
62 | | {0.0556F, -0.2040F, 1.0570F}}, |
63 | | 100.0F, |
64 | | 100.0F, |
65 | | 100.0F, /* Light o/p for reference white */ |
66 | | 255, |
67 | | 255, |
68 | | 255, /* Pixel values for ref. white */ |
69 | | 1.0F, |
70 | | 1.0F, |
71 | | 1.0F, /* Residual light o/p for black pixel */ |
72 | | 2.4F, |
73 | | 2.4F, |
74 | | 2.4F, /* Gamma values for the three guns */ |
75 | | }; |
76 | | |
77 | | /* |
78 | | * Check the image to see if TIFFReadRGBAImage can deal with it. |
79 | | * 1/0 is returned according to whether or not the image can |
80 | | * be handled. If 0 is returned, emsg contains the reason |
81 | | * why it is being rejected. |
82 | | */ |
83 | | int TIFFRGBAImageOK(TIFF *tif, char emsg[EMSG_BUF_SIZE]) |
84 | 273k | { |
85 | 273k | TIFFDirectory *td = &tif->tif_dir; |
86 | 273k | uint16_t photometric; |
87 | 273k | int colorchannels; |
88 | | |
89 | 273k | if (!tif->tif_decodestatus) |
90 | 0 | { |
91 | 0 | snprintf(emsg, EMSG_BUF_SIZE, |
92 | 0 | "Sorry, requested compression method is not configured"); |
93 | 0 | return (0); |
94 | 0 | } |
95 | 273k | switch (td->td_bitspersample) |
96 | 273k | { |
97 | 44.7k | case 1: |
98 | 73.6k | case 2: |
99 | 75.6k | case 4: |
100 | 243k | case 8: |
101 | 273k | case 16: |
102 | 273k | break; |
103 | 28 | default: |
104 | 28 | snprintf(emsg, EMSG_BUF_SIZE, |
105 | 28 | "Sorry, can not handle images with %" PRIu16 |
106 | 28 | "-bit samples", |
107 | 28 | td->td_bitspersample); |
108 | 28 | return (0); |
109 | 273k | } |
110 | 273k | if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP) |
111 | 4 | { |
112 | 4 | snprintf( |
113 | 4 | emsg, EMSG_BUF_SIZE, |
114 | 4 | "Sorry, can not handle images with IEEE floating-point samples"); |
115 | 4 | return (0); |
116 | 4 | } |
117 | 273k | colorchannels = td->td_samplesperpixel - td->td_extrasamples; |
118 | 273k | if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) |
119 | 0 | { |
120 | 0 | switch (colorchannels) |
121 | 0 | { |
122 | 0 | case 1: |
123 | 0 | photometric = PHOTOMETRIC_MINISBLACK; |
124 | 0 | break; |
125 | 0 | case 3: |
126 | 0 | photometric = PHOTOMETRIC_RGB; |
127 | 0 | break; |
128 | 0 | default: |
129 | 0 | snprintf(emsg, EMSG_BUF_SIZE, "Missing needed %s tag", |
130 | 0 | photoTag); |
131 | 0 | return (0); |
132 | 0 | } |
133 | 0 | } |
134 | 273k | switch (photometric) |
135 | 273k | { |
136 | 56.3k | case PHOTOMETRIC_MINISWHITE: |
137 | 99.7k | case PHOTOMETRIC_MINISBLACK: |
138 | 126k | case PHOTOMETRIC_PALETTE: |
139 | 126k | if (td->td_planarconfig == PLANARCONFIG_CONTIG && |
140 | 126k | td->td_samplesperpixel != 1 && td->td_bitspersample < 8) |
141 | 3 | { |
142 | 3 | snprintf( |
143 | 3 | emsg, EMSG_BUF_SIZE, |
144 | 3 | "Sorry, can not handle contiguous data with %s=%" PRIu16 |
145 | 3 | ", " |
146 | 3 | "and %s=%" PRIu16 " and Bits/Sample=%" PRIu16 "", |
147 | 3 | photoTag, photometric, "Samples/pixel", |
148 | 3 | td->td_samplesperpixel, td->td_bitspersample); |
149 | 3 | return (0); |
150 | 3 | } |
151 | | /* |
152 | | * We should likely validate that any extra samples are either |
153 | | * to be ignored, or are alpha, and if alpha we should try to use |
154 | | * them. But for now we won't bother with this. |
155 | | */ |
156 | 126k | break; |
157 | 126k | case PHOTOMETRIC_YCBCR: |
158 | | /* |
159 | | * TODO: if at all meaningful and useful, make more complete |
160 | | * support check here, or better still, refactor to let supporting |
161 | | * code decide whether there is support and what meaningful |
162 | | * error to return |
163 | | */ |
164 | 85.1k | break; |
165 | 34.4k | case PHOTOMETRIC_RGB: |
166 | 34.4k | if (colorchannels < 3) |
167 | 4 | { |
168 | 4 | snprintf(emsg, EMSG_BUF_SIZE, |
169 | 4 | "Sorry, can not handle RGB image with %s=%d", |
170 | 4 | "Color channels", colorchannels); |
171 | 4 | return (0); |
172 | 4 | } |
173 | 34.4k | break; |
174 | 34.4k | case PHOTOMETRIC_SEPARATED: |
175 | 8.77k | { |
176 | 8.77k | uint16_t inkset; |
177 | 8.77k | TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset); |
178 | 8.77k | if (inkset != INKSET_CMYK) |
179 | 3 | { |
180 | 3 | snprintf(emsg, EMSG_BUF_SIZE, |
181 | 3 | "Sorry, can not handle separated image with %s=%d", |
182 | 3 | "InkSet", inkset); |
183 | 3 | return 0; |
184 | 3 | } |
185 | 8.76k | if (td->td_samplesperpixel < 4) |
186 | 4 | { |
187 | 4 | snprintf( |
188 | 4 | emsg, EMSG_BUF_SIZE, |
189 | 4 | "Sorry, can not handle separated image with %s=%" PRIu16, |
190 | 4 | "Samples/pixel", td->td_samplesperpixel); |
191 | 4 | return 0; |
192 | 4 | } |
193 | 8.76k | break; |
194 | 8.76k | } |
195 | 8.76k | case PHOTOMETRIC_LOGL: |
196 | 0 | if (td->td_compression != COMPRESSION_SGILOG) |
197 | 0 | { |
198 | 0 | snprintf(emsg, EMSG_BUF_SIZE, |
199 | 0 | "Sorry, LogL data must have %s=%d", "Compression", |
200 | 0 | COMPRESSION_SGILOG); |
201 | 0 | return (0); |
202 | 0 | } |
203 | 0 | break; |
204 | 0 | case PHOTOMETRIC_LOGLUV: |
205 | 0 | if (td->td_compression != COMPRESSION_SGILOG && |
206 | 0 | td->td_compression != COMPRESSION_SGILOG24) |
207 | 0 | { |
208 | 0 | snprintf(emsg, EMSG_BUF_SIZE, |
209 | 0 | "Sorry, LogLuv data must have %s=%d or %d", |
210 | 0 | "Compression", COMPRESSION_SGILOG, |
211 | 0 | COMPRESSION_SGILOG24); |
212 | 0 | return (0); |
213 | 0 | } |
214 | 0 | if (td->td_planarconfig != PLANARCONFIG_CONTIG) |
215 | 0 | { |
216 | 0 | snprintf(emsg, EMSG_BUF_SIZE, |
217 | 0 | "Sorry, can not handle LogLuv images with %s=%" PRIu16, |
218 | 0 | "Planarconfiguration", td->td_planarconfig); |
219 | 0 | return (0); |
220 | 0 | } |
221 | 0 | if (td->td_samplesperpixel != 3 || colorchannels != 3) |
222 | 0 | { |
223 | 0 | snprintf(emsg, EMSG_BUF_SIZE, |
224 | 0 | "Sorry, can not handle image with %s=%" PRIu16 |
225 | 0 | ", %s=%d", |
226 | 0 | "Samples/pixel", td->td_samplesperpixel, |
227 | 0 | "colorchannels", colorchannels); |
228 | 0 | return 0; |
229 | 0 | } |
230 | 0 | break; |
231 | 18.3k | case PHOTOMETRIC_CIELAB: |
232 | 18.3k | if (td->td_samplesperpixel != 3 || colorchannels != 3 || |
233 | 18.3k | (td->td_bitspersample != 8 && td->td_bitspersample != 16)) |
234 | 9 | { |
235 | 9 | snprintf(emsg, EMSG_BUF_SIZE, |
236 | 9 | "Sorry, can not handle image with %s=%" PRIu16 |
237 | 9 | ", %s=%d and %s=%" PRIu16, |
238 | 9 | "Samples/pixel", td->td_samplesperpixel, |
239 | 9 | "colorchannels", colorchannels, "Bits/sample", |
240 | 9 | td->td_bitspersample); |
241 | 9 | return 0; |
242 | 9 | } |
243 | 18.3k | break; |
244 | 18.3k | default: |
245 | 120 | snprintf(emsg, EMSG_BUF_SIZE, |
246 | 120 | "Sorry, can not handle image with %s=%" PRIu16, photoTag, |
247 | 120 | photometric); |
248 | 120 | return (0); |
249 | 273k | } |
250 | 273k | return (1); |
251 | 273k | } |
252 | | |
253 | | void TIFFRGBAImageEnd(TIFFRGBAImage *img) |
254 | 273k | { |
255 | 273k | if (img->Map) |
256 | 4 | { |
257 | 4 | _TIFFfreeExt(img->tif, img->Map); |
258 | 4 | img->Map = NULL; |
259 | 4 | } |
260 | 273k | if (img->BWmap) |
261 | 97.0k | { |
262 | 97.0k | _TIFFfreeExt(img->tif, img->BWmap); |
263 | 97.0k | img->BWmap = NULL; |
264 | 97.0k | } |
265 | 273k | if (img->PALmap) |
266 | 27.0k | { |
267 | 27.0k | _TIFFfreeExt(img->tif, img->PALmap); |
268 | 27.0k | img->PALmap = NULL; |
269 | 27.0k | } |
270 | 273k | if (img->ycbcr) |
271 | 85.0k | { |
272 | 85.0k | _TIFFfreeExt(img->tif, img->ycbcr); |
273 | 85.0k | img->ycbcr = NULL; |
274 | 85.0k | } |
275 | 273k | if (img->cielab) |
276 | 18.3k | { |
277 | 18.3k | _TIFFfreeExt(img->tif, img->cielab); |
278 | 18.3k | img->cielab = NULL; |
279 | 18.3k | } |
280 | 273k | if (img->UaToAa) |
281 | 1.22k | { |
282 | 1.22k | _TIFFfreeExt(img->tif, img->UaToAa); |
283 | 1.22k | img->UaToAa = NULL; |
284 | 1.22k | } |
285 | 273k | if (img->Bitdepth16To8) |
286 | 22.7k | { |
287 | 22.7k | _TIFFfreeExt(img->tif, img->Bitdepth16To8); |
288 | 22.7k | img->Bitdepth16To8 = NULL; |
289 | 22.7k | } |
290 | | |
291 | 273k | if (img->redcmap) |
292 | 27.0k | { |
293 | 27.0k | _TIFFfreeExt(img->tif, img->redcmap); |
294 | 27.0k | _TIFFfreeExt(img->tif, img->greencmap); |
295 | 27.0k | _TIFFfreeExt(img->tif, img->bluecmap); |
296 | 27.0k | img->redcmap = img->greencmap = img->bluecmap = NULL; |
297 | 27.0k | } |
298 | 273k | } |
299 | | |
300 | | static int isCCITTCompression(TIFF *tif) |
301 | 0 | { |
302 | 0 | uint16_t compress; |
303 | 0 | TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress); |
304 | 0 | return (compress == COMPRESSION_CCITTFAX3 || |
305 | 0 | compress == COMPRESSION_CCITTFAX4 || |
306 | 0 | compress == COMPRESSION_CCITTRLE || |
307 | 0 | compress == COMPRESSION_CCITTRLEW); |
308 | 0 | } |
309 | | |
310 | | int TIFFRGBAImageBegin(TIFFRGBAImage *img, TIFF *tif, int stop, |
311 | | char emsg[EMSG_BUF_SIZE]) |
312 | 273k | { |
313 | 273k | uint16_t *sampleinfo; |
314 | 273k | uint16_t extrasamples; |
315 | 273k | uint16_t planarconfig; |
316 | 273k | uint16_t compress; |
317 | 273k | int colorchannels; |
318 | 273k | uint16_t *red_orig, *green_orig, *blue_orig; |
319 | 273k | int n_color; |
320 | | |
321 | 273k | if (!TIFFRGBAImageOK(tif, emsg)) |
322 | 175 | return 0; |
323 | | |
324 | | /* Initialize to normal values */ |
325 | 273k | img->row_offset = 0; |
326 | 273k | img->col_offset = 0; |
327 | 273k | img->redcmap = NULL; |
328 | 273k | img->greencmap = NULL; |
329 | 273k | img->bluecmap = NULL; |
330 | 273k | img->Map = NULL; |
331 | 273k | img->BWmap = NULL; |
332 | 273k | img->PALmap = NULL; |
333 | 273k | img->ycbcr = NULL; |
334 | 273k | img->cielab = NULL; |
335 | 273k | img->UaToAa = NULL; |
336 | 273k | img->Bitdepth16To8 = NULL; |
337 | 273k | img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */ |
338 | | |
339 | 273k | img->tif = tif; |
340 | 273k | img->stoponerr = stop; |
341 | 273k | TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample); |
342 | 273k | switch (img->bitspersample) |
343 | 273k | { |
344 | 44.6k | case 1: |
345 | 73.5k | case 2: |
346 | 75.5k | case 4: |
347 | 243k | case 8: |
348 | 273k | case 16: |
349 | 273k | break; |
350 | 0 | default: |
351 | 0 | snprintf(emsg, EMSG_BUF_SIZE, |
352 | 0 | "Sorry, can not handle images with %" PRIu16 |
353 | 0 | "-bit samples", |
354 | 0 | img->bitspersample); |
355 | 0 | goto fail_return; |
356 | 273k | } |
357 | 273k | img->alpha = 0; |
358 | 273k | TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel); |
359 | 273k | TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extrasamples, |
360 | 273k | &sampleinfo); |
361 | 273k | if (extrasamples >= 1) |
362 | 35.3k | { |
363 | 35.3k | switch (sampleinfo[0]) |
364 | 35.3k | { |
365 | 24.9k | case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without |
366 | | */ |
367 | 24.9k | if (img->samplesperpixel > |
368 | 24.9k | 3) /* correct info about alpha channel */ |
369 | 14.3k | img->alpha = EXTRASAMPLE_ASSOCALPHA; |
370 | 24.9k | break; |
371 | 7.71k | case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */ |
372 | 10.4k | case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */ |
373 | 10.4k | img->alpha = sampleinfo[0]; |
374 | 10.4k | break; |
375 | 35.3k | } |
376 | 35.3k | } |
377 | | |
378 | 273k | #ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA |
379 | 273k | if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) |
380 | 0 | img->photometric = PHOTOMETRIC_MINISWHITE; |
381 | | |
382 | 273k | if (extrasamples == 0 && img->samplesperpixel == 4 && |
383 | 273k | img->photometric == PHOTOMETRIC_RGB) |
384 | 0 | { |
385 | 0 | img->alpha = EXTRASAMPLE_ASSOCALPHA; |
386 | 0 | extrasamples = 1; |
387 | 0 | } |
388 | 273k | #endif |
389 | | |
390 | 273k | colorchannels = img->samplesperpixel - extrasamples; |
391 | 273k | TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress); |
392 | 273k | TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig); |
393 | 273k | if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) |
394 | 0 | { |
395 | 0 | switch (colorchannels) |
396 | 0 | { |
397 | 0 | case 1: |
398 | 0 | if (isCCITTCompression(tif)) |
399 | 0 | img->photometric = PHOTOMETRIC_MINISWHITE; |
400 | 0 | else |
401 | 0 | img->photometric = PHOTOMETRIC_MINISBLACK; |
402 | 0 | break; |
403 | 0 | case 3: |
404 | 0 | img->photometric = PHOTOMETRIC_RGB; |
405 | 0 | break; |
406 | 0 | default: |
407 | 0 | snprintf(emsg, EMSG_BUF_SIZE, "Missing needed %s tag", |
408 | 0 | photoTag); |
409 | 0 | goto fail_return; |
410 | 0 | } |
411 | 0 | } |
412 | 273k | switch (img->photometric) |
413 | 273k | { |
414 | 27.0k | case PHOTOMETRIC_PALETTE: |
415 | 27.0k | if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &red_orig, &green_orig, |
416 | 27.0k | &blue_orig)) |
417 | 0 | { |
418 | 0 | snprintf(emsg, EMSG_BUF_SIZE, |
419 | 0 | "Missing required \"Colormap\" tag"); |
420 | 0 | goto fail_return; |
421 | 0 | } |
422 | | |
423 | | /* copy the colormaps so we can modify them */ |
424 | 27.0k | n_color = (1U << img->bitspersample); |
425 | 27.0k | img->redcmap = |
426 | 27.0k | (uint16_t *)_TIFFmallocExt(tif, sizeof(uint16_t) * n_color); |
427 | 27.0k | img->greencmap = |
428 | 27.0k | (uint16_t *)_TIFFmallocExt(tif, sizeof(uint16_t) * n_color); |
429 | 27.0k | img->bluecmap = |
430 | 27.0k | (uint16_t *)_TIFFmallocExt(tif, sizeof(uint16_t) * n_color); |
431 | 27.0k | if (!img->redcmap || !img->greencmap || !img->bluecmap) |
432 | 0 | { |
433 | 0 | snprintf(emsg, EMSG_BUF_SIZE, |
434 | 0 | "Out of memory for colormap copy"); |
435 | 0 | goto fail_return; |
436 | 0 | } |
437 | | |
438 | 27.0k | _TIFFmemcpy(img->redcmap, red_orig, n_color * 2); |
439 | 27.0k | _TIFFmemcpy(img->greencmap, green_orig, n_color * 2); |
440 | 27.0k | _TIFFmemcpy(img->bluecmap, blue_orig, n_color * 2); |
441 | | |
442 | | /* fall through... */ |
443 | 83.3k | case PHOTOMETRIC_MINISWHITE: |
444 | 126k | case PHOTOMETRIC_MINISBLACK: |
445 | 126k | if (planarconfig == PLANARCONFIG_CONTIG && |
446 | 126k | img->samplesperpixel != 1 && img->bitspersample < 8) |
447 | 0 | { |
448 | 0 | snprintf( |
449 | 0 | emsg, EMSG_BUF_SIZE, |
450 | 0 | "Sorry, can not handle contiguous data with %s=%" PRIu16 |
451 | 0 | ", " |
452 | 0 | "and %s=%" PRIu16 " and Bits/Sample=%" PRIu16, |
453 | 0 | photoTag, img->photometric, "Samples/pixel", |
454 | 0 | img->samplesperpixel, img->bitspersample); |
455 | 0 | goto fail_return; |
456 | 0 | } |
457 | 126k | break; |
458 | 126k | case PHOTOMETRIC_YCBCR: |
459 | | /* It would probably be nice to have a reality check here. */ |
460 | 85.1k | if (planarconfig == PLANARCONFIG_CONTIG) |
461 | | /* can rely on libjpeg to convert to RGB */ |
462 | | /* XXX should restore current state on exit */ |
463 | 83.8k | switch (compress) |
464 | 83.8k | { |
465 | 8 | case COMPRESSION_JPEG: |
466 | | /* |
467 | | * TODO: when complete tests verify complete |
468 | | * desubsampling and YCbCr handling, remove use of |
469 | | * TIFFTAG_JPEGCOLORMODE in favor of tif_getimage.c |
470 | | * native handling |
471 | | */ |
472 | 8 | TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, |
473 | 8 | JPEGCOLORMODE_RGB); |
474 | 8 | img->photometric = PHOTOMETRIC_RGB; |
475 | 8 | break; |
476 | 83.8k | default: |
477 | 83.8k | /* do nothing */; |
478 | 83.8k | break; |
479 | 83.8k | } |
480 | | /* |
481 | | * TODO: if at all meaningful and useful, make more complete |
482 | | * support check here, or better still, refactor to let supporting |
483 | | * code decide whether there is support and what meaningful |
484 | | * error to return |
485 | | */ |
486 | 85.1k | break; |
487 | 85.1k | case PHOTOMETRIC_RGB: |
488 | 34.4k | if (colorchannels < 3) |
489 | 0 | { |
490 | 0 | snprintf(emsg, EMSG_BUF_SIZE, |
491 | 0 | "Sorry, can not handle RGB image with %s=%d", |
492 | 0 | "Color channels", colorchannels); |
493 | 0 | goto fail_return; |
494 | 0 | } |
495 | 34.4k | break; |
496 | 34.4k | case PHOTOMETRIC_SEPARATED: |
497 | 8.76k | { |
498 | 8.76k | uint16_t inkset; |
499 | 8.76k | TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset); |
500 | 8.76k | if (inkset != INKSET_CMYK) |
501 | 0 | { |
502 | 0 | snprintf( |
503 | 0 | emsg, EMSG_BUF_SIZE, |
504 | 0 | "Sorry, can not handle separated image with %s=%" PRIu16, |
505 | 0 | "InkSet", inkset); |
506 | 0 | goto fail_return; |
507 | 0 | } |
508 | 8.76k | if (img->samplesperpixel < 4) |
509 | 0 | { |
510 | 0 | snprintf( |
511 | 0 | emsg, EMSG_BUF_SIZE, |
512 | 0 | "Sorry, can not handle separated image with %s=%" PRIu16, |
513 | 0 | "Samples/pixel", img->samplesperpixel); |
514 | 0 | goto fail_return; |
515 | 0 | } |
516 | 8.76k | } |
517 | 8.76k | break; |
518 | 8.76k | case PHOTOMETRIC_LOGL: |
519 | 0 | if (compress != COMPRESSION_SGILOG) |
520 | 0 | { |
521 | 0 | snprintf(emsg, EMSG_BUF_SIZE, |
522 | 0 | "Sorry, LogL data must have %s=%d", "Compression", |
523 | 0 | COMPRESSION_SGILOG); |
524 | 0 | goto fail_return; |
525 | 0 | } |
526 | 0 | TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT); |
527 | 0 | img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */ |
528 | 0 | img->bitspersample = 8; |
529 | 0 | break; |
530 | 0 | case PHOTOMETRIC_LOGLUV: |
531 | 0 | if (compress != COMPRESSION_SGILOG && |
532 | 0 | compress != COMPRESSION_SGILOG24) |
533 | 0 | { |
534 | 0 | snprintf(emsg, EMSG_BUF_SIZE, |
535 | 0 | "Sorry, LogLuv data must have %s=%d or %d", |
536 | 0 | "Compression", COMPRESSION_SGILOG, |
537 | 0 | COMPRESSION_SGILOG24); |
538 | 0 | goto fail_return; |
539 | 0 | } |
540 | 0 | if (planarconfig != PLANARCONFIG_CONTIG) |
541 | 0 | { |
542 | 0 | snprintf(emsg, EMSG_BUF_SIZE, |
543 | 0 | "Sorry, can not handle LogLuv images with %s=%" PRIu16, |
544 | 0 | "Planarconfiguration", planarconfig); |
545 | 0 | return (0); |
546 | 0 | } |
547 | 0 | TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT); |
548 | 0 | img->photometric = PHOTOMETRIC_RGB; /* little white lie */ |
549 | 0 | img->bitspersample = 8; |
550 | 0 | break; |
551 | 18.3k | case PHOTOMETRIC_CIELAB: |
552 | 18.3k | break; |
553 | 0 | default: |
554 | 0 | snprintf(emsg, EMSG_BUF_SIZE, |
555 | 0 | "Sorry, can not handle image with %s=%" PRIu16, photoTag, |
556 | 0 | img->photometric); |
557 | 0 | goto fail_return; |
558 | 273k | } |
559 | 273k | TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width); |
560 | 273k | TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height); |
561 | 273k | TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation); |
562 | 273k | img->isContig = |
563 | 273k | !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1); |
564 | 273k | if (img->isContig) |
565 | 261k | { |
566 | 261k | if (!PickContigCase(img)) |
567 | 63 | { |
568 | 63 | snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image"); |
569 | 63 | goto fail_return; |
570 | 63 | } |
571 | 261k | } |
572 | 12.4k | else |
573 | 12.4k | { |
574 | 12.4k | if (!PickSeparateCase(img)) |
575 | 32 | { |
576 | 32 | snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image"); |
577 | 32 | goto fail_return; |
578 | 32 | } |
579 | 12.4k | } |
580 | 273k | return 1; |
581 | | |
582 | 95 | fail_return: |
583 | 95 | TIFFRGBAImageEnd(img); |
584 | 95 | return 0; |
585 | 273k | } |
586 | | |
587 | | int TIFFRGBAImageGet(TIFFRGBAImage *img, uint32_t *raster, uint32_t w, |
588 | | uint32_t h) |
589 | 273k | { |
590 | 273k | if (img->get == NULL) |
591 | 0 | { |
592 | 0 | TIFFErrorExtR(img->tif, TIFFFileName(img->tif), |
593 | 0 | "No \"get\" routine setup"); |
594 | 0 | return (0); |
595 | 0 | } |
596 | 273k | if (img->put.any == NULL) |
597 | 0 | { |
598 | 0 | TIFFErrorExtR( |
599 | 0 | img->tif, TIFFFileName(img->tif), |
600 | 0 | "No \"put\" routine setupl; probably can not handle image format"); |
601 | 0 | return (0); |
602 | 0 | } |
603 | | /* Verify raster height against image height. |
604 | | * Width is checked in img->get() function individually. */ |
605 | 273k | if (0 <= img->row_offset && (uint32_t)img->row_offset < img->height) |
606 | 273k | { |
607 | 273k | uint32_t hx = img->height - img->row_offset; |
608 | 273k | if (h > hx) |
609 | 0 | { |
610 | | /* Adapt parameters to read only available lines and put image |
611 | | * at the bottom of the raster. */ |
612 | 0 | raster += (size_t)(h - hx) * w; |
613 | 0 | h = hx; |
614 | 0 | } |
615 | 273k | } |
616 | 0 | else |
617 | 0 | { |
618 | 0 | TIFFErrorExtR(img->tif, TIFFFileName(img->tif), |
619 | 0 | "Error in TIFFRGBAImageGet: row offset %d exceeds " |
620 | 0 | "image height %d", |
621 | 0 | img->row_offset, img->height); |
622 | 0 | return 0; |
623 | 0 | } |
624 | 273k | return (*img->get)(img, raster, w, h); |
625 | 273k | } |
626 | | |
627 | | /* |
628 | | * Read the specified image into an ABGR-format rastertaking in account |
629 | | * specified orientation. |
630 | | */ |
631 | | int TIFFReadRGBAImageOriented(TIFF *tif, uint32_t rwidth, uint32_t rheight, |
632 | | uint32_t *raster, int orientation, int stop) |
633 | 3.87k | { |
634 | 3.87k | char emsg[EMSG_BUF_SIZE] = ""; |
635 | 3.87k | TIFFRGBAImage img; |
636 | 3.87k | int ok; |
637 | | |
638 | 3.87k | if (TIFFRGBAImageBegin(&img, tif, stop, emsg)) |
639 | 3.70k | { |
640 | 3.70k | img.req_orientation = (uint16_t)orientation; |
641 | 3.70k | ok = TIFFRGBAImageGet(&img, raster, rwidth, rheight); |
642 | 3.70k | TIFFRGBAImageEnd(&img); |
643 | 3.70k | } |
644 | 163 | else |
645 | 163 | { |
646 | 163 | TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg); |
647 | 163 | ok = 0; |
648 | 163 | } |
649 | 3.87k | return (ok); |
650 | 3.87k | } |
651 | | |
652 | | /* |
653 | | * Read the specified image into an ABGR-format raster. Use bottom left |
654 | | * origin for raster by default. |
655 | | */ |
656 | | int TIFFReadRGBAImage(TIFF *tif, uint32_t rwidth, uint32_t rheight, |
657 | | uint32_t *raster, int stop) |
658 | 3.87k | { |
659 | 3.87k | return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster, |
660 | 3.87k | ORIENTATION_BOTLEFT, stop); |
661 | 3.87k | } |
662 | | |
663 | | static int setorientation(TIFFRGBAImage *img) |
664 | 273k | { |
665 | 273k | switch (img->orientation) |
666 | 273k | { |
667 | 214k | case ORIENTATION_TOPLEFT: |
668 | 220k | case ORIENTATION_LEFTTOP: |
669 | 220k | if (img->req_orientation == ORIENTATION_TOPRIGHT || |
670 | 220k | img->req_orientation == ORIENTATION_RIGHTTOP) |
671 | 0 | return FLIP_HORIZONTALLY; |
672 | 220k | else if (img->req_orientation == ORIENTATION_BOTRIGHT || |
673 | 220k | img->req_orientation == ORIENTATION_RIGHTBOT) |
674 | 0 | return FLIP_HORIZONTALLY | FLIP_VERTICALLY; |
675 | 220k | else if (img->req_orientation == ORIENTATION_BOTLEFT || |
676 | 220k | img->req_orientation == ORIENTATION_LEFTBOT) |
677 | 220k | return FLIP_VERTICALLY; |
678 | 0 | else |
679 | 0 | return 0; |
680 | 4.72k | case ORIENTATION_TOPRIGHT: |
681 | 15.7k | case ORIENTATION_RIGHTTOP: |
682 | 15.7k | if (img->req_orientation == ORIENTATION_TOPLEFT || |
683 | 15.7k | img->req_orientation == ORIENTATION_LEFTTOP) |
684 | 0 | return FLIP_HORIZONTALLY; |
685 | 15.7k | else if (img->req_orientation == ORIENTATION_BOTRIGHT || |
686 | 15.7k | img->req_orientation == ORIENTATION_RIGHTBOT) |
687 | 0 | return FLIP_VERTICALLY; |
688 | 15.7k | else if (img->req_orientation == ORIENTATION_BOTLEFT || |
689 | 15.7k | img->req_orientation == ORIENTATION_LEFTBOT) |
690 | 15.7k | return FLIP_HORIZONTALLY | FLIP_VERTICALLY; |
691 | 0 | else |
692 | 0 | return 0; |
693 | 19.8k | case ORIENTATION_BOTRIGHT: |
694 | 30.2k | case ORIENTATION_RIGHTBOT: |
695 | 30.2k | if (img->req_orientation == ORIENTATION_TOPLEFT || |
696 | 30.2k | img->req_orientation == ORIENTATION_LEFTTOP) |
697 | 0 | return FLIP_HORIZONTALLY | FLIP_VERTICALLY; |
698 | 30.2k | else if (img->req_orientation == ORIENTATION_TOPRIGHT || |
699 | 30.2k | img->req_orientation == ORIENTATION_RIGHTTOP) |
700 | 0 | return FLIP_VERTICALLY; |
701 | 30.2k | else if (img->req_orientation == ORIENTATION_BOTLEFT || |
702 | 30.2k | img->req_orientation == ORIENTATION_LEFTBOT) |
703 | 30.2k | return FLIP_HORIZONTALLY; |
704 | 0 | else |
705 | 0 | return 0; |
706 | 2.58k | case ORIENTATION_BOTLEFT: |
707 | 7.31k | case ORIENTATION_LEFTBOT: |
708 | 7.31k | if (img->req_orientation == ORIENTATION_TOPLEFT || |
709 | 7.31k | img->req_orientation == ORIENTATION_LEFTTOP) |
710 | 0 | return FLIP_VERTICALLY; |
711 | 7.31k | else if (img->req_orientation == ORIENTATION_TOPRIGHT || |
712 | 7.31k | img->req_orientation == ORIENTATION_RIGHTTOP) |
713 | 0 | return FLIP_HORIZONTALLY | FLIP_VERTICALLY; |
714 | 7.31k | else if (img->req_orientation == ORIENTATION_BOTRIGHT || |
715 | 7.31k | img->req_orientation == ORIENTATION_RIGHTBOT) |
716 | 0 | return FLIP_HORIZONTALLY; |
717 | 7.31k | else |
718 | 7.31k | return 0; |
719 | 0 | default: /* NOTREACHED */ |
720 | 0 | return 0; |
721 | 273k | } |
722 | 273k | } |
723 | | |
724 | | /* |
725 | | * Get an tile-organized image that has |
726 | | * PlanarConfiguration contiguous if SamplesPerPixel > 1 |
727 | | * or |
728 | | * SamplesPerPixel == 1 |
729 | | */ |
730 | | static int gtTileContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t w, |
731 | | uint32_t h) |
732 | 189k | { |
733 | 189k | TIFF *tif = img->tif; |
734 | 189k | tileContigRoutine put = img->put.contig; |
735 | 189k | uint32_t col, row, y, rowstoread; |
736 | 189k | tmsize_t pos; |
737 | 189k | uint32_t tw, th; |
738 | 189k | unsigned char *buf = NULL; |
739 | 189k | int32_t fromskew, toskew; |
740 | 189k | uint32_t nrow; |
741 | 189k | int ret = 1, flip; |
742 | 189k | uint32_t this_tw, tocol; |
743 | 189k | int32_t this_toskew, leftmost_toskew; |
744 | 189k | int32_t leftmost_fromskew; |
745 | 189k | uint32_t leftmost_tw; |
746 | 189k | tmsize_t bufsize; |
747 | | |
748 | | /* If the raster is smaller than the image, |
749 | | * or if there is a col_offset, adapt the samples to be copied per row. */ |
750 | 189k | uint32_t wmin; |
751 | | |
752 | 189k | if (0 <= img->col_offset && (uint32_t)img->col_offset < img->width) |
753 | 189k | { |
754 | 189k | wmin = TIFFmin(w, img->width - img->col_offset); |
755 | 189k | } |
756 | 0 | else |
757 | 0 | { |
758 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), |
759 | 0 | "Error in gtTileContig: column offset %d exceeds " |
760 | 0 | "image width %d", |
761 | 0 | img->col_offset, img->width); |
762 | 0 | return 0; |
763 | 0 | } |
764 | 189k | bufsize = TIFFTileSize(tif); |
765 | 189k | if (bufsize == 0) |
766 | 0 | { |
767 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), "%s", "No space for tile buffer"); |
768 | 0 | return (0); |
769 | 0 | } |
770 | | |
771 | 189k | TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw); |
772 | 189k | TIFFGetField(tif, TIFFTAG_TILELENGTH, &th); |
773 | | |
774 | 189k | flip = setorientation(img); |
775 | 189k | if (flip & FLIP_VERTICALLY) |
776 | 161k | { |
777 | 161k | if (((int64_t)tw + w) > INT_MAX) |
778 | 0 | { |
779 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), "%s", |
780 | 0 | "unsupported tile size (too wide)"); |
781 | 0 | return (0); |
782 | 0 | } |
783 | 161k | y = h - 1; |
784 | 161k | toskew = -(int32_t)(tw + w); |
785 | 161k | } |
786 | 28.2k | else |
787 | 28.2k | { |
788 | 28.2k | if (tw > ((int64_t)INT_MAX + w)) |
789 | 0 | { |
790 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), "%s", |
791 | 0 | "unsupported tile size (too wide)"); |
792 | 0 | return (0); |
793 | 0 | } |
794 | 28.2k | y = 0; |
795 | 28.2k | toskew = -(int32_t)(tw - w); |
796 | 28.2k | } |
797 | | |
798 | 189k | if (tw == 0 || th == 0) |
799 | 0 | { |
800 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero"); |
801 | 0 | return (0); |
802 | 0 | } |
803 | | |
804 | | /* |
805 | | * Leftmost tile is clipped on left side if col_offset > 0. |
806 | | */ |
807 | 189k | leftmost_fromskew = img->col_offset % tw; |
808 | 189k | leftmost_tw = tw - leftmost_fromskew; |
809 | 189k | int64_t skew_i64 = (int64_t)toskew + leftmost_fromskew; |
810 | 189k | if (skew_i64 > INT_MAX || skew_i64 < INT_MIN) |
811 | 0 | { |
812 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), "%s %" PRId64, "Invalid skew", |
813 | 0 | skew_i64); |
814 | 0 | return (0); |
815 | 0 | } |
816 | 189k | leftmost_toskew = (int32_t)skew_i64; |
817 | 379k | for (row = 0; ret != 0 && row < h; row += nrow) |
818 | 189k | { |
819 | 189k | rowstoread = th - (row + img->row_offset) % th; |
820 | 189k | nrow = (row + rowstoread > h ? h - row : rowstoread); |
821 | 189k | fromskew = leftmost_fromskew; |
822 | 189k | this_tw = leftmost_tw; |
823 | 189k | this_toskew = leftmost_toskew; |
824 | 189k | tocol = 0; |
825 | 189k | col = img->col_offset; |
826 | | /* wmin: only write imagewidth if raster is bigger. */ |
827 | 378k | while (tocol < wmin) |
828 | 189k | { |
829 | 189k | if (_TIFFReadTileAndAllocBuffer(tif, (void **)&buf, bufsize, col, |
830 | 189k | row + img->row_offset, 0, |
831 | 189k | 0) == (tmsize_t)(-1) && |
832 | 189k | (buf == NULL || img->stoponerr)) |
833 | 505 | { |
834 | 505 | ret = 0; |
835 | 505 | break; |
836 | 505 | } |
837 | 189k | pos = ((row + img->row_offset) % th) * TIFFTileRowSize(tif) + |
838 | 189k | ((tmsize_t)fromskew * img->samplesperpixel); |
839 | 189k | if (tocol + this_tw > wmin) |
840 | 7.88k | { |
841 | | /* |
842 | | * Rightmost tile is clipped on right side. |
843 | | */ |
844 | 7.88k | fromskew = tw - (wmin - tocol); |
845 | 7.88k | this_tw = tw - fromskew; |
846 | 7.88k | this_toskew = toskew + fromskew; |
847 | 7.88k | } |
848 | 189k | tmsize_t roffset = (tmsize_t)y * w + tocol; |
849 | 189k | (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew, |
850 | 189k | this_toskew, buf + pos); |
851 | 189k | tocol += this_tw; |
852 | 189k | col += this_tw; |
853 | | /* |
854 | | * After the leftmost tile, tiles are no longer clipped on left |
855 | | * side. |
856 | | */ |
857 | 189k | fromskew = 0; |
858 | 189k | this_tw = tw; |
859 | 189k | this_toskew = toskew; |
860 | 189k | } |
861 | | |
862 | 189k | y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow); |
863 | 189k | } |
864 | 189k | _TIFFfreeExt(img->tif, buf); |
865 | | |
866 | 189k | if (flip & FLIP_HORIZONTALLY) |
867 | 36.4k | { |
868 | 36.4k | uint32_t line; |
869 | | |
870 | 213k | for (line = 0; line < h; line++) |
871 | 177k | { |
872 | 177k | uint32_t *left = raster + (line * w); |
873 | | /* Use wmin to only flip horizontally data in place and not complete |
874 | | * raster-row. */ |
875 | 177k | uint32_t *right = left + wmin - 1; |
876 | | |
877 | 792k | while (left < right) |
878 | 615k | { |
879 | 615k | uint32_t temp = *left; |
880 | 615k | *left = *right; |
881 | 615k | *right = temp; |
882 | 615k | left++; |
883 | 615k | right--; |
884 | 615k | } |
885 | 177k | } |
886 | 36.4k | } |
887 | | |
888 | 189k | return (ret); |
889 | 189k | } |
890 | | |
891 | | /* |
892 | | * Get an tile-organized image that has |
893 | | * SamplesPerPixel > 1 |
894 | | * PlanarConfiguration separated |
895 | | * We assume that all such images are RGB. |
896 | | */ |
897 | | static int gtTileSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t w, |
898 | | uint32_t h) |
899 | 4.35k | { |
900 | 4.35k | TIFF *tif = img->tif; |
901 | 4.35k | tileSeparateRoutine put = img->put.separate; |
902 | 4.35k | uint32_t col, row, y, rowstoread; |
903 | 4.35k | tmsize_t pos; |
904 | 4.35k | uint32_t tw, th; |
905 | 4.35k | unsigned char *buf = NULL; |
906 | 4.35k | unsigned char *p0 = NULL; |
907 | 4.35k | unsigned char *p1 = NULL; |
908 | 4.35k | unsigned char *p2 = NULL; |
909 | 4.35k | unsigned char *pa = NULL; |
910 | 4.35k | tmsize_t tilesize; |
911 | 4.35k | tmsize_t bufsize; |
912 | 4.35k | int32_t fromskew, toskew; |
913 | 4.35k | int alpha = img->alpha; |
914 | 4.35k | uint32_t nrow; |
915 | 4.35k | int ret = 1, flip; |
916 | 4.35k | uint16_t colorchannels; |
917 | 4.35k | uint32_t this_tw, tocol; |
918 | 4.35k | int32_t this_toskew, leftmost_toskew; |
919 | 4.35k | int32_t leftmost_fromskew; |
920 | 4.35k | uint32_t leftmost_tw; |
921 | | |
922 | | /* If the raster is smaller than the image, |
923 | | * or if there is a col_offset, adapt the samples to be copied per row. */ |
924 | 4.35k | uint32_t wmin; |
925 | 4.35k | if (0 <= img->col_offset && (uint32_t)img->col_offset < img->width) |
926 | 4.35k | { |
927 | 4.35k | wmin = TIFFmin(w, img->width - img->col_offset); |
928 | 4.35k | } |
929 | 0 | else |
930 | 0 | { |
931 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), |
932 | 0 | "Error in gtTileSeparate: column offset %d exceeds " |
933 | 0 | "image width %d", |
934 | 0 | img->col_offset, img->width); |
935 | 0 | return 0; |
936 | 0 | } |
937 | | |
938 | 4.35k | tilesize = TIFFTileSize(tif); |
939 | 4.35k | bufsize = |
940 | 4.35k | _TIFFMultiplySSize(tif, alpha ? 4 : 3, tilesize, "gtTileSeparate"); |
941 | 4.35k | if (bufsize == 0) |
942 | 0 | { |
943 | 0 | return (0); |
944 | 0 | } |
945 | | |
946 | 4.35k | TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw); |
947 | 4.35k | TIFFGetField(tif, TIFFTAG_TILELENGTH, &th); |
948 | | |
949 | 4.35k | flip = setorientation(img); |
950 | 4.35k | if (flip & FLIP_VERTICALLY) |
951 | 4.34k | { |
952 | 4.34k | if (((int64_t)tw + w) > INT_MAX) |
953 | 0 | { |
954 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), "%s", |
955 | 0 | "unsupported tile size (too wide)"); |
956 | 0 | return (0); |
957 | 0 | } |
958 | 4.34k | y = h - 1; |
959 | 4.34k | toskew = -(int32_t)(tw + w); |
960 | 4.34k | } |
961 | 4 | else |
962 | 4 | { |
963 | 4 | if (tw > ((int64_t)INT_MAX + w)) |
964 | 0 | { |
965 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), "%s", |
966 | 0 | "unsupported tile size (too wide)"); |
967 | 0 | return (0); |
968 | 0 | } |
969 | 4 | y = 0; |
970 | 4 | toskew = -(int32_t)(tw - w); |
971 | 4 | } |
972 | | |
973 | 4.35k | switch (img->photometric) |
974 | 4.35k | { |
975 | 826 | case PHOTOMETRIC_MINISWHITE: |
976 | 1.29k | case PHOTOMETRIC_MINISBLACK: |
977 | 1.29k | case PHOTOMETRIC_PALETTE: |
978 | 1.29k | colorchannels = 1; |
979 | 1.29k | break; |
980 | | |
981 | 3.05k | default: |
982 | 3.05k | colorchannels = 3; |
983 | 3.05k | break; |
984 | 4.35k | } |
985 | | |
986 | 4.35k | if (tw == 0 || th == 0) |
987 | 0 | { |
988 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero"); |
989 | 0 | return (0); |
990 | 0 | } |
991 | | |
992 | | /* |
993 | | * Leftmost tile is clipped on left side if col_offset > 0. |
994 | | */ |
995 | 4.35k | leftmost_fromskew = img->col_offset % tw; |
996 | 4.35k | leftmost_tw = tw - leftmost_fromskew; |
997 | 4.35k | int64_t skew_i64 = (int64_t)toskew + leftmost_fromskew; |
998 | 4.35k | if (skew_i64 > INT_MAX || skew_i64 < INT_MIN) |
999 | 0 | { |
1000 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), "%s %" PRId64, "Invalid skew", |
1001 | 0 | skew_i64); |
1002 | 0 | return (0); |
1003 | 0 | } |
1004 | 4.35k | leftmost_toskew = (int32_t)skew_i64; |
1005 | 8.70k | for (row = 0; ret != 0 && row < h; row += nrow) |
1006 | 4.35k | { |
1007 | 4.35k | rowstoread = th - (row + img->row_offset) % th; |
1008 | 4.35k | nrow = (row + rowstoread > h ? h - row : rowstoread); |
1009 | 4.35k | fromskew = leftmost_fromskew; |
1010 | 4.35k | this_tw = leftmost_tw; |
1011 | 4.35k | this_toskew = leftmost_toskew; |
1012 | 4.35k | tocol = 0; |
1013 | 4.35k | col = img->col_offset; |
1014 | | /* wmin: only write imagewidth if raster is bigger. */ |
1015 | 8.48k | while (tocol < wmin) |
1016 | 4.35k | { |
1017 | 4.35k | if (buf == NULL) |
1018 | 4.35k | { |
1019 | 4.35k | if (_TIFFReadTileAndAllocBuffer(tif, (void **)&buf, bufsize, |
1020 | 4.35k | col, row + img->row_offset, 0, |
1021 | 4.35k | 0) == (tmsize_t)(-1) && |
1022 | 4.35k | (buf == NULL || img->stoponerr)) |
1023 | 214 | { |
1024 | 214 | ret = 0; |
1025 | 214 | break; |
1026 | 214 | } |
1027 | 4.13k | p0 = buf; |
1028 | 4.13k | if (colorchannels == 1) |
1029 | 1.22k | { |
1030 | 1.22k | p2 = p1 = p0; |
1031 | 1.22k | pa = (alpha ? (p0 + 3 * tilesize) : NULL); |
1032 | 1.22k | } |
1033 | 2.91k | else |
1034 | 2.91k | { |
1035 | 2.91k | p1 = p0 + tilesize; |
1036 | 2.91k | p2 = p1 + tilesize; |
1037 | 2.91k | pa = (alpha ? (p2 + tilesize) : NULL); |
1038 | 2.91k | } |
1039 | 4.13k | } |
1040 | 0 | else if (TIFFReadTile(tif, p0, col, row + img->row_offset, 0, 0) == |
1041 | 0 | (tmsize_t)(-1) && |
1042 | 0 | img->stoponerr) |
1043 | 0 | { |
1044 | 0 | ret = 0; |
1045 | 0 | break; |
1046 | 0 | } |
1047 | 4.13k | if (colorchannels > 1 && |
1048 | 4.13k | TIFFReadTile(tif, p1, col, row + img->row_offset, 0, 1) == |
1049 | 2.91k | (tmsize_t)(-1) && |
1050 | 4.13k | img->stoponerr) |
1051 | 0 | { |
1052 | 0 | ret = 0; |
1053 | 0 | break; |
1054 | 0 | } |
1055 | 4.13k | if (colorchannels > 1 && |
1056 | 4.13k | TIFFReadTile(tif, p2, col, row + img->row_offset, 0, 2) == |
1057 | 2.91k | (tmsize_t)(-1) && |
1058 | 4.13k | img->stoponerr) |
1059 | 0 | { |
1060 | 0 | ret = 0; |
1061 | 0 | break; |
1062 | 0 | } |
1063 | 4.13k | if (alpha && |
1064 | 4.13k | TIFFReadTile(tif, pa, col, row + img->row_offset, 0, |
1065 | 3.98k | colorchannels) == (tmsize_t)(-1) && |
1066 | 4.13k | img->stoponerr) |
1067 | 0 | { |
1068 | 0 | ret = 0; |
1069 | 0 | break; |
1070 | 0 | } |
1071 | | |
1072 | | /* For SEPARATE the pos-offset is per sample and should not be |
1073 | | * multiplied by img->samplesperpixel. */ |
1074 | 4.13k | pos = ((row + img->row_offset) % th) * TIFFTileRowSize(tif) + |
1075 | 4.13k | (tmsize_t)fromskew; |
1076 | 4.13k | if (tocol + this_tw > wmin) |
1077 | 106 | { |
1078 | | /* |
1079 | | * Rightmost tile is clipped on right side. |
1080 | | */ |
1081 | 106 | fromskew = tw - (wmin - tocol); |
1082 | 106 | this_tw = tw - fromskew; |
1083 | 106 | this_toskew = toskew + fromskew; |
1084 | 106 | } |
1085 | 4.13k | tmsize_t roffset = (tmsize_t)y * w + tocol; |
1086 | 4.13k | (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew, |
1087 | 4.13k | this_toskew, p0 + pos, p1 + pos, p2 + pos, |
1088 | 4.13k | (alpha ? (pa + pos) : NULL)); |
1089 | 4.13k | tocol += this_tw; |
1090 | 4.13k | col += this_tw; |
1091 | | /* |
1092 | | * After the leftmost tile, tiles are no longer clipped on left |
1093 | | * side. |
1094 | | */ |
1095 | 4.13k | fromskew = 0; |
1096 | 4.13k | this_tw = tw; |
1097 | 4.13k | this_toskew = toskew; |
1098 | 4.13k | } |
1099 | | |
1100 | 4.35k | y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow); |
1101 | 4.35k | } |
1102 | | |
1103 | 4.35k | if (flip & FLIP_HORIZONTALLY) |
1104 | 43 | { |
1105 | 43 | uint32_t line; |
1106 | | |
1107 | 2.24k | for (line = 0; line < h; line++) |
1108 | 2.20k | { |
1109 | 2.20k | uint32_t *left = raster + (line * w); |
1110 | | /* Use wmin to only flip horizontally data in place and not complete |
1111 | | * raster-row. */ |
1112 | 2.20k | uint32_t *right = left + wmin - 1; |
1113 | | |
1114 | 262k | while (left < right) |
1115 | 260k | { |
1116 | 260k | uint32_t temp = *left; |
1117 | 260k | *left = *right; |
1118 | 260k | *right = temp; |
1119 | 260k | left++; |
1120 | 260k | right--; |
1121 | 260k | } |
1122 | 2.20k | } |
1123 | 43 | } |
1124 | | |
1125 | 4.35k | _TIFFfreeExt(img->tif, buf); |
1126 | 4.35k | return (ret); |
1127 | 4.35k | } |
1128 | | |
1129 | | /* |
1130 | | * Get a strip-organized image that has |
1131 | | * PlanarConfiguration contiguous if SamplesPerPixel > 1 |
1132 | | * or |
1133 | | * SamplesPerPixel == 1 |
1134 | | */ |
1135 | | static int gtStripContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t w, |
1136 | | uint32_t h) |
1137 | 71.4k | { |
1138 | 71.4k | TIFF *tif = img->tif; |
1139 | 71.4k | tileContigRoutine put = img->put.contig; |
1140 | 71.4k | uint32_t row, y, nrow, nrowsub, rowstoread; |
1141 | 71.4k | tmsize_t pos; |
1142 | 71.4k | unsigned char *buf = NULL; |
1143 | 71.4k | uint32_t rowsperstrip; |
1144 | 71.4k | uint16_t subsamplinghor, subsamplingver; |
1145 | 71.4k | uint32_t imagewidth = img->width; |
1146 | 71.4k | tmsize_t scanline; |
1147 | | /* fromskew, toskew are the increments within the input image or the raster |
1148 | | * from the end of a line to the start of the next line to read or write. */ |
1149 | 71.4k | int32_t fromskew, toskew; |
1150 | 71.4k | int ret = 1, flip; |
1151 | 71.4k | tmsize_t maxstripsize; |
1152 | | |
1153 | | /* If the raster is smaller than the image, |
1154 | | * or if there is a col_offset, adapt the samples to be copied per row. */ |
1155 | 71.4k | uint32_t wmin; |
1156 | 71.4k | if (0 <= img->col_offset && (uint32_t)img->col_offset < imagewidth) |
1157 | 71.4k | { |
1158 | 71.4k | wmin = TIFFmin(w, imagewidth - img->col_offset); |
1159 | 71.4k | } |
1160 | 0 | else |
1161 | 0 | { |
1162 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), |
1163 | 0 | "Error in gtStripContig: column offset %d exceeds " |
1164 | 0 | "image width %d", |
1165 | 0 | img->col_offset, imagewidth); |
1166 | 0 | return 0; |
1167 | 0 | } |
1168 | | |
1169 | 71.4k | TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, |
1170 | 71.4k | &subsamplingver); |
1171 | 71.4k | if (subsamplingver == 0) |
1172 | 0 | { |
1173 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), |
1174 | 0 | "Invalid vertical YCbCr subsampling"); |
1175 | 0 | return (0); |
1176 | 0 | } |
1177 | | |
1178 | 71.4k | maxstripsize = TIFFStripSize(tif); |
1179 | | |
1180 | 71.4k | flip = setorientation(img); |
1181 | 71.4k | if (flip & FLIP_VERTICALLY) |
1182 | 62.2k | { |
1183 | 62.2k | if (w > INT_MAX / 2) |
1184 | 0 | { |
1185 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow"); |
1186 | 0 | return (0); |
1187 | 0 | } |
1188 | 62.2k | y = h - 1; |
1189 | | /* Skew back to the raster row before the currently written row |
1190 | | * -> one raster width plus copied image pixels. */ |
1191 | 62.2k | toskew = -(int32_t)(w + wmin); |
1192 | 62.2k | } |
1193 | 9.23k | else |
1194 | 9.23k | { |
1195 | 9.23k | y = 0; |
1196 | | /* Skew forward to the end of the raster width of the row currently |
1197 | | * copied. */ |
1198 | 9.23k | toskew = w - wmin; |
1199 | 9.23k | } |
1200 | | |
1201 | 71.4k | TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); |
1202 | 71.4k | if (rowsperstrip == 0) |
1203 | 0 | { |
1204 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero"); |
1205 | 0 | return (0); |
1206 | 0 | } |
1207 | | |
1208 | 71.4k | scanline = TIFFScanlineSize(tif); |
1209 | 71.4k | fromskew = (w < imagewidth ? imagewidth - w : 0); |
1210 | 140k | for (row = 0; row < h; row += nrow) |
1211 | 71.4k | { |
1212 | 71.4k | uint32_t temp; |
1213 | 71.4k | rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip; |
1214 | 71.4k | nrow = (row + rowstoread > h ? h - row : rowstoread); |
1215 | 71.4k | nrowsub = nrow; |
1216 | 71.4k | if ((nrowsub % subsamplingver) != 0) |
1217 | 34.5k | nrowsub += subsamplingver - nrowsub % subsamplingver; |
1218 | 71.4k | temp = (row + img->row_offset) % rowsperstrip + nrowsub; |
1219 | 71.4k | if (scanline > 0 && temp > (size_t)(TIFF_TMSIZE_T_MAX / scanline)) |
1220 | 0 | { |
1221 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), |
1222 | 0 | "Integer overflow in gtStripContig"); |
1223 | 0 | return 0; |
1224 | 0 | } |
1225 | 71.4k | if (_TIFFReadEncodedStripAndAllocBuffer( |
1226 | 71.4k | tif, TIFFComputeStrip(tif, row + img->row_offset, 0), |
1227 | 71.4k | (void **)(&buf), maxstripsize, |
1228 | 71.4k | temp * scanline) == (tmsize_t)(-1) && |
1229 | 71.4k | (buf == NULL || img->stoponerr)) |
1230 | 2.27k | { |
1231 | 2.27k | ret = 0; |
1232 | 2.27k | break; |
1233 | 2.27k | } |
1234 | | |
1235 | 69.1k | pos = ((row + img->row_offset) % rowsperstrip) * scanline + |
1236 | 69.1k | ((tmsize_t)img->col_offset * img->samplesperpixel); |
1237 | 69.1k | tmsize_t roffset = (tmsize_t)y * w; |
1238 | 69.1k | (*put)(img, raster + roffset, 0, y, wmin, nrow, fromskew, toskew, |
1239 | 69.1k | buf + pos); |
1240 | 69.1k | y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow); |
1241 | 69.1k | } |
1242 | | |
1243 | 71.4k | if (flip & FLIP_HORIZONTALLY) |
1244 | 9.40k | { |
1245 | | /* Flips the complete raster matrix horizontally. If raster width is |
1246 | | * larger than image width, data are moved horizontally to the right |
1247 | | * side. |
1248 | | * Use wmin to only flip data in place. */ |
1249 | 9.40k | uint32_t line; |
1250 | | |
1251 | 30.8k | for (line = 0; line < h; line++) |
1252 | 21.4k | { |
1253 | 21.4k | uint32_t *left = raster + (line * w); |
1254 | | /* Use wmin to only flip horizontally data in place and not complete |
1255 | | * raster-row. */ |
1256 | 21.4k | uint32_t *right = left + wmin - 1; |
1257 | | |
1258 | 434k | while (left < right) |
1259 | 413k | { |
1260 | 413k | uint32_t temp = *left; |
1261 | 413k | *left = *right; |
1262 | 413k | *right = temp; |
1263 | 413k | left++; |
1264 | 413k | right--; |
1265 | 413k | } |
1266 | 21.4k | } |
1267 | 9.40k | } |
1268 | | |
1269 | 71.4k | _TIFFfreeExt(img->tif, buf); |
1270 | 71.4k | return (ret); |
1271 | 71.4k | } |
1272 | | |
1273 | | /* |
1274 | | * Get a strip-organized image with |
1275 | | * SamplesPerPixel > 1 |
1276 | | * PlanarConfiguration separated |
1277 | | * We assume that all such images are RGB. |
1278 | | */ |
1279 | | static int gtStripSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t w, |
1280 | | uint32_t h) |
1281 | 8.04k | { |
1282 | 8.04k | TIFF *tif = img->tif; |
1283 | 8.04k | tileSeparateRoutine put = img->put.separate; |
1284 | 8.04k | unsigned char *buf = NULL; |
1285 | 8.04k | unsigned char *p0 = NULL, *p1 = NULL, *p2 = NULL, *pa = NULL; |
1286 | 8.04k | uint32_t row, y, nrow, rowstoread; |
1287 | 8.04k | tmsize_t pos; |
1288 | 8.04k | tmsize_t scanline; |
1289 | 8.04k | uint32_t rowsperstrip, offset_row; |
1290 | 8.04k | uint32_t imagewidth = img->width; |
1291 | 8.04k | tmsize_t stripsize; |
1292 | 8.04k | tmsize_t bufsize; |
1293 | 8.04k | int32_t fromskew, toskew; |
1294 | 8.04k | int alpha = img->alpha; |
1295 | 8.04k | int ret = 1, flip; |
1296 | 8.04k | uint16_t colorchannels; |
1297 | | |
1298 | | /* If the raster is smaller than the image, |
1299 | | * or if there is a col_offset, adapt the samples to be copied per row. */ |
1300 | 8.04k | uint32_t wmin; |
1301 | 8.04k | if (0 <= img->col_offset && (uint32_t)img->col_offset < imagewidth) |
1302 | 8.04k | { |
1303 | 8.04k | wmin = TIFFmin(w, imagewidth - img->col_offset); |
1304 | 8.04k | } |
1305 | 0 | else |
1306 | 0 | { |
1307 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), |
1308 | 0 | "Error in gtStripSeparate: column offset %d exceeds " |
1309 | 0 | "image width %d", |
1310 | 0 | img->col_offset, imagewidth); |
1311 | 0 | return 0; |
1312 | 0 | } |
1313 | | |
1314 | 8.04k | stripsize = TIFFStripSize(tif); |
1315 | 8.04k | bufsize = |
1316 | 8.04k | _TIFFMultiplySSize(tif, alpha ? 4 : 3, stripsize, "gtStripSeparate"); |
1317 | 8.04k | if (bufsize == 0) |
1318 | 0 | { |
1319 | 0 | return (0); |
1320 | 0 | } |
1321 | | |
1322 | 8.04k | flip = setorientation(img); |
1323 | 8.04k | if (flip & FLIP_VERTICALLY) |
1324 | 8.01k | { |
1325 | 8.01k | if (w > INT_MAX / 2) |
1326 | 0 | { |
1327 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow"); |
1328 | 0 | return (0); |
1329 | 0 | } |
1330 | 8.01k | y = h - 1; |
1331 | | /* Skew back to the raster row before the currently written row |
1332 | | * -> one raster width plus one image width. */ |
1333 | 8.01k | toskew = -(int32_t)(w + wmin); |
1334 | 8.01k | } |
1335 | 29 | else |
1336 | 29 | { |
1337 | 29 | y = 0; |
1338 | | /* Skew forward to the end of the raster width of the row currently |
1339 | | * written. */ |
1340 | 29 | toskew = w - wmin; |
1341 | 29 | } |
1342 | | |
1343 | 8.04k | switch (img->photometric) |
1344 | 8.04k | { |
1345 | 740 | case PHOTOMETRIC_MINISWHITE: |
1346 | 1.38k | case PHOTOMETRIC_MINISBLACK: |
1347 | 1.38k | case PHOTOMETRIC_PALETTE: |
1348 | 1.38k | colorchannels = 1; |
1349 | 1.38k | break; |
1350 | | |
1351 | 6.65k | default: |
1352 | 6.65k | colorchannels = 3; |
1353 | 6.65k | break; |
1354 | 8.04k | } |
1355 | | |
1356 | 8.04k | TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); |
1357 | 8.04k | if (rowsperstrip == 0) |
1358 | 0 | { |
1359 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero"); |
1360 | 0 | return (0); |
1361 | 0 | } |
1362 | | |
1363 | 8.04k | scanline = TIFFScanlineSize(tif); |
1364 | 8.04k | fromskew = (w < imagewidth ? imagewidth - w : 0); |
1365 | 15.8k | for (row = 0; row < h; row += nrow) |
1366 | 8.04k | { |
1367 | 8.04k | uint32_t temp; |
1368 | 8.04k | rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip; |
1369 | 8.04k | nrow = (row + rowstoread > h ? h - row : rowstoread); |
1370 | 8.04k | offset_row = row + img->row_offset; |
1371 | 8.04k | temp = (row + img->row_offset) % rowsperstrip + nrow; |
1372 | 8.04k | if (scanline > 0 && temp > (size_t)(TIFF_TMSIZE_T_MAX / scanline)) |
1373 | 0 | { |
1374 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), |
1375 | 0 | "Integer overflow in gtStripSeparate"); |
1376 | 0 | return 0; |
1377 | 0 | } |
1378 | 8.04k | if (buf == NULL) |
1379 | 8.04k | { |
1380 | 8.04k | if (_TIFFReadEncodedStripAndAllocBuffer( |
1381 | 8.04k | tif, TIFFComputeStrip(tif, offset_row, 0), (void **)&buf, |
1382 | 8.04k | bufsize, temp * scanline) == (tmsize_t)(-1) && |
1383 | 8.04k | (buf == NULL || img->stoponerr)) |
1384 | 262 | { |
1385 | 262 | ret = 0; |
1386 | 262 | break; |
1387 | 262 | } |
1388 | 7.78k | p0 = buf; |
1389 | 7.78k | if (colorchannels == 1) |
1390 | 1.31k | { |
1391 | 1.31k | p2 = p1 = p0; |
1392 | 1.31k | pa = (alpha ? (p0 + 3 * stripsize) : NULL); |
1393 | 1.31k | } |
1394 | 6.46k | else |
1395 | 6.46k | { |
1396 | 6.46k | p1 = p0 + stripsize; |
1397 | 6.46k | p2 = p1 + stripsize; |
1398 | 6.46k | pa = (alpha ? (p2 + stripsize) : NULL); |
1399 | 6.46k | } |
1400 | 7.78k | } |
1401 | 0 | else if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0), |
1402 | 0 | p0, temp * scanline) == (tmsize_t)(-1) && |
1403 | 0 | img->stoponerr) |
1404 | 0 | { |
1405 | 0 | ret = 0; |
1406 | 0 | break; |
1407 | 0 | } |
1408 | 7.78k | if (colorchannels > 1 && |
1409 | 7.78k | TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1), p1, |
1410 | 6.46k | temp * scanline) == (tmsize_t)(-1) && |
1411 | 7.78k | img->stoponerr) |
1412 | 0 | { |
1413 | 0 | ret = 0; |
1414 | 0 | break; |
1415 | 0 | } |
1416 | 7.78k | if (colorchannels > 1 && |
1417 | 7.78k | TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2), p2, |
1418 | 6.46k | temp * scanline) == (tmsize_t)(-1) && |
1419 | 7.78k | img->stoponerr) |
1420 | 0 | { |
1421 | 0 | ret = 0; |
1422 | 0 | break; |
1423 | 0 | } |
1424 | 7.78k | if (alpha) |
1425 | 6.64k | { |
1426 | 6.64k | if (TIFFReadEncodedStrip( |
1427 | 6.64k | tif, TIFFComputeStrip(tif, offset_row, colorchannels), pa, |
1428 | 6.64k | temp * scanline) == (tmsize_t)(-1) && |
1429 | 6.64k | img->stoponerr) |
1430 | 0 | { |
1431 | 0 | ret = 0; |
1432 | 0 | break; |
1433 | 0 | } |
1434 | 6.64k | } |
1435 | | |
1436 | | /* For SEPARATE the pos-offset is per sample and should not be |
1437 | | * multiplied by img->samplesperpixel. */ |
1438 | 7.78k | pos = ((row + img->row_offset) % rowsperstrip) * scanline + |
1439 | 7.78k | (tmsize_t)img->col_offset; |
1440 | 7.78k | tmsize_t roffset = (tmsize_t)y * w; |
1441 | 7.78k | (*put)(img, raster + roffset, 0, y, wmin, nrow, fromskew, toskew, |
1442 | 7.78k | p0 + pos, p1 + pos, p2 + pos, (alpha ? (pa + pos) : NULL)); |
1443 | 7.78k | y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow); |
1444 | 7.78k | } |
1445 | | |
1446 | 8.04k | if (flip & FLIP_HORIZONTALLY) |
1447 | 72 | { |
1448 | 72 | uint32_t line; |
1449 | | |
1450 | 8.63k | for (line = 0; line < h; line++) |
1451 | 8.56k | { |
1452 | 8.56k | uint32_t *left = raster + (line * w); |
1453 | | /* Use wmin to only flip horizontally data in place and not complete |
1454 | | * raster-row. */ |
1455 | 8.56k | uint32_t *right = left + wmin - 1; |
1456 | | |
1457 | 1.29M | while (left < right) |
1458 | 1.28M | { |
1459 | 1.28M | uint32_t temp = *left; |
1460 | 1.28M | *left = *right; |
1461 | 1.28M | *right = temp; |
1462 | 1.28M | left++; |
1463 | 1.28M | right--; |
1464 | 1.28M | } |
1465 | 8.56k | } |
1466 | 72 | } |
1467 | | |
1468 | 8.04k | _TIFFfreeExt(img->tif, buf); |
1469 | 8.04k | return (ret); |
1470 | 8.04k | } |
1471 | | |
1472 | | /* |
1473 | | * The following routines move decoded data returned |
1474 | | * from the TIFF library into rasters filled with packed |
1475 | | * ABGR pixels (i.e. suitable for passing to lrecwrite.) |
1476 | | * |
1477 | | * The routines have been created according to the most |
1478 | | * important cases and optimized. PickContigCase and |
1479 | | * PickSeparateCase analyze the parameters and select |
1480 | | * the appropriate "get" and "put" routine to use. |
1481 | | */ |
1482 | | #define REPEAT8(op) \ |
1483 | 32.2M | REPEAT4(op); \ |
1484 | 32.2M | REPEAT4(op) |
1485 | | #define REPEAT4(op) \ |
1486 | 64.8M | REPEAT2(op); \ |
1487 | 64.8M | REPEAT2(op) |
1488 | | #define REPEAT2(op) \ |
1489 | 130M | op; \ |
1490 | 130M | op |
1491 | | #define CASE8(x, op) \ |
1492 | 459k | switch (x) \ |
1493 | 459k | { \ |
1494 | 23.7k | case 7: \ |
1495 | 23.7k | op; /*-fallthrough*/ \ |
1496 | 85.9k | case 6: \ |
1497 | 85.9k | op; /*-fallthrough*/ \ |
1498 | 110k | case 5: \ |
1499 | 110k | op; /*-fallthrough*/ \ |
1500 | 148k | case 4: \ |
1501 | 148k | op; /*-fallthrough*/ \ |
1502 | 260k | case 3: \ |
1503 | 260k | op; /*-fallthrough*/ \ |
1504 | 404k | case 2: \ |
1505 | 404k | op; /*-fallthrough*/ \ |
1506 | 459k | case 1: \ |
1507 | 459k | op; \ |
1508 | 459k | } |
1509 | | #define CASE4(x, op) \ |
1510 | 1.00M | switch (x) \ |
1511 | 1.00M | { \ |
1512 | 8.31k | case 3: \ |
1513 | 8.31k | op; /*-fallthrough*/ \ |
1514 | 16.4k | case 2: \ |
1515 | 16.4k | op; /*-fallthrough*/ \ |
1516 | 1.00M | case 1: \ |
1517 | 1.00M | op; \ |
1518 | 1.00M | } |
1519 | | #define NOP |
1520 | | |
1521 | | #define UNROLL8(w, op1, op2) \ |
1522 | 1.06M | { \ |
1523 | 1.06M | uint32_t _x; \ |
1524 | 33.3M | for (_x = w; _x >= 8; _x -= 8) \ |
1525 | 32.2M | { \ |
1526 | 32.2M | op1; \ |
1527 | 32.2M | REPEAT8(op2); \ |
1528 | 32.2M | } \ |
1529 | 1.06M | if (_x > 0) \ |
1530 | 1.06M | { \ |
1531 | 459k | op1; \ |
1532 | 459k | CASE8(_x, op2); \ |
1533 | 459k | } \ |
1534 | 1.06M | } |
1535 | | #define UNROLL4(w, op1, op2) \ |
1536 | 1.01M | { \ |
1537 | 1.01M | uint32_t _x; \ |
1538 | 1.27M | for (_x = w; _x >= 4; _x -= 4) \ |
1539 | 1.01M | { \ |
1540 | 262k | op1; \ |
1541 | 262k | REPEAT4(op2); \ |
1542 | 262k | } \ |
1543 | 1.01M | if (_x > 0) \ |
1544 | 1.01M | { \ |
1545 | 1.00M | op1; \ |
1546 | 1.00M | CASE4(_x, op2); \ |
1547 | 1.00M | } \ |
1548 | 1.01M | } |
1549 | | #define UNROLL2(w, op1, op2) \ |
1550 | 12.1k | { \ |
1551 | 12.1k | uint32_t _x; \ |
1552 | 528k | for (_x = w; _x >= 2; _x -= 2) \ |
1553 | 516k | { \ |
1554 | 516k | op1; \ |
1555 | 516k | REPEAT2(op2); \ |
1556 | 516k | } \ |
1557 | 12.1k | if (_x) \ |
1558 | 12.1k | { \ |
1559 | 5.15k | op1; \ |
1560 | 5.15k | op2; \ |
1561 | 5.15k | } \ |
1562 | 12.1k | } |
1563 | | |
1564 | | #define SKEW(r, g, b, skew) \ |
1565 | 19.6k | { \ |
1566 | 19.6k | r += skew; \ |
1567 | 19.6k | g += skew; \ |
1568 | 19.6k | b += skew; \ |
1569 | 19.6k | } |
1570 | | #define SKEW4(r, g, b, a, skew) \ |
1571 | 724k | { \ |
1572 | 724k | r += skew; \ |
1573 | 724k | g += skew; \ |
1574 | 724k | b += skew; \ |
1575 | 724k | a += skew; \ |
1576 | 724k | } |
1577 | | |
1578 | 157M | #define A1 (((uint32_t)0xffL) << 24) |
1579 | | #define PACK(r, g, b) \ |
1580 | 156M | ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) | A1) |
1581 | | #define PACK4(r, g, b, a) \ |
1582 | 510k | ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) | \ |
1583 | 510k | ((uint32_t)(a) << 24)) |
1584 | | #define W2B(v) (((v) >> 8) & 0xff) |
1585 | | /* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */ |
1586 | | #define PACKW(r, g, b) \ |
1587 | | ((uint32_t)W2B(r) | ((uint32_t)W2B(g) << 8) | ((uint32_t)W2B(b) << 16) | A1) |
1588 | | #define PACKW4(r, g, b, a) \ |
1589 | | ((uint32_t)W2B(r) | ((uint32_t)W2B(g) << 8) | ((uint32_t)W2B(b) << 16) | \ |
1590 | | ((uint32_t)W2B(a) << 24)) |
1591 | | |
1592 | | #define DECLAREContigPutFunc(name) \ |
1593 | | static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \ |
1594 | | uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \ |
1595 | | unsigned char *pp) |
1596 | | |
1597 | | /* |
1598 | | * 8-bit palette => colormap/RGB |
1599 | | */ |
1600 | | DECLAREContigPutFunc(put8bitcmaptile) |
1601 | 24.8k | { |
1602 | 24.8k | uint32_t **PALmap = img->PALmap; |
1603 | 24.8k | int samplesperpixel = img->samplesperpixel; |
1604 | | |
1605 | 24.8k | (void)y; |
1606 | 225k | for (; h > 0; --h) |
1607 | 200k | { |
1608 | 152M | for (x = w; x > 0; --x) |
1609 | 152M | { |
1610 | 152M | *cp++ = PALmap[*pp][0]; |
1611 | 152M | pp += samplesperpixel; |
1612 | 152M | } |
1613 | 200k | cp += toskew; |
1614 | 200k | pp += fromskew; |
1615 | 200k | } |
1616 | 24.8k | } |
1617 | | |
1618 | | /* |
1619 | | * 4-bit palette => colormap/RGB |
1620 | | */ |
1621 | | DECLAREContigPutFunc(put4bitcmaptile) |
1622 | 12 | { |
1623 | 12 | uint32_t **PALmap = img->PALmap; |
1624 | | |
1625 | 12 | (void)x; |
1626 | 12 | (void)y; |
1627 | 12 | fromskew /= 2; |
1628 | 123 | for (; h > 0; --h) |
1629 | 111 | { |
1630 | 111 | uint32_t *bw; |
1631 | 111 | UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++); |
1632 | 111 | cp += toskew; |
1633 | 111 | pp += fromskew; |
1634 | 111 | } |
1635 | 12 | } |
1636 | | |
1637 | | /* |
1638 | | * 2-bit palette => colormap/RGB |
1639 | | */ |
1640 | | DECLAREContigPutFunc(put2bitcmaptile) |
1641 | 1.40k | { |
1642 | 1.40k | uint32_t **PALmap = img->PALmap; |
1643 | | |
1644 | 1.40k | (void)x; |
1645 | 1.40k | (void)y; |
1646 | 1.40k | fromskew /= 4; |
1647 | 11.1k | for (; h > 0; --h) |
1648 | 9.75k | { |
1649 | 9.75k | uint32_t *bw; |
1650 | 9.75k | UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++); |
1651 | 9.75k | cp += toskew; |
1652 | 9.75k | pp += fromskew; |
1653 | 9.75k | } |
1654 | 1.40k | } |
1655 | | |
1656 | | /* |
1657 | | * 1-bit palette => colormap/RGB |
1658 | | */ |
1659 | | DECLAREContigPutFunc(put1bitcmaptile) |
1660 | 0 | { |
1661 | 0 | uint32_t **PALmap = img->PALmap; |
1662 | |
|
1663 | 0 | (void)x; |
1664 | 0 | (void)y; |
1665 | 0 | fromskew /= 8; |
1666 | 0 | for (; h > 0; --h) |
1667 | 0 | { |
1668 | 0 | uint32_t *bw; |
1669 | 0 | UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++); |
1670 | 0 | cp += toskew; |
1671 | 0 | pp += fromskew; |
1672 | 0 | } |
1673 | 0 | } |
1674 | | |
1675 | | /* |
1676 | | * 8-bit greyscale => colormap/RGB |
1677 | | */ |
1678 | | DECLAREContigPutFunc(putgreytile) |
1679 | 16.5k | { |
1680 | 16.5k | int samplesperpixel = img->samplesperpixel; |
1681 | 16.5k | uint32_t **BWmap = img->BWmap; |
1682 | | |
1683 | 16.5k | (void)y; |
1684 | 448k | for (; h > 0; --h) |
1685 | 431k | { |
1686 | 15.1M | for (x = w; x > 0; --x) |
1687 | 14.7M | { |
1688 | 14.7M | *cp++ = BWmap[*pp][0]; |
1689 | 14.7M | pp += samplesperpixel; |
1690 | 14.7M | } |
1691 | 431k | cp += toskew; |
1692 | 431k | pp += fromskew; |
1693 | 431k | } |
1694 | 16.5k | } |
1695 | | |
1696 | | /* |
1697 | | * 8-bit greyscale with associated alpha => colormap/RGBA |
1698 | | */ |
1699 | | DECLAREContigPutFunc(putagreytile) |
1700 | 213 | { |
1701 | 213 | int samplesperpixel = img->samplesperpixel; |
1702 | 213 | uint32_t **BWmap = img->BWmap; |
1703 | | |
1704 | 213 | (void)y; |
1705 | 1.83k | for (; h > 0; --h) |
1706 | 1.62k | { |
1707 | 527k | for (x = w; x > 0; --x) |
1708 | 526k | { |
1709 | 526k | *cp++ = BWmap[*pp][0] & ((uint32_t) * (pp + 1) << 24 | ~A1); |
1710 | 526k | pp += samplesperpixel; |
1711 | 526k | } |
1712 | 1.62k | cp += toskew; |
1713 | 1.62k | pp += fromskew; |
1714 | 1.62k | } |
1715 | 213 | } |
1716 | | |
1717 | | /* |
1718 | | * 16-bit greyscale => colormap/RGB |
1719 | | */ |
1720 | | DECLAREContigPutFunc(put16bitbwtile) |
1721 | 6.01k | { |
1722 | 6.01k | int samplesperpixel = img->samplesperpixel; |
1723 | 6.01k | uint32_t **BWmap = img->BWmap; |
1724 | | |
1725 | 6.01k | (void)y; |
1726 | 21.1k | for (; h > 0; --h) |
1727 | 15.1k | { |
1728 | 15.1k | uint16_t *wp = (uint16_t *)pp; |
1729 | | |
1730 | 509k | for (x = w; x > 0; --x) |
1731 | 494k | { |
1732 | | /* use high order byte of 16bit value */ |
1733 | | |
1734 | 494k | *cp++ = BWmap[*wp >> 8][0]; |
1735 | 494k | pp += 2 * samplesperpixel; |
1736 | 494k | wp += samplesperpixel; |
1737 | 494k | } |
1738 | 15.1k | cp += toskew; |
1739 | 15.1k | pp += fromskew; |
1740 | 15.1k | } |
1741 | 6.01k | } |
1742 | | |
1743 | | /* |
1744 | | * 1-bit bilevel => colormap/RGB |
1745 | | */ |
1746 | | DECLAREContigPutFunc(put1bitbwtile) |
1747 | 44.3k | { |
1748 | 44.3k | uint32_t **BWmap = img->BWmap; |
1749 | | |
1750 | 44.3k | (void)x; |
1751 | 44.3k | (void)y; |
1752 | 44.3k | fromskew /= 8; |
1753 | 291k | for (; h > 0; --h) |
1754 | 247k | { |
1755 | 247k | uint32_t *bw; |
1756 | 247k | UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++); |
1757 | 247k | cp += toskew; |
1758 | 247k | pp += fromskew; |
1759 | 247k | } |
1760 | 44.3k | } |
1761 | | |
1762 | | /* |
1763 | | * 2-bit greyscale => colormap/RGB |
1764 | | */ |
1765 | | DECLAREContigPutFunc(put2bitbwtile) |
1766 | 27.3k | { |
1767 | 27.3k | uint32_t **BWmap = img->BWmap; |
1768 | | |
1769 | 27.3k | (void)x; |
1770 | 27.3k | (void)y; |
1771 | 27.3k | fromskew /= 4; |
1772 | 1.03M | for (; h > 0; --h) |
1773 | 1.00M | { |
1774 | 1.00M | uint32_t *bw; |
1775 | 1.00M | UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++); |
1776 | 1.00M | cp += toskew; |
1777 | 1.00M | pp += fromskew; |
1778 | 1.00M | } |
1779 | 27.3k | } |
1780 | | |
1781 | | /* |
1782 | | * 4-bit greyscale => colormap/RGB |
1783 | | */ |
1784 | | DECLAREContigPutFunc(put4bitbwtile) |
1785 | 1.91k | { |
1786 | 1.91k | uint32_t **BWmap = img->BWmap; |
1787 | | |
1788 | 1.91k | (void)x; |
1789 | 1.91k | (void)y; |
1790 | 1.91k | fromskew /= 2; |
1791 | 13.9k | for (; h > 0; --h) |
1792 | 12.0k | { |
1793 | 12.0k | uint32_t *bw; |
1794 | 12.0k | UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++); |
1795 | 12.0k | cp += toskew; |
1796 | 12.0k | pp += fromskew; |
1797 | 12.0k | } |
1798 | 1.91k | } |
1799 | | |
1800 | | /* |
1801 | | * 8-bit packed samples, no Map => RGB |
1802 | | */ |
1803 | | DECLAREContigPutFunc(putRGBcontig8bittile) |
1804 | 3.74k | { |
1805 | 3.74k | int samplesperpixel = img->samplesperpixel; |
1806 | | |
1807 | 3.74k | (void)x; |
1808 | 3.74k | (void)y; |
1809 | 3.74k | fromskew *= samplesperpixel; |
1810 | 19.4k | for (; h > 0; --h) |
1811 | 15.6k | { |
1812 | 15.6k | UNROLL8(w, NOP, *cp++ = PACK(pp[0], pp[1], pp[2]); |
1813 | 15.6k | pp += samplesperpixel); |
1814 | 15.6k | cp += toskew; |
1815 | 15.6k | pp += fromskew; |
1816 | 15.6k | } |
1817 | 3.74k | } |
1818 | | |
1819 | | /* |
1820 | | * 8-bit packed samples => RGBA w/ associated alpha |
1821 | | * (known to have Map == NULL) |
1822 | | */ |
1823 | | DECLAREContigPutFunc(putRGBAAcontig8bittile) |
1824 | 2.99k | { |
1825 | 2.99k | int samplesperpixel = img->samplesperpixel; |
1826 | | |
1827 | 2.99k | (void)x; |
1828 | 2.99k | (void)y; |
1829 | 2.99k | fromskew *= samplesperpixel; |
1830 | 71.9k | for (; h > 0; --h) |
1831 | 69.0k | { |
1832 | 69.0k | UNROLL8(w, NOP, *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]); |
1833 | 69.0k | pp += samplesperpixel); |
1834 | 69.0k | cp += toskew; |
1835 | 69.0k | pp += fromskew; |
1836 | 69.0k | } |
1837 | 2.99k | } |
1838 | | |
1839 | | /* |
1840 | | * 8-bit packed samples => RGBA w/ unassociated alpha |
1841 | | * (known to have Map == NULL) |
1842 | | */ |
1843 | | DECLAREContigPutFunc(putRGBUAcontig8bittile) |
1844 | 434 | { |
1845 | 434 | int samplesperpixel = img->samplesperpixel; |
1846 | 434 | (void)y; |
1847 | 434 | fromskew *= samplesperpixel; |
1848 | 1.45k | for (; h > 0; --h) |
1849 | 1.01k | { |
1850 | 1.01k | uint32_t r, g, b, a; |
1851 | 1.01k | uint8_t *m; |
1852 | 33.6k | for (x = w; x > 0; --x) |
1853 | 32.6k | { |
1854 | 32.6k | a = pp[3]; |
1855 | 32.6k | m = img->UaToAa + ((size_t)a << 8); |
1856 | 32.6k | r = m[pp[0]]; |
1857 | 32.6k | g = m[pp[1]]; |
1858 | 32.6k | b = m[pp[2]]; |
1859 | 32.6k | *cp++ = PACK4(r, g, b, a); |
1860 | 32.6k | pp += samplesperpixel; |
1861 | 32.6k | } |
1862 | 1.01k | cp += toskew; |
1863 | 1.01k | pp += fromskew; |
1864 | 1.01k | } |
1865 | 434 | } |
1866 | | |
1867 | | /* |
1868 | | * 16-bit packed samples => RGB |
1869 | | */ |
1870 | | DECLAREContigPutFunc(putRGBcontig16bittile) |
1871 | 20.7k | { |
1872 | 20.7k | int samplesperpixel = img->samplesperpixel; |
1873 | 20.7k | uint16_t *wp = (uint16_t *)pp; |
1874 | 20.7k | (void)y; |
1875 | 20.7k | fromskew *= samplesperpixel; |
1876 | 61.8k | for (; h > 0; --h) |
1877 | 41.1k | { |
1878 | 210k | for (x = w; x > 0; --x) |
1879 | 169k | { |
1880 | 169k | *cp++ = PACK(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]], |
1881 | 169k | img->Bitdepth16To8[wp[2]]); |
1882 | 169k | wp += samplesperpixel; |
1883 | 169k | } |
1884 | 41.1k | cp += toskew; |
1885 | 41.1k | wp += fromskew; |
1886 | 41.1k | } |
1887 | 20.7k | } |
1888 | | |
1889 | | /* |
1890 | | * 16-bit packed samples => RGBA w/ associated alpha |
1891 | | * (known to have Map == NULL) |
1892 | | */ |
1893 | | DECLAREContigPutFunc(putRGBAAcontig16bittile) |
1894 | 1.06k | { |
1895 | 1.06k | int samplesperpixel = img->samplesperpixel; |
1896 | 1.06k | uint16_t *wp = (uint16_t *)pp; |
1897 | 1.06k | (void)y; |
1898 | 1.06k | fromskew *= samplesperpixel; |
1899 | 3.43k | for (; h > 0; --h) |
1900 | 2.37k | { |
1901 | 77.1k | for (x = w; x > 0; --x) |
1902 | 74.7k | { |
1903 | 74.7k | *cp++ = PACK4(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]], |
1904 | 74.7k | img->Bitdepth16To8[wp[2]], img->Bitdepth16To8[wp[3]]); |
1905 | 74.7k | wp += samplesperpixel; |
1906 | 74.7k | } |
1907 | 2.37k | cp += toskew; |
1908 | 2.37k | wp += fromskew; |
1909 | 2.37k | } |
1910 | 1.06k | } |
1911 | | |
1912 | | /* |
1913 | | * 16-bit packed samples => RGBA w/ unassociated alpha |
1914 | | * (known to have Map == NULL) |
1915 | | */ |
1916 | | DECLAREContigPutFunc(putRGBUAcontig16bittile) |
1917 | 711 | { |
1918 | 711 | int samplesperpixel = img->samplesperpixel; |
1919 | 711 | uint16_t *wp = (uint16_t *)pp; |
1920 | 711 | (void)y; |
1921 | 711 | fromskew *= samplesperpixel; |
1922 | 4.80k | for (; h > 0; --h) |
1923 | 4.09k | { |
1924 | 4.09k | uint32_t r, g, b, a; |
1925 | 4.09k | uint8_t *m; |
1926 | 25.4k | for (x = w; x > 0; --x) |
1927 | 21.3k | { |
1928 | 21.3k | a = img->Bitdepth16To8[wp[3]]; |
1929 | 21.3k | m = img->UaToAa + ((size_t)a << 8); |
1930 | 21.3k | r = m[img->Bitdepth16To8[wp[0]]]; |
1931 | 21.3k | g = m[img->Bitdepth16To8[wp[1]]]; |
1932 | 21.3k | b = m[img->Bitdepth16To8[wp[2]]]; |
1933 | 21.3k | *cp++ = PACK4(r, g, b, a); |
1934 | 21.3k | wp += samplesperpixel; |
1935 | 21.3k | } |
1936 | 4.09k | cp += toskew; |
1937 | 4.09k | wp += fromskew; |
1938 | 4.09k | } |
1939 | 711 | } |
1940 | | |
1941 | | /* |
1942 | | * 8-bit packed CMYK samples w/o Map => RGB |
1943 | | * |
1944 | | * NB: The conversion of CMYK->RGB is *very* crude. |
1945 | | */ |
1946 | | DECLAREContigPutFunc(putRGBcontig8bitCMYKtile) |
1947 | 4.48k | { |
1948 | 4.48k | int samplesperpixel = img->samplesperpixel; |
1949 | 4.48k | uint16_t r, g, b, k; |
1950 | | |
1951 | 4.48k | (void)x; |
1952 | 4.48k | (void)y; |
1953 | 4.48k | fromskew *= samplesperpixel; |
1954 | 15.9k | for (; h > 0; --h) |
1955 | 11.4k | { |
1956 | 11.4k | UNROLL8(w, NOP, k = 255 - pp[3]; r = (k * (255 - pp[0])) / 255; |
1957 | 11.4k | g = (k * (255 - pp[1])) / 255; b = (k * (255 - pp[2])) / 255; |
1958 | 11.4k | *cp++ = PACK(r, g, b); pp += samplesperpixel); |
1959 | 11.4k | cp += toskew; |
1960 | 11.4k | pp += fromskew; |
1961 | 11.4k | } |
1962 | 4.48k | } |
1963 | | |
1964 | | /* |
1965 | | * 8-bit packed CMYK samples w/Map => RGB |
1966 | | * |
1967 | | * NB: The conversion of CMYK->RGB is *very* crude. |
1968 | | */ |
1969 | | DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile) |
1970 | 0 | { |
1971 | 0 | int samplesperpixel = img->samplesperpixel; |
1972 | 0 | TIFFRGBValue *Map = img->Map; |
1973 | 0 | uint16_t r, g, b, k; |
1974 | |
|
1975 | 0 | (void)y; |
1976 | 0 | fromskew *= samplesperpixel; |
1977 | 0 | for (; h > 0; --h) |
1978 | 0 | { |
1979 | 0 | for (x = w; x > 0; --x) |
1980 | 0 | { |
1981 | 0 | k = 255 - pp[3]; |
1982 | 0 | r = (k * (255 - pp[0])) / 255; |
1983 | 0 | g = (k * (255 - pp[1])) / 255; |
1984 | 0 | b = (k * (255 - pp[2])) / 255; |
1985 | 0 | *cp++ = PACK(Map[r], Map[g], Map[b]); |
1986 | 0 | pp += samplesperpixel; |
1987 | 0 | } |
1988 | 0 | pp += fromskew; |
1989 | 0 | cp += toskew; |
1990 | 0 | } |
1991 | 0 | } |
1992 | | |
1993 | | #define DECLARESepPutFunc(name) \ |
1994 | | static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \ |
1995 | | uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \ |
1996 | | unsigned char *r, unsigned char *g, unsigned char *b, \ |
1997 | | unsigned char *a) |
1998 | | |
1999 | | /* |
2000 | | * 8-bit unpacked samples => RGB |
2001 | | */ |
2002 | | DECLARESepPutFunc(putRGBseparate8bittile) |
2003 | 285 | { |
2004 | 285 | (void)img; |
2005 | 285 | (void)x; |
2006 | 285 | (void)y; |
2007 | 285 | (void)a; |
2008 | 11.7k | for (; h > 0; --h) |
2009 | 11.5k | { |
2010 | 11.5k | UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++)); |
2011 | 11.5k | SKEW(r, g, b, fromskew); |
2012 | 11.5k | cp += toskew; |
2013 | 11.5k | } |
2014 | 285 | } |
2015 | | |
2016 | | /* |
2017 | | * 8-bit unpacked samples => RGBA w/ associated alpha |
2018 | | */ |
2019 | | DECLARESepPutFunc(putRGBAAseparate8bittile) |
2020 | 6.38k | { |
2021 | 6.38k | (void)img; |
2022 | 6.38k | (void)x; |
2023 | 6.38k | (void)y; |
2024 | 719k | for (; h > 0; --h) |
2025 | 713k | { |
2026 | 713k | UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++)); |
2027 | 713k | SKEW4(r, g, b, a, fromskew); |
2028 | 713k | cp += toskew; |
2029 | 713k | } |
2030 | 6.38k | } |
2031 | | |
2032 | | /* |
2033 | | * 8-bit unpacked CMYK samples => RGBA |
2034 | | */ |
2035 | | DECLARESepPutFunc(putCMYKseparate8bittile) |
2036 | 4.01k | { |
2037 | 4.01k | (void)img; |
2038 | 4.01k | (void)y; |
2039 | 11.9k | for (; h > 0; --h) |
2040 | 7.98k | { |
2041 | 7.98k | uint32_t rv, gv, bv, kv; |
2042 | 203k | for (x = w; x > 0; --x) |
2043 | 195k | { |
2044 | 195k | kv = 255 - *a++; |
2045 | 195k | rv = (kv * (255 - *r++)) / 255; |
2046 | 195k | gv = (kv * (255 - *g++)) / 255; |
2047 | 195k | bv = (kv * (255 - *b++)) / 255; |
2048 | 195k | *cp++ = PACK4(rv, gv, bv, 255); |
2049 | 195k | } |
2050 | 7.98k | SKEW4(r, g, b, a, fromskew); |
2051 | 7.98k | cp += toskew; |
2052 | 7.98k | } |
2053 | 4.01k | } |
2054 | | |
2055 | | /* |
2056 | | * 8-bit unpacked samples => RGBA w/ unassociated alpha |
2057 | | */ |
2058 | | DECLARESepPutFunc(putRGBUAseparate8bittile) |
2059 | 11 | { |
2060 | 11 | (void)img; |
2061 | 11 | (void)y; |
2062 | 413 | for (; h > 0; --h) |
2063 | 402 | { |
2064 | 402 | uint32_t rv, gv, bv, av; |
2065 | 402 | uint8_t *m; |
2066 | 7.37k | for (x = w; x > 0; --x) |
2067 | 6.97k | { |
2068 | 6.97k | av = *a++; |
2069 | 6.97k | m = img->UaToAa + ((size_t)av << 8); |
2070 | 6.97k | rv = m[*r++]; |
2071 | 6.97k | gv = m[*g++]; |
2072 | 6.97k | bv = m[*b++]; |
2073 | 6.97k | *cp++ = PACK4(rv, gv, bv, av); |
2074 | 6.97k | } |
2075 | 402 | SKEW4(r, g, b, a, fromskew); |
2076 | 402 | cp += toskew; |
2077 | 402 | } |
2078 | 11 | } |
2079 | | |
2080 | | /* |
2081 | | * 16-bit unpacked samples => RGB |
2082 | | */ |
2083 | | DECLARESepPutFunc(putRGBseparate16bittile) |
2084 | 43 | { |
2085 | 43 | uint16_t *wr = (uint16_t *)r; |
2086 | 43 | uint16_t *wg = (uint16_t *)g; |
2087 | 43 | uint16_t *wb = (uint16_t *)b; |
2088 | 43 | (void)img; |
2089 | 43 | (void)y; |
2090 | 43 | (void)a; |
2091 | 1.46k | for (; h > 0; --h) |
2092 | 1.41k | { |
2093 | 103k | for (x = 0; x < w; x++) |
2094 | 101k | *cp++ = PACK(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++], |
2095 | 1.41k | img->Bitdepth16To8[*wb++]); |
2096 | 1.41k | SKEW(wr, wg, wb, fromskew); |
2097 | 1.41k | cp += toskew; |
2098 | 1.41k | } |
2099 | 43 | } |
2100 | | |
2101 | | /* |
2102 | | * 16-bit unpacked samples => RGBA w/ associated alpha |
2103 | | */ |
2104 | | DECLARESepPutFunc(putRGBAAseparate16bittile) |
2105 | 35 | { |
2106 | 35 | uint16_t *wr = (uint16_t *)r; |
2107 | 35 | uint16_t *wg = (uint16_t *)g; |
2108 | 35 | uint16_t *wb = (uint16_t *)b; |
2109 | 35 | uint16_t *wa = (uint16_t *)a; |
2110 | 35 | (void)img; |
2111 | 35 | (void)y; |
2112 | 1.95k | for (; h > 0; --h) |
2113 | 1.92k | { |
2114 | 178k | for (x = 0; x < w; x++) |
2115 | 176k | *cp++ = PACK4(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++], |
2116 | 1.92k | img->Bitdepth16To8[*wb++], img->Bitdepth16To8[*wa++]); |
2117 | 1.92k | SKEW4(wr, wg, wb, wa, fromskew); |
2118 | 1.92k | cp += toskew; |
2119 | 1.92k | } |
2120 | 35 | } |
2121 | | |
2122 | | /* |
2123 | | * 16-bit unpacked samples => RGBA w/ unassociated alpha |
2124 | | */ |
2125 | | DECLARESepPutFunc(putRGBUAseparate16bittile) |
2126 | 18 | { |
2127 | 18 | uint16_t *wr = (uint16_t *)r; |
2128 | 18 | uint16_t *wg = (uint16_t *)g; |
2129 | 18 | uint16_t *wb = (uint16_t *)b; |
2130 | 18 | uint16_t *wa = (uint16_t *)a; |
2131 | 18 | (void)img; |
2132 | 18 | (void)y; |
2133 | 995 | for (; h > 0; --h) |
2134 | 977 | { |
2135 | 977 | uint32_t r2, g2, b2, a2; |
2136 | 977 | uint8_t *m; |
2137 | 4.54k | for (x = w; x > 0; --x) |
2138 | 3.56k | { |
2139 | 3.56k | a2 = img->Bitdepth16To8[*wa++]; |
2140 | 3.56k | m = img->UaToAa + ((size_t)a2 << 8); |
2141 | 3.56k | r2 = m[img->Bitdepth16To8[*wr++]]; |
2142 | 3.56k | g2 = m[img->Bitdepth16To8[*wg++]]; |
2143 | 3.56k | b2 = m[img->Bitdepth16To8[*wb++]]; |
2144 | 3.56k | *cp++ = PACK4(r2, g2, b2, a2); |
2145 | 3.56k | } |
2146 | 977 | SKEW4(wr, wg, wb, wa, fromskew); |
2147 | 977 | cp += toskew; |
2148 | 977 | } |
2149 | 18 | } |
2150 | | |
2151 | | /* |
2152 | | * 8-bit packed CIE L*a*b 1976 samples => RGB |
2153 | | */ |
2154 | | DECLAREContigPutFunc(putcontig8bitCIELab8) |
2155 | 16.7k | { |
2156 | 16.7k | float X, Y, Z; |
2157 | 16.7k | uint32_t r, g, b; |
2158 | 16.7k | (void)y; |
2159 | 16.7k | fromskew *= 3; |
2160 | 39.6k | for (; h > 0; --h) |
2161 | 22.8k | { |
2162 | 472k | for (x = w; x > 0; --x) |
2163 | 449k | { |
2164 | 449k | TIFFCIELabToXYZ(img->cielab, (unsigned char)pp[0], |
2165 | 449k | (signed char)pp[1], (signed char)pp[2], &X, &Y, &Z); |
2166 | 449k | TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b); |
2167 | 449k | *cp++ = PACK(r, g, b); |
2168 | 449k | pp += 3; |
2169 | 449k | } |
2170 | 22.8k | cp += toskew; |
2171 | 22.8k | pp += fromskew; |
2172 | 22.8k | } |
2173 | 16.7k | } |
2174 | | |
2175 | | /* |
2176 | | * 16-bit packed CIE L*a*b 1976 samples => RGB |
2177 | | */ |
2178 | | DECLAREContigPutFunc(putcontig8bitCIELab16) |
2179 | 1.42k | { |
2180 | 1.42k | float X, Y, Z; |
2181 | 1.42k | uint32_t r, g, b; |
2182 | 1.42k | uint16_t *wp = (uint16_t *)pp; |
2183 | 1.42k | (void)y; |
2184 | 1.42k | fromskew *= 3; |
2185 | 4.10k | for (; h > 0; --h) |
2186 | 2.68k | { |
2187 | 67.3k | for (x = w; x > 0; --x) |
2188 | 64.7k | { |
2189 | 64.7k | TIFFCIELab16ToXYZ(img->cielab, (uint16_t)wp[0], (int16_t)wp[1], |
2190 | 64.7k | (int16_t)wp[2], &X, &Y, &Z); |
2191 | 64.7k | TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b); |
2192 | 64.7k | *cp++ = PACK(r, g, b); |
2193 | 64.7k | wp += 3; |
2194 | 64.7k | } |
2195 | 2.68k | cp += toskew; |
2196 | 2.68k | wp += fromskew; |
2197 | 2.68k | } |
2198 | 1.42k | } |
2199 | | |
2200 | | /* |
2201 | | * YCbCr -> RGB conversion and packing routines. |
2202 | | */ |
2203 | | |
2204 | | #define YCbCrtoRGB(dst, Y) \ |
2205 | 21.2M | { \ |
2206 | 21.2M | uint32_t r, g, b; \ |
2207 | 21.2M | TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b); \ |
2208 | 21.2M | dst = PACK(r, g, b); \ |
2209 | 21.2M | } |
2210 | | |
2211 | | /* |
2212 | | * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB |
2213 | | */ |
2214 | | DECLAREContigPutFunc(putcontig8bitYCbCr44tile) |
2215 | 1.48k | { |
2216 | 1.48k | uint32_t *cp1 = cp + w + toskew; |
2217 | 1.48k | uint32_t *cp2 = cp1 + w + toskew; |
2218 | 1.48k | uint32_t *cp3 = cp2 + w + toskew; |
2219 | 1.48k | int32_t incr = 3 * w + 4 * toskew; |
2220 | | |
2221 | 1.48k | (void)y; |
2222 | | /* adjust fromskew */ |
2223 | 1.48k | fromskew = (fromskew / 4) * (4 * 2 + 2); |
2224 | 1.48k | if ((h & 3) == 0 && (w & 3) == 0) |
2225 | 368 | { |
2226 | 1.75k | for (; h >= 4; h -= 4) |
2227 | 1.38k | { |
2228 | 1.38k | x = w >> 2; |
2229 | 1.38k | do |
2230 | 6.59k | { |
2231 | 6.59k | int32_t Cb = pp[16]; |
2232 | 6.59k | int32_t Cr = pp[17]; |
2233 | | |
2234 | 6.59k | YCbCrtoRGB(cp[0], pp[0]); |
2235 | 6.59k | YCbCrtoRGB(cp[1], pp[1]); |
2236 | 6.59k | YCbCrtoRGB(cp[2], pp[2]); |
2237 | 6.59k | YCbCrtoRGB(cp[3], pp[3]); |
2238 | 6.59k | YCbCrtoRGB(cp1[0], pp[4]); |
2239 | 6.59k | YCbCrtoRGB(cp1[1], pp[5]); |
2240 | 6.59k | YCbCrtoRGB(cp1[2], pp[6]); |
2241 | 6.59k | YCbCrtoRGB(cp1[3], pp[7]); |
2242 | 6.59k | YCbCrtoRGB(cp2[0], pp[8]); |
2243 | 6.59k | YCbCrtoRGB(cp2[1], pp[9]); |
2244 | 6.59k | YCbCrtoRGB(cp2[2], pp[10]); |
2245 | 6.59k | YCbCrtoRGB(cp2[3], pp[11]); |
2246 | 6.59k | YCbCrtoRGB(cp3[0], pp[12]); |
2247 | 6.59k | YCbCrtoRGB(cp3[1], pp[13]); |
2248 | 6.59k | YCbCrtoRGB(cp3[2], pp[14]); |
2249 | 6.59k | YCbCrtoRGB(cp3[3], pp[15]); |
2250 | | |
2251 | 6.59k | cp += 4; |
2252 | 6.59k | cp1 += 4; |
2253 | 6.59k | cp2 += 4; |
2254 | 6.59k | cp3 += 4; |
2255 | 6.59k | pp += 18; |
2256 | 6.59k | } while (--x); |
2257 | 1.38k | cp += incr; |
2258 | 1.38k | cp1 += incr; |
2259 | 1.38k | cp2 += incr; |
2260 | 1.38k | cp3 += incr; |
2261 | 1.38k | pp += fromskew; |
2262 | 1.38k | } |
2263 | 368 | } |
2264 | 1.12k | else |
2265 | 1.12k | { |
2266 | 4.50k | while (h > 0) |
2267 | 4.50k | { |
2268 | 94.6k | for (x = w; x > 0;) |
2269 | 90.1k | { |
2270 | 90.1k | int32_t Cb = pp[16]; |
2271 | 90.1k | int32_t Cr = pp[17]; |
2272 | 90.1k | switch (x) |
2273 | 90.1k | { |
2274 | 86.8k | default: |
2275 | 86.8k | switch (h) |
2276 | 86.8k | { |
2277 | 80.5k | default: |
2278 | 80.5k | YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */ |
2279 | 81.6k | case 3: |
2280 | 81.6k | YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */ |
2281 | 83.7k | case 2: |
2282 | 83.7k | YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */ |
2283 | 86.8k | case 1: |
2284 | 86.8k | YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */ |
2285 | 86.8k | } /* FALLTHROUGH */ |
2286 | 88.0k | case 3: |
2287 | 88.0k | switch (h) |
2288 | 88.0k | { |
2289 | 81.5k | default: |
2290 | 81.5k | YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */ |
2291 | 82.6k | case 3: |
2292 | 82.6k | YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */ |
2293 | 84.9k | case 2: |
2294 | 84.9k | YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */ |
2295 | 88.0k | case 1: |
2296 | 88.0k | YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */ |
2297 | 88.0k | } /* FALLTHROUGH */ |
2298 | 89.0k | case 2: |
2299 | 89.0k | switch (h) |
2300 | 89.0k | { |
2301 | 82.5k | default: |
2302 | 82.5k | YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */ |
2303 | 83.6k | case 3: |
2304 | 83.6k | YCbCrtoRGB(cp2[1], pp[9]); /* FALLTHROUGH */ |
2305 | 85.9k | case 2: |
2306 | 85.9k | YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */ |
2307 | 89.0k | case 1: |
2308 | 89.0k | YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */ |
2309 | 89.0k | } /* FALLTHROUGH */ |
2310 | 90.1k | case 1: |
2311 | 90.1k | switch (h) |
2312 | 90.1k | { |
2313 | 83.4k | default: |
2314 | 83.4k | YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */ |
2315 | 84.6k | case 3: |
2316 | 84.6k | YCbCrtoRGB(cp2[0], pp[8]); /* FALLTHROUGH */ |
2317 | 87.0k | case 2: |
2318 | 87.0k | YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */ |
2319 | 90.1k | case 1: |
2320 | 90.1k | YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */ |
2321 | 90.1k | } /* FALLTHROUGH */ |
2322 | 90.1k | } |
2323 | 90.1k | if (x < 4) |
2324 | 3.35k | { |
2325 | 3.35k | cp += x; |
2326 | 3.35k | cp1 += x; |
2327 | 3.35k | cp2 += x; |
2328 | 3.35k | cp3 += x; |
2329 | 3.35k | x = 0; |
2330 | 3.35k | } |
2331 | 86.8k | else |
2332 | 86.8k | { |
2333 | 86.8k | cp += 4; |
2334 | 86.8k | cp1 += 4; |
2335 | 86.8k | cp2 += 4; |
2336 | 86.8k | cp3 += 4; |
2337 | 86.8k | x -= 4; |
2338 | 86.8k | } |
2339 | 90.1k | pp += 18; |
2340 | 90.1k | } |
2341 | 4.50k | if (h <= 4) |
2342 | 1.12k | break; |
2343 | 3.38k | h -= 4; |
2344 | 3.38k | cp += incr; |
2345 | 3.38k | cp1 += incr; |
2346 | 3.38k | cp2 += incr; |
2347 | 3.38k | cp3 += incr; |
2348 | 3.38k | pp += fromskew; |
2349 | 3.38k | } |
2350 | 1.12k | } |
2351 | 1.48k | } |
2352 | | |
2353 | | /* |
2354 | | * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB |
2355 | | */ |
2356 | | DECLAREContigPutFunc(putcontig8bitYCbCr42tile) |
2357 | 779 | { |
2358 | 779 | uint32_t *cp1 = cp + w + toskew; |
2359 | 779 | int32_t incr = 2 * toskew + w; |
2360 | | |
2361 | 779 | (void)y; |
2362 | 779 | fromskew = (fromskew / 4) * (4 * 2 + 2); |
2363 | 779 | if ((w & 3) == 0 && (h & 1) == 0) |
2364 | 205 | { |
2365 | 1.87k | for (; h >= 2; h -= 2) |
2366 | 1.67k | { |
2367 | 1.67k | x = w >> 2; |
2368 | 1.67k | do |
2369 | 9.51k | { |
2370 | 9.51k | int32_t Cb = pp[8]; |
2371 | 9.51k | int32_t Cr = pp[9]; |
2372 | | |
2373 | 9.51k | YCbCrtoRGB(cp[0], pp[0]); |
2374 | 9.51k | YCbCrtoRGB(cp[1], pp[1]); |
2375 | 9.51k | YCbCrtoRGB(cp[2], pp[2]); |
2376 | 9.51k | YCbCrtoRGB(cp[3], pp[3]); |
2377 | 9.51k | YCbCrtoRGB(cp1[0], pp[4]); |
2378 | 9.51k | YCbCrtoRGB(cp1[1], pp[5]); |
2379 | 9.51k | YCbCrtoRGB(cp1[2], pp[6]); |
2380 | 9.51k | YCbCrtoRGB(cp1[3], pp[7]); |
2381 | | |
2382 | 9.51k | cp += 4; |
2383 | 9.51k | cp1 += 4; |
2384 | 9.51k | pp += 10; |
2385 | 9.51k | } while (--x); |
2386 | 1.67k | cp += incr; |
2387 | 1.67k | cp1 += incr; |
2388 | 1.67k | pp += fromskew; |
2389 | 1.67k | } |
2390 | 205 | } |
2391 | 574 | else |
2392 | 574 | { |
2393 | 3.58k | while (h > 0) |
2394 | 3.58k | { |
2395 | 79.2k | for (x = w; x > 0;) |
2396 | 75.6k | { |
2397 | 75.6k | int32_t Cb = pp[8]; |
2398 | 75.6k | int32_t Cr = pp[9]; |
2399 | 75.6k | switch (x) |
2400 | 75.6k | { |
2401 | 72.7k | default: |
2402 | 72.7k | switch (h) |
2403 | 72.7k | { |
2404 | 71.5k | default: |
2405 | 71.5k | YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */ |
2406 | 72.7k | case 1: |
2407 | 72.7k | YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */ |
2408 | 72.7k | } /* FALLTHROUGH */ |
2409 | 73.2k | case 3: |
2410 | 73.2k | switch (h) |
2411 | 73.2k | { |
2412 | 72.0k | default: |
2413 | 72.0k | YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */ |
2414 | 73.2k | case 1: |
2415 | 73.2k | YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */ |
2416 | 73.2k | } /* FALLTHROUGH */ |
2417 | 74.3k | case 2: |
2418 | 74.3k | switch (h) |
2419 | 74.3k | { |
2420 | 73.0k | default: |
2421 | 73.0k | YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */ |
2422 | 74.3k | case 1: |
2423 | 74.3k | YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */ |
2424 | 74.3k | } /* FALLTHROUGH */ |
2425 | 75.6k | case 1: |
2426 | 75.6k | switch (h) |
2427 | 75.6k | { |
2428 | 74.3k | default: |
2429 | 74.3k | YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */ |
2430 | 75.6k | case 1: |
2431 | 75.6k | YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */ |
2432 | 75.6k | } /* FALLTHROUGH */ |
2433 | 75.6k | } |
2434 | 75.6k | if (x < 4) |
2435 | 2.90k | { |
2436 | 2.90k | cp += x; |
2437 | 2.90k | cp1 += x; |
2438 | 2.90k | x = 0; |
2439 | 2.90k | } |
2440 | 72.7k | else |
2441 | 72.7k | { |
2442 | 72.7k | cp += 4; |
2443 | 72.7k | cp1 += 4; |
2444 | 72.7k | x -= 4; |
2445 | 72.7k | } |
2446 | 75.6k | pp += 10; |
2447 | 75.6k | } |
2448 | 3.58k | if (h <= 2) |
2449 | 574 | break; |
2450 | 3.01k | h -= 2; |
2451 | 3.01k | cp += incr; |
2452 | 3.01k | cp1 += incr; |
2453 | 3.01k | pp += fromskew; |
2454 | 3.01k | } |
2455 | 574 | } |
2456 | 779 | } |
2457 | | |
2458 | | /* |
2459 | | * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB |
2460 | | */ |
2461 | | DECLAREContigPutFunc(putcontig8bitYCbCr41tile) |
2462 | 33.8k | { |
2463 | 33.8k | (void)y; |
2464 | 33.8k | fromskew = (fromskew / 4) * (4 * 1 + 2); |
2465 | 33.8k | do |
2466 | 279k | { |
2467 | 279k | x = w >> 2; |
2468 | 566k | while (x > 0) |
2469 | 286k | { |
2470 | 286k | int32_t Cb = pp[4]; |
2471 | 286k | int32_t Cr = pp[5]; |
2472 | | |
2473 | 286k | YCbCrtoRGB(cp[0], pp[0]); |
2474 | 286k | YCbCrtoRGB(cp[1], pp[1]); |
2475 | 286k | YCbCrtoRGB(cp[2], pp[2]); |
2476 | 286k | YCbCrtoRGB(cp[3], pp[3]); |
2477 | | |
2478 | 286k | cp += 4; |
2479 | 286k | pp += 6; |
2480 | 286k | x--; |
2481 | 286k | } |
2482 | | |
2483 | 279k | if ((w & 3) != 0) |
2484 | 274k | { |
2485 | 274k | int32_t Cb = pp[4]; |
2486 | 274k | int32_t Cr = pp[5]; |
2487 | | |
2488 | 274k | switch ((w & 3)) |
2489 | 274k | { |
2490 | 17.4k | case 3: |
2491 | 17.4k | YCbCrtoRGB(cp[2], pp[2]); /*-fallthrough*/ |
2492 | 269k | case 2: |
2493 | 269k | YCbCrtoRGB(cp[1], pp[1]); /*-fallthrough*/ |
2494 | 274k | case 1: |
2495 | 274k | YCbCrtoRGB(cp[0], pp[0]); /*-fallthrough*/ |
2496 | 274k | case 0: |
2497 | 274k | break; |
2498 | 274k | } |
2499 | | |
2500 | 274k | cp += (w & 3); |
2501 | 274k | pp += 6; |
2502 | 274k | } |
2503 | | |
2504 | 279k | cp += toskew; |
2505 | 279k | pp += fromskew; |
2506 | 279k | } while (--h); |
2507 | 33.8k | } |
2508 | | |
2509 | | /* |
2510 | | * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB |
2511 | | */ |
2512 | | DECLAREContigPutFunc(putcontig8bitYCbCr22tile) |
2513 | 36.2k | { |
2514 | 36.2k | uint32_t *cp2; |
2515 | 36.2k | int32_t incr = 2 * toskew + w; |
2516 | 36.2k | (void)y; |
2517 | 36.2k | fromskew = (fromskew / 2) * (2 * 2 + 2); |
2518 | 36.2k | cp2 = cp + w + toskew; |
2519 | 478k | while (h >= 2) |
2520 | 442k | { |
2521 | 442k | x = w; |
2522 | 3.51M | while (x >= 2) |
2523 | 3.07M | { |
2524 | 3.07M | uint32_t Cb = pp[4]; |
2525 | 3.07M | uint32_t Cr = pp[5]; |
2526 | 3.07M | YCbCrtoRGB(cp[0], pp[0]); |
2527 | 3.07M | YCbCrtoRGB(cp[1], pp[1]); |
2528 | 3.07M | YCbCrtoRGB(cp2[0], pp[2]); |
2529 | 3.07M | YCbCrtoRGB(cp2[1], pp[3]); |
2530 | 3.07M | cp += 2; |
2531 | 3.07M | cp2 += 2; |
2532 | 3.07M | pp += 6; |
2533 | 3.07M | x -= 2; |
2534 | 3.07M | } |
2535 | 442k | if (x == 1) |
2536 | 197k | { |
2537 | 197k | uint32_t Cb = pp[4]; |
2538 | 197k | uint32_t Cr = pp[5]; |
2539 | 197k | YCbCrtoRGB(cp[0], pp[0]); |
2540 | 197k | YCbCrtoRGB(cp2[0], pp[2]); |
2541 | 197k | cp++; |
2542 | 197k | cp2++; |
2543 | 197k | pp += 6; |
2544 | 197k | } |
2545 | 442k | cp += incr; |
2546 | 442k | cp2 += incr; |
2547 | 442k | pp += fromskew; |
2548 | 442k | h -= 2; |
2549 | 442k | } |
2550 | 36.2k | if (h == 1) |
2551 | 6.67k | { |
2552 | 6.67k | x = w; |
2553 | 106k | while (x >= 2) |
2554 | 100k | { |
2555 | 100k | uint32_t Cb = pp[4]; |
2556 | 100k | uint32_t Cr = pp[5]; |
2557 | 100k | YCbCrtoRGB(cp[0], pp[0]); |
2558 | 100k | YCbCrtoRGB(cp[1], pp[1]); |
2559 | 100k | cp += 2; |
2560 | 100k | cp2 += 2; |
2561 | 100k | pp += 6; |
2562 | 100k | x -= 2; |
2563 | 100k | } |
2564 | 6.67k | if (x == 1) |
2565 | 2.26k | { |
2566 | 2.26k | uint32_t Cb = pp[4]; |
2567 | 2.26k | uint32_t Cr = pp[5]; |
2568 | 2.26k | YCbCrtoRGB(cp[0], pp[0]); |
2569 | 2.26k | } |
2570 | 6.67k | } |
2571 | 36.2k | } |
2572 | | |
2573 | | /* |
2574 | | * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB |
2575 | | */ |
2576 | | DECLAREContigPutFunc(putcontig8bitYCbCr21tile) |
2577 | 2.91k | { |
2578 | 2.91k | (void)y; |
2579 | 2.91k | fromskew = (fromskew / 2) * (2 * 1 + 2); |
2580 | 2.91k | do |
2581 | 290k | { |
2582 | 290k | x = w >> 1; |
2583 | 810k | while (x > 0) |
2584 | 520k | { |
2585 | 520k | int32_t Cb = pp[2]; |
2586 | 520k | int32_t Cr = pp[3]; |
2587 | | |
2588 | 520k | YCbCrtoRGB(cp[0], pp[0]); |
2589 | 520k | YCbCrtoRGB(cp[1], pp[1]); |
2590 | | |
2591 | 520k | cp += 2; |
2592 | 520k | pp += 4; |
2593 | 520k | x--; |
2594 | 520k | } |
2595 | | |
2596 | 290k | if ((w & 1) != 0) |
2597 | 10.0k | { |
2598 | 10.0k | int32_t Cb = pp[2]; |
2599 | 10.0k | int32_t Cr = pp[3]; |
2600 | | |
2601 | 10.0k | YCbCrtoRGB(cp[0], pp[0]); |
2602 | | |
2603 | 10.0k | cp += 1; |
2604 | 10.0k | pp += 4; |
2605 | 10.0k | } |
2606 | | |
2607 | 290k | cp += toskew; |
2608 | 290k | pp += fromskew; |
2609 | 290k | } while (--h); |
2610 | 2.91k | } |
2611 | | |
2612 | | /* |
2613 | | * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB |
2614 | | */ |
2615 | | DECLAREContigPutFunc(putcontig8bitYCbCr12tile) |
2616 | 993 | { |
2617 | 993 | uint32_t *cp2; |
2618 | 993 | int32_t incr = 2 * toskew + w; |
2619 | 993 | (void)y; |
2620 | 993 | fromskew = (fromskew / 1) * (1 * 2 + 2); |
2621 | 993 | cp2 = cp + w + toskew; |
2622 | 5.04k | while (h >= 2) |
2623 | 4.05k | { |
2624 | 4.05k | x = w; |
2625 | 4.05k | do |
2626 | 829k | { |
2627 | 829k | uint32_t Cb = pp[2]; |
2628 | 829k | uint32_t Cr = pp[3]; |
2629 | 829k | YCbCrtoRGB(cp[0], pp[0]); |
2630 | 829k | YCbCrtoRGB(cp2[0], pp[1]); |
2631 | 829k | cp++; |
2632 | 829k | cp2++; |
2633 | 829k | pp += 4; |
2634 | 829k | } while (--x); |
2635 | 4.05k | cp += incr; |
2636 | 4.05k | cp2 += incr; |
2637 | 4.05k | pp += fromskew; |
2638 | 4.05k | h -= 2; |
2639 | 4.05k | } |
2640 | 993 | if (h == 1) |
2641 | 369 | { |
2642 | 369 | x = w; |
2643 | 369 | do |
2644 | 4.06k | { |
2645 | 4.06k | uint32_t Cb = pp[2]; |
2646 | 4.06k | uint32_t Cr = pp[3]; |
2647 | 4.06k | YCbCrtoRGB(cp[0], pp[0]); |
2648 | 4.06k | cp++; |
2649 | 4.06k | pp += 4; |
2650 | 4.06k | } while (--x); |
2651 | 369 | } |
2652 | 993 | } |
2653 | | |
2654 | | /* |
2655 | | * 8-bit packed YCbCr samples w/ no subsampling => RGB |
2656 | | */ |
2657 | | DECLAREContigPutFunc(putcontig8bitYCbCr11tile) |
2658 | 6.95k | { |
2659 | 6.95k | (void)y; |
2660 | 6.95k | fromskew = (fromskew / 1) * (1 * 1 + 2); |
2661 | 6.95k | do |
2662 | 8.15k | { |
2663 | 8.15k | x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */ |
2664 | 8.15k | do |
2665 | 1.85M | { |
2666 | 1.85M | int32_t Cb = pp[1]; |
2667 | 1.85M | int32_t Cr = pp[2]; |
2668 | | |
2669 | 1.85M | YCbCrtoRGB(*cp++, pp[0]); |
2670 | | |
2671 | 1.85M | pp += 3; |
2672 | 1.85M | } while (--x); |
2673 | 8.15k | cp += toskew; |
2674 | 8.15k | pp += fromskew; |
2675 | 8.15k | } while (--h); |
2676 | 6.95k | } |
2677 | | |
2678 | | /* |
2679 | | * 8-bit packed YCbCr samples w/ no subsampling => RGB |
2680 | | */ |
2681 | | DECLARESepPutFunc(putseparate8bitYCbCr11tile) |
2682 | 1.12k | { |
2683 | 1.12k | (void)y; |
2684 | 1.12k | (void)a; |
2685 | | /* TODO: naming of input vars is still off, change obfuscating declaration |
2686 | | * inside define, or resolve obfuscation */ |
2687 | 7.84k | for (; h > 0; --h) |
2688 | 6.71k | { |
2689 | 6.71k | x = w; |
2690 | 6.71k | do |
2691 | 82.5k | { |
2692 | 82.5k | uint32_t dr, dg, db; |
2693 | 82.5k | TIFFYCbCrtoRGB(img->ycbcr, *r++, *g++, *b++, &dr, &dg, &db); |
2694 | 82.5k | *cp++ = PACK(dr, dg, db); |
2695 | 82.5k | } while (--x); |
2696 | 6.71k | SKEW(r, g, b, fromskew); |
2697 | 6.71k | cp += toskew; |
2698 | 6.71k | } |
2699 | 1.12k | } |
2700 | | #undef YCbCrtoRGB |
2701 | | |
2702 | | static int isInRefBlackWhiteRange(float f) |
2703 | 510k | { |
2704 | 510k | return f > (float)(-0x7FFFFFFF + 128) && f < (float)0x7FFFFFFF; |
2705 | 510k | } |
2706 | | |
2707 | | static int initYCbCrConversion(TIFFRGBAImage *img) |
2708 | 85.0k | { |
2709 | 85.0k | static const char module[] = "initYCbCrConversion"; |
2710 | | |
2711 | 85.0k | float *luma, *refBlackWhite; |
2712 | | |
2713 | 85.0k | if (img->ycbcr == NULL) |
2714 | 85.0k | { |
2715 | 85.0k | img->ycbcr = (TIFFYCbCrToRGB *)_TIFFmallocExt( |
2716 | 85.0k | img->tif, TIFFroundup_32(sizeof(TIFFYCbCrToRGB), sizeof(long)) + |
2717 | 85.0k | 4 * 256 * sizeof(TIFFRGBValue) + |
2718 | 85.0k | 2 * 256 * sizeof(int) + 3 * 256 * sizeof(int32_t)); |
2719 | 85.0k | if (img->ycbcr == NULL) |
2720 | 0 | { |
2721 | 0 | TIFFErrorExtR(img->tif, module, |
2722 | 0 | "No space for YCbCr->RGB conversion state"); |
2723 | 0 | return (0); |
2724 | 0 | } |
2725 | 85.0k | } |
2726 | | |
2727 | 85.0k | TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma); |
2728 | 85.0k | TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE, |
2729 | 85.0k | &refBlackWhite); |
2730 | | |
2731 | | /* Do some validation to avoid later issues. Detect NaN for now */ |
2732 | | /* and also if lumaGreen is zero since we divide by it later */ |
2733 | 85.0k | if (luma[0] != luma[0] || luma[1] != luma[1] || luma[1] == 0.0 || |
2734 | 85.0k | luma[2] != luma[2]) |
2735 | 4 | { |
2736 | 4 | TIFFErrorExtR(img->tif, module, |
2737 | 4 | "Invalid values for YCbCrCoefficients tag"); |
2738 | 4 | return (0); |
2739 | 4 | } |
2740 | | |
2741 | 85.0k | if (!isInRefBlackWhiteRange(refBlackWhite[0]) || |
2742 | 85.0k | !isInRefBlackWhiteRange(refBlackWhite[1]) || |
2743 | 85.0k | !isInRefBlackWhiteRange(refBlackWhite[2]) || |
2744 | 85.0k | !isInRefBlackWhiteRange(refBlackWhite[3]) || |
2745 | 85.0k | !isInRefBlackWhiteRange(refBlackWhite[4]) || |
2746 | 85.0k | !isInRefBlackWhiteRange(refBlackWhite[5])) |
2747 | 32 | { |
2748 | 32 | TIFFErrorExtR(img->tif, module, |
2749 | 32 | "Invalid values for ReferenceBlackWhite tag"); |
2750 | 32 | return (0); |
2751 | 32 | } |
2752 | | |
2753 | 85.0k | if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0) |
2754 | 0 | return (0); |
2755 | 85.0k | return (1); |
2756 | 85.0k | } |
2757 | | |
2758 | | static tileContigRoutine initCIELabConversion(TIFFRGBAImage *img) |
2759 | 18.3k | { |
2760 | 18.3k | static const char module[] = "initCIELabConversion"; |
2761 | | |
2762 | 18.3k | float *whitePoint; |
2763 | 18.3k | float refWhite[3]; |
2764 | | |
2765 | 18.3k | TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint); |
2766 | 18.3k | if (whitePoint[1] == 0.0f) |
2767 | 1 | { |
2768 | 1 | TIFFErrorExtR(img->tif, module, "Invalid value for WhitePoint tag."); |
2769 | 1 | return NULL; |
2770 | 1 | } |
2771 | | |
2772 | 18.3k | if (!img->cielab) |
2773 | 18.3k | { |
2774 | 18.3k | img->cielab = (TIFFCIELabToRGB *)_TIFFmallocExt( |
2775 | 18.3k | img->tif, sizeof(TIFFCIELabToRGB)); |
2776 | 18.3k | if (!img->cielab) |
2777 | 0 | { |
2778 | 0 | TIFFErrorExtR(img->tif, module, |
2779 | 0 | "No space for CIE L*a*b*->RGB conversion state."); |
2780 | 0 | return NULL; |
2781 | 0 | } |
2782 | 18.3k | } |
2783 | | |
2784 | 18.3k | refWhite[1] = 100.0F; |
2785 | 18.3k | refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1]; |
2786 | 18.3k | refWhite[2] = |
2787 | 18.3k | (1.0F - whitePoint[0] - whitePoint[1]) / whitePoint[1] * refWhite[1]; |
2788 | 18.3k | if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0) |
2789 | 0 | { |
2790 | 0 | TIFFErrorExtR(img->tif, module, |
2791 | 0 | "Failed to initialize CIE L*a*b*->RGB conversion state."); |
2792 | 0 | _TIFFfreeExt(img->tif, img->cielab); |
2793 | 0 | return NULL; |
2794 | 0 | } |
2795 | | |
2796 | 18.3k | if (img->bitspersample == 8) |
2797 | 16.8k | return putcontig8bitCIELab8; |
2798 | 1.46k | else if (img->bitspersample == 16) |
2799 | 1.46k | return putcontig8bitCIELab16; |
2800 | 0 | return NULL; |
2801 | 18.3k | } |
2802 | | |
2803 | | /* |
2804 | | * Greyscale images with less than 8 bits/sample are handled |
2805 | | * with a table to avoid lots of shifts and masks. The table |
2806 | | * is setup so that put*bwtile (below) can retrieve 8/bitspersample |
2807 | | * pixel values simply by indexing into the table with one |
2808 | | * number. |
2809 | | */ |
2810 | | static int makebwmap(TIFFRGBAImage *img) |
2811 | 97.0k | { |
2812 | 97.0k | TIFFRGBValue *Map = img->Map; |
2813 | 97.0k | int bitspersample = img->bitspersample; |
2814 | 97.0k | int nsamples = 8 / bitspersample; |
2815 | 97.0k | int i; |
2816 | 97.0k | uint32_t *p; |
2817 | | |
2818 | 97.0k | if (nsamples == 0) |
2819 | 6.06k | nsamples = 1; |
2820 | | |
2821 | 97.0k | img->BWmap = (uint32_t **)_TIFFmallocExt( |
2822 | 97.0k | img->tif, |
2823 | 97.0k | 256 * sizeof(uint32_t *) + (256 * nsamples * sizeof(uint32_t))); |
2824 | 97.0k | if (img->BWmap == NULL) |
2825 | 0 | { |
2826 | 0 | TIFFErrorExtR(img->tif, TIFFFileName(img->tif), |
2827 | 0 | "No space for B&W mapping table"); |
2828 | 0 | return (0); |
2829 | 0 | } |
2830 | 97.0k | p = (uint32_t *)(img->BWmap + 256); |
2831 | 24.9M | for (i = 0; i < 256; i++) |
2832 | 24.8M | { |
2833 | 24.8M | TIFFRGBValue c; |
2834 | 24.8M | img->BWmap[i] = p; |
2835 | 24.8M | switch (bitspersample) |
2836 | 24.8M | { |
2837 | 0 | #define GREY(x) \ |
2838 | 126M | c = Map[x]; \ |
2839 | 126M | *p++ = PACK(c, c, c); |
2840 | 11.4M | case 1: |
2841 | 11.4M | GREY(i >> 7); |
2842 | 11.4M | GREY((i >> 6) & 1); |
2843 | 11.4M | GREY((i >> 5) & 1); |
2844 | 11.4M | GREY((i >> 4) & 1); |
2845 | 11.4M | GREY((i >> 3) & 1); |
2846 | 11.4M | GREY((i >> 2) & 1); |
2847 | 11.4M | GREY((i >> 1) & 1); |
2848 | 11.4M | GREY(i & 1); |
2849 | 11.4M | break; |
2850 | 7.02M | case 2: |
2851 | 7.02M | GREY(i >> 6); |
2852 | 7.02M | GREY((i >> 4) & 3); |
2853 | 7.02M | GREY((i >> 2) & 3); |
2854 | 7.02M | GREY(i & 3); |
2855 | 7.02M | break; |
2856 | 504k | case 4: |
2857 | 504k | GREY(i >> 4); |
2858 | 504k | GREY(i & 0xf); |
2859 | 504k | break; |
2860 | 4.32M | case 8: |
2861 | 5.87M | case 16: |
2862 | 5.87M | GREY(i); |
2863 | 5.87M | break; |
2864 | 24.8M | } |
2865 | 24.8M | #undef GREY |
2866 | 24.8M | } |
2867 | 97.0k | return (1); |
2868 | 97.0k | } |
2869 | | |
2870 | | /* |
2871 | | * Construct a mapping table to convert from the range |
2872 | | * of the data samples to [0,255] --for display. This |
2873 | | * process also handles inverting B&W images when needed. |
2874 | | */ |
2875 | | static int setupMap(TIFFRGBAImage *img) |
2876 | 97.0k | { |
2877 | 97.0k | int32_t x, range; |
2878 | | |
2879 | 97.0k | range = (int32_t)((1L << img->bitspersample) - 1); |
2880 | | |
2881 | | /* treat 16 bit the same as eight bit */ |
2882 | 97.0k | if (img->bitspersample == 16) |
2883 | 6.06k | range = (int32_t)255; |
2884 | | |
2885 | 97.0k | img->Map = (TIFFRGBValue *)_TIFFmallocExt( |
2886 | 97.0k | img->tif, (range + 1) * sizeof(TIFFRGBValue)); |
2887 | 97.0k | if (img->Map == NULL) |
2888 | 0 | { |
2889 | 0 | TIFFErrorExtR(img->tif, TIFFFileName(img->tif), |
2890 | 0 | "No space for photometric conversion table"); |
2891 | 0 | return (0); |
2892 | 0 | } |
2893 | 97.0k | if (img->photometric == PHOTOMETRIC_MINISWHITE) |
2894 | 54.7k | { |
2895 | 5.51M | for (x = 0; x <= range; x++) |
2896 | 5.45M | img->Map[x] = (TIFFRGBValue)(((range - x) * 255) / range); |
2897 | 54.7k | } |
2898 | 42.3k | else |
2899 | 42.3k | { |
2900 | 693k | for (x = 0; x <= range; x++) |
2901 | 651k | img->Map[x] = (TIFFRGBValue)((x * 255) / range); |
2902 | 42.3k | } |
2903 | 97.0k | if (img->bitspersample <= 16 && |
2904 | 97.0k | (img->photometric == PHOTOMETRIC_MINISBLACK || |
2905 | 97.0k | img->photometric == PHOTOMETRIC_MINISWHITE)) |
2906 | 97.0k | { |
2907 | | /* |
2908 | | * Use photometric mapping table to construct |
2909 | | * unpacking tables for samples <= 8 bits. |
2910 | | */ |
2911 | 97.0k | if (!makebwmap(img)) |
2912 | 0 | return (0); |
2913 | | /* no longer need Map, free it */ |
2914 | 97.0k | _TIFFfreeExt(img->tif, img->Map); |
2915 | 97.0k | img->Map = NULL; |
2916 | 97.0k | } |
2917 | 97.0k | return (1); |
2918 | 97.0k | } |
2919 | | |
2920 | | static int checkcmap(TIFFRGBAImage *img) |
2921 | 27.0k | { |
2922 | 27.0k | uint16_t *r = img->redcmap; |
2923 | 27.0k | uint16_t *g = img->greencmap; |
2924 | 27.0k | uint16_t *b = img->bluecmap; |
2925 | 27.0k | long n = 1L << img->bitspersample; |
2926 | | |
2927 | 819k | while (n-- > 0) |
2928 | 816k | if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256) |
2929 | 23.9k | return (16); |
2930 | 3.06k | return (8); |
2931 | 27.0k | } |
2932 | | |
2933 | | static void cvtcmap(TIFFRGBAImage *img) |
2934 | 23.9k | { |
2935 | 23.9k | uint16_t *r = img->redcmap; |
2936 | 23.9k | uint16_t *g = img->greencmap; |
2937 | 23.9k | uint16_t *b = img->bluecmap; |
2938 | 23.9k | long i; |
2939 | | |
2940 | 5.80M | for (i = (1L << img->bitspersample) - 1; i >= 0; i--) |
2941 | 5.77M | { |
2942 | 17.3M | #define CVT(x) ((uint16_t)((x) >> 8)) |
2943 | 5.77M | r[i] = CVT(r[i]); |
2944 | 5.77M | g[i] = CVT(g[i]); |
2945 | 5.77M | b[i] = CVT(b[i]); |
2946 | 5.77M | #undef CVT |
2947 | 5.77M | } |
2948 | 23.9k | } |
2949 | | |
2950 | | /* |
2951 | | * Palette images with <= 8 bits/sample are handled |
2952 | | * with a table to avoid lots of shifts and masks. The table |
2953 | | * is setup so that put*cmaptile (below) can retrieve 8/bitspersample |
2954 | | * pixel values simply by indexing into the table with one |
2955 | | * number. |
2956 | | */ |
2957 | | static int makecmap(TIFFRGBAImage *img) |
2958 | 27.0k | { |
2959 | 27.0k | int bitspersample = img->bitspersample; |
2960 | 27.0k | int nsamples = 8 / bitspersample; |
2961 | 27.0k | uint16_t *r = img->redcmap; |
2962 | 27.0k | uint16_t *g = img->greencmap; |
2963 | 27.0k | uint16_t *b = img->bluecmap; |
2964 | 27.0k | uint32_t *p; |
2965 | 27.0k | int i; |
2966 | | |
2967 | 27.0k | img->PALmap = (uint32_t **)_TIFFmallocExt( |
2968 | 27.0k | img->tif, |
2969 | 27.0k | 256 * sizeof(uint32_t *) + (256 * nsamples * sizeof(uint32_t))); |
2970 | 27.0k | if (img->PALmap == NULL) |
2971 | 0 | { |
2972 | 0 | TIFFErrorExtR(img->tif, TIFFFileName(img->tif), |
2973 | 0 | "No space for Palette mapping table"); |
2974 | 0 | return (0); |
2975 | 0 | } |
2976 | 27.0k | p = (uint32_t *)(img->PALmap + 256); |
2977 | 6.95M | for (i = 0; i < 256; i++) |
2978 | 6.92M | { |
2979 | 6.92M | TIFFRGBValue c; |
2980 | 6.92M | img->PALmap[i] = p; |
2981 | 6.92M | #define CMAP(x) \ |
2982 | 8.02M | c = (TIFFRGBValue)x; \ |
2983 | 8.02M | *p++ = PACK(r[c] & 0xff, g[c] & 0xff, b[c] & 0xff); |
2984 | 6.92M | switch (bitspersample) |
2985 | 6.92M | { |
2986 | 0 | case 1: |
2987 | 0 | CMAP(i >> 7); |
2988 | 0 | CMAP((i >> 6) & 1); |
2989 | 0 | CMAP((i >> 5) & 1); |
2990 | 0 | CMAP((i >> 4) & 1); |
2991 | 0 | CMAP((i >> 3) & 1); |
2992 | 0 | CMAP((i >> 2) & 1); |
2993 | 0 | CMAP((i >> 1) & 1); |
2994 | 0 | CMAP(i & 1); |
2995 | 0 | break; |
2996 | 366k | case 2: |
2997 | 366k | CMAP(i >> 6); |
2998 | 366k | CMAP((i >> 4) & 3); |
2999 | 366k | CMAP((i >> 2) & 3); |
3000 | 366k | CMAP(i & 3); |
3001 | 366k | break; |
3002 | 3.07k | case 4: |
3003 | 3.07k | CMAP(i >> 4); |
3004 | 3.07k | CMAP(i & 0xf); |
3005 | 3.07k | break; |
3006 | 6.55M | case 8: |
3007 | 6.55M | CMAP(i); |
3008 | 6.55M | break; |
3009 | 6.92M | } |
3010 | 6.92M | #undef CMAP |
3011 | 6.92M | } |
3012 | 27.0k | return (1); |
3013 | 27.0k | } |
3014 | | |
3015 | | /* |
3016 | | * Construct any mapping table used |
3017 | | * by the associated put routine. |
3018 | | */ |
3019 | | static int buildMap(TIFFRGBAImage *img) |
3020 | 147k | { |
3021 | 147k | switch (img->photometric) |
3022 | 147k | { |
3023 | 0 | case PHOTOMETRIC_RGB: |
3024 | 0 | case PHOTOMETRIC_YCBCR: |
3025 | 4.61k | case PHOTOMETRIC_SEPARATED: |
3026 | 4.61k | if (img->bitspersample == 8) |
3027 | 4.61k | break; |
3028 | | /* fall through... */ |
3029 | 42.3k | case PHOTOMETRIC_MINISBLACK: |
3030 | 97.0k | case PHOTOMETRIC_MINISWHITE: |
3031 | 97.0k | if (!setupMap(img)) |
3032 | 0 | return (0); |
3033 | 97.0k | break; |
3034 | 97.0k | case PHOTOMETRIC_PALETTE: |
3035 | | /* |
3036 | | * Convert 16-bit colormap to 8-bit (unless it looks |
3037 | | * like an old-style 8-bit colormap). |
3038 | | */ |
3039 | 27.0k | if (checkcmap(img) == 16) |
3040 | 23.9k | cvtcmap(img); |
3041 | 3.06k | else |
3042 | 3.06k | TIFFWarningExtR(img->tif, TIFFFileName(img->tif), |
3043 | 3.06k | "Assuming 8-bit colormap"); |
3044 | | /* |
3045 | | * Use mapping table and colormap to construct |
3046 | | * unpacking tables for samples < 8 bits. |
3047 | | */ |
3048 | 27.0k | if (img->bitspersample <= 8 && !makecmap(img)) |
3049 | 0 | return (0); |
3050 | 27.0k | break; |
3051 | 147k | } |
3052 | 147k | return (1); |
3053 | 147k | } |
3054 | | |
3055 | | /* |
3056 | | * Select the appropriate conversion routine for packed data. |
3057 | | */ |
3058 | | static int PickContigCase(TIFFRGBAImage *img) |
3059 | 261k | { |
3060 | 261k | img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig; |
3061 | 261k | img->put.contig = NULL; |
3062 | 261k | switch (img->photometric) |
3063 | 261k | { |
3064 | 30.1k | case PHOTOMETRIC_RGB: |
3065 | 30.1k | switch (img->bitspersample) |
3066 | 30.1k | { |
3067 | 7.51k | case 8: |
3068 | 7.51k | if (img->alpha == EXTRASAMPLE_ASSOCALPHA && |
3069 | 7.51k | img->samplesperpixel >= 4) |
3070 | 3.14k | img->put.contig = putRGBAAcontig8bittile; |
3071 | 4.37k | else if (img->alpha == EXTRASAMPLE_UNASSALPHA && |
3072 | 4.37k | img->samplesperpixel >= 4) |
3073 | 462 | { |
3074 | 462 | if (BuildMapUaToAa(img)) |
3075 | 462 | img->put.contig = putRGBUAcontig8bittile; |
3076 | 462 | } |
3077 | 3.90k | else if (img->samplesperpixel >= 3) |
3078 | 3.90k | img->put.contig = putRGBcontig8bittile; |
3079 | 7.51k | break; |
3080 | 22.6k | case 16: |
3081 | 22.6k | if (img->alpha == EXTRASAMPLE_ASSOCALPHA && |
3082 | 22.6k | img->samplesperpixel >= 4) |
3083 | 1.11k | { |
3084 | 1.11k | if (BuildMapBitdepth16To8(img)) |
3085 | 1.11k | img->put.contig = putRGBAAcontig16bittile; |
3086 | 1.11k | } |
3087 | 21.5k | else if (img->alpha == EXTRASAMPLE_UNASSALPHA && |
3088 | 21.5k | img->samplesperpixel >= 4) |
3089 | 736 | { |
3090 | 736 | if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img)) |
3091 | 736 | img->put.contig = putRGBUAcontig16bittile; |
3092 | 736 | } |
3093 | 20.7k | else if (img->samplesperpixel >= 3) |
3094 | 20.7k | { |
3095 | 20.7k | if (BuildMapBitdepth16To8(img)) |
3096 | 20.7k | img->put.contig = putRGBcontig16bittile; |
3097 | 20.7k | } |
3098 | 22.6k | break; |
3099 | 30.1k | } |
3100 | 30.1k | break; |
3101 | 30.1k | case PHOTOMETRIC_SEPARATED: |
3102 | 4.61k | if (img->samplesperpixel >= 4 && buildMap(img)) |
3103 | 4.61k | { |
3104 | 4.61k | if (img->bitspersample == 8) |
3105 | 4.61k | { |
3106 | 4.61k | if (!img->Map) |
3107 | 4.61k | img->put.contig = putRGBcontig8bitCMYKtile; |
3108 | 0 | else |
3109 | 0 | img->put.contig = putRGBcontig8bitCMYKMaptile; |
3110 | 4.61k | } |
3111 | 4.61k | } |
3112 | 4.61k | break; |
3113 | 27.0k | case PHOTOMETRIC_PALETTE: |
3114 | 27.0k | if (buildMap(img)) |
3115 | 27.0k | { |
3116 | 27.0k | switch (img->bitspersample) |
3117 | 27.0k | { |
3118 | 25.6k | case 8: |
3119 | 25.6k | img->put.contig = put8bitcmaptile; |
3120 | 25.6k | break; |
3121 | 12 | case 4: |
3122 | 12 | img->put.contig = put4bitcmaptile; |
3123 | 12 | break; |
3124 | 1.43k | case 2: |
3125 | 1.43k | img->put.contig = put2bitcmaptile; |
3126 | 1.43k | break; |
3127 | 0 | case 1: |
3128 | 0 | img->put.contig = put1bitcmaptile; |
3129 | 0 | break; |
3130 | 27.0k | } |
3131 | 27.0k | } |
3132 | 27.0k | break; |
3133 | 54.7k | case PHOTOMETRIC_MINISWHITE: |
3134 | 97.0k | case PHOTOMETRIC_MINISBLACK: |
3135 | 97.0k | if (buildMap(img)) |
3136 | 97.0k | { |
3137 | 97.0k | switch (img->bitspersample) |
3138 | 97.0k | { |
3139 | 6.06k | case 16: |
3140 | 6.06k | img->put.contig = put16bitbwtile; |
3141 | 6.06k | break; |
3142 | 16.9k | case 8: |
3143 | 16.9k | if (img->alpha && img->samplesperpixel == 2) |
3144 | 239 | img->put.contig = putagreytile; |
3145 | 16.6k | else |
3146 | 16.6k | img->put.contig = putgreytile; |
3147 | 16.9k | break; |
3148 | 1.97k | case 4: |
3149 | 1.97k | img->put.contig = put4bitbwtile; |
3150 | 1.97k | break; |
3151 | 27.4k | case 2: |
3152 | 27.4k | img->put.contig = put2bitbwtile; |
3153 | 27.4k | break; |
3154 | 44.6k | case 1: |
3155 | 44.6k | img->put.contig = put1bitbwtile; |
3156 | 44.6k | break; |
3157 | 97.0k | } |
3158 | 97.0k | } |
3159 | 97.0k | break; |
3160 | 97.0k | case PHOTOMETRIC_YCBCR: |
3161 | 83.8k | if ((img->bitspersample == 8) && (img->samplesperpixel == 3)) |
3162 | 83.8k | { |
3163 | 83.8k | if (initYCbCrConversion(img) != 0) |
3164 | 83.8k | { |
3165 | | /* |
3166 | | * The 6.0 spec says that subsampling must be |
3167 | | * one of 1, 2, or 4, and that vertical subsampling |
3168 | | * must always be <= horizontal subsampling; so |
3169 | | * there are only a few possibilities and we just |
3170 | | * enumerate the cases. |
3171 | | * Joris: added support for the [1,2] case, nonetheless, to |
3172 | | * accommodate some OJPEG files |
3173 | | */ |
3174 | 83.8k | uint16_t SubsamplingHor; |
3175 | 83.8k | uint16_t SubsamplingVer; |
3176 | 83.8k | TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, |
3177 | 83.8k | &SubsamplingHor, &SubsamplingVer); |
3178 | 83.8k | switch ((SubsamplingHor << 4) | SubsamplingVer) |
3179 | 83.8k | { |
3180 | 1.58k | case 0x44: |
3181 | 1.58k | img->put.contig = putcontig8bitYCbCr44tile; |
3182 | 1.58k | break; |
3183 | 858 | case 0x42: |
3184 | 858 | img->put.contig = putcontig8bitYCbCr42tile; |
3185 | 858 | break; |
3186 | 33.9k | case 0x41: |
3187 | 33.9k | img->put.contig = putcontig8bitYCbCr41tile; |
3188 | 33.9k | break; |
3189 | 36.4k | case 0x22: |
3190 | 36.4k | img->put.contig = putcontig8bitYCbCr22tile; |
3191 | 36.4k | break; |
3192 | 2.96k | case 0x21: |
3193 | 2.96k | img->put.contig = putcontig8bitYCbCr21tile; |
3194 | 2.96k | break; |
3195 | 1.05k | case 0x12: |
3196 | 1.05k | img->put.contig = putcontig8bitYCbCr12tile; |
3197 | 1.05k | break; |
3198 | 7.01k | case 0x11: |
3199 | 7.01k | img->put.contig = putcontig8bitYCbCr11tile; |
3200 | 7.01k | break; |
3201 | 83.8k | } |
3202 | 83.8k | } |
3203 | 83.8k | } |
3204 | 83.8k | break; |
3205 | 83.8k | case PHOTOMETRIC_CIELAB: |
3206 | 18.3k | if (img->samplesperpixel == 3 && buildMap(img)) |
3207 | 18.3k | { |
3208 | 18.3k | if (img->bitspersample == 8 || img->bitspersample == 16) |
3209 | 18.3k | img->put.contig = initCIELabConversion(img); |
3210 | 18.3k | break; |
3211 | 18.3k | } |
3212 | 261k | } |
3213 | 261k | return ((img->get != NULL) && (img->put.contig != NULL)); |
3214 | 261k | } |
3215 | | |
3216 | | /* |
3217 | | * Select the appropriate conversion routine for unpacked data. |
3218 | | * |
3219 | | * NB: we assume that unpacked single channel data is directed |
3220 | | * to the "packed routines. |
3221 | | */ |
3222 | | static int PickSeparateCase(TIFFRGBAImage *img) |
3223 | 12.4k | { |
3224 | 12.4k | img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate; |
3225 | 12.4k | img->put.separate = NULL; |
3226 | 12.4k | switch (img->photometric) |
3227 | 12.4k | { |
3228 | 1.56k | case PHOTOMETRIC_MINISWHITE: |
3229 | 2.68k | case PHOTOMETRIC_MINISBLACK: |
3230 | | /* greyscale images processed pretty much as RGB by gtTileSeparate |
3231 | | */ |
3232 | 7.04k | case PHOTOMETRIC_RGB: |
3233 | 7.04k | switch (img->bitspersample) |
3234 | 7.04k | { |
3235 | 6.94k | case 8: |
3236 | 6.94k | if (img->alpha == EXTRASAMPLE_ASSOCALPHA) |
3237 | 6.57k | img->put.separate = putRGBAAseparate8bittile; |
3238 | 366 | else if (img->alpha == EXTRASAMPLE_UNASSALPHA) |
3239 | 11 | { |
3240 | 11 | if (BuildMapUaToAa(img)) |
3241 | 11 | img->put.separate = putRGBUAseparate8bittile; |
3242 | 11 | } |
3243 | 355 | else |
3244 | 355 | img->put.separate = putRGBseparate8bittile; |
3245 | 6.94k | break; |
3246 | 104 | case 16: |
3247 | 104 | if (img->alpha == EXTRASAMPLE_ASSOCALPHA) |
3248 | 35 | { |
3249 | 35 | if (BuildMapBitdepth16To8(img)) |
3250 | 35 | img->put.separate = putRGBAAseparate16bittile; |
3251 | 35 | } |
3252 | 69 | else if (img->alpha == EXTRASAMPLE_UNASSALPHA) |
3253 | 19 | { |
3254 | 19 | if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img)) |
3255 | 19 | img->put.separate = putRGBUAseparate16bittile; |
3256 | 19 | } |
3257 | 50 | else |
3258 | 50 | { |
3259 | 50 | if (BuildMapBitdepth16To8(img)) |
3260 | 50 | img->put.separate = putRGBseparate16bittile; |
3261 | 50 | } |
3262 | 104 | break; |
3263 | 7.04k | } |
3264 | 7.04k | break; |
3265 | 7.04k | case PHOTOMETRIC_SEPARATED: |
3266 | 4.14k | if (img->bitspersample == 8 && img->samplesperpixel == 4) |
3267 | 4.14k | { |
3268 | | /* Not alpha, but seems like the only way to get 4th band */ |
3269 | 4.14k | img->alpha = 1; |
3270 | 4.14k | img->put.separate = putCMYKseparate8bittile; |
3271 | 4.14k | } |
3272 | 4.14k | break; |
3273 | 1.22k | case PHOTOMETRIC_YCBCR: |
3274 | 1.22k | if ((img->bitspersample == 8) && (img->samplesperpixel == 3)) |
3275 | 1.22k | { |
3276 | 1.22k | if (initYCbCrConversion(img) != 0) |
3277 | 1.21k | { |
3278 | 1.21k | uint16_t hs, vs; |
3279 | 1.21k | TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, |
3280 | 1.21k | &hs, &vs); |
3281 | 1.21k | switch ((hs << 4) | vs) |
3282 | 1.21k | { |
3283 | 1.20k | case 0x11: |
3284 | 1.20k | img->put.separate = putseparate8bitYCbCr11tile; |
3285 | 1.20k | break; |
3286 | | /* TODO: add other cases here */ |
3287 | 1.21k | } |
3288 | 1.21k | } |
3289 | 1.22k | } |
3290 | 1.22k | break; |
3291 | 12.4k | } |
3292 | 12.4k | return ((img->get != NULL) && (img->put.separate != NULL)); |
3293 | 12.4k | } |
3294 | | |
3295 | | static int BuildMapUaToAa(TIFFRGBAImage *img) |
3296 | 1.22k | { |
3297 | 1.22k | static const char module[] = "BuildMapUaToAa"; |
3298 | 1.22k | uint8_t *m; |
3299 | 1.22k | uint16_t na, nv; |
3300 | 1.22k | assert(img->UaToAa == NULL); |
3301 | 1.22k | img->UaToAa = _TIFFmallocExt(img->tif, 65536); |
3302 | 1.22k | if (img->UaToAa == NULL) |
3303 | 0 | { |
3304 | 0 | TIFFErrorExtR(img->tif, module, "Out of memory"); |
3305 | 0 | return (0); |
3306 | 0 | } |
3307 | 1.22k | m = img->UaToAa; |
3308 | 315k | for (na = 0; na < 256; na++) |
3309 | 314k | { |
3310 | 80.7M | for (nv = 0; nv < 256; nv++) |
3311 | 80.4M | *m++ = (uint8_t)((nv * na + 127) / 255); |
3312 | 314k | } |
3313 | 1.22k | return (1); |
3314 | 1.22k | } |
3315 | | |
3316 | | static int BuildMapBitdepth16To8(TIFFRGBAImage *img) |
3317 | 22.7k | { |
3318 | 22.7k | static const char module[] = "BuildMapBitdepth16To8"; |
3319 | 22.7k | uint8_t *m; |
3320 | 22.7k | uint32_t n; |
3321 | 22.7k | assert(img->Bitdepth16To8 == NULL); |
3322 | 22.7k | img->Bitdepth16To8 = _TIFFmallocExt(img->tif, 65536); |
3323 | 22.7k | if (img->Bitdepth16To8 == NULL) |
3324 | 0 | { |
3325 | 0 | TIFFErrorExtR(img->tif, module, "Out of memory"); |
3326 | 0 | return (0); |
3327 | 0 | } |
3328 | 22.7k | m = img->Bitdepth16To8; |
3329 | 1.48G | for (n = 0; n < 65536; n++) |
3330 | 1.48G | *m++ = (uint8_t)((n + 128) / 257); |
3331 | 22.7k | return (1); |
3332 | 22.7k | } |
3333 | | |
3334 | | /* |
3335 | | * Read a whole strip off data from the file, and convert to RGBA form. |
3336 | | * If this is the last strip, then it will only contain the portion of |
3337 | | * the strip that is actually within the image space. The result is |
3338 | | * organized in bottom to top form. |
3339 | | */ |
3340 | | |
3341 | | int TIFFReadRGBAStrip(TIFF *tif, uint32_t row, uint32_t *raster) |
3342 | | |
3343 | 75.8k | { |
3344 | 75.8k | return TIFFReadRGBAStripExt(tif, row, raster, 0); |
3345 | 75.8k | } |
3346 | | |
3347 | | int TIFFReadRGBAStripExt(TIFF *tif, uint32_t row, uint32_t *raster, |
3348 | | int stop_on_error) |
3349 | | |
3350 | 75.8k | { |
3351 | 75.8k | char emsg[EMSG_BUF_SIZE] = ""; |
3352 | 75.8k | TIFFRGBAImage img; |
3353 | 75.8k | int ok; |
3354 | 75.8k | uint32_t rowsperstrip, rows_to_read; |
3355 | | |
3356 | 75.8k | if (TIFFIsTiled(tif)) |
3357 | 0 | { |
3358 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), |
3359 | 0 | "Can't use TIFFReadRGBAStrip() with tiled file."); |
3360 | 0 | return (0); |
3361 | 0 | } |
3362 | | |
3363 | 75.8k | TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); |
3364 | | |
3365 | 75.8k | if (rowsperstrip == 0) |
3366 | 0 | { |
3367 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero"); |
3368 | 0 | return (0); |
3369 | 0 | } |
3370 | | |
3371 | 75.8k | if ((row % rowsperstrip) != 0) |
3372 | 0 | { |
3373 | 0 | TIFFErrorExtR( |
3374 | 0 | tif, TIFFFileName(tif), |
3375 | 0 | "Row passed to TIFFReadRGBAStrip() must be first in a strip."); |
3376 | 0 | return (0); |
3377 | 0 | } |
3378 | | |
3379 | 75.8k | if (TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg)) |
3380 | 75.7k | { |
3381 | 75.7k | if (row >= img.height) |
3382 | 0 | { |
3383 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), |
3384 | 0 | "Invalid row passed to TIFFReadRGBAStrip()."); |
3385 | 0 | TIFFRGBAImageEnd(&img); |
3386 | 0 | return (0); |
3387 | 0 | } |
3388 | | |
3389 | 75.7k | img.row_offset = row; |
3390 | 75.7k | img.col_offset = 0; |
3391 | | |
3392 | 75.7k | if (row + rowsperstrip > img.height) |
3393 | 644 | rows_to_read = img.height - row; |
3394 | 75.1k | else |
3395 | 75.1k | rows_to_read = rowsperstrip; |
3396 | | |
3397 | 75.7k | ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read); |
3398 | | |
3399 | 75.7k | TIFFRGBAImageEnd(&img); |
3400 | 75.7k | } |
3401 | 64 | else |
3402 | 64 | { |
3403 | 64 | TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg); |
3404 | 64 | ok = 0; |
3405 | 64 | } |
3406 | | |
3407 | 75.8k | return (ok); |
3408 | 75.8k | } |
3409 | | |
3410 | | /* |
3411 | | * Read a whole tile off data from the file, and convert to RGBA form. |
3412 | | * The returned RGBA data is organized from bottom to top of tile, |
3413 | | * and may include zeroed areas if the tile extends off the image. |
3414 | | */ |
3415 | | |
3416 | | int TIFFReadRGBATile(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster) |
3417 | | |
3418 | 193k | { |
3419 | 193k | return TIFFReadRGBATileExt(tif, col, row, raster, 0); |
3420 | 193k | } |
3421 | | |
3422 | | int TIFFReadRGBATileExt(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster, |
3423 | | int stop_on_error) |
3424 | 193k | { |
3425 | 193k | char emsg[EMSG_BUF_SIZE] = ""; |
3426 | 193k | TIFFRGBAImage img; |
3427 | 193k | int ok; |
3428 | 193k | uint32_t tile_xsize, tile_ysize; |
3429 | 193k | uint32_t read_xsize, read_ysize; |
3430 | 193k | uint32_t i_row; |
3431 | | |
3432 | | /* |
3433 | | * Verify that our request is legal - on a tile file, and on a |
3434 | | * tile boundary. |
3435 | | */ |
3436 | | |
3437 | 193k | if (!TIFFIsTiled(tif)) |
3438 | 0 | { |
3439 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), |
3440 | 0 | "Can't use TIFFReadRGBATile() with striped file."); |
3441 | 0 | return (0); |
3442 | 0 | } |
3443 | | |
3444 | 193k | TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize); |
3445 | 193k | TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize); |
3446 | 193k | if (tile_xsize == 0 || tile_ysize == 0) |
3447 | 0 | { |
3448 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), |
3449 | 0 | "tile_xsize or tile_ysize is zero"); |
3450 | 0 | return (0); |
3451 | 0 | } |
3452 | | |
3453 | 193k | if ((col % tile_xsize) != 0 || (row % tile_ysize) != 0) |
3454 | 0 | { |
3455 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), |
3456 | 0 | "Row/col passed to TIFFReadRGBATile() must be top" |
3457 | 0 | "left corner of a tile."); |
3458 | 0 | return (0); |
3459 | 0 | } |
3460 | | |
3461 | | /* |
3462 | | * Setup the RGBA reader. |
3463 | | */ |
3464 | | |
3465 | 193k | if (!TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg)) |
3466 | 43 | { |
3467 | 43 | TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg); |
3468 | 43 | return (0); |
3469 | 43 | } |
3470 | | |
3471 | 193k | if (col >= img.width || row >= img.height) |
3472 | 0 | { |
3473 | 0 | TIFFErrorExtR(tif, TIFFFileName(tif), |
3474 | 0 | "Invalid row/col passed to TIFFReadRGBATile()."); |
3475 | 0 | TIFFRGBAImageEnd(&img); |
3476 | 0 | return (0); |
3477 | 0 | } |
3478 | | |
3479 | | /* |
3480 | | * The TIFFRGBAImageGet() function doesn't allow us to get off the |
3481 | | * edge of the image, even to fill an otherwise valid tile. So we |
3482 | | * figure out how much we can read, and fix up the tile buffer to |
3483 | | * a full tile configuration afterwards. |
3484 | | */ |
3485 | | |
3486 | 193k | if (row + tile_ysize > img.height) |
3487 | 7.60k | read_ysize = img.height - row; |
3488 | 186k | else |
3489 | 186k | read_ysize = tile_ysize; |
3490 | | |
3491 | 193k | if (col + tile_xsize > img.width) |
3492 | 8.11k | read_xsize = img.width - col; |
3493 | 185k | else |
3494 | 185k | read_xsize = tile_xsize; |
3495 | | |
3496 | | /* |
3497 | | * Read the chunk of imagery. |
3498 | | */ |
3499 | | |
3500 | 193k | img.row_offset = row; |
3501 | 193k | img.col_offset = col; |
3502 | | |
3503 | 193k | ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize); |
3504 | | |
3505 | 193k | TIFFRGBAImageEnd(&img); |
3506 | | |
3507 | | /* |
3508 | | * If our read was incomplete we will need to fix up the tile by |
3509 | | * shifting the data around as if a full tile of data is being returned. |
3510 | | * |
3511 | | * This is all the more complicated because the image is organized in |
3512 | | * bottom to top format. |
3513 | | */ |
3514 | | |
3515 | 193k | if (read_xsize == tile_xsize && read_ysize == tile_ysize) |
3516 | 178k | return (ok); |
3517 | | |
3518 | 171k | for (i_row = 0; i_row < read_ysize; i_row++) |
3519 | 155k | { |
3520 | 155k | memmove(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize, |
3521 | 155k | raster + (size_t)(read_ysize - i_row - 1) * read_xsize, |
3522 | 155k | read_xsize * sizeof(uint32_t)); |
3523 | 155k | _TIFFmemset(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize + |
3524 | 155k | read_xsize, |
3525 | 155k | 0, sizeof(uint32_t) * (tile_xsize - read_xsize)); |
3526 | 155k | } |
3527 | | |
3528 | 387k | for (i_row = read_ysize; i_row < tile_ysize; i_row++) |
3529 | 372k | { |
3530 | 372k | _TIFFmemset(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize, 0, |
3531 | 372k | sizeof(uint32_t) * tile_xsize); |
3532 | 372k | } |
3533 | | |
3534 | 15.5k | return (ok); |
3535 | 193k | } |