Coverage Report

Created: 2025-08-11 09:23

/src/gdal/gcore/gdalrescaledalphaband.cpp
Line
Count
Source (jump to first uncovered line)
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
 * SPDX-License-Identifier: MIT
12
 ****************************************************************************/
13
14
#include "cpl_port.h"
15
#include "gdal_priv.h"
16
17
#include <cstddef>
18
19
#include "cpl_error.h"
20
#include "cpl_vsi.h"
21
#include "gdal.h"
22
23
//! @cond Doxygen_Suppress
24
/************************************************************************/
25
/*                        GDALRescaledAlphaBand()                       */
26
/************************************************************************/
27
28
GDALRescaledAlphaBand::GDALRescaledAlphaBand(GDALRasterBand *poParentIn)
29
853
    : poParent(poParentIn), pTemp(nullptr)
30
853
{
31
853
    CPLAssert(poParent->GetRasterDataType() == GDT_UInt16);
32
33
    // Defined in GDALRasterBand.
34
853
    poDS = nullptr;
35
853
    nBand = 0;
36
37
853
    nRasterXSize = poParent->GetXSize();
38
853
    nRasterYSize = poParent->GetYSize();
39
40
853
    eDataType = GDT_Byte;
41
853
    poParent->GetBlockSize(&nBlockXSize, &nBlockYSize);
42
853
}
43
44
/************************************************************************/
45
/*                      ~GDALRescaledAlphaBand()                        */
46
/************************************************************************/
47
48
GDALRescaledAlphaBand::~GDALRescaledAlphaBand()
49
853
{
50
853
    VSIFree(pTemp);
51
853
}
52
53
/************************************************************************/
54
/*                             IReadBlock()                             */
55
/************************************************************************/
56
57
CPLErr GDALRescaledAlphaBand::IReadBlock(int nXBlockOff, int nYBlockOff,
58
                                         void *pImage)
59
691
{
60
691
    int nXSizeRequest = nBlockXSize;
61
691
    if (nXBlockOff * nBlockXSize + nBlockXSize > nRasterXSize)
62
0
        nXSizeRequest = nRasterXSize - nXBlockOff * nBlockXSize;
63
691
    int nYSizeRequest = nBlockYSize;
64
691
    if (nYBlockOff * nBlockYSize + nBlockYSize > nRasterYSize)
65
0
        nYSizeRequest = nRasterYSize - nYBlockOff * nBlockYSize;
66
67
691
    GDALRasterIOExtraArg sExtraArg;
68
691
    INIT_RASTERIO_EXTRA_ARG(sExtraArg);
69
70
691
    return IRasterIO(GF_Read, nXBlockOff * nBlockXSize,
71
691
                     nYBlockOff * nBlockYSize, nXSizeRequest, nYSizeRequest,
72
691
                     pImage, nXSizeRequest, nYSizeRequest, GDT_Byte, 1,
73
691
                     nBlockXSize, &sExtraArg);
74
691
}
75
76
/************************************************************************/
77
/*                             IRasterIO()                              */
78
/************************************************************************/
79
80
CPLErr GDALRescaledAlphaBand::IRasterIO(
81
    GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize,
82
    void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType,
83
    GSpacing nPixelSpace, GSpacing nLineSpace, GDALRasterIOExtraArg *psExtraArg)
84
56.5k
{
85
    // Optimization in common use case.
86
    // This avoids triggering the block cache on this band, which helps
87
    // reducing the global block cache consumption.
88
56.5k
    if (eRWFlag == GF_Read && eBufType == GDT_Byte && nXSize == nBufXSize &&
89
56.5k
        nYSize == nBufYSize && nPixelSpace == 1)
90
34.5k
    {
91
34.5k
        if (pTemp == nullptr)
92
680
        {
93
680
            pTemp = VSI_MALLOC2_VERBOSE(sizeof(GUInt16), nRasterXSize);
94
680
            if (pTemp == nullptr)
95
0
            {
96
0
                return CE_Failure;
97
0
            }
98
680
        }
99
259k
        for (int j = 0; j < nBufYSize; j++)
100
234k
        {
101
234k
            const CPLErr eErr =
102
234k
                poParent->RasterIO(GF_Read, nXOff, nYOff + j, nXSize, 1, pTemp,
103
234k
                                   nBufXSize, 1, GDT_UInt16, 0, 0, nullptr);
104
234k
            if (eErr != CE_None)
105
9.69k
                return eErr;
106
107
224k
            GByte *pabyImage = static_cast<GByte *>(pData) + j * nLineSpace;
108
224k
            GUInt16 *pSrc = static_cast<GUInt16 *>(pTemp);
109
110
9.08M
            for (int i = 0; i < nBufXSize; i++)
111
8.85M
            {
112
                // In case the dynamics was actually 0-255 and not 0-65535 as
113
                // expected, we want to make sure non-zero alpha will still
114
                // be non-zero.
115
8.85M
                if (pSrc[i] > 0 && pSrc[i] < 257)
116
16.5k
                    pabyImage[i] = 1;
117
8.84M
                else
118
8.84M
                    pabyImage[i] = static_cast<GByte>((pSrc[i] * 255) / 65535);
119
8.85M
            }
120
224k
        }
121
24.8k
        return CE_None;
122
34.5k
    }
123
124
22.0k
    return GDALRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
125
22.0k
                                     pData, nBufXSize, nBufYSize, eBufType,
126
22.0k
                                     nPixelSpace, nLineSpace, psExtraArg);
127
56.5k
}
128
129
/************************************************************************/
130
/*                   EmitErrorMessageIfWriteNotSupported()              */
131
/************************************************************************/
132
133
bool GDALRescaledAlphaBand::EmitErrorMessageIfWriteNotSupported(
134
    const char *pszCaller) const
135
0
{
136
0
    ReportError(CE_Failure, CPLE_NoWriteAccess,
137
0
                "%s: attempt to write to a GDALRescaledAlphaBand.", pszCaller);
138
139
0
    return true;
140
0
}
141
142
//! @endcond