Debian Codegraph @

converter/ppm/ppmtoacad.c

raw symbols SCIP
1 /*
2
3 Convert a portable pixmap to an AutoCAD slide or DXB file
4
5 Author:
6 John Walker
7 Autodesk SA
8 Avenue des Champs-Montants 14b
9 CH-2074 MARIN
10 Switzerland
11 Usenet: [email protected]
12 Fax: 038/33 88 15
13 Voice: 038/33 76 33
14
15 Permission to use, copy, modify, and distribute this software and
16 its documentation for any purpose and without fee is hereby
17 granted, without any conditions or restrictions. This software is
18 provided "as is" without express or implied warranty.
19
20 */
21
22 #include <stdio.h>
23
24 #include "pm_c_util.h"
25 #include "ppm.h"
26
27 #define EOS '\0'
28
29 #define MAXHIST 32767 /* Color histogram maximum size */
30
31 static pixel **pixels; /* Input pixel map */
32 static colorhash_table cht; /* Color hash table */
33 static int curcol = -1; /* Current slide output color */
34 static int polymode = FALSE; /* Output filled polygons ? */
35 static int dxbmode = FALSE; /* Output .dxb format ? */
36 static int bgcol = -1; /* Screen background color */
37 static double aspect = 1.0; /* Pixel aspect ratio correction */
38 static int gamut = 256; /* Output color gamut */
39
40 #include "autocad.h" /* AutoCAD standard color assignments */
41
42 /* prototypes */
43 static void outrun (int const color,
44 int const ysize,
45 int const y,
46 int const xstart,
47 int const xend);
48
49 static void slideout (int const xdots,
50 int const ydots,
51 int const ncolors,
52 unsigned char *red,
53 unsigned char *green,
54 unsigned char *blue);
55
56
57 /* OUTRUN -- Output a run of pixels. */
58
59 static void
60 outrun (int const color,
61 int const ysize,
62 int const y,
63 int const xstart,
64 int const xend) {
65
66 if (color == 0) {
67 return; /* Let screen background handle this */
68 }
69
70 if (curcol != color) {
71 if (dxbmode) {
72 putchar((char)136);
73 (void) pm_writelittleshort(stdout, color);
74 } else {
75 (void) pm_writelittleshort(stdout, (short) 0xFF00 | color);
76 }
77 curcol = color;
78 }
79 if (polymode) {
80 int v, yb = (ysize - y) + 1, yspan = 1;
81
82 /* Since we're emitting filled polygons, let's scan downward
83 in the pixmap and see if we can extend the run on this line
84 vertically as well. If so, emit a polygon that handles
85 both the horizontal and vertical run and clear the pixels
86 in the subsequent lines to the background color. */
87
88 for (v = y + 1; v <= ysize; v++) {
89 int j, mismatch = FALSE;
90
91 for (j = xstart; j <= xend; j++) {
92 if (PPM_GETR(pixels[y][j]) != PPM_GETR(pixels[v][j])) {
93 mismatch = TRUE;
94 break;
95 }
96 }
97 if (mismatch) {
98 break;
99 }
100 for (j = xstart; j <= xend; j++) {
101 PPM_ASSIGN(pixels[v][j], 0, 0, 0);
102 }
103 }
104 yspan = v - y;
105
106 if (dxbmode) {
107 putchar(11); /* Solid */
108 (void) pm_writelittleshort(
109 stdout, (int) (xstart * aspect + 0.4999));
110 (void) pm_writelittleshort(stdout, yb);
111
112 (void) pm_writelittleshort(
113 stdout, (int) ((xend + 1) * aspect + 0.4999));
114 (void) pm_writelittleshort(stdout, yb);
115
116 (void) pm_writelittleshort(
117 stdout, (int) (xstart * aspect + 0.4999));
118 (void) pm_writelittleshort(stdout, yb - yspan);
119
120 (void) pm_writelittleshort(
121 stdout, (int) ((xend + 1) * aspect + 0.4999));
122 (void) pm_writelittleshort(stdout, yb - yspan);
123 } else {
124 (void) pm_writelittleshort(stdout, (short) 0xFD00);
125 /* Solid fill header */
126 (void) pm_writelittleshort(stdout, 4);
127 /* Vertices to follow */
128 (void) pm_writelittleshort(stdout, -2); /* Fill type */
129
130 (void) pm_writelittleshort(stdout, (short)0xFD00);
131 /* Solid fill vertex */
132 (void) pm_writelittleshort(stdout, xstart);
133 (void) pm_writelittleshort(stdout, yb);
134
135 (void) pm_writelittleshort(stdout, (short) 0xFD00);
136 /* Solid fill vertex */
137 (void) pm_writelittleshort(stdout, xend + 1);
138 (void) pm_writelittleshort(stdout, yb);
139
140 (void) pm_writelittleshort(stdout, (short) 0xFD00);
141 /* Solid fill vertex */
142 (void) pm_writelittleshort(stdout, xend + 1);
143 (void) pm_writelittleshort(stdout, yb - yspan);
144
145 (void) pm_writelittleshort(stdout, (short) 0xFD00);
146 /* Solid fill vertex */
147 (void) pm_writelittleshort(stdout, xstart);
148 (void) pm_writelittleshort(stdout, yb - yspan);
149
150 (void) pm_writelittleshort(stdout, (short) 0xFD00);
151 /* Solid fill trailer */
152 (void) pm_writelittleshort(stdout, 4); /* Vertices that precede */
153 (void) pm_writelittleshort(stdout, -2); /* Fill type */
154 }
155 } else {
156 (void) pm_writelittleshort(stdout, xstart); /* Vector: From X */
157 (void) pm_writelittleshort(stdout, ysize - y); /* From Y */
158 (void) pm_writelittleshort(stdout, xend); /* To X */
159 (void) pm_writelittleshort(stdout, ysize - y); /* To Y */
160 }
161 }
162
163
164
165 /* SLIDEOUT -- Write an AutoCAD slide. */
166
167 static void
168 slideout (int const xdots,
169 int const ydots,
170 int const ncolors,
171 unsigned char *red,
172 unsigned char *green,
173 unsigned char *blue) {
174
175 static char sldhead[18] = "AutoCAD Slide\r\n\32";
176 static char dxbhead[20] = "AutoCAD DXB 1.0\r\n\32";
177 unsigned char *acadmap;
178 int i, xsize, ysize;
179
180 /* If the user has specified a non-black screen background color,
181 swap the screen background color into color index zero and
182 move black into the slot previously occupied by the background
183 color. */
184
185 if (bgcol > 0) {
186 acadcol[0][0] = acadcol[bgcol][0];
187 acadcol[0][1] = acadcol[bgcol][1];
188 acadcol[0][2] = acadcol[bgcol][2];
189 acadcol[bgcol][0] = acadcol[bgcol][1] = acadcol[bgcol][2] = 0;
190 }
191
192 acadmap = (unsigned char *) pm_allocrow(ncolors, sizeof(unsigned char));
193 xsize = polymode ? xdots : (xdots - 1);
194 ysize = polymode ? ydots : (ydots - 1);
195 if (dxbmode) {
196 fwrite(dxbhead, 19, 1, stdout); /* DXB file header */
197 putchar((char)135); /* Number mode */
198 (void) pm_writelittleshort(stdout, 0); /* ...short integers */
199 } else {
200 fwrite(sldhead, 17, 1, stdout); /* Slide file header */
201 putchar(86); /* Number format indicator */
202 putchar(2); /* File level number */
203 (void) pm_writelittleshort(stdout, xsize); /* Max X co-ordinate value */
204 (void) pm_writelittleshort(stdout, ysize); /* Max Y co-ordinate value */
205 /* Aspect ratio indicator */
206 (void) pm_writelittlelong(
207 stdout, (long int) ((((double) xsize) / ysize) * aspect * 1E7));
208 (void) pm_writelittleshort(stdout, 2); /* Polygon fill type */
209 (void) pm_writelittleshort(stdout, 0x1234); /* Byte order indicator */
210 }
211
212 /* Now build a mapping from the image's computed color map
213 indices to the closest representation of each color within
214 AutoCAD's color repertoire. */
215
216 for (i = 0; i < ncolors; i++) {
217 int best, j;
218 long int dist = 3 * 256 * 256;
219
220 for (j = 0; j < gamut; j++) {
221 long int const dr = red[i] - acadcol[j][0],
222 dg = green[i] - acadcol[j][1],
223 db = blue[i] - acadcol[j][2];
224 long int const tdist = dr * dr + dg * dg + db * db;
225
226 if (tdist < dist) {
227 dist = tdist;
228 best = j;
229 }
230 }
231 acadmap[i] = best;
232 }
233
234 /* Swoop over the entire map and replace each RGB pixel with its
235 closest AutoCAD color representation. We do this in a
236 separate pass since it avoids repetitive mapping of pixels as
237 we examine them for compression. */
238
239 for (i = 0; i < ydots; i++) {
240 int x;
241
242 for (x = 0; x < xdots; x++) {
243 PPM_ASSIGN(pixels[i][x],
244 acadmap[ppm_lookupcolor(cht, &pixels[i][x])], 0 ,0);
245 }
246 }
247
248 /* Output a run-length encoded expression of the pixmap as AutoCAD
249 vectors. */
250
251 for (i = 0; i < ydots; i++) {
252 int x, rx, rpix = -1, nrun = 0;
253
254 for (x = 0; x < xdots; x++) {
255 int pix = PPM_GETR(pixels[i][x]);
256
257 if (pix != rpix) {
258 if (nrun > 0) {
259 if (rpix > 0) {
260 outrun(rpix, ydots - 1, i, rx, x - 1);
261 }
262 }
263 rpix = pix;
264 rx = x;
265 nrun = 1;
266 }
267 }
268 if ((nrun > 0) && (rpix > 0)) {
269 outrun(rpix, ydots - 1, i, rx, xdots - 1);
270 }
271 }
272 if (dxbmode) {
273 putchar(0); /* DXB end sentinel */
274 } else {
275 (void) pm_writelittleshort(stdout, (short) 0xFC00);
276 /* End of file marker */
277 }
278 pm_freerow((char *) acadmap);
279 }
280
281
282
283 /* Main program. */
284
285 int main(int argc, char* argv[]) {
286
287 FILE *ifp;
288 int argn, rows, cols, ncolors, i;
289 int aspectspec = FALSE;
290 pixval maxval;
291 colorhist_vector chv;
292 unsigned char *Red, *Green, *Blue;
293 const char * const usage =
294 "[-poly] [-dxb] [-white] [-background <col>]\n\
295 [-aspect <f>] [-8] [ppmfile]";
296
297 ppm_init(&argc, argv);
298
299 argn = 1;
300 while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != EOS) {
301 if (pm_keymatch(argv[argn], "-dxb", 2)) {
302 dxbmode = polymode = TRUE;
303 } else if (pm_keymatch(argv[argn], "-poly", 2)) {
304 polymode = TRUE;
305 } else if (pm_keymatch(argv[argn], "-white", 2)) {
306 if (bgcol >= 0) {
307 pm_error("already specified a background color");
308 }
309 bgcol = 7;
310 } else if (pm_keymatch(argv[argn], "-background", 2)) {
311 if (bgcol >= 0) {
312 pm_error("already specified a background color");
313 }
314 argn++;
315 if ((argn == argc) || (sscanf(argv[argn], "%d", &bgcol) != 1))
316 pm_usage(usage);
317 if (bgcol < 0) {
318 pm_error("background color must be >= 0 and <= 255");
319 }
320 } else if (pm_keymatch(argv[argn], "-aspect", 2)) {
321 if (aspectspec) {
322 pm_error("already specified an aspect ratio");
323 }
324 argn++;
325 if ((argn == argc) || (sscanf(argv[argn], "%lf", &aspect) != 1))
326 pm_usage(usage);
327 if (aspect <= 0.0) {
328 pm_error("aspect ratio must be greater than 0");
329 }
330 aspectspec = TRUE;
331 } else if (pm_keymatch(argv[argn], "-8", 2)) {
332 gamut = 8;
333 } else {
334 pm_usage(usage);
335 }
336 argn++;
337 }
338
339 if (argn < argc) {
340 ifp = pm_openr(argv[argn]);
341 argn++;
342 } else {
343 ifp = stdin;
344 }
345
346 if (argn != argc) {
347 pm_usage(usage);
348 }
349
350 pixels = ppm_readppm(ifp, &cols, &rows, &maxval);
351
352 pm_close(ifp);
353
354 /* Figure out the colormap. Logic for squeezing depth to limit the
355 number of colors in the image was swiped from ppmquant.c. */
356
357 while (TRUE) {
358 int row, col;
359 pixval newmaxval;
360 pixel *pP;
361
362 pm_message("computing colormap...");
363 chv = ppm_computecolorhist(pixels, cols, rows, MAXHIST, &ncolors);
364 if (chv != (colorhist_vector) 0)
365 break;
366 newmaxval = maxval / 2;
367 pm_message(
368 "scaling colors from maxval=%d to maxval=%d to improve clustering...",
369 maxval, newmaxval );
370 for (row = 0; row < rows; ++row) {
371 for (col = 0, pP = pixels[row]; col < cols; ++col, ++pP) {
372 PPM_DEPTH(*pP, *pP, maxval, newmaxval);
373 }
374 }
375 maxval = newmaxval;
376 }
377 pm_message("%d colors found", ncolors);
378
379 /* Scale the color map derived for the PPM file into one compatible
380 with AutoCAD's convention of 8 bit intensities. */
381
382 if (maxval != 255) {
383 pm_message("maxval is not 255 - automatically rescaling colors");
384 }
385 Red = (unsigned char *) pm_allocrow(ncolors, sizeof(unsigned char));
386 Green = (unsigned char *) pm_allocrow(ncolors, sizeof(unsigned char));
387 Blue = (unsigned char *) pm_allocrow(ncolors, sizeof(unsigned char));
388
389 for (i = 0; i < ncolors; ++i) {
390 if ( maxval == 255 ) {
391 Red[i] = PPM_GETR(chv[i].color);
392 Green[i] = PPM_GETG(chv[i].color);
393 Blue[i] = PPM_GETB( chv[i].color );
394 } else {
395 Red[i] = ((int) PPM_GETR(chv[i].color) * 255) / maxval;
396 Green[i] = ((int) PPM_GETG(chv[i].color) * 255) / maxval;
397 Blue[i] = ((int) PPM_GETB(chv[i].color) * 255) / maxval;
398 }
399 }
400
401 /* And make a hash table for fast lookup. */
402
403 cht = ppm_colorhisttocolorhash(chv, ncolors);
404 ppm_freecolorhist(chv);
405
406 slideout(cols, rows, ncolors, Red, Green, Blue);
407 pm_freerow((char *) Red);
408 pm_freerow((char *) Green);
409 pm_freerow((char *) Blue);
410 exit(0);
411 }
412
413
414