Coverage Report

Created: 2026-10-28 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.3.1.x/src/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, 2024, 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
23
#include "cmyk.h"
24
#include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
25
26
#if defined(PPM_SUPPORTED) && \
27
    (BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED))
28
29
30
/*
31
 * For 12-bit JPEG data, we either downscale the values to 8 bits
32
 * (to write standard byte-per-sample PPM/PGM files), or output
33
 * nonstandard word-per-sample PPM/PGM files.  Downscaling is done
34
 * if PPM_NORAWWORD is defined (this can be done in the Makefile
35
 * or in jconfig.h).
36
 * (When the core library supports data precision reduction, a cleaner
37
 * implementation will be to ask for that instead.)
38
 */
39
40
#if BITS_IN_JSAMPLE == 8
41
0
#define PUTPPMSAMPLE(ptr, v)  *ptr++ = (char)(v)
42
0
#define BYTESPERSAMPLE  1
43
0
#define PPM_MAXVAL  ((1 << cinfo->data_precision) - 1)
44
#else
45
#ifdef PPM_NORAWWORD
46
#define PUTPPMSAMPLE(ptr, v) \
47
  *ptr++ = (char)((v) >> (cinfo->data_precision - 8))
48
#define BYTESPERSAMPLE  1
49
#define PPM_MAXVAL  255
50
#else
51
/* The word-per-sample format always puts the MSB first. */
52
0
#define PUTPPMSAMPLE(ptr, v) { \
53
0
  register int val_ = v; \
54
0
  *ptr++ = (char)((val_ >> 8) & 0xFF); \
55
0
  *ptr++ = (char)(val_ & 0xFF); \
56
0
}
57
0
#define BYTESPERSAMPLE  2
58
0
#define PPM_MAXVAL  ((1 << cinfo->data_precision) - 1)
59
#endif
60
#endif
61
62
63
/*
64
 * When _JSAMPLE is the same size as char, we can just fwrite() the
65
 * decompressed data to the PPM or PGM file.
66
 */
67
68
69
/* Private version of data destination object */
70
71
typedef struct {
72
  struct djpeg_dest_struct pub; /* public fields */
73
74
  /* Usually these two pointers point to the same place: */
75
  char *iobuffer;               /* fwrite's I/O buffer */
76
  _JSAMPROW pixrow;             /* decompressor output buffer */
77
  size_t buffer_width;          /* width of I/O buffer */
78
  JDIMENSION samples_per_row;   /* _JSAMPLEs per output row */
79
} ppm_dest_struct;
80
81
typedef ppm_dest_struct *ppm_dest_ptr;
82
83
84
#if BITS_IN_JSAMPLE == 8
85
86
static INLINE boolean is_big_endian(void)
87
0
{
88
0
  int test_value = 1;
89
0
  if (*(char *)&test_value != 1)
90
0
    return TRUE;
91
0
  return FALSE;
92
0
}
93
94
95
/*
96
 * Write some pixel data.
97
 * In this module rows_supplied will always be 1.
98
 *
99
 * put_pixel_rows handles the "normal" 8-bit case where the decompressor
100
 * output buffer is physically the same as the fwrite buffer.
101
 */
102
103
#ifndef DJPEG_MULTILINE
104
105
METHODDEF(void)
106
put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
107
               JDIMENSION rows_supplied)
108
0
{
109
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
110
111
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
112
0
}
113
114
#endif
115
116
#endif
117
118
119
/*
120
 * This code is used when we have to copy the data and apply a pixel
121
 * format translation.  Typically this only happens in 12-bit mode.
122
 */
123
124
METHODDEF(void)
125
copy_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
126
                JDIMENSION rows_supplied)
127
0
{
128
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
129
0
  register char *bufferptr;
130
0
  register _JSAMPROW ptr;
131
0
  register JDIMENSION row = 0, col;
132
133
#ifdef DJPEG_MULTILINE
134
  for (row = 0; row < MIN(dest->pub.buffer_height, rows_supplied); row++) {
135
#endif
136
0
    ptr = dest->pub._buffer[row];
137
0
    bufferptr = dest->iobuffer;
138
0
    for (col = dest->samples_per_row; col > 0; col--) {
139
0
      PUTPPMSAMPLE(bufferptr, *ptr++);
140
0
    }
141
0
    fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
142
#ifdef DJPEG_MULTILINE
143
  }
144
#endif
145
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
146
147
148
/*
149
 * Convert extended RGB to RGB.
150
 */
151
152
METHODDEF(void)
153
put_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, JDIMENSION rows_supplied)
154
0
{
155
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
156
0
  register char *bufferptr;
157
0
  register _JSAMPROW ptr;
158
0
  register JDIMENSION row = 0, col;
159
0
  register int rindex = rgb_red[cinfo->out_color_space];
160
0
  register int gindex = rgb_green[cinfo->out_color_space];
161
0
  register int bindex = rgb_blue[cinfo->out_color_space];
162
0
  register int ps = rgb_pixelsize[cinfo->out_color_space];
163
164
#ifdef DJPEG_MULTILINE
165
  for (row = 0; row < MIN(dest->pub.buffer_height, rows_supplied); row++) {
166
#endif
167
0
    ptr = dest->pub._buffer[row];
168
0
    bufferptr = dest->iobuffer;
169
0
    for (col = cinfo->output_width; col > 0; col--) {
170
0
      PUTPPMSAMPLE(bufferptr, ptr[rindex]);
171
0
      PUTPPMSAMPLE(bufferptr, ptr[gindex]);
172
0
      PUTPPMSAMPLE(bufferptr, ptr[bindex]);
173
0
      ptr += ps;
174
0
    }
175
0
    fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
176
#ifdef DJPEG_MULTILINE
177
  }
178
#endif
179
0
}
Unexecuted instantiation: wrppm-8.c:put_rgb
Unexecuted instantiation: wrppm-12.c:put_rgb
Unexecuted instantiation: wrppm-16.c:put_rgb
180
181
182
#if BITS_IN_JSAMPLE == 8
183
184
/*
185
 * Convert RGB565 to RGB.
186
 */
187
188
METHODDEF(void)
189
put_rgb565(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
190
           JDIMENSION rows_supplied)
191
0
{
192
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
193
0
  register char *bufferptr;
194
0
  register unsigned short *ptr;
195
0
  register JDIMENSION row = 0, col;
196
0
  boolean big_endian = is_big_endian();
197
198
#ifdef DJPEG_MULTILINE
199
  for (row = 0; row < MIN(dest->pub.buffer_height, rows_supplied); row++) {
200
#endif
201
0
    ptr = (unsigned short *)dest->pub._buffer[row];
202
0
    bufferptr = dest->iobuffer;
203
0
    for (col = cinfo->output_width; col > 0; col--) {
204
0
      if (big_endian) {
205
0
        bufferptr[0] = *ptr & 0xF8;
206
0
        bufferptr[1] = ((*ptr << 5) & 0xE0) | ((*ptr >> 11) & 0x1C);
207
0
        bufferptr[2] = (*ptr >> 5) & 0xF8;
208
0
      } else {
209
0
        bufferptr[0] = (*ptr >> 8) & 0xF8;
210
0
        bufferptr[1] = (*ptr >> 3) & 0xFC;
211
0
        bufferptr[2] = (*ptr << 3) & 0xF8;
212
0
      }
213
0
      bufferptr += 3;
214
0
      ptr++;
215
0
    }
216
0
    fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
217
#ifdef DJPEG_MULTILINE
218
  }
219
#endif
220
0
}
221
222
#endif
223
224
225
/*
226
 * Convert CMYK to RGB.
227
 */
228
229
METHODDEF(void)
230
put_cmyk(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
231
         JDIMENSION rows_supplied)
232
0
{
233
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
234
0
  register char *bufferptr;
235
0
  register _JSAMPROW ptr;
236
0
  register JDIMENSION row = 0, col;
237
238
#ifdef DJPEG_MULTILINE
239
  for (row = 0; row < MIN(dest->pub.buffer_height, rows_supplied); row++) {
240
#endif
241
0
    ptr = dest->pub._buffer[row];
242
0
    bufferptr = dest->iobuffer;
243
0
    for (col = cinfo->output_width; col > 0; col--) {
244
0
      _JSAMPLE r, g, b, c = *ptr++, m = *ptr++, y = *ptr++, k = *ptr++;
245
0
      cmyk_to_rgb(PPM_MAXVAL, c, m, y, k, &r, &g, &b);
246
0
      PUTPPMSAMPLE(bufferptr, r);
247
0
      PUTPPMSAMPLE(bufferptr, g);
248
0
      PUTPPMSAMPLE(bufferptr, b);
249
0
    }
250
0
    fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
251
#ifdef DJPEG_MULTILINE
252
  }
253
#endif
254
0
}
Unexecuted instantiation: wrppm-8.c:put_cmyk
Unexecuted instantiation: wrppm-12.c:put_cmyk
Unexecuted instantiation: wrppm-16.c:put_cmyk
255
256
257
/*
258
 * Write some pixel data when color quantization is in effect.
259
 * We have to demap the color index values to straight data.
260
 */
261
262
METHODDEF(void)
263
put_demapped_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
264
                 JDIMENSION rows_supplied)
265
0
{
266
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
267
0
  register char *bufferptr;
268
0
  register int pixval;
269
0
  register _JSAMPROW ptr;
270
0
  register _JSAMPROW color_map0 =
271
0
    ((_JSAMPARRAY)cinfo->colormap)[rgb_red[cinfo->out_color_space]];
272
0
  register _JSAMPROW color_map1 =
273
0
    ((_JSAMPARRAY)cinfo->colormap)[rgb_green[cinfo->out_color_space]];
274
0
  register _JSAMPROW color_map2 =
275
0
    ((_JSAMPARRAY)cinfo->colormap)[rgb_blue[cinfo->out_color_space]];
276
0
  register JDIMENSION row = 0, col;
277
278
#ifdef DJPEG_MULTILINE
279
  for (row = 0; row < MIN(dest->pub.buffer_height, rows_supplied); row++) {
280
#endif
281
0
    ptr = dest->pub._buffer[row];
282
0
    bufferptr = dest->iobuffer;
283
0
    for (col = cinfo->output_width; col > 0; col--) {
284
0
      pixval = *ptr++;
285
0
      PUTPPMSAMPLE(bufferptr, color_map0[pixval]);
286
0
      PUTPPMSAMPLE(bufferptr, color_map1[pixval]);
287
0
      PUTPPMSAMPLE(bufferptr, color_map2[pixval]);
288
0
    }
289
0
    fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
290
#ifdef DJPEG_MULTILINE
291
  }
292
#endif
293
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
294
295
296
METHODDEF(void)
297
put_demapped_gray(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
298
                  JDIMENSION rows_supplied)
299
0
{
300
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
301
0
  register char *bufferptr;
302
0
  register _JSAMPROW ptr;
303
0
  register _JSAMPROW color_map = ((_JSAMPARRAY)cinfo->colormap)[0];
304
0
  register JDIMENSION row = 0, col;
305
306
#ifdef DJPEG_MULTILINE
307
  for (row = 0; row < MIN(dest->pub.buffer_height, rows_supplied); row++) {
308
#endif
309
0
    ptr = dest->pub._buffer[row];
310
0
    bufferptr = dest->iobuffer;
311
0
    for (col = cinfo->output_width; col > 0; col--) {
312
0
      PUTPPMSAMPLE(bufferptr, color_map[*ptr++]);
313
0
    }
314
0
    fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
315
#ifdef DJPEG_MULTILINE
316
  }
317
#endif
318
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
319
320
321
/*
322
 * Startup: write the file header.
323
 */
324
325
METHODDEF(void)
326
start_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
327
0
{
328
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
329
330
  /* Emit file header */
331
0
  switch (cinfo->out_color_space) {
332
0
  case JCS_GRAYSCALE:
333
    /* emit header for raw PGM format */
334
0
    fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
335
0
            (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL);
336
0
    break;
337
0
  case JCS_RGB:
338
0
  case JCS_EXT_RGB:
339
0
  case JCS_EXT_RGBX:
340
0
  case JCS_EXT_BGR:
341
0
  case JCS_EXT_BGRX:
342
0
  case JCS_EXT_XBGR:
343
0
  case JCS_EXT_XRGB:
344
0
  case JCS_EXT_RGBA:
345
0
  case JCS_EXT_BGRA:
346
0
  case JCS_EXT_ABGR:
347
0
  case JCS_EXT_ARGB:
348
#if BITS_IN_JSAMPLE == 8
349
0
  case JCS_RGB565:
350
0
#endif
351
0
  case JCS_CMYK:
352
0
    if (!IsExtRGB(cinfo->out_color_space) && cinfo->quantize_colors)
353
0
      ERREXIT(cinfo, JERR_PPM_COLORSPACE);
354
    /* emit header for raw PPM format */
355
0
    fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
356
0
            (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL);
357
0
    break;
358
0
  default:
359
0
    ERREXIT(cinfo, JERR_PPM_COLORSPACE);
360
0
  }
361
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
362
363
364
/*
365
 * Finish up at the end of the file.
366
 */
367
368
METHODDEF(void)
369
finish_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
370
0
{
371
  /* Make sure we wrote the output file OK */
372
0
  fflush(dinfo->output_file);
373
0
  if (ferror(dinfo->output_file))
374
0
    ERREXIT(cinfo, JERR_FILE_WRITE);
375
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
376
377
378
/*
379
 * Re-calculate buffer dimensions based on output dimensions.
380
 */
381
382
METHODDEF(void)
383
calc_buffer_dimensions_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
384
0
{
385
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
386
387
0
  if (cinfo->out_color_space == JCS_GRAYSCALE)
388
0
    dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
389
0
  else
390
0
    dest->samples_per_row = cinfo->output_width * 3;
391
0
  dest->buffer_width = dest->samples_per_row * BYTESPERSAMPLE;
392
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
393
394
395
/*
396
 * The module selection routine for PPM format output.
397
 */
398
399
GLOBAL(djpeg_dest_ptr)
400
_jinit_write_ppm(j_decompress_ptr cinfo)
401
0
{
402
0
  ppm_dest_ptr dest;
403
404
#if BITS_IN_JSAMPLE == 8
405
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
406
#else
407
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
408
0
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
409
0
#endif
410
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
411
412
  /* Create module interface object, fill in method pointers */
413
0
  dest = (ppm_dest_ptr)
414
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
415
0
                                  sizeof(ppm_dest_struct));
416
0
  dest->pub.start_output = start_output_ppm;
417
0
  dest->pub.finish_output = finish_output_ppm;
418
0
  dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_ppm;
419
420
  /* Calculate output image dimensions so we can allocate space */
421
0
  if (cinfo->image_width > JPEG_MAX_DIMENSION ||
422
0
      cinfo->image_height > JPEG_MAX_DIMENSION)
423
0
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
424
0
  jpeg_calc_output_dimensions(cinfo);
425
426
  /* Create physical I/O buffer */
427
0
  dest->pub.calc_buffer_dimensions(cinfo, (djpeg_dest_ptr)dest);
428
0
  dest->iobuffer = (char *)(*cinfo->mem->alloc_small)
429
0
    ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->buffer_width);
430
431
#if BITS_IN_JSAMPLE == 8 && !defined(DJPEG_MULTILINE)
432
0
  if (cinfo->quantize_colors ||
433
0
      (cinfo->out_color_space != JCS_EXT_RGB &&
434
0
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
435
0
       cinfo->out_color_space != JCS_RGB &&
436
0
#endif
437
0
       cinfo->out_color_space != JCS_GRAYSCALE))
438
0
#endif
439
0
  {
440
    /* When quantizing, we need an output buffer for colormap indexes
441
     * that's separate from the physical I/O buffer.  We also need a
442
     * separate buffer if pixel format translation must take place.
443
     */
444
#if BITS_IN_JSAMPLE == 8 && defined(DJPEG_MULTILINE)
445
    /* Allocate the sample array manually to ensure that the rows are
446
     * contiguous in memory, which further exercises esoteric code paths in the
447
     * library.
448
     */
449
    JDIMENSION row_width = cinfo->output_width * cinfo->output_components;
450
    dest->pub._buffer = (JSAMPARRAY)(*cinfo->mem->alloc_small)
451
      ((j_common_ptr)cinfo, JPOOL_IMAGE, 4 * sizeof(JSAMPROW));
452
    dest->pub._buffer[0] = (JSAMPROW)(*cinfo->mem->alloc_small)
453
      ((j_common_ptr)cinfo, JPOOL_IMAGE, row_width * 4);
454
    dest->pub._buffer[1] = dest->pub._buffer[0] + row_width;
455
    dest->pub._buffer[2] = dest->pub._buffer[1] + row_width;
456
    dest->pub._buffer[3] = dest->pub._buffer[2] + row_width;
457
    dest->pub.buffer_height = 4;
458
#else
459
0
    dest->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
460
0
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
461
0
       cinfo->output_width * cinfo->output_components, (JDIMENSION)1);
462
0
    dest->pub.buffer_height = 1;
463
0
#endif
464
0
    if (!cinfo->quantize_colors) {
465
0
      if (IsExtRGB(cinfo->out_color_space))
466
0
        dest->pub.put_pixel_rows = put_rgb;
467
#if BITS_IN_JSAMPLE == 8
468
0
      else if (cinfo->out_color_space == JCS_RGB565)
469
0
        dest->pub.put_pixel_rows = put_rgb565;
470
0
#endif
471
0
      else if (cinfo->out_color_space == JCS_CMYK)
472
0
        dest->pub.put_pixel_rows = put_cmyk;
473
0
      else
474
0
        dest->pub.put_pixel_rows = copy_pixel_rows;
475
0
    } else if (cinfo->out_color_space == JCS_GRAYSCALE)
476
0
      dest->pub.put_pixel_rows = put_demapped_gray;
477
0
    else
478
0
      dest->pub.put_pixel_rows = put_demapped_rgb;
479
0
  }
480
#if BITS_IN_JSAMPLE == 8 && !defined(DJPEG_MULTILINE)
481
0
  else {
482
    /* We will fwrite() directly from decompressor output buffer. */
483
    /* Synthesize a _JSAMPARRAY pointer structure */
484
0
    dest->pixrow = (_JSAMPROW)dest->iobuffer;
485
0
    dest->pub._buffer = &dest->pixrow;
486
0
    dest->pub.buffer_height = 1;
487
0
    dest->pub.put_pixel_rows = put_pixel_rows;
488
0
  }
489
#endif
490
491
0
  return (djpeg_dest_ptr)dest;
492
0
}
Unexecuted instantiation: jinit_write_ppm
Unexecuted instantiation: j12init_write_ppm
Unexecuted instantiation: j16init_write_ppm
493
494
#endif /* defined(PPM_SUPPORTED) &&
495
          (BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)) */