/src/gdal/frmts/gtiff/gtiffrasterband.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: GeoTIFF Driver |
4 | | * Purpose: General methods of GTiffRasterBand |
5 | | * Author: Frank Warmerdam, warmerdam@pobox.com |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 1998, 2002, Frank Warmerdam <warmerdam@pobox.com> |
9 | | * Copyright (c) 2007-2015, Even Rouault <even dot rouault at spatialys dot com> |
10 | | * |
11 | | * SPDX-License-Identifier: MIT |
12 | | ****************************************************************************/ |
13 | | |
14 | | #include "gtiffrasterband.h" |
15 | | #include "gtiffdataset.h" |
16 | | |
17 | | #include <algorithm> |
18 | | #include <set> |
19 | | |
20 | | #include "gdal_priv.h" |
21 | | #include "cpl_vsi_virtual.h" |
22 | | #include "tifvsi.h" |
23 | | |
24 | | /************************************************************************/ |
25 | | /* GTiffRasterBand() */ |
26 | | /************************************************************************/ |
27 | | |
28 | | GTiffRasterBand::GTiffRasterBand(GTiffDataset *poDSIn, int nBandIn) |
29 | 77.8M | : m_poGDS(poDSIn) |
30 | 77.8M | { |
31 | 77.8M | poDS = poDSIn; |
32 | 77.8M | nBand = nBandIn; |
33 | | |
34 | | /* -------------------------------------------------------------------- */ |
35 | | /* Get the GDAL data type. */ |
36 | | /* -------------------------------------------------------------------- */ |
37 | 77.8M | const uint16_t nBitsPerSample = m_poGDS->m_nBitsPerSample; |
38 | 77.8M | const uint16_t nSampleFormat = m_poGDS->m_nSampleFormat; |
39 | | |
40 | 77.8M | eDataType = GDT_Unknown; |
41 | | |
42 | 77.8M | if (nBitsPerSample <= 8) |
43 | 63.9M | { |
44 | 63.9M | if (nSampleFormat == SAMPLEFORMAT_INT) |
45 | 6.54M | eDataType = GDT_Int8; |
46 | 57.4M | else |
47 | 57.4M | eDataType = GDT_UInt8; |
48 | 63.9M | } |
49 | 13.9M | else if (nBitsPerSample <= 16) |
50 | 4.63M | { |
51 | 4.63M | if (nBitsPerSample == 16 && nSampleFormat == SAMPLEFORMAT_IEEEFP) |
52 | 1.03M | eDataType = GDT_Float16; |
53 | 3.59M | else if (nSampleFormat == SAMPLEFORMAT_INT) |
54 | 542k | eDataType = GDT_Int16; |
55 | 3.05M | else |
56 | 3.05M | eDataType = GDT_UInt16; |
57 | 4.63M | } |
58 | 9.29M | else if (nBitsPerSample == 32) |
59 | 1.90M | { |
60 | 1.90M | if (nSampleFormat == SAMPLEFORMAT_COMPLEXINT) |
61 | 128k | eDataType = GDT_CInt16; |
62 | 1.77M | else if (nSampleFormat == SAMPLEFORMAT_COMPLEXIEEEFP) |
63 | 53.1k | eDataType = GDT_CFloat16; |
64 | 1.72M | else if (nSampleFormat == SAMPLEFORMAT_IEEEFP) |
65 | 229k | eDataType = GDT_Float32; |
66 | 1.49M | else if (nSampleFormat == SAMPLEFORMAT_INT) |
67 | 324k | eDataType = GDT_Int32; |
68 | 1.16M | else |
69 | 1.16M | eDataType = GDT_UInt32; |
70 | 1.90M | } |
71 | 7.39M | else if (nBitsPerSample == 64) |
72 | 3.02M | { |
73 | 3.02M | if (nSampleFormat == SAMPLEFORMAT_IEEEFP) |
74 | 243k | eDataType = GDT_Float64; |
75 | 2.78M | else if (nSampleFormat == SAMPLEFORMAT_COMPLEXIEEEFP) |
76 | 70.4k | eDataType = GDT_CFloat32; |
77 | 2.71M | else if (nSampleFormat == SAMPLEFORMAT_COMPLEXINT) |
78 | 349k | eDataType = GDT_CInt32; |
79 | 2.36M | else if (nSampleFormat == SAMPLEFORMAT_INT) |
80 | 181k | eDataType = GDT_Int64; |
81 | 2.18M | else |
82 | 2.18M | eDataType = GDT_UInt64; |
83 | 3.02M | } |
84 | 4.36M | else if (nBitsPerSample == 128) |
85 | 404k | { |
86 | 404k | if (nSampleFormat == SAMPLEFORMAT_COMPLEXIEEEFP) |
87 | 220k | eDataType = GDT_CFloat64; |
88 | 404k | } |
89 | | |
90 | | /* -------------------------------------------------------------------- */ |
91 | | /* Try to work out band color interpretation. */ |
92 | | /* -------------------------------------------------------------------- */ |
93 | 77.8M | bool bLookForExtraSamples = false; |
94 | | |
95 | 77.8M | if (m_poGDS->m_poColorTable != nullptr && nBand == 1) |
96 | 4.42k | { |
97 | 4.42k | m_eBandInterp = GCI_PaletteIndex; |
98 | 4.42k | } |
99 | 77.8M | else if (m_poGDS->m_nPhotometric == PHOTOMETRIC_RGB || |
100 | 77.5M | (m_poGDS->m_nPhotometric == PHOTOMETRIC_YCBCR && |
101 | 826k | m_poGDS->m_nCompression == COMPRESSION_JPEG && |
102 | 740k | CPLTestBool(CPLGetConfigOption("CONVERT_YCBCR_TO_RGB", "YES")))) |
103 | 1.11M | { |
104 | 1.11M | if (nBand == 1) |
105 | 22.2k | m_eBandInterp = GCI_RedBand; |
106 | 1.09M | else if (nBand == 2) |
107 | 20.8k | m_eBandInterp = GCI_GreenBand; |
108 | 1.07M | else if (nBand == 3) |
109 | 20.8k | m_eBandInterp = GCI_BlueBand; |
110 | 1.05M | else |
111 | 1.05M | bLookForExtraSamples = true; |
112 | 1.11M | } |
113 | 76.7M | else if (m_poGDS->m_nPhotometric == PHOTOMETRIC_YCBCR) |
114 | 85.4k | { |
115 | 85.4k | if (nBand == 1) |
116 | 16.4k | m_eBandInterp = GCI_YCbCr_YBand; |
117 | 69.0k | else if (nBand == 2) |
118 | 16.3k | m_eBandInterp = GCI_YCbCr_CbBand; |
119 | 52.6k | else if (nBand == 3) |
120 | 16.3k | m_eBandInterp = GCI_YCbCr_CrBand; |
121 | 36.2k | else |
122 | 36.2k | bLookForExtraSamples = true; |
123 | 85.4k | } |
124 | 76.6M | else if (m_poGDS->m_nPhotometric == PHOTOMETRIC_SEPARATED) |
125 | 2.39M | { |
126 | 2.39M | if (nBand == 1) |
127 | 2.87k | m_eBandInterp = GCI_CyanBand; |
128 | 2.39M | else if (nBand == 2) |
129 | 2.40k | m_eBandInterp = GCI_MagentaBand; |
130 | 2.38M | else if (nBand == 3) |
131 | 2.32k | m_eBandInterp = GCI_YellowBand; |
132 | 2.38M | else if (nBand == 4) |
133 | 2.19k | m_eBandInterp = GCI_BlackBand; |
134 | 2.38M | else |
135 | 2.38M | bLookForExtraSamples = true; |
136 | 2.39M | } |
137 | 74.2M | else if (m_poGDS->m_nPhotometric == PHOTOMETRIC_MINISBLACK && nBand == 1) |
138 | 158k | { |
139 | 158k | m_eBandInterp = GCI_GrayIndex; |
140 | 158k | } |
141 | 74.1M | else |
142 | 74.1M | { |
143 | 74.1M | bLookForExtraSamples = true; |
144 | 74.1M | } |
145 | | |
146 | 77.8M | if (bLookForExtraSamples) |
147 | 77.5M | { |
148 | 77.5M | uint16_t *v = nullptr; |
149 | 77.5M | uint16_t count = 0; |
150 | | |
151 | 77.5M | if (TIFFGetField(m_poGDS->m_hTIFF, TIFFTAG_EXTRASAMPLES, &count, &v)) |
152 | 3.25M | { |
153 | 3.25M | const int nBaseSamples = m_poGDS->m_nSamplesPerPixel - count; |
154 | 3.25M | const int nExpectedBaseSamples = |
155 | 3.25M | (m_poGDS->m_nPhotometric == PHOTOMETRIC_MINISBLACK) ? 1 |
156 | 3.25M | : (m_poGDS->m_nPhotometric == PHOTOMETRIC_MINISWHITE) ? 1 |
157 | 1.94M | : (m_poGDS->m_nPhotometric == PHOTOMETRIC_RGB) ? 3 |
158 | 1.52M | : (m_poGDS->m_nPhotometric == PHOTOMETRIC_YCBCR) ? 3 |
159 | 1.38M | : (m_poGDS->m_nPhotometric == PHOTOMETRIC_SEPARATED) ? 4 |
160 | 909k | : 0; |
161 | | |
162 | 3.25M | if (nExpectedBaseSamples > 0 && nBand == nExpectedBaseSamples + 1 && |
163 | 3.24k | nBaseSamples != nExpectedBaseSamples) |
164 | 87 | { |
165 | 87 | ReportError( |
166 | 87 | CE_Warning, CPLE_AppDefined, |
167 | 87 | "Wrong number of ExtraSamples : %d. %d were expected", |
168 | 87 | count, m_poGDS->m_nSamplesPerPixel - nExpectedBaseSamples); |
169 | 87 | } |
170 | | |
171 | 3.25M | if (nBand > nBaseSamples && nBand - nBaseSamples - 1 < count && |
172 | 2.82M | (v[nBand - nBaseSamples - 1] == EXTRASAMPLE_ASSOCALPHA || |
173 | 2.82M | v[nBand - nBaseSamples - 1] == EXTRASAMPLE_UNASSALPHA)) |
174 | 5.88k | { |
175 | 5.88k | if (v[nBand - nBaseSamples - 1] == EXTRASAMPLE_ASSOCALPHA) |
176 | 1.65k | m_oGTiffMDMD.SetMetadataItem("ALPHA", "PREMULTIPLIED", |
177 | 1.65k | "IMAGE_STRUCTURE"); |
178 | 5.88k | m_eBandInterp = GCI_AlphaBand; |
179 | 5.88k | } |
180 | 3.25M | else |
181 | 3.25M | m_eBandInterp = GCI_Undefined; |
182 | 3.25M | } |
183 | 74.3M | else |
184 | 74.3M | { |
185 | 74.3M | m_eBandInterp = GCI_Undefined; |
186 | 74.3M | } |
187 | 77.5M | } |
188 | | |
189 | | /* -------------------------------------------------------------------- */ |
190 | | /* Establish block size for strip or tiles. */ |
191 | | /* -------------------------------------------------------------------- */ |
192 | 77.8M | nBlockXSize = m_poGDS->m_nBlockXSize; |
193 | 77.8M | nBlockYSize = m_poGDS->m_nBlockYSize; |
194 | 77.8M | nRasterXSize = m_poGDS->nRasterXSize; |
195 | 77.8M | nRasterYSize = m_poGDS->nRasterYSize; |
196 | 77.8M | nBlocksPerRow = DIV_ROUND_UP(nRasterXSize, nBlockXSize); |
197 | 77.8M | nBlocksPerColumn = DIV_ROUND_UP(nRasterYSize, nBlockYSize); |
198 | 77.8M | } |
199 | | |
200 | | /************************************************************************/ |
201 | | /* ~GTiffRasterBand() */ |
202 | | /************************************************************************/ |
203 | | |
204 | | GTiffRasterBand::~GTiffRasterBand() |
205 | 77.8M | { |
206 | | // So that any future DropReferenceVirtualMem() will not try to access the |
207 | | // raster band object, but this would not conform to the advertised |
208 | | // contract. |
209 | 77.8M | if (!m_aSetPSelf.empty()) |
210 | 0 | { |
211 | 0 | ReportError(CE_Warning, CPLE_AppDefined, |
212 | 0 | "Virtual memory objects still exist at GTiffRasterBand " |
213 | 0 | "destruction"); |
214 | 0 | std::set<GTiffRasterBand **>::iterator oIter = m_aSetPSelf.begin(); |
215 | 0 | for (; oIter != m_aSetPSelf.end(); ++oIter) |
216 | 0 | *(*oIter) = nullptr; |
217 | 0 | } |
218 | 77.8M | } |
219 | | |
220 | | /************************************************************************/ |
221 | | /* IRasterIO() */ |
222 | | /************************************************************************/ |
223 | | |
224 | | CPLErr GTiffRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, |
225 | | int nXSize, int nYSize, void *pData, |
226 | | int nBufXSize, int nBufYSize, |
227 | | GDALDataType eBufType, GSpacing nPixelSpace, |
228 | | GSpacing nLineSpace, |
229 | | GDALRasterIOExtraArg *psExtraArg) |
230 | 2.20M | { |
231 | | #if DEBUG_VERBOSE |
232 | | CPLDebug("GTiff", "RasterIO(%d, %d, %d, %d, %d, %d)", nXOff, nYOff, nXSize, |
233 | | nYSize, nBufXSize, nBufYSize); |
234 | | #endif |
235 | | |
236 | | // Try to pass the request to the most appropriate overview dataset. |
237 | 2.20M | if (nBufXSize < nXSize && nBufYSize < nYSize) |
238 | 41.9k | { |
239 | 41.9k | int bTried = FALSE; |
240 | 41.9k | if (psExtraArg->eResampleAlg == GRIORA_NearestNeighbour) |
241 | 41.8k | ++m_poGDS->m_nJPEGOverviewVisibilityCounter; |
242 | 41.9k | const CPLErr eErr = TryOverviewRasterIO( |
243 | 41.9k | eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize, |
244 | 41.9k | eBufType, nPixelSpace, nLineSpace, psExtraArg, &bTried); |
245 | 41.9k | if (psExtraArg->eResampleAlg == GRIORA_NearestNeighbour) |
246 | 41.8k | --m_poGDS->m_nJPEGOverviewVisibilityCounter; |
247 | 41.9k | if (bTried) |
248 | 275 | return eErr; |
249 | 41.9k | } |
250 | | |
251 | 2.20M | if (m_poGDS->m_eVirtualMemIOUsage != GTiffDataset::VirtualMemIOEnum::NO) |
252 | 0 | { |
253 | 0 | const int nErr = m_poGDS->VirtualMemIO( |
254 | 0 | eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize, |
255 | 0 | eBufType, 1, &nBand, nPixelSpace, nLineSpace, 0, psExtraArg); |
256 | 0 | if (nErr >= 0) |
257 | 0 | return static_cast<CPLErr>(nErr); |
258 | 0 | } |
259 | 2.20M | if (m_poGDS->m_bDirectIO) |
260 | 0 | { |
261 | 0 | int nErr = |
262 | 0 | DirectIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, |
263 | 0 | nBufYSize, eBufType, nPixelSpace, nLineSpace, psExtraArg); |
264 | 0 | if (nErr >= 0) |
265 | 0 | return static_cast<CPLErr>(nErr); |
266 | 0 | } |
267 | | |
268 | 2.20M | bool bCanUseMultiThreadedRead = false; |
269 | 2.20M | if (m_poGDS->m_nDisableMultiThreadedRead == 0 && eRWFlag == GF_Read && |
270 | 2.17M | m_poGDS->m_poThreadPool != nullptr && nXSize == nBufXSize && |
271 | 0 | nYSize == nBufYSize && m_poGDS->IsMultiThreadedReadCompatible()) |
272 | 0 | { |
273 | 0 | const int nBlockX1 = nXOff / nBlockXSize; |
274 | 0 | const int nBlockY1 = nYOff / nBlockYSize; |
275 | 0 | const int nBlockX2 = (nXOff + nXSize - 1) / nBlockXSize; |
276 | 0 | const int nBlockY2 = (nYOff + nYSize - 1) / nBlockYSize; |
277 | 0 | const int nXBlocks = nBlockX2 - nBlockX1 + 1; |
278 | 0 | const int nYBlocks = nBlockY2 - nBlockY1 + 1; |
279 | 0 | if (nXBlocks > 1 || nYBlocks > 1) |
280 | 0 | { |
281 | 0 | bCanUseMultiThreadedRead = true; |
282 | 0 | } |
283 | 0 | } |
284 | | |
285 | | // Cleanup data cached by below CacheMultiRange() call. |
286 | 2.20M | struct BufferedDataFreer |
287 | 2.20M | { |
288 | 2.20M | void *m_pBufferedData = nullptr; |
289 | 2.20M | TIFF *m_hTIFF = nullptr; |
290 | | |
291 | 2.20M | void Init(void *pBufferedData, TIFF *hTIFF) |
292 | 2.20M | { |
293 | 0 | m_pBufferedData = pBufferedData; |
294 | 0 | m_hTIFF = hTIFF; |
295 | 0 | } |
296 | | |
297 | 2.20M | ~BufferedDataFreer() |
298 | 2.20M | { |
299 | 2.20M | if (m_pBufferedData) |
300 | 0 | { |
301 | 0 | VSIFree(m_pBufferedData); |
302 | 0 | VSI_TIFFSetCachedRanges(TIFFClientdata(m_hTIFF), 0, nullptr, |
303 | 0 | nullptr, nullptr); |
304 | 0 | } |
305 | 2.20M | } |
306 | 2.20M | }; |
307 | | |
308 | | // bufferedDataFreer must be left in this scope ! |
309 | 2.20M | BufferedDataFreer bufferedDataFreer; |
310 | | |
311 | 2.20M | if (m_poGDS->eAccess == GA_ReadOnly && eRWFlag == GF_Read && |
312 | 2.17M | m_poGDS->HasOptimizedReadMultiRange()) |
313 | 0 | { |
314 | 0 | if (bCanUseMultiThreadedRead && |
315 | 0 | VSI_TIFFGetVSILFile(TIFFClientdata(m_poGDS->m_hTIFF))->HasPRead()) |
316 | 0 | { |
317 | | // use the multi-threaded implementation rather than the multi-range |
318 | | // one |
319 | 0 | } |
320 | 0 | else |
321 | 0 | { |
322 | 0 | bCanUseMultiThreadedRead = false; |
323 | 0 | GTiffDataset *poDSForCache = m_poGDS; |
324 | 0 | int nBandForCache = nBand; |
325 | 0 | if (!m_poGDS->m_bStreamingIn && m_poGDS->m_bBlockOrderRowMajor && |
326 | 0 | m_poGDS->m_bLeaderSizeAsUInt4 && |
327 | 0 | m_poGDS->m_bMaskInterleavedWithImagery && |
328 | 0 | m_poGDS->m_poImageryDS) |
329 | 0 | { |
330 | 0 | poDSForCache = m_poGDS->m_poImageryDS; |
331 | 0 | nBandForCache = 1; |
332 | 0 | } |
333 | 0 | bufferedDataFreer.Init( |
334 | 0 | poDSForCache->CacheMultiRange(nXOff, nYOff, nXSize, nYSize, |
335 | 0 | nBufXSize, nBufYSize, |
336 | 0 | &nBandForCache, 1, psExtraArg), |
337 | 0 | poDSForCache->m_hTIFF); |
338 | 0 | } |
339 | 0 | } |
340 | | |
341 | 2.20M | if (eRWFlag == GF_Read && nXSize == nBufXSize && nYSize == nBufYSize) |
342 | 2.13M | { |
343 | 2.13M | const int nBlockX1 = nXOff / nBlockXSize; |
344 | 2.13M | const int nBlockY1 = nYOff / nBlockYSize; |
345 | 2.13M | const int nBlockX2 = (nXOff + nXSize - 1) / nBlockXSize; |
346 | 2.13M | const int nBlockY2 = (nYOff + nYSize - 1) / nBlockYSize; |
347 | 2.13M | const int nXBlocks = nBlockX2 - nBlockX1 + 1; |
348 | 2.13M | const int nYBlocks = nBlockY2 - nBlockY1 + 1; |
349 | | |
350 | 2.13M | if (bCanUseMultiThreadedRead) |
351 | 0 | { |
352 | 0 | return m_poGDS->MultiThreadedRead(nXOff, nYOff, nXSize, nYSize, |
353 | 0 | pData, eBufType, 1, &nBand, |
354 | 0 | nPixelSpace, nLineSpace, 0); |
355 | 0 | } |
356 | 2.13M | else if (m_poGDS->nBands != 1 && |
357 | 1.58M | m_poGDS->m_nPlanarConfig == PLANARCONFIG_CONTIG) |
358 | 1.12M | { |
359 | 1.12M | const GIntBig nRequiredMem = |
360 | 1.12M | static_cast<GIntBig>(m_poGDS->nBands) * nXBlocks * nYBlocks * |
361 | 1.12M | nBlockXSize * nBlockYSize * GDALGetDataTypeSizeBytes(eDataType); |
362 | 1.12M | if (nRequiredMem > GDALGetCacheMax64()) |
363 | 0 | { |
364 | 0 | if (!m_poGDS->m_bHasWarnedDisableAggressiveBandCaching) |
365 | 0 | { |
366 | 0 | CPLDebug("GTiff", |
367 | 0 | "Disable aggressive band caching. " |
368 | 0 | "Cache not big enough. " |
369 | 0 | "At least " CPL_FRMT_GIB " bytes necessary", |
370 | 0 | nRequiredMem); |
371 | 0 | m_poGDS->m_bHasWarnedDisableAggressiveBandCaching = true; |
372 | 0 | } |
373 | 0 | m_poGDS->m_bLoadingOtherBands = true; |
374 | 0 | } |
375 | 1.12M | } |
376 | 2.13M | } |
377 | | |
378 | | // Write optimization when writing whole blocks, by-passing the block cache. |
379 | | // We require the block cache to be non instantiated to simplify things |
380 | | // (otherwise we might need to evict corresponding existing blocks from the |
381 | | // block cache). |
382 | 67.1k | else if (eRWFlag == GF_Write && |
383 | | // Could be extended to "odd bit" case, but more work |
384 | 23.6k | m_poGDS->m_nBitsPerSample == GDALGetDataTypeSizeBits(eDataType) && |
385 | 14.8k | nXSize == nBufXSize && nYSize == nBufYSize && !HasBlockCache() && |
386 | 1.05k | !m_poGDS->m_bLoadedBlockDirty && |
387 | 1.05k | (m_poGDS->nBands == 1 || |
388 | 900 | m_poGDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE) && |
389 | 1.05k | !m_poGDS->m_bLeaderSizeAsUInt4 && (nXOff % nBlockXSize) == 0 && |
390 | 1.05k | (nYOff % nBlockYSize) == 0 && |
391 | 1.05k | (nXOff + nXSize == nRasterXSize || (nXSize % nBlockXSize) == 0) && |
392 | 1.05k | (nYOff + nYSize == nRasterYSize || (nYSize % nBlockYSize) == 0)) |
393 | 970 | { |
394 | 970 | m_poGDS->Crystalize(); |
395 | | |
396 | 970 | if (m_poGDS->m_bDebugDontWriteBlocks) |
397 | 0 | return CE_None; |
398 | | |
399 | 970 | const int nDTSize = GDALGetDataTypeSizeBytes(eDataType); |
400 | 970 | if (nXSize == nBlockXSize && nYSize == nBlockYSize && |
401 | 953 | eBufType == eDataType && nPixelSpace == nDTSize && |
402 | 953 | nLineSpace == nPixelSpace * nBlockXSize) |
403 | 953 | { |
404 | | // If writing one single block with the right data type and layout, |
405 | | // we don't need a temporary buffer |
406 | 953 | const int nBlockId = |
407 | 953 | ComputeBlockId(nXOff / nBlockXSize, nYOff / nBlockYSize); |
408 | 953 | return m_poGDS->WriteEncodedTileOrStrip( |
409 | 953 | nBlockId, pData, /* bPreserveDataBuffer= */ true); |
410 | 953 | } |
411 | | |
412 | | // Make sure m_poGDS->m_pabyBlockBuf is allocated. |
413 | | // We could actually use any temporary buffer |
414 | 17 | if (m_poGDS->LoadBlockBuf(/* nBlockId = */ -1, |
415 | 17 | /* bReadFromDisk = */ false) != CE_None) |
416 | 0 | { |
417 | 0 | return CE_Failure; |
418 | 0 | } |
419 | | |
420 | | // Iterate over all blocks defined by |
421 | | // [nXOff, nXOff+nXSize[ * [nYOff, nYOff+nYSize[ |
422 | | // and write their content as a nBlockXSize x nBlockYSize strile |
423 | | // in a temporary buffer, before calling WriteEncodedTileOrStrip() |
424 | | // on it |
425 | 17 | const int nYBlockStart = nYOff / nBlockYSize; |
426 | 17 | const int nYBlockEnd = 1 + (nYOff + nYSize - 1) / nBlockYSize; |
427 | 17 | const int nXBlockStart = nXOff / nBlockXSize; |
428 | 17 | const int nXBlockEnd = 1 + (nXOff + nXSize - 1) / nBlockXSize; |
429 | 86 | for (int nYBlock = nYBlockStart; nYBlock < nYBlockEnd; ++nYBlock) |
430 | 69 | { |
431 | 69 | const int nValidY = |
432 | 69 | std::min(nBlockYSize, nRasterYSize - nYBlock * nBlockYSize); |
433 | 195 | for (int nXBlock = nXBlockStart; nXBlock < nXBlockEnd; ++nXBlock) |
434 | 126 | { |
435 | 126 | const int nValidX = |
436 | 126 | std::min(nBlockXSize, nRasterXSize - nXBlock * nBlockXSize); |
437 | 126 | if (nValidY < nBlockYSize || nValidX < nBlockXSize) |
438 | 52 | { |
439 | | // Make sure padding bytes at the right/bottom of the |
440 | | // tile are initialized to zero. |
441 | 52 | memset(m_poGDS->m_pabyBlockBuf, 0, |
442 | 52 | static_cast<size_t>(nBlockXSize) * nBlockYSize * |
443 | 52 | nDTSize); |
444 | 52 | } |
445 | 126 | const GByte *pabySrcData = |
446 | 126 | static_cast<const GByte *>(pData) + |
447 | 126 | static_cast<size_t>(nYBlock - nYBlockStart) * nBlockYSize * |
448 | 126 | nLineSpace + |
449 | 126 | static_cast<size_t>(nXBlock - nXBlockStart) * nBlockXSize * |
450 | 126 | nPixelSpace; |
451 | 7.51k | for (int iY = 0; iY < nValidY; ++iY) |
452 | 7.38k | { |
453 | 7.38k | GDALCopyWords64( |
454 | 7.38k | pabySrcData + static_cast<size_t>(iY) * nLineSpace, |
455 | 7.38k | eBufType, static_cast<int>(nPixelSpace), |
456 | 7.38k | m_poGDS->m_pabyBlockBuf + |
457 | 7.38k | static_cast<size_t>(iY) * nBlockXSize * nDTSize, |
458 | 7.38k | eDataType, nDTSize, nValidX); |
459 | 7.38k | } |
460 | 126 | const int nBlockId = ComputeBlockId(nXBlock, nYBlock); |
461 | 126 | if (m_poGDS->WriteEncodedTileOrStrip( |
462 | 126 | nBlockId, m_poGDS->m_pabyBlockBuf, |
463 | 126 | /* bPreserveDataBuffer= */ false) != CE_None) |
464 | 0 | { |
465 | 0 | return CE_Failure; |
466 | 0 | } |
467 | 126 | } |
468 | 69 | } |
469 | 17 | return CE_None; |
470 | 17 | } |
471 | | |
472 | 2.20M | if (psExtraArg->eResampleAlg == GRIORA_NearestNeighbour) |
473 | 2.20M | ++m_poGDS->m_nJPEGOverviewVisibilityCounter; |
474 | 2.20M | const CPLErr eErr = GDALPamRasterBand::IRasterIO( |
475 | 2.20M | eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize, |
476 | 2.20M | eBufType, nPixelSpace, nLineSpace, psExtraArg); |
477 | 2.20M | if (psExtraArg->eResampleAlg == GRIORA_NearestNeighbour) |
478 | 2.20M | --m_poGDS->m_nJPEGOverviewVisibilityCounter; |
479 | | |
480 | 2.20M | m_poGDS->m_bLoadingOtherBands = false; |
481 | | |
482 | 2.20M | return eErr; |
483 | 2.20M | } |
484 | | |
485 | | /************************************************************************/ |
486 | | /* ComputeBlockId() */ |
487 | | /************************************************************************/ |
488 | | |
489 | | /** Computes the TIFF block identifier from the tile coordinate, band |
490 | | * number and planar configuration. |
491 | | */ |
492 | | int GTiffRasterBand::ComputeBlockId(int nBlockXOff, int nBlockYOff) const |
493 | 2.95M | { |
494 | 2.95M | const int nBlockId = nBlockXOff + nBlockYOff * nBlocksPerRow; |
495 | 2.95M | if (m_poGDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE) |
496 | 1.06M | { |
497 | 1.06M | return nBlockId + (nBand - 1) * m_poGDS->m_nBlocksPerBand; |
498 | 1.06M | } |
499 | 1.88M | return nBlockId; |
500 | 2.95M | } |