Debian Codegraph @

converter/pbm/cistopbm.c

raw
1 /*
2 * cistopbm: Convert images in the CompuServe RLE format to PBM
3 * Copyright (C) 2009 John Elliott
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "pbm.h"
25
26
27 static void syntax(const char *prog)
28 {
29 pm_usage(" { options } { input }\n\n"
30 "Input file should be in CompuServe RLE format.\n"
31 "Output file will be in PBM format.\n"
32 "Options:\n"
33 "-i, --inverse: Reverse black and white.\n"
34 "--: End of options\n\n"
35 "cistopbm v1.01, Copyright 2009 John Elliott <[email protected]>\n"
36 "This program is redistributable under the terms of the GNU General Public\n"
37 "License, version 2 or later.\n"
38 );
39 }
40
41
42
43 int main(int argc, const char **argv)
44 {
45 FILE *ifP;
46 int c[3];
47 int inoptions = 1;
48 int n, x, y;
49 int bw = PBM_BLACK; /* Default colouring is white on black */
50 const char *inpname = NULL;
51 int height, width;
52 bit **bits;
53
54 pm_proginit(&argc, argv);
55
56 for (n = 1; n < argc; n++)
57 {
58 if (!strcmp(argv[n], "--"))
59 {
60 inoptions = 0;
61 continue;
62 }
63 if (inoptions)
64 {
65 if (pm_keymatch(argv[n], "-h", 2) ||
66 pm_keymatch(argv[n], "-H", 2) ||
67 pm_keymatch(argv[n], "--help", 6))
68 {
69 syntax(argv[0]);
70 return EXIT_SUCCESS;
71 }
72 if (pm_keymatch(argv[n], "-i", 2) ||
73 pm_keymatch(argv[n], "-I", 2) ||
74 pm_keymatch(argv[n], "--inverse", 9))
75 {
76 bw ^= (PBM_WHITE ^ PBM_BLACK);
77 continue;
78 }
79 if (argv[n][0] == '-' && argv[n][1] != 0)
80 {
81 pm_message("Unknown option: %s", argv[n]);
82 syntax(argv[0]);
83 return EXIT_FAILURE;
84 }
85 }
86
87 if (inpname == NULL) inpname = argv[n];
88 else { syntax(argv[0]); return EXIT_FAILURE; }
89 }
90 if (inpname == NULL) inpname = "-";
91 ifP = pm_openr(inpname);
92
93 /* There may be junk before the magic number. If so, skip it. */
94 x = 0;
95 c[0] = c[1] = c[2] = EOF;
96
97 /* Read until the array c[] holds the magic number. */
98 do
99 {
100 c[0] = c[1];
101 c[1] = c[2];
102 c[2] = fgetc(ifP);
103
104 /* If character read was EOF, end of file was reached and magic number
105 * not found.
106 */
107 if (c[2] == EOF)
108 {
109 pm_error("Input file is not in CompuServe RLE format");
110 }
111 ++x;
112 } while (c[0] != 0x1B || c[1] != 0x47);
113
114 /* x = number of bytes read. Should be 3 if signature is at the start */
115 if (x > 3)
116 {
117 pm_message("Warning: %d bytes of junk skipped before image",
118 x - 3);
119 }
120 /* Parse the resolution */
121 switch(c[2])
122 {
123 case 0x48: height = 192; width = 256; break;
124 case 0x4D: height = 96; width = 128; break;
125 default: pm_error("Unknown resolution 0x%02x", c[2]);
126 break;
127 }
128 /* Convert the data */
129 bits = pbm_allocarray(width, height);
130 x = y = 0;
131 do
132 {
133 c[0] = fgetc(ifP);
134
135 /* Stop if we hit EOF or Escape */
136 if (c[0] == EOF) break; /* EOF */
137 if (c[0] == 0x1B) break; /* End of graphics */
138 /* Other non-printing characters are ignored; some files contain a
139 * BEL
140 */
141 if (c[0] < 0x20) continue;
142
143 /* Each character gives the number of pixels to draw in the appropriate
144 * colour.
145 */
146 for (n = 0x20; n < c[0]; n++)
147 {
148 if (x < width && y < height) bits[y][x] = bw;
149 x++;
150 /* Wrap at end of line */
151 if (x >= width)
152 {
153 x = 0;
154 y++;
155 }
156 }
157 /* And toggle colours */
158 bw ^= (PBM_WHITE ^ PBM_BLACK);
159 }
160 while (1);
161
162 /* See if the end-graphics signature (ESC G N) is present. */
163 c[1] = EOF;
164 if (c[0] == 0x1B)
165 {
166 c[1] = fgetc(ifP);
167 c[2] = fgetc(ifP);
168 }
169 if (c[0] != 0x1B || c[1] != 0x47 || c[2] != 0x4E)
170 {
171 pm_message("Warning: End-graphics signature not found");
172 }
173 /* See if we decoded the right number of pixels */
174 if (x != 0 || y != height)
175 {
176 pm_message("Warning: %d pixels found, should be %d",
177 y * width + x, width * height);
178 }
179 pbm_writepbm(stdout, bits, width, height, 0);
180 pm_close(ifP);
181 return 0;
182 }
183
184
185