Coverage Report

Created: 2025-06-13 06:29

/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
0
    : poParent(poParentIn), pTemp(nullptr)
30
0
{
31
0
    CPLAssert(poParent->GetRasterDataType() == GDT_UInt16);
32
33
    // Defined in GDALRasterBand.
34
0
    poDS = nullptr;
35
0
    nBand = 0;
36
37
0
    nRasterXSize = poParent->GetXSize();
38
0
    nRasterYSize = poParent->GetYSize();
39
40
0
    eDataType = GDT_Byte;
41
0
    poParent->GetBlockSize(&nBlockXSize, &nBlockYSize);
42
0
}
43
44
/************************************************************************/
45
/*                      ~GDALRescaledAlphaBand()                        */
46
/************************************************************************/
47
48
GDALRescaledAlphaBand::~GDALRescaledAlphaBand()
49
0
{
50
0
    VSIFree(pTemp);
51
0
}
52
53
/************************************************************************/
54
/*                             IReadBlock()                             */
55
/************************************************************************/
56
57
CPLErr GDALRescaledAlphaBand::IReadBlock(int nXBlockOff, int nYBlockOff,
58
                                         void *pImage)
59
0
{
60
0
    int nXSizeRequest = nBlockXSize;
61
0
    if (nXBlockOff * nBlockXSize + nBlockXSize > nRasterXSize)
62
0
        nXSizeRequest = nRasterXSize - nXBlockOff * nBlockXSize;
63
0
    int nYSizeRequest = nBlockYSize;
64
0
    if (nYBlockOff * nBlockYSize + nBlockYSize > nRasterYSize)
65
0
        nYSizeRequest = nRasterYSize - nYBlockOff * nBlockYSize;
66
67
0
    GDALRasterIOExtraArg sExtraArg;
68
0
    INIT_RASTERIO_EXTRA_ARG(sExtraArg);
69
70
0
    return IRasterIO(GF_Read, nXBlockOff * nBlockXSize,
71
0
                     nYBlockOff * nBlockYSize, nXSizeRequest, nYSizeRequest,
72
0
                     pImage, nXSizeRequest, nYSizeRequest, GDT_Byte, 1,
73
0
                     nBlockXSize, &sExtraArg);
74
0
}
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
0
{
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
0
    if (eRWFlag == GF_Read && eBufType == GDT_Byte && nXSize == nBufXSize &&
89
0
        nYSize == nBufYSize && nPixelSpace == 1)
90
0
    {
91
0
        if (pTemp == nullptr)
92
0
        {
93
0
            pTemp = VSI_MALLOC2_VERBOSE(sizeof(GUInt16), nRasterXSize);
94
0
            if (pTemp == nullptr)
95
0
            {
96
0
                return CE_Failure;
97
0
            }
98
0
        }
99
0
        for (int j = 0; j < nBufYSize; j++)
100
0
        {
101
0
            const CPLErr eErr =
102
0
                poParent->RasterIO(GF_Read, nXOff, nYOff + j, nXSize, 1, pTemp,
103
0
                                   nBufXSize, 1, GDT_UInt16, 0, 0, nullptr);
104
0
            if (eErr != CE_None)
105
0
                return eErr;
106
107
0
            GByte *pabyImage = static_cast<GByte *>(pData) + j * nLineSpace;
108
0
            GUInt16 *pSrc = static_cast<GUInt16 *>(pTemp);
109
110
0
            for (int i = 0; i < nBufXSize; i++)
111
0
            {
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
0
                if (pSrc[i] > 0 && pSrc[i] < 257)
116
0
                    pabyImage[i] = 1;
117
0
                else
118
0
                    pabyImage[i] = static_cast<GByte>((pSrc[i] * 255) / 65535);
119
0
            }
120
0
        }
121
0
        return CE_None;
122
0
    }
123
124
0
    return GDALRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
125
0
                                     pData, nBufXSize, nBufYSize, eBufType,
126
0
                                     nPixelSpace, nLineSpace, psExtraArg);
127
0
}
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