Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GDAL Core
4 : * Purpose: Implementation of GDALRescaledAlphaBand, a class implementing
5 : * a band mask based from a non-GDT_UInt8 alpha band
6 : * Author: Even Rouault, <even dot rouault at spatialys dot com>
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2014, Even Rouault <even dot rouault at spatialys dot com>
10 : *
11 : * SPDX-License-Identifier: MIT
12 : ****************************************************************************/
13 :
14 : #include "cpl_port.h"
15 : #include "gdal_priv.h"
16 :
17 : #include <algorithm>
18 : #include <cstddef>
19 :
20 : #include "cpl_error.h"
21 : #include "cpl_vsi.h"
22 : #include "gdal.h"
23 :
24 : //! @cond Doxygen_Suppress
25 : /************************************************************************/
26 : /* GDALRescaledAlphaBand() */
27 : /************************************************************************/
28 :
29 65 : GDALRescaledAlphaBand::GDALRescaledAlphaBand(GDALRasterBand *poParentIn)
30 65 : : poParent(poParentIn), pTemp(nullptr)
31 : {
32 65 : CPLAssert(poParent->GetRasterDataType() == GDT_UInt16);
33 :
34 : // Defined in GDALRasterBand.
35 65 : poDS = nullptr;
36 65 : nBand = 0;
37 :
38 65 : nRasterXSize = poParent->GetXSize();
39 65 : nRasterYSize = poParent->GetYSize();
40 :
41 65 : eDataType = GDT_UInt8;
42 65 : poParent->GetBlockSize(&nBlockXSize, &nBlockYSize);
43 65 : }
44 :
45 : /************************************************************************/
46 : /* ~GDALRescaledAlphaBand() */
47 : /************************************************************************/
48 :
49 130 : GDALRescaledAlphaBand::~GDALRescaledAlphaBand()
50 : {
51 65 : VSIFree(pTemp);
52 130 : }
53 :
54 : /************************************************************************/
55 : /* IReadBlock() */
56 : /************************************************************************/
57 :
58 1 : CPLErr GDALRescaledAlphaBand::IReadBlock(int nXBlockOff, int nYBlockOff,
59 : void *pImage)
60 : {
61 1 : const int nXOff = nXBlockOff * nBlockXSize;
62 1 : const int nXSizeRequest = std::min(nBlockXSize, nRasterXSize - nXOff);
63 1 : const int nYOff = nYBlockOff * nBlockYSize;
64 1 : const int nYSizeRequest = std::min(nBlockYSize, nRasterYSize - nYOff);
65 :
66 : GDALRasterIOExtraArg sExtraArg;
67 1 : INIT_RASTERIO_EXTRA_ARG(sExtraArg);
68 :
69 2 : return IRasterIO(GF_Read, nXOff, nYOff, nXSizeRequest, nYSizeRequest,
70 : pImage, nXSizeRequest, nYSizeRequest, GDT_UInt8, 1,
71 2 : nBlockXSize, &sExtraArg);
72 : }
73 :
74 : /************************************************************************/
75 : /* IRasterIO() */
76 : /************************************************************************/
77 :
78 972 : CPLErr GDALRescaledAlphaBand::IRasterIO(
79 : GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize,
80 : void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType,
81 : GSpacing nPixelSpace, GSpacing nLineSpace, GDALRasterIOExtraArg *psExtraArg)
82 : {
83 : // Optimization in common use case.
84 : // This avoids triggering the block cache on this band, which helps
85 : // reducing the global block cache consumption.
86 972 : if (eRWFlag == GF_Read && eBufType == GDT_UInt8 && nXSize == nBufXSize &&
87 972 : nYSize == nBufYSize && nPixelSpace == 1)
88 : {
89 972 : if (pTemp == nullptr)
90 : {
91 15 : pTemp = VSI_MALLOC2_VERBOSE(sizeof(GUInt16), nRasterXSize);
92 15 : if (pTemp == nullptr)
93 : {
94 0 : return CE_Failure;
95 : }
96 : }
97 1953 : for (int j = 0; j < nBufYSize; j++)
98 : {
99 : const CPLErr eErr =
100 981 : poParent->RasterIO(GF_Read, nXOff, nYOff + j, nXSize, 1, pTemp,
101 : nBufXSize, 1, GDT_UInt16, 0, 0, nullptr);
102 981 : if (eErr != CE_None)
103 0 : return eErr;
104 :
105 981 : GByte *pabyImage = static_cast<GByte *>(pData) + j * nLineSpace;
106 981 : GUInt16 *pSrc = static_cast<GUInt16 *>(pTemp);
107 :
108 202592 : for (int i = 0; i < nBufXSize; i++)
109 : {
110 : // In case the dynamics was actually 0-255 and not 0-65535 as
111 : // expected, we want to make sure non-zero alpha will still
112 : // be non-zero.
113 201611 : if (pSrc[i] > 0 && pSrc[i] < 257)
114 4001 : pabyImage[i] = 1;
115 : else
116 197610 : pabyImage[i] = static_cast<GByte>((pSrc[i] * 255) / 65535);
117 : }
118 : }
119 972 : return CE_None;
120 : }
121 :
122 0 : return GDALRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
123 : pData, nBufXSize, nBufYSize, eBufType,
124 0 : nPixelSpace, nLineSpace, psExtraArg);
125 : }
126 :
127 : /************************************************************************/
128 : /* EmitErrorMessageIfWriteNotSupported() */
129 : /************************************************************************/
130 :
131 0 : bool GDALRescaledAlphaBand::EmitErrorMessageIfWriteNotSupported(
132 : const char *pszCaller) const
133 : {
134 0 : ReportError(CE_Failure, CPLE_NoWriteAccess,
135 : "%s: attempt to write to a GDALRescaledAlphaBand.", pszCaller);
136 :
137 0 : return true;
138 : }
139 :
140 : //! @endcond
|