about summary refs log tree commit diff
path: root/converter/pbm/pbmto4425.c
blob: 1d97ac6a83dcfc2fa9f238b344c24e187843debd (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
#include <string.h>

#include "nstring.h"
#include "pbm.h"

static char bit_table[2][3] = {
{1, 4, 0x10},
{2, 8, 0x40}
};

static int vmap_width;
static int vmap_height;

static int xres;
static int yres;

static char *vmap;


static void
init_map()
{
    int x, y;


    for(x = 0; x < vmap_width; ++x)
    {
        for(y = 0; y < vmap_height; ++y)
        {
            vmap[y*(vmap_width) + x] = 0x20;
        }
    }
}



static void
set_vmap(x, y)
    int x, y;
{
    int ix, iy;

    ix = x/2;
    iy = y/3;

    vmap[iy*(vmap_width) + ix] |= bit_table[x % 2][y % 3];
}



static void
fill_map(pbmfp)
    FILE *pbmfp;
{
    bit **pbm_image;
    int cols;
    int rows;
    int x;
    int y;

    pbm_image = pbm_readpbm(pbmfp, &cols, &rows);
    for(y = 0; y < rows && y < yres; ++y)
    {
        for(x = 0; x < cols && x < xres; ++x)
        {
            if(pbm_image[y][x] == PBM_WHITE)
            {
                set_vmap(x, y);
            }
        }
    }
}


static void
print_map()
{
    int x, y;
    int last_byte;

#ifdef BUFFERED
    char *iobuf;
    iobuf = (char *)malloc(BUFSIZ);
    if(iobuf == NULL)
    {
        pm_message( "Can't allocate space for I/O buffer.  "
                    "Using unbuffered I/O...\n" );
        setbuf(stdout, NULL);
    }
    else
    {
        setbuf(stdout, iobuf);
    }
#endif

    fputs("\033[H\033[J", stdout);	/* clear screen */
    fputs("\033[?3h", stdout);	/* 132 column mode */
    fputs("\033)}\016", stdout);	/* mosaic mode */

    for(y = 0; y < vmap_height; ++y)
    {
        for(last_byte = vmap_width - 1;
            last_byte >= 0
                && vmap[y * vmap_width + last_byte] == 0x20;
            --last_byte)
            ;

        for(x = 0; x <= last_byte; ++x)
        {
            fputc(vmap[y*(vmap_width) + x], stdout);
        }
        fputc('\n', stdout);
    }

    fputs("\033(B\017", stdout);
}



int
main(int argc, char * argv[]) {
    int argn;
    const char *pbmfile;
    FILE *pbmfp;
    const char *usage="[pbmfile]";

    pbm_init( &argc, argv );
    for(argn = 1;
        argn < argc && argv[argn][0] == '-' && strlen(argv[argn]) > 1;
        ++argn)
    {
        pm_usage(usage);
    }

    if(argn >= argc)
    {
        pbmfile = "-";
    }
    else if(argc - argn != 1)
    {
        pm_usage(usage);
    }
    else
    {
        pbmfile = argv[argn];
    }

    if(streq(pbmfile, "-"))
    {
        pbmfp = stdin;
    }
    else
    {
        pbmfp = pm_openr( argv[argn] );
    }

    vmap_width = 132;
    vmap_height = 23;

    xres = vmap_width * 2;
    yres = vmap_height * 3;

    vmap = malloc(vmap_width * vmap_height * sizeof(char));
    if(vmap == NULL)
	{
        pm_error( "Cannot allocate memory" );
	}

    init_map();
    fill_map(pbmfp);
    print_map();
    /* If the program failed, it previously aborted with nonzero completion
       code, via various function calls.
    */
    return 0; 
}