1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * pbmtools.c -- Routines for dealing with portable bitmap files (PBM)
|
---|
4 | * Copyright (C) 1994 Stuart Inglis
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | * $Id: pbmtools.c 23717 2011-02-16 22:00:10Z sjm84 $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | #include "sysfuncs.h"
|
---|
25 |
|
---|
26 | #include "marklist.h"
|
---|
27 | #include "pbmtools.h"
|
---|
28 | #include "extractor.h"
|
---|
29 | #include "utils.h"
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 | void
|
---|
34 | pbm_putpixel_range (Pixel ** bitmap, int c, int r, int val, int cols, int rows)
|
---|
35 | {
|
---|
36 | if (((c) >= 0) && ((c) < cols) && ((r) >= 0) && ((r) < rows))
|
---|
37 | pbm_putpixel (bitmap, c, r, val);
|
---|
38 | else
|
---|
39 | fprintf (stderr, "putpitxel (%d %d) outside (%d %d)\n", c, r, cols, rows);
|
---|
40 | }
|
---|
41 |
|
---|
42 | int
|
---|
43 | pbm_getpixel_range (Pixel ** bitmap, int c, int r, int cols, int rows)
|
---|
44 | {
|
---|
45 | if (((c) >= 0) && ((c) < cols) && ((r) >= 0) && ((r) < rows))
|
---|
46 | return pbm_getpixel (bitmap, c, r);
|
---|
47 | else
|
---|
48 | {
|
---|
49 | fprintf (stderr, "getpixel (%d %d) outside (%d %d)\n", c, r, cols, rows);
|
---|
50 | return 0;
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 | /*
|
---|
59 | Returns 1 if *fp points to a PBM (magic is P1 or P4) file.
|
---|
60 | leaves the fp in it's original position.
|
---|
61 | usage: if(pbm_isapbmfile(fp)) process(fp);
|
---|
62 | */
|
---|
63 | int
|
---|
64 | pbm_isapbmfile (FILE * fp)
|
---|
65 | {
|
---|
66 | mg_u_long magic;
|
---|
67 |
|
---|
68 | magic = getmagicno_short (fp);
|
---|
69 | if ((magic == MAGIC_P4) || (magic == MAGIC_P1))
|
---|
70 | return 1;
|
---|
71 | else
|
---|
72 | return 0;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /*
|
---|
77 | Frees the memory used in the bitmap structure.
|
---|
78 | usage: pbm_freearray(&bitmap);
|
---|
79 | Sets bitmap to be NULL on return.
|
---|
80 | */
|
---|
81 | void
|
---|
82 | pbm_freearray (Pixel *** bitmap, int rows)
|
---|
83 | {
|
---|
84 | int r;
|
---|
85 | if (rows == 0)
|
---|
86 | rows++;
|
---|
87 | for (r = 0; r < rows; r++)
|
---|
88 | free ((*bitmap)[r]);
|
---|
89 | free (*bitmap);
|
---|
90 | (*bitmap) = NULL;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /*
|
---|
95 | This function allocates the bitmap.
|
---|
96 | usage: bitmap=pbm_allocarray(cols,rows);
|
---|
97 | */
|
---|
98 | Pixel **
|
---|
99 | pbm_allocarray (int cols, int rows)
|
---|
100 | {
|
---|
101 | Pixel **p;
|
---|
102 | int i, newc = (cols + PBITS) / PBITS;
|
---|
103 |
|
---|
104 | if (cols == 0)
|
---|
105 | cols++;
|
---|
106 | if (rows == 0)
|
---|
107 | rows++;
|
---|
108 | p = (Pixel **) malloc (rows * sizeof (Pixel *));
|
---|
109 | if (p == NULL)
|
---|
110 | error_msg ("pbm_allocarray", "out of memory", "");
|
---|
111 | for (i = 0; i < rows; i++)
|
---|
112 | {
|
---|
113 | p[i] = (Pixel *) calloc (newc, sizeof (Pixel));
|
---|
114 | if (p[i] == NULL)
|
---|
115 | error_msg ("pbm_allocarray", "out of memory", "");
|
---|
116 | }
|
---|
117 | return p;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 |
|
---|
122 | Pixel **
|
---|
123 | pbm_copy (Pixel ** bitmap, int cols, int rows)
|
---|
124 | {
|
---|
125 | Pixel **p;
|
---|
126 | int newc = (cols + PBITS) / PBITS;
|
---|
127 | int x, y;
|
---|
128 |
|
---|
129 | p = pbm_allocarray (cols, rows);
|
---|
130 | for (x = 0; x < newc; x++)
|
---|
131 | for (y = 0; y < rows; y++)
|
---|
132 | p[y][x] = bitmap[y][x];
|
---|
133 | return p;
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 |
|
---|
138 |
|
---|
139 | /*
|
---|
140 | Reads in a pbm file and returns a pointer to the allocated bitmap,
|
---|
141 | in the P4 format.
|
---|
142 | Assumes fp was opened and keeps it that way.
|
---|
143 | usage: bitmap=pbm_readfile(fp,&cols,&rows);
|
---|
144 | */
|
---|
145 | Pixel **
|
---|
146 | pbm_readfile (FILE * fp, int *cols, int *rows)
|
---|
147 | {
|
---|
148 | Pixel **p = NULL;
|
---|
149 | char s[100];
|
---|
150 | unsigned char *buf;
|
---|
151 | int r, c, ii = 0;
|
---|
152 | int p1 = 0;
|
---|
153 | int arg;
|
---|
154 |
|
---|
155 |
|
---|
156 | do
|
---|
157 | {
|
---|
158 | readline (s, fp);
|
---|
159 | }
|
---|
160 | while (s[0] == '#');
|
---|
161 |
|
---|
162 | if (strcmp (s, "P1") == 0)
|
---|
163 | p1 = 1;
|
---|
164 | else if (strcmp (s, "P4") == 0)
|
---|
165 | p1 = 0;
|
---|
166 | else
|
---|
167 | error_msg ("pbm_readfile", "only pbm files are supported", "");
|
---|
168 |
|
---|
169 | do
|
---|
170 | {
|
---|
171 | readline (s, fp);
|
---|
172 | }
|
---|
173 | while (s[0] == '#');
|
---|
174 |
|
---|
175 | arg = sscanf (s, "%d %d", cols, rows);
|
---|
176 | if (arg == 1)
|
---|
177 | {
|
---|
178 | readline (s, fp);
|
---|
179 | arg = sscanf (s, "%d", rows);
|
---|
180 | if (arg != 1)
|
---|
181 | error_msg ("pbm_readfile", "bizzare pbm format", "");
|
---|
182 | }
|
---|
183 | p = pbm_allocarray (*cols, *rows);
|
---|
184 | buf = (unsigned char *) malloc ((*cols + 2) * sizeof (unsigned char));
|
---|
185 | if (!buf)
|
---|
186 | error_msg ("pbm_readfile", "out of memory", "");
|
---|
187 |
|
---|
188 | if (p1)
|
---|
189 | {
|
---|
190 | for (r = 0; r < *rows; r++)
|
---|
191 | for (c = 0; c < *cols; c++)
|
---|
192 | {
|
---|
193 | fscanf (fp, "%d", &ii);
|
---|
194 | pbm_putpixel (p, c, r, ii);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | else
|
---|
198 | {
|
---|
199 | for (r = 0; r < *rows; r++)
|
---|
200 | {
|
---|
201 | c = 0;
|
---|
202 | fread (buf, 1, (*cols + 7) / 8, fp);
|
---|
203 | for (c = 0; c < *cols; c++)
|
---|
204 | {
|
---|
205 | if (!(c % 8))
|
---|
206 | ii = buf[c / 8];
|
---|
207 | if (ii < 0)
|
---|
208 | error_msg ("pbm_readfile", "file reading error", "");
|
---|
209 | pbm_putpixel (p, c, r, (ii & 128) ? 1 : 0);
|
---|
210 | ii <<= 1;
|
---|
211 | }
|
---|
212 | }
|
---|
213 | } /* p4 type */
|
---|
214 | free (buf);
|
---|
215 | return p;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | /*
|
---|
220 | Writes a pbm file given a file pointer and bitmap. Assumes that
|
---|
221 | fp is already open, and keeps in that way.
|
---|
222 | usage: pbm_writefile(fp,bitmap,cols,rows);
|
---|
223 | */
|
---|
224 | void
|
---|
225 | pbm_writefile (FILE * fp, Pixel ** bitmap, int cols, int rows)
|
---|
226 | {
|
---|
227 | int r, c, ii = 0, j;
|
---|
228 | unsigned char *buf;
|
---|
229 |
|
---|
230 | buf = (unsigned char *) malloc (((cols + 14) / 8) * sizeof (unsigned char));
|
---|
231 |
|
---|
232 | fprintf (fp, "%s\n", "P4");
|
---|
233 | fprintf (fp, "%d %d\n", cols, rows);
|
---|
234 |
|
---|
235 | for (r = 0; r < rows; r++)
|
---|
236 | {
|
---|
237 | for (c = 0; c < cols; c += 8)
|
---|
238 | {
|
---|
239 | ii = 0;
|
---|
240 | for (j = 0; j < 8; j++)
|
---|
241 | if (c + j < cols)
|
---|
242 | {
|
---|
243 | if (pbm_getpixel (bitmap, c + j, r))
|
---|
244 | ii |= (128 >> j);
|
---|
245 | }
|
---|
246 | buf[c / 8] = ii;
|
---|
247 | }
|
---|
248 | fwrite (buf, 1, (cols + 7) / 8, fp);
|
---|
249 | }
|
---|
250 | free (buf);
|
---|
251 | /* putc('\n',fp); if this line is included programs don't say things like 'short image' &c, but then again,
|
---|
252 | in most cases a 'cmp' between the original will fail because of the last byte...sigh, "standards". */
|
---|
253 | }
|
---|
254 |
|
---|
255 | #define BUF_SIZE 131072
|
---|
256 | char buffer[BUF_SIZE];
|
---|
257 |
|
---|
258 | void
|
---|
259 | pbm_writenamedfile (char fn[], Pixel ** bitmap, int cols, int rows)
|
---|
260 | {
|
---|
261 | FILE *tempfp;
|
---|
262 |
|
---|
263 | tempfp = fopen (fn, "wb");
|
---|
264 | if (tempfp == NULL)
|
---|
265 | error_msg ("pbm_writenamedfile", "can't open file:", fn);
|
---|
266 | setbuffer (tempfp, buffer, BUF_SIZE);
|
---|
267 | pbm_writefile (tempfp, bitmap, cols, rows);
|
---|
268 | fclose (tempfp);
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 |
|
---|
273 | Pixel **
|
---|
274 | pbm_readnamedfile (char fn[], int *cols, int *rows)
|
---|
275 | {
|
---|
276 | FILE *tempfp;
|
---|
277 | Pixel **temp;
|
---|
278 |
|
---|
279 | tempfp = fopen (fn, "rb");
|
---|
280 | if (tempfp == NULL)
|
---|
281 | return NULL;
|
---|
282 | setbuffer (tempfp, buffer, BUF_SIZE);
|
---|
283 |
|
---|
284 | temp = pbm_readfile (tempfp, cols, rows);
|
---|
285 |
|
---|
286 | fclose (tempfp);
|
---|
287 | return temp;
|
---|
288 | }
|
---|