/src/gdal/frmts/gtiff/gtiffsplitband.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: GeoTIFF Driver |
4 | | * Purpose: GDAL GeoTIFF support. |
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 "gtiffsplitband.h" |
15 | | |
16 | | #include "gtiffdataset.h" |
17 | | #include "gdal_priv.h" |
18 | | |
19 | | /************************************************************************/ |
20 | | /* GTiffSplitBand() */ |
21 | | /************************************************************************/ |
22 | | |
23 | | GTiffSplitBand::GTiffSplitBand(GTiffDataset *poDSIn, int nBandIn) |
24 | 3.27M | : GTiffRasterBand(poDSIn, nBandIn) |
25 | 3.27M | { |
26 | 3.27M | nBlockXSize = poDS->GetRasterXSize(); |
27 | 3.27M | nBlockYSize = 1; |
28 | 3.27M | } |
29 | | |
30 | | /************************************************************************/ |
31 | | /* IGetDataCoverageStatus() */ |
32 | | /************************************************************************/ |
33 | | |
34 | | int GTiffSplitBand::IGetDataCoverageStatus(int, int, int, int, int, double *) |
35 | 0 | { |
36 | 0 | return GDAL_DATA_COVERAGE_STATUS_UNIMPLEMENTED | |
37 | 0 | GDAL_DATA_COVERAGE_STATUS_DATA; |
38 | 0 | } |
39 | | |
40 | | /************************************************************************/ |
41 | | /* IReadBlock() */ |
42 | | /************************************************************************/ |
43 | | |
44 | | CPLErr GTiffSplitBand::IReadBlock(int /* nBlockXOff */, int nBlockYOff, |
45 | | void *pImage) |
46 | | |
47 | 319k | { |
48 | 319k | m_poGDS->Crystalize(); |
49 | | |
50 | | // Optimization when reading the same line in a contig multi-band TIFF. |
51 | 319k | if (m_poGDS->m_nPlanarConfig == PLANARCONFIG_CONTIG && |
52 | 233k | m_poGDS->nBands > 1 && m_poGDS->m_nLoadedBlock == nBlockYOff) |
53 | 0 | { |
54 | 0 | goto extract_band_data; |
55 | 0 | } |
56 | | |
57 | 319k | if (m_poGDS->m_nPlanarConfig == PLANARCONFIG_CONTIG && m_poGDS->nBands > 1) |
58 | 205k | { |
59 | 205k | if (m_poGDS->m_pabyBlockBuf == nullptr) |
60 | 664 | { |
61 | 664 | m_poGDS->m_pabyBlockBuf = static_cast<GByte *>( |
62 | 664 | VSI_MALLOC_VERBOSE(TIFFScanlineSize(m_poGDS->m_hTIFF))); |
63 | 664 | if (m_poGDS->m_pabyBlockBuf == nullptr) |
64 | 0 | { |
65 | 0 | return CE_Failure; |
66 | 0 | } |
67 | 664 | } |
68 | 205k | } |
69 | 113k | else |
70 | 113k | { |
71 | 113k | CPLAssert(TIFFScanlineSize(m_poGDS->m_hTIFF) == nBlockXSize); |
72 | 113k | } |
73 | | |
74 | | /* -------------------------------------------------------------------- */ |
75 | | /* Read through to target scanline. */ |
76 | | /* -------------------------------------------------------------------- */ |
77 | 319k | if (m_poGDS->m_nLoadedBlock >= nBlockYOff) |
78 | 322 | m_poGDS->m_nLoadedBlock = -1; |
79 | | |
80 | 319k | if (m_poGDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE && |
81 | 85.8k | m_poGDS->nBands > 1) |
82 | 84.3k | { |
83 | | // If we change of band, we must start reading the |
84 | | // new strip from its beginning. |
85 | 84.3k | if (m_poGDS->m_nLastBandRead != nBand) |
86 | 2.23k | m_poGDS->m_nLoadedBlock = -1; |
87 | 84.3k | m_poGDS->m_nLastBandRead = nBand; |
88 | 84.3k | } |
89 | | |
90 | 837k | while (m_poGDS->m_nLoadedBlock < nBlockYOff) |
91 | 524k | { |
92 | 524k | ++m_poGDS->m_nLoadedBlock; |
93 | 524k | if (TIFFReadScanline(m_poGDS->m_hTIFF, |
94 | 524k | m_poGDS->m_pabyBlockBuf ? m_poGDS->m_pabyBlockBuf |
95 | 524k | : pImage, |
96 | 524k | m_poGDS->m_nLoadedBlock, |
97 | 524k | (m_poGDS->m_nPlanarConfig == PLANARCONFIG_SEPARATE) |
98 | 524k | ? static_cast<uint16_t>(nBand - 1) |
99 | 524k | : 0) == -1 && |
100 | 6.36k | !m_poGDS->m_bIgnoreReadErrors) |
101 | 6.36k | { |
102 | 6.36k | ReportError(CE_Failure, CPLE_AppDefined, |
103 | 6.36k | "TIFFReadScanline() failed."); |
104 | 6.36k | m_poGDS->m_nLoadedBlock = -1; |
105 | 6.36k | return CE_Failure; |
106 | 6.36k | } |
107 | 524k | } |
108 | | |
109 | 313k | extract_band_data: |
110 | | /* -------------------------------------------------------------------- */ |
111 | | /* Extract band data from contig buffer. */ |
112 | | /* -------------------------------------------------------------------- */ |
113 | 313k | if (m_poGDS->m_pabyBlockBuf != nullptr) |
114 | 202k | { |
115 | 202k | for (int iPixel = 0, iSrcOffset = nBand - 1, iDstOffset = 0; |
116 | 3.27M | iPixel < nBlockXSize; |
117 | 3.07M | ++iPixel, iSrcOffset += m_poGDS->nBands, ++iDstOffset) |
118 | 3.07M | { |
119 | 3.07M | static_cast<GByte *>(pImage)[iDstOffset] = |
120 | 3.07M | m_poGDS->m_pabyBlockBuf[iSrcOffset]; |
121 | 3.07M | } |
122 | 202k | } |
123 | | |
124 | 313k | return CE_None; |
125 | 319k | } |
126 | | |
127 | | /************************************************************************/ |
128 | | /* IWriteBlock() */ |
129 | | /************************************************************************/ |
130 | | |
131 | | CPLErr GTiffSplitBand::IWriteBlock(int /* nBlockXOff */, int /* nBlockYOff */, |
132 | | void * /* pImage */) |
133 | | |
134 | 0 | { |
135 | 0 | ReportError(CE_Failure, CPLE_AppDefined, "Split bands are read-only."); |
136 | 0 | return CE_Failure; |
137 | 0 | } |