Coverage Report

Created: 2025-07-11 07:02

/src/libjpeg-turbo.main/src/wrppm.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * wrppm.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1996, Thomas G. Lane.
6
 * Modified 2009 by Guido Vollbeding.
7
 * libjpeg-turbo Modifications:
8
 * Copyright (C) 2017, 2019-2020, 2022, 2024, D. R. Commander.
9
 * For conditions of distribution and use, see the accompanying README.ijg
10
 * file.
11
 *
12
 * This file contains routines to write output images in PPM/PGM format.
13
 * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
14
 * The PBMPLUS library is NOT required to compile this software
15
 * (but it is highly useful as a set of PPM image manipulation programs).
16
 *
17
 * These routines may need modification for non-Unix environments or
18
 * specialized applications.  As they stand, they assume output to
19
 * an ordinary stdio stream.
20
 */
21
22
#include "cmyk.h"
23
#include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
24
25
#if defined(PPM_SUPPORTED) && \
26
    (BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED))
27
28
29
/*
30
 * For 12-bit JPEG data, we either downscale the values to 8 bits
31
 * (to write standard byte-per-sample PPM/PGM files), or output
32
 * nonstandard word-per-sample PPM/PGM files.  Downscaling is done
33
 * if PPM_NORAWWORD is defined (this can be done in the Makefile
34
 * or in jconfig.h).
35
 * (When the core library supports data precision reduction, a cleaner
36
 * implementation will be to ask for that instead.)
37
 */
38
39
#if BITS_IN_JSAMPLE == 8
40
0
#define PUTPPMSAMPLE(ptr, v)  *ptr++ = (char)(v)
41
0
#define BYTESPERSAMPLE  1
42
0
#define PPM_MAXVAL  ((1 << cinfo->data_precision) - 1)
43
#else
44
#ifdef PPM_NORAWWORD
45
#define PUTPPMSAMPLE(ptr, v) \
46
  *ptr++ = (char)((v) >> (cinfo->data_precision - 8))
47
#define BYTESPERSAMPLE  1
48
#define PPM_MAXVAL  255
49
#else
50
/* The word-per-sample format always puts the MSB first. */
51
0
#define PUTPPMSAMPLE(ptr, v) { \
52
0
  register int val_ = v; \
53
0
  *ptr++ = (char)((val_ >> 8) & 0xFF); \
54
0
  *ptr++ = (char)(val_ & 0xFF); \
55
0
}
56
0
#define BYTESPERSAMPLE  2
57
0
#define PPM_MAXVAL  ((1 << cinfo->data_precision) - 1)
58
#endif
59
#endif
60
61
62
/*
63
 * When _JSAMPLE is the same size as char, we can just fwrite() the
64
 * decompressed data to the PPM or PGM file.
65
 */
66
67
68
/* Private version of data destination object */
69
70
typedef struct {
71
  struct djpeg_dest_struct pub; /* public fields */
72
73
  /* Usually these two pointers point to the same place: */
74
  char *iobuffer;               /* fwrite's I/O buffer */
75
  _JSAMPROW pixrow;             /* decompressor output buffer */
76
  size_t buffer_width;          /* width of I/O buffer */
77
  JDIMENSION samples_per_row;   /* _JSAMPLEs per output row */
78
} ppm_dest_struct;
79
80
typedef ppm_dest_struct *ppm_dest_ptr;
81
82
83
/*
84
 * Write some pixel data.
85
 * In this module rows_supplied will always be 1.
86
 *
87
 * put_pixel_rows handles the "normal" 8-bit case where the decompressor
88
 * output buffer is physically the same as the fwrite buffer.
89
 */
90
91
METHODDEF(void)
92
put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
93
               JDIMENSION rows_supplied)
94
0
{
95
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
96
97
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
98
0
}
Unexecuted instantiation: wrppm-8.c:put_pixel_rows
Unexecuted instantiation: wrppm-12.c:put_pixel_rows
Unexecuted instantiation: wrppm-16.c:put_pixel_rows
99
100
101
/*
102
 * This code is used when we have to copy the data and apply a pixel
103
 * format translation.  Typically this only happens in 12-bit mode.
104
 */
105
106
METHODDEF(void)
107
copy_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
108
                JDIMENSION rows_supplied)
109
0
{
110
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
111
0
  register char *bufferptr;
112
0
  register _JSAMPROW ptr;
113
#if BITS_IN_JSAMPLE != 8
114
  register JDIMENSION col;
115
#endif
116
117
0
  ptr = dest->pub._buffer[0];
118
0
  bufferptr = dest->iobuffer;
119
#if BITS_IN_JSAMPLE == 8
120
  memcpy(bufferptr, ptr, dest->samples_per_row);
121
#else
122
0
  for (col = dest->samples_per_row; col > 0; col--) {
123
0
    PUTPPMSAMPLE(bufferptr, *ptr++);
124
0
  }
125
#endif
126
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
127
0
}
Unexecuted instantiation: wrppm-8.c:copy_pixel_rows
Unexecuted instantiation: wrppm-12.c:copy_pixel_rows
Unexecuted instantiation: wrppm-16.c:copy_pixel_rows
128
129
130
/*
131
 * Convert extended RGB to RGB.
132
 */
133
134
METHODDEF(void)
135
put_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, JDIMENSION rows_supplied)
136
0
{
137
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
138
0
  register char *bufferptr;
139
0
  register _JSAMPROW ptr;
140
0
  register JDIMENSION col;
141
0
  register int rindex = rgb_red[cinfo->out_color_space];
142
0
  register int gindex = rgb_green[cinfo->out_color_space];
143
0
  register int bindex = rgb_blue[cinfo->out_color_space];
144
0
  register int ps = rgb_pixelsize[cinfo->out_color_space];
145
146
0
  ptr = dest->pub._buffer[0];
147
0
  bufferptr = dest->iobuffer;
148
0
  for (col = cinfo->output_width; col > 0; col--) {
149
0
    PUTPPMSAMPLE(bufferptr, ptr[rindex]);
150
0
    PUTPPMSAMPLE(bufferptr, ptr[gindex]);
151
0
    PUTPPMSAMPLE(bufferptr, ptr[bindex]);
152
0
    ptr += ps;
153
0
  }
154
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
155
0
}
Unexecuted instantiation: wrppm-8.c:put_rgb
Unexecuted instantiation: wrppm-12.c:put_rgb
Unexecuted instantiation: wrppm-16.c:put_rgb
156
157
158
/*
159
 * Convert CMYK to RGB.
160
 */
161
162
METHODDEF(void)
163
put_cmyk(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
164
         JDIMENSION rows_supplied)
165
0
{
166
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
167
0
  register char *bufferptr;
168
0
  register _JSAMPROW ptr;
169
0
  register JDIMENSION col;
170
171
0
  ptr = dest->pub._buffer[0];
172
0
  bufferptr = dest->iobuffer;
173
0
  for (col = cinfo->output_width; col > 0; col--) {
174
0
    _JSAMPLE r, g, b, c = *ptr++, m = *ptr++, y = *ptr++, k = *ptr++;
175
0
    cmyk_to_rgb(PPM_MAXVAL, c, m, y, k, &r, &g, &b);
176
0
    PUTPPMSAMPLE(bufferptr, r);
177
0
    PUTPPMSAMPLE(bufferptr, g);
178
0
    PUTPPMSAMPLE(bufferptr, b);
179
0
  }
180
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
181
0
}
Unexecuted instantiation: wrppm-8.c:put_cmyk
Unexecuted instantiation: wrppm-12.c:put_cmyk
Unexecuted instantiation: wrppm-16.c:put_cmyk
182
183
184
/*
185
 * Write some pixel data when color quantization is in effect.
186
 * We have to demap the color index values to straight data.
187
 */
188
189
METHODDEF(void)
190
put_demapped_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
191
                 JDIMENSION rows_supplied)
192
0
{
193
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
194
0
  register char *bufferptr;
195
0
  register int pixval;
196
0
  register _JSAMPROW ptr;
197
0
  register _JSAMPROW color_map0 = ((_JSAMPARRAY)cinfo->colormap)[0];
198
0
  register _JSAMPROW color_map1 = ((_JSAMPARRAY)cinfo->colormap)[1];
199
0
  register _JSAMPROW color_map2 = ((_JSAMPARRAY)cinfo->colormap)[2];
200
0
  register JDIMENSION col;
201
202
0
  ptr = dest->pub._buffer[0];
203
0
  bufferptr = dest->iobuffer;
204
0
  for (col = cinfo->output_width; col > 0; col--) {
205
0
    pixval = *ptr++;
206
0
    PUTPPMSAMPLE(bufferptr, color_map0[pixval]);
207
0
    PUTPPMSAMPLE(bufferptr, color_map1[pixval]);
208
0
    PUTPPMSAMPLE(bufferptr, color_map2[pixval]);
209
0
  }
210
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
211
0
}
Unexecuted instantiation: wrppm-8.c:put_demapped_rgb
Unexecuted instantiation: wrppm-12.c:put_demapped_rgb
Unexecuted instantiation: wrppm-16.c:put_demapped_rgb
212
213
214
METHODDEF(void)
215
put_demapped_gray(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
216
                  JDIMENSION rows_supplied)
217
0
{
218
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
219
0
  register char *bufferptr;
220
0
  register _JSAMPROW ptr;
221
0
  register _JSAMPROW color_map = ((_JSAMPARRAY)cinfo->colormap)[0];
222
0
  register JDIMENSION col;
223
224
0
  ptr = dest->pub._buffer[0];
225
0
  bufferptr = dest->iobuffer;
226
0
  for (col = cinfo->output_width; col > 0; col--) {
227
0
    PUTPPMSAMPLE(bufferptr, color_map[*ptr++]);
228
0
  }
229
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
230
0
}
Unexecuted instantiation: wrppm-8.c:put_demapped_gray
Unexecuted instantiation: wrppm-12.c:put_demapped_gray
Unexecuted instantiation: wrppm-16.c:put_demapped_gray
231
232
233
/*
234
 * Startup: write the file header.
235
 */
236
237
METHODDEF(void)
238
start_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
239
0
{
240
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
241
242
  /* Emit file header */
243
0
  switch (cinfo->out_color_space) {
244
0
  case JCS_GRAYSCALE:
245
    /* emit header for raw PGM format */
246
0
    fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
247
0
            (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL);
248
0
    break;
249
0
  case JCS_RGB:
250
0
  case JCS_EXT_RGB:
251
0
  case JCS_EXT_RGBX:
252
0
  case JCS_EXT_BGR:
253
0
  case JCS_EXT_BGRX:
254
0
  case JCS_EXT_XBGR:
255
0
  case JCS_EXT_XRGB:
256
0
  case JCS_EXT_RGBA:
257
0
  case JCS_EXT_BGRA:
258
0
  case JCS_EXT_ABGR:
259
0
  case JCS_EXT_ARGB:
260
0
  case JCS_CMYK:
261
0
    if (!IsExtRGB(cinfo->out_color_space) && cinfo->quantize_colors)
262
0
      ERREXIT(cinfo, JERR_PPM_COLORSPACE);
263
    /* emit header for raw PPM format */
264
0
    fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
265
0
            (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL);
266
0
    break;
267
0
  default:
268
0
    ERREXIT(cinfo, JERR_PPM_COLORSPACE);
269
0
  }
270
0
}
Unexecuted instantiation: wrppm-8.c:start_output_ppm
Unexecuted instantiation: wrppm-12.c:start_output_ppm
Unexecuted instantiation: wrppm-16.c:start_output_ppm
271
272
273
/*
274
 * Finish up at the end of the file.
275
 */
276
277
METHODDEF(void)
278
finish_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
279
0
{
280
  /* Make sure we wrote the output file OK */
281
0
  fflush(dinfo->output_file);
282
0
  if (ferror(dinfo->output_file))
283
0
    ERREXIT(cinfo, JERR_FILE_WRITE);
284
0
}
Unexecuted instantiation: wrppm-8.c:finish_output_ppm
Unexecuted instantiation: wrppm-12.c:finish_output_ppm
Unexecuted instantiation: wrppm-16.c:finish_output_ppm
285
286
287
/*
288
 * Re-calculate buffer dimensions based on output dimensions.
289
 */
290
291
METHODDEF(void)
292
calc_buffer_dimensions_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
293
0
{
294
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
295
296
0
  if (cinfo->out_color_space == JCS_GRAYSCALE)
297
0
    dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
298
0
  else
299
0
    dest->samples_per_row = cinfo->output_width * 3;
300
0
  dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * sizeof(char));
301
0
}
Unexecuted instantiation: wrppm-8.c:calc_buffer_dimensions_ppm
Unexecuted instantiation: wrppm-12.c:calc_buffer_dimensions_ppm
Unexecuted instantiation: wrppm-16.c:calc_buffer_dimensions_ppm
302
303
304
/*
305
 * The module selection routine for PPM format output.
306
 */
307
308
GLOBAL(djpeg_dest_ptr)
309
_jinit_write_ppm(j_decompress_ptr cinfo)
310
0
{
311
0
  ppm_dest_ptr dest;
312
313
#if BITS_IN_JSAMPLE == 8
314
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
315
#else
316
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
317
0
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
318
0
#endif
319
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
320
321
  /* Create module interface object, fill in method pointers */
322
0
  dest = (ppm_dest_ptr)
323
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
324
0
                                  sizeof(ppm_dest_struct));
325
0
  dest->pub.start_output = start_output_ppm;
326
0
  dest->pub.finish_output = finish_output_ppm;
327
0
  dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_ppm;
328
329
  /* Calculate output image dimensions so we can allocate space */
330
0
  jpeg_calc_output_dimensions(cinfo);
331
332
  /* Create physical I/O buffer */
333
0
  dest->pub.calc_buffer_dimensions(cinfo, (djpeg_dest_ptr)dest);
334
0
  dest->iobuffer = (char *)(*cinfo->mem->alloc_small)
335
0
    ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->buffer_width);
336
337
0
  if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
338
0
      sizeof(_JSAMPLE) != sizeof(char) ||
339
0
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
340
0
      (cinfo->out_color_space != JCS_EXT_RGB &&
341
0
       cinfo->out_color_space != JCS_RGB)) {
342
#else
343
      cinfo->out_color_space != JCS_EXT_RGB) {
344
#endif
345
    /* When quantizing, we need an output buffer for colormap indexes
346
     * that's separate from the physical I/O buffer.  We also need a
347
     * separate buffer if pixel format translation must take place.
348
     */
349
0
    dest->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
350
0
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
351
0
       cinfo->output_width * cinfo->output_components, (JDIMENSION)1);
352
0
    dest->pub.buffer_height = 1;
353
0
    if (!cinfo->quantize_colors) {
354
0
      if (IsExtRGB(cinfo->out_color_space))
355
0
        dest->pub.put_pixel_rows = put_rgb;
356
0
      else if (cinfo->out_color_space == JCS_CMYK)
357
0
        dest->pub.put_pixel_rows = put_cmyk;
358
0
      else
359
0
        dest->pub.put_pixel_rows = copy_pixel_rows;
360
0
    } else if (cinfo->out_color_space == JCS_GRAYSCALE)
361
0
      dest->pub.put_pixel_rows = put_demapped_gray;
362
0
    else
363
0
      dest->pub.put_pixel_rows = put_demapped_rgb;
364
0
  } else {
365
    /* We will fwrite() directly from decompressor output buffer. */
366
    /* Synthesize a _JSAMPARRAY pointer structure */
367
0
    dest->pixrow = (_JSAMPROW)dest->iobuffer;
368
0
    dest->pub._buffer = &dest->pixrow;
369
0
    dest->pub.buffer_height = 1;
370
0
    dest->pub.put_pixel_rows = put_pixel_rows;
371
0
  }
372
373
0
  return (djpeg_dest_ptr)dest;
374
0
}
Unexecuted instantiation: jinit_write_ppm
Unexecuted instantiation: j12init_write_ppm
Unexecuted instantiation: j16init_write_ppm
375
376
#endif /* defined(PPM_SUPPORTED) &&
377
          (BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)) */