about summary refs log tree commit diff
path: root/converter/other/pnmtorast.c
blob: 7d1ae05aa06f3c4a0a40abbd1a437568a8d593e9 (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
/* pnmtorast.c - read a portable anymap and produce a Sun rasterfile
**
** Copyright (C) 1989, 1991 by Jef Poskanzer.
**
** 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 "pnm.h"
#include "rast.h"
#include "mallocvar.h"

#define MAXCOLORS 256
static colormap_t* make_pr_colormap ARGS(( colorhist_vector chv, int colors ));
static colormap_t* make_gray_pr_colormap ARGS(( void ));
static colormap_t* alloc_pr_colormap ARGS(( void ));

int
main( argc, argv )
    int argc;
    char* argv[];
{
    FILE* ifp;
    xel** xels;
    xel* xelrow;
    xel p;
    register xel* xP;
    colorhist_vector chv;
    colorhash_table cht;
    colormap_t* pr_colormapP;
    int argn, pr_type, rows, cols, format, i;
    int depth, colors, linesize, row;
    register int col, bitcount;
    xelval maxval;
    struct pixrect* pr;
    unsigned char* data;
    register unsigned char* byteP;
    const char* const usage = "[-standard|-rle] [pnmfile]";

    pnm_init( &argc, argv );

    argn = 1;
    pr_type = RT_BYTE_ENCODED;

    while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
    {
        if ( pm_keymatch( argv[argn], "-standard", 2 ) )
            pr_type = RT_STANDARD;
        else if ( pm_keymatch( argv[argn], "-rle", 2 ) )
            pr_type = RT_BYTE_ENCODED;
        else
            pm_usage( usage );
        ++argn;
    }

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

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

    xels = pnm_readpnm( ifp, &cols, &rows, &maxval, &format );

    pm_close( ifp );

    /* Figure out the proper depth and colormap. */
    switch ( PNM_FORMAT_TYPE(format) )
    {
    case PPM_TYPE:
        pm_message( "computing colormap..." );
        chv = ppm_computecolorhist( xels, cols, rows, MAXCOLORS, &colors );
        if ( chv == (colorhist_vector) 0 )
        {
            pm_message(
                "Too many colors - proceeding to write a 24-bit non-mapped" );
            pm_message(
                "rasterfile.  If you want 8 bits, try doing a 'pnmquant %d'.",
                MAXCOLORS );
            depth = 24;
            pr_type = RT_STANDARD;
            pr_colormapP = (colormap_t*) 0;
        }
        else
        {
            pm_message( "%d colors found", colors );

            if ( maxval != 255 )
                for ( i = 0; i < colors; ++i )
                    PPM_DEPTH( chv[i].color, chv[i].color, maxval, 255 );

            /* Force white to slot 0 and black to slot 1, if possible. */
            PPM_ASSIGN( p, 255, 255, 255 );
            ppm_addtocolorhist( chv, &colors, MAXCOLORS, &p, 0, 0 );
            PPM_ASSIGN( p, 0, 0, 0 );
            ppm_addtocolorhist( chv, &colors, MAXCOLORS, &p, 0, 1 );

            if ( colors == 2 )
            {
                /* Monochrome. */
                depth = 1;
                pr_colormapP = (colormap_t*) 0;
            }
            else
            {
                /* Turn the ppm colormap into the appropriate Sun colormap. */
                depth = 8;
                pr_colormapP = make_pr_colormap( chv, colors );
            }
            cht = ppm_colorhisttocolorhash( chv, colors );
            ppm_freecolorhist( chv );
        }

        break;

    case PGM_TYPE:
        depth = 8;
        pr_colormapP = make_gray_pr_colormap( );
        break;

    default:
        depth = 1;
        pr_colormapP = (colormap_t*) 0;
        break;
    }

    if ( maxval > 255 && depth != 1 )
        pm_message(
            "maxval is not 255 - automatically rescaling colors" );
    
    /* Allocate space for the Sun-format image. */
    if ( (pr = mem_create(cols, rows, depth)) == (struct pixrect*) 0 )
        pm_error( "unable to create new pixrect" );
    data = ( (struct mpr_data*) pr->pr_data )->md_image;
    linesize = ( (struct mpr_data*) pr->pr_data )->md_linebytes;

    /* And compute the Sun image.  The variables at this point are:
    **   cht is null or not
    **   depth is 1, 8, or 24
    */
    for ( row = 0; row < rows; ++row )
    {
        xelrow = xels[row];
        byteP = data;
        switch ( depth )
        {
        case 1:
            *byteP = 0;
            bitcount = 7;
            for ( col = 0, xP = xelrow; col < cols; ++col, ++xP )
            {
                register int color;

                switch ( PNM_FORMAT_TYPE(format) )
                {
                case PPM_TYPE:
                    if ( maxval != 255 )
                        PPM_DEPTH( *xP, *xP, maxval, 255 );
                    color = ppm_lookupcolor( cht, xP );
                    if ( color == -1 )
                        pm_error(
                            "color not found?!?  row=%d col=%d  r=%d g=%d b=%d",
                            row, col, PPM_GETR(*xP), PPM_GETG(*xP),
                            PPM_GETB(*xP) );
                    if ( color )
                        *byteP |= 1 << bitcount;
                    break;

                default:
                    color = PNM_GET1( *xP );
                    if ( ! color )
                        *byteP |= 1 << bitcount;
                    break;
                }
                --bitcount;
                if ( bitcount < 0 )
                {
                    ++byteP;
                    *byteP = 0;
                    bitcount = 7;
                }
            }
            break;

        case 8:
            for ( col = 0, xP = xelrow; col < cols; ++col, ++xP )
            {
                register int color;

                switch ( PNM_FORMAT_TYPE(format) )
                {
                case PPM_TYPE:
                    if ( maxval != 255 )
                        PPM_DEPTH( *xP, *xP, maxval, 255 );
                    color = ppm_lookupcolor( cht, xP );
                    if ( color == -1 )
                        pm_error(
                            "color not found?!?  row=%d col=%d  r=%d g=%d b=%d",
                            row, col, PPM_GETR(*xP), PPM_GETG(*xP),
                            PPM_GETB(*xP) );
                    break;

                case PGM_TYPE:
                    color = PNM_GET1( *xP );
                    if ( maxval != 255 )
                        color = color * 255 / maxval;
                    break;

                default:
                    color = PNM_GET1( *xP );
                }
                *byteP++ = color;
            }
            break;

        case 24:
            /* If depth is 24, we do NOT have a valid cht. */
            for ( col = 0, xP = xelrow; col < cols; ++col, ++xP )
            {
                if ( maxval != 255 )
                    PPM_DEPTH( *xP, *xP, maxval, 255 );
                *byteP++ = PPM_GETB( *xP );
                *byteP++ = PPM_GETG( *xP );
                *byteP++ = PPM_GETR( *xP );
            }
            break;

        default:
            pm_error( "can't happen" );
        }
        data += linesize;
    }
    pnm_freearray( xels, rows );

    /* Finally, write the sucker out. */
    if ( pr_dump( pr, stdout, pr_colormapP, pr_type, 0 ) == PIX_ERR )
        pm_error( "error writing rasterfile" );

    exit( 0 );
}

static colormap_t*
make_pr_colormap( chv, colors )
    colorhist_vector chv;
    int colors;
{
    colormap_t* pr_colormapP;
    int i;

    pr_colormapP = alloc_pr_colormap( );

    for ( i = 0; i < colors; ++i )
    {
        pr_colormapP->map[0][i] = PPM_GETR( chv[i].color );
        pr_colormapP->map[1][i] = PPM_GETG( chv[i].color );
        pr_colormapP->map[2][i] = PPM_GETB( chv[i].color );
    }
    for ( ; i < MAXCOLORS; ++i )
        pr_colormapP->map[0][i] = pr_colormapP->map[1][i] =
            pr_colormapP->map[2][i] = 0;

    return pr_colormapP;
}

static colormap_t*
make_gray_pr_colormap( )
{
    colormap_t* pr_colormapP;
    int i;

    pr_colormapP = alloc_pr_colormap( );

    for ( i = 0; i < MAXCOLORS; ++i )
    {
        pr_colormapP->map[0][i] = i;
        pr_colormapP->map[1][i] = i;
        pr_colormapP->map[2][i] = i;
    }

    return pr_colormapP;
}

static colormap_t*
alloc_pr_colormap( )
{
    colormap_t* pr_colormapP;

    MALLOCVAR(pr_colormapP);
    if ( pr_colormapP == NULL )
        pm_error( "out of memory" );
    pr_colormapP->type = RMT_EQUAL_RGB;
    pr_colormapP->length = MAXCOLORS;
    MALLOCARRAY(pr_colormapP->map[0], MAXCOLORS);
    MALLOCARRAY(pr_colormapP->map[1], MAXCOLORS);
    MALLOCARRAY(pr_colormapP->map[2], MAXCOLORS);
    if ( pr_colormapP->map[0] == NULL || 
         pr_colormapP->map[1] == NULL ||
         pr_colormapP->map[2] == NULL )
        pm_error( "out of memory" );

    return pr_colormapP;
}