Skip to content

Commit

Permalink
Merge pull request #11791 from dbaston/rasterize-frac
Browse files Browse the repository at this point in the history
rasterize: Minor readability improvements
  • Loading branch information
rouault authored Feb 4, 2025
2 parents 8d8fb57 + c4d4c62 commit 28c687a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 49 deletions.
23 changes: 11 additions & 12 deletions alg/gdal_alg_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,31 @@ typedef enum
/* Low level rasterizer API. */
/************************************************************************/

typedef void (*llScanlineFunc)(void *, int, int, int, double);
typedef void (*llPointFunc)(void *, int, int, double);
typedef void (*llScanlineFunc)(GDALRasterizeInfo *, int, int, int, double);
typedef void (*llPointFunc)(GDALRasterizeInfo *, int, int, double);

void GDALdllImagePoint(int nRasterXSize, int nRasterYSize, int nPartCount,
const int *panPartSize, const double *padfX,
const double *padfY, const double *padfVariant,
llPointFunc pfnPointFunc, void *pCBData);
llPointFunc pfnPointFunc, GDALRasterizeInfo *pCBData);

void GDALdllImageLine(int nRasterXSize, int nRasterYSize, int nPartCount,
const int *panPartSize, const double *padfX,
const double *padfY, const double *padfVariant,
llPointFunc pfnPointFunc, void *pCBData);
llPointFunc pfnPointFunc, GDALRasterizeInfo *pCBData);

void GDALdllImageLineAllTouched(int nRasterXSize, int nRasterYSize,
int nPartCount, const int *panPartSize,
const double *padfX, const double *padfY,
const double *padfVariant,
llPointFunc pfnPointFunc, void *pCBData,
bool bAvoidBurningSamePoints,
bool bIntersectOnly);
void GDALdllImageLineAllTouched(
int nRasterXSize, int nRasterYSize, int nPartCount, const int *panPartSize,
const double *padfX, const double *padfY, const double *padfVariant,
llPointFunc pfnPointFunc, GDALRasterizeInfo *pCBData,
bool bAvoidBurningSamePoints, bool bIntersectOnly);

void GDALdllImageFilledPolygon(int nRasterXSize, int nRasterYSize,
int nPartCount, const int *panPartSize,
const double *padfX, const double *padfY,
const double *padfVariant,
llScanlineFunc pfnScanlineFunc, void *pCBData,
llScanlineFunc pfnScanlineFunc,
GDALRasterizeInfo *pCBData,
bool bAvoidBurningSamePoints);

CPL_C_END
Expand Down
53 changes: 40 additions & 13 deletions alg/gdalrasterize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,10 @@ static inline void gvBurnScanlineInt64UserBurnValue(GDALRasterizeInfo *psInfo,
/************************************************************************/
/* gvBurnScanline() */
/************************************************************************/
static void gvBurnScanline(void *pCBData, int nY, int nXStart, int nXEnd,
double dfVariant)
static void gvBurnScanline(GDALRasterizeInfo *psInfo, int nY, int nXStart,
int nXEnd, double dfVariant)

{
GDALRasterizeInfo *psInfo = static_cast<GDALRasterizeInfo *>(pCBData);

if (nXStart > nXEnd)
return;

Expand Down Expand Up @@ -313,10 +311,10 @@ static inline void gvBurnPointInt64UserBurnValue(GDALRasterizeInfo *psInfo,
/************************************************************************/
/* gvBurnPoint() */
/************************************************************************/
static void gvBurnPoint(void *pCBData, int nY, int nX, double dfVariant)
static void gvBurnPoint(GDALRasterizeInfo *psInfo, int nY, int nX,
double dfVariant)

{
GDALRasterizeInfo *psInfo = static_cast<GDALRasterizeInfo *>(pCBData);

CPLAssert(nY >= 0 && nY < psInfo->nYSize);
CPLAssert(nX >= 0 && nX < psInfo->nXSize);
Expand Down Expand Up @@ -539,9 +537,35 @@ static void GDALCollectRingsFromGeometry(const OGRGeometry *poShape,
}
}

/************************************************************************/
/* gv_rasterize_one_shape() */
/************************************************************************/
/************************************************************************
* gv_rasterize_one_shape()
*
* @param pabyChunkBuf buffer to which values will be burned
* @param nXOff chunk column offset from left edge of raster
* @param nYOff chunk scanline offset from top of raster
* @param nXSize number of columns in chunk
* @param nYSize number of rows in chunk
* @param nBands number of bands in chunk
* @param eType data type of pabyChunkBuf
* @param nPixelSpace number of bytes between adjacent pixels in chunk
* (0 to calculate automatically)
* @param nLineSpace number of bytes between adjacent scanlines in chunk
* (0 to calculate automatically)
* @param nBandSpace number of bytes between adjacent bands in chunk
* (0 to calculate automatically)
* @param bAllTouched burn value to all touched pixels?
* @param poShape geometry to rasterize, in original coordinates
* @param eBurnValueType type of value to be burned (must be Float64 or Int64)
* @param padfBurnValues array of nBands values to burn (Float64), or nullptr
* @param panBurnValues array of nBands values to burn (Int64), or nullptr
* @param eBurnValueSrc whether to burn values from padfBurnValues /
* panBurnValues, or from the Z or M values of poShape
* @param eMergeAlg whether the burn value should replace or be added to the
* existing values
* @param pfnTransformer transformer from CRS of geometry to pixel/line
* coordinates of raster
* @param pTransformArg arguments to pass to pfnTransformer
************************************************************************/
static void gv_rasterize_one_shape(
unsigned char *pabyChunkBuf, int nXOff, int nYOff, int nXSize, int nYSize,
int nBands, GDALDataType eType, int nPixelSpace, GSpacing nLineSpace,
Expand Down Expand Up @@ -614,10 +638,13 @@ static void gv_rasterize_one_shape(
/* Transform polygon geometries into a set of rings and a part */
/* size list. */
/* -------------------------------------------------------------------- */
std::vector<double> aPointX;
std::vector<double> aPointY;
std::vector<double> aPointVariant;
std::vector<int> aPartSize;
std::vector<double>
aPointX; // coordinate X values from all rings/components
std::vector<double>
aPointY; // coordinate Y values from all rings/components
std::vector<double> aPointVariant; // coordinate Z values
std::vector<int> aPartSize; // number of X/Y/(Z) values associated with
// each ring/component

GDALCollectRingsFromGeometry(poShape, aPointX, aPointY, aPointVariant,
aPartSize, eBurnValueSrc);
Expand Down
22 changes: 10 additions & 12 deletions alg/llrasterize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ void GDALdllImageFilledPolygon(int nRasterXSize, int nRasterYSize,
int nPartCount, const int *panPartSize,
const double *padfX, const double *padfY,
const double *dfVariant,
llScanlineFunc pfnScanlineFunc, void *pCBData,
llScanlineFunc pfnScanlineFunc,
GDALRasterizeInfo *pCBData,
bool bAvoidBurningSamePoints)
{
if (!nPartCount)
Expand Down Expand Up @@ -232,7 +233,7 @@ void GDALdllImageFilledPolygon(int nRasterXSize, int nRasterYSize,
void GDALdllImagePoint(int nRasterXSize, int nRasterYSize, int nPartCount,
const int * /*panPartSize*/, const double *padfX,
const double *padfY, const double *padfVariant,
llPointFunc pfnPointFunc, void *pCBData)
llPointFunc pfnPointFunc, GDALRasterizeInfo *pCBData)
{
for (int i = 0; i < nPartCount; i++)
{
Expand All @@ -254,7 +255,7 @@ void GDALdllImagePoint(int nRasterXSize, int nRasterYSize, int nPartCount,
void GDALdllImageLine(int nRasterXSize, int nRasterYSize, int nPartCount,
const int *panPartSize, const double *padfX,
const double *padfY, const double *padfVariant,
llPointFunc pfnPointFunc, void *pCBData)
llPointFunc pfnPointFunc, GDALRasterizeInfo *pCBData)
{
if (!nPartCount)
return;
Expand All @@ -272,8 +273,7 @@ void GDALdllImageLine(int nRasterXSize, int nRasterYSize, int nPartCount,
double dfVariant = 0.0;
double dfVariant1 = 0.0;
if (padfVariant != nullptr &&
static_cast<GDALRasterizeInfo *>(pCBData)->eBurnValueSource !=
GBV_UserBurnValue)
pCBData->eBurnValueSource != GBV_UserBurnValue)
{
dfVariant = padfVariant[n + j - 1];
dfVariant1 = padfVariant[n + j];
Expand Down Expand Up @@ -380,13 +380,11 @@ void GDALdllImageLine(int nRasterXSize, int nRasterYSize, int nPartCount,
/* will be drawn with the burn value. */
/************************************************************************/

void GDALdllImageLineAllTouched(int nRasterXSize, int nRasterYSize,
int nPartCount, const int *panPartSize,
const double *padfX, const double *padfY,
const double *padfVariant,
llPointFunc pfnPointFunc, void *pCBData,
bool bAvoidBurningSamePoints,
bool bIntersectOnly)
void GDALdllImageLineAllTouched(
int nRasterXSize, int nRasterYSize, int nPartCount, const int *panPartSize,
const double *padfX, const double *padfY, const double *padfVariant,
llPointFunc pfnPointFunc, GDALRasterizeInfo *pCBData,
bool bAvoidBurningSamePoints, bool bIntersectOnly)

{
// This is an epsilon to detect geometries that are aligned with pixel
Expand Down
17 changes: 5 additions & 12 deletions apps/gdal_rasterize_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ static GDALDatasetH CreateOutputDataset(
* @param hSrcDataset the source dataset handle.
* @param psOptionsIn the options struct returned by GDALRasterizeOptionsNew()
* or NULL.
* @param pbUsageError pointer to a integer output variable to store if any
* @param pbUsageError pointer to an integer output variable to store if any
* usage error has occurred or NULL.
* @return the output dataset (new dataset that must be closed using
* GDALClose(), or hDstDS is not NULL) or NULL in case of error.
Expand Down Expand Up @@ -987,12 +987,13 @@ GDALDatasetH GDALRasterize(const char *pszDest, GDALDatasetH hDstDS,
return nullptr;
}

GDALRasterizeOptions *psOptionsToFree = nullptr;
std::unique_ptr<GDALRasterizeOptions, decltype(&GDALRasterizeOptionsFree)>
psOptionsToFree(nullptr, GDALRasterizeOptionsFree);
const GDALRasterizeOptions *psOptions = psOptionsIn;
if (psOptions == nullptr)
{
psOptionsToFree = GDALRasterizeOptionsNew(nullptr, nullptr);
psOptions = psOptionsToFree;
psOptionsToFree.reset(GDALRasterizeOptionsNew(nullptr, nullptr));
psOptions = psOptionsToFree.get();
}

const bool bCloseOutDSOnError = hDstDS == nullptr;
Expand All @@ -1007,7 +1008,6 @@ GDALDatasetH GDALRasterize(const char *pszDest, GDALDatasetH hDstDS,
"has not one single layer.");
if (pbUsageError)
*pbUsageError = TRUE;
GDALRasterizeOptionsFree(psOptionsToFree);
return nullptr;
}

Expand All @@ -1026,7 +1026,6 @@ GDALDatasetH GDALRasterize(const char *pszDest, GDALDatasetH hDstDS,
osFormat = GetOutputDriverForRaster(pszDest);
if (osFormat.empty())
{
GDALRasterizeOptionsFree(psOptionsToFree);
return nullptr;
}
}
Expand All @@ -1053,7 +1052,6 @@ GDALDatasetH GDALRasterize(const char *pszDest, GDALDatasetH hDstDS,
"Output driver `%s' not recognised or does not support "
"direct output file creation.",
osFormat.c_str());
GDALRasterizeOptionsFree(psOptionsToFree);
return nullptr;
}
}
Expand Down Expand Up @@ -1122,7 +1120,6 @@ GDALDatasetH GDALRasterize(const char *pszDest, GDALDatasetH hDstDS,
if (hDstDS == nullptr)
{
GDALDatasetReleaseResultSet(hSrcDataset, hLayer);
GDALRasterizeOptionsFree(psOptionsToFree);
return nullptr;
}
}
Expand Down Expand Up @@ -1167,7 +1164,6 @@ GDALDatasetH GDALRasterize(const char *pszDest, GDALDatasetH hDstDS,
psOptions->aosLayers.size() > static_cast<size_t>(i)
? psOptions->aosLayers[i].c_str()
: "0");
GDALRasterizeOptionsFree(psOptionsToFree);
return nullptr;
}
if (eOutputType == GDT_Unknown)
Expand All @@ -1193,7 +1189,6 @@ GDALDatasetH GDALRasterize(const char *pszDest, GDALDatasetH hDstDS,
psOptions->osNoData.c_str());
if (hDstDS == nullptr)
{
GDALRasterizeOptionsFree(psOptionsToFree);
return nullptr;
}
}
Expand Down Expand Up @@ -1247,8 +1242,6 @@ GDALDatasetH GDALRasterize(const char *pszDest, GDALDatasetH hDstDS,
break;
}

GDALRasterizeOptionsFree(psOptionsToFree);

if (eErr != CE_None)
{
if (bCloseOutDSOnError)
Expand Down

0 comments on commit 28c687a

Please sign in to comment.