libppm1.c
4.36 KB
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
/* libppm1.c - ppm utility library part 1
**
** Copyright (C) 1989 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 "ppm.h"
#include "libppm.h"
#include "pgm.h"
#include "libpgm.h"
#include "pbm.h"
#include "libpbm.h"
void
ppm_init( argcP, argv )
int* argcP;
char* argv[];
{
pgm_init( argcP, argv );
}
void
ppm_readppminitrest( file, colsP, rowsP, maxvalP )
FILE* file;
int* colsP;
int* rowsP;
pixval* maxvalP;
{
int maxval;
/* Read size. */
*colsP = pbm_getint( file );
*rowsP = pbm_getint( file );
/* Read maxval. */
maxval = pbm_getint( file );
if ( maxval > PPM_MAXMAXVAL )
pm_error(
"maxval is too large - try reconfiguring with PGM_BIGGRAYS\n or without PPM_PACKCOLORS" );
*maxvalP = maxval;
}
pixval ppm_pbmmaxval = 1;
void
ppm_readppminit( file, colsP, rowsP, maxvalP, formatP )
FILE* file;
int* colsP;
int* rowsP;
int* formatP;
pixval* maxvalP;
{
/* Check magic number. */
*formatP = pbm_readmagicnumber( file );
switch ( PPM_FORMAT_TYPE(*formatP) )
{
case PPM_TYPE:
ppm_readppminitrest( file, colsP, rowsP, maxvalP );
break;
case PGM_TYPE:
pgm_readpgminitrest( file, colsP, rowsP, maxvalP );
break;
case PBM_TYPE:
pbm_readpbminitrest( file, colsP, rowsP );
*maxvalP = ppm_pbmmaxval;
break;
default:
pm_error( "bad magic number - not a ppm, pgm, or pbm file" );
}
}
#if __STDC__
void
ppm_readppmrow( FILE* file, pixel* pixelrow, int cols, pixval maxval, int format )
#else /*__STDC__*/
void
ppm_readppmrow( file, pixelrow, cols, maxval, format )
FILE* file;
pixel* pixelrow;
int cols, format;
pixval maxval;
#endif /*__STDC__*/
{
register int col;
register pixel* pP;
register pixval r, g, b;
gray* grayrow;
register gray* gP;
bit* bitrow;
register bit* bP;
switch ( format )
{
case PPM_FORMAT:
for ( col = 0, pP = pixelrow; col < cols; ++col, ++pP )
{
r = pbm_getint( file );
#ifdef DEBUG
if ( r > maxval )
pm_error( "r value out of bounds (%u > %u)", r, maxval );
#endif /*DEBUG*/
g = pbm_getint( file );
#ifdef DEBUG
if ( g > maxval )
pm_error( "g value out of bounds (%u > %u)", g, maxval );
#endif /*DEBUG*/
b = pbm_getint( file );
#ifdef DEBUG
if ( b > maxval )
pm_error( "b value out of bounds (%u > %u)", b, maxval );
#endif /*DEBUG*/
PPM_ASSIGN( *pP, r, g, b );
}
break;
case RPPM_FORMAT:
for ( col = 0, pP = pixelrow; col < cols; ++col, ++pP )
{
r = pbm_getrawbyte( file );
#ifdef DEBUG
if ( r > maxval )
pm_error( "r value out of bounds (%u > %u)", r, maxval );
#endif /*DEBUG*/
g = pbm_getrawbyte( file );
#ifdef DEBUG
if ( g > maxval )
pm_error( "g value out of bounds (%u > %u)", g, maxval );
#endif /*DEBUG*/
b = pbm_getrawbyte( file );
#ifdef DEBUG
if ( b > maxval )
pm_error( "b value out of bounds (%u > %u)", b, maxval );
#endif /*DEBUG*/
PPM_ASSIGN( *pP, r, g, b );
}
break;
case PGM_FORMAT:
case RPGM_FORMAT:
grayrow = pgm_allocrow( cols );
pgm_readpgmrow( file, grayrow, cols, maxval, format );
for ( col = 0, gP = grayrow, pP = pixelrow; col < cols; ++col, ++gP, ++pP )
{
r = *gP;
PPM_ASSIGN( *pP, r, r, r );
}
pgm_freerow( grayrow );
break;
case PBM_FORMAT:
case RPBM_FORMAT:
bitrow = pbm_allocrow( cols );
pbm_readpbmrow( file, bitrow, cols, format );
for ( col = 0, bP = bitrow, pP = pixelrow; col < cols; ++col, ++bP, ++pP )
{
r = ( *bP == PBM_WHITE ) ? maxval : 0;
PPM_ASSIGN( *pP, r, r, r );
}
pbm_freerow( bitrow );
break;
default:
pm_error( "can't happen" );
}
}
pixel**
ppm_readppm( file, colsP, rowsP, maxvalP )
FILE* file;
int* colsP;
int* rowsP;
pixval* maxvalP;
{
pixel** pixels;
int row;
int format;
ppm_readppminit( file, colsP, rowsP, maxvalP, &format );
pixels = ppm_allocarray( *colsP, *rowsP );
for ( row = 0; row < *rowsP; ++row )
ppm_readppmrow( file, pixels[row], *colsP, *maxvalP, format );
return pixels;
}