about summary refs log tree commit diff
path: root/analyzer/pamtilt.c
blob: 031c42066d182c2077926d427041dd963cfe5e93 (plain) (blame)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*=============================================================================
                             pamtilt
===============================================================================
  Print the tilt angle of an image (typically black and white text).

  Based on pgmskew by Gregg Townsend, August 2005.

  Adapted to Netpbm by Bryan Henderson, August 2005.

  All work has been contributed to the public domain by its authors.
=============================================================================*/

#define _XOPEN_SOURCE 500  /* get M_PI in math.h */

#include <assert.h>
#include <math.h>

#include "pm_c_util.h"
#include "pam.h"
#include "mallocvar.h"
#include "shhopt.h"

/* program constants */
#define DIFFMAX 255                     /* maximum scaling for differences */
#define BARLENGTH 60                    /* length of bars printed with -v */

struct cmdlineInfo {
    /* command parameters */
    const char * inputFilename;
    float maxangle;     /* maximum angle attempted */
    float astep;        /* initial angle increment */
    float qmin;         /* minimum quality of solution */
    unsigned int hstep; /* horizontal step size */
    unsigned int vstep; /* vertical step size */
    unsigned int dstep; /* difference distance */
    unsigned int fast;    /* skip third iteration */
    unsigned int verbose; /* generate commentary */
};

static void
abandon(void) {

    printf("00.00\n");
    exit(0);
}



static void
parseCommandLine(int argc, const char ** const argv,
                 struct cmdlineInfo * const cmdlineP) {

    static optEntry option_def[50];
    static optStruct3 opt;
    unsigned int option_def_index;

    /* set defaults */
    cmdlineP->hstep = 11;        /* read only every 11th column */
    cmdlineP->vstep = 5;         /* calc differences every 5th row */
    cmdlineP->dstep = 2;         /* check for differences two rows down */
    cmdlineP->maxangle = 10.0;   /* assume skew is less than +/- ten degrees */
    cmdlineP->astep = 1.0;       /* initially check by one-degree increments */
    cmdlineP->qmin = 1.0;        /* don't require S/N better than 1.0 */

    /* initialize option table */
    option_def_index = 0;       /* incremented by OPTENT3 */
    OPTENT3(0, "fast",     OPT_FLAG,  NULL, &cmdlineP->fast,     0);
    OPTENT3(0, "verbose",  OPT_FLAG,  NULL, &cmdlineP->verbose,  0);
    OPTENT3(0, "angle",    OPT_FLOAT, &cmdlineP->maxangle, NULL, 0);
    OPTENT3(0, "quality",  OPT_FLOAT, &cmdlineP->qmin,     NULL, 0);
    OPTENT3(0, "astep",    OPT_FLOAT, &cmdlineP->astep,    NULL, 0);
    OPTENT3(0, "hstep",    OPT_UINT,  &cmdlineP->hstep,    NULL, 0);
    OPTENT3(0, "vstep",    OPT_UINT,  &cmdlineP->vstep,    NULL, 0);
    OPTENT3(0, "dstep",    OPT_UINT,  &cmdlineP->dstep,    NULL, 0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;          /* no short options used */
    opt.allowNegNum = FALSE;            /* don't allow negative values */
    pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);

    if (cmdlineP->hstep < 1)
        pm_error("-hstep must be at least 1 column.");
    if (cmdlineP->vstep < 1)
        pm_error("-vstep must be at least 1 row.");
    if (cmdlineP->dstep < 1)
        pm_error("-dstep must be at least 1 row.");
    if (cmdlineP->maxangle < 1 || cmdlineP->maxangle > 45)
        pm_error("-maxangle must be between 1 and 45 degrees.");

    if (argc-1 < 1)                      /* if input file name given */
        cmdlineP->inputFilename = "-";
    else {
        cmdlineP->inputFilename = argv[1];

        if (argc-1 > 1)
            pm_error("There is at most one argument.  You specified %d",
                     argc-1);
    }
}



static void
computeSteps(const struct pam * const pamP,
             unsigned int       const hstepReq,
             unsigned int       const vstepReq,
             unsigned int *     const hstepP,
             unsigned int *     const vstepP) {
/*----------------------------------------------------------------------------
  Adjust parameters if necessary now that we have the image size
-----------------------------------------------------------------------------*/
    if (pamP->width < 10 || pamP->height < 10)
        abandon();

    if (pamP->width < 10 * hstepReq)
        *hstepP = pamP->width / 10;
    else
        *hstepP = hstepReq;

    if (pamP->height < 10 * vstepReq)
        *vstepP = pamP->height / 10;
    else
        *vstepP = vstepReq;
}



static void
load(const struct pam * const pamP,
     unsigned int       const hstep,
     sample ***         const pixelsP,
     unsigned int *     const hsampleCtP) {
/*----------------------------------------------------------------------------
  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 hsampleCt = 1 + (pamP->width - 1) / hstep;
        /* use only this many cols */

    unsigned int row;
    tuple *  tuplerow;
    sample ** pixels;

    tuplerow = pnm_allocpamrow(pamP);

    MALLOCARRAY(pixels, pamP->height);

    if (pixels == NULL)
        pm_error("Unable to allocate array of %u rows", pamP->height);

    for (row = 0; row < pamP->height; ++row) {
        unsigned int i;
        unsigned int col;

        pnm_readpamrow(pamP, tuplerow);

        MALLOCARRAY(pixels[row], hsampleCt);
        if (pixels[row] == NULL)
            pm_error("Unable to allocate %u-sample array for Row %u",
                     hsampleCt, row);

        /* save every hstep'th column */
        for (i = col = 0; i < hsampleCt; ++i, col += hstep)
            pixels[row][i] = tuplerow[col][0];
    }

    pnm_freepamrow(tuplerow);

    *hsampleCtP = hsampleCt;
    *pixelsP   = pixels;
}



static void
freePixels(sample **    const samples,
           unsigned int const rows) {

    unsigned int row;

    for (row = 0; row < rows; ++row)
        free(samples[row]);

    free(samples);
}



static void
replacePixelValuesWithScaledDiffs(
    const struct pam * const pamP,
    sample **          const pixels,
    unsigned int       const hsampleCt,
    unsigned int       const dstep) {
/*----------------------------------------------------------------------------
  Replace pixel values with scaled differences used for all
  calculations
-----------------------------------------------------------------------------*/
    float const m = DIFFMAX / (float) pamP->maxval;  /* scale multiplier */

    unsigned int row;

    for (row = dstep; row < pamP->height; ++row) {
        unsigned int 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 */
        }
    }
}



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 hsampleCt,
                 unsigned int const startRow,
                 unsigned int const endRow,
                 unsigned int const vstep,
                 float        const dy,
                 float *      const scoreP) {
/*----------------------------------------------------------------------------
   Same as scoreAngle(), but we look only at the region from 'startRow'
   to 'endRow', which we assume is enough inside the image that tilted
   lines starting at the left within that region don't run off the top
   or bottom of the image.

   We assume 'startRow' and 'endRow' are within the image and denote at
   least one row.

   Instead of a tilt angle, we have 'dy', the slope (downward) of the lines
   in our assumed tilt.
-----------------------------------------------------------------------------*/
    double const tscale  = 1.0 / hsampleCt;

    unsigned int row;
    double sum;
        /* Sum of brightness measure of all sampled lines which are
           (assuming tilt) contained within image.
        */
    unsigned int n;
        /* Number of lines that went into 'total' */

    for (row = startRow, sum = 0.0, n = 0; row < endRow; row += vstep) {
        double const dt =
            tscale * totalBrightness(pixels, hsampleCt, row, dy);
            /* mean brightness of the samples in the line */

        sum += dt * dt;
        n += 1;
    }
    assert(n > 0);  /* Because we assume there's at least one row */
    *scoreP = sum / n;
}



static void
scoreAngle(const struct pam * const pamP,
           sample **          const pixels,
           unsigned int       const hstep,
           unsigned int       const vstep,
           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'.  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 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
  page without running off the top or bottom, we call the score -1.  In
  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 '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.

  Return the score as *scoreP.
-----------------------------------------------------------------------------*/
    float  const radians = (float)angle/360 * 2 * M_PI;
    float  const dy      = hstep * tan(radians);
        /* 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 * 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.
        */
        *scoreP = -1.0;
    } else {
        unsigned int startRow, endRow;

        if (dy > 0) {
            /* Lines of image drop as you go right, so the bottommost lines go
               off the page and we can't follow them.
            */
            startRow = 0;
            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 = (unsigned int)(0 - dy * hsampleCt + 0.5);
            endRow = pamP->height;
        }
        assert(endRow > startRow);  /* because of 'if (fabs(dy ...' */

        scoreAngleRegion(pixels, hsampleCt, startRow, endRow, vstep, dy,
                         scoreP);
    }
}



static void
getBestAngleLocal(
    const struct pam * const pamP,
    sample **          const pixels,
    unsigned int       const hstep,
    unsigned int       const vstep,
    unsigned int       const hsampleCt,
    float              const minangle,
    float              const maxangle,
    float              const incr,
    bool               const verbose,
    float *            const bestAngleP,
    float *            const qualityP) {
/*----------------------------------------------------------------------------
  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);

    float score;
    float quality;  /* signal/noise ratio of the best angle */
    float angle;
    float bestangle;
    float bestscore;
    float total;
    float others;
    float * results;
    int i;

    MALLOCARRAY_NOFAIL(results, nsamples);      /* allocate array of results */

    /* try all angles within the given range, stepping by incr */
    bestangle = minangle;
    bestscore = 0;
    total = 0;
    for (i = 0; i < nsamples; i++) {
        angle = minangle + i * incr;
        scoreAngle(pamP, pixels, hstep, vstep, hsampleCt, angle, &score);
        results[i] = score;
        if (score > bestscore ||
            (score == bestscore && fabs(angle) < fabs(bestangle))) {
            bestscore = score;
            bestangle = angle;
        }
        total += score;
    }

    others = (total-bestscore) / (nsamples-1);  /* get mean of other scores */
    quality = bestscore / others;

    if (verbose) {
        fprintf(stderr,
                "\n%2d angles from %6.2f to %5.2f by %4.2f:  "
                "best = %6.2f, S:N = %4.2f\n",
                nsamples, minangle, maxangle, incr, bestangle, quality);
        for (i = 0; i < nsamples; ++i) {
            float const angle = minangle + i * incr;
            int const n = (int) (BARLENGTH * results[i] / bestscore + 0.5);
            fprintf(stderr, "%6.2f: %8.2f %0*d\n", angle, results[i], n, 0);
        }
    }
    free(results);

    *qualityP   = quality;
    *bestAngleP = bestangle;
}



static void
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) {
/*----------------------------------------------------------------------------
  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, hsampleCtP);

    *hstepP = hstep;
    *vstepP = vstep;

    pm_close(ifP);
}



static void
getAngle(const struct pam * const pamP,
         sample **          const pixels,
         unsigned int       const hstep,
         unsigned int       const vstep,
         unsigned int       const hsampleCt,
         float              const maxangle,
         float              const astep,
         float              const qmin,
         bool               const fast,
         bool               const verbose,
         float *            const angleP) {

    float a;
    float da;
    float lastq;        /* quality (s/n ratio) of last measurement */

    getBestAngleLocal(pamP, pixels, hstep, vstep, hsampleCt,
                      -maxangle, maxangle, astep, verbose,
                      &a, &lastq);

    if ((a < -maxangle + astep / 2) || (a > maxangle - astep / 2))
        /* extreme val almost certainly wrong */
        abandon();
    if (lastq < qmin)
        /* insufficient s/n ratio */
        abandon();

    /* make a finer search in the neighborhood */
    da = astep / 10;
    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, hsampleCt,
                          a - 9 * da, a + 9 * da, da, verbose,
                          &a, &lastq);
    }
    *angleP = a;
}



int
main(int argc, const char ** argv) {

    struct cmdlineInfo cmdline;
    struct pam pam;
    sample ** pixels;       /* pixel data */
    unsigned int hsampleCt; /* number of horizontal samples used */
    unsigned int hstep;    /* horizontal step size */
    unsigned int vstep;    /* vertical step size */
    float angle;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    readSampledPixels(cmdline.inputFilename, cmdline.hstep, cmdline.vstep,
                      &hstep, &vstep, &pixels, &pam, &hsampleCt);

    replacePixelValuesWithScaledDiffs(&pam, pixels, hsampleCt, cmdline.dstep);

    getAngle(&pam, pixels, hstep, vstep, hsampleCt,
             cmdline.maxangle, cmdline.astep, cmdline.qmin,
             cmdline.fast, cmdline.verbose, &angle);

    /* report the result on stdout */
    printf("%.2f\n", angle);

    freePixels(pixels, pam.height);

    return 0;
}