8
8
* This file is part of GPUJPEG.
9
9
*/
10
10
/*
11
- * Copyright (c) 2013-2023 , CESNET z.s.p.o.
11
+ * Copyright (c) 2013-2025 , CESNET
12
12
*
13
13
* All rights reserved.
14
14
*
@@ -64,7 +64,7 @@ static bool parse_pam(FILE *file, struct pam_metadata *info) {
64
64
} else if (strcmp (key , "HEIGHT" ) == 0 ) {
65
65
info -> height = atoi (val );
66
66
} else if (strcmp (key , "DEPTH" ) == 0 ) {
67
- info -> depth = atoi (val );
67
+ info -> ch_count = atoi (val );
68
68
} else if (strcmp (key , "MAXVAL" ) == 0 ) {
69
69
info -> maxval = atoi (val );
70
70
} else if (strcmp (key , "TUPLTYPE" ) == 0 ) {
@@ -92,15 +92,15 @@ static bool parse_pnm(FILE *file, char pnm_id, struct pam_metadata *info) {
92
92
fprintf (stderr , "Plain (ASCII) PNM are not supported, input is P%c\n" , pnm_id );
93
93
return false;
94
94
case '4' :
95
- info -> depth = 1 ;
95
+ info -> ch_count = 1 ;
96
96
info -> maxval = 1 ;
97
97
info -> bitmap_pbm = true;
98
98
break ;
99
99
case '5' :
100
- info -> depth = 1 ;
100
+ info -> ch_count = 1 ;
101
101
break ;
102
102
case '6' :
103
- info -> depth = 3 ;
103
+ info -> ch_count = 3 ;
104
104
break ;
105
105
default :
106
106
fprintf (stderr , "Wrong PNM type P%c\n" , pnm_id );
@@ -166,8 +166,8 @@ bool pam_read(const char *filename, struct pam_metadata *info, unsigned char **d
166
166
fprintf (stderr , "Unspecified/incorrect size %dx%d!\n" , info -> width , info -> height );
167
167
parse_rc = false;
168
168
}
169
- if (info -> depth <= 0 ) {
170
- fprintf (stderr , "Unspecified/incorrect depth %d!\n" , info -> depth );
169
+ if (info -> ch_count <= 0 ) {
170
+ fprintf (stderr , "Unspecified/incorrect channel count %d!\n" , info -> ch_count );
171
171
parse_rc = false;
172
172
}
173
173
if (info -> maxval <= 0 || info -> maxval > 65535 ) {
@@ -178,15 +178,15 @@ bool pam_read(const char *filename, struct pam_metadata *info, unsigned char **d
178
178
fclose (file );
179
179
return parse_rc ;
180
180
}
181
- size_t datalen = (size_t ) info -> depth * info -> width * info -> height ;
181
+ size_t datalen = (size_t ) info -> ch_count * info -> width * info -> height ;
182
182
if (info -> maxval == 1 && info -> bitmap_pbm ) {
183
183
datalen = (info -> width + 7 ) / 8 * info -> height ;
184
184
} else if (info -> maxval > 255 ) {
185
185
datalen *= 2 ;
186
186
}
187
187
* data = (unsigned char * ) allocator (datalen );
188
188
if (!* data ) {
189
- fprintf (stderr , "Unspecified depth header field !\n" );
189
+ fprintf (stderr , "Failed to allocate data !\n" );
190
190
fclose (file );
191
191
return false;
192
192
}
@@ -201,32 +201,32 @@ bool pam_read(const char *filename, struct pam_metadata *info, unsigned char **d
201
201
return true;
202
202
}
203
203
204
- bool pam_write (const char * filename , unsigned int width , unsigned int height , int depth , int maxval , const unsigned char * data , bool pnm ) {
204
+ bool pam_write (const char * filename , unsigned int width , unsigned int height , int ch_count , int maxval , const unsigned char * data , bool pnm ) {
205
205
errno = 0 ;
206
206
FILE * file = fopen (filename , "wb" );
207
207
if (!file ) {
208
208
fprintf (stderr , "Failed to open %s for writing: %s\n" , filename , strerror (errno ));
209
209
return false;
210
210
}
211
211
if (pnm ) {
212
- if (depth != 1 && depth != 3 ) {
212
+ if (ch_count != 1 && ch_count != 3 ) {
213
213
fprintf (stderr , "Only 1 or 3 channels supported for PNM!\n" );
214
214
fclose (file );
215
215
return false;
216
216
}
217
217
fprintf (file , "P%d\n"
218
218
"%u %u\n"
219
219
"%d\n" ,
220
- depth == 1 ? 5 : 6 ,
220
+ ch_count == 1 ? 5 : 6 ,
221
221
width , height , maxval );
222
222
} else {
223
223
const char * tuple_type = "INVALID" ;
224
- switch (depth ) {
224
+ switch (ch_count ) {
225
225
case 4 : tuple_type = "RGB_ALPHA" ; break ;
226
226
case 3 : tuple_type = "RGB" ; break ;
227
227
case 2 : tuple_type = "GRAYSCALE_ALPHA" ; break ;
228
228
case 1 : tuple_type = "GRAYSCALE" ; break ;
229
- default : fprintf (stderr , "Wrong depth : %d\n" , depth );
229
+ default : fprintf (stderr , "Wrong channel count : %d\n" , ch_count );
230
230
}
231
231
fprintf (file , "P7\n"
232
232
"WIDTH %u\n"
@@ -235,9 +235,9 @@ bool pam_write(const char *filename, unsigned int width, unsigned int height, in
235
235
"MAXVAL %d\n"
236
236
"TUPLTYPE %s\n"
237
237
"ENDHDR\n" ,
238
- width , height , depth , maxval , tuple_type );
238
+ width , height , ch_count , maxval , tuple_type );
239
239
}
240
- size_t len = (size_t ) width * height * depth * (maxval <= 255 ? 1 : 2 );
240
+ size_t len = (size_t ) width * height * ch_count * (maxval <= 255 ? 1 : 2 );
241
241
errno = 0 ;
242
242
size_t bytes_written = fwrite ((const char * ) data , 1 , len , file );
243
243
if (bytes_written != len ) {
0 commit comments