Coverage Report

Created: 2024-01-23 06:26

/src/libjpeg-turbo.main/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, 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
/*
83
 * Write some pixel data.
84
 * In this module rows_supplied will always be 1.
85
 *
86
 * put_pixel_rows handles the "normal" 8-bit case where the decompressor
87
 * output buffer is physically the same as the fwrite buffer.
88
 */
89
90
METHODDEF(void)
91
put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
92
               JDIMENSION rows_supplied)
93
0
{
94
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
95
96
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
97
0
}
98
99
100
/*
101
 * This code is used when we have to copy the data and apply a pixel
102
 * format translation.  Typically this only happens in 12-bit mode.
103
 */
104
105
METHODDEF(void)
106
copy_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
0
  register char *bufferptr;
111
0
  register _JSAMPROW ptr;
112
#if BITS_IN_JSAMPLE != 8
113
  register JDIMENSION col;
114
#endif
115
116
0
  ptr = dest->pub._buffer[0];
117
0
  bufferptr = dest->iobuffer;
118
0
#if BITS_IN_JSAMPLE == 8
119
0
  memcpy(bufferptr, ptr, dest->samples_per_row);
120
#else
121
  for (col = dest->samples_per_row; col > 0; col--) {
122
    PUTPPMSAMPLE(bufferptr, *ptr++);
123
  }
124
#endif
125
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
126
0
}
127
128
129
/*
130
 * Convert extended RGB to RGB.
131
 */
132
133
METHODDEF(void)
134
put_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, JDIMENSION rows_supplied)
135
0
{
136
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
137
0
  register char *bufferptr;
138
0
  register _JSAMPROW ptr;
139
0
  register JDIMENSION col;
140
0
  register int rindex = rgb_red[cinfo->out_color_space];
141
0
  register int gindex = rgb_green[cinfo->out_color_space];
142
0
  register int bindex = rgb_blue[cinfo->out_color_space];
143
0
  register int ps = rgb_pixelsize[cinfo->out_color_space];
144
145
0
  ptr = dest->pub._buffer[0];
146
0
  bufferptr = dest->iobuffer;
147
0
  for (col = cinfo->output_width; col > 0; col--) {
148
0
    PUTPPMSAMPLE(bufferptr, ptr[rindex]);
149
0
    PUTPPMSAMPLE(bufferptr, ptr[gindex]);
150
0
    PUTPPMSAMPLE(bufferptr, ptr[bindex]);
151
0
    ptr += ps;
152
0
  }
153
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
154
0
}
155
156
157
/*
158
 * Convert CMYK to RGB.
159
 */
160
161
METHODDEF(void)
162
put_cmyk(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
163
         JDIMENSION rows_supplied)
164
0
{
165
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
166
0
  register char *bufferptr;
167
0
  register _JSAMPROW ptr;
168
0
  register JDIMENSION col;
169
170
0
  ptr = dest->pub._buffer[0];
171
0
  bufferptr = dest->iobuffer;
172
0
  for (col = cinfo->output_width; col > 0; col--) {
173
0
    _JSAMPLE r, g, b, c = *ptr++, m = *ptr++, y = *ptr++, k = *ptr++;
174
0
    cmyk_to_rgb(c, m, y, k, &r, &g, &b);
175
0
    PUTPPMSAMPLE(bufferptr, r);
176
0
    PUTPPMSAMPLE(bufferptr, g);
177
0
    PUTPPMSAMPLE(bufferptr, b);
178
0
  }
179
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
180
0
}
181
182
183
/*
184
 * Write some pixel data when color quantization is in effect.
185
 * We have to demap the color index values to straight data.
186
 */
187
188
METHODDEF(void)
189
put_demapped_rgb(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 int pixval;
195
0
  register _JSAMPROW ptr;
196
0
  register _JSAMPROW color_map0 = ((_JSAMPARRAY)cinfo->colormap)[0];
197
0
  register _JSAMPROW color_map1 = ((_JSAMPARRAY)cinfo->colormap)[1];
198
0
  register _JSAMPROW color_map2 = ((_JSAMPARRAY)cinfo->colormap)[2];
199
0
  register JDIMENSION col;
200
201
0
  ptr = dest->pub._buffer[0];
202
0
  bufferptr = dest->iobuffer;
203
0
  for (col = cinfo->output_width; col > 0; col--) {
204
0
    pixval = *ptr++;
205
0
    PUTPPMSAMPLE(bufferptr, color_map0[pixval]);
206
0
    PUTPPMSAMPLE(bufferptr, color_map1[pixval]);
207
0
    PUTPPMSAMPLE(bufferptr, color_map2[pixval]);
208
0
  }
209
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
210
0
}
211
212
213
METHODDEF(void)
214
put_demapped_gray(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
215
                  JDIMENSION rows_supplied)
216
0
{
217
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
218
0
  register char *bufferptr;
219
0
  register _JSAMPROW ptr;
220
0
  register _JSAMPROW color_map = ((_JSAMPARRAY)cinfo->colormap)[0];
221
0
  register JDIMENSION col;
222
223
0
  ptr = dest->pub._buffer[0];
224
0
  bufferptr = dest->iobuffer;
225
0
  for (col = cinfo->output_width; col > 0; col--) {
226
0
    PUTPPMSAMPLE(bufferptr, color_map[*ptr++]);
227
0
  }
228
0
  fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
229
0
}
230
231
232
/*
233
 * Startup: write the file header.
234
 */
235
236
METHODDEF(void)
237
start_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
238
0
{
239
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
240
241
  /* Emit file header */
242
0
  switch (cinfo->out_color_space) {
243
0
  case JCS_GRAYSCALE:
244
    /* emit header for raw PGM format */
245
0
    fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
246
0
            (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL);
247
0
    break;
248
0
  case JCS_RGB:
249
0
  case JCS_EXT_RGB:
250
0
  case JCS_EXT_RGBX:
251
0
  case JCS_EXT_BGR:
252
0
  case JCS_EXT_BGRX:
253
0
  case JCS_EXT_XBGR:
254
0
  case JCS_EXT_XRGB:
255
0
  case JCS_EXT_RGBA:
256
0
  case JCS_EXT_BGRA:
257
0
  case JCS_EXT_ABGR:
258
0
  case JCS_EXT_ARGB:
259
0
  case JCS_CMYK:
260
0
    if (!IsExtRGB(cinfo->out_color_space) && cinfo->quantize_colors)
261
0
      ERREXIT(cinfo, JERR_PPM_COLORSPACE);
262
    /* emit header for raw PPM format */
263
0
    fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
264
0
            (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL);
265
0
    break;
266
0
  default:
267
0
    ERREXIT(cinfo, JERR_PPM_COLORSPACE);
268
0
  }
269
0
}
270
271
272
/*
273
 * Finish up at the end of the file.
274
 */
275
276
METHODDEF(void)
277
finish_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
278
0
{
279
  /* Make sure we wrote the output file OK */
280
0
  fflush(dinfo->output_file);
281
0
  if (ferror(dinfo->output_file))
282
0
    ERREXIT(cinfo, JERR_FILE_WRITE);
283
0
}
284
285
286
/*
287
 * Re-calculate buffer dimensions based on output dimensions.
288
 */
289
290
METHODDEF(void)
291
calc_buffer_dimensions_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
292
0
{
293
0
  ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
294
295
0
  if (cinfo->out_color_space == JCS_GRAYSCALE)
296
0
    dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
297
0
  else
298
0
    dest->samples_per_row = cinfo->output_width * 3;
299
0
  dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * sizeof(char));
300
0
}
301
302
303
/*
304
 * The module selection routine for PPM format output.
305
 */
306
307
GLOBAL(djpeg_dest_ptr)
308
_jinit_write_ppm(j_decompress_ptr cinfo)
309
0
{
310
0
  ppm_dest_ptr dest;
311
312
0
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
313
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
314
315
  /* Create module interface object, fill in method pointers */
316
0
  dest = (ppm_dest_ptr)
317
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
318
0
                                  sizeof(ppm_dest_struct));
319
0
  dest->pub.start_output = start_output_ppm;
320
0
  dest->pub.finish_output = finish_output_ppm;
321
0
  dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_ppm;
322
323
  /* Calculate output image dimensions so we can allocate space */
324
0
  jpeg_calc_output_dimensions(cinfo);
325
326
  /* Create physical I/O buffer */
327
0
  dest->pub.calc_buffer_dimensions(cinfo, (djpeg_dest_ptr)dest);
328
0
  dest->iobuffer = (char *)(*cinfo->mem->alloc_small)
329
0
    ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->buffer_width);
330
331
0
  if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
332
0
      sizeof(_JSAMPLE) != sizeof(char) ||
333
0
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
334
0
      (cinfo->out_color_space != JCS_EXT_RGB &&
335
0
       cinfo->out_color_space != JCS_RGB)) {
336
#else
337
      cinfo->out_color_space != JCS_EXT_RGB) {
338
#endif
339
    /* When quantizing, we need an output buffer for colormap indexes
340
     * that's separate from the physical I/O buffer.  We also need a
341
     * separate buffer if pixel format translation must take place.
342
     */
343
0
    dest->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
344
0
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
345
0
       cinfo->output_width * cinfo->output_components, (JDIMENSION)1);
346
0
    dest->pub.buffer_height = 1;
347
0
    if (!cinfo->quantize_colors) {
348
0
      if (IsExtRGB(cinfo->out_color_space))
349
0
        dest->pub.put_pixel_rows = put_rgb;
350
0
      else if (cinfo->out_color_space == JCS_CMYK)
351
0
        dest->pub.put_pixel_rows = put_cmyk;
352
0
      else
353
0
        dest->pub.put_pixel_rows = copy_pixel_rows;
354
0
    } else if (cinfo->out_color_space == JCS_GRAYSCALE)
355
0
      dest->pub.put_pixel_rows = put_demapped_gray;
356
0
    else
357
0
      dest->pub.put_pixel_rows = put_demapped_rgb;
358
0
  } else {
359
    /* We will fwrite() directly from decompressor output buffer. */
360
    /* Synthesize a _JSAMPARRAY pointer structure */
361
0
    dest->pixrow = (_JSAMPROW)dest->iobuffer;
362
0
    dest->pub._buffer = &dest->pixrow;
363
0
    dest->pub.buffer_height = 1;
364
0
    dest->pub.put_pixel_rows = put_pixel_rows;
365
0
  }
366
367
0
  return (djpeg_dest_ptr)dest;
368
0
}
Unexecuted instantiation: jinit_write_ppm
Unexecuted instantiation: j12init_write_ppm
Unexecuted instantiation: j16init_write_ppm
369
370
#endif /* defined(PPM_SUPPORTED) &&
371
          (BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)) */