Debian Codegraph @

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

raw
1 /******************************************************************************
2 * Project: TIFF Overview Builder
3 * Purpose: Library functions to maintain two rows of tiles or two strips
4 * of data for output overviews as an output cache.
5 * Author: Frank Warmerdam, [email protected]
6 *
7 ******************************************************************************
8 * Copyright (c) 2000, Frank Warmerdam
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 ******************************************************************************
28 */
29
30 #include "tif_ovrcache.h"
31 #include "tiffiop.h"
32 #include <assert.h>
33
34 /************************************************************************/
35 /* TIFFCreateOvrCache() */
36 /* */
37 /* Create an overview cache to hold two rows of blocks from an */
38 /* existing TIFF directory. */
39 /************************************************************************/
40
41 TIFFOvrCache *TIFFCreateOvrCache(TIFF *hTIFF, toff_t nDirOffset)
42
43 {
44 TIFFOvrCache *psCache;
45 toff_t nBaseDirOffset;
46 int nRet;
47
48 psCache = (TIFFOvrCache *)_TIFFmalloc(sizeof(TIFFOvrCache));
49 psCache->nDirOffset = nDirOffset;
50 psCache->hTIFF = hTIFF;
51
52 /* -------------------------------------------------------------------- */
53 /* Get definition of this raster from the TIFF file itself. */
54 /* -------------------------------------------------------------------- */
55 nBaseDirOffset = TIFFCurrentDirOffset(psCache->hTIFF);
56 nRet = TIFFSetSubDirectory(hTIFF, nDirOffset);
57 (void)nRet;
58 assert(nRet == 1);
59
60 TIFFGetField(hTIFF, TIFFTAG_IMAGEWIDTH, &(psCache->nXSize));
61 TIFFGetField(hTIFF, TIFFTAG_IMAGELENGTH, &(psCache->nYSize));
62
63 TIFFGetField(hTIFF, TIFFTAG_BITSPERSAMPLE, &(psCache->nBitsPerPixel));
64 TIFFGetField(hTIFF, TIFFTAG_SAMPLESPERPIXEL, &(psCache->nSamples));
65 TIFFGetField(hTIFF, TIFFTAG_PLANARCONFIG, &(psCache->nPlanarConfig));
66
67 if (!TIFFIsTiled(hTIFF))
68 {
69 TIFFGetField(hTIFF, TIFFTAG_ROWSPERSTRIP, &(psCache->nBlockYSize));
70 psCache->nBlockXSize = psCache->nXSize;
71 psCache->nBytesPerBlock = TIFFStripSize(hTIFF);
72 psCache->bTiled = FALSE;
73 }
74 else
75 {
76 TIFFGetField(hTIFF, TIFFTAG_TILEWIDTH, &(psCache->nBlockXSize));
77 TIFFGetField(hTIFF, TIFFTAG_TILELENGTH, &(psCache->nBlockYSize));
78 psCache->nBytesPerBlock = TIFFTileSize(hTIFF);
79 psCache->bTiled = TRUE;
80 }
81
82 /* -------------------------------------------------------------------- */
83 /* Compute some values from this. */
84 /* -------------------------------------------------------------------- */
85
86 psCache->nBlocksPerRow =
87 (psCache->nXSize + psCache->nBlockXSize - 1) / psCache->nBlockXSize;
88 psCache->nBlocksPerColumn =
89 (psCache->nYSize + psCache->nBlockYSize - 1) / psCache->nBlockYSize;
90
91 if (psCache->nPlanarConfig == PLANARCONFIG_SEPARATE)
92 psCache->nBytesPerRow = psCache->nBytesPerBlock *
93 psCache->nBlocksPerRow * psCache->nSamples;
94 else
95 psCache->nBytesPerRow =
96 psCache->nBytesPerBlock * psCache->nBlocksPerRow;
97
98 /* -------------------------------------------------------------------- */
99 /* Allocate and initialize the data buffers. */
100 /* -------------------------------------------------------------------- */
101
102 psCache->pabyRow1Blocks =
103 (unsigned char *)_TIFFmalloc(psCache->nBytesPerRow);
104 psCache->pabyRow2Blocks =
105 (unsigned char *)_TIFFmalloc(psCache->nBytesPerRow);
106
107 if (psCache->pabyRow1Blocks == NULL || psCache->pabyRow2Blocks == NULL)
108 {
109 TIFFErrorExt(hTIFF->tif_clientdata, hTIFF->tif_name,
110 "Can't allocate memory for overview cache.");
111 /* TODO: use of TIFFError is inconsistent with use of fprintf in
112 * addtiffo.c, sort out */
113 if (psCache->pabyRow1Blocks)
114 _TIFFfree(psCache->pabyRow1Blocks);
115 if (psCache->pabyRow2Blocks)
116 _TIFFfree(psCache->pabyRow2Blocks);
117 _TIFFfree(psCache);
118 return NULL;
119 }
120
121 _TIFFmemset(psCache->pabyRow1Blocks, 0, psCache->nBytesPerRow);
122 _TIFFmemset(psCache->pabyRow2Blocks, 0, psCache->nBytesPerRow);
123
124 psCache->nBlockOffset = 0;
125
126 nRet = TIFFSetSubDirectory(psCache->hTIFF, nBaseDirOffset);
127 (void)nRet;
128 assert(nRet == 1);
129
130 return psCache;
131 }
132
133 /************************************************************************/
134 /* TIFFWriteOvrRow() */
135 /* */
136 /* Write one entire row of blocks (row 1) to the tiff file, and */
137 /* then rotate the block buffers, essentially moving things */
138 /* down by one block. */
139 /************************************************************************/
140
141 static void TIFFWriteOvrRow(TIFFOvrCache *psCache)
142
143 {
144 int nRet, iTileX, iTileY = psCache->nBlockOffset;
145 unsigned char *pabyData;
146 toff_t nBaseDirOffset;
147 uint32_t RowsInStrip;
148
149 /* -------------------------------------------------------------------- */
150 /* If the output cache is multi-byte per sample, and the file */
151 /* being written to is of a different byte order than the current */
152 /* platform, we will need to byte swap the data. */
153 /* -------------------------------------------------------------------- */
154 if (TIFFIsByteSwapped(psCache->hTIFF))
155 {
156 if (psCache->nBitsPerPixel == 16)
157 TIFFSwabArrayOfShort((uint16_t *)psCache->pabyRow1Blocks,
158 (psCache->nBytesPerBlock * psCache->nSamples) /
159 2);
160
161 else if (psCache->nBitsPerPixel == 32)
162 TIFFSwabArrayOfLong((uint32_t *)psCache->pabyRow1Blocks,
163 (psCache->nBytesPerBlock * psCache->nSamples) /
164 4);
165
166 else if (psCache->nBitsPerPixel == 64)
167 TIFFSwabArrayOfDouble(
168 (double *)psCache->pabyRow1Blocks,
169 (psCache->nBytesPerBlock * psCache->nSamples) / 8);
170 }
171
172 /* -------------------------------------------------------------------- */
173 /* Record original directory position, so we can restore it at */
174 /* end. */
175 /* -------------------------------------------------------------------- */
176 nBaseDirOffset = TIFFCurrentDirOffset(psCache->hTIFF);
177 nRet = TIFFSetSubDirectory(psCache->hTIFF, psCache->nDirOffset);
178 (void)nRet;
179 assert(nRet == 1);
180
181 /* -------------------------------------------------------------------- */
182 /* Write blocks to TIFF file. */
183 /* -------------------------------------------------------------------- */
184 for (iTileX = 0; iTileX < psCache->nBlocksPerRow; iTileX++)
185 {
186 int nTileID;
187
188 if (psCache->nPlanarConfig == PLANARCONFIG_SEPARATE)
189 {
190 int iSample;
191
192 for (iSample = 0; iSample < psCache->nSamples; iSample++)
193 {
194 pabyData = TIFFGetOvrBlock(psCache, iTileX, iTileY, iSample);
195
196 if (psCache->bTiled)
197 {
198 nTileID = TIFFComputeTile(
199 psCache->hTIFF, iTileX * psCache->nBlockXSize,
200 iTileY * psCache->nBlockYSize, 0, (tsample_t)iSample);
201 if (TIFFWriteEncodedTile(psCache->hTIFF, nTileID, pabyData,
202 TIFFTileSize(psCache->hTIFF)) < 0)
203 {
204 fprintf(stderr, "TIFFWriteEncodedTile() failed\n");
205 }
206 }
207 else
208 {
209 nTileID = TIFFComputeStrip(psCache->hTIFF,
210 iTileY * psCache->nBlockYSize,
211 (tsample_t)iSample);
212 RowsInStrip = psCache->nBlockYSize;
213 if ((iTileY + 1) * psCache->nBlockYSize > psCache->nYSize)
214 RowsInStrip =
215 psCache->nYSize - iTileY * psCache->nBlockYSize;
216 if (TIFFWriteEncodedStrip(
217 psCache->hTIFF, nTileID, pabyData,
218 TIFFVStripSize(psCache->hTIFF, RowsInStrip)) < 0)
219 {
220 fprintf(stderr, "TIFFWriteEncodedStrip() failed\n");
221 }
222 }
223 }
224 }
225 else
226 {
227 pabyData = TIFFGetOvrBlock(psCache, iTileX, iTileY, 0);
228
229 if (psCache->bTiled)
230 {
231 nTileID = TIFFComputeTile(psCache->hTIFF,
232 iTileX * psCache->nBlockXSize,
233 iTileY * psCache->nBlockYSize, 0, 0);
234 if (TIFFWriteEncodedTile(psCache->hTIFF, nTileID, pabyData,
235 TIFFTileSize(psCache->hTIFF)) < 0)
236 {
237 fprintf(stderr, "TIFFWriteEncodedTile() failed\n");
238 }
239 }
240 else
241 {
242 nTileID = TIFFComputeStrip(psCache->hTIFF,
243 iTileY * psCache->nBlockYSize, 0);
244 RowsInStrip = psCache->nBlockYSize;
245 if ((iTileY + 1) * psCache->nBlockYSize > psCache->nYSize)
246 RowsInStrip =
247 psCache->nYSize - iTileY * psCache->nBlockYSize;
248 if (TIFFWriteEncodedStrip(
249 psCache->hTIFF, nTileID, pabyData,
250 TIFFVStripSize(psCache->hTIFF, RowsInStrip)) < 0)
251 {
252 fprintf(stderr, "TIFFWriteEncodedStrip() failed\n");
253 }
254 }
255 }
256 }
257
258 /* -------------------------------------------------------------------- */
259 /* Rotate buffers. */
260 /* -------------------------------------------------------------------- */
261 pabyData = psCache->pabyRow1Blocks;
262 psCache->pabyRow1Blocks = psCache->pabyRow2Blocks;
263 psCache->pabyRow2Blocks = pabyData;
264
265 _TIFFmemset(pabyData, 0, psCache->nBytesPerRow);
266
267 psCache->nBlockOffset++;
268
269 /* -------------------------------------------------------------------- */
270 /* Restore access to original directory. */
271 /* -------------------------------------------------------------------- */
272 TIFFFlush(psCache->hTIFF);
273 /* TODO: add checks on error status return of TIFFFlush */
274 TIFFSetSubDirectory(psCache->hTIFF, nBaseDirOffset);
275 /* TODO: add checks on error status return of TIFFSetSubDirectory */
276 }
277
278 /************************************************************************/
279 /* TIFFGetOvrBlock() */
280 /************************************************************************/
281
282 /* TODO: make TIFF_Downsample handle iSample offset, so that we can
283 * do with a single TIFFGetOvrBlock and no longer need
284 * TIFFGetOvrBlock_Subsampled */
285 unsigned char *TIFFGetOvrBlock(TIFFOvrCache *psCache, int iTileX, int iTileY,
286 int iSample)
287
288 {
289 long nRowOffset;
290
291 if (iTileY > psCache->nBlockOffset + 1)
292 TIFFWriteOvrRow(psCache);
293
294 assert(iTileX >= 0 && iTileX < psCache->nBlocksPerRow);
295 assert(iTileY >= 0 && iTileY < psCache->nBlocksPerColumn);
296 assert(iTileY >= psCache->nBlockOffset &&
297 iTileY < psCache->nBlockOffset + 2);
298 assert(iSample >= 0 && iSample < psCache->nSamples);
299
300 if (psCache->nPlanarConfig == PLANARCONFIG_SEPARATE)
301 nRowOffset = ((((toff_t)iTileX * psCache->nSamples) + iSample) *
302 psCache->nBytesPerBlock);
303 else
304 nRowOffset = iTileX * psCache->nBytesPerBlock +
305 (psCache->nBitsPerPixel + 7) / 8 * iSample;
306
307 if (iTileY == psCache->nBlockOffset)
308 return psCache->pabyRow1Blocks + nRowOffset;
309 else
310 return psCache->pabyRow2Blocks + nRowOffset;
311 }
312
313 /************************************************************************/
314 /* TIFFGetOvrBlock_Subsampled() */
315 /************************************************************************/
316
317 unsigned char *TIFFGetOvrBlock_Subsampled(TIFFOvrCache *psCache, int iTileX,
318 int iTileY)
319
320 {
321 int nRowOffset;
322
323 if (iTileY > psCache->nBlockOffset + 1)
324 TIFFWriteOvrRow(psCache);
325
326 assert(iTileX >= 0 && iTileX < psCache->nBlocksPerRow);
327 assert(iTileY >= 0 && iTileY < psCache->nBlocksPerColumn);
328 assert(iTileY >= psCache->nBlockOffset &&
329 iTileY < psCache->nBlockOffset + 2);
330 assert(psCache->nPlanarConfig != PLANARCONFIG_SEPARATE);
331
332 nRowOffset = iTileX * psCache->nBytesPerBlock;
333
334 if (iTileY == psCache->nBlockOffset)
335 return psCache->pabyRow1Blocks + nRowOffset;
336 else
337 return psCache->pabyRow2Blocks + nRowOffset;
338 }
339
340 /************************************************************************/
341 /* TIFFDestroyOvrCache() */
342 /************************************************************************/
343
344 void TIFFDestroyOvrCache(TIFFOvrCache *psCache)
345
346 {
347 while (psCache->nBlockOffset < psCache->nBlocksPerColumn)
348 TIFFWriteOvrRow(psCache);
349
350 _TIFFfree(psCache->pabyRow1Blocks);
351 _TIFFfree(psCache->pabyRow2Blocks);
352 _TIFFfree(psCache);
353 }