Skip to content

Commit

Permalink
Eliminate off-image error messages for some pixel access operations.
Browse files Browse the repository at this point in the history
* Such messages can send a lot of data to stderr.
  • Loading branch information
DanBloomberg committed Nov 30, 2018
1 parent a069230 commit 47c1d43
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 49 deletions.
48 changes: 28 additions & 20 deletions src/fpix1.c
Expand Up @@ -632,7 +632,11 @@ fpixSetData(FPIX *fpix,
* \param[in] fpix
* \param[in] x,y pixel coords
* \param[out] pval pixel value
* \return 0 if OK; 1 on error
* \return 0 if OK; 1 or 2 on error
*
* Notes:
* (1) If the point is outside the image, this returns an error (2),
* with 0.0 in %pval. To avoid spamming output, it fails silently.
*/
l_ok
fpixGetPixel(FPIX *fpix,
Expand All @@ -651,10 +655,8 @@ l_int32 w, h;
return ERROR_INT("fpix not defined", procName, 1);

fpixGetDimensions(fpix, &w, &h);
if (x < 0 || x >= w)
return ERROR_INT("x out of bounds", procName, 1);
if (y < 0 || y >= h)
return ERROR_INT("y out of bounds", procName, 1);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;

*pval = *(fpix->data + y * w + x);
return 0;
Expand All @@ -667,7 +669,11 @@ l_int32 w, h;
* \param[in] fpix
* \param[in] x,y pixel coords
* \param[in] val pixel value
* \return 0 if OK; 1 on error
* \return 0 if OK; 1 or 2 on error
*
* Notes:
* (1) If the point is outside the image, this returns an error (2),
* with 0.0 in %pval. To avoid spamming output, it fails silently.
*/
l_ok
fpixSetPixel(FPIX *fpix,
Expand All @@ -683,10 +689,8 @@ l_int32 w, h;
return ERROR_INT("fpix not defined", procName, 1);

fpixGetDimensions(fpix, &w, &h);
if (x < 0 || x >= w)
return ERROR_INT("x out of bounds", procName, 1);
if (y < 0 || y >= h)
return ERROR_INT("y out of bounds", procName, 1);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;

*(fpix->data + y * w + x) = val;
return 0;
Expand Down Expand Up @@ -1622,7 +1626,11 @@ dpixSetData(DPIX *dpix,
* \param[in] dpix
* \param[in] x,y pixel coords
* \param[out] pval pixel value
* \return 0 if OK; 1 on error
* \return 0 if OK; 1 or 2 on error
*
* Notes:
* (1) If the point is outside the image, this returns an error (2),
* with 0.0 in %pval. To avoid spamming output, it fails silently.
*/
l_ok
dpixGetPixel(DPIX *dpix,
Expand All @@ -1641,10 +1649,8 @@ l_int32 w, h;
return ERROR_INT("dpix not defined", procName, 1);

dpixGetDimensions(dpix, &w, &h);
if (x < 0 || x >= w)
return ERROR_INT("x out of bounds", procName, 1);
if (y < 0 || y >= h)
return ERROR_INT("y out of bounds", procName, 1);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;

*pval = *(dpix->data + y * w + x);
return 0;
Expand All @@ -1657,7 +1663,11 @@ l_int32 w, h;
* \param[in] dpix
* \param[in] x,y pixel coords
* \param[in] val pixel value
* \return 0 if OK; 1 on error
* \return 0 if OK; 1 or 2 on error
*
* Notes:
* (1) If the point is outside the image, this returns an error (2),
* with 0.0 in %pval. To avoid spamming output, it fails silently.
*/
l_ok
dpixSetPixel(DPIX *dpix,
Expand All @@ -1673,10 +1683,8 @@ l_int32 w, h;
return ERROR_INT("dpix not defined", procName, 1);

dpixGetDimensions(dpix, &w, &h);
if (x < 0 || x >= w)
return ERROR_INT("x out of bounds", procName, 1);
if (y < 0 || y >= h)
return ERROR_INT("y out of bounds", procName, 1);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;

*(dpix->data + y * w + x) = val;
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/pix1.c
Expand Up @@ -464,7 +464,7 @@ PIX *pixd;
return (PIX *)ERROR_PTR("wpl >= 2^29", procName, NULL);
}
wpl = (l_int32)wpl64;
bignum = 4L * wpl * height; /* number of bytes to be requested */
bignum = 4LL * wpl * height; /* number of bytes to be requested */
if (bignum > ((1LL << 31) - 1)) {
L_ERROR("requested w = %d, h = %d, d = %d\n",
procName, width, height, depth);
Expand Down
64 changes: 36 additions & 28 deletions src/pix2.c
Expand Up @@ -159,7 +159,7 @@ LEPT_DLL l_float32 AlphaMaskBorderVals[2] = {0.0, 0.5};
* \param[in] pix
* \param[in] x,y pixel coords
* \param[out] pval pixel value
* \return 0 if OK; 1 on error
* \return 0 if OK; 1 or 2 on error
*
* <pre>
* Notes:
Expand All @@ -172,7 +172,7 @@ LEPT_DLL l_float32 AlphaMaskBorderVals[2] = {0.0, 0.5};
* * GET_DATA/SET_DATA: ~350 MPix/sec
* If speed is important and you're doing random access into
* the pix, use pixGetLinePtrs() and the array access macros.
* (3) If the point is outside the image, this returns an error (1),
* (3) If the point is outside the image, this returns an error (2),
* with 0 in %pval. To avoid spamming output, it fails silently.
* </pre>
*/
Expand All @@ -195,7 +195,7 @@ l_uint32 *line, *data;

pixGetDimensions(pix, &w, &h, &d);
if (x < 0 || x >= w || y < 0 || y >= h)
return 1;
return 2;

wpl = pixGetWpl(pix);
data = pixGetData(pix);
Expand Down Expand Up @@ -235,7 +235,7 @@ l_uint32 *line, *data;
* \param[in] pix
* \param[in] x,y pixel coords
* \param[in] val value to be inserted
* \return 0 if OK; 1 on error
* \return 0 if OK; 1 or 2 on error
*
* <pre>
* Notes:
Expand All @@ -245,6 +245,8 @@ l_uint32 *line, *data;
* * For d == 2, 4, 8 and 16, %val is masked to the maximum allowable
* pixel value, and any (invalid) higher order bits are discarded.
* (2) See pixGetPixel() for information on performance.
* (3) If the point is outside the image, this returns an error (2),
* with 0 in %pval. To avoid spamming output, it fails silently.
* </pre>
*/
l_ok
Expand All @@ -261,10 +263,8 @@ l_uint32 *line, *data;
if (!pix)
return ERROR_INT("pix not defined", procName, 1);
pixGetDimensions(pix, &w, &h, &d);
if (x < 0 || x >= w)
return ERROR_INT("x out of bounds", procName, 1);
if (y < 0 || y >= h)
return ERROR_INT("y out of bounds", procName, 1);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;

data = pixGetData(pix);
wpl = pixGetWpl(pix);
Expand Down Expand Up @@ -308,7 +308,11 @@ l_uint32 *line, *data;
* \param[out] prval [optional] red component
* \param[out] pgval [optional] green component
* \param[out] pbval [optional] blue component
* \return 0 if OK; 1 on error
* \return 0 if OK; 1 or 2 on error
*
* Notes:
* (1) If the point is outside the image, this returns an error (2),
* with 0 in %pval. To avoid spamming output, it fails silently.
*/
l_ok
pixGetRGBPixel(PIX *pix,
Expand All @@ -333,10 +337,8 @@ l_uint32 *data, *ppixel;
pixGetDimensions(pix, &w, &h, &d);
if (d != 32)
return ERROR_INT("pix not 32 bpp", procName, 1);
if (x < 0 || x >= w)
return ERROR_INT("x out of bounds", procName, 1);
if (y < 0 || y >= h)
return ERROR_INT("y out of bounds", procName, 1);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;

wpl = pixGetWpl(pix);
data = pixGetData(pix);
Expand All @@ -356,7 +358,11 @@ l_uint32 *data, *ppixel;
* \param[in] rval red component
* \param[in] gval green component
* \param[in] bval blue component
* \return 0 if OK; 1 on error
* \return 0 if OK; 1 or 2 on error
*
* Notes:
* (1) If the point is outside the image, this returns an error (2),
* with 0 in %pval. To avoid spamming output, it fails silently.
*/
l_ok
pixSetRGBPixel(PIX *pix,
Expand All @@ -377,10 +383,8 @@ l_uint32 *data, *line;
pixGetDimensions(pix, &w, &h, &d);
if (d != 32)
return ERROR_INT("pix not 32 bpp", procName, 1);
if (x < 0 || x >= w)
return ERROR_INT("x out of bounds", procName, 1);
if (y < 0 || y >= h)
return ERROR_INT("y out of bounds", procName, 1);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;

wpl = pixGetWpl(pix);
data = pixGetData(pix);
Expand Down Expand Up @@ -449,7 +453,11 @@ PIXCMAP *cmap;
*
* \param[in] pix any depth; warning if colormapped
* \param[in] x,y pixel coords
* \return 0 if OK; 1 on error.
* \return 0 if OK; 1 or 2 on error.
*
* Notes:
* (1) If the point is outside the image, this returns an error (2),
* with 0 in %pval. To avoid spamming output, it fails silently.
*/
l_ok
pixClearPixel(PIX *pix,
Expand All @@ -466,10 +474,8 @@ l_uint32 *line, *data;
if (pixGetColormap(pix))
L_WARNING("cmapped: setting to 0 may not be intended\n", procName);
pixGetDimensions(pix, &w, &h, &d);
if (x < 0 || x >= w)
return ERROR_INT("x out of bounds", procName, 1);
if (y < 0 || y >= h)
return ERROR_INT("y out of bounds", procName, 1);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;

wpl = pixGetWpl(pix);
data = pixGetData(pix);
Expand Down Expand Up @@ -507,7 +513,11 @@ l_uint32 *line, *data;
*
* \param[in] pix any depth, warning if colormapped
* \param[in] x,y pixel coords
* \return 0 if OK; 1 on error
* \return 0 if OK; 1 or 2 on error
*
* Notes:
* (1) If the point is outside the image, this returns an error (2),
* with 0 in %pval. To avoid spamming output, it fails silently.
*/
l_ok
pixFlipPixel(PIX *pix,
Expand All @@ -525,10 +535,8 @@ l_uint32 *line, *data;
if (pixGetColormap(pix))
L_WARNING("cmapped: setting to 0 may not be intended\n", procName);
pixGetDimensions(pix, &w, &h, &d);
if (x < 0 || x >= w)
return ERROR_INT("x out of bounds", procName, 1);
if (y < 0 || y >= h)
return ERROR_INT("y out of bounds", procName, 1);
if (x < 0 || x >= w || y < 0 || y >= h)
return 2;

data = pixGetData(pix);
wpl = pixGetWpl(pix);
Expand Down

0 comments on commit 47c1d43

Please sign in to comment.