about summary refs log tree commit diff
path: root/editor/specialty/pgmbentley.c
blob: aed920748ea41e3b26694ef99e338a4758b3acfa (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
/* pgmbentley.c - read a portable graymap and smear it according to brightness
**
** Copyright (C) 1990 by Wilson Bent (whb@hoh-2.att.com)
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include <stdio.h>

#include "pm_c_util.h"
#include "pgm.h"


static unsigned int const N = 4;


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

    FILE * ifP;
    int rows, cols;
    gray maxval;
    gray ** gin;
    gray ** gout;
    unsigned int row;
    const char * inputFileName;

    pm_proginit(&argc, argv);

    if (argc-1 < 1)
        inputFileName = "-";
    else {
        inputFileName = argv[1];

        if (argc-1 > 1)
            pm_error("There are no options and only one argument.  "
                     "You specified %u", argc-1);
    }
	ifP = pm_openr(inputFileName);

    gin = pgm_readpgm(ifP, &cols, &rows, &maxval);

    pm_close(ifP);

    gout = pgm_allocarray(cols, rows);

    for (row = 0; row < rows; ++row) {
        unsigned int col;
        for (col = 0; col < cols; ++col)
            gout[row][col] = 0;
    }

    for (row = 0; row < rows; ++row) {
        unsigned int col;

        for (col = 0; col < cols; ++col) {
            unsigned int const brow = MIN(rows-1, row + gin[row][col] / N);

            gout[brow][col] = gin[row][col];
	    }
    }
     
    pgm_writepgm(stdout, gout, cols, rows, maxval, 0);

    pm_close(stdout);
    pgm_freearray(gout, rows);

    return 0;
}