about summary refs log tree commit diff
path: root/converter/ppm/sldtoppm.c
blob: 6ba4cb40ae60cf812aa8f36e0abd73a2c8d15578 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*

      Convert an AutoCAD slide (.sld) file to PPM format

    An AutoCAD slide is a compressed sequence of  vectors  and  filled
    polygons.   The  ppmdraw  package  is  used  to scan convert these
    geometrical objects into a portable pixmap.

    Author:
        John Walker
        Autodesk SA
        Avenue des Champs-Montants 14b
        CH-2074 MARIN
        Switzerland
        Usenet: kelvin@Autodesk.com
        Fax:    038/33 88 15
        Voice:  038/33 76 33

    Permission  to  use, copy, modify, and distribute this software and
    its documentation  for  any  purpose  and  without  fee  is  hereby
    granted,  without any conditions or restrictions.  This software is
    provided "as is" without express or implied warranty.

*/

#include <string.h>
#include <math.h>

#include "pm_c_util.h"
#include "ppm.h"
#include "ppmdraw.h"
#include "nstring.h"
#include <assert.h>

#include "autocad.h"                  /* AutoCAD standard color assignments */


/*  Define a variable type accepting numbers -127 <= n <= 127.  But note
    that we still expect it to act UNSIGNED. */

#define smallint unsigned char        /* Small integers */

#define EOS     '\0'

/* Screen point */

struct spoint {
    int x, y;
};

/* Screen polygon */

struct spolygon { 
    int npoints,              /* Number of points in polygon */
          fill;           /* Fill type */
    struct spoint pt[11];         /* Actual points */
};

/* Screen vector */

struct svector {
    struct spoint f;          /* From point */
    struct spoint t;          /* To point */
};

typedef void (slvecfn)(struct svector * vec, int color);
typedef void (slfloodfn)(struct spolygon * poly, int color);


static unsigned long const pixmaxval = 255;  /* Largest pixel value */

static int ixdots, iydots;        /* Screen size in dots */
static FILE * slfile;             /* Slide file descriptor */
static bool blither;              /* Dump slide file information ? */
static bool info;                 /* Print header information */
static pixel **pixels;            /* Pixel map */
static int pixcols, pixrows;      /* Pixel map size */
static double uscale = -1;        /* Uniform scale factor */
static int sxsize = -1, sysize = -1;  /* Scale to X, Y size ? */

/*  Local variables  */

struct slhead {
    char slh[17];             /* Primate-readable header */
    char sntype;              /* Machine type (for number compat) */
    char slevel;              /* Format type */
    short sxdots, sydots;         /* Display X, Y dots */
    double sdsar;             /* Display aspect ratio */
    short shwfill;            /* Display hardware fill type */
    char spad;                /* Pad to even byte length */
};

static bool adjust;           /* Adjust to correct aspect ratio ? */
static struct slhead slfrof;  /* Slide file header */
static long xfac, yfac;       /* Aspect ratio scale factors */

static bool sdrawkcab;
    /* Slide drawing kinematic conversion of ass-backwards data flag */



/*  EXTEND  --  Turn a smallint into an int with sign extension, whether
    or not that happens automatically.
*/

static int
extend(unsigned char const ch) {
    return ((int) ((ch & 0x80) ? (ch | ~0xFF) : ch));
}



/*  SLI  --  Input word from slide file  */

static int
sli(void) {
    short wd;

    if (fread(&wd, sizeof wd, 1, slfile) != 1) {
        pm_error("error reading slide file");
    } else {
    if (sdrawkcab) {
        wd = ((wd >> 8) & 0xFF) | (wd << 8);
    }
    }
    return wd;
}



/*  SLIB  --  Input byte from slide file  */

static int 
slib(void) {
    unsigned char ch;

    if (fread(&ch, sizeof(ch), 1, slfile) != 1) {
        pm_error("error reading slide file");
    }
    return extend(ch);
}



/*  VSCALE -- scale screen coordinates for mismatched display.  */

static void
vscale(int * const px,
       int * const py) {

    *px = (((unsigned) *px) * xfac) >> 16;
    *py = (((unsigned) *py) * yfac) >> 16;
}



/*  SLIDEFIND  --  Find  a  slide  in  a  library  or,  if  DIRONLY is
           nonzero, print a directory listing of the  library.
           If  UCASEN  is nonzero, the requested slide name is
           converted to upper case. */

static void
slidefind(const char * const sname,
          bool         const dironly,
          bool         const ucasen) {

    char uname[32];
    unsigned char libent[36];
    long pos;
    bool found;
    bool eof;

    if (dironly)
        pm_message("Slides in library:");
    else {
        unsigned int i;
        const char * ip;
        
        ip = sname; /* initial value */
        
        for (i = 0; i < 31; ++i) {
            char const ch = *ip++;
            if (ch == EOS)
                break;

            {
                char const upperCh =
                    ucasen && islower(ch) ? toupper(ch) : ch;
                
                uname[i] = upperCh;
            }
        }
        uname[i] = EOS;
    }
    
    /* Read slide library header and verify. */
    
    if ((fread(libent, 32, 1, slfile) != 1) ||
        (!streq((char *)libent, "AutoCAD Slide Library 1.0\015\012\32"))) {
        pm_error("not an AutoCAD slide library file.");
    }
    pos = 32;
    
    /* Search for a slide with the requested name or list the directory */
    
    for (found = false, eof = false; !found && !eof; ) {
        size_t readCt;
        readCt = fread(libent, 36, 1, slfile);
        if (readCt != 1)
            eof = true;
        else if (strlen((char *)libent) == 0)
            eof = true;
    }
    if (!eof) {
        pos += 36;
        if (dironly) {
            pm_message("  %s", libent);
        } else if (streq((char *)libent, uname)) {
            long dpos;

            dpos = (((((libent[35] << 8) | libent[34]) << 8) |
                     libent[33]) << 8) | libent[32];

            if ((slfile == stdin) || (fseek(slfile, dpos, 0) == -1)) {
                dpos -= pos;
        
                while (dpos-- > 0)
                    getc(slfile);
            }
            found = true;
        }
    }

    if (!found && !dironly)
        pm_error("slide '%s' not in library.", sname);
}



/*  DRAW  --  Draw a vector in the given AutoCAD color.  */

static slvecfn draw;

static void
draw(struct svector * vec,
     int              color) {

    pixel rgbcolor;

    if (blither) {
        pm_message("Vector (%d, %d) - (%d, %d)  Color %d",
           vec->f.x, vec->f.y, vec->t.x, vec->t.y, color);
    }
    assert(vec->f.x >= 0 && vec->f.x < pixcols);
    assert(vec->f.y >= 0 && vec->f.y < pixrows);
    assert(vec->t.x >= 0 && vec->t.x < pixcols);
    assert(vec->t.y >= 0 && vec->t.y < pixrows);
    PPM_ASSIGN(rgbcolor,
               acadcol[color][0], acadcol[color][1], acadcol[color][2]);
    ppmd_line(pixels, pixcols, pixrows, pixmaxval,
              vec->f.x, iydots - vec->f.y, vec->t.x, iydots - vec->t.y,
              PPMD_NULLDRAWPROC,
              (char *) &rgbcolor);
}



/*  FLOOD  --  Draw a filled polygon.  */

static slfloodfn flood;

static void
flood(struct spolygon * const poly,
      int               const color) {

    unsigned int i;
    struct fillobj * handle;
    pixel rgbcolor;

    handle = ppmd_fill_create();

    if (blither) {
        unsigned int i;
        pm_message("Polygon: %d points, fill type %d, color %d",
                   poly->npoints, poly->fill, color);
        for (i = 0; i < poly->npoints; i++) {
            pm_message("   Point %d:  (%d, %d)", i + 1,
                       poly->pt[i].x, poly->pt[i].y);
        }
    }

    PPM_ASSIGN(rgbcolor,
               acadcol[color][0], acadcol[color][1], acadcol[color][2]);
    for (i = 0; i < poly->npoints; i++) {
        assert(poly->pt[i].x >= 0 && poly->pt[i].x < pixcols);
        assert(poly->pt[i].y >= 0 && poly->pt[i].y < pixrows);
        ppmd_line(pixels, pixcols, pixrows, pixmaxval,
                  poly->pt[i].x, iydots - poly->pt[i].y, 
                  poly->pt[(i + 1) % poly->npoints].x,
                  iydots - poly->pt[(i + 1) % poly->npoints].y,
                  ppmd_fill_drawproc, handle);
    }
    ppmd_fill(pixels, pixcols, pixrows, pixmaxval,
              handle, PPMD_NULLDRAWPROC, (char *) &rgbcolor);
    
    ppmd_fill_destroy(handle);
}



/*  SLIDER  --  Read slide file.  This is called with the name of the
        file to be read and function pointers to the routines
        which process vectors and polygon fill requests
        respectively.
*/

static void
slider(slvecfn   slvec,
       slfloodfn slflood) {

    int i, rescale;
    unsigned char ubfr[4];        /* Utility character buffer */
    int lx, ly;               /* Last x and y point */
    int slx, sly;             /* Last x and y scaled screen point */
    struct svector vec;           /* Screen vector */
    struct spolygon poly;         /* Screen polygon */
    unsigned short cw;            /* Control word */
    double dsar;              /* Screen aspect ratio */
    long ldsar;               /* Scaled long DSAR */
    short rtest;              /* Value to test byte reversal */
    short btest = 0x1234;         /* Value to test byte-reversal */
    static struct slhead slhi =       /* Master slide header sample */
    {"AutoCAD Slide\r\n\32", 86,2, 0,0, 0.0, 0};
    int curcolor = 7;             /* Current vector color */
    pixel rgbcolor;           /* Pixel used to clear pixmap */
    
    lx = ly = 32000;
    
    /* Process the header of the slide file.  */
    
    sdrawkcab = false;            /* Initially guess byte order is OK */
    fread(slfrof.slh, 17, 1, slfile);
    fread(&slfrof.sntype, sizeof(char), 1, slfile);
    fread(&slfrof.slevel, sizeof(char), 1, slfile);
    fread(&slfrof.sxdots, sizeof(short), 1, slfile);
    fread(&slfrof.sydots, sizeof(short), 1, slfile);
    fread(ubfr, 4, 1, slfile);
    fread(&slfrof.shwfill, sizeof(short), 1, slfile);
    fread(&rtest, sizeof rtest, 1, slfile);

    /* Verify that slide format is compatible with this program. */

    if (streq(slfrof.slh, slhi.slh))
        pm_error("this is not an AutoCAD slide file.");

    /* Verify that the number format and file level in the header  are
       compatible.  All slides written by versions of AutoCAD released
       since September of 1987 are compatible with this format.  */

    if ((slfrof.sntype != slhi.sntype) || (slfrof.slevel != slhi.slevel))
        pm_error("incompatible slide file format");

    /* Build SDSAR value from long scaled version. */
    
    ldsar = 0L;
    for (i = 3; i >= 0; --i)
        ldsar = (ldsar << 8) | ubfr[i];
    slfrof.sdsar = ((double) ldsar) / 1E7;
    
    /* Examine the byte order test value.   If it's backwards, set the
       byte-reversal flag and correct all of the values we've read  in
       so far.
    */

    if (btest != rtest) {
        sdrawkcab = true;
        #define rshort(x) x = ((x >> 8) & 0xFF) | (x << 8)
        rshort(slfrof.sxdots);
        rshort(slfrof.sydots);
        rshort(slfrof.shwfill);
        #undef rshort
    }
    
    /* Dump the header if we're blithering. */

    if (blither || info) {
        pm_message("Slide file type %d, level %d, hwfill type %d.",
                   slfrof.sntype, slfrof.slevel, slfrof.shwfill);
        pm_message("Original screen size %dx%d, aspect ratio %.3f.",
                   slfrof.sxdots + 1, slfrof.sydots + 1, slfrof.sdsar);
        pm_message("Byte order is %s.",
                   sdrawkcab ? "being reversed" : "the same");
    }

    /* If the display aspect ratio indicates that the  pixels  on  the
       sending  screen  were  not  square,  adjust  the  size  of  the
       generated bitmap to correct the  aspect  ratio  to  square  the
       pixels.

       We  always  correct  the aspect ratio by adjusting the width of
       the image.  This guarantees that output from the SHADE command,
       which  is  essentially  scan-line  data written in vector form,
       will not be corrupted.
    */

    dsar = ((double) slfrof.sxdots) / slfrof.sydots;
    if (fabs(slfrof.sdsar - dsar) > 0.0001) {
        if (adjust) {
            ixdots = slfrof.sxdots * (slfrof.sdsar / dsar) + 0.5;
            iydots = slfrof.sydots;
            dsar = ((double) ixdots) / iydots;
        } else {
            pm_message("Warning - pixels on source screen were non-square.  "
                       "Specifying -adjust will correct image width "
                       "to compensate.");
            ixdots = slfrof.sxdots;
            iydots = slfrof.sydots;
            dsar = slfrof.sdsar;
        }
    } else {
        /* Source pixels were square. */
        ixdots = slfrof.sxdots;
        iydots = slfrof.sydots;
        dsar = slfrof.sdsar;
        adjust = false;           /* Mark no adjustment needed */
    }

    /* If there's a uniform scale factor specified, apply it. */

    if (uscale > 0) {
        ixdots = (ixdots * uscale) + 0.5;
        iydots = (iydots * uscale) + 0.5;
    }

    /* If the image is to be stretched  to  a  given  width,  set  the
       output  image  sizes accordingly.  If only a height or width is
       given, scale the other direction proportionally to preserve the
       aspect ratio.
    */

    if (sxsize > 0) {
        if (sysize > 0) {
            iydots = sysize - 1;
        } else {
            iydots = ((((long) iydots) * (sxsize - 1)) +
                      (iydots / 2)) / ixdots;
        }
        ixdots = sxsize - 1;
    } else if (sysize > 0) {
        if (sxsize > 0) {
            ixdots = sxsize - 1;
        } else {
            ixdots = ((((long) ixdots) * (sysize - 1)) +
                      (ixdots / 2)) / iydots;
        }
        iydots = sysize - 1;
    }
    
    if (adjust) {
        pm_message(
            "Resized from %dx%d to %dx%d to correct pixel aspect ratio.",
            slfrof.sxdots + 1, slfrof.sydots + 1, ixdots + 1, iydots + 1);
    }
    
    /* Allocate image buffer and clear it to black. */
    
    pixels = ppm_allocarray(pixcols = ixdots + 1, pixrows = iydots + 1);
    PPM_ASSIGN(rgbcolor, 0, 0, 0);
    ppmd_filledrectangle(pixels, pixcols, pixrows, pixmaxval, 0, 0,
                         pixcols, pixrows, PPMD_NULLDRAWPROC,
                         (char *) &rgbcolor);
    
    if ((rescale = slfrof.sxdots != ixdots ||
         slfrof.sydots != iydots ||
         slfrof.sdsar != dsar) != 0) {
        
        /* Rescale all coords. so they'll look (more or less)
           right on this display.
        */
        
        xfac = (ixdots + 1) * 0x10000L;
        xfac /= (long) (slfrof.sxdots + 1);
        yfac = (iydots + 1) * 0x10000L;
        yfac /= (long) (slfrof.sydots + 1);
        if (dsar < slfrof.sdsar) {
            yfac = yfac * dsar / slfrof.sdsar;
        } else {
            xfac = xfac * slfrof.sdsar / dsar;
        }
    }

    poly.npoints = 0;             /* No flood in progress. */
    
    while ((cw = sli()) != 0xFC00) {
        switch (cw & 0xFF00) {
        case 0xFB00:          /*  Short vector compressed  */
            vec.f.x = lx + extend(cw & 0xFF);
            vec.f.y = ly + slib();
            vec.t.x = lx + slib();
            vec.t.y = ly + slib();
            lx = vec.f.x;
            ly = vec.f.y;
            if (rescale) {
                vscale(&vec.f.x, &vec.f.y);
                vscale(&vec.t.x, &vec.t.y);
            }
            (*slvec)(&vec, curcolor);/* Draw vector on screen */
            slx = vec.f.x;        /* Save scaled point */
            sly = vec.f.y;
            break;
            
        case 0xFC00:          /*  End of file  */
            break;
            
        case 0xFD00:          /*  Flood command  */
            vec.f.x = sli();
            vec.f.y = sli();
            if ((int) vec.f.y < 0) { /* start or end */
                if (poly.npoints != 0) { /* end?  */
                    if (poly.npoints > 2 && poly.npoints < 11) {
                        (*slflood)(&poly, curcolor);
                    } else {
                        pm_error("Bad polygon vertex count (%d)",
                                 poly.npoints);
                    }
                    poly.npoints = 0;
                } else {
                    poly.fill = -vec.f.y;  /* Start */
                }
            } else {          /* Polygon vertex */
                if (poly.npoints < 10) {
                    if (rescale) {
                        vscale(&vec.f.x, &vec.f.y);
                    }
                    poly.pt[poly.npoints].x = vec.f.x;
                    poly.pt[poly.npoints].y = vec.f.y;
                }
                poly.npoints++;
            }
            break;
            
        case 0xFE00:          /*  Common endpoint compressed  */
            vec.f.x = lx + extend(cw & 0xFF);
            vec.f.y = ly + slib();
            lx = vec.f.x;
            ly = vec.f.y;
            vec.t.x = slx;
            vec.t.y = sly;
            if (rescale) {
                vscale(&vec.f.x, &vec.f.y);
            }
            (*slvec)(&vec, curcolor);/* Draw vector */
            slx = vec.f.x;        /* Save scaled point */
            sly = vec.f.y;
            break;
            
        case 0xFF00:          /*  Change color  */
            curcolor = cw & 0xFF;
            break;

        default:              /*  Co-ordinates  */
            lx = vec.f.x = cw;
            ly = vec.f.y = sli();
            vec.t.x = sli();
            vec.t.y = sli();
            if (rescale) {
                vscale(&vec.f.x, &vec.f.y);
                vscale(&vec.t.x, &vec.t.y);
            }
            (*slvec)(&vec, curcolor);
            slx = vec.f.x;        /* Save scaled point */
            sly = vec.f.y;
            break;
        }
    }
}



/*  Main program. */

int
main(int          argc,
     const char * argv[]) {

    int argn;
    const char * const usage = "[-verbose] [-info] [-adjust] [-scale <s>]\n\
[-dir] [-lib|-Lib <name>]\n\
[-xsize|-width <x>] [-ysize|-height <y>] [sldfile]";
    bool dironly;
    bool hgtspec;
    bool widspec;
    bool scalespec;
    bool ucasen;
    const char * slobber;       /* Slide library item */

    pm_proginit(&argc, argv);
    argn = 1;

    slobber = NULL;
    dironly = false;
    hgtspec = false;
    widspec = false;
    scalespec = false;
    ucasen = false;
    blither = false;
    info = false;
    adjust = false;

    while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0') {
        if (pm_keymatch(argv[argn], "-verbose", 2)) {
            blither = true;
        } else if (pm_keymatch(argv[argn], "-adjust", 2)) {
            adjust = true;
        } else if (pm_keymatch(argv[argn], "-dir", 2)) {
            dironly = true;
        } else if (pm_keymatch(argv[argn], "-info", 2)) {
            info = true;
        } else if (pm_keymatch(argv[argn], "-lib", 2)) {
            if (slobber)
                pm_error("already specified a library item");
            ucasen = argv[argn][1] != 'L';
            argn++;
            if (argn == argc) {
                pm_usage(usage);
            }
            slobber = argv[argn];
        } else if (pm_keymatch(argv[argn], "-scale", 2)) {
            if (scalespec) {
                pm_error("already specified a scale factor");
            }
            argn++;
            if ((argn == argc) || (sscanf(argv[argn], "%lf", &uscale) != 1))
                pm_usage(usage);
            if (uscale <= 0.0) {
                pm_error("scale factor must be greater than 0");
            }
            scalespec = true;
        } else if (pm_keymatch(argv[argn], "-xsize", 2) ||
                   pm_keymatch(argv[argn], "-width", 2)) {
            if (widspec) {
                pm_error("already specified a width/xsize");
            }
            argn++;
            if ((argn == argc) || (sscanf(argv[argn], "%d", &sxsize) != 1))
                pm_usage(usage);
            widspec = true;
        } else if (pm_keymatch(argv[argn], "-ysize", 2) ||
                   pm_keymatch(argv[argn], "-height", 2)) {
            if (hgtspec) {
                pm_error("already specified a height/ysize");
            }
            argn++;
            if ((argn == argc) || (sscanf(argv[argn], "%d", &sysize) != 1))
                pm_usage(usage);
            hgtspec = true;
        } else {
            pm_usage(usage);
        }
        argn++;
    }

    /* If a file name is specified, open it.  Otherwise read from
       standard input. 
    */

    if (argn < argc) {
        slfile = pm_openr(argv[argn]);
        argn++;
    } else {
        slfile = stdin;
    }
    
    if (argn != argc) {           /* Extra bogus arguments ? */
        pm_usage(usage);
    }
    
    /* If we're extracting an item from a slide library, position the
       input stream to the start of the chosen slide.
    */
 
    if (dironly || slobber)
        slidefind(slobber, dironly, ucasen);
 
    if (!dironly) {
        slider(draw, flood);
        ppm_writeppm(stdout, pixels, pixcols, pixrows, pixmaxval, 0);
    }
    pm_close(slfile);
    pm_close(stdout);
    
    return 0;
}