1 /******************************************************************************
2 *
3 * Project: GDAL Core
4 * Purpose: Implementation of GDALRescaledAlphaBand, a class implementing
5 * a band mask based from a non-GDT_Byte 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 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 ****************************************************************************/
29
30 #include "cpl_port.h"
31 #include "gdal_priv.h"
32
33 #include <cstddef>
34
35 #include "cpl_error.h"
36 #include "cpl_vsi.h"
37 #include "gdal.h"
38
39 CPL_CVSID("$Id: gdalrescaledalphaband.cpp 7e07230bbff24eb333608de4dbd460b7312839d0 2017-12-11 19:08:47Z Even Rouault $")
40
41 //! @cond Doxygen_Suppress
42 /************************************************************************/
43 /* GDALRescaledAlphaBand() */
44 /************************************************************************/
45
GDALRescaledAlphaBand(GDALRasterBand * poParentIn)46 GDALRescaledAlphaBand::GDALRescaledAlphaBand( GDALRasterBand *poParentIn ) :
47 poParent(poParentIn),
48 pTemp(nullptr)
49 {
50 CPLAssert(poParent->GetRasterDataType() == GDT_UInt16);
51
52 // Defined in GDALRasterBand.
53 poDS = nullptr;
54 nBand = 0;
55
56 nRasterXSize = poParent->GetXSize();
57 nRasterYSize = poParent->GetYSize();
58
59 eDataType = GDT_Byte;
60 poParent->GetBlockSize( &nBlockXSize, &nBlockYSize );
61 }
62
63 /************************************************************************/
64 /* ~GDALRescaledAlphaBand() */
65 /************************************************************************/
66
~GDALRescaledAlphaBand()67 GDALRescaledAlphaBand::~GDALRescaledAlphaBand() { VSIFree(pTemp); }
68
69 /************************************************************************/
70 /* IReadBlock() */
71 /************************************************************************/
72
IReadBlock(int nXBlockOff,int nYBlockOff,void * pImage)73 CPLErr GDALRescaledAlphaBand::IReadBlock( int nXBlockOff, int nYBlockOff,
74 void * pImage )
75 {
76 int nXSizeRequest = nBlockXSize;
77 if (nXBlockOff * nBlockXSize + nBlockXSize > nRasterXSize)
78 nXSizeRequest = nRasterXSize - nXBlockOff * nBlockXSize;
79 int nYSizeRequest = nBlockYSize;
80 if (nYBlockOff * nBlockYSize + nBlockYSize > nRasterYSize)
81 nYSizeRequest = nRasterYSize - nYBlockOff * nBlockYSize;
82
83 GDALRasterIOExtraArg sExtraArg;
84 INIT_RASTERIO_EXTRA_ARG(sExtraArg);
85
86 return IRasterIO(
87 GF_Read, nXBlockOff * nBlockXSize, nYBlockOff * nBlockYSize,
88 nXSizeRequest, nYSizeRequest, pImage,
89 nXSizeRequest, nYSizeRequest, GDT_Byte,
90 1, nBlockXSize, &sExtraArg );
91 }
92
93 /************************************************************************/
94 /* IRasterIO() */
95 /************************************************************************/
96
IRasterIO(GDALRWFlag eRWFlag,int nXOff,int nYOff,int nXSize,int nYSize,void * pData,int nBufXSize,int nBufYSize,GDALDataType eBufType,GSpacing nPixelSpace,GSpacing nLineSpace,GDALRasterIOExtraArg * psExtraArg)97 CPLErr GDALRescaledAlphaBand::IRasterIO(
98 GDALRWFlag eRWFlag,
99 int nXOff, int nYOff, int nXSize, int nYSize,
100 void * pData, int nBufXSize, int nBufYSize,
101 GDALDataType eBufType,
102 GSpacing nPixelSpace,
103 GSpacing nLineSpace,
104 GDALRasterIOExtraArg* psExtraArg )
105 {
106 // Optimization in common use case.
107 // This avoids triggering the block cache on this band, which helps
108 // reducing the global block cache consumption.
109 if( eRWFlag == GF_Read && eBufType == GDT_Byte &&
110 nXSize == nBufXSize && nYSize == nBufYSize &&
111 nPixelSpace == 1 )
112 {
113 if( pTemp == nullptr )
114 {
115 pTemp = VSI_MALLOC2_VERBOSE( sizeof(GUInt16), nRasterXSize );
116 if( pTemp == nullptr )
117 {
118 return CE_Failure;
119 }
120 }
121 for( int j = 0; j < nBufYSize; j++ )
122 {
123 const CPLErr eErr =
124 poParent->RasterIO( GF_Read, nXOff, nYOff + j, nXSize, 1,
125 pTemp, nBufXSize, 1,
126 GDT_UInt16,
127 0, 0, nullptr );
128 if( eErr != CE_None )
129 return eErr;
130
131 GByte* pabyImage = static_cast<GByte*>(pData) + j * nLineSpace;
132 GUInt16* pSrc = static_cast<GUInt16 *>(pTemp);
133
134 for( int i = 0; i < nBufXSize; i++ )
135 {
136 // In case the dynamics was actually 0-255 and not 0-65535 as
137 // expected, we want to make sure non-zero alpha will still
138 // be non-zero.
139 if( pSrc[i] > 0 && pSrc[i] < 257 )
140 pabyImage[i] = 1;
141 else
142 pabyImage[i] = static_cast<GByte>((pSrc[i] * 255) / 65535);
143 }
144 }
145 return CE_None;
146 }
147
148 return GDALRasterBand::IRasterIO( eRWFlag, nXOff, nYOff, nXSize, nYSize,
149 pData, nBufXSize, nBufYSize,
150 eBufType,
151 nPixelSpace, nLineSpace, psExtraArg );
152 }
153 //! @endcond
154