1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/******************************************************************************
*
* Project: GDAL
* Purpose: Compute simple checksum for a region of image data.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2003, Frank Warmerdam
* Copyright (c) 2007-2008, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "cpl_port.h"
#include "gdal_alg.h"
#include <cmath>
#include <cstddef>
#include <algorithm>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_vsi.h"
#include "gdal.h"
#include "gdal_priv.h"
/************************************************************************/
/* GDALChecksumImage() */
/************************************************************************/
/**
* Compute checksum for image region.
*
* Computes a 16bit (0-65535) checksum from a region of raster data on a GDAL
* supported band. Floating point data is converted to 32bit integer
* so decimal portions of such raster data will not affect the checksum.
* Real and Imaginary components of complex bands influence the result.
*
* @param hBand the raster band to read from.
* @param nXOff pixel offset of window to read.
* @param nYOff line offset of window to read.
* @param nXSize pixel size of window to read.
* @param nYSize line size of window to read.
*
* @return Checksum value, or -1 in case of error (starting with GDAL 3.6)
*/
int CPL_STDCALL GDALChecksumImage(GDALRasterBandH hBand, int nXOff, int nYOff,
int nXSize, int nYSize)
{
VALIDATE_POINTER1(hBand, "GDALChecksumImage", 0);
const static int anPrimes[11] = {7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43};
int nChecksum = 0;
int iPrime = 0;
const GDALDataType eDataType = GDALGetRasterDataType(hBand);
const bool bComplex = CPL_TO_BOOL(GDALDataTypeIsComplex(eDataType));
const bool bIsFloatingPoint =
(eDataType == GDT_Float16 || eDataType == GDT_Float32 ||
eDataType == GDT_Float64 || eDataType == GDT_CFloat16 ||
eDataType == GDT_CFloat32 || eDataType == GDT_CFloat64);
const auto IntFromDouble = [](double dfVal)
{
int nVal;
if (!std::isfinite(dfVal))
{
nVal = INT_MIN;
}
else
{
// Standard behavior of GDALCopyWords when converting
// from floating point to Int32.
dfVal += 0.5;
if (dfVal < -2147483647.0)
nVal = -2147483647;
else if (dfVal > 2147483647)
nVal = 2147483647;
else
nVal = static_cast<GInt32>(floor(dfVal));
}
return nVal;
};
if (bIsFloatingPoint && nXOff == 0 && nYOff == 0)
{
const GDALDataType eDstDataType = bComplex ? GDT_CFloat64 : GDT_Float64;
int nBlockXSize = 0;
int nBlockYSize = 0;
GDALGetBlockSize(hBand, &nBlockXSize, &nBlockYSize);
const int nDstDataTypeSize = GDALGetDataTypeSizeBytes(eDstDataType);
int nChunkXSize = nBlockXSize;
const int nChunkYSize = nBlockYSize;
if (nBlockXSize < nXSize)
{
const GIntBig nMaxChunkSize =
std::max(static_cast<GIntBig>(10 * 1000 * 1000),
GDALGetCacheMax64() / 10);
if (nDstDataTypeSize > 0 &&
static_cast<GIntBig>(nXSize) * nChunkYSize <
nMaxChunkSize / nDstDataTypeSize)
{
// A full line of height nChunkYSize can fit in the maximum
// allowed memory
nChunkXSize = nXSize;
}
else if (nDstDataTypeSize > 0)
{
// Otherwise compute a size that is a multiple of nBlockXSize
nChunkXSize = static_cast<int>(std::min(
static_cast<GIntBig>(nXSize),
nBlockXSize *
std::max(static_cast<GIntBig>(1),
nMaxChunkSize /
(static_cast<GIntBig>(nBlockXSize) *
nChunkYSize * nDstDataTypeSize))));
}
}
double *padfLineData = static_cast<double *>(
VSI_MALLOC3_VERBOSE(nChunkXSize, nChunkYSize, nDstDataTypeSize));
if (padfLineData == nullptr)
{
return -1;
}
const int nValsPerIter = bComplex ? 2 : 1;
const int nYBlocks = DIV_ROUND_UP(nYSize, nChunkYSize);
const int nXBlocks = DIV_ROUND_UP(nXSize, nChunkXSize);
for (int iYBlock = 0; iYBlock < nYBlocks; ++iYBlock)
{
const int iYStart = iYBlock * nChunkYSize;
const int iYEnd =
iYBlock == nYBlocks - 1 ? nYSize : iYStart + nChunkYSize;
const int nChunkActualHeight = iYEnd - iYStart;
for (int iXBlock = 0; iXBlock < nXBlocks; ++iXBlock)
{
const int iXStart = iXBlock * nChunkXSize;
const int iXEnd =
iXBlock == nXBlocks - 1 ? nXSize : iXStart + nChunkXSize;
const int nChunkActualXSize = iXEnd - iXStart;
if (GDALRasterIO(
hBand, GF_Read, iXStart, iYStart, nChunkActualXSize,
nChunkActualHeight, padfLineData, nChunkActualXSize,
nChunkActualHeight, eDstDataType, 0, 0) != CE_None)
{
CPLError(CE_Failure, CPLE_FileIO,
"Checksum value could not be computed due to I/O "
"read error.");
nChecksum = -1;
break;
}
const size_t xIters =
static_cast<size_t>(nValsPerIter) * nChunkActualXSize;
for (int iY = iYStart; iY < iYEnd; ++iY)
{
// Initialize iPrime so that it is consistent with a
// per full line iteration strategy
iPrime = (nValsPerIter *
(static_cast<int64_t>(iY) * nXSize + iXStart)) %
11;
const size_t nOffset = nValsPerIter *
static_cast<size_t>(iY - iYStart) *
nChunkActualXSize;
for (size_t i = 0; i < xIters; ++i)
{
const double dfVal = padfLineData[nOffset + i];
nChecksum += IntFromDouble(dfVal) % anPrimes[iPrime++];
if (iPrime > 10)
iPrime = 0;
}
nChecksum &= 0xffff;
}
}
if (nChecksum < 0)
break;
}
CPLFree(padfLineData);
}
else if (bIsFloatingPoint)
{
const GDALDataType eDstDataType = bComplex ? GDT_CFloat64 : GDT_Float64;
double *padfLineData = static_cast<double *>(VSI_MALLOC2_VERBOSE(
nXSize, GDALGetDataTypeSizeBytes(eDstDataType)));
if (padfLineData == nullptr)
{
return -1;
}
for (int iLine = nYOff; iLine < nYOff + nYSize; iLine++)
{
if (GDALRasterIO(hBand, GF_Read, nXOff, iLine, nXSize, 1,
padfLineData, nXSize, 1, eDstDataType, 0,
0) != CE_None)
{
CPLError(CE_Failure, CPLE_FileIO,
"Checksum value couldn't be computed due to "
"I/O read error.");
nChecksum = -1;
break;
}
const size_t nCount = bComplex ? static_cast<size_t>(nXSize) * 2
: static_cast<size_t>(nXSize);
for (size_t i = 0; i < nCount; i++)
{
const double dfVal = padfLineData[i];
nChecksum += IntFromDouble(dfVal) % anPrimes[iPrime++];
if (iPrime > 10)
iPrime = 0;
nChecksum &= 0xffff;
}
}
CPLFree(padfLineData);
}
else if (nXOff == 0 && nYOff == 0)
{
const GDALDataType eDstDataType = bComplex ? GDT_CInt32 : GDT_Int32;
int nBlockXSize = 0;
int nBlockYSize = 0;
GDALGetBlockSize(hBand, &nBlockXSize, &nBlockYSize);
const int nDstDataTypeSize = GDALGetDataTypeSizeBytes(eDstDataType);
int nChunkXSize = nBlockXSize;
const int nChunkYSize = nBlockYSize;
if (nBlockXSize < nXSize)
{
const GIntBig nMaxChunkSize =
std::max(static_cast<GIntBig>(10 * 1000 * 1000),
GDALGetCacheMax64() / 10);
if (nDstDataTypeSize > 0 &&
static_cast<GIntBig>(nXSize) * nChunkYSize <
nMaxChunkSize / nDstDataTypeSize)
{
// A full line of height nChunkYSize can fit in the maximum
// allowed memory
nChunkXSize = nXSize;
}
else if (nDstDataTypeSize > 0)
{
// Otherwise compute a size that is a multiple of nBlockXSize
nChunkXSize = static_cast<int>(std::min(
static_cast<GIntBig>(nXSize),
nBlockXSize *
std::max(static_cast<GIntBig>(1),
nMaxChunkSize /
(static_cast<GIntBig>(nBlockXSize) *
nChunkYSize * nDstDataTypeSize))));
}
}
int *panChunkData = static_cast<GInt32 *>(
VSI_MALLOC3_VERBOSE(nChunkXSize, nChunkYSize, nDstDataTypeSize));
if (panChunkData == nullptr)
{
return -1;
}
const int nValsPerIter = bComplex ? 2 : 1;
const int nYBlocks = DIV_ROUND_UP(nYSize, nChunkYSize);
const int nXBlocks = DIV_ROUND_UP(nXSize, nChunkXSize);
for (int iYBlock = 0; iYBlock < nYBlocks; ++iYBlock)
{
const int iYStart = iYBlock * nChunkYSize;
const int iYEnd =
iYBlock == nYBlocks - 1 ? nYSize : iYStart + nChunkYSize;
const int nChunkActualHeight = iYEnd - iYStart;
for (int iXBlock = 0; iXBlock < nXBlocks; ++iXBlock)
{
const int iXStart = iXBlock * nChunkXSize;
const int iXEnd =
iXBlock == nXBlocks - 1 ? nXSize : iXStart + nChunkXSize;
const int nChunkActualXSize = iXEnd - iXStart;
if (GDALRasterIO(
hBand, GF_Read, iXStart, iYStart, nChunkActualXSize,
nChunkActualHeight, panChunkData, nChunkActualXSize,
nChunkActualHeight, eDstDataType, 0, 0) != CE_None)
{
CPLError(CE_Failure, CPLE_FileIO,
"Checksum value could not be computed due to I/O "
"read error.");
nChecksum = -1;
break;
}
const size_t xIters =
static_cast<size_t>(nValsPerIter) * nChunkActualXSize;
for (int iY = iYStart; iY < iYEnd; ++iY)
{
// Initialize iPrime so that it is consistent with a
// per full line iteration strategy
iPrime = (nValsPerIter *
(static_cast<int64_t>(iY) * nXSize + iXStart)) %
11;
const size_t nOffset = nValsPerIter *
static_cast<size_t>(iY - iYStart) *
nChunkActualXSize;
for (size_t i = 0; i < xIters; ++i)
{
nChecksum +=
panChunkData[nOffset + i] % anPrimes[iPrime++];
if (iPrime > 10)
iPrime = 0;
}
nChecksum &= 0xffff;
}
}
if (nChecksum < 0)
break;
}
CPLFree(panChunkData);
}
else
{
const GDALDataType eDstDataType = bComplex ? GDT_CInt32 : GDT_Int32;
int *panLineData = static_cast<GInt32 *>(VSI_MALLOC2_VERBOSE(
nXSize, GDALGetDataTypeSizeBytes(eDstDataType)));
if (panLineData == nullptr)
{
return -1;
}
for (int iLine = nYOff; iLine < nYOff + nYSize; iLine++)
{
if (GDALRasterIO(hBand, GF_Read, nXOff, iLine, nXSize, 1,
panLineData, nXSize, 1, eDstDataType, 0,
0) != CE_None)
{
CPLError(CE_Failure, CPLE_FileIO,
"Checksum value could not be computed due to I/O "
"read error.");
nChecksum = -1;
break;
}
const size_t nCount = bComplex ? static_cast<size_t>(nXSize) * 2
: static_cast<size_t>(nXSize);
for (size_t i = 0; i < nCount; i++)
{
nChecksum += panLineData[i] % anPrimes[iPrime++];
if (iPrime > 10)
iPrime = 0;
nChecksum &= 0xffff;
}
}
CPLFree(panLineData);
}
// coverity[return_overflow]
return nChecksum;
}
|