Debian Codegraph @

converter/other/ipdb.c

raw
1 /*
2 *
3 * Copyright (C) 1997 Eric A. Howe
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., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * Authors: Eric A. Howe ([email protected])
20 * Bryan Henderson, 2010
21 */
22 #define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
23 #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
24 #define _BSD_SOURCE /* Ensure strdup() is in <string.h> */
25 #include <assert.h>
26 #include <string.h>
27
28 #include "mallocvar.h"
29 #include "nstring.h"
30
31 #include "ipdb.h"
32
33
34
35 static unsigned int
36 imgPpb(IMAGE * const imgP) {
37 /*----------------------------------------------------------------------------
38 Pixels per byte
39 -----------------------------------------------------------------------------*/
40 return
41 imgP->type == IMG_GRAY ? 4 :
42 imgP->type == IMG_GRAY16 ? 2 :
43 8;
44 }
45
46
47
48 unsigned int
49 ipdb_imgPpb(IMAGE * const imgP) {
50 /*----------------------------------------------------------------------------
51 Pixels per byte
52 -----------------------------------------------------------------------------*/
53 return imgPpb(imgP);
54 }
55
56
57
58 size_t
59 ipdb_imgSize(IMAGE * const imgP) {
60 /*----------------------------------------------------------------------------
61 Size (in bytes) of an image's data.
62 -----------------------------------------------------------------------------*/
63 return (size_t)(imgP->width / imgPpb(imgP) * imgP->height);
64 }
65
66
67
68 /*
69 * Return the start of row `r'.
70 */
71 uint8_t *
72 ipdb_imgRow(IMAGE * const imgP,
73 unsigned int const row) {
74
75 return &imgP->data[(row) * imgP->width / imgPpb(imgP)];
76 }
77
78
79
80 static const char * const errorDesc[] = {
81 /* E_BADCOLORS */
82 "Invalid palette, only {0x00, 0x55, 0xAA, 0xFF} allowed.",
83
84 /* E_NOTIMAGE */
85 "Not an image file.",
86
87 /* E_IMAGETHERE */
88 "Image record already present, logic error.",
89
90 /* E_IMAGENOTTHERE */
91 "Image record required before text record, logic error.",
92
93 /* E_TEXTTHERE */
94 "Text record already present, logic error.",
95
96 /* E_NOTRECHDR */
97 "Invalid record header encountered.",
98
99 /* E_UNKNOWNRECHDR */
100 "Unknown record header.",
101
102 /* E_TOOBIGG */
103 "Image too big, maximum size approx. 640*400 gray pixels.",
104
105 /* E_TOOBIGM */
106 "Image too big, maximum size approx. 640*800 monochrome pixels.",
107 };
108
109
110
111 const char *
112 ipdb_err(int const e) {
113
114 if (e < 0)
115 return e >= E_LAST ? errorDesc[-e - 1] : "unknown error";
116 else
117 return strerror(e);
118 }
119
120
121
122 static void
123 rechdr_free(RECHDR * const recP) {
124
125 if (recP) {
126 free(recP->extra);
127 free(recP);
128 }
129 }
130
131
132
133 void
134 ipdb_imageFree(IMAGE * const imgP) {
135
136 if (imgP) {
137 rechdr_free(imgP->r);
138 free(imgP->data);
139 free(imgP);
140 }
141 }
142
143
144
145 void
146 ipdb_textFree(TEXT * const textP) {
147
148 if (textP) {
149 rechdr_free(textP->r);
150 if (textP->data)
151 free(textP->data);
152 free(textP);
153 }
154 }
155
156
157
158 void
159 ipdb_pdbheadFree(PDBHEAD * const headP) {
160
161 free(headP);
162 }
163
164
165
166 void
167 ipdb_free(IPDB * const pdbP) {
168
169 if (pdbP->i)
170 ipdb_imageFree(pdbP->i);
171
172 if (pdbP->t)
173 ipdb_textFree(pdbP->t);
174
175 if (pdbP->p)
176 ipdb_pdbheadFree(pdbP->p);
177
178 free(pdbP);
179 }
180
181
182
183 PDBHEAD *
184 ipdb_pdbheadAlloc() {
185
186 PDBHEAD * pdbHeadP;
187
188 MALLOCVAR(pdbHeadP);
189
190 if (pdbHeadP) {
191 MEMSZERO(pdbHeadP);
192 }
193 return pdbHeadP;
194 }
195
196
197
198 static RECHDR *
199 rechdrCreate(int const type,
200 uint32_t const offset) {
201
202 /*
203 * We never produce the `extra' bytes (we only read them from a file)
204 * so there is no point allocating them here.
205 */
206
207 RECHDR * recHdrP;
208
209 MALLOCVAR(recHdrP);
210
211 if (recHdrP) {
212 MEMSSET(recHdrP, 0);
213
214 recHdrP->offset = offset;
215 recHdrP->rec_type = (uint8_t)(0xff & type);
216 MEMSCPY(&recHdrP->unknown, IPDB_MYST);
217 }
218 return recHdrP;
219 }
220
221
222
223 /*
224 * The offset will be patched up as needed elsewhere.
225 */
226 #define IMGOFFSET (PDBHEAD_SIZE + 8)
227
228
229
230 IMAGE *
231 ipdb_imageCreate(const char * const name,
232 int const type,
233 int const w,
234 int const h) {
235
236 bool failed;
237 IMAGE * imgP;
238
239 failed = false;
240
241 MALLOCVAR(imgP);
242
243 if (imgP) {
244 MEMSZERO(imgP);
245
246 STRSCPY(imgP->name, name == NULL ? "unnamed" : name);
247 imgP->type = type;
248 imgP->x_anchor = 0xffff;
249 imgP->y_anchor = 0xffff;
250 imgP->width = w;
251 imgP->height = h;
252
253 imgP->r = rechdrCreate(IMG_REC, IMGOFFSET);
254
255 if (imgP->r) {
256 if (w != 0 && h != 0) {
257 MALLOCARRAY(imgP->data, w * h);
258
259 if (imgP->data != NULL) {
260 memset(imgP->data, 0, sizeof(*(imgP->data)) * w * h);
261 } else
262 failed = true;
263 }
264 if (failed)
265 rechdr_free(imgP->r);
266 } else
267 failed = true;
268
269 if (failed)
270 free(imgP);
271 } else
272 failed = true;
273
274 return failed ? NULL : imgP;
275 }
276
277
278
279 TEXT *
280 ipdb_textAlloc(void) {
281
282 TEXT * textP;
283 bool failed;
284
285 failed = false;
286 /*
287 * The offset will be patched up later on when we know what it
288 * should be.
289 */
290
291 MALLOCVAR(textP);
292
293 if (textP) {
294 MEMSZERO(textP);
295
296 textP->r = rechdrCreate(TEXT_REC, 0);
297
298 if (textP->r == NULL)
299 failed = true;
300
301 if (failed)
302 free(textP);
303 } else
304 failed = true;
305
306 return failed ? NULL : textP;
307 }
308
309
310
311 IPDB *
312 ipdb_alloc(void) {
313
314 IPDB * pdbP;
315 bool failed;
316
317 failed = false;
318
319 MALLOCVAR(pdbP);
320
321 if (pdbP) {
322 MEMSZERO(pdbP);
323
324 pdbP->p = ipdb_pdbheadAlloc();
325
326 if (!pdbP->p)
327 failed = true;
328
329 if (failed)
330 ipdb_free(pdbP);
331 } else
332 failed = true;
333
334 return failed ? NULL : pdbP;
335 }
336
337
338
339 const char *
340 ipdb_typeName(uint8_t const type) {
341
342 switch (type) {
343 case IMG_GRAY16: return "16 Bit Grayscale"; break;
344 case IMG_GRAY: return "Grayscale"; break;
345 case IMG_MONO: return "Monochrome"; break;
346 default: return "???";
347 }
348 }
349
350
351