about summary refs log tree commit diff
path: root/converter/ppm/ppmtomitsu.c
blob: 3934ae45d20afd4f46c180a674d0034726409a8a (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
/* ppmtomitsu.c - read a portable pixmap and produce output for the
**                Mitsubishi S340-10 Thermo-Sublimation Printer
**                (or the S3410-30 parallel interface)
**
** Copyright (C) 1992,93 by S.Petra Zeidler
** Minor modifications by Ingo Wilken:
x**  - mymalloc() and check_and_rotate() functions for often used
**    code fragments.  Reduces code size by a few KB.
**  - use pm_error() instead of fprintf(stderr)
**  - localized allocation of colorhastable
**
** This software was written for the Max Planck Institut fuer Radioastronomie,
** Bonn, Germany, Optical Interferometry group
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include <string.h>

#include "pm_c_util.h"
#include "ppm.h"
#include "nstring.h"
#include "mallocvar.h"

#include "mitsu.h"


#include <stdio.h>

#define HASHSIZE 2048
#define myhash(x) ((PPM_GETR(x)*3 + PPM_GETG(x)*5 + PPM_GETB(x)*7) % HASHSIZE)

typedef struct hashinfo {
        pixel     color;
        long      flag;
        struct hashinfo *next;
} hashinfo;

#ifdef __STDC__
static void lineputinit(int cols, int rows, int sharpness, int enlarge, int
                        copy, struct mediasize medias);
static void frametransferinit(int cols, int rows, int sharpness, int enlarge,
                              int copy, struct mediasize medias);
static void lookuptableinit(int sharpness, int enlarge, int copy,
                            struct mediasize medias);
static void lookuptabledata(int cols, int rows, int enlarge,
                            struct mediasize medias);
static void check_and_rotate(int cols, int rows, int enlarge,
                             struct mediasize medias);
#define CONST const
#else /*__STDC__*/
static int lineputinit();
static int lookuptableinit();
static int lookuptabledata();
static int frametransferinit();
static int check_and_rotate();
#define CONST
#endif

#define cmd(arg)           fputc((arg), stdout)
#define datum(arg)         fputc((char)(arg), stdout)
#define data(arg,num)      fwrite((arg), sizeof(char), (num), stdout)


#ifdef __STDC__
int main(int argc, char *argv[] )
#else
int main( argc, argv )
    int argc;
    char* argv[];
#endif
    {
    FILE             *ifp;
    /*hashinfo         colorhashtable[HASHSIZE];*/
    struct hashinfo  *hashrun;
    pixel            *xP;
    int              argn;
    bool             dpi300;
    int              cols, rows, format, col, row;
    int              sharpness, enlarge, copy, tiny;
    pixval           maxval;
    struct mediasize medias;
    char             media[16];
    const char * const usage = "[-sharpness <1-4>] [-enlarge <1-3>] [-media <a,a4,as,a4s>] [-copy <1-9>] [-tiny] [-dpi300] [ppmfile]";

    ppm_init(&argc, argv);

    dpi300 = FALSE;
    argn = 1;
    sharpness = 32;
    enlarge   = 1;
    copy      = 1;
    memset(media, '\0', 16);
    tiny      = FALSE;

    /* check for flags */
    while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0') {
    if (pm_keymatch(argv[argn], "-sharpness", 2)) {
        ++argn;
        if (argn == argc || sscanf(argv[argn], "%d", &sharpness) != 1)
            pm_usage(usage);
        else if (sharpness < 1 || sharpness > 4)
            pm_usage(usage);
        }
    else if (pm_keymatch(argv[argn], "-enlarge", 2)) {
        ++argn;
        if (argn == argc || sscanf(argv[argn], "%d", &enlarge) != 1)
            pm_usage(usage);
        else if (enlarge < 1 || enlarge > 3)
            pm_usage(usage);
        }
    else if (pm_keymatch(argv[argn], "-media", 2)) {
        ++argn;
        if (argn == argc || sscanf(argv[argn], "%15s", media) < 1)
            pm_usage(usage);
        else if (TOUPPER(media[0]) != 'A')
            pm_usage(usage);
    }
    else if (pm_keymatch(argv[argn], "-copy", 2)) {
        ++argn;
        if (argn == argc || sscanf(argv[argn], "%d", &copy) != 1)
            pm_usage(usage);
        else if (copy < 1 || copy > 9)
            pm_usage(usage);
        }
    else if (pm_keymatch(argv[argn], "-dpi300", 2))
        dpi300 = TRUE;
    else if (pm_keymatch(argv[argn], "-tiny", 2))
        tiny = TRUE;
    else
        pm_usage(usage);
        ++argn;
    }

    if (argn < argc) {
        ifp = pm_openr(argv[argn]);
        ++argn;
    }
    else
        ifp = stdin;

    if (argn != argc)
        pm_usage(usage);

    if (TOUPPER(media[0]) == 'A')
        switch (TOUPPER(media[1])) {
        case 'S':
            medias = MSize_AS;
            break;
        case '4':
            if(TOUPPER(media[2]) == 'S')
                medias = MSize_A4S;
            else {
                medias = MSize_A4;
            }
            break;
        default:
            medias = MSize_A;
        }
    else
        medias = MSize_User;

        if (dpi300) {
                medias.maxcols *= 2;
                medias.maxrows *= 2;
        }

    if (tiny) {
        pixel            *pixelrow;
        char             *redrow, *greenrow, *bluerow;

        ppm_readppminit(ifp, &cols, &rows, &maxval, &format);
        pixelrow = (pixel *) ppm_allocrow(cols);
        MALLOCARRAY_NOFAIL(redrow, cols);
        MALLOCARRAY_NOFAIL(greenrow, cols);
        MALLOCARRAY_NOFAIL(bluerow, cols);
        lineputinit(cols, rows, sharpness, enlarge, copy, medias);

        for ( row = 0; row < rows; ++row ) {
            ppm_readppmrow(ifp, pixelrow, cols, maxval, format);
            switch(PPM_FORMAT_TYPE(format)) {
            /* color */
            case PPM_TYPE:
                for (col = 0, xP = pixelrow; col < cols; col++, xP++) {
                    /* First red. */
                    redrow[col] = PPM_GETR(*xP);
                    /* Then green. */
                    greenrow[col] = PPM_GETG(*xP);
                    /* And blue. */
                    bluerow[col] = PPM_GETB(*xP);
                }
                data(redrow,   cols);
                data(greenrow, cols);
                data(bluerow,  cols);
                break;
            /* grayscale */
            default:
                for (col = 0, xP = pixelrow; col < cols; col++, xP++)
                    bluerow[col] = PPM_GETB(*xP);
                data(bluerow, cols);
                data(bluerow, cols);
                data(bluerow, cols);
                break;
            }
        }
        pm_close(ifp);
    }
    else {
        pixel            **pixelpic;
        int              colanz, colval;
        int                 i;
        colorhist_vector table;

        ppm_readppminit( ifp, &cols, &rows, &maxval, &format );
        pixelpic = ppm_allocarray( cols, rows );
        for (row = 0; row < rows; row++)
            ppm_readppmrow( ifp, pixelpic[row], cols, maxval, format );
        pm_close(ifp);

        /* first check wether we can use the lut transfer */

        table = ppm_computecolorhist(pixelpic, cols, rows, MAXLUTCOL+1, 
                                     &colanz);
        if (table != NULL) {
            hashinfo *colorhashtable;

            MALLOCARRAY_NOFAIL(colorhashtable, HASHSIZE);
            for (i=0; i<HASHSIZE; i++) {
                colorhashtable[i].flag = -1;
                colorhashtable[i].next = NULL;
            }

            /* we can use the lookuptable */
            pm_message("found %d colors - using the lookuptable-method",
                       colanz);
            lookuptableinit(sharpness, enlarge, copy, medias);
            switch(PPM_FORMAT_TYPE(format)) {
            /* color */
            case PPM_TYPE:
                for (colval=0; colval<colanz; colval++) {
                    cmd('$');
                    datum(colval);
                    datum(PPM_GETR((table[colval]).color));
                    datum(PPM_GETG((table[colval]).color));
                    datum(PPM_GETB((table[colval]).color));

                    hashrun = &colorhashtable[myhash((table[colval]).color)];
                    if (hashrun->flag == -1) {
                        hashrun->color = (table[colval]).color;
                        hashrun->flag  = colval;
                    }
                    else {
                        while (hashrun->next != NULL)
                            hashrun = hashrun->next;
                        MALLOCVAR_NOFAIL(hashrun->next);
                        hashrun = hashrun->next;
                        hashrun->color = (table[colval]).color;
                        hashrun->flag  = colval;
                        hashrun->next  = NULL;
                    }
                }
                break;
            /* other */
            default:
                for (colval=0; colval<colanz; colval++) {
                    cmd('$');
                    datum(colval);
                    datum(PPM_GETB((table[colval]).color));
                    datum(PPM_GETB((table[colval]).color));
                    datum(PPM_GETB((table[colval]).color));

                    hashrun = &colorhashtable[myhash((table[colval]).color)];
                    if (hashrun->flag == -1) {
                        hashrun->color = (table[colval]).color;
                        hashrun->flag  = colval;
                    }
                    else {
                        while (hashrun->next != NULL)
                            hashrun = hashrun->next;
                        MALLOCVAR_NOFAIL(hashrun->next);
                        hashrun = hashrun->next;
                        hashrun->color = (table[colval]).color;
                        hashrun->flag  = colval;
                        hashrun->next  = NULL;
                    }
                }
            }
            lookuptabledata(cols, rows, enlarge, medias);
            for (row=0; row<rows; row++) {
                xP = pixelpic[row];
                for (col=0; col<cols; col++, xP++) {
                    hashrun = &colorhashtable[myhash(*xP)];
                    while (!PPM_EQUAL((hashrun->color), *xP))
                        if (hashrun->next != NULL)
                            hashrun = hashrun->next;
                        else {
                            pm_error("you just found a lethal bug.");
                        }
                    datum(hashrun->flag);
                }
            }
            free(colorhashtable);
        }
        else {
        /* $#%@^!& no lut possible, so send the pic as 24bit */
            pm_message("found too many colors for fast lookuptable mode");
            frametransferinit(cols, rows, sharpness, enlarge, copy, medias);
            switch(PPM_FORMAT_TYPE(format)) {
            /* color */
            case PPM_TYPE:
                COLORDES(RED);
                DATASTART;                    /* red coming */
                for (row=0; row<rows; row++) {
                    xP = pixelpic[row];
                    for (col=0; col<cols; col++, xP++)
                        datum(PPM_GETR(*xP));
                }
                COLORDES(GREEN);
                DATASTART;                    /* green coming */
                for (row=0; row<rows; row++) {
                    xP = pixelpic[row];
                    for (col=0; col<cols; col++, xP++)
                        datum(PPM_GETG(*xP));
                }
                COLORDES(BLUE);
                DATASTART;                    /* blue coming */
                for (row=0; row<rows; row++) {
                    xP = pixelpic[row];
                    for (col=0; col<cols; col++, xP++)
                        datum(PPM_GETB(*xP));
                }
                break;
            /* grayscale */
            default:
                COLORDES(RED);
                DATASTART;                    /* red coming */
                for (row=0; row<rows; row++) {
                    xP = pixelpic[row];
                    for (col=0; col<cols; col++, xP++)
                        datum(PPM_GETB(*xP));
                }
                COLORDES(GREEN);
                DATASTART;                    /* green coming */
                for (row=0; row<rows; row++) {
                    xP = pixelpic[row];
                    for (col=0; col<cols; col++, xP++)
                        datum(PPM_GETB(*xP));
                }
                COLORDES(BLUE);
                DATASTART;                    /* blue coming */
                for (row=0; row<rows; row++) {
                    xP = pixelpic[row];
                    for (col=0; col<cols; col++, xP++)
                        datum(PPM_GETB(*xP));
                }
            }
        }
    }
    PRINTIT;
    exit(0);
}

#ifdef __STDC__
static void lineputinit(int cols, int rows,
                        int sharpness, int enlarge, int copy,
                        struct mediasize medias)
#else /*__STDC__*/
static int lineputinit(cols, rows, sharpness, enlarge, copy, medias)
    int cols, rows;
    int sharpness, enlarge, copy;
    struct mediasize medias;
#endif /*__STDC__*/
{
    ONLINE;
    CLRMEM;
    MEDIASIZE(medias);

    switch (enlarge) {
    case 2:
        HENLARGE(ENLARGEx2); /* enlarge horizontal */
        VENLARGE(ENLARGEx2); /* enlarge vertical */
        break;
    case 3:
        HENLARGE(ENLARGEx3); /* enlarge horizontal */
        VENLARGE(ENLARGEx3); /* enlarge vertical */
        break;
    default:
        HENLARGE(NOENLARGE); /* enlarge horizontal */
        VENLARGE(NOENLARGE); /* enlarge vertical */
    }

    COLREVERSION(DONTREVERTCOLOR);
    NUMCOPY(copy);

    HOFFINCH('\000');
    VOFFINCH('\000');
    CENTERING(DONTCENTER);

    TRANSFERFORMAT(LINEORDER);
    COLORSYSTEM(RGB);
    GRAYSCALELVL(BIT_8);

    switch (sharpness) {          /* sharpness :-) */
    case 0:
        SHARPNESS(SP_NONE);
        break;
    case 1:
        SHARPNESS(SP_LOW);
        break;
    case 2:
        SHARPNESS(SP_MIDLOW);
        break;
    case 3:
        SHARPNESS(SP_MIDHIGH);
        break;
    case 4:
        SHARPNESS(SP_HIGH);
        break;
    default:
        SHARPNESS(SP_USER);
    }
    check_and_rotate(cols, rows, enlarge, medias);
    DATASTART;
    return;
}

#ifdef __STDC__
static void lookuptableinit(int sharpness, int enlarge, int copy,
                            struct mediasize medias)
#else /*__STDC__*/
static int lookuptableinit(sharpness, enlarge, copy, medias)
    int sharpness, enlarge, copy;
    struct mediasize medias;
#endif /*__STDC__*/
{
    ONLINE;
    CLRMEM;
    MEDIASIZE(medias);

    switch (enlarge) {
    case 2:
        HENLARGE(ENLARGEx2); /* enlarge horizontal */
        VENLARGE(ENLARGEx2); /* enlarge vertical */
        break;
    case 3:
        HENLARGE(ENLARGEx3); /* enlarge horizontal */
        VENLARGE(ENLARGEx3); /* enlarge vertical */
        break;
    default:
        HENLARGE(NOENLARGE); /* enlarge horizontal */
        VENLARGE(NOENLARGE); /* enlarge vertical */
    }

    COLREVERSION(DONTREVERTCOLOR);
    NUMCOPY(copy);

    HOFFINCH('\000');
    VOFFINCH('\000');
    CENTERING(DONTCENTER);

    TRANSFERFORMAT(LOOKUPTABLE);

    switch (sharpness) {          /* sharpness :-) */
    case 0:
        SHARPNESS(SP_NONE);
        break;
    case 1:
        SHARPNESS(SP_LOW);
        break;
    case 2:
        SHARPNESS(SP_MIDLOW);
        break;
    case 3:
        SHARPNESS(SP_MIDHIGH);
        break;
    case 4:
        SHARPNESS(SP_HIGH);
        break;
    default:
        SHARPNESS(SP_USER);
    }

    LOADLOOKUPTABLE;
    return;
}

#ifdef __STDC__
static void lookuptabledata(int cols, int rows, int enlarge,
                                                        struct mediasize medias)
#else /*__STDC__*/
static int lookuptabledata(cols, rows, enlarge, medias)
    int   rows, cols;
    int   enlarge;
    struct mediasize medias;
#endif /*__STDC__*/
{
    DONELOOKUPTABLE;
    check_and_rotate(cols, rows, enlarge, medias);
    DATASTART;
    return;
}

#ifdef __STDC__
static void frametransferinit(int cols, int rows, int sharpness,
                              int enlarge, int copy, struct mediasize medias)
#else
static int frametransferinit(cols, rows, sharpness, enlarge, copy, medias)

    int     rows, cols;
    int     sharpness, enlarge, copy;
    struct mediasize medias;
#endif
{
    ONLINE;
    CLRMEM;
    MEDIASIZE(medias);

    switch (enlarge) {
    case 2:
        HENLARGE(ENLARGEx2); /* enlarge horizontal */
        VENLARGE(ENLARGEx2); /* enlarge vertical */
        break;
    case 3:
        HENLARGE(ENLARGEx3); /* enlarge horizontal */
        VENLARGE(ENLARGEx3); /* enlarge vertical */
        break;
    default:
        HENLARGE(NOENLARGE); /* enlarge horizontal */
        VENLARGE(NOENLARGE); /* enlarge vertical */
    }

    COLREVERSION(DONTREVERTCOLOR);
    NUMCOPY(copy);

    HOFFINCH('\000');
    VOFFINCH('\000');
    CENTERING(DONTCENTER);

    TRANSFERFORMAT(FRAMEORDER);
    COLORSYSTEM(RGB);
    GRAYSCALELVL(BIT_8);

    switch (sharpness) {          /* sharpness :-) */
    case 0:
        SHARPNESS(SP_NONE);
        break;
    case 1:
        SHARPNESS(SP_LOW);
        break;
    case 2:
        SHARPNESS(SP_MIDLOW);
        break;
    case 3:
        SHARPNESS(SP_MIDHIGH);
        break;
    case 4:
        SHARPNESS(SP_HIGH);
        break;
    default:
        SHARPNESS(SP_USER);
    }
    check_and_rotate(cols, rows, enlarge, medias);
    return;
}


#ifdef __STDC__
static void
check_and_rotate(int cols, int rows, int enlarge, struct mediasize medias)
#else
static int
check_and_rotate(cols, rows, enlarge, medias)
    int cols, rows, enlarge;
    struct mediasize medias;
#endif
{
    if (cols > rows) {
        ROTATEIMG(DOROTATE);                        /* rotate image */
        if (enlarge*rows > medias.maxcols || enlarge*cols > medias.maxrows) {
            pm_error("Image too large, MaxPixels = %d x %d", medias.maxrows, medias.maxcols);
        }
        HPIXELS(cols);
        VPIXELS(rows);
        HPIXELSOFF((medias.maxcols/enlarge - rows)/2);
        VPIXELSOFF((medias.maxrows/enlarge - cols)/2);
        pm_message("rotating image for output");
    }
    else {
        ROTATEIMG(DONTROTATE);
        if (enlarge*rows > medias.maxrows || enlarge*cols > medias.maxcols) {
            pm_error("Image too large, MaxPixels = %d x %d", medias.maxrows, medias.maxcols);
        }
        HPIXELS(cols);
        VPIXELS(rows);
        HPIXELSOFF((medias.maxcols/enlarge - cols)/2);
        VPIXELSOFF((medias.maxrows/enlarge - rows)/2);
    }
}