Coverage Report

Created: 2026-02-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/gcore/gdalrescaledalphaband.cpp
Line
Count
Source
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
GDALRescaledAlphaBand::GDALRescaledAlphaBand(GDALRasterBand *poParentIn)
30
0
    : poParent(poParentIn), pTemp(nullptr)
31
0
{
32
0
    CPLAssert(poParent->GetRasterDataType() == GDT_UInt16);
33
34
    // Defined in GDALRasterBand.
35
0
    poDS = nullptr;
36
0
    nBand = 0;
37
38
0
    nRasterXSize = poParent->GetXSize();
39
0
    nRasterYSize = poParent->GetYSize();
40
41
0
    eDataType = GDT_UInt8;
42
0
    poParent->GetBlockSize(&nBlockXSize, &nBlockYSize);
43
0
}
44
45
/************************************************************************/
46
/*                       ~GDALRescaledAlphaBand()                       */
47
/************************************************************************/
48
49
GDALRescaledAlphaBand::~GDALRescaledAlphaBand()
50
0
{
51
0
    VSIFree(pTemp);
52
0
}
53
54
/************************************************************************/
55
/*                             IReadBlock()                             */
56
/************************************************************************/
57
58
CPLErr GDALRescaledAlphaBand::IReadBlock(int nXBlockOff, int nYBlockOff,
59
                                         void *pImage)
60
0
{
61
0
    const int nXOff = nXBlockOff * nBlockXSize;
62
0
    const int nXSizeRequest = std::min(nBlockXSize, nRasterXSize - nXOff);
63
0
    const int nYOff = nYBlockOff * nBlockYSize;
64
0
    const int nYSizeRequest = std::min(nBlockYSize, nRasterYSize - nYOff);
65
66
0
    GDALRasterIOExtraArg sExtraArg;
67
0
    INIT_RASTERIO_EXTRA_ARG(sExtraArg);
68
69
0
    return IRasterIO(GF_Read, nXOff, nYOff, nXSizeRequest, nYSizeRequest,
70
0
                     pImage, nXSizeRequest, nYSizeRequest, GDT_UInt8, 1,
71
0
                     nBlockXSize, &sExtraArg);
72
0
}
73
74
/************************************************************************/
75
/*                             IRasterIO()                              */
76
/************************************************************************/
77
78
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
0
{
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
0
    if (eRWFlag == GF_Read && eBufType == GDT_UInt8 && nXSize == nBufXSize &&
87
0
        nYSize == nBufYSize && nPixelSpace == 1)
88
0
    {
89
0
        if (pTemp == nullptr)
90
0
        {
91
0
            pTemp = VSI_MALLOC2_VERBOSE(sizeof(GUInt16), nRasterXSize);
92
0
            if (pTemp == nullptr)
93
0
            {
94
0
                return CE_Failure;
95
0
            }
96
0
        }
97
0
        for (int j = 0; j < nBufYSize; j++)
98
0
        {
99
0
            const CPLErr eErr =
100
0
                poParent->RasterIO(GF_Read, nXOff, nYOff + j, nXSize, 1, pTemp,
101
0
                                   nBufXSize, 1, GDT_UInt16, 0, 0, nullptr);
102
0
            if (eErr != CE_None)
103
0
                return eErr;
104
105
0
            GByte *pabyImage = static_cast<GByte *>(pData) + j * nLineSpace;
106
0
            GUInt16 *pSrc = static_cast<GUInt16 *>(pTemp);
107
108
0
            for (int i = 0; i < nBufXSize; i++)
109
0
            {
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
0
                if (pSrc[i] > 0 && pSrc[i] < 257)
114
0
                    pabyImage[i] = 1;
115
0
                else
116
0
                    pabyImage[i] = static_cast<GByte>((pSrc[i] * 255) / 65535);
117
0
            }
118
0
        }
119
0
        return CE_None;
120
0
    }
121
122
0
    return GDALRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
123
0
                                     pData, nBufXSize, nBufYSize, eBufType,
124
0
                                     nPixelSpace, nLineSpace, psExtraArg);
125
0
}
126
127
/************************************************************************/
128
/*                EmitErrorMessageIfWriteNotSupported()                 */
129
/************************************************************************/
130
131
bool GDALRescaledAlphaBand::EmitErrorMessageIfWriteNotSupported(
132
    const char *pszCaller) const
133
0
{
134
0
    ReportError(CE_Failure, CPLE_NoWriteAccess,
135
0
                "%s: attempt to write to a GDALRescaledAlphaBand.", pszCaller);
136
137
0
    return true;
138
0
}
139
140
//! @endcond