Coverage Report

Created: 2026-04-12 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.3.0.x/wrppm.c
Line
Count
Source
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, 2026, 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  255
43
#else
44
#ifdef PPM_NORAWWORD
45
#define PUTPPMSAMPLE(ptr, v)  *ptr++ = (char)((v) >> (BITS_IN_JSAMPLE - 8))
46
#define BYTESPERSAMPLE  1
47
#define PPM_MAXVAL  255
48
#else
49
/* The word-per-sample format always puts the MSB first. */
50
#define PUTPPMSAMPLE(ptr, v) { \
51
  register int val_ = v; \
52
  *ptr++ = (char)((val_ >> 8) & 0xFF); \
53
  *ptr++ = (char)(val_ & 0xFF); \
54
}
55
#define BYTESPERSAMPLE  2
56
#define PPM_MAXVAL  ((1 << BITS_IN_JSAMPLE) - 1)
57
#endif
58
#endif
59
60
61
/*
62
 * When _JSAMPLE is the same size as char, we can just fwrite() the
63
 * decompressed data to the PPM or PGM file.
64
 */
65
66
67
/* Private version of data destination object */
68
69
typedef struct {
70
  struct djpeg_dest_struct pub; /* public fields */
71
72
  /* Usually these two pointers point to the same place: */
73
  char *iobuffer;               /* fwrite's I/O buffer */
74
  _JSAMPROW pixrow;             /* decompressor output buffer */
75
  size_t buffer_width;          /* width of I/O buffer */
76
  JDIMENSION samples_per_row;   /* _JSAMPLEs per output row */
77
} ppm_dest_struct;
78
79
typedef ppm_dest_struct *ppm_dest_ptr;
80
81
82
#if BITS_IN_JSAMPLE == 8
83
84
/*
85
 * Write some pixel data.
86
 * In this module rows_supplied will always be 1.
87
 *
88
 * put_pixel_rows handles the "normal" 8-bit case where the decompressor
89
 * output buffer is physically the same as the fwrite buffer.
90
 */
91
92
METHODDEF(void)
93
put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
94
               JDIMENSION rows_supplied)
95
0
{
96
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
97
98
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
99
0
}
100
101
#endif
102
103
104
/*
105
 * This code is used when we have to copy the data and apply a pixel
106
 * format translation.  Typically this only happens in 12-bit mode.
107
 */
108
109
METHODDEF(void)
110
copy_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
111
                JDIMENSION rows_supplied)
112
0
{
113
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
114
0
  register char *bufferptr;
115
0
  register _JSAMPROW ptr;
116
0
  register JDIMENSION col;
117
118
0
  ptr = dest->pub._buffer[0];
119
0
  bufferptr = dest->iobuffer;
120
0
  for (col = dest->samples_per_row; col > 0; col--) {
121
0
    PUTPPMSAMPLE(bufferptr, *ptr++);
122
0
  }
123
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
124
0
}
125
126
127
/*
128
 * Convert extended RGB to RGB.
129
 */
130
131
METHODDEF(void)
132
put_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, JDIMENSION rows_supplied)
133
0
{
134
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
135
0
  register char *bufferptr;
136
0
  register _JSAMPROW ptr;
137
0
  register JDIMENSION col;
138
0
  register int rindex = rgb_red[cinfo->out_color_space];
139
0
  register int gindex = rgb_green[cinfo->out_color_space];
140
0
  register int bindex = rgb_blue[cinfo->out_color_space];
141
0
  register int ps = rgb_pixelsize[cinfo->out_color_space];
142
143
0
  ptr = dest->pub._buffer[0];
144
0
  bufferptr = dest->iobuffer;
145
0
  for (col = cinfo->output_width; col > 0; col--) {
146
0
    PUTPPMSAMPLE(bufferptr, ptr[rindex]);
147
0
    PUTPPMSAMPLE(bufferptr, ptr[gindex]);
148
0
    PUTPPMSAMPLE(bufferptr, ptr[bindex]);
149
0
    ptr += ps;
150
0
  }
151
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
152
0
}
153
154
155
/*
156
 * Convert CMYK to RGB.
157
 */
158
159
METHODDEF(void)
160
put_cmyk(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
161
         JDIMENSION rows_supplied)
162
0
{
163
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
164
0
  register char *bufferptr;
165
0
  register _JSAMPROW ptr;
166
0
  register JDIMENSION col;
167
168
0
  ptr = dest->pub._buffer[0];
169
0
  bufferptr = dest->iobuffer;
170
0
  for (col = cinfo->output_width; col > 0; col--) {
171
0
    _JSAMPLE r, g, b, c = *ptr++, m = *ptr++, y = *ptr++, k = *ptr++;
172
0
    cmyk_to_rgb(c, m, y, k, &r, &g, &b);
173
0
    PUTPPMSAMPLE(bufferptr, r);
174
0
    PUTPPMSAMPLE(bufferptr, g);
175
0
    PUTPPMSAMPLE(bufferptr, b);
176
0
  }
177
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
178
0
}
179
180
181
/*
182
 * Write some pixel data when color quantization is in effect.
183
 * We have to demap the color index values to straight data.
184
 */
185
186
METHODDEF(void)
187
put_demapped_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
188
                 JDIMENSION rows_supplied)
189
0
{
190
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
191
0
  register char *bufferptr;
192
0
  register int pixval;
193
0
  register _JSAMPROW ptr;
194
0
  register _JSAMPROW color_map0 =
195
0
    ((_JSAMPARRAY)cinfo->colormap)[rgb_red[cinfo->out_color_space]];
196
0
  register _JSAMPROW color_map1 =
197
0
    ((_JSAMPARRAY)cinfo->colormap)[rgb_green[cinfo->out_color_space]];
198
0
  register _JSAMPROW color_map2 =
199
0
    ((_JSAMPARRAY)cinfo->colormap)[rgb_blue[cinfo->out_color_space]];
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
}
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
}
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
}
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
}
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;
301
0
}
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
0
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
314
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
315
316
  /* Create module interface object, fill in method pointers */
317
0
  dest = (ppm_dest_ptr)
318
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
319
0
                                  sizeof(ppm_dest_struct));
320
0
  dest->pub.start_output = start_output_ppm;
321
0
  dest->pub.finish_output = finish_output_ppm;
322
0
  dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_ppm;
323
324
  /* Calculate output image dimensions so we can allocate space */
325
0
  if (cinfo->image_width > JPEG_MAX_DIMENSION ||
326
0
      cinfo->image_height > JPEG_MAX_DIMENSION)
327
0
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
328
0
  jpeg_calc_output_dimensions(cinfo);
329
330
  /* Create physical I/O buffer */
331
0
  dest->pub.calc_buffer_dimensions(cinfo, (djpeg_dest_ptr)dest);
332
0
  dest->iobuffer = (char *)(*cinfo->mem->alloc_small)
333
0
    ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->buffer_width);
334
335
#if BITS_IN_JSAMPLE == 8
336
0
  if (cinfo->quantize_colors ||
337
0
      (cinfo->out_color_space != JCS_EXT_RGB &&
338
0
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
339
0
       cinfo->out_color_space != JCS_RGB &&
340
0
#endif
341
0
       cinfo->out_color_space != JCS_GRAYSCALE))
342
0
#endif
343
0
  {
344
    /* When quantizing, we need an output buffer for colormap indexes
345
     * that's separate from the physical I/O buffer.  We also need a
346
     * separate buffer if pixel format translation must take place.
347
     */
348
0
    dest->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
349
0
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
350
0
       cinfo->output_width * cinfo->output_components, (JDIMENSION)1);
351
0
    dest->pub.buffer_height = 1;
352
0
    if (!cinfo->quantize_colors) {
353
0
      if (IsExtRGB(cinfo->out_color_space))
354
0
        dest->pub.put_pixel_rows = put_rgb;
355
0
      else if (cinfo->out_color_space == JCS_CMYK)
356
0
        dest->pub.put_pixel_rows = put_cmyk;
357
0
      else
358
0
        dest->pub.put_pixel_rows = copy_pixel_rows;
359
0
    } else if (cinfo->out_color_space == JCS_GRAYSCALE)
360
0
      dest->pub.put_pixel_rows = put_demapped_gray;
361
0
    else
362
0
      dest->pub.put_pixel_rows = put_demapped_rgb;
363
0
  }
364
#if BITS_IN_JSAMPLE == 8
365
0
  else {
366
    /* We will fwrite() directly from decompressor output buffer. */
367
    /* Synthesize a _JSAMPARRAY pointer structure */
368
0
    dest->pixrow = (_JSAMPROW)dest->iobuffer;
369
0
    dest->pub._buffer = &dest->pixrow;
370
0
    dest->pub.buffer_height = 1;
371
0
    dest->pub.put_pixel_rows = put_pixel_rows;
372
0
  }
373
#endif
374
375
0
  return (djpeg_dest_ptr)dest;
376
0
}
Unexecuted instantiation: jinit_write_ppm
Unexecuted instantiation: j12init_write_ppm
Unexecuted instantiation: j16init_write_ppm
377
378
#endif /* defined(PPM_SUPPORTED) &&
379
          (BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)) */