about summary refs log tree commit diff
path: root/converter/pbm/cistopbm.c
blob: b62f6045c9cb0f58aac2f41dfe322bda2fb9e819 (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
/*
 *  cistopbm: Convert images in the CompuServe RLE format to PBM
 *  Copyright (C) 2009  John Elliott
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pbm.h"


static void syntax(const char *prog)
{
        pm_usage(" { options } { input }\n\n"
                 "Input file should be in CompuServe RLE format.\n"
                 "Output file will be in PBM format.\n"
                 "Options:\n"
                 "-i, --inverse: Reverse black and white.\n"
                 "--:            End of options\n\n"
"cistopbm v1.01, Copyright 2009 John Elliott <jce@seasip.demon.co.uk>\n"
"This program is redistributable under the terms of the GNU General Public\n"
"License, version 2 or later.\n"
                 );
}



int main(int argc, const char **argv)
{
    FILE *ifP;
    int c[3];
    int inoptions = 1;
    int n, x, y;
    int bw = PBM_BLACK;     /* Default colouring is white on black */
    const char *inpname = NULL;
    int height, width;
    bit **bits;

    pm_proginit(&argc, argv);

    for (n = 1; n < argc; n++)
    {
        if (!strcmp(argv[n], "--"))
        {
            inoptions = 0;
            continue;
        }
        if (inoptions)
        {
            if (pm_keymatch(argv[n], "-h", 2) ||
                pm_keymatch(argv[n], "-H", 2) ||
                pm_keymatch(argv[n], "--help", 6))
            {
                syntax(argv[0]);
                return EXIT_SUCCESS;
            }
            if (pm_keymatch(argv[n], "-i", 2) ||
                pm_keymatch(argv[n], "-I", 2) ||
                pm_keymatch(argv[n], "--inverse", 9))
            {
                bw ^= (PBM_WHITE ^ PBM_BLACK);
                continue;
            }
            if (argv[n][0] == '-' && argv[n][1] != 0)
            {
                pm_message("Unknown option: %s", argv[n]);
                syntax(argv[0]);
                return EXIT_FAILURE;
            }
        }

        if (inpname == NULL) inpname = argv[n];
        else { syntax(argv[0]); return EXIT_FAILURE; }
    }
    if (inpname == NULL) inpname = "-";
    ifP  = pm_openr(inpname);

    /* There may be junk before the magic number. If so, skip it. */
    x = 0;
    c[0] = c[1] = c[2] = EOF;

    /* Read until the array c[] holds the magic number. */
    do
    {
        c[0] = c[1];
        c[1] = c[2];
        c[2] = fgetc(ifP);

        /* If character read was EOF, end of file was reached and magic number
         * not found.
         */
        if (c[2] == EOF)
        {
            pm_error("Input file is not in CompuServe RLE format");
        }
        ++x;
    } while (c[0] != 0x1B || c[1] != 0x47);

    /* x = number of bytes read. Should be 3 if signature is at the start */
    if (x > 3)
    {
        pm_message("Warning: %d bytes of junk skipped before image",
                   x - 3);
    }
    /* Parse the resolution */
    switch(c[2])
    {
    case 0x48:      height = 192; width = 256; break;
    case 0x4D:      height =  96; width = 128; break;
    default:        pm_error("Unknown resolution 0x%02x", c[2]);
        break;
    }
    /* Convert the data */
    bits = pbm_allocarray(width, height);
    x = y = 0;
    do
    {
        c[0] = fgetc(ifP);

        /* Stop if we hit EOF or Escape */
        if (c[0] == EOF)  break;        /* EOF */
        if (c[0] == 0x1B) break;        /* End of graphics */
        /* Other non-printing characters are ignored; some files contain a
         * BEL
         */
        if (c[0] < 0x20)  continue;

        /* Each character gives the number of pixels to draw in the appropriate
         * colour.
         */
        for (n = 0x20; n < c[0]; n++)
        {
            if (x < width && y < height) bits[y][x] = bw;
            x++;
            /* Wrap at end of line */
            if (x >= width)
            {
                x = 0;
                y++;
            }
        }
        /* And toggle colours */
        bw ^= (PBM_WHITE ^ PBM_BLACK);
    }
    while (1);

    /* See if the end-graphics signature (ESC G N) is present. */
    c[1] = EOF;
    if (c[0] == 0x1B)
    {
        c[1] = fgetc(ifP);
        c[2] = fgetc(ifP);
    }
    if (c[0] != 0x1B || c[1] != 0x47 || c[2] != 0x4E)
    {
        pm_message("Warning: End-graphics signature not found");
    }
    /* See if we decoded the right number of pixels */
    if (x != 0 || y != height)
    {
        pm_message("Warning: %d pixels found, should be %d",
                   y * width + x, width * height);
    }
    pbm_writepbm(stdout, bits, width, height, 0);
    pm_close(ifP);
    return 0;
}