about summary refs log tree commit diff
path: root/converter/ppm/ppmtoleaf.c
blob: dc73dc4839be6e848d897d331fa24e785ddf49e4 (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
/* ppmtoleaf.c - read a PPM and produce a ileaf img file
 *
 * Copyright (C) 1994 by Bill O'Donnell.
 *
 * 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.
 *
 * Known problems: pgms are not converted to leaf grayscales; they are
 * converted to 8-bit color images with all gray for colors.
 *
 */

#include <stdio.h>

#include "ppm.h"

#define MAXCOLORS 256

pixel ** pixels;
colorhash_table cht;

static int Red[MAXCOLORS], Green[MAXCOLORS], Blue[MAXCOLORS];



static int
colorstobpp(unsigned int const colors) {

    int bpp;

    if (colors <= 2)
        bpp = 1;
    else if (colors <= 256)
        bpp = 8;
    else
        bpp = 24;

    return bpp;
}



static int
GetPixel(int const x,
         int const y) {

    int color;

    color = ppm_lookupcolor(cht, &pixels[y][x]);

    return color;
}



static void
leaf_writeimg(unsigned int const width,
              unsigned int const height,
              unsigned int const depth,
              unsigned int const ncolors,
              pixval       const maxval) {

    /* OK, this routine is not wicked efficient, but it is simple to follow
       and it works.
    */

    /* NOTE: byte order in ileaf img file fmt is big-endian, always! */

    /* magic */
    fputc(0x89, stdout);
    fputc(0x4f, stdout);
    fputc(0x50, stdout);
    fputc(0x53, stdout);

    /* version 4 */
    fputc(0x00, stdout);
    fputc(0x04, stdout);

    /* h resolution: pixels/inch: say 75=screen resolution */
    fputc(0x00, stdout);
    fputc(75, stdout);

    /* v resolution: pixels/inch: say 75=screen resolution */
    fputc(0x00, stdout);
    fputc(75, stdout);

    /* unique id, could be anything */
    fputc(0x01, stdout);
    fputc(0x02, stdout);
    fputc(0x03, stdout);
    fputc(0x04, stdout);

    /* x offset, always zero */
    fputc(0x00, stdout);
    fputc(0x00, stdout);

    /* y offset, always zero */
    fputc(0x00, stdout);
    fputc(0x00, stdout);

    /* dimensions 64k x 64k max */
    fputc((unsigned char)((width >> 8) & 0x00ff), stdout);
    fputc((unsigned char)(width  & 0x00ff), stdout);
    fputc((unsigned char)((height >> 8) & 0x00ff), stdout);
    fputc((unsigned char)(height  & 0x00ff), stdout);

    /* depth */
    fputc(0x00, stdout);
    fputc((unsigned char)depth, stdout);

    /* compressed, 0=uncompressed, 1=compressed */
    fputc(0x00, stdout);

    /* format, mono/gray = 0x20000000, RGB=0x29000000 */
    if (depth == 1)
        fputc(0x20, stdout);
    else
        fputc(0x29, stdout);

    fputc(0x00, stdout);
    fputc(0x00, stdout);
    fputc(0x00, stdout);

    /* colormap size */
    if (depth == 8) {
        unsigned int i;
        unsigned int row;

        fputc((unsigned char)((ncolors >> 8) & 0x00ff), stdout);
        fputc((unsigned char)(ncolors  & 0x00ff), stdout);
        for (i = 0; i < 256; ++i)
            fputc((unsigned char) Red[i]*255/maxval, stdout);
        for (i=0; i < 256; ++i)
            fputc((unsigned char) Green[i]*255/maxval, stdout);
        for (i = 0; i < 256; ++i)
            fputc((unsigned char) Blue[i]*255/maxval, stdout);

        for (row=0; row < height; ++row) {
            unsigned int col;
            for (col = 0; col < width; ++col)
                fputc(GetPixel(col, row), stdout);
            if ((width % 2) != 0)
                fputc(0x00, stdout); /* pad to 2-bytes */
        }
    } else if (depth == 1) {
        /* mono image */
        /* no colormap */

        unsigned int row;

        fputc(0x00, stdout);
        fputc(0x00, stdout);

        for (row = 0; row < height; ++row) {
            unsigned char bits;
            unsigned int col;
            bits = 0;
            for (col = 0; col < width; ++col) {
                if (GetPixel(col,row))
                    bits |= (unsigned char) (0x0080 >> (col % 8));
                if (((col + 1) % 8) == 0)  {
                    fputc(bits, stdout);
                    bits = 0;
                }
            }
            if ((width % 8) != 0)
                fputc(bits, stdout);
            if ((width % 16) && (width % 16) <= 8)
                fputc(0x00, stdout);  /* 16 bit pad */
        }
    } else {
        /* no colormap, direct or true color (24 bit) image */

        unsigned int row;

        fputc(0x00, stdout);
        fputc(0x00, stdout);

        for (row = 0; row < height; ++row) {
            unsigned int col;
            for (col = 0; col < width; ++col)
                fputc(pixels[row][col].r * 255 / maxval, stdout);
            if (width % 2 != 0)
                fputc(0x00, stdout); /* pad to 2-bytes */
            for (col = 0; col < width; ++col)
                fputc(pixels[row][col].g * 255 / maxval, stdout);
            if (width % 2 != 0)
                fputc(0x00, stdout); /* pad to 2-bytes */
            for (col = 0; col < width; ++col)
                fputc(pixels[row][col].b * 255 / maxval, stdout);
            if (width % 2 != 0)
                fputc(0x00, stdout); /* pad to 2-bytes */
        }
    }
}



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

    FILE * ifP;
    int argn;
    int rows, cols;
    unsigned int BitsPerPixel;
    int colors;
    pixval maxval;
    colorhist_vector chv;
    const char * const usage = "[ppmfile]";

    pm_proginit(&argc, argv);

    argn = 1;

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

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

    pixels = ppm_readppm(ifP, &cols, &rows, &maxval);

    pm_close(ifP);

    /* Figure out the colormap. */
    pm_message("Computing colormap...");
    chv = ppm_computecolorhist(pixels, cols, rows, MAXCOLORS, &colors);
    if (chv) {
        unsigned int i;

        pm_message("... Done.  %u colors found.", colors);

        for (i = 0; i < colors; ++i) {
            Red[i]   = (int) PPM_GETR( chv[i].color);
            Green[i] = (int) PPM_GETG( chv[i].color);
            Blue[i]  = (int) PPM_GETB( chv[i].color);
        }
        BitsPerPixel = colorstobpp(colors);

        /* And make a hash table for fast lookup. */
        cht = ppm_colorhisttocolorhash(chv, colors);
        ppm_freecolorhist(chv);
    } else {
        BitsPerPixel = 24;
        pm_message("  ... Done.  24-bit true color %u color image.", colors);
    }

    leaf_writeimg(cols, rows, BitsPerPixel, colors, maxval);

    return 0;
}