about summary refs log tree commit diff
path: root/converter/ppm/rawtoppm.c
blob: 8d6c3b2ca6929f1368891afe10356d5d12f61d0c (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
/* rawtoppm.c - convert raw RGB bytes into a portable pixmap
**
** Copyright (C) 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 <math.h>
#include "ppm.h"

static void dorowskip ARGS(( FILE* ifp, int rowskip ));

int
main( argc, argv )
    int argc;
    char* argv[];
    {
    FILE* ifp;
    pixel* pixrow;
    register pixel* pP;
    int argn, headerskip, rowskip, rows, cols, row, i;
    register int col;
    int order;
#define ORD_RGB 1
#define ORD_RBG 2
#define ORD_GRB 3
#define ORD_GBR 4
#define ORD_BRG 5
#define ORD_BGR 6
int interleave;
#define INT_PIX 1
#define INT_ROW 2
    int val1, val2, val3;
    gray* grow1;
    gray* grow2;
    gray* grow3;
    register gray* g1P;
    register gray* g2P;
    register gray* g3P;
    const char* const usage = "[-headerskip N] [-rowskip N] [-rgb|-rbg|-grb|-gbr|-brg|-bgr] [-interpixel|-interrow] <width> <height> [rawfile]";


    ppm_init( &argc, argv );

    argn = 1;
    headerskip = 0;
    rowskip = 0;
    order = ORD_RGB;
    interleave = INT_PIX;

    while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
	{
	if ( pm_keymatch( argv[argn], "-headerskip", 2 ) )
	    {
	    ++argn;
	    if ( argn >= argc )
		pm_usage( usage );
	    headerskip = atoi( argv[argn] );
	    }
	else if ( pm_keymatch( argv[argn], "-rowskip", 3 ) )
	    {
	    ++argn;
	    if ( argn >= argc )
		pm_usage( usage );
	    rowskip = atoi( argv[argn] );
	    }
	else if ( pm_keymatch( argv[argn], "-rgb", 3 ) )
	    order = ORD_RGB;
	else if ( pm_keymatch( argv[argn], "-rbg", 3 ) )
	    order = ORD_RBG;
	else if ( pm_keymatch( argv[argn], "-grb", 3 ) )
	    order = ORD_GRB;
	else if ( pm_keymatch( argv[argn], "-gbr", 3 ) )
	    order = ORD_GBR;
	else if ( pm_keymatch( argv[argn], "-brg", 3 ) )
	    order = ORD_BRG;
	else if ( pm_keymatch( argv[argn], "-bgr", 3 ) )
	    order = ORD_BGR;
	else if ( pm_keymatch( argv[argn], "-interpixel", 7 ) )
	    interleave = INT_PIX;
	else if ( pm_keymatch( argv[argn], "-interrow", 7 ) )
	    interleave = INT_ROW;
	else
	    pm_usage( usage );
	++argn;
	}

    if ( argn + 2 > argc )
	pm_usage( usage );

    cols = atoi( argv[argn++] );
    rows = atoi( argv[argn++] );
    if ( cols <= 0 || rows <= 0 )
	pm_usage( usage );

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

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

    ppm_writeppminit( stdout, cols, rows, (pixval) 255, 0 );
    pixrow = ppm_allocrow( cols );

    if ( interleave == INT_ROW )
	{
	grow1 = pgm_allocrow( cols );
	grow2 = pgm_allocrow( cols );
	grow3 = pgm_allocrow( cols );
	}

    for ( i = 0; i < headerskip; ++i )
	{
	val1 = getc( ifp );
	if ( val1 == EOF )
	    pm_error( "EOF / read error" );
	}

    for ( row = 0; row < rows; ++row)
	{
	switch ( interleave )
	    {
	    case INT_PIX:
	    for ( col = 0, pP = pixrow; col < cols; ++col, ++pP )
		{
		val1 = getc( ifp );
		if ( val1 == EOF )
		    pm_error( "EOF / read error" );
		val2 = getc( ifp );
		if ( val2 == EOF )
		    pm_error( "EOF / read error" );
		val3 = getc( ifp );
		if ( val3 == EOF )
		    pm_error( "EOF / read error" );
		switch ( order )
		    {
		    case ORD_RGB:
		    PPM_ASSIGN( *pP, val1, val2, val3 );
		    break;
		    case ORD_RBG:
		    PPM_ASSIGN( *pP, val1, val3, val2 );
		    break;
		    case ORD_GRB:
		    PPM_ASSIGN( *pP, val2, val1, val3 );
		    break;
		    case ORD_GBR:
		    PPM_ASSIGN( *pP, val3, val1, val2 );
		    break;
		    case ORD_BRG:
		    PPM_ASSIGN( *pP, val2, val3, val1 );
		    break;
		    case ORD_BGR:
		    PPM_ASSIGN( *pP, val3, val2, val1 );
		    break;
		    }
		}
	    dorowskip( ifp, rowskip );
	    break;

	    case INT_ROW:
	    for ( col = 0, g1P = grow1; col < cols; ++col, ++g1P )
		{
		val1 = getc( ifp );
		if ( val1 == EOF )
		    pm_error( "EOF / read error" );
		*g1P = val1;
		}
	    dorowskip( ifp, rowskip );
	    for ( col = 0, g2P = grow2; col < cols; ++col, ++g2P )
		{
		val2 = getc( ifp );
		if ( val2 == EOF )
		    pm_error( "EOF / read error" );
		*g2P = val2;
		}
	    dorowskip( ifp, rowskip );
	    for ( col = 0, g3P = grow3; col < cols; ++col, ++g3P )
		{
		val3 = getc( ifp );
		if ( val3 == EOF )
		    pm_error( "EOF / read error" );
		*g3P = val3;
		}
	    dorowskip( ifp, rowskip );
	    for ( col = 0, pP = pixrow, g1P = grow1, g2P = grow2, g3P = grow3;
		  col < cols; ++col, ++pP, ++g1P, ++g2P, ++g3P )
		{
		switch ( order )
		    {
		    case ORD_RGB:
		    PPM_ASSIGN( *pP, *g1P, *g2P, *g3P );
		    break;
		    case ORD_RBG:
		    PPM_ASSIGN( *pP, *g1P, *g3P, *g2P );
		    break;
		    case ORD_GRB:
		    PPM_ASSIGN( *pP, *g2P, *g1P, *g3P );
		    break;
		    case ORD_GBR:
		    PPM_ASSIGN( *pP, *g3P, *g1P, *g2P );
		    break;
		    case ORD_BRG:
		    PPM_ASSIGN( *pP, *g2P, *g3P, *g1P );
		    break;
		    case ORD_BGR:
		    PPM_ASSIGN( *pP, *g3P, *g2P, *g1P );
		    break;
		    }
		}
	    break;
	    }
	ppm_writeppmrow( stdout, pixrow, cols, (pixval) 255, 0 );
	}

    pm_close( ifp );
    pm_close( stdout );

    exit( 0 );
    }

static void
dorowskip( ifp, rowskip )
    FILE* ifp;
    int rowskip;
    {
    int i, val;

    for ( i = 0; i < rowskip; ++i )
	{
	val = getc( ifp );
	if ( val == EOF )
	    pm_error( "EOF / read error" );
	}
    }