LCOV - code coverage report
Current view: top level - gcore - gdalrescaledalphaband.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 38 44 86.4 %
Date: 2024-05-03 15:49:35 Functions: 5 5 100.0 %

          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_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             : //! @cond Doxygen_Suppress
      40             : /************************************************************************/
      41             : /*                        GDALRescaledAlphaBand()                       */
      42             : /************************************************************************/
      43             : 
      44          58 : GDALRescaledAlphaBand::GDALRescaledAlphaBand(GDALRasterBand *poParentIn)
      45          58 :     : poParent(poParentIn), pTemp(nullptr)
      46             : {
      47          58 :     CPLAssert(poParent->GetRasterDataType() == GDT_UInt16);
      48             : 
      49             :     // Defined in GDALRasterBand.
      50          58 :     poDS = nullptr;
      51          58 :     nBand = 0;
      52             : 
      53          58 :     nRasterXSize = poParent->GetXSize();
      54          58 :     nRasterYSize = poParent->GetYSize();
      55             : 
      56          58 :     eDataType = GDT_Byte;
      57          58 :     poParent->GetBlockSize(&nBlockXSize, &nBlockYSize);
      58          58 : }
      59             : 
      60             : /************************************************************************/
      61             : /*                      ~GDALRescaledAlphaBand()                        */
      62             : /************************************************************************/
      63             : 
      64         116 : GDALRescaledAlphaBand::~GDALRescaledAlphaBand()
      65             : {
      66          58 :     VSIFree(pTemp);
      67         116 : }
      68             : 
      69             : /************************************************************************/
      70             : /*                             IReadBlock()                             */
      71             : /************************************************************************/
      72             : 
      73           1 : CPLErr GDALRescaledAlphaBand::IReadBlock(int nXBlockOff, int nYBlockOff,
      74             :                                          void *pImage)
      75             : {
      76           1 :     int nXSizeRequest = nBlockXSize;
      77           1 :     if (nXBlockOff * nBlockXSize + nBlockXSize > nRasterXSize)
      78           0 :         nXSizeRequest = nRasterXSize - nXBlockOff * nBlockXSize;
      79           1 :     int nYSizeRequest = nBlockYSize;
      80           1 :     if (nYBlockOff * nBlockYSize + nBlockYSize > nRasterYSize)
      81           0 :         nYSizeRequest = nRasterYSize - nYBlockOff * nBlockYSize;
      82             : 
      83             :     GDALRasterIOExtraArg sExtraArg;
      84           1 :     INIT_RASTERIO_EXTRA_ARG(sExtraArg);
      85             : 
      86           1 :     return IRasterIO(GF_Read, nXBlockOff * nBlockXSize,
      87           1 :                      nYBlockOff * nBlockYSize, nXSizeRequest, nYSizeRequest,
      88             :                      pImage, nXSizeRequest, nYSizeRequest, GDT_Byte, 1,
      89           2 :                      nBlockXSize, &sExtraArg);
      90             : }
      91             : 
      92             : /************************************************************************/
      93             : /*                             IRasterIO()                              */
      94             : /************************************************************************/
      95             : 
      96         204 : CPLErr GDALRescaledAlphaBand::IRasterIO(
      97             :     GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize,
      98             :     void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType,
      99             :     GSpacing nPixelSpace, GSpacing nLineSpace, GDALRasterIOExtraArg *psExtraArg)
     100             : {
     101             :     // Optimization in common use case.
     102             :     // This avoids triggering the block cache on this band, which helps
     103             :     // reducing the global block cache consumption.
     104         204 :     if (eRWFlag == GF_Read && eBufType == GDT_Byte && nXSize == nBufXSize &&
     105         204 :         nYSize == nBufYSize && nPixelSpace == 1)
     106             :     {
     107         204 :         if (pTemp == nullptr)
     108             :         {
     109          12 :             pTemp = VSI_MALLOC2_VERBOSE(sizeof(GUInt16), nRasterXSize);
     110          12 :             if (pTemp == nullptr)
     111             :             {
     112           0 :                 return CE_Failure;
     113             :             }
     114             :         }
     115         417 :         for (int j = 0; j < nBufYSize; j++)
     116             :         {
     117             :             const CPLErr eErr =
     118         213 :                 poParent->RasterIO(GF_Read, nXOff, nYOff + j, nXSize, 1, pTemp,
     119             :                                    nBufXSize, 1, GDT_UInt16, 0, 0, nullptr);
     120         213 :             if (eErr != CE_None)
     121           0 :                 return eErr;
     122             : 
     123         213 :             GByte *pabyImage = static_cast<GByte *>(pData) + j * nLineSpace;
     124         213 :             GUInt16 *pSrc = static_cast<GUInt16 *>(pTemp);
     125             : 
     126        5216 :             for (int i = 0; i < nBufXSize; i++)
     127             :             {
     128             :                 // In case the dynamics was actually 0-255 and not 0-65535 as
     129             :                 // expected, we want to make sure non-zero alpha will still
     130             :                 // be non-zero.
     131        5003 :                 if (pSrc[i] > 0 && pSrc[i] < 257)
     132        4001 :                     pabyImage[i] = 1;
     133             :                 else
     134        1002 :                     pabyImage[i] = static_cast<GByte>((pSrc[i] * 255) / 65535);
     135             :             }
     136             :         }
     137         204 :         return CE_None;
     138             :     }
     139             : 
     140           0 :     return GDALRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
     141             :                                      pData, nBufXSize, nBufYSize, eBufType,
     142           0 :                                      nPixelSpace, nLineSpace, psExtraArg);
     143             : }
     144             : 
     145             : //! @endcond

Generated by: LCOV version 1.14