Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/contrib/addtiffo/tif_overview.c

raw
1 /******************************************************************************
2 * tif_overview.c,v 1.9 2005/05/25 09:03:16 dron Exp
3 *
4 * Project: TIFF Overview Builder
5 * Purpose: Library function for building overviews in a TIFF file.
6 * Author: Frank Warmerdam, [email protected]
7 *
8 * Notes:
9 * o Currently only images with bits_per_sample of a multiple of eight
10 * will work.
11 *
12 * o The downsampler currently just takes the top left pixel from the
13 * source rectangle. Eventually sampling options of averaging, mode, and
14 * ``center pixel'' should be offered.
15 *
16 * o The code will attempt to use the same kind of compression,
17 * photometric interpretation, and organization as the source image, but
18 * it doesn't copy geotiff tags to the reduced resolution images.
19 *
20 * o Reduced resolution overviews for multi-sample files will currently
21 * always be generated as PLANARCONFIG_SEPARATE. This could be fixed
22 * reasonable easily if needed to improve compatibility with other
23 * packages. Many don't properly support PLANARCONFIG_SEPARATE.
24 *
25 ******************************************************************************
26 * Copyright (c) 1999, Frank Warmerdam
27 *
28 * Permission is hereby granted, free of charge, to any person obtaining a
29 * copy of this software and associated documentation files (the "Software"),
30 * to deal in the Software without restriction, including without limitation
31 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
32 * and/or sell copies of the Software, and to permit persons to whom the
33 * Software is furnished to do so, subject to the following conditions:
34 *
35 * The above copyright notice and this permission notice shall be included
36 * in all copies or substantial portions of the Software.
37 *
38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
39 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
41 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
43 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
44 * DEALINGS IN THE SOFTWARE.
45 ******************************************************************************
46 */
47
48 /* TODO: update notes in header above */
49
50 #include <assert.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54
55 #include "tif_ovrcache.h"
56 #include "tiffio.h"
57
58 #ifndef FALSE
59 #define FALSE 0
60 #define TRUE 1
61 #endif
62
63 #ifndef MAX
64 #define MIN(a, b) ((a < b) ? a : b)
65 #define MAX(a, b) ((a > b) ? a : b)
66 #endif
67
68 #define TIFF_DIR_MAX 65534
69
70 void TIFFBuildOverviews(TIFF *, int, int *, int, const char *,
71 int (*)(double, void *), void *);
72
73 /************************************************************************/
74 /* TIFF_WriteOverview() */
75 /* */
76 /* Create a new directory, without any image data for an overview. */
77 /* Returns offset of newly created overview directory, but the */
78 /* current directory is reset to be the one in used when this */
79 /* function is called. */
80 /************************************************************************/
81
82 uint32_t TIFF_WriteOverview(TIFF *hTIFF, uint32_t nXSize, uint32_t nYSize,
83 int nBitsPerPixel, int nPlanarConfig, int nSamples,
84 int nBlockXSize, int nBlockYSize, int bTiled,
85 int nCompressFlag, int nPhotometric,
86 int nSampleFormat, unsigned short *panRed,
87 unsigned short *panGreen, unsigned short *panBlue,
88 int bUseSubIFDs, int nHorSubsampling,
89 int nVerSubsampling)
90
91 {
92 toff_t nBaseDirOffset;
93 toff_t nOffset;
94 tdir_t iNumDir;
95
96 (void)bUseSubIFDs;
97
98 nBaseDirOffset = TIFFCurrentDirOffset(hTIFF);
99
100 TIFFCreateDirectory(hTIFF);
101
102 /* -------------------------------------------------------------------- */
103 /* Setup TIFF fields. */
104 /* -------------------------------------------------------------------- */
105 TIFFSetField(hTIFF, TIFFTAG_IMAGEWIDTH, nXSize);
106 TIFFSetField(hTIFF, TIFFTAG_IMAGELENGTH, nYSize);
107 if (nSamples == 1)
108 TIFFSetField(hTIFF, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
109 else
110 TIFFSetField(hTIFF, TIFFTAG_PLANARCONFIG, nPlanarConfig);
111
112 TIFFSetField(hTIFF, TIFFTAG_BITSPERSAMPLE, nBitsPerPixel);
113 TIFFSetField(hTIFF, TIFFTAG_SAMPLESPERPIXEL, nSamples);
114 TIFFSetField(hTIFF, TIFFTAG_COMPRESSION, nCompressFlag);
115 TIFFSetField(hTIFF, TIFFTAG_PHOTOMETRIC, nPhotometric);
116 TIFFSetField(hTIFF, TIFFTAG_SAMPLEFORMAT, nSampleFormat);
117
118 if (bTiled)
119 {
120 TIFFSetField(hTIFF, TIFFTAG_TILEWIDTH, nBlockXSize);
121 TIFFSetField(hTIFF, TIFFTAG_TILELENGTH, nBlockYSize);
122 }
123 else
124 TIFFSetField(hTIFF, TIFFTAG_ROWSPERSTRIP, nBlockYSize);
125
126 TIFFSetField(hTIFF, TIFFTAG_SUBFILETYPE, FILETYPE_REDUCEDIMAGE);
127
128 if (nPhotometric == PHOTOMETRIC_YCBCR || nPhotometric == PHOTOMETRIC_ITULAB)
129 {
130 TIFFSetField(hTIFF, TIFFTAG_YCBCRSUBSAMPLING, nHorSubsampling,
131 nVerSubsampling);
132 /* TODO: also write YCbCrPositioning and YCbCrCoefficients tag identical
133 * to source IFD */
134 }
135 /* TODO: add command-line parameter for selecting jpeg compression quality
136 * that gets ignored when compression isn't jpeg */
137
138 /* -------------------------------------------------------------------- */
139 /* Write color table if one is present. */
140 /* -------------------------------------------------------------------- */
141 if (panRed != NULL)
142 {
143 TIFFSetField(hTIFF, TIFFTAG_COLORMAP, panRed, panGreen, panBlue);
144 }
145
146 /* -------------------------------------------------------------------- */
147 /* Write directory, and return byte offset. */
148 /* -------------------------------------------------------------------- */
149 if (TIFFWriteCheck(hTIFF, bTiled, "TIFFBuildOverviews") == 0)
150 return 0;
151
152 if (!TIFFWriteDirectory(hTIFF))
153 return 0;
154 iNumDir = TIFFNumberOfDirectories(hTIFF);
155 if (iNumDir > TIFF_DIR_MAX)
156 {
157 TIFFErrorExt(TIFFClientdata(hTIFF), "TIFF_WriteOverview",
158 "File `%s' has too many directories.\n",
159 TIFFFileName(hTIFF));
160 exit(-1);
161 }
162 TIFFSetDirectory(hTIFF, (tdir_t)(iNumDir - 1));
163
164 nOffset = TIFFCurrentDirOffset(hTIFF);
165
166 TIFFSetSubDirectory(hTIFF, nBaseDirOffset);
167
168 return nOffset;
169 }
170
171 /************************************************************************/
172 /* TIFF_GetSourceSamples() */
173 /************************************************************************/
174
175 static void TIFF_GetSourceSamples(double *padfSamples, unsigned char *pabySrc,
176 int nPixelBytes, int nSampleFormat,
177 uint32_t nXSize, uint32_t nYSize,
178 int nPixelOffset, int nLineOffset)
179 {
180 uint32_t iXOff, iYOff;
181 int iSample;
182
183 iSample = 0;
184
185 for (iYOff = 0; iYOff < nYSize; iYOff++)
186 {
187 for (iXOff = 0; iXOff < nXSize; iXOff++)
188 {
189 unsigned char *pabyData;
190
191 pabyData = pabySrc + iYOff * nLineOffset + iXOff * nPixelOffset;
192
193 if (nSampleFormat == SAMPLEFORMAT_UINT && nPixelBytes == 1)
194 {
195 padfSamples[iSample++] = *pabyData;
196 }
197 else if (nSampleFormat == SAMPLEFORMAT_UINT && nPixelBytes == 2)
198 {
199 padfSamples[iSample++] = ((uint16_t *)pabyData)[0];
200 }
201 else if (nSampleFormat == SAMPLEFORMAT_UINT && nPixelBytes == 4)
202 {
203 padfSamples[iSample++] = ((uint32_t *)pabyData)[0];
204 }
205 else if (nSampleFormat == SAMPLEFORMAT_INT && nPixelBytes == 2)
206 {
207 padfSamples[iSample++] = ((int16_t *)pabyData)[0];
208 }
209 else if (nSampleFormat == SAMPLEFORMAT_INT && nPixelBytes == 32)
210 {
211 padfSamples[iSample++] = ((int32_t *)pabyData)[0];
212 }
213 else if (nSampleFormat == SAMPLEFORMAT_IEEEFP && nPixelBytes == 4)
214 {
215 padfSamples[iSample++] = ((float *)pabyData)[0];
216 }
217 else if (nSampleFormat == SAMPLEFORMAT_IEEEFP && nPixelBytes == 8)
218 {
219 padfSamples[iSample++] = ((double *)pabyData)[0];
220 }
221 }
222 }
223 }
224
225 /************************************************************************/
226 /* TIFF_SetSample() */
227 /************************************************************************/
228
229 static void TIFF_SetSample(unsigned char *pabyData, int nPixelBytes,
230 int nSampleFormat, double dfValue)
231
232 {
233 if (nSampleFormat == SAMPLEFORMAT_UINT && nPixelBytes == 1)
234 {
235 *pabyData = (unsigned char)MAX(0, MIN(255, dfValue));
236 }
237 else if (nSampleFormat == SAMPLEFORMAT_UINT && nPixelBytes == 2)
238 {
239 *((uint16_t *)pabyData) = (uint16_t)MAX(0, MIN(65535, dfValue));
240 }
241 else if (nSampleFormat == SAMPLEFORMAT_UINT && nPixelBytes == 4)
242 {
243 *((uint32_t *)pabyData) = (uint32_t)dfValue;
244 }
245 else if (nSampleFormat == SAMPLEFORMAT_INT && nPixelBytes == 2)
246 {
247 *((int16_t *)pabyData) = (int16_t)MAX(-32768, MIN(32767, dfValue));
248 }
249 else if (nSampleFormat == SAMPLEFORMAT_INT && nPixelBytes == 32)
250 {
251 *((int32_t *)pabyData) = (int32_t)dfValue;
252 }
253 else if (nSampleFormat == SAMPLEFORMAT_IEEEFP && nPixelBytes == 4)
254 {
255 *((float *)pabyData) = (float)dfValue;
256 }
257 else if (nSampleFormat == SAMPLEFORMAT_IEEEFP && nPixelBytes == 8)
258 {
259 *((double *)pabyData) = dfValue;
260 }
261 }
262
263 /************************************************************************/
264 /* TIFF_DownSample() */
265 /* */
266 /* Down sample a tile of full res data into a window of a tile */
267 /* of downsampled data. */
268 /************************************************************************/
269
270 static void TIFF_DownSample(unsigned char *pabySrcTile, uint32_t nBlockXSize,
271 uint32_t nBlockYSize, int nPixelSkewBits,
272 int nBitsPerPixel, unsigned char *pabyOTile,
273 uint32_t nOBlockXSize, uint32_t nOBlockYSize,
274 uint32_t nTXOff, uint32_t nTYOff, int nOMult,
275 int nSampleFormat, const char *pszResampling)
276
277 {
278 uint32_t i, j;
279 int k, nPixelBytes = (nBitsPerPixel) / 8;
280 int nPixelGroupBytes = (nBitsPerPixel + nPixelSkewBits) / 8;
281 unsigned char *pabySrc, *pabyDst;
282 double *padfSamples;
283 size_t tpadfSamples_size, padfSamples_size;
284
285 assert(nBitsPerPixel >= 8);
286
287 /* sizeof(double) * nOMult * nOMult */
288 tpadfSamples_size = nOMult * nOMult;
289 if ((nOMult != 0) && (tpadfSamples_size / nOMult == (size_t)nOMult))
290 {
291 padfSamples_size = tpadfSamples_size;
292 tpadfSamples_size = padfSamples_size * sizeof(double);
293 if ((tpadfSamples_size / padfSamples_size) == sizeof(double))
294 padfSamples_size = tpadfSamples_size;
295 else
296 padfSamples_size = 0;
297 }
298 else
299 {
300 padfSamples_size = 0;
301 }
302 if (padfSamples_size == 0)
303 {
304 /* TODO: This is an error condition */
305 return;
306 }
307 padfSamples = (double *)malloc(padfSamples_size);
308
309 /* ==================================================================== */
310 /* Loop over scanline chunks to process, establishing where the */
311 /* data is going. */
312 /* ==================================================================== */
313 for (j = 0; j * nOMult < nBlockYSize; j++)
314 {
315 if (j + nTYOff >= nOBlockYSize)
316 break;
317
318 pabyDst = pabyOTile + ((j + nTYOff) * nOBlockXSize + nTXOff) *
319 nPixelBytes * nPixelGroupBytes;
320
321 /* --------------------------------------------------------------------
322 */
323 /* Handler nearest resampling ... we don't even care about the */
324 /* data type, we just do a bytewise copy. */
325 /* --------------------------------------------------------------------
326 */
327 if (strncmp(pszResampling, "nearest", 4) == 0 ||
328 strncmp(pszResampling, "NEAR", 4) == 0)
329 {
330 pabySrc = pabySrcTile + j * nOMult * nBlockXSize * nPixelGroupBytes;
331
332 for (i = 0; i * nOMult < nBlockXSize; i++)
333 {
334 if (i + nTXOff >= nOBlockXSize)
335 break;
336
337 /*
338 * For now use simple subsampling, from the top left corner
339 * of the source block of pixels.
340 */
341
342 for (k = 0; k < nPixelBytes; k++)
343 pabyDst[k] = pabySrc[k];
344
345 pabyDst += nPixelBytes * nPixelGroupBytes;
346 pabySrc += nOMult * nPixelGroupBytes;
347 }
348 }
349
350 /* --------------------------------------------------------------------
351 */
352 /* Handle the case of averaging. For this we also have to */
353 /* handle each sample format we are concerned with. */
354 /* --------------------------------------------------------------------
355 */
356 else if (strncmp(pszResampling, "averag", 6) == 0 ||
357 strncmp(pszResampling, "AVERAG", 6) == 0)
358 {
359 pabySrc = pabySrcTile + j * nOMult * nBlockXSize * nPixelGroupBytes;
360
361 for (i = 0; i * nOMult < nBlockXSize; i++)
362 {
363 double dfTotal;
364 uint32_t nXSize, nYSize, iSample;
365
366 if (i + nTXOff >= nOBlockXSize)
367 break;
368
369 nXSize = MIN((uint32_t)nOMult, nBlockXSize - i);
370 nYSize = MIN((uint32_t)nOMult, nBlockYSize - j);
371
372 TIFF_GetSourceSamples(
373 padfSamples, pabySrc, nPixelBytes, nSampleFormat, nXSize,
374 nYSize, nPixelGroupBytes, nPixelGroupBytes * nBlockXSize);
375
376 dfTotal = 0;
377 for (iSample = 0; iSample < nXSize * nYSize; iSample++)
378 {
379 dfTotal += padfSamples[iSample];
380 }
381
382 TIFF_SetSample(pabyDst, nPixelBytes, nSampleFormat,
383 dfTotal / (nXSize * nYSize));
384
385 pabySrc += nOMult * nPixelGroupBytes;
386 pabyDst += nPixelBytes;
387 }
388 }
389 }
390
391 free(padfSamples);
392 }
393
394 /************************************************************************/
395 /* TIFF_DownSample_Subsampled() */
396 /************************************************************************/
397 static void TIFF_DownSample_Subsampled(
398 unsigned char *pabySrcTile, int nSample, uint32_t nBlockXSize,
399 uint32_t nBlockYSize, unsigned char *pabyOTile, uint32_t nOBlockXSize,
400 uint32_t nOBlockYSize, uint32_t nTXOff, uint32_t nTYOff, int nOMult,
401 const char *pszResampling, int nHorSubsampling, int nVerSubsampling)
402 {
403 /* TODO: test with variety of subsampling values, and incovinient tile/strip
404 * sizes */
405 int nSampleBlockSize;
406 int nSourceSampleRowSize;
407 int nDestSampleRowSize;
408 uint32_t nSourceX, nSourceY;
409 uint32_t nSourceXSec, nSourceYSec;
410 uint32_t nSourceXSecEnd, nSourceYSecEnd;
411 uint32_t nDestX, nDestY;
412 int nSampleOffsetInSampleBlock;
413 unsigned int nCummulator;
414 unsigned int nCummulatorCount;
415
416 nSampleBlockSize = nHorSubsampling * nVerSubsampling + 2;
417 nSourceSampleRowSize =
418 ((nBlockXSize + nHorSubsampling - 1) / nHorSubsampling) *
419 nSampleBlockSize;
420 nDestSampleRowSize =
421 ((nOBlockXSize + nHorSubsampling - 1) / nHorSubsampling) *
422 nSampleBlockSize;
423
424 if (strncmp(pszResampling, "nearest", 4) == 0 ||
425 strncmp(pszResampling, "NEAR", 4) == 0)
426 {
427 if (nSample == 0)
428 {
429 for (nSourceY = 0, nDestY = nTYOff; nSourceY < nBlockYSize;
430 nSourceY += nOMult, nDestY++)
431 {
432 if (nDestY >= nOBlockYSize)
433 break;
434
435 for (nSourceX = 0, nDestX = nTXOff; nSourceX < nBlockXSize;
436 nSourceX += nOMult, nDestX++)
437 {
438 if (nDestX >= nOBlockXSize)
439 break;
440
441 *(pabyOTile +
442 (nDestY / nVerSubsampling) * nDestSampleRowSize +
443 (nDestY % nVerSubsampling) * nHorSubsampling +
444 (nDestX / nHorSubsampling) * nSampleBlockSize +
445 (nDestX % nHorSubsampling)) =
446 *(pabySrcTile +
447 (nSourceY / nVerSubsampling) * nSourceSampleRowSize +
448 (nSourceY % nVerSubsampling) * nHorSubsampling +
449 (nSourceX / nHorSubsampling) * nSampleBlockSize +
450 (nSourceX % nHorSubsampling));
451 }
452 }
453 }
454 else
455 {
456 nSampleOffsetInSampleBlock =
457 nHorSubsampling * nVerSubsampling + nSample - 1;
458 for (nSourceY = 0, nDestY = (nTYOff / nVerSubsampling);
459 nSourceY < (nBlockYSize / nVerSubsampling);
460 nSourceY += nOMult, nDestY++)
461 {
462 if (nDestY * nVerSubsampling >= nOBlockYSize)
463 break;
464
465 for (nSourceX = 0, nDestX = (nTXOff / nHorSubsampling);
466 nSourceX < (nBlockXSize / nHorSubsampling);
467 nSourceX += nOMult, nDestX++)
468 {
469 if (nDestX * nHorSubsampling >= nOBlockXSize)
470 break;
471
472 *(pabyOTile + nDestY * nDestSampleRowSize +
473 nDestX * nSampleBlockSize + nSampleOffsetInSampleBlock) =
474 *(pabySrcTile + nSourceY * nSourceSampleRowSize +
475 nSourceX * nSampleBlockSize +
476 nSampleOffsetInSampleBlock);
477 }
478 }
479 }
480 }
481 else if (strncmp(pszResampling, "averag", 6) == 0 ||
482 strncmp(pszResampling, "AVERAG", 6) == 0)
483 {
484 if (nSample == 0)
485 {
486 for (nSourceY = 0, nDestY = nTYOff; nSourceY < nBlockYSize;
487 nSourceY += nOMult, nDestY++)
488 {
489 if (nDestY >= nOBlockYSize)
490 break;
491
492 for (nSourceX = 0, nDestX = nTXOff; nSourceX < nBlockXSize;
493 nSourceX += nOMult, nDestX++)
494 {
495 if (nDestX >= nOBlockXSize)
496 break;
497
498 nSourceXSecEnd = nSourceX + nOMult;
499 if (nSourceXSecEnd > nBlockXSize)
500 nSourceXSecEnd = nBlockXSize;
501 nSourceYSecEnd = nSourceY + nOMult;
502 if (nSourceYSecEnd > nBlockYSize)
503 nSourceYSecEnd = nBlockYSize;
504 nCummulator = 0;
505 for (nSourceYSec = nSourceY; nSourceYSec < nSourceYSecEnd;
506 nSourceYSec++)
507 {
508 for (nSourceXSec = nSourceX;
509 nSourceXSec < nSourceXSecEnd; nSourceXSec++)
510 {
511 nCummulator += *(pabySrcTile +
512 (nSourceYSec / nVerSubsampling) *
513 nSourceSampleRowSize +
514 (nSourceYSec % nVerSubsampling) *
515 nHorSubsampling +
516 (nSourceXSec / nHorSubsampling) *
517 nSampleBlockSize +
518 (nSourceXSec % nHorSubsampling));
519 }
520 }
521 nCummulatorCount = (nSourceXSecEnd - nSourceX) *
522 (nSourceYSecEnd - nSourceY);
523 *(pabyOTile +
524 (nDestY / nVerSubsampling) * nDestSampleRowSize +
525 (nDestY % nVerSubsampling) * nHorSubsampling +
526 (nDestX / nHorSubsampling) * nSampleBlockSize +
527 (nDestX % nHorSubsampling)) =
528 ((nCummulator + (nCummulatorCount >> 1)) /
529 nCummulatorCount);
530 }
531 }
532 }
533 else
534 {
535 nSampleOffsetInSampleBlock =
536 nHorSubsampling * nVerSubsampling + nSample - 1;
537 for (nSourceY = 0, nDestY = (nTYOff / nVerSubsampling);
538 nSourceY < (nBlockYSize / nVerSubsampling);
539 nSourceY += nOMult, nDestY++)
540 {
541 if (nDestY * nVerSubsampling >= nOBlockYSize)
542 break;
543
544 for (nSourceX = 0, nDestX = (nTXOff / nHorSubsampling);
545 nSourceX < (nBlockXSize / nHorSubsampling);
546 nSourceX += nOMult, nDestX++)
547 {
548 if (nDestX * nHorSubsampling >= nOBlockXSize)
549 break;
550
551 nSourceXSecEnd = nSourceX + nOMult;
552 if (nSourceXSecEnd > (nBlockXSize / nHorSubsampling))
553 nSourceXSecEnd = (nBlockXSize / nHorSubsampling);
554 nSourceYSecEnd = nSourceY + nOMult;
555 if (nSourceYSecEnd > (nBlockYSize / nVerSubsampling))
556 nSourceYSecEnd = (nBlockYSize / nVerSubsampling);
557 nCummulator = 0;
558 for (nSourceYSec = nSourceY; nSourceYSec < nSourceYSecEnd;
559 nSourceYSec++)
560 {
561 for (nSourceXSec = nSourceX;
562 nSourceXSec < nSourceXSecEnd; nSourceXSec++)
563 {
564 nCummulator +=
565 *(pabySrcTile +
566 nSourceYSec * nSourceSampleRowSize +
567 nSourceXSec * nSampleBlockSize +
568 nSampleOffsetInSampleBlock);
569 }
570 }
571 nCummulatorCount = (nSourceXSecEnd - nSourceX) *
572 (nSourceYSecEnd - nSourceY);
573 *(pabyOTile + nDestY * nDestSampleRowSize +
574 nDestX * nSampleBlockSize + nSampleOffsetInSampleBlock) =
575 ((nCummulator + (nCummulatorCount >> 1)) /
576 nCummulatorCount);
577 }
578 }
579 }
580 }
581 }
582
583 /************************************************************************/
584 /* TIFF_ProcessFullResBlock() */
585 /* */
586 /* Process one block of full res data, downsampling into each */
587 /* of the overviews. */
588 /************************************************************************/
589
590 void TIFF_ProcessFullResBlock(TIFF *hTIFF, int nPlanarConfig, int bSubsampled,
591 int nHorSubsampling, int nVerSubsampling,
592 int nOverviews, int *panOvList, int nBitsPerPixel,
593 int nSamples, TIFFOvrCache **papoRawBIs,
594 uint32_t nSXOff, uint32_t nSYOff,
595 unsigned char *pabySrcTile, uint32_t nBlockXSize,
596 uint32_t nBlockYSize, int nSampleFormat,
597 const char *pszResampling)
598
599 {
600 int iOverview, iSample;
601
602 for (iSample = 0; iSample < nSamples; iSample++)
603 {
604 /*
605 * We have to read a tile/strip for each sample for
606 * PLANARCONFIG_SEPARATE. Otherwise, we just read all the samples
607 * at once when handling the first sample.
608 */
609 if (nPlanarConfig == PLANARCONFIG_SEPARATE || iSample == 0)
610 {
611 if (TIFFIsTiled(hTIFF))
612 {
613 TIFFReadEncodedTile(hTIFF,
614 TIFFComputeTile(hTIFF, nSXOff, nSYOff, 0,
615 (tsample_t)iSample),
616 pabySrcTile, TIFFTileSize(hTIFF));
617 }
618 else
619 {
620 TIFFReadEncodedStrip(
621 hTIFF, TIFFComputeStrip(hTIFF, nSYOff, (tsample_t)iSample),
622 pabySrcTile, TIFFStripSize(hTIFF));
623 }
624 }
625
626 /*
627 * Loop over destination overview layers
628 */
629 for (iOverview = 0; iOverview < nOverviews; iOverview++)
630 {
631 TIFFOvrCache *poRBI = papoRawBIs[iOverview];
632 unsigned char *pabyOTile;
633 uint32_t nTXOff, nTYOff, nOXOff, nOYOff, nOMult;
634 uint32_t nOBlockXSize = poRBI->nBlockXSize;
635 uint32_t nOBlockYSize = poRBI->nBlockYSize;
636 int nSkewBits, nSampleByteOffset;
637
638 /*
639 * Fetch the destination overview tile
640 */
641 nOMult = panOvList[iOverview];
642 nOXOff = (nSXOff / nOMult) / nOBlockXSize;
643 nOYOff = (nSYOff / nOMult) / nOBlockYSize;
644
645 if (bSubsampled)
646 {
647 pabyOTile = TIFFGetOvrBlock_Subsampled(poRBI, nOXOff, nOYOff);
648
649 /*
650 * Establish the offset into this tile at which we should
651 * start placing data.
652 */
653 nTXOff = (nSXOff - nOXOff * nOMult * nOBlockXSize) / nOMult;
654 nTYOff = (nSYOff - nOYOff * nOMult * nOBlockYSize) / nOMult;
655
656 #ifdef DBMALLOC
657 malloc_chain_check(1);
658 #endif
659 TIFF_DownSample_Subsampled(
660 pabySrcTile, iSample, nBlockXSize, nBlockYSize, pabyOTile,
661 poRBI->nBlockXSize, poRBI->nBlockYSize, nTXOff, nTYOff,
662 nOMult, pszResampling, nHorSubsampling, nVerSubsampling);
663 #ifdef DBMALLOC
664 malloc_chain_check(1);
665 #endif
666 }
667 else
668 {
669
670 pabyOTile = TIFFGetOvrBlock(poRBI, nOXOff, nOYOff, iSample);
671
672 /*
673 * Establish the offset into this tile at which we should
674 * start placing data.
675 */
676 nTXOff = (nSXOff - nOXOff * nOMult * nOBlockXSize) / nOMult;
677 nTYOff = (nSYOff - nOYOff * nOMult * nOBlockYSize) / nOMult;
678
679 /*
680 * Figure out the skew (extra space between ``our samples'') and
681 * the byte offset to the first sample.
682 */
683 assert((nBitsPerPixel % 8) == 0);
684 if (nPlanarConfig == PLANARCONFIG_SEPARATE)
685 {
686 nSkewBits = 0;
687 nSampleByteOffset = 0;
688 }
689 else
690 {
691 nSkewBits = nBitsPerPixel * (nSamples - 1);
692 nSampleByteOffset = (nBitsPerPixel / 8) * iSample;
693 }
694
695 /*
696 * Perform the downsampling.
697 */
698 #ifdef DBMALLOC
699 malloc_chain_check(1);
700 #endif
701 TIFF_DownSample(pabySrcTile + nSampleByteOffset, nBlockXSize,
702 nBlockYSize, nSkewBits, nBitsPerPixel,
703 pabyOTile, poRBI->nBlockXSize,
704 poRBI->nBlockYSize, nTXOff, nTYOff, nOMult,
705 nSampleFormat, pszResampling);
706 #ifdef DBMALLOC
707 malloc_chain_check(1);
708 #endif
709 }
710 }
711 }
712 }
713
714 /************************************************************************/
715 /* TIFF_BuildOverviews() */
716 /* */
717 /* Build the requested list of overviews. Overviews are */
718 /* maintained in a bunch of temporary files and then these are */
719 /* written back to the TIFF file. Only one pass through the */
720 /* source TIFF file is made for any number of output */
721 /* overviews. */
722 /************************************************************************/
723
724 void TIFFBuildOverviews(TIFF *hTIFF, int nOverviews, int *panOvList,
725 int bUseSubIFDs, const char *pszResampleMethod,
726 int (*pfnProgress)(double, void *), void *pProgressData)
727
728 {
729 TIFFOvrCache **papoRawBIs;
730 uint32_t nXSize, nYSize, nBlockXSize, nBlockYSize;
731 uint16_t nBitsPerPixel, nPhotometric, nCompressFlag, nSamples,
732 nPlanarConfig, nSampleFormat;
733 int bSubsampled;
734 uint16_t nHorSubsampling, nVerSubsampling;
735 int bTiled, nSXOff, nSYOff, i;
736 unsigned char *pabySrcTile;
737 uint16_t *panRedMap, *panGreenMap, *panBlueMap;
738 TIFFErrorHandler pfnWarning;
739
740 (void)pfnProgress;
741 (void)pProgressData;
742
743 /* -------------------------------------------------------------------- */
744 /* Get the base raster size. */
745 /* -------------------------------------------------------------------- */
746 TIFFGetField(hTIFF, TIFFTAG_IMAGEWIDTH, &nXSize);
747 TIFFGetField(hTIFF, TIFFTAG_IMAGELENGTH, &nYSize);
748
749 TIFFGetField(hTIFF, TIFFTAG_BITSPERSAMPLE, &nBitsPerPixel);
750 /* TODO: nBitsPerPixel seems misnomer and may need renaming to
751 * nBitsPerSample */
752 TIFFGetField(hTIFF, TIFFTAG_SAMPLESPERPIXEL, &nSamples);
753 TIFFGetFieldDefaulted(hTIFF, TIFFTAG_PLANARCONFIG, &nPlanarConfig);
754
755 TIFFGetFieldDefaulted(hTIFF, TIFFTAG_PHOTOMETRIC, &nPhotometric);
756 TIFFGetFieldDefaulted(hTIFF, TIFFTAG_COMPRESSION, &nCompressFlag);
757 TIFFGetFieldDefaulted(hTIFF, TIFFTAG_SAMPLEFORMAT, &nSampleFormat);
758
759 if (nPhotometric == PHOTOMETRIC_YCBCR || nPhotometric == PHOTOMETRIC_ITULAB)
760 {
761 if (nBitsPerPixel != 8 || nSamples != 3 ||
762 nPlanarConfig != PLANARCONFIG_CONTIG ||
763 nSampleFormat != SAMPLEFORMAT_UINT)
764 {
765 /* TODO: use of TIFFError is inconsistent with use of fprintf in
766 * addtiffo.c, sort out */
767 TIFFErrorExt(
768 TIFFClientdata(hTIFF), "TIFFBuildOverviews",
769 "File `%s' has an unsupported subsampling configuration.\n",
770 TIFFFileName(hTIFF));
771 /* If you need support for this particular flavor, please contact
772 * either Frank Warmerdam [email protected] Joris Van Damme
773 * [email protected]
774 */
775 return;
776 }
777 bSubsampled = 1;
778 TIFFGetField(hTIFF, TIFFTAG_YCBCRSUBSAMPLING, &nHorSubsampling,
779 &nVerSubsampling);
780 /* TODO: find out if maybe TIFFGetFieldDefaulted is better choice for
781 * YCbCrSubsampling tag */
782 }
783 else
784 {
785 if (nBitsPerPixel < 8)
786 {
787 /* TODO: use of TIFFError is inconsistent with use of fprintf in
788 * addtiffo.c, sort out */
789 TIFFErrorExt(
790 TIFFClientdata(hTIFF), "TIFFBuildOverviews",
791 "File `%s' has samples of %d bits per sample. Sample\n"
792 "sizes of less than 8 bits per sample are not supported.\n",
793 TIFFFileName(hTIFF), nBitsPerPixel);
794 return;
795 }
796 bSubsampled = 0;
797 nHorSubsampling = 1;
798 nVerSubsampling = 1;
799 }
800
801 /* -------------------------------------------------------------------- */
802 /* Turn off warnings to avoid a lot of repeated warnings while */
803 /* rereading directories. */
804 /* -------------------------------------------------------------------- */
805 pfnWarning = TIFFSetWarningHandler(NULL);
806
807 /* -------------------------------------------------------------------- */
808 /* Get the base raster block size. */
809 /* -------------------------------------------------------------------- */
810 if (TIFFGetField(hTIFF, TIFFTAG_ROWSPERSTRIP, &(nBlockYSize)))
811 {
812 nBlockXSize = nXSize;
813 bTiled = FALSE;
814 }
815 else
816 {
817 TIFFGetField(hTIFF, TIFFTAG_TILEWIDTH, &nBlockXSize);
818 TIFFGetField(hTIFF, TIFFTAG_TILELENGTH, &nBlockYSize);
819 bTiled = TRUE;
820 }
821
822 /* -------------------------------------------------------------------- */
823 /* Capture the palette if there is one. */
824 /* -------------------------------------------------------------------- */
825 if (TIFFGetField(hTIFF, TIFFTAG_COLORMAP, &panRedMap, &panGreenMap,
826 &panBlueMap))
827 {
828 uint16_t *panRed2, *panGreen2, *panBlue2;
829 int nColorCount = 1 << nBitsPerPixel;
830
831 panRed2 = (uint16_t *)_TIFFmalloc(2 * nColorCount);
832 panGreen2 = (uint16_t *)_TIFFmalloc(2 * nColorCount);
833 panBlue2 = (uint16_t *)_TIFFmalloc(2 * nColorCount);
834
835 memcpy(panRed2, panRedMap, 2 * nColorCount);
836 memcpy(panGreen2, panGreenMap, 2 * nColorCount);
837 memcpy(panBlue2, panBlueMap, 2 * nColorCount);
838
839 panRedMap = panRed2;
840 panGreenMap = panGreen2;
841 panBlueMap = panBlue2;
842 }
843 else
844 {
845 panRedMap = panGreenMap = panBlueMap = NULL;
846 }
847
848 /* -------------------------------------------------------------------- */
849 /* Initialize overviews. */
850 /* -------------------------------------------------------------------- */
851 papoRawBIs = (TIFFOvrCache **)_TIFFmalloc(nOverviews * sizeof(void *));
852
853 for (i = 0; i < nOverviews; i++)
854 {
855 uint32_t nOXSize, nOYSize, nOBlockXSize, nOBlockYSize;
856 toff_t nDirOffset;
857
858 nOXSize = (nXSize + panOvList[i] - 1) / panOvList[i];
859 nOYSize = (nYSize + panOvList[i] - 1) / panOvList[i];
860
861 nOBlockXSize = MIN(nBlockXSize, nOXSize);
862 nOBlockYSize = MIN(nBlockYSize, nOYSize);
863
864 if (bTiled)
865 {
866 if ((nOBlockXSize % 16) != 0)
867 nOBlockXSize = nOBlockXSize + 16 - (nOBlockXSize % 16);
868
869 if ((nOBlockYSize % 16) != 0)
870 nOBlockYSize = nOBlockYSize + 16 - (nOBlockYSize % 16);
871 }
872
873 nDirOffset = TIFF_WriteOverview(
874 hTIFF, nOXSize, nOYSize, nBitsPerPixel, nPlanarConfig, nSamples,
875 nOBlockXSize, nOBlockYSize, bTiled, nCompressFlag, nPhotometric,
876 nSampleFormat, panRedMap, panGreenMap, panBlueMap, bUseSubIFDs,
877 nHorSubsampling, nVerSubsampling);
878
879 papoRawBIs[i] = TIFFCreateOvrCache(hTIFF, nDirOffset);
880 }
881
882 if (panRedMap != NULL)
883 {
884 _TIFFfree(panRedMap);
885 _TIFFfree(panGreenMap);
886 _TIFFfree(panBlueMap);
887 }
888
889 /* -------------------------------------------------------------------- */
890 /* Allocate a buffer to hold a source block. */
891 /* -------------------------------------------------------------------- */
892 if (bTiled)
893 pabySrcTile = (unsigned char *)_TIFFmalloc(TIFFTileSize(hTIFF));
894 else
895 pabySrcTile = (unsigned char *)_TIFFmalloc(TIFFStripSize(hTIFF));
896
897 /* -------------------------------------------------------------------- */
898 /* Loop over the source raster, applying data to the */
899 /* destination raster. */
900 /* -------------------------------------------------------------------- */
901 for (nSYOff = 0; nSYOff < (int)nYSize; nSYOff += nBlockYSize)
902 {
903 for (nSXOff = 0; nSXOff < (int)nXSize; nSXOff += nBlockXSize)
904 {
905 /*
906 * Read and resample into the various overview images.
907 */
908
909 TIFF_ProcessFullResBlock(
910 hTIFF, nPlanarConfig, bSubsampled, nHorSubsampling,
911 nVerSubsampling, nOverviews, panOvList, nBitsPerPixel, nSamples,
912 papoRawBIs, nSXOff, nSYOff, pabySrcTile, nBlockXSize,
913 nBlockYSize, nSampleFormat, pszResampleMethod);
914 }
915 }
916
917 _TIFFfree(pabySrcTile);
918
919 /* -------------------------------------------------------------------- */
920 /* Cleanup the rawblockedimage files. */
921 /* -------------------------------------------------------------------- */
922 for (i = 0; i < nOverviews; i++)
923 {
924 TIFFDestroyOvrCache(papoRawBIs[i]);
925 }
926
927 if (papoRawBIs != NULL)
928 _TIFFfree(papoRawBIs);
929
930 TIFFSetWarningHandler(pfnWarning);
931 }