libppm2.c
4.99 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* libppm2.c - ppm utility library part 2
**
** 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"
static void putus ARGS((unsigned short n, FILE* file));
static void ppm_writeppmrowplain ARGS((FILE* file, pixel* pixelrow, int cols, pixval maxval));
#ifdef PBMPLUS_RAWBITS
static void ppm_writeppmrowraw ARGS((FILE* file, pixel* pixelrow, int cols, pixval maxval));
#endif /* PBMPLUS_RAWBITS */
#if __STDC__
void
ppm_writeppminit( FILE* file, int cols, int rows, pixval maxval, int forceplain )
#else /*__STDC__*/
void
ppm_writeppminit( file, cols, rows, maxval, forceplain )
FILE* file;
int cols, rows;
pixval maxval;
int forceplain;
#endif /*__STDC__*/
{
#ifdef PBMPLUS_RAWBITS
if ( maxval <= 255 && ! forceplain ) {
fprintf(
file, "%c%c\n%d %d\n%d\n", PPM_MAGIC1, RPPM_MAGIC2,
cols, rows, maxval );
#ifdef VMS
set_outfile_binary();
#endif
}
else
fprintf(
file, "%c%c\n%d %d\n%d\n", PPM_MAGIC1, PPM_MAGIC2,
cols, rows, maxval );
#else /*PBMPLUS_RAWBITS*/
fprintf(
file, "%c%c\n%d %d\n%d\n", PPM_MAGIC1, PPM_MAGIC2,
cols, rows, maxval );
#endif /*PBMPLUS_RAWBITS*/
}
static void
#ifdef sgi
putus( unsigned short n, FILE* file )
#else
putus( n, file )
unsigned short n;
FILE* file;
#endif
{
if ( n >= 10 )
putus( n / 10, file );
(void) putc( n % 10 + '0', file );
}
#ifdef PBMPLUS_RAWBITS
static void
#ifdef sgi
ppm_writeppmrowraw(
FILE* file,
pixel* pixelrow,
int cols,
pixval maxval )
#else
ppm_writeppmrowraw( file, pixelrow, cols, maxval )
FILE* file;
pixel* pixelrow;
int cols;
pixval maxval;
#endif
{
register int col;
register pixel* pP;
register pixval val;
for ( col = 0, pP = pixelrow; col < cols; ++col, ++pP )
{
val = PPM_GETR( *pP );
#ifdef DEBUG
if ( val > maxval )
pm_error( "r value out of bounds (%u > %u)", val, maxval );
#endif /*DEBUG*/
(void) putc( val, file );
val = PPM_GETG( *pP );
#ifdef DEBUG
if ( val > maxval )
pm_error( "g value out of bounds (%u > %u)", val, maxval );
#endif /*DEBUG*/
(void) putc( val, file );
val = PPM_GETB( *pP );
#ifdef DEBUG
if ( val > maxval )
pm_error( "b value out of bounds (%u > %u)", val, maxval );
#endif /*DEBUG*/
(void) putc( val, file );
}
}
#endif /*PBMPLUS_RAWBITS*/
static void
#ifdef sgi
ppm_writeppmrowplain(
FILE* file,
pixel* pixelrow,
int cols,
pixval maxval )
#else
ppm_writeppmrowplain( file, pixelrow, cols, maxval )
FILE* file;
pixel* pixelrow;
int cols;
pixval maxval;
#endif
{
register int col, charcount;
register pixel* pP;
register pixval val;
charcount = 0;
for ( col = 0, pP = pixelrow; col < cols; ++col, ++pP )
{
if ( charcount >= 65 )
{
(void) putc( '\n', file );
charcount = 0;
}
else if ( charcount > 0 )
{
(void) putc( ' ', file );
(void) putc( ' ', file );
charcount += 2;
}
val = PPM_GETR( *pP );
#ifdef DEBUG
if ( val > maxval )
pm_error( "r value out of bounds (%u > %u)", val, maxval );
#endif /*DEBUG*/
putus( val, file );
(void) putc( ' ', file );
val = PPM_GETG( *pP );
#ifdef DEBUG
if ( val > maxval )
pm_error( "g value out of bounds (%u > %u)", val, maxval );
#endif /*DEBUG*/
putus( val, file );
(void) putc( ' ', file );
val = PPM_GETB( *pP );
#ifdef DEBUG
if ( val > maxval )
pm_error( "b value out of bounds (%u > %u)", val, maxval );
#endif /*DEBUG*/
putus( val, file );
charcount += 11;
}
if ( charcount > 0 )
(void) putc( '\n', file );
}
#if __STDC__
void
ppm_writeppmrow( FILE* file, pixel* pixelrow, int cols, pixval maxval, int forceplain )
#else /*__STDC__*/
void
ppm_writeppmrow( file, pixelrow, cols, maxval, forceplain )
FILE* file;
pixel* pixelrow;
int cols;
pixval maxval;
int forceplain;
#endif /*__STDC__*/
{
#ifdef PBMPLUS_RAWBITS
if ( maxval <= 255 && ! forceplain )
ppm_writeppmrowraw( file, pixelrow, cols, maxval );
else
ppm_writeppmrowplain( file, pixelrow, cols, maxval );
#else /*PBMPLUS_RAWBITS*/
ppm_writeppmrowplain( file, pixelrow, cols, maxval );
#endif /*PBMPLUS_RAWBITS*/
}
#if __STDC__
void
ppm_writeppm( FILE* file, pixel** pixels, int cols, int rows, pixval maxval, int forceplain )
#else /*__STDC__*/
void
ppm_writeppm( file, pixels, cols, rows, maxval, forceplain )
FILE* file;
pixel** pixels;
int cols, rows;
pixval maxval;
int forceplain;
#endif /*__STDC__*/
{
int row;
ppm_writeppminit( file, cols, rows, maxval, forceplain );
for ( row = 0; row < rows; ++row )
ppm_writeppmrow( file, pixels[row], cols, maxval, forceplain );
}