diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2016-03-27 01:46:26 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2016-03-27 01:46:26 +0000 |
commit | dff6b9fdfeb78fe21a66aa459ddc1d5f7e362dfa (patch) | |
tree | b147568ccffc4cdba9e2a98de1452450ba8e55c3 /analyzer/pamtilt.c | |
parent | 4ce684c4978610d1ea42be1b00f7332f3f5f337a (diff) | |
download | netpbm-mirror-dff6b9fdfeb78fe21a66aa459ddc1d5f7e362dfa.tar.gz netpbm-mirror-dff6b9fdfeb78fe21a66aa459ddc1d5f7e362dfa.tar.xz netpbm-mirror-dff6b9fdfeb78fe21a66aa459ddc1d5f7e362dfa.zip |
Promote Advanced (10.73) to Stable
git-svn-id: http://svn.code.sf.net/p/netpbm/code/stable@2692 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'analyzer/pamtilt.c')
-rw-r--r-- | analyzer/pamtilt.c | 167 |
1 files changed, 105 insertions, 62 deletions
diff --git a/analyzer/pamtilt.c b/analyzer/pamtilt.c index d70f491b..302c6247 100644 --- a/analyzer/pamtilt.c +++ b/analyzer/pamtilt.c @@ -47,7 +47,7 @@ abandon(void) { static void -parseCommandLine(int argc, char *argv[], +parseCommandLine(int argc, const char ** const argv, struct cmdlineInfo * const cmdlineP) { static optEntry option_def[50]; @@ -76,7 +76,7 @@ parseCommandLine(int argc, char *argv[], opt.opt_table = option_def; opt.short_allowed = FALSE; /* no short options used */ opt.allowNegNum = FALSE; /* don't allow negative values */ - optParseOptions3(&argc, argv, opt, sizeof(opt), 0); + pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); if (cmdlineP->hstep < 1) pm_error("-hstep must be at least 1 column."); @@ -129,11 +129,13 @@ static void load(const struct pam * const pamP, unsigned int const hstep, sample *** const pixelsP, - unsigned int * const hsamplesP) { + unsigned int * const hsampleCtP) { /*---------------------------------------------------------------------------- - read file into memory, returning array of rows + Read part of the raster - to wit, every hstep'th column of every row. + Return that sampling of the raster as *pixelsP, and the resulting + array width (pixels in each row) as *hsampleCtP. -----------------------------------------------------------------------------*/ - unsigned int const hsamples = 1 + (pamP->width - 1) / hstep; + unsigned int const hsampleCt = 1 + (pamP->width - 1) / hstep; /* use only this many cols */ unsigned int row; @@ -143,9 +145,6 @@ load(const struct pam * const pamP, tuplerow = pnm_allocpamrow(pamP); MALLOCARRAY(pixels, pamP->height); - if (pixels == NULL) - pm_error("Unable to allocate array of %u pixel rows", - pamP->height); if (pixels == NULL) pm_error("Unable to allocate array of %u rows", pamP->height); @@ -156,19 +155,19 @@ load(const struct pam * const pamP, pnm_readpamrow(pamP, tuplerow); - MALLOCARRAY(pixels[row], hsamples); + MALLOCARRAY(pixels[row], hsampleCt); if (pixels[row] == NULL) pm_error("Unable to allocate %u-sample array for Row %u", - hsamples, row); + hsampleCt, row); /* save every hstep'th column */ - for (i = col = 0; i < hsamples; ++i, col += hstep) + for (i = col = 0; i < hsampleCt; ++i, col += hstep) pixels[row][i] = tuplerow[col][0]; } pnm_freepamrow(tuplerow); - *hsamplesP = hsamples; + *hsampleCtP = hsampleCt; *pixelsP = pixels; } @@ -192,7 +191,7 @@ static void replacePixelValuesWithScaledDiffs( const struct pam * const pamP, sample ** const pixels, - unsigned int const hsamples, + unsigned int const hsampleCt, unsigned int const dstep) { /*---------------------------------------------------------------------------- Replace pixel values with scaled differences used for all @@ -204,7 +203,7 @@ replacePixelValuesWithScaledDiffs( for (row = dstep; row < pamP->height; ++row) { unsigned int col; - for (col = 0; col < hsamples; ++col) { + for (col = 0; col < hsampleCt; ++col) { int const d = pixels[row - dstep][col] - pixels[row][col]; unsigned int const absd = d < 0 ? -d : d; pixels[row - dstep][col] = m * absd; /* scale the difference */ @@ -214,9 +213,46 @@ replacePixelValuesWithScaledDiffs( +static unsigned long +totalBrightness(sample ** const pixels, + unsigned int const hsampleCt, + unsigned int const startRow, + float const dy) { +/*---------------------------------------------------------------------------- + Total brightness of samples in the line that goes from the left edge + of Row 'startRow' of 'pixels' down to the right at 'dy' rows per column. + + Note that 'dy' can be negative. + + Assume that whatever 'dy' is, the sloping line thus described remains + within 'pixels'. +-----------------------------------------------------------------------------*/ + unsigned long total; + unsigned int x; + float y; + /* The vertical location in the 'pixels' region of the currently + analyzed pixel. 0 is the top of the image. Note that while + 'pixels' is discrete pixels, this is a location in continuous + space. We consider the brightness of the pixel at 1.5 to be + the mean of the brightness of the pixel in row 1 and the pixel + in row 2. + */ + for (x = 0, y = startRow + 0.5, total = 0; + x < hsampleCt; + ++x, y += dy) { + + assert(y >= 0.0); /* Because of entry conditions */ + + total += pixels[(unsigned)y][x]; + } + return total; +} + + + static void scoreAngleRegion(sample ** const pixels, - unsigned int const hsamples, + unsigned int const hsampleCt, unsigned int const startRow, unsigned int const endRow, unsigned int const vstep, @@ -234,7 +270,7 @@ scoreAngleRegion(sample ** const pixels, Instead of a tilt angle, we have 'dy', the slope (downward) of the lines in our assumed tilt. -----------------------------------------------------------------------------*/ - double const tscale = 1.0 / hsamples; + double const tscale = 1.0 / hsampleCt; unsigned int row; double sum; @@ -245,17 +281,10 @@ scoreAngleRegion(sample ** const pixels, /* Number of lines that went into 'total' */ for (row = startRow, sum = 0.0, n = 0; row < endRow; row += vstep) { - float o; - long t; /* total brightness of the samples in the line */ - double dt; /* mean brightness of the samples in the line */ + double const dt = + tscale * totalBrightness(pixels, hsampleCt, row, dy); + /* mean brightness of the samples in the line */ - unsigned int i; - - for (i = 0, t = 0, o = 0.5; - i < hsamples; - t += pixels[(int)(row + o)][i], ++i, o += dy) { - } - dt = tscale * t; sum += dt * dt; n += 1; } @@ -270,20 +299,21 @@ scoreAngle(const struct pam * const pamP, sample ** const pixels, unsigned int const hstep, unsigned int const vstep, - unsigned int const hsamples, + unsigned int const hsampleCt, float const angle, float * const scoreP) { /*---------------------------------------------------------------------------- - Calculate the score for assuming the image described by *pamP and - 'pixels' is tilted down by angle 'angle' (in degrees) from what it - should be. I.e. the angle from the top edge of the paper to the - lines of text in the image is 'angle'. + Calculate the score for assuming the image described by *pamP and 'pixels' + is tilted down by angle 'angle' (in degrees) from what it should be. + I.e. the angle from the top edge of the paper to the lines of text in the + image is 'angle'. Positive means the text slopes down; negative means it + slopes up. The score is a measure of how bright the lines of the image are if we assume this angle. If the angle is right, there should be lots of lines that are all white, because they are between lines of text. If the angle is wrong, many lines will cross over lines of text and - thus be less that all white. A higher score indicates 'angle' is more + thus be less than all white. A higher score indicates 'angle' is more likely to be the angle at which the image is tilted. If 'angle' is so great that not a single line goes all the way across the @@ -291,7 +321,7 @@ scoreAngle(const struct pam * const pamP, every other case, it is nonnegative. 'pixels' is NOT all the pixels in the image; it is just a sampling. - In each row, it contains only 'hsamples' pixels, sampled from the + In each row, it contains only 'hsampleCt' pixels, sampled from the image at intervals of 'hstep' pixels. E.g if the image is 1000 pixels wide, pixels might be only 10 pixels wide, containing columns 0, 100, 200, etc. of the image. @@ -300,10 +330,10 @@ scoreAngle(const struct pam * const pamP, -----------------------------------------------------------------------------*/ float const radians = (float)angle/360 * 2 * M_PI; float const dy = hstep * tan(radians); - /* How much a line sinks due to the tilt when we move one sample + /* How much a line sinks because of the tilt when we move one sample ('hstep' columns of the image) to the right. */ - if (fabs(dy * hsamples) > pamP->height) { + if (fabs(dy * hsampleCt) > pamP->height) { /* This is so tilted that not a single line of the image fits entirely on the page, so we can't do the measurement. */ @@ -316,17 +346,17 @@ scoreAngle(const struct pam * const pamP, off the page and we can't follow them. */ startRow = 0; - endRow = pamP->height - dy * hsamples; + endRow = (unsigned int)(pamP->height - dy * hsampleCt + 0.5); } else { /* Lines of image rise as you go right, so the topmost lines go off the page and we can't follow them. */ - startRow = 0 - dy * hsamples; + startRow = (unsigned int)(0 - dy * hsampleCt + 0.5); endRow = pamP->height; } assert(endRow > startRow); /* because of 'if (fabs(dy ...' */ - scoreAngleRegion(pixels, hsamples, startRow, endRow, vstep, dy, + scoreAngleRegion(pixels, hsampleCt, startRow, endRow, vstep, dy, scoreP); } } @@ -339,7 +369,7 @@ getBestAngleLocal( sample ** const pixels, unsigned int const hstep, unsigned int const vstep, - unsigned int const hsamples, + unsigned int const hsampleCt, float const minangle, float const maxangle, float const incr, @@ -347,7 +377,10 @@ getBestAngleLocal( float * const bestAngleP, float * const qualityP) { /*---------------------------------------------------------------------------- - find angle of highest score within a range + Find the angle that gives the highest score within the range + [minangle, maxangle]. Those are in degrees, with positive meaning + the subject is rotated clockwise on the page (so a horizontal line in + the subject would slope down across the page). -----------------------------------------------------------------------------*/ int const nsamples = ((maxangle - minangle) / incr + 1.5); @@ -369,7 +402,7 @@ getBestAngleLocal( total = 0; for (i = 0; i < nsamples; i++) { angle = minangle + i * incr; - scoreAngle(pamP, pixels, hstep, vstep, hsamples, angle, &score); + scoreAngle(pamP, pixels, hstep, vstep, hsampleCt, angle, &score); results[i] = score; if (score > bestscore || (score == bestscore && fabs(angle) < fabs(bestangle))) { @@ -402,26 +435,36 @@ getBestAngleLocal( static void -readRelevantPixels(const char * const inputFilename, - unsigned int const hstepReq, - unsigned int const vstepReq, - unsigned int * const hstepP, - unsigned int * const vstepP, - sample *** const pixelsP, - struct pam * const pamP, - unsigned int * const hsamplesP) { +readSampledPixels(const char * const inputFilename, + unsigned int const hstepReq, + unsigned int const vstepReq, + unsigned int * const hstepP, + unsigned int * const vstepP, + sample *** const pixelsP, + struct pam * const pamP, + unsigned int * const hsampleCtP) { /*---------------------------------------------------------------------------- - load the image, saving only the pixels we might actually inspect + Read the image. + + Sample the image and return the selected pixels as *pixelsP, which is + an array the same height as the image, but with only certain columns + sampled. Return as *hsampleCtP the number of columns sampled (i.e. the + width of the *pixelsP array). + + Return the horizontal step size used in the sampling as *hstepP. + Return the appropriate vertical step size as *vstepP. -----------------------------------------------------------------------------*/ FILE * ifP; unsigned int hstep; unsigned int vstep; ifP = pm_openr(inputFilename); + pnm_readpaminit(ifP, pamP, PAM_STRUCT_SIZE(tuple_type)); + computeSteps(pamP, hstepReq, vstepReq, &hstep, &vstep); - load(pamP, hstep, pixelsP, hsamplesP); + load(pamP, hstep, pixelsP, hsampleCtP); *hstepP = hstep; *vstepP = vstep; @@ -436,7 +479,7 @@ getAngle(const struct pam * const pamP, sample ** const pixels, unsigned int const hstep, unsigned int const vstep, - unsigned int const hsamples, + unsigned int const hsampleCt, float const maxangle, float const astep, float const qmin, @@ -448,7 +491,7 @@ getAngle(const struct pam * const pamP, float da; float lastq; /* quality (s/n ratio) of last measurement */ - getBestAngleLocal(pamP, pixels, hstep, vstep, hsamples, + getBestAngleLocal(pamP, pixels, hstep, vstep, hsampleCt, -maxangle, maxangle, astep, verbose, &a, &lastq); @@ -461,14 +504,14 @@ getAngle(const struct pam * const pamP, /* make a finer search in the neighborhood */ da = astep / 10; - getBestAngleLocal(pamP, pixels, hstep, vstep, hsamples, + getBestAngleLocal(pamP, pixels, hstep, vstep, hsampleCt, a - 9 * da, a + 9 * da, da, verbose, &a, &lastq); /* iterate once more unless we don't need that much accuracy */ if (!fast) { da /= 10; - getBestAngleLocal(pamP, pixels, hstep, vstep, hsamples, + getBestAngleLocal(pamP, pixels, hstep, vstep, hsampleCt, a - 9 * da, a + 9 * da, da, verbose, &a, &lastq); } @@ -478,26 +521,26 @@ getAngle(const struct pam * const pamP, int -main(int argc, char *argv[]) { +main(int argc, const char ** argv) { struct cmdlineInfo cmdline; struct pam pam; sample ** pixels; /* pixel data */ - unsigned int hsamples; /* horizontal samples used */ + unsigned int hsampleCt; /* number of horizontal samples used */ unsigned int hstep; /* horizontal step size */ unsigned int vstep; /* vertical step size */ float angle; - pgm_init(&argc, argv); /* initialize netpbm system */ + pm_proginit(&argc, argv); parseCommandLine(argc, argv, &cmdline); - readRelevantPixels(cmdline.inputFilename, cmdline.hstep, cmdline.vstep, - &hstep, &vstep, &pixels, &pam, &hsamples); + readSampledPixels(cmdline.inputFilename, cmdline.hstep, cmdline.vstep, + &hstep, &vstep, &pixels, &pam, &hsampleCt); - replacePixelValuesWithScaledDiffs(&pam, pixels, hsamples, cmdline.dstep); + replacePixelValuesWithScaledDiffs(&pam, pixels, hsampleCt, cmdline.dstep); - getAngle(&pam, pixels, hstep, vstep, hsamples, + getAngle(&pam, pixels, hstep, vstep, hsampleCt, cmdline.maxangle, cmdline.astep, cmdline.qmin, cmdline.fast, cmdline.verbose, &angle); |