Skip to content

Commit

Permalink
Remove use of pixCreateTemplateNoInit() where it may cause problems
Browse files Browse the repository at this point in the history
* namely, where it is not obvious that all pixels are later initialized
* specifically in pixUnsharpMaskingGray2D() (possibly related
  Tesseract issue #644)
  • Loading branch information
DanBloomberg committed Jun 8, 2017
1 parent d20c5a6 commit b54c6ce
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/convolve.c
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ PIXTILING *pt;
* although the filter is symmetric with respect to its origin,
* the implementation is asymmetric -- see the implementation in
* pixBlockconvGrayTile(). */
if ((pixd = pixCreateTemplateNoInit(pixs)) == NULL) {
if ((pixd = pixCreateTemplate(pixs)) == NULL) {
pixDestroy(&pixs);
return (PIX *)ERROR_PTR("pixd not made", procName, NULL);
}
Expand Down
6 changes: 5 additions & 1 deletion src/enhance.c
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,11 @@ FPIX *fpix;
}

/* For halfwidth == 2, do the low pass separably. Store
* the result of horizontal smoothing in an intermediate fpix. */
* the result of horizontal smoothing in an intermediate fpix.
* Note that in the horizontal smoothing step, the values of
* fpix in the 2 pixel-wide border region are unchanged from 0.
* In vertical smoothing, the normalization constant takes this
* into account for pixels near the upper and lower boundaries. */
fpix = fpixCreate(w, h);
dataf = fpixGetData(fpix);
wplf = fpixGetWpl(fpix);
Expand Down
10 changes: 4 additions & 6 deletions src/graymorph.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,7 @@ PIX *pixd;
if (pixGetDepth(pixs) != 8)
return (PIX *)ERROR_PTR("pixs not 8 bpp", procName, NULL);

pixd = pixCreateTemplateNoInit(pixs);
pixSetBorderVal(pixd, 4, 8, 2, 8, 0); /* only to silence valgrind */
pixd = pixCreateTemplate(pixs);
pixGetDimensions(pixs, &w, &h, NULL);
datas = pixGetData(pixs);
datad = pixGetData(pixd);
Expand Down Expand Up @@ -793,7 +792,7 @@ PIX *pixd;
if (pixGetDepth(pixs) != 8)
return (PIX *)ERROR_PTR("pixs not 8 bpp", procName, NULL);

pixd = pixCreateTemplateNoInit(pixs);
pixd = pixCreateTemplate(pixs);
pixGetDimensions(pixs, &w, &h, NULL);
datas = pixGetData(pixs);
datad = pixGetData(pixd);
Expand Down Expand Up @@ -912,8 +911,7 @@ PIX *pixd;
if (pixGetDepth(pixs) != 8)
return (PIX *)ERROR_PTR("pixs not 8 bpp", procName, NULL);

pixd = pixCreateTemplateNoInit(pixs);
pixSetBorderVal(pixd, 4, 8, 2, 8, 0); /* only to silence valgrind */
pixd = pixCreateTemplate(pixs);
pixGetDimensions(pixs, &w, &h, NULL);
datas = pixGetData(pixs);
datad = pixGetData(pixd);
Expand Down Expand Up @@ -977,7 +975,7 @@ PIX *pixd;
if (pixGetDepth(pixs) != 8)
return (PIX *)ERROR_PTR("pixs not 8 bpp", procName, NULL);

pixd = pixCreateTemplateNoInit(pixs);
pixd = pixCreateTemplate(pixs);
pixGetDimensions(pixs, &w, &h, NULL);
datas = pixGetData(pixs);
datad = pixGetData(pixd);
Expand Down
4 changes: 2 additions & 2 deletions src/morphdwa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ PIX *pixt1, *pixt2, *pixt3;
getExtendedCompositeParameters(vsize, &nv, &extrav, NULL);

/* Horizontal dilation first: pixs --> pixt2. Do not alter pixs. */
pixt1 = pixCreateTemplateNoInit(pixs); /* temp image */
pixt1 = pixCreateTemplate(pixs); /* temp image */
if (hsize == 1) {
pixt2 = pixClone(pixs);
} else if (hsize < 64) {
Expand Down Expand Up @@ -1335,7 +1335,7 @@ PIX *pixt1, *pixt2, *pixt3;
getExtendedCompositeParameters(vsize, &nv, &extrav, NULL);

/* Horizontal erosion first: pixs --> pixt2. Do not alter pixs. */
pixt1 = pixCreateTemplateNoInit(pixs); /* temp image */
pixt1 = pixCreateTemplate(pixs); /* temp image */
if (hsize == 1) {
pixt2 = pixClone(pixs);
} else if (hsize < 64) {
Expand Down
1 change: 0 additions & 1 deletion src/pix1.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,6 @@ PIX *pixd;
pixCopyColormap(pixd, pixs);
pixCopyText(pixd, pixs);
pixCopyInputFormat(pixd, pixs);

return pixd;
}

Expand Down
7 changes: 4 additions & 3 deletions src/pix2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1673,9 +1673,11 @@ l_int32 i, j, w, h;
* Notes:
* (1) pixd can be null, but otherwise it must be the same size
* and depth as pixs. Always returns pixd.
* (1) This is useful in situations where by setting a few border
* (2) This is useful in situations where by setting a few border
* pixels we can avoid having to copy all pixels in pixs into
* pixd as an initialization step for some operation.
* Nevertheless, for safety, if making a new pixd, all the
* non-border pixels are initialized to 0.
* </pre>
*/
PIX *
Expand All @@ -1702,7 +1704,7 @@ l_int32 w, h;
procName, pixd);
}
} else {
if ((pixd = pixCreateTemplateNoInit(pixs)) == NULL)
if ((pixd = pixCreateTemplate(pixs)) == NULL)
return (PIX *)ERROR_PTR("pixd not made", procName, pixd);
}

Expand All @@ -1711,7 +1713,6 @@ l_int32 w, h;
pixRasterop(pixd, w - right, 0, right, h, PIX_SRC, pixs, w - right, 0);
pixRasterop(pixd, 0, 0, w, top, PIX_SRC, pixs, 0, 0);
pixRasterop(pixd, 0, h - bot, w, bot, PIX_SRC, pixs, 0, h - bot);

return pixd;
}

Expand Down
4 changes: 2 additions & 2 deletions src/pix5.c
Original file line number Diff line number Diff line change
Expand Up @@ -2816,7 +2816,7 @@ PIX *pixd;
return (PIX *)ERROR_PTR("pixs has a colormap", procName, NULL);

pixGetDimensions(pixs, &w, &h, NULL);
pixd = pixCreateTemplateNoInit(pixs);
pixd = pixCreateTemplate(pixs);
datas = pixGetData(pixs);
datad = pixGetData(pixd);
wpl = pixGetWpl(pixs);
Expand Down Expand Up @@ -2869,7 +2869,7 @@ PIX *pixd;
return (PIX *)ERROR_PTR("pixs has a colormap", procName, NULL);

pixGetDimensions(pixs, &w, &h, NULL);
pixd = pixCreateTemplateNoInit(pixs);
pixd = pixCreateTemplate(pixs);
lines8 = pixGetLinePtrs(pixs, NULL);
lined8 = pixGetLinePtrs(pixd, NULL);
for (j = 0; j < w; j++) {
Expand Down
2 changes: 1 addition & 1 deletion src/pixtiling.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
* where the desired tile width is 256 pixels and the overlap is
* 30 pixels on left and right sides:
*
* PIX *pixd = pixCreateTemplateNoInit(pixs); // output
* PIX *pixd = pixCreateTemplate(pixs); // output
* PIXTILING *pt = pixTilingCreate(pixs, 0, 1, 256, 30, 0);
* pixTilingGetCount(pt, &nx, NULL);
* for (j = 0; j < nx; j++) {
Expand Down
104 changes: 50 additions & 54 deletions src/rotate.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ static const l_float32 LIMIT_SHEAR_ANGLE = 0.35; /* radians; ~20 deg */
/*!
* \brief pixRotate()
*
* \param[in] pixs 1, 2, 4, 8, 32 bpp rgb
* \param[in] angle radians; clockwise is positive
* \param[in] type L_ROTATE_AREA_MAP, L_ROTATE_SHEAR, L_ROTATE_SAMPLING
* \param[in] incolor L_BRING_IN_WHITE, L_BRING_IN_BLACK
* \param[in] width original width; use 0 to avoid embedding
* \param[in] height original height; use 0 to avoid embedding
* \param[in] pixs 1, 2, 4, 8, 32 bpp rgb
* \param[in] angle radians; clockwise is positive
* \param[in] type L_ROTATE_AREA_MAP, L_ROTATE_SHEAR, L_ROTATE_SAMPLING
* \param[in] incolor L_BRING_IN_WHITE, L_BRING_IN_BLACK
* \param[in] width original width; use 0 to avoid embedding
* \param[in] height original height; use 0 to avoid embedding
* \return pixd, or NULL on error
*
* <pre>
Expand All @@ -93,8 +93,6 @@ static const l_float32 LIMIT_SHEAR_ANGLE = 0.35; /* radians; ~20 deg */
* original width and height allows the expansion to
* stop at the maximum required size, which is a square
* with side = sqrt(w*w + h*h).
*
* *** Warning: implicit assumption about RGB component ordering ***
* </pre>
*/
PIX *
Expand All @@ -107,7 +105,7 @@ pixRotate(PIX *pixs,
{
l_int32 w, h, d;
l_uint32 fillval;
PIX *pixt1, *pixt2, *pixt3, *pixd;
PIX *pix1, *pix2, *pix3, *pixd;
PIXCMAP *cmap;

PROCNAME("pixRotate");
Expand Down Expand Up @@ -145,10 +143,10 @@ PIXCMAP *cmap;
/* Remove colormap if we rotate by area mapping. */
cmap = pixGetColormap(pixs);
if (cmap && type == L_ROTATE_AREA_MAP)
pixt1 = pixRemoveColormap(pixs, REMOVE_CMAP_BASED_ON_SRC);
pix1 = pixRemoveColormap(pixs, REMOVE_CMAP_BASED_ON_SRC);
else
pixt1 = pixClone(pixs);
cmap = pixGetColormap(pixt1);
pix1 = pixClone(pixs);
cmap = pixGetColormap(pix1);

/* Otherwise, if there is a colormap and we're not embedding,
* add white color if it doesn't exist. */
Expand All @@ -160,22 +158,22 @@ PIXCMAP *cmap;
}

/* Request to embed in a larger image; do if necessary */
pixt2 = pixEmbedForRotation(pixt1, angle, incolor, width, height);
pix2 = pixEmbedForRotation(pix1, angle, incolor, width, height);

/* Area mapping requires 8 or 32 bpp. If less than 8 bpp and
* area map rotation is requested, convert to 8 bpp. */
d = pixGetDepth(pixt2);
d = pixGetDepth(pix2);
if (type == L_ROTATE_AREA_MAP && d < 8)
pixt3 = pixConvertTo8(pixt2, FALSE);
pix3 = pixConvertTo8(pix2, FALSE);
else
pixt3 = pixClone(pixt2);
pix3 = pixClone(pix2);

/* Do the rotation: shear, sampling or area mapping */
pixGetDimensions(pixt3, &w, &h, &d);
pixGetDimensions(pix3, &w, &h, &d);
if (type == L_ROTATE_SHEAR) {
pixd = pixRotateShearCenter(pixt3, angle, incolor);
pixd = pixRotateShearCenter(pix3, angle, incolor);
} else if (type == L_ROTATE_SAMPLING) {
pixd = pixRotateBySampling(pixt3, w / 2, h / 2, angle, incolor);
pixd = pixRotateBySampling(pix3, w / 2, h / 2, angle, incolor);
} else { /* rotate by area mapping */
fillval = 0;
if (incolor == L_BRING_IN_WHITE) {
Expand All @@ -185,26 +183,26 @@ PIXCMAP *cmap;
fillval = 0xffffff00;
}
if (d == 8)
pixd = pixRotateAMGray(pixt3, angle, fillval);
pixd = pixRotateAMGray(pix3, angle, fillval);
else /* d == 32 */
pixd = pixRotateAMColor(pixt3, angle, fillval);
pixd = pixRotateAMColor(pix3, angle, fillval);
}

pixDestroy(&pixt1);
pixDestroy(&pixt2);
pixDestroy(&pixt3);
pixDestroy(&pix1);
pixDestroy(&pix2);
pixDestroy(&pix3);
return pixd;
}


/*!
* \brief pixEmbedForRotation()
*
* \param[in] pixs 1, 2, 4, 8, 32 bpp rgb
* \param[in] angle radians; clockwise is positive
* \param[in] incolor L_BRING_IN_WHITE, L_BRING_IN_BLACK
* \param[in] width original width; use 0 to avoid embedding
* \param[in] height original height; use 0 to avoid embedding
* \param[in] pixs 1, 2, 4, 8, 32 bpp rgb
* \param[in] angle radians; clockwise is positive
* \param[in] incolor L_BRING_IN_WHITE, L_BRING_IN_BLACK
* \param[in] width original width; use 0 to avoid embedding
* \param[in] height original height; use 0 to avoid embedding
* \return pixd, or NULL on error
*
* <pre>
Expand Down Expand Up @@ -305,11 +303,11 @@ PIX *pixd;
/*!
* \brief pixRotateBySampling()
*
* \param[in] pixs 1, 2, 4, 8, 16, 32 bpp rgb; can be cmapped
* \param[in] xcen x value of center of rotation
* \param[in] ycen y value of center of rotation
* \param[in] angle radians; clockwise is positive
* \param[in] incolor L_BRING_IN_WHITE, L_BRING_IN_BLACK
* \param[in] pixs 1, 2, 4, 8, 16, 32 bpp rgb; can be cmapped
* \param[in] xcen x value of center of rotation
* \param[in] ycen y value of center of rotation
* \param[in] angle radians; clockwise is positive
* \param[in] incolor L_BRING_IN_WHITE, L_BRING_IN_BLACK
* \return pixd, or NULL on error
*
* <pre>
Expand Down Expand Up @@ -431,9 +429,9 @@ PIX *pixd;
/*!
* \brief pixRotateBinaryNice()
*
* \param[in] pixs 1 bpp
* \param[in] angle radians; clockwise is positive; about the center
* \param[in] incolor L_BRING_IN_WHITE, L_BRING_IN_BLACK
* \param[in] pixs 1 bpp
* \param[in] angle radians; clockwise is positive; about the center
* \param[in] incolor L_BRING_IN_WHITE, L_BRING_IN_BLACK
* \return pixd, or NULL on error
*
* <pre>
Expand All @@ -458,7 +456,7 @@ pixRotateBinaryNice(PIX *pixs,
l_float32 angle,
l_int32 incolor)
{
PIX *pixt1, *pixt2, *pixt3, *pixt4, *pixd;
PIX *pix1, *pix2, *pix3, *pix4, *pixd;

PROCNAME("pixRotateBinaryNice");

Expand All @@ -467,15 +465,15 @@ PIX *pixt1, *pixt2, *pixt3, *pixt4, *pixd;
if (incolor != L_BRING_IN_WHITE && incolor != L_BRING_IN_BLACK)
return (PIX *)ERROR_PTR("invalid incolor", procName, NULL);

pixt1 = pixConvertTo8(pixs, 0);
pixt2 = pixBlockconv(pixt1, 1, 1); /* smallest blur allowed */
pixt3 = pixRotateAM(pixt2, angle, incolor);
pixt4 = pixUnsharpMasking(pixt3, 1, 1.0); /* sharpen a bit */
pixd = pixThresholdToBinary(pixt4, 128);
pixDestroy(&pixt1);
pixDestroy(&pixt2);
pixDestroy(&pixt3);
pixDestroy(&pixt4);
pix1 = pixConvertTo8(pixs, 0);
pix2 = pixBlockconv(pix1, 1, 1); /* smallest blur allowed */
pix3 = pixRotateAM(pix2, angle, incolor);
pix4 = pixUnsharpMasking(pix3, 1, 1.0); /* sharpen a bit */
pixd = pixThresholdToBinary(pix4, 128);
pixDestroy(&pix1);
pixDestroy(&pix2);
pixDestroy(&pix3);
pixDestroy(&pix4);
return pixd;
}

Expand All @@ -486,11 +484,11 @@ PIX *pixt1, *pixt2, *pixt3, *pixt4, *pixd;
/*!
* \brief pixRotateWithAlpha()
*
* \param[in] pixs 32 bpp rgb or cmapped
* \param[in] angle radians; clockwise is positive
* \param[in] pixg [optional] 8 bpp, can be null
* \param[in] fract between 0.0 and 1.0, with 0.0 fully transparent
* and 1.0 fully opaque
* \param[in] pixs 32 bpp rgb or cmapped
* \param[in] angle radians; clockwise is positive
* \param[in] pixg [optional] 8 bpp, can be null
* \param[in] fract between 0.0 and 1.0, with 0.0 fully transparent
* and 1.0 fully opaque
* \return pixd 32 bpp rgba, or NULL on error
*
* <pre>
Expand Down Expand Up @@ -529,8 +527,6 @@ PIX *pixt1, *pixt2, *pixt3, *pixt4, *pixd;
* pixDestroy(&pixt);
* This has the side-effect of producing artifacts in the very
* dark regions.
*
* *** Warning: implicit assumption about RGB component ordering ***
* </pre>
*/
PIX *
Expand Down

0 comments on commit b54c6ce

Please sign in to comment.