Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/libpng/pngread.c
Line
Count
Source
1
/* pngread.c - read a PNG file
2
 *
3
 * Copyright (c) 2018-2026 Cosmin Truta
4
 * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
5
 * Copyright (c) 1996-1997 Andreas Dilger
6
 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
7
 *
8
 * This code is released under the libpng license.
9
 * For conditions of distribution and use, see the disclaimer
10
 * and license in png.h
11
 *
12
 * This file contains routines that an application calls directly to
13
 * read a PNG file or stream.
14
 */
15
16
#include "pngpriv.h"
17
#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
18
#  include <errno.h>
19
#endif
20
21
#ifdef PNG_READ_SUPPORTED
22
23
/* Create a PNG structure for reading, and allocate any memory needed. */
24
PNG_FUNCTION(png_structp,PNGAPI
25
png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
26
    png_error_ptr error_fn, png_error_ptr warn_fn),
27
    PNG_ALLOCATED)
28
16.3k
{
29
#ifndef PNG_USER_MEM_SUPPORTED
30
   png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
31
        error_fn, warn_fn, NULL, NULL, NULL);
32
#else
33
16.3k
   return png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
34
16.3k
        warn_fn, NULL, NULL, NULL);
35
16.3k
}
36
37
/* Alternate create PNG structure for reading, and allocate any memory
38
 * needed.
39
 */
40
PNG_FUNCTION(png_structp,PNGAPI
41
png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
42
    png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
43
    png_malloc_ptr malloc_fn, png_free_ptr free_fn),
44
    PNG_ALLOCATED)
45
16.3k
{
46
16.3k
   png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
47
16.3k
       error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
48
16.3k
#endif /* USER_MEM */
49
50
16.3k
   if (png_ptr != NULL)
51
16.3k
   {
52
16.3k
      png_ptr->mode = PNG_IS_READ_STRUCT;
53
54
      /* Added in libpng-1.6.0; this can be used to detect a read structure if
55
       * required (it will be zero in a write structure.)
56
       */
57
16.3k
#     ifdef PNG_SEQUENTIAL_READ_SUPPORTED
58
16.3k
         png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE;
59
16.3k
#     endif
60
61
16.3k
#     ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED
62
16.3k
         png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
63
64
         /* In stable builds only warn if an application error can be completely
65
          * handled.
66
          */
67
16.3k
#        if PNG_RELEASE_BUILD
68
16.3k
            png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
69
16.3k
#        endif
70
16.3k
#     endif
71
72
      /* TODO: delay this, it can be done in png_init_io (if the app doesn't
73
       * do it itself) avoiding setting the default function if it is not
74
       * required.
75
       */
76
16.3k
      png_set_read_fn(png_ptr, NULL, NULL);
77
16.3k
   }
78
79
16.3k
   return png_ptr;
80
16.3k
}
81
82
83
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
84
/* Read the information before the actual image data.  This has been
85
 * changed in v0.90 to allow reading a file that already has the magic
86
 * bytes read from the stream.  You can tell libpng how many bytes have
87
 * been read from the beginning of the stream (up to the maximum of 8)
88
 * via png_set_sig_bytes(), and we will only check the remaining bytes
89
 * here.  The application can then have access to the signature bytes we
90
 * read if it is determined that this isn't a valid PNG file.
91
 */
92
void PNGAPI
93
png_read_info(png_structrp png_ptr, png_inforp info_ptr)
94
16.3k
{
95
16.3k
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
96
16.3k
   int keep;
97
16.3k
#endif
98
99
16.3k
   png_debug(1, "in png_read_info");
100
101
16.3k
   if (png_ptr == NULL || info_ptr == NULL)
102
0
      return;
103
104
   /* Read and check the PNG file signature. */
105
16.3k
   png_read_sig(png_ptr, info_ptr);
106
107
16.3k
   for (;;)
108
221k
   {
109
221k
      png_uint_32 length = png_read_chunk_header(png_ptr);
110
221k
      png_uint_32 chunk_name = png_ptr->chunk_name;
111
112
      /* IDAT logic needs to happen here to simplify getting the two flags
113
       * right.
114
       */
115
221k
      if (chunk_name == png_IDAT)
116
9.68k
      {
117
9.68k
         if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
118
1
            png_chunk_error(png_ptr, "Missing IHDR before IDAT");
119
120
9.68k
         else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
121
428
             (png_ptr->mode & PNG_HAVE_PLTE) == 0)
122
7
            png_chunk_error(png_ptr, "Missing PLTE before IDAT");
123
124
9.67k
         else if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
125
0
            png_chunk_benign_error(png_ptr, "Too many IDATs found");
126
127
9.67k
         png_ptr->mode |= PNG_HAVE_IDAT;
128
9.67k
      }
129
130
211k
      else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
131
0
      {
132
0
         png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
133
0
         png_ptr->mode |= PNG_AFTER_IDAT;
134
0
      }
135
136
221k
      if (chunk_name == png_IHDR)
137
15.2k
         png_handle_chunk(png_ptr, info_ptr, length);
138
139
205k
      else if (chunk_name == png_IEND)
140
344
         png_handle_chunk(png_ptr, info_ptr, length);
141
142
205k
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
143
205k
      else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
144
0
      {
145
0
         png_handle_unknown(png_ptr, info_ptr, length, keep);
146
147
0
         if (chunk_name == png_PLTE)
148
0
            png_ptr->mode |= PNG_HAVE_PLTE;
149
150
0
         else if (chunk_name == png_IDAT)
151
0
         {
152
0
            png_ptr->idat_size = 0; /* It has been consumed */
153
0
            break;
154
0
         }
155
0
      }
156
205k
#endif
157
158
205k
      else if (chunk_name == png_IDAT)
159
9.67k
      {
160
9.67k
         png_ptr->idat_size = length;
161
9.67k
         break;
162
9.67k
      }
163
164
195k
      else
165
195k
         png_handle_chunk(png_ptr, info_ptr, length);
166
221k
   }
167
16.3k
}
168
#endif /* SEQUENTIAL_READ */
169
170
/* Optional call to update the users info_ptr structure */
171
void PNGAPI
172
png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
173
9.67k
{
174
9.67k
   png_debug(1, "in png_read_update_info");
175
176
9.67k
   if (png_ptr != NULL)
177
9.67k
   {
178
9.67k
      if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
179
9.67k
      {
180
9.67k
         png_read_start_row(png_ptr);
181
182
9.67k
#        ifdef PNG_READ_TRANSFORMS_SUPPORTED
183
9.67k
            png_read_transform_info(png_ptr, info_ptr);
184
#        else
185
            PNG_UNUSED(info_ptr)
186
#        endif
187
9.67k
      }
188
189
      /* New in 1.6.0 this avoids the bug of doing the initializations twice */
190
0
      else
191
0
         png_app_error(png_ptr,
192
0
             "png_read_update_info/png_start_read_image: duplicate call");
193
9.67k
   }
194
9.67k
}
195
196
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
197
/* Initialize palette, background, etc, after transformations
198
 * are set, but before any reading takes place.  This allows
199
 * the user to obtain a gamma-corrected palette, for example.
200
 * If the user doesn't call this, we will do it ourselves.
201
 */
202
void PNGAPI
203
png_start_read_image(png_structrp png_ptr)
204
0
{
205
0
   png_debug(1, "in png_start_read_image");
206
207
0
   if (png_ptr != NULL)
208
0
   {
209
0
      if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
210
0
         png_read_start_row(png_ptr);
211
212
      /* New in 1.6.0 this avoids the bug of doing the initializations twice */
213
0
      else
214
0
         png_app_error(png_ptr,
215
0
             "png_start_read_image/png_read_update_info: duplicate call");
216
0
   }
217
0
}
218
#endif /* SEQUENTIAL_READ */
219
220
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
221
#ifdef PNG_MNG_FEATURES_SUPPORTED
222
/* Undoes intrapixel differencing,
223
 * NOTE: this is apparently only supported in the 'sequential' reader.
224
 */
225
static void
226
png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
227
0
{
228
0
   png_debug(1, "in png_do_read_intrapixel");
229
230
0
   if (
231
0
       (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
232
0
   {
233
0
      int bytes_per_pixel;
234
0
      png_uint_32 row_width = row_info->width;
235
236
0
      if (row_info->bit_depth == 8)
237
0
      {
238
0
         png_bytep rp;
239
0
         png_uint_32 i;
240
241
0
         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
242
0
            bytes_per_pixel = 3;
243
244
0
         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
245
0
            bytes_per_pixel = 4;
246
247
0
         else
248
0
            return;
249
250
0
         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
251
0
         {
252
0
            *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
253
0
            *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
254
0
         }
255
0
      }
256
0
      else if (row_info->bit_depth == 16)
257
0
      {
258
0
         png_bytep rp;
259
0
         png_uint_32 i;
260
261
0
         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
262
0
            bytes_per_pixel = 6;
263
264
0
         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
265
0
            bytes_per_pixel = 8;
266
267
0
         else
268
0
            return;
269
270
0
         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
271
0
         {
272
0
            png_uint_32 s0   = (png_uint_32)(*(rp    ) << 8) | *(rp + 1);
273
0
            png_uint_32 s1   = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3);
274
0
            png_uint_32 s2   = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5);
275
0
            png_uint_32 red  = (s0 + s1 + 65536) & 0xffff;
276
0
            png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
277
0
            *(rp    ) = (png_byte)((red >> 8) & 0xff);
278
0
            *(rp + 1) = (png_byte)(red & 0xff);
279
0
            *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
280
0
            *(rp + 5) = (png_byte)(blue & 0xff);
281
0
         }
282
0
      }
283
0
   }
284
0
}
285
#endif /* MNG_FEATURES */
286
287
void PNGAPI
288
png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
289
13.7M
{
290
13.7M
   png_row_info row_info;
291
292
13.7M
   if (png_ptr == NULL)
293
0
      return;
294
295
13.7M
   png_debug2(1, "in png_read_row (row %lu, pass %d)",
296
13.7M
       (unsigned long)png_ptr->row_number, png_ptr->pass);
297
298
   /* png_read_start_row sets the information (in particular iwidth) for this
299
    * interlace pass.
300
    */
301
13.7M
   if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
302
0
      png_read_start_row(png_ptr);
303
304
   /* 1.5.6: row_info moved out of png_struct to a local here. */
305
13.7M
   row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
306
13.7M
   row_info.color_type = png_ptr->color_type;
307
13.7M
   row_info.bit_depth = png_ptr->bit_depth;
308
13.7M
   row_info.channels = png_ptr->channels;
309
13.7M
   row_info.pixel_depth = png_ptr->pixel_depth;
310
13.7M
   row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
311
312
13.7M
#ifdef PNG_WARNINGS_SUPPORTED
313
13.7M
   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
314
9.58k
   {
315
   /* Check for transforms that have been set but were defined out */
316
#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
317
   if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
318
      png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
319
#endif
320
321
#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
322
   if ((png_ptr->transformations & PNG_FILLER) != 0)
323
      png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
324
#endif
325
326
#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
327
    !defined(PNG_READ_PACKSWAP_SUPPORTED)
328
   if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
329
      png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
330
#endif
331
332
#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
333
   if ((png_ptr->transformations & PNG_PACK) != 0)
334
      png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
335
#endif
336
337
#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
338
   if ((png_ptr->transformations & PNG_SHIFT) != 0)
339
      png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
340
#endif
341
342
#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
343
   if ((png_ptr->transformations & PNG_BGR) != 0)
344
      png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
345
#endif
346
347
#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
348
   if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
349
      png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
350
#endif
351
9.58k
   }
352
13.7M
#endif /* WARNINGS */
353
354
13.7M
#ifdef PNG_READ_INTERLACING_SUPPORTED
355
   /* If interlaced and we do not need a new row, combine row and return.
356
    * Notice that the pixels we have from previous rows have been transformed
357
    * already; we can only combine like with like (transformed or
358
    * untransformed) and, because of the libpng API for interlaced images, this
359
    * means we must transform before de-interlacing.
360
    */
361
13.7M
   if (png_ptr->interlaced != 0 &&
362
13.0M
       (png_ptr->transformations & PNG_INTERLACE) != 0)
363
13.0M
   {
364
13.0M
      switch (png_ptr->pass)
365
13.0M
      {
366
2.63M
         case 0:
367
2.63M
            if (png_ptr->row_number & 0x07)
368
2.30M
            {
369
2.30M
               if (dsp_row != NULL)
370
0
                  png_combine_row(png_ptr, dsp_row, 1/*display*/);
371
2.30M
               png_read_finish_row(png_ptr);
372
2.30M
               return;
373
2.30M
            }
374
334k
            break;
375
376
2.20M
         case 1:
377
2.20M
            if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
378
1.93M
            {
379
1.93M
               if (dsp_row != NULL)
380
0
                  png_combine_row(png_ptr, dsp_row, 1/*display*/);
381
382
1.93M
               png_read_finish_row(png_ptr);
383
1.93M
               return;
384
1.93M
            }
385
276k
            break;
386
387
1.96M
         case 2:
388
1.96M
            if ((png_ptr->row_number & 0x07) != 4)
389
1.72M
            {
390
1.72M
               if (dsp_row != NULL && (png_ptr->row_number & 4))
391
0
                  png_combine_row(png_ptr, dsp_row, 1/*display*/);
392
393
1.72M
               png_read_finish_row(png_ptr);
394
1.72M
               return;
395
1.72M
            }
396
245k
            break;
397
398
1.82M
         case 3:
399
1.82M
            if ((png_ptr->row_number & 3) || png_ptr->width < 3)
400
1.36M
            {
401
1.36M
               if (dsp_row != NULL)
402
0
                  png_combine_row(png_ptr, dsp_row, 1/*display*/);
403
404
1.36M
               png_read_finish_row(png_ptr);
405
1.36M
               return;
406
1.36M
            }
407
459k
            break;
408
409
1.76M
         case 4:
410
1.76M
            if ((png_ptr->row_number & 3) != 2)
411
1.32M
            {
412
1.32M
               if (dsp_row != NULL && (png_ptr->row_number & 2))
413
0
                  png_combine_row(png_ptr, dsp_row, 1/*display*/);
414
415
1.32M
               png_read_finish_row(png_ptr);
416
1.32M
               return;
417
1.32M
            }
418
439k
            break;
419
420
1.49M
         case 5:
421
1.49M
            if ((png_ptr->row_number & 1) || png_ptr->width < 2)
422
747k
            {
423
747k
               if (dsp_row != NULL)
424
0
                  png_combine_row(png_ptr, dsp_row, 1/*display*/);
425
426
747k
               png_read_finish_row(png_ptr);
427
747k
               return;
428
747k
            }
429
749k
            break;
430
431
749k
         default:
432
1.09M
         case 6:
433
1.09M
            if ((png_ptr->row_number & 1) == 0)
434
550k
            {
435
550k
               png_read_finish_row(png_ptr);
436
550k
               return;
437
550k
            }
438
545k
            break;
439
13.0M
      }
440
13.0M
   }
441
3.79M
#endif
442
443
3.79M
   if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
444
0
      png_error(png_ptr, "Invalid attempt to read row data");
445
446
   /* Fill the row with IDAT data: */
447
3.79M
   png_ptr->row_buf[0]=255; /* to force error if no data was found */
448
3.79M
   png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
449
450
3.79M
   if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
451
2.27M
   {
452
2.27M
      if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
453
2.27M
         png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
454
2.27M
             png_ptr->prev_row + 1, png_ptr->row_buf[0]);
455
152
      else
456
152
         png_error(png_ptr, "bad adaptive filter value");
457
2.27M
   }
458
459
   /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
460
    * 1.5.6, while the buffer really is this big in current versions of libpng
461
    * it may not be in the future, so this was changed just to copy the
462
    * interlaced count:
463
    */
464
3.79M
   memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
465
466
3.79M
#ifdef PNG_MNG_FEATURES_SUPPORTED
467
3.79M
   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
468
0
       (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
469
0
   {
470
      /* Intrapixel differencing */
471
0
      png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
472
0
   }
473
3.79M
#endif
474
475
3.79M
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
476
3.79M
   if (png_ptr->transformations
477
7.77k
#     ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
478
7.77k
         || png_ptr->num_palette_max >= 0
479
3.79M
#     endif
480
3.79M
      )
481
3.79M
      png_do_read_transformations(png_ptr, &row_info);
482
3.79M
#endif
483
484
   /* The transformed pixel depth should match the depth now in row_info. */
485
3.79M
   if (png_ptr->transformed_pixel_depth == 0)
486
8.85k
   {
487
8.85k
      png_ptr->transformed_pixel_depth = row_info.pixel_depth;
488
8.85k
      if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
489
0
         png_error(png_ptr, "sequential row overflow");
490
8.85k
   }
491
492
3.78M
   else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
493
0
      png_error(png_ptr, "internal sequential row size calculation error");
494
495
3.79M
#ifdef PNG_READ_INTERLACING_SUPPORTED
496
   /* Expand interlaced rows to full size */
497
3.79M
   if (png_ptr->interlaced != 0 &&
498
3.05M
      (png_ptr->transformations & PNG_INTERLACE) != 0)
499
3.05M
   {
500
3.05M
      if (png_ptr->pass < 6)
501
2.50M
         png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
502
2.50M
             png_ptr->transformations);
503
504
3.05M
      if (dsp_row != NULL)
505
0
         png_combine_row(png_ptr, dsp_row, 1/*display*/);
506
507
3.05M
      if (row != NULL)
508
3.05M
         png_combine_row(png_ptr, row, 0/*row*/);
509
3.05M
   }
510
511
748k
   else
512
748k
#endif
513
748k
   {
514
748k
      if (row != NULL)
515
746k
         png_combine_row(png_ptr, row, -1/*ignored*/);
516
517
748k
      if (dsp_row != NULL)
518
0
         png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
519
748k
   }
520
3.79M
   png_read_finish_row(png_ptr);
521
522
3.79M
   if (png_ptr->read_row_fn != NULL)
523
0
      (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
524
3.79M
}
525
#endif /* SEQUENTIAL_READ */
526
527
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
528
/* Read one or more rows of image data.  If the image is interlaced,
529
 * and png_set_interlace_handling() has been called, the rows need to
530
 * contain the contents of the rows from the previous pass.  If the
531
 * image has alpha or transparency, and png_handle_alpha()[*] has been
532
 * called, the rows contents must be initialized to the contents of the
533
 * screen.
534
 *
535
 * "row" holds the actual image, and pixels are placed in it
536
 * as they arrive.  If the image is displayed after each pass, it will
537
 * appear to "sparkle" in.  "display_row" can be used to display a
538
 * "chunky" progressive image, with finer detail added as it becomes
539
 * available.  If you do not want this "chunky" display, you may pass
540
 * NULL for display_row.  If you do not want the sparkle display, and
541
 * you have not called png_handle_alpha(), you may pass NULL for rows.
542
 * If you have called png_handle_alpha(), and the image has either an
543
 * alpha channel or a transparency chunk, you must provide a buffer for
544
 * rows.  In this case, you do not have to provide a display_row buffer
545
 * also, but you may.  If the image is not interlaced, or if you have
546
 * not called png_set_interlace_handling(), the display_row buffer will
547
 * be ignored, so pass NULL to it.
548
 *
549
 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
550
 */
551
552
void PNGAPI
553
png_read_rows(png_structrp png_ptr, png_bytepp row,
554
    png_bytepp display_row, png_uint_32 num_rows)
555
0
{
556
0
   png_uint_32 i;
557
0
   png_bytepp rp;
558
0
   png_bytepp dp;
559
560
0
   png_debug(1, "in png_read_rows");
561
562
0
   if (png_ptr == NULL)
563
0
      return;
564
565
0
   rp = row;
566
0
   dp = display_row;
567
0
   if (rp != NULL && dp != NULL)
568
0
      for (i = 0; i < num_rows; i++)
569
0
      {
570
0
         png_bytep rptr = *rp++;
571
0
         png_bytep dptr = *dp++;
572
573
0
         png_read_row(png_ptr, rptr, dptr);
574
0
      }
575
576
0
   else if (rp != NULL)
577
0
      for (i = 0; i < num_rows; i++)
578
0
      {
579
0
         png_bytep rptr = *rp;
580
0
         png_read_row(png_ptr, rptr, NULL);
581
0
         rp++;
582
0
      }
583
584
0
   else if (dp != NULL)
585
0
      for (i = 0; i < num_rows; i++)
586
0
      {
587
0
         png_bytep dptr = *dp;
588
0
         png_read_row(png_ptr, NULL, dptr);
589
0
         dp++;
590
0
      }
591
0
}
592
#endif /* SEQUENTIAL_READ */
593
594
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
595
/* Read the entire image.  If the image has an alpha channel or a tRNS
596
 * chunk, and you have called png_handle_alpha()[*], you will need to
597
 * initialize the image to the current image that PNG will be overlaying.
598
 * We set the num_rows again here, in case it was incorrectly set in
599
 * png_read_start_row() by a call to png_read_update_info() or
600
 * png_start_read_image() if png_set_interlace_handling() wasn't called
601
 * prior to either of these functions like it should have been.  You can
602
 * only call this function once.  If you desire to have an image for
603
 * each pass of a interlaced image, use png_read_rows() instead.
604
 *
605
 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
606
 */
607
void PNGAPI
608
png_read_image(png_structrp png_ptr, png_bytepp image)
609
0
{
610
0
   png_uint_32 i, image_height;
611
0
   int pass, j;
612
0
   png_bytepp rp;
613
614
0
   png_debug(1, "in png_read_image");
615
616
0
   if (png_ptr == NULL)
617
0
      return;
618
619
0
#ifdef PNG_READ_INTERLACING_SUPPORTED
620
0
   if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
621
0
   {
622
0
      pass = png_set_interlace_handling(png_ptr);
623
      /* And make sure transforms are initialized. */
624
0
      png_start_read_image(png_ptr);
625
0
   }
626
0
   else
627
0
   {
628
0
      if (png_ptr->interlaced != 0 &&
629
0
          (png_ptr->transformations & PNG_INTERLACE) == 0)
630
0
      {
631
         /* Caller called png_start_read_image or png_read_update_info without
632
          * first turning on the PNG_INTERLACE transform.  We can fix this here,
633
          * but the caller should do it!
634
          */
635
0
         png_warning(png_ptr, "Interlace handling should be turned on when "
636
0
             "using png_read_image");
637
         /* Make sure this is set correctly */
638
0
         png_ptr->num_rows = png_ptr->height;
639
0
      }
640
641
      /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
642
       * the above error case.
643
       */
644
0
      pass = png_set_interlace_handling(png_ptr);
645
0
   }
646
#else
647
   if (png_ptr->interlaced)
648
      png_error(png_ptr,
649
          "Cannot read interlaced image -- interlace handler disabled");
650
651
   pass = 1;
652
#endif
653
654
0
   image_height=png_ptr->height;
655
656
0
   for (j = 0; j < pass; j++)
657
0
   {
658
0
      rp = image;
659
0
      for (i = 0; i < image_height; i++)
660
0
      {
661
0
         png_read_row(png_ptr, *rp, NULL);
662
0
         rp++;
663
0
      }
664
0
   }
665
0
}
666
#endif /* SEQUENTIAL_READ */
667
668
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
669
/* Read the end of the PNG file.  Will not read past the end of the
670
 * file, will verify the end is accurate, and will read any comments
671
 * or time information at the end of the file, if info is not NULL.
672
 */
673
void PNGAPI
674
png_read_end(png_structrp png_ptr, png_inforp info_ptr)
675
7.60k
{
676
7.60k
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
677
7.60k
   int keep;
678
7.60k
#endif
679
680
7.60k
   png_debug(1, "in png_read_end");
681
682
7.60k
   if (png_ptr == NULL)
683
0
      return;
684
685
   /* If png_read_end is called in the middle of reading the rows there may
686
    * still be pending IDAT data and an owned zstream.  Deal with this here.
687
    */
688
7.60k
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
689
7.60k
   if (png_chunk_unknown_handling(png_ptr, png_IDAT) == 0)
690
7.60k
#endif
691
7.60k
      png_read_finish_IDAT(png_ptr);
692
693
7.60k
#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
694
   /* Report invalid palette index; added at libng-1.5.10 */
695
7.60k
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
696
252
       png_ptr->num_palette_max >= png_ptr->num_palette)
697
0
      png_benign_error(png_ptr, "Read palette index exceeding num_palette");
698
7.60k
#endif
699
700
7.60k
   do
701
28.3k
   {
702
28.3k
      png_uint_32 length = png_read_chunk_header(png_ptr);
703
28.3k
      png_uint_32 chunk_name = png_ptr->chunk_name;
704
705
28.3k
      if (chunk_name != png_IDAT)
706
25.2k
      {
707
         /* These flags must be set consistently for all non-IDAT chunks,
708
          * including the unknown chunks.
709
          */
710
25.2k
         png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT | PNG_AFTER_IDAT;
711
25.2k
      }
712
713
28.3k
      if (chunk_name == png_IEND)
714
7.44k
         png_handle_chunk(png_ptr, info_ptr, length);
715
716
20.9k
      else if (chunk_name == png_IHDR)
717
1
         png_handle_chunk(png_ptr, info_ptr, length);
718
719
20.9k
      else if (info_ptr == NULL)
720
0
         png_crc_finish(png_ptr, length);
721
722
20.9k
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
723
20.9k
      else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
724
0
      {
725
0
         if (chunk_name == png_IDAT)
726
0
         {
727
0
            if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
728
0
                || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
729
0
               png_benign_error(png_ptr, ".Too many IDATs found");
730
0
         }
731
0
         png_handle_unknown(png_ptr, info_ptr, length, keep);
732
0
         if (chunk_name == png_PLTE)
733
0
            png_ptr->mode |= PNG_HAVE_PLTE;
734
0
      }
735
20.9k
#endif
736
737
20.9k
      else if (chunk_name == png_IDAT)
738
3.07k
      {
739
         /* Zero length IDATs are legal after the last IDAT has been
740
          * read, but not after other chunks have been read.  1.6 does not
741
          * always read all the deflate data; specifically it cannot be relied
742
          * upon to read the Adler32 at the end.  If it doesn't ignore IDAT
743
          * chunks which are longer than zero as well:
744
          */
745
3.07k
         if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
746
3.07k
             || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
747
2.35k
            png_benign_error(png_ptr, "..Too many IDATs found");
748
749
3.07k
         png_crc_finish(png_ptr, length);
750
3.07k
      }
751
752
17.8k
      else
753
17.8k
         png_handle_chunk(png_ptr, info_ptr, length);
754
28.3k
   } while ((png_ptr->mode & PNG_HAVE_IEND) == 0);
755
7.60k
}
756
#endif /* SEQUENTIAL_READ */
757
758
/* Free all memory used in the read struct */
759
static void
760
png_read_destroy(png_structrp png_ptr)
761
16.3k
{
762
16.3k
   png_debug(1, "in png_read_destroy");
763
764
16.3k
#ifdef PNG_READ_GAMMA_SUPPORTED
765
16.3k
   png_destroy_gamma_table(png_ptr);
766
16.3k
#endif
767
768
16.3k
   png_free(png_ptr, png_ptr->big_row_buf);
769
16.3k
   png_ptr->big_row_buf = NULL;
770
16.3k
   png_free(png_ptr, png_ptr->big_prev_row);
771
16.3k
   png_ptr->big_prev_row = NULL;
772
16.3k
   png_free(png_ptr, png_ptr->read_buffer);
773
16.3k
   png_ptr->read_buffer = NULL;
774
775
16.3k
#ifdef PNG_READ_QUANTIZE_SUPPORTED
776
16.3k
   png_free(png_ptr, png_ptr->palette_lookup);
777
16.3k
   png_ptr->palette_lookup = NULL;
778
16.3k
   png_free(png_ptr, png_ptr->quantize_index);
779
16.3k
   png_ptr->quantize_index = NULL;
780
16.3k
#endif
781
782
16.3k
   if ((png_ptr->free_me & PNG_FREE_PLTE) != 0)
783
0
   {
784
0
      png_zfree(png_ptr, png_ptr->palette);
785
0
      png_ptr->palette = NULL;
786
0
   }
787
16.3k
   png_ptr->free_me &= ~PNG_FREE_PLTE;
788
789
16.3k
#if defined(PNG_tRNS_SUPPORTED) || \
790
16.3k
    defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
791
16.3k
   if ((png_ptr->free_me & PNG_FREE_TRNS) != 0)
792
0
   {
793
0
      png_free(png_ptr, png_ptr->trans_alpha);
794
0
      png_ptr->trans_alpha = NULL;
795
0
   }
796
16.3k
   png_ptr->free_me &= ~PNG_FREE_TRNS;
797
16.3k
#endif
798
799
16.3k
   inflateEnd(&png_ptr->zstream);
800
801
16.3k
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
802
16.3k
   png_free(png_ptr, png_ptr->save_buffer);
803
16.3k
   png_ptr->save_buffer = NULL;
804
16.3k
#endif
805
806
16.3k
#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \
807
16.3k
   defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
808
16.3k
   png_free(png_ptr, png_ptr->unknown_chunk.data);
809
16.3k
   png_ptr->unknown_chunk.data = NULL;
810
16.3k
#endif
811
812
16.3k
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
813
16.3k
   png_free(png_ptr, png_ptr->chunk_list);
814
16.3k
   png_ptr->chunk_list = NULL;
815
16.3k
#endif
816
817
16.3k
#if defined(PNG_READ_EXPAND_SUPPORTED) && \
818
16.3k
    (defined(PNG_ARM_NEON_IMPLEMENTATION) || \
819
16.3k
     defined(PNG_RISCV_RVV_IMPLEMENTATION))
820
16.3k
   png_free(png_ptr, png_ptr->riffled_palette);
821
16.3k
   png_ptr->riffled_palette = NULL;
822
16.3k
#endif
823
824
   /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
825
    * callbacks are still set at this point.  They are required to complete the
826
    * destruction of the png_struct itself.
827
    */
828
16.3k
}
829
830
/* Free all memory used by the read */
831
void PNGAPI
832
png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
833
    png_infopp end_info_ptr_ptr)
834
16.3k
{
835
16.3k
   png_structrp png_ptr = NULL;
836
837
16.3k
   png_debug(1, "in png_destroy_read_struct");
838
839
16.3k
   if (png_ptr_ptr != NULL)
840
16.3k
      png_ptr = *png_ptr_ptr;
841
842
16.3k
   if (png_ptr == NULL)
843
0
      return;
844
845
   /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
846
    * behavior.  Prior to 1.6.0 libpng did extra 'info' destruction in this API.
847
    * The extra was, apparently, unnecessary yet this hides memory leak bugs.
848
    */
849
16.3k
   png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
850
16.3k
   png_destroy_info_struct(png_ptr, info_ptr_ptr);
851
852
16.3k
   *png_ptr_ptr = NULL;
853
16.3k
   png_read_destroy(png_ptr);
854
16.3k
   png_destroy_png_struct(png_ptr);
855
16.3k
}
856
857
void PNGAPI
858
png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
859
0
{
860
0
   if (png_ptr == NULL)
861
0
      return;
862
863
0
   png_ptr->read_row_fn = read_row_fn;
864
0
}
865
866
867
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
868
#ifdef PNG_INFO_IMAGE_SUPPORTED
869
void PNGAPI
870
png_read_png(png_structrp png_ptr, png_inforp info_ptr,
871
    int transforms, png_voidp params)
872
0
{
873
0
   png_debug(1, "in png_read_png");
874
875
0
   if (png_ptr == NULL || info_ptr == NULL)
876
0
      return;
877
878
   /* png_read_info() gives us all of the information from the
879
    * PNG file before the first IDAT (image data chunk).
880
    */
881
0
   png_read_info(png_ptr, info_ptr);
882
0
   if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
883
0
      png_error(png_ptr, "Image is too high to process with png_read_png()");
884
885
   /* -------------- image transformations start here ------------------- */
886
   /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
887
    * is not implemented.  This will only happen in de-configured (non-default)
888
    * libpng builds.  The results can be unexpected - png_read_png may return
889
    * short or mal-formed rows because the transform is skipped.
890
    */
891
892
   /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
893
    */
894
0
   if ((transforms & PNG_TRANSFORM_SCALE_16) != 0)
895
      /* Added at libpng-1.5.4. "strip_16" produces the same result that it
896
       * did in earlier versions, while "scale_16" is now more accurate.
897
       */
898
0
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
899
0
      png_set_scale_16(png_ptr);
900
#else
901
      png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported");
902
#endif
903
904
   /* If both SCALE and STRIP are required pngrtran will effectively cancel the
905
    * latter by doing SCALE first.  This is ok and allows apps not to check for
906
    * which is supported to get the right answer.
907
    */
908
0
   if ((transforms & PNG_TRANSFORM_STRIP_16) != 0)
909
0
#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
910
0
      png_set_strip_16(png_ptr);
911
#else
912
      png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported");
913
#endif
914
915
   /* Strip alpha bytes from the input data without combining with
916
    * the background (not recommended).
917
    */
918
0
   if ((transforms & PNG_TRANSFORM_STRIP_ALPHA) != 0)
919
0
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
920
0
      png_set_strip_alpha(png_ptr);
921
#else
922
      png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported");
923
#endif
924
925
   /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
926
    * byte into separate bytes (useful for paletted and grayscale images).
927
    */
928
0
   if ((transforms & PNG_TRANSFORM_PACKING) != 0)
929
0
#ifdef PNG_READ_PACK_SUPPORTED
930
0
      png_set_packing(png_ptr);
931
#else
932
      png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
933
#endif
934
935
   /* Change the order of packed pixels to least significant bit first
936
    * (not useful if you are using png_set_packing).
937
    */
938
0
   if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
939
0
#ifdef PNG_READ_PACKSWAP_SUPPORTED
940
0
      png_set_packswap(png_ptr);
941
#else
942
      png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
943
#endif
944
945
   /* Expand paletted colors into true RGB triplets
946
    * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
947
    * Expand paletted or RGB images with transparency to full alpha
948
    * channels so the data will be available as RGBA quartets.
949
    */
950
0
   if ((transforms & PNG_TRANSFORM_EXPAND) != 0)
951
0
#ifdef PNG_READ_EXPAND_SUPPORTED
952
0
      png_set_expand(png_ptr);
953
#else
954
      png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported");
955
#endif
956
957
   /* We don't handle background color or gamma transformation or quantizing.
958
    */
959
960
   /* Invert monochrome files to have 0 as white and 1 as black
961
    */
962
0
   if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
963
0
#ifdef PNG_READ_INVERT_SUPPORTED
964
0
      png_set_invert_mono(png_ptr);
965
#else
966
      png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
967
#endif
968
969
   /* If you want to shift the pixel values from the range [0,255] or
970
    * [0,65535] to the original [0,7] or [0,31], or whatever range the
971
    * colors were originally in:
972
    */
973
0
   if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
974
0
#ifdef PNG_READ_SHIFT_SUPPORTED
975
0
      if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
976
0
         png_set_shift(png_ptr, &info_ptr->sig_bit);
977
#else
978
      png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
979
#endif
980
981
   /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
982
0
   if ((transforms & PNG_TRANSFORM_BGR) != 0)
983
0
#ifdef PNG_READ_BGR_SUPPORTED
984
0
      png_set_bgr(png_ptr);
985
#else
986
      png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
987
#endif
988
989
   /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
990
0
   if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
991
0
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
992
0
      png_set_swap_alpha(png_ptr);
993
#else
994
      png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
995
#endif
996
997
   /* Swap bytes of 16-bit files to least significant byte first */
998
0
   if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
999
0
#ifdef PNG_READ_SWAP_SUPPORTED
1000
0
      png_set_swap(png_ptr);
1001
#else
1002
      png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
1003
#endif
1004
1005
/* Added at libpng-1.2.41 */
1006
   /* Invert the alpha channel from opacity to transparency */
1007
0
   if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
1008
0
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1009
0
      png_set_invert_alpha(png_ptr);
1010
#else
1011
      png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
1012
#endif
1013
1014
/* Added at libpng-1.2.41 */
1015
   /* Expand grayscale image to RGB */
1016
0
   if ((transforms & PNG_TRANSFORM_GRAY_TO_RGB) != 0)
1017
0
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1018
0
      png_set_gray_to_rgb(png_ptr);
1019
#else
1020
      png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
1021
#endif
1022
1023
/* Added at libpng-1.5.4 */
1024
0
   if ((transforms & PNG_TRANSFORM_EXPAND_16) != 0)
1025
0
#ifdef PNG_READ_EXPAND_16_SUPPORTED
1026
0
      png_set_expand_16(png_ptr);
1027
#else
1028
      png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported");
1029
#endif
1030
1031
   /* We don't handle adding filler bytes */
1032
1033
   /* We use png_read_image and rely on that for interlace handling, but we also
1034
    * call png_read_update_info therefore must turn on interlace handling now:
1035
    */
1036
0
   (void)png_set_interlace_handling(png_ptr);
1037
1038
   /* Optional call to gamma correct and add the background to the palette
1039
    * and update info structure.  REQUIRED if you are expecting libpng to
1040
    * update the palette for you (i.e., you selected such a transform above).
1041
    */
1042
0
   png_read_update_info(png_ptr, info_ptr);
1043
1044
   /* -------------- image transformations end here ------------------- */
1045
1046
0
   png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1047
0
   if (info_ptr->row_pointers == NULL)
1048
0
   {
1049
0
      png_uint_32 iptr;
1050
1051
0
      info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr,
1052
0
          info_ptr->height * (sizeof (png_bytep))));
1053
1054
0
      for (iptr=0; iptr<info_ptr->height; iptr++)
1055
0
         info_ptr->row_pointers[iptr] = NULL;
1056
1057
0
      info_ptr->free_me |= PNG_FREE_ROWS;
1058
1059
0
      for (iptr = 0; iptr < info_ptr->height; iptr++)
1060
0
         info_ptr->row_pointers[iptr] = png_voidcast(png_bytep,
1061
0
             png_malloc(png_ptr, info_ptr->rowbytes));
1062
0
   }
1063
1064
0
   png_read_image(png_ptr, info_ptr->row_pointers);
1065
0
   info_ptr->valid |= PNG_INFO_IDAT;
1066
1067
   /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1068
0
   png_read_end(png_ptr, info_ptr);
1069
1070
0
   PNG_UNUSED(params)
1071
0
}
1072
#endif /* INFO_IMAGE */
1073
#endif /* SEQUENTIAL_READ */
1074
1075
#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1076
/* SIMPLIFIED READ
1077
 *
1078
 * This code currently relies on the sequential reader, though it could easily
1079
 * be made to work with the progressive one.
1080
 */
1081
/* Arguments to png_image_finish_read: */
1082
1083
/* Encoding of PNG data (used by the color-map code) */
1084
0
#  define P_NOTSET  0 /* File encoding not yet known */
1085
0
#  define P_sRGB    1 /* 8-bit encoded to sRGB gamma */
1086
0
#  define P_LINEAR  2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1087
0
#  define P_FILE    3 /* 8-bit encoded to file gamma, not sRGB or linear */
1088
0
#  define P_LINEAR8 4 /* 8-bit linear: only from a file value */
1089
1090
/* Color-map processing: after libpng has run on the PNG image further
1091
 * processing may be needed to convert the data to color-map indices.
1092
 */
1093
0
#define PNG_CMAP_NONE      0
1094
0
#define PNG_CMAP_GA        1 /* Process GA data to a color-map with alpha */
1095
0
#define PNG_CMAP_TRANS     2 /* Process GA data to a background index */
1096
0
#define PNG_CMAP_RGB       3 /* Process RGB data */
1097
0
#define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1098
1099
/* The following document where the background is for each processing case. */
1100
0
#define PNG_CMAP_NONE_BACKGROUND      256
1101
0
#define PNG_CMAP_GA_BACKGROUND        231
1102
0
#define PNG_CMAP_TRANS_BACKGROUND     254
1103
0
#define PNG_CMAP_RGB_BACKGROUND       256
1104
0
#define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1105
1106
typedef struct
1107
{
1108
   /* Arguments */
1109
   png_imagep image;
1110
   png_voidp buffer;
1111
   png_int_32 row_stride;
1112
   png_voidp colormap;
1113
   png_const_colorp background;
1114
1115
   /* Instance variables */
1116
   png_voidp local_row;
1117
   png_voidp first_row;
1118
   ptrdiff_t row_step;              /* step between rows */
1119
   int file_encoding;               /* E_ values above */
1120
   png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */
1121
   int colormap_processing;         /* PNG_CMAP_ values above */
1122
} png_image_read_control;
1123
1124
/* Do all the *safe* initialization - 'safe' means that png_error won't be
1125
 * called, so setting up the jmp_buf is not required.  This means that anything
1126
 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1127
 * instead so that control is returned safely back to this routine.
1128
 */
1129
static int
1130
png_image_read_init(png_imagep image)
1131
0
{
1132
0
   if (image->opaque == NULL)
1133
0
   {
1134
0
      png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
1135
0
          png_safe_error, png_safe_warning);
1136
1137
      /* And set the rest of the structure to NULL to ensure that the various
1138
       * fields are consistent.
1139
       */
1140
0
      memset(image, 0, (sizeof *image));
1141
0
      image->version = PNG_IMAGE_VERSION;
1142
1143
0
      if (png_ptr != NULL)
1144
0
      {
1145
0
         png_infop info_ptr = png_create_info_struct(png_ptr);
1146
1147
0
         if (info_ptr != NULL)
1148
0
         {
1149
0
            png_controlp control = png_voidcast(png_controlp,
1150
0
                png_malloc_warn(png_ptr, (sizeof *control)));
1151
1152
0
            if (control != NULL)
1153
0
            {
1154
0
               memset(control, 0, (sizeof *control));
1155
1156
0
               control->png_ptr = png_ptr;
1157
0
               control->info_ptr = info_ptr;
1158
0
               control->for_write = 0;
1159
1160
0
               image->opaque = control;
1161
0
               return 1;
1162
0
            }
1163
1164
            /* Error clean up */
1165
0
            png_destroy_info_struct(png_ptr, &info_ptr);
1166
0
         }
1167
1168
0
         png_destroy_read_struct(&png_ptr, NULL, NULL);
1169
0
      }
1170
1171
0
      return png_image_error(image, "png_image_read: out of memory");
1172
0
   }
1173
1174
0
   return png_image_error(image, "png_image_read: opaque pointer not NULL");
1175
0
}
1176
1177
/* Utility to find the base format of a PNG file from a png_struct. */
1178
static png_uint_32
1179
png_image_format(png_structrp png_ptr)
1180
0
{
1181
0
   png_uint_32 format = 0;
1182
1183
0
   if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1184
0
      format |= PNG_FORMAT_FLAG_COLOR;
1185
1186
0
   if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
1187
0
      format |= PNG_FORMAT_FLAG_ALPHA;
1188
1189
   /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1190
    * sets the png_struct fields; that's all we are interested in here.  The
1191
    * precise interaction with an app call to png_set_tRNS and PNG file reading
1192
    * is unclear.
1193
    */
1194
0
   else if (png_ptr->num_trans > 0)
1195
0
      format |= PNG_FORMAT_FLAG_ALPHA;
1196
1197
0
   if (png_ptr->bit_depth == 16)
1198
0
      format |= PNG_FORMAT_FLAG_LINEAR;
1199
1200
0
   if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) != 0)
1201
0
      format |= PNG_FORMAT_FLAG_COLORMAP;
1202
1203
0
   return format;
1204
0
}
1205
1206
static int
1207
chromaticities_match_sRGB(const png_xy *xy)
1208
0
{
1209
0
#  define sRGB_TOLERANCE 1000
1210
0
   static const png_xy sRGB_xy = /* From ITU-R BT.709-3 */
1211
0
   {
1212
      /* color      x       y */
1213
0
      /* red   */ 64000, 33000,
1214
0
      /* green */ 30000, 60000,
1215
0
      /* blue  */ 15000,  6000,
1216
0
      /* white */ 31270, 32900
1217
0
   };
1218
1219
0
   if (PNG_OUT_OF_RANGE(xy->whitex, sRGB_xy.whitex,sRGB_TOLERANCE) ||
1220
0
       PNG_OUT_OF_RANGE(xy->whitey, sRGB_xy.whitey,sRGB_TOLERANCE) ||
1221
0
       PNG_OUT_OF_RANGE(xy->redx,   sRGB_xy.redx,  sRGB_TOLERANCE) ||
1222
0
       PNG_OUT_OF_RANGE(xy->redy,   sRGB_xy.redy,  sRGB_TOLERANCE) ||
1223
0
       PNG_OUT_OF_RANGE(xy->greenx, sRGB_xy.greenx,sRGB_TOLERANCE) ||
1224
0
       PNG_OUT_OF_RANGE(xy->greeny, sRGB_xy.greeny,sRGB_TOLERANCE) ||
1225
0
       PNG_OUT_OF_RANGE(xy->bluex,  sRGB_xy.bluex, sRGB_TOLERANCE) ||
1226
0
       PNG_OUT_OF_RANGE(xy->bluey,  sRGB_xy.bluey, sRGB_TOLERANCE))
1227
0
      return 0;
1228
0
   return 1;
1229
0
}
1230
1231
/* Is the given gamma significantly different from sRGB?  The test is the same
1232
 * one used in pngrtran.c when deciding whether to do gamma correction.  The
1233
 * arithmetic optimizes the division by using the fact that the inverse of the
1234
 * file sRGB gamma is 2.2
1235
 */
1236
static int
1237
png_gamma_not_sRGB(png_fixed_point g)
1238
0
{
1239
   /* 1.6.47: use the same sanity checks as used in pngrtran.c */
1240
0
   if (g < PNG_LIB_GAMMA_MIN || g > PNG_LIB_GAMMA_MAX)
1241
0
      return 0; /* Includes the uninitialized value 0 */
1242
1243
0
   return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
1244
0
}
1245
1246
/* Do the main body of a 'png_image_begin_read' function; read the PNG file
1247
 * header and fill in all the information.  This is executed in a safe context,
1248
 * unlike the init routine above.
1249
 */
1250
static int
1251
png_image_is_not_sRGB(png_const_structrp png_ptr)
1252
0
{
1253
   /* Does the colorspace **not** match sRGB?  The flag is only set if the
1254
    * answer can be determined reliably.
1255
    *
1256
    * png_struct::chromaticities always exists since the simplified API
1257
    * requires rgb-to-gray.  The mDCV, cICP and cHRM chunks may all set it to
1258
    * a non-sRGB value, so it needs to be checked but **only** if one of
1259
    * those chunks occured in the file.
1260
    */
1261
   /* Highest priority: check to be safe. */
1262
0
   if (png_has_chunk(png_ptr, cICP) || png_has_chunk(png_ptr, mDCV))
1263
0
      return !chromaticities_match_sRGB(&png_ptr->chromaticities);
1264
1265
   /* If the image is marked as sRGB then it is... */
1266
0
   if (png_has_chunk(png_ptr, sRGB))
1267
0
      return 0;
1268
1269
   /* Last stop: cHRM, must check: */
1270
0
   if (png_has_chunk(png_ptr, cHRM))
1271
0
      return !chromaticities_match_sRGB(&png_ptr->chromaticities);
1272
1273
   /* Else default to sRGB */
1274
0
   return 0;
1275
0
}
1276
1277
static int
1278
png_image_read_header(png_voidp argument)
1279
0
{
1280
0
   png_imagep image = png_voidcast(png_imagep, argument);
1281
0
   png_structrp png_ptr = image->opaque->png_ptr;
1282
0
   png_inforp info_ptr = image->opaque->info_ptr;
1283
1284
0
#ifdef PNG_BENIGN_ERRORS_SUPPORTED
1285
0
   png_set_benign_errors(png_ptr, 1/*warn*/);
1286
0
#endif
1287
0
   png_read_info(png_ptr, info_ptr);
1288
1289
   /* Do this the fast way; just read directly out of png_struct. */
1290
0
   image->width = png_ptr->width;
1291
0
   image->height = png_ptr->height;
1292
1293
0
   {
1294
0
      png_uint_32 format = png_image_format(png_ptr);
1295
1296
0
      image->format = format;
1297
1298
      /* Greyscale images don't (typically) have colour space information and
1299
       * using it is pretty much impossible, so use sRGB for grayscale (it
1300
       * doesn't matter r==g==b so the transform is irrelevant.)
1301
       */
1302
0
      if ((format & PNG_FORMAT_FLAG_COLOR) != 0 &&
1303
0
          png_image_is_not_sRGB(png_ptr))
1304
0
         image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1305
0
   }
1306
1307
   /* We need the maximum number of entries regardless of the format the
1308
    * application sets here.
1309
    */
1310
0
   {
1311
0
      png_uint_32 cmap_entries;
1312
1313
0
      switch (png_ptr->color_type)
1314
0
      {
1315
0
         case PNG_COLOR_TYPE_GRAY:
1316
0
            cmap_entries = 1U << png_ptr->bit_depth;
1317
0
            break;
1318
1319
0
         case PNG_COLOR_TYPE_PALETTE:
1320
0
            cmap_entries = (png_uint_32)png_ptr->num_palette;
1321
0
            break;
1322
1323
0
         default:
1324
0
            cmap_entries = 256;
1325
0
            break;
1326
0
      }
1327
1328
0
      if (cmap_entries > 256)
1329
0
         cmap_entries = 256;
1330
1331
0
      image->colormap_entries = cmap_entries;
1332
0
   }
1333
1334
0
   return 1;
1335
0
}
1336
1337
#ifdef PNG_STDIO_SUPPORTED
1338
int PNGAPI
1339
png_image_begin_read_from_stdio(png_imagep image, FILE *file)
1340
0
{
1341
0
   if (image != NULL && image->version == PNG_IMAGE_VERSION)
1342
0
   {
1343
0
      if (file != NULL)
1344
0
      {
1345
0
         if (png_image_read_init(image) != 0)
1346
0
         {
1347
            /* This is slightly evil, but png_init_io doesn't do anything other
1348
             * than this and we haven't changed the standard IO functions so
1349
             * this saves a 'safe' function.
1350
             */
1351
0
            image->opaque->png_ptr->io_ptr = file;
1352
0
            return png_safe_execute(image, png_image_read_header, image);
1353
0
         }
1354
0
      }
1355
1356
0
      else
1357
0
         return png_image_error(image,
1358
0
             "png_image_begin_read_from_stdio: invalid argument");
1359
0
   }
1360
1361
0
   else if (image != NULL)
1362
0
      return png_image_error(image,
1363
0
          "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
1364
1365
0
   return 0;
1366
0
}
1367
1368
int PNGAPI
1369
png_image_begin_read_from_file(png_imagep image, const char *file_name)
1370
0
{
1371
0
   if (image != NULL && image->version == PNG_IMAGE_VERSION)
1372
0
   {
1373
0
      if (file_name != NULL)
1374
0
      {
1375
0
         FILE *fp = fopen(file_name, "rb");
1376
1377
0
         if (fp != NULL)
1378
0
         {
1379
0
            if (png_image_read_init(image) != 0)
1380
0
            {
1381
0
               image->opaque->png_ptr->io_ptr = fp;
1382
0
               image->opaque->owned_file = 1;
1383
0
               return png_safe_execute(image, png_image_read_header, image);
1384
0
            }
1385
1386
            /* Clean up: just the opened file. */
1387
0
            (void)fclose(fp);
1388
0
         }
1389
1390
0
         else
1391
0
            return png_image_error(image, strerror(errno));
1392
0
      }
1393
1394
0
      else
1395
0
         return png_image_error(image,
1396
0
             "png_image_begin_read_from_file: invalid argument");
1397
0
   }
1398
1399
0
   else if (image != NULL)
1400
0
      return png_image_error(image,
1401
0
          "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
1402
1403
0
   return 0;
1404
0
}
1405
#endif /* STDIO */
1406
1407
static void PNGCBAPI
1408
png_image_memory_read(png_structp png_ptr, png_bytep out, size_t need)
1409
0
{
1410
0
   if (png_ptr != NULL)
1411
0
   {
1412
0
      png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
1413
0
      if (image != NULL)
1414
0
      {
1415
0
         png_controlp cp = image->opaque;
1416
0
         if (cp != NULL)
1417
0
         {
1418
0
            png_const_bytep memory = cp->memory;
1419
0
            size_t size = cp->size;
1420
1421
0
            if (memory != NULL && size >= need)
1422
0
            {
1423
0
               memcpy(out, memory, need);
1424
0
               cp->memory = memory + need;
1425
0
               cp->size = size - need;
1426
0
               return;
1427
0
            }
1428
1429
0
            png_error(png_ptr, "read beyond end of data");
1430
0
         }
1431
0
      }
1432
1433
0
      png_error(png_ptr, "invalid memory read");
1434
0
   }
1435
0
}
1436
1437
int PNGAPI png_image_begin_read_from_memory(png_imagep image,
1438
    png_const_voidp memory, size_t size)
1439
0
{
1440
0
   if (image != NULL && image->version == PNG_IMAGE_VERSION)
1441
0
   {
1442
0
      if (memory != NULL && size > 0)
1443
0
      {
1444
0
         if (png_image_read_init(image) != 0)
1445
0
         {
1446
            /* Now set the IO functions to read from the memory buffer and
1447
             * store it into io_ptr.  Again do this in-place to avoid calling a
1448
             * libpng function that requires error handling.
1449
             */
1450
0
            image->opaque->memory = png_voidcast(png_const_bytep, memory);
1451
0
            image->opaque->size = size;
1452
0
            image->opaque->png_ptr->io_ptr = image;
1453
0
            image->opaque->png_ptr->read_data_fn = png_image_memory_read;
1454
1455
0
            return png_safe_execute(image, png_image_read_header, image);
1456
0
         }
1457
0
      }
1458
1459
0
      else
1460
0
         return png_image_error(image,
1461
0
             "png_image_begin_read_from_memory: invalid argument");
1462
0
   }
1463
1464
0
   else if (image != NULL)
1465
0
      return png_image_error(image,
1466
0
          "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
1467
1468
0
   return 0;
1469
0
}
1470
1471
/* Utility function to skip chunks that are not used by the simplified image
1472
 * read functions and an appropriate macro to call it.
1473
 */
1474
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1475
static void
1476
png_image_skip_unused_chunks(png_structrp png_ptr)
1477
0
{
1478
   /* Prepare the reader to ignore all recognized chunks whose data will not
1479
    * be used, i.e., all chunks recognized by libpng except for those
1480
    * involved in basic image reading:
1481
    *
1482
    *    IHDR, PLTE, IDAT, IEND
1483
    *
1484
    * Or image data handling:
1485
    *
1486
    *    tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
1487
    *
1488
    * This provides a small performance improvement and eliminates any
1489
    * potential vulnerability to security problems in the unused chunks.
1490
    *
1491
    * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
1492
    * too.  This allows the simplified API to be compiled without iCCP support.
1493
    */
1494
0
   {
1495
0
         static const png_byte chunks_to_process[] = {
1496
0
            98,  75,  71,  68, '\0',  /* bKGD */
1497
0
            99,  72,  82,  77, '\0',  /* cHRM */
1498
0
            99,  73,  67,  80, '\0',  /* cICP */
1499
0
           103,  65,  77,  65, '\0',  /* gAMA */
1500
0
           109,  68,  67,  86, '\0',  /* mDCV */
1501
0
           115,  66,  73,  84, '\0',  /* sBIT */
1502
0
           115,  82,  71,  66, '\0',  /* sRGB */
1503
0
         };
1504
1505
       /* Ignore unknown chunks and all other chunks except for the
1506
        * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
1507
        */
1508
0
       png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER,
1509
0
           NULL, -1);
1510
1511
       /* But do not ignore image data handling chunks */
1512
0
       png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT,
1513
0
           chunks_to_process, (int)/*SAFE*/(sizeof chunks_to_process)/5);
1514
0
   }
1515
0
}
1516
1517
0
#  define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1518
#else
1519
#  define PNG_SKIP_CHUNKS(p) ((void)0)
1520
#endif /* HANDLE_AS_UNKNOWN */
1521
1522
/* The following macro gives the exact rounded answer for all values in the
1523
 * range 0..255 (it actually divides by 51.2, but the rounding still generates
1524
 * the correct numbers 0..5
1525
 */
1526
0
#define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1527
1528
/* Utility functions to make particular color-maps */
1529
static void
1530
set_file_encoding(png_image_read_control *display)
1531
0
{
1532
0
   png_structrp png_ptr = display->image->opaque->png_ptr;
1533
0
   png_fixed_point g = png_resolve_file_gamma(png_ptr);
1534
1535
   /* PNGv3: the result may be 0 however the 'default_gamma' should have been
1536
    * set before this is called so zero is an error:
1537
    */
1538
0
   if (g == 0)
1539
0
      png_error(png_ptr, "internal: default gamma not set");
1540
1541
0
   if (png_gamma_significant(g) != 0)
1542
0
   {
1543
0
      if (png_gamma_not_sRGB(g) != 0)
1544
0
      {
1545
0
         display->file_encoding = P_FILE;
1546
0
         display->gamma_to_linear = png_reciprocal(g);
1547
0
      }
1548
1549
0
      else
1550
0
         display->file_encoding = P_sRGB;
1551
0
   }
1552
1553
0
   else
1554
0
      display->file_encoding = P_LINEAR8;
1555
0
}
1556
1557
static unsigned int
1558
decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
1559
0
{
1560
0
   if (encoding == P_FILE) /* double check */
1561
0
      encoding = display->file_encoding;
1562
1563
0
   if (encoding == P_NOTSET) /* must be the file encoding */
1564
0
   {
1565
0
      set_file_encoding(display);
1566
0
      encoding = display->file_encoding;
1567
0
   }
1568
1569
0
   switch (encoding)
1570
0
   {
1571
0
      case P_FILE:
1572
0
         value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
1573
0
         break;
1574
1575
0
      case P_sRGB:
1576
0
         value = png_sRGB_table[value];
1577
0
         break;
1578
1579
0
      case P_LINEAR:
1580
0
         break;
1581
1582
0
      case P_LINEAR8:
1583
0
         value *= 257;
1584
0
         break;
1585
1586
0
#ifdef __GNUC__
1587
0
      default:
1588
0
         png_error(display->image->opaque->png_ptr,
1589
0
             "unexpected encoding (internal error)");
1590
0
#endif
1591
0
   }
1592
1593
0
   return value;
1594
0
}
1595
1596
static png_uint_32
1597
png_colormap_compose(png_image_read_control *display,
1598
    png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
1599
    png_uint_32 background, int encoding)
1600
0
{
1601
   /* The file value is composed on the background, the background has the given
1602
    * encoding and so does the result, the file is encoded with P_FILE and the
1603
    * file and alpha are 8-bit values.  The (output) encoding will always be
1604
    * P_LINEAR or P_sRGB.
1605
    */
1606
0
   png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
1607
0
   png_uint_32 b = decode_gamma(display, background, encoding);
1608
1609
   /* The alpha is always an 8-bit value (it comes from the palette), the value
1610
    * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1611
    */
1612
0
   f = f * alpha + b * (255-alpha);
1613
1614
0
   if (encoding == P_LINEAR)
1615
0
   {
1616
      /* Scale to 65535; divide by 255, approximately (in fact this is extremely
1617
       * accurate, it divides by 255.00000005937181414556, with no overflow.)
1618
       */
1619
0
      f *= 257; /* Now scaled by 65535 */
1620
0
      f += f >> 16;
1621
0
      f = (f+32768) >> 16;
1622
0
   }
1623
1624
0
   else /* P_sRGB */
1625
0
      f = PNG_sRGB_FROM_LINEAR(f);
1626
1627
0
   return f;
1628
0
}
1629
1630
/* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
1631
 * be 8-bit.
1632
 */
1633
static void
1634
png_create_colormap_entry(png_image_read_control *display,
1635
    png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
1636
    png_uint_32 alpha, int encoding)
1637
0
{
1638
0
   png_imagep image = display->image;
1639
0
   int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
1640
0
       P_LINEAR : P_sRGB;
1641
0
   int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
1642
0
       (red != green || green != blue);
1643
1644
0
   if (ip > 255)
1645
0
      png_error(image->opaque->png_ptr, "color-map index out of range");
1646
1647
   /* Update the cache with whether the file gamma is significantly different
1648
    * from sRGB.
1649
    */
1650
0
   if (encoding == P_FILE)
1651
0
   {
1652
0
      if (display->file_encoding == P_NOTSET)
1653
0
         set_file_encoding(display);
1654
1655
      /* Note that the cached value may be P_FILE too, but if it is then the
1656
       * gamma_to_linear member has been set.
1657
       */
1658
0
      encoding = display->file_encoding;
1659
0
   }
1660
1661
0
   if (encoding == P_FILE)
1662
0
   {
1663
0
      png_fixed_point g = display->gamma_to_linear;
1664
1665
0
      red = png_gamma_16bit_correct(red*257, g);
1666
0
      green = png_gamma_16bit_correct(green*257, g);
1667
0
      blue = png_gamma_16bit_correct(blue*257, g);
1668
1669
0
      if (convert_to_Y != 0 || output_encoding == P_LINEAR)
1670
0
      {
1671
0
         alpha *= 257;
1672
0
         encoding = P_LINEAR;
1673
0
      }
1674
1675
0
      else
1676
0
      {
1677
0
         red = PNG_sRGB_FROM_LINEAR(red * 255);
1678
0
         green = PNG_sRGB_FROM_LINEAR(green * 255);
1679
0
         blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1680
0
         encoding = P_sRGB;
1681
0
      }
1682
0
   }
1683
1684
0
   else if (encoding == P_LINEAR8)
1685
0
   {
1686
      /* This encoding occurs quite frequently in test cases because PngSuite
1687
       * includes a gAMA 1.0 chunk with most images.
1688
       */
1689
0
      red *= 257;
1690
0
      green *= 257;
1691
0
      blue *= 257;
1692
0
      alpha *= 257;
1693
0
      encoding = P_LINEAR;
1694
0
   }
1695
1696
0
   else if (encoding == P_sRGB &&
1697
0
       (convert_to_Y  != 0 || output_encoding == P_LINEAR))
1698
0
   {
1699
      /* The values are 8-bit sRGB values, but must be converted to 16-bit
1700
       * linear.
1701
       */
1702
0
      red = png_sRGB_table[red];
1703
0
      green = png_sRGB_table[green];
1704
0
      blue = png_sRGB_table[blue];
1705
0
      alpha *= 257;
1706
0
      encoding = P_LINEAR;
1707
0
   }
1708
1709
   /* This is set if the color isn't gray but the output is. */
1710
0
   if (encoding == P_LINEAR)
1711
0
   {
1712
0
      if (convert_to_Y != 0)
1713
0
      {
1714
         /* NOTE: these values are copied from png_do_rgb_to_gray */
1715
0
         png_uint_32 y = (png_uint_32)6968 * red  + (png_uint_32)23434 * green +
1716
0
            (png_uint_32)2366 * blue;
1717
1718
0
         if (output_encoding == P_LINEAR)
1719
0
            y = (y + 16384) >> 15;
1720
1721
0
         else
1722
0
         {
1723
            /* y is scaled by 32768, we need it scaled by 255: */
1724
0
            y = (y + 128) >> 8;
1725
0
            y *= 255;
1726
0
            y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
1727
0
            alpha = PNG_DIV257(alpha);
1728
0
            encoding = P_sRGB;
1729
0
         }
1730
1731
0
         blue = red = green = y;
1732
0
      }
1733
1734
0
      else if (output_encoding == P_sRGB)
1735
0
      {
1736
0
         red = PNG_sRGB_FROM_LINEAR(red * 255);
1737
0
         green = PNG_sRGB_FROM_LINEAR(green * 255);
1738
0
         blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1739
0
         alpha = PNG_DIV257(alpha);
1740
0
         encoding = P_sRGB;
1741
0
      }
1742
0
   }
1743
1744
0
   if (encoding != output_encoding)
1745
0
      png_error(image->opaque->png_ptr, "bad encoding (internal error)");
1746
1747
   /* Store the value. */
1748
0
   {
1749
0
#     ifdef PNG_FORMAT_AFIRST_SUPPORTED
1750
0
         int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1751
0
            (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
1752
#     else
1753
#        define afirst 0
1754
#     endif
1755
0
#     ifdef PNG_FORMAT_BGR_SUPPORTED
1756
0
         int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
1757
#     else
1758
#        define bgr 0
1759
#     endif
1760
1761
0
      if (output_encoding == P_LINEAR)
1762
0
      {
1763
0
         png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
1764
1765
0
         entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1766
1767
         /* The linear 16-bit values must be pre-multiplied by the alpha channel
1768
          * value, if less than 65535 (this is, effectively, composite on black
1769
          * if the alpha channel is removed.)
1770
          */
1771
0
         switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1772
0
         {
1773
0
            case 4:
1774
0
               entry[afirst ? 0 : 3] = (png_uint_16)alpha;
1775
               /* FALLTHROUGH */
1776
1777
0
            case 3:
1778
0
               if (alpha < 65535)
1779
0
               {
1780
0
                  if (alpha > 0)
1781
0
                  {
1782
0
                     blue = (blue * alpha + 32767U)/65535U;
1783
0
                     green = (green * alpha + 32767U)/65535U;
1784
0
                     red = (red * alpha + 32767U)/65535U;
1785
0
                  }
1786
1787
0
                  else
1788
0
                     red = green = blue = 0;
1789
0
               }
1790
0
               entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
1791
0
               entry[afirst + 1] = (png_uint_16)green;
1792
0
               entry[afirst + bgr] = (png_uint_16)red;
1793
0
               break;
1794
1795
0
            case 2:
1796
0
               entry[1 ^ afirst] = (png_uint_16)alpha;
1797
               /* FALLTHROUGH */
1798
1799
0
            case 1:
1800
0
               if (alpha < 65535)
1801
0
               {
1802
0
                  if (alpha > 0)
1803
0
                     green = (green * alpha + 32767U)/65535U;
1804
1805
0
                  else
1806
0
                     green = 0;
1807
0
               }
1808
0
               entry[afirst] = (png_uint_16)green;
1809
0
               break;
1810
1811
0
            default:
1812
0
               break;
1813
0
         }
1814
0
      }
1815
1816
0
      else /* output encoding is P_sRGB */
1817
0
      {
1818
0
         png_bytep entry = png_voidcast(png_bytep, display->colormap);
1819
1820
0
         entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1821
1822
0
         switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1823
0
         {
1824
0
            case 4:
1825
0
               entry[afirst ? 0 : 3] = (png_byte)alpha;
1826
               /* FALLTHROUGH */
1827
0
            case 3:
1828
0
               entry[afirst + (2 ^ bgr)] = (png_byte)blue;
1829
0
               entry[afirst + 1] = (png_byte)green;
1830
0
               entry[afirst + bgr] = (png_byte)red;
1831
0
               break;
1832
1833
0
            case 2:
1834
0
               entry[1 ^ afirst] = (png_byte)alpha;
1835
               /* FALLTHROUGH */
1836
0
            case 1:
1837
0
               entry[afirst] = (png_byte)green;
1838
0
               break;
1839
1840
0
            default:
1841
0
               break;
1842
0
         }
1843
0
      }
1844
1845
#     ifdef afirst
1846
#        undef afirst
1847
#     endif
1848
#     ifdef bgr
1849
#        undef bgr
1850
#     endif
1851
0
   }
1852
0
}
1853
1854
static int
1855
make_gray_file_colormap(png_image_read_control *display)
1856
0
{
1857
0
   unsigned int i;
1858
1859
0
   for (i=0; i<256; ++i)
1860
0
      png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
1861
1862
0
   return (int)i;
1863
0
}
1864
1865
static int
1866
make_gray_colormap(png_image_read_control *display)
1867
0
{
1868
0
   unsigned int i;
1869
1870
0
   for (i=0; i<256; ++i)
1871
0
      png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
1872
1873
0
   return (int)i;
1874
0
}
1875
0
#define PNG_GRAY_COLORMAP_ENTRIES 256
1876
1877
static int
1878
make_ga_colormap(png_image_read_control *display)
1879
0
{
1880
0
   unsigned int i, a;
1881
1882
   /* Alpha is retained, the output will be a color-map with entries
1883
    * selected by six levels of alpha.  One transparent entry, 6 gray
1884
    * levels for all the intermediate alpha values, leaving 230 entries
1885
    * for the opaque grays.  The color-map entries are the six values
1886
    * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
1887
    * relevant entry.
1888
    *
1889
    * if (alpha > 229) // opaque
1890
    * {
1891
    *    // The 231 entries are selected to make the math below work:
1892
    *    base = 0;
1893
    *    entry = (231 * gray + 128) >> 8;
1894
    * }
1895
    * else if (alpha < 26) // transparent
1896
    * {
1897
    *    base = 231;
1898
    *    entry = 0;
1899
    * }
1900
    * else // partially opaque
1901
    * {
1902
    *    base = 226 + 6 * PNG_DIV51(alpha);
1903
    *    entry = PNG_DIV51(gray);
1904
    * }
1905
    */
1906
0
   i = 0;
1907
0
   while (i < 231)
1908
0
   {
1909
0
      unsigned int gray = (i * 256 + 115) / 231;
1910
0
      png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
1911
0
   }
1912
1913
   /* 255 is used here for the component values for consistency with the code
1914
    * that undoes premultiplication in pngwrite.c.
1915
    */
1916
0
   png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
1917
1918
0
   for (a=1; a<5; ++a)
1919
0
   {
1920
0
      unsigned int g;
1921
1922
0
      for (g=0; g<6; ++g)
1923
0
         png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
1924
0
             P_sRGB);
1925
0
   }
1926
1927
0
   return (int)i;
1928
0
}
1929
1930
0
#define PNG_GA_COLORMAP_ENTRIES 256
1931
1932
static int
1933
make_rgb_colormap(png_image_read_control *display)
1934
0
{
1935
0
   unsigned int i, r;
1936
1937
   /* Build a 6x6x6 opaque RGB cube */
1938
0
   for (i=r=0; r<6; ++r)
1939
0
   {
1940
0
      unsigned int g;
1941
1942
0
      for (g=0; g<6; ++g)
1943
0
      {
1944
0
         unsigned int b;
1945
1946
0
         for (b=0; b<6; ++b)
1947
0
            png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
1948
0
                P_sRGB);
1949
0
      }
1950
0
   }
1951
1952
0
   return (int)i;
1953
0
}
1954
1955
0
#define PNG_RGB_COLORMAP_ENTRIES 216
1956
1957
/* Return a palette index to the above palette given three 8-bit sRGB values. */
1958
#define PNG_RGB_INDEX(r,g,b) \
1959
0
   ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
1960
1961
static int
1962
png_image_read_colormap(png_voidp argument)
1963
0
{
1964
0
   png_image_read_control *display =
1965
0
      png_voidcast(png_image_read_control*, argument);
1966
0
   png_imagep image = display->image;
1967
1968
0
   png_structrp png_ptr = image->opaque->png_ptr;
1969
0
   png_uint_32 output_format = image->format;
1970
0
   int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
1971
0
      P_LINEAR : P_sRGB;
1972
1973
0
   unsigned int cmap_entries;
1974
0
   unsigned int output_processing;        /* Output processing option */
1975
0
   unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */
1976
1977
   /* Background information; the background color and the index of this color
1978
    * in the color-map if it exists (else 256).
1979
    */
1980
0
   unsigned int background_index = 256;
1981
0
   png_uint_32 back_r, back_g, back_b;
1982
1983
   /* Flags to accumulate things that need to be done to the input. */
1984
0
   int expand_tRNS = 0;
1985
1986
   /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
1987
    * very difficult to do, the results look awful, and it is difficult to see
1988
    * what possible use it is because the application can't control the
1989
    * color-map.
1990
    */
1991
0
   if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
1992
0
         png_ptr->num_trans > 0) /* alpha in input */ &&
1993
0
      ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
1994
0
   {
1995
0
      if (output_encoding == P_LINEAR) /* compose on black */
1996
0
         back_b = back_g = back_r = 0;
1997
1998
0
      else if (display->background == NULL /* no way to remove it */)
1999
0
         png_error(png_ptr,
2000
0
             "background color must be supplied to remove alpha/transparency");
2001
2002
      /* Get a copy of the background color (this avoids repeating the checks
2003
       * below.)  The encoding is 8-bit sRGB or 16-bit linear, depending on the
2004
       * output format.
2005
       */
2006
0
      else
2007
0
      {
2008
0
         back_g = display->background->green;
2009
0
         if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0)
2010
0
         {
2011
0
            back_r = display->background->red;
2012
0
            back_b = display->background->blue;
2013
0
         }
2014
0
         else
2015
0
            back_b = back_r = back_g;
2016
0
      }
2017
0
   }
2018
2019
0
   else if (output_encoding == P_LINEAR)
2020
0
      back_b = back_r = back_g = 65535;
2021
2022
0
   else
2023
0
      back_b = back_r = back_g = 255;
2024
2025
   /* Default the input file gamma if required - this is necessary because
2026
    * libpng assumes that if no gamma information is present the data is in the
2027
    * output format, but the simplified API deduces the gamma from the input
2028
    * format.  The 'default' gamma value is also set by png_set_alpha_mode, but
2029
    * this is happening before any such call, so:
2030
    *
2031
    * TODO: should be an internal API and all this code should be copied into a
2032
    * single common gamma+colorspace file.
2033
    */
2034
0
   if (png_ptr->bit_depth == 16 &&
2035
0
      (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
2036
0
      png_ptr->default_gamma = PNG_GAMMA_LINEAR;
2037
2038
0
   else
2039
0
      png_ptr->default_gamma = PNG_GAMMA_sRGB_INVERSE;
2040
2041
   /* Decide what to do based on the PNG color type of the input data.  The
2042
    * utility function png_create_colormap_entry deals with most aspects of the
2043
    * output transformations; this code works out how to produce bytes of
2044
    * color-map entries from the original format.
2045
    */
2046
0
   switch (png_ptr->color_type)
2047
0
   {
2048
0
      case PNG_COLOR_TYPE_GRAY:
2049
0
         if (png_ptr->bit_depth <= 8)
2050
0
         {
2051
            /* There at most 256 colors in the output, regardless of
2052
             * transparency.
2053
             */
2054
0
            unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
2055
2056
0
            cmap_entries = 1U << png_ptr->bit_depth;
2057
0
            if (cmap_entries > image->colormap_entries)
2058
0
               png_error(png_ptr, "gray[8] color-map: too few entries");
2059
2060
0
            step = 255 / (cmap_entries - 1);
2061
0
            output_processing = PNG_CMAP_NONE;
2062
2063
            /* If there is a tRNS chunk then this either selects a transparent
2064
             * value or, if the output has no alpha, the background color.
2065
             */
2066
0
            if (png_ptr->num_trans > 0)
2067
0
            {
2068
0
               trans = png_ptr->trans_color.gray;
2069
2070
0
               if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
2071
0
                  back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
2072
0
            }
2073
2074
            /* png_create_colormap_entry just takes an RGBA and writes the
2075
             * corresponding color-map entry using the format from 'image',
2076
             * including the required conversion to sRGB or linear as
2077
             * appropriate.  The input values are always either sRGB (if the
2078
             * gamma correction flag is 0) or 0..255 scaled file encoded values
2079
             * (if the function must gamma correct them).
2080
             */
2081
0
            for (i=val=0; i<cmap_entries; ++i, val += step)
2082
0
            {
2083
               /* 'i' is a file value.  While this will result in duplicated
2084
                * entries for 8-bit non-sRGB encoded files it is necessary to
2085
                * have non-gamma corrected values to do tRNS handling.
2086
                */
2087
0
               if (i != trans)
2088
0
                  png_create_colormap_entry(display, i, val, val, val, 255,
2089
0
                      P_FILE/*8-bit with file gamma*/);
2090
2091
               /* Else this entry is transparent.  The colors don't matter if
2092
                * there is an alpha channel (back_alpha == 0), but it does no
2093
                * harm to pass them in; the values are not set above so this
2094
                * passes in white.
2095
                *
2096
                * NOTE: this preserves the full precision of the application
2097
                * supplied background color when it is used.
2098
                */
2099
0
               else
2100
0
                  png_create_colormap_entry(display, i, back_r, back_g, back_b,
2101
0
                      back_alpha, output_encoding);
2102
0
            }
2103
2104
            /* We need libpng to preserve the original encoding. */
2105
0
            data_encoding = P_FILE;
2106
2107
            /* The rows from libpng, while technically gray values, are now also
2108
             * color-map indices; however, they may need to be expanded to 1
2109
             * byte per pixel.  This is what png_set_packing does (i.e., it
2110
             * unpacks the bit values into bytes.)
2111
             */
2112
0
            if (png_ptr->bit_depth < 8)
2113
0
               png_set_packing(png_ptr);
2114
0
         }
2115
2116
0
         else /* bit depth is 16 */
2117
0
         {
2118
            /* The 16-bit input values can be converted directly to 8-bit gamma
2119
             * encoded values; however, if a tRNS chunk is present 257 color-map
2120
             * entries are required.  This means that the extra entry requires
2121
             * special processing; add an alpha channel, sacrifice gray level
2122
             * 254 and convert transparent (alpha==0) entries to that.
2123
             *
2124
             * Use libpng to chop the data to 8 bits.  Convert it to sRGB at the
2125
             * same time to minimize quality loss.  If a tRNS chunk is present
2126
             * this means libpng must handle it too; otherwise it is impossible
2127
             * to do the exact match on the 16-bit value.
2128
             *
2129
             * If the output has no alpha channel *and* the background color is
2130
             * gray then it is possible to let libpng handle the substitution by
2131
             * ensuring that the corresponding gray level matches the background
2132
             * color exactly.
2133
             */
2134
0
            data_encoding = P_sRGB;
2135
2136
0
            if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2137
0
               png_error(png_ptr, "gray[16] color-map: too few entries");
2138
2139
0
            cmap_entries = (unsigned int)make_gray_colormap(display);
2140
2141
0
            if (png_ptr->num_trans > 0)
2142
0
            {
2143
0
               unsigned int back_alpha;
2144
2145
0
               if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2146
0
                  back_alpha = 0;
2147
2148
0
               else
2149
0
               {
2150
0
                  if (back_r == back_g && back_g == back_b)
2151
0
                  {
2152
                     /* Background is gray; no special processing will be
2153
                      * required.
2154
                      */
2155
0
                     png_color_16 c;
2156
0
                     png_uint_32 gray = back_g;
2157
2158
0
                     if (output_encoding == P_LINEAR)
2159
0
                     {
2160
0
                        gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2161
2162
                        /* And make sure the corresponding palette entry
2163
                         * matches.
2164
                         */
2165
0
                        png_create_colormap_entry(display, gray, back_g, back_g,
2166
0
                            back_g, 65535, P_LINEAR);
2167
0
                     }
2168
2169
                     /* The background passed to libpng, however, must be the
2170
                      * sRGB value.
2171
                      */
2172
0
                     c.index = 0; /*unused*/
2173
0
                     c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2174
2175
                     /* NOTE: does this work without expanding tRNS to alpha?
2176
                      * It should be the color->gray case below apparently
2177
                      * doesn't.
2178
                      */
2179
0
                     png_set_background_fixed(png_ptr, &c,
2180
0
                         PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2181
0
                         0/*gamma: not used*/);
2182
2183
0
                     output_processing = PNG_CMAP_NONE;
2184
0
                     break;
2185
0
                  }
2186
#ifdef __COVERITY__
2187
                 /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
2188
                  * here.
2189
                  */
2190
                  back_alpha = 255;
2191
#else
2192
0
                  back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
2193
0
#endif
2194
0
               }
2195
2196
               /* output_processing means that the libpng-processed row will be
2197
                * 8-bit GA and it has to be processing to single byte color-map
2198
                * values.  Entry 254 is replaced by either a completely
2199
                * transparent entry or by the background color at full
2200
                * precision (and the background color is not a simple gray
2201
                * level in this case.)
2202
                */
2203
0
               expand_tRNS = 1;
2204
0
               output_processing = PNG_CMAP_TRANS;
2205
0
               background_index = 254;
2206
2207
               /* And set (overwrite) color-map entry 254 to the actual
2208
                * background color at full precision.
2209
                */
2210
0
               png_create_colormap_entry(display, 254, back_r, back_g, back_b,
2211
0
                   back_alpha, output_encoding);
2212
0
            }
2213
2214
0
            else
2215
0
               output_processing = PNG_CMAP_NONE;
2216
0
         }
2217
0
         break;
2218
2219
0
      case PNG_COLOR_TYPE_GRAY_ALPHA:
2220
         /* 8-bit or 16-bit PNG with two channels - gray and alpha.  A minimum
2221
          * of 65536 combinations.  If, however, the alpha channel is to be
2222
          * removed there are only 256 possibilities if the background is gray.
2223
          * (Otherwise there is a subset of the 65536 possibilities defined by
2224
          * the triangle between black, white and the background color.)
2225
          *
2226
          * Reduce 16-bit files to 8-bit and sRGB encode the result.  No need to
2227
          * worry about tRNS matching - tRNS is ignored if there is an alpha
2228
          * channel.
2229
          */
2230
0
         data_encoding = P_sRGB;
2231
2232
0
         if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2233
0
         {
2234
0
            if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2235
0
               png_error(png_ptr, "gray+alpha color-map: too few entries");
2236
2237
0
            cmap_entries = (unsigned int)make_ga_colormap(display);
2238
2239
0
            background_index = PNG_CMAP_GA_BACKGROUND;
2240
0
            output_processing = PNG_CMAP_GA;
2241
0
         }
2242
2243
0
         else /* alpha is removed */
2244
0
         {
2245
            /* Alpha must be removed as the PNG data is processed when the
2246
             * background is a color because the G and A channels are
2247
             * independent and the vector addition (non-parallel vectors) is a
2248
             * 2-D problem.
2249
             *
2250
             * This can be reduced to the same algorithm as above by making a
2251
             * colormap containing gray levels (for the opaque grays), a
2252
             * background entry (for a transparent pixel) and a set of four six
2253
             * level color values, one set for each intermediate alpha value.
2254
             * See the comments in make_ga_colormap for how this works in the
2255
             * per-pixel processing.
2256
             *
2257
             * If the background is gray, however, we only need a 256 entry gray
2258
             * level color map.  It is sufficient to make the entry generated
2259
             * for the background color be exactly the color specified.
2260
             */
2261
0
            if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
2262
0
               (back_r == back_g && back_g == back_b))
2263
0
            {
2264
               /* Background is gray; no special processing will be required. */
2265
0
               png_color_16 c;
2266
0
               png_uint_32 gray = back_g;
2267
2268
0
               if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2269
0
                  png_error(png_ptr, "gray-alpha color-map: too few entries");
2270
2271
0
               cmap_entries = (unsigned int)make_gray_colormap(display);
2272
2273
0
               if (output_encoding == P_LINEAR)
2274
0
               {
2275
0
                  gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2276
2277
                  /* And make sure the corresponding palette entry matches. */
2278
0
                  png_create_colormap_entry(display, gray, back_g, back_g,
2279
0
                      back_g, 65535, P_LINEAR);
2280
0
               }
2281
2282
               /* The background passed to libpng, however, must be the sRGB
2283
                * value.
2284
                */
2285
0
               c.index = 0; /*unused*/
2286
0
               c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2287
2288
0
               png_set_background_fixed(png_ptr, &c,
2289
0
                   PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2290
0
                   0/*gamma: not used*/);
2291
2292
0
               output_processing = PNG_CMAP_NONE;
2293
0
            }
2294
2295
0
            else
2296
0
            {
2297
0
               png_uint_32 i, a;
2298
2299
               /* This is the same as png_make_ga_colormap, above, except that
2300
                * the entries are all opaque.
2301
                */
2302
0
               if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2303
0
                  png_error(png_ptr, "ga-alpha color-map: too few entries");
2304
2305
0
               i = 0;
2306
0
               while (i < 231)
2307
0
               {
2308
0
                  png_uint_32 gray = (i * 256 + 115) / 231;
2309
0
                  png_create_colormap_entry(display, i++, gray, gray, gray,
2310
0
                      255, P_sRGB);
2311
0
               }
2312
2313
               /* NOTE: this preserves the full precision of the application
2314
                * background color.
2315
                */
2316
0
               background_index = i;
2317
0
               png_create_colormap_entry(display, i++, back_r, back_g, back_b,
2318
#ifdef __COVERITY__
2319
                   /* Coverity claims that output_encoding
2320
                    * cannot be 2 (P_LINEAR) here.
2321
                    */ 255U,
2322
#else
2323
0
                    output_encoding == P_LINEAR ? 65535U : 255U,
2324
0
#endif
2325
0
                    output_encoding);
2326
2327
               /* For non-opaque input composite on the sRGB background - this
2328
                * requires inverting the encoding for each component.  The input
2329
                * is still converted to the sRGB encoding because this is a
2330
                * reasonable approximate to the logarithmic curve of human
2331
                * visual sensitivity, at least over the narrow range which PNG
2332
                * represents.  Consequently 'G' is always sRGB encoded, while
2333
                * 'A' is linear.  We need the linear background colors.
2334
                */
2335
0
               if (output_encoding == P_sRGB) /* else already linear */
2336
0
               {
2337
                  /* This may produce a value not exactly matching the
2338
                   * background, but that's ok because these numbers are only
2339
                   * used when alpha != 0
2340
                   */
2341
0
                  back_r = png_sRGB_table[back_r];
2342
0
                  back_g = png_sRGB_table[back_g];
2343
0
                  back_b = png_sRGB_table[back_b];
2344
0
               }
2345
2346
0
               for (a=1; a<5; ++a)
2347
0
               {
2348
0
                  unsigned int g;
2349
2350
                  /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2351
                   * by an 8-bit alpha value (0..255).
2352
                   */
2353
0
                  png_uint_32 alpha = 51 * a;
2354
0
                  png_uint_32 back_rx = (255-alpha) * back_r;
2355
0
                  png_uint_32 back_gx = (255-alpha) * back_g;
2356
0
                  png_uint_32 back_bx = (255-alpha) * back_b;
2357
2358
0
                  for (g=0; g<6; ++g)
2359
0
                  {
2360
0
                     png_uint_32 gray = png_sRGB_table[g*51] * alpha;
2361
2362
0
                     png_create_colormap_entry(display, i++,
2363
0
                         PNG_sRGB_FROM_LINEAR(gray + back_rx),
2364
0
                         PNG_sRGB_FROM_LINEAR(gray + back_gx),
2365
0
                         PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB);
2366
0
                  }
2367
0
               }
2368
2369
0
               cmap_entries = i;
2370
0
               output_processing = PNG_CMAP_GA;
2371
0
            }
2372
0
         }
2373
0
         break;
2374
2375
0
      case PNG_COLOR_TYPE_RGB:
2376
0
      case PNG_COLOR_TYPE_RGB_ALPHA:
2377
         /* Exclude the case where the output is gray; we can always handle this
2378
          * with the cases above.
2379
          */
2380
0
         if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
2381
0
         {
2382
            /* The color-map will be grayscale, so we may as well convert the
2383
             * input RGB values to a simple grayscale and use the grayscale
2384
             * code above.
2385
             *
2386
             * NOTE: calling this apparently damages the recognition of the
2387
             * transparent color in background color handling; call
2388
             * png_set_tRNS_to_alpha before png_set_background_fixed.
2389
             */
2390
0
            png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
2391
0
                -1);
2392
0
            data_encoding = P_sRGB;
2393
2394
            /* The output will now be one or two 8-bit gray or gray+alpha
2395
             * channels.  The more complex case arises when the input has alpha.
2396
             */
2397
0
            if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2398
0
               png_ptr->num_trans > 0) &&
2399
0
               (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2400
0
            {
2401
               /* Both input and output have an alpha channel, so no background
2402
                * processing is required; just map the GA bytes to the right
2403
                * color-map entry.
2404
                */
2405
0
               expand_tRNS = 1;
2406
2407
0
               if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2408
0
                  png_error(png_ptr, "rgb[ga] color-map: too few entries");
2409
2410
0
               cmap_entries = (unsigned int)make_ga_colormap(display);
2411
0
               background_index = PNG_CMAP_GA_BACKGROUND;
2412
0
               output_processing = PNG_CMAP_GA;
2413
0
            }
2414
2415
0
            else
2416
0
            {
2417
0
               const png_fixed_point gamma = png_resolve_file_gamma(png_ptr);
2418
2419
               /* Either the input or the output has no alpha channel, so there
2420
                * will be no non-opaque pixels in the color-map; it will just be
2421
                * grayscale.
2422
                */
2423
0
               if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2424
0
                  png_error(png_ptr, "rgb[gray] color-map: too few entries");
2425
2426
               /* Ideally this code would use libpng to do the gamma correction,
2427
                * but if an input alpha channel is to be removed we will hit the
2428
                * libpng bug in gamma+compose+rgb-to-gray (the double gamma
2429
                * correction bug).  Fix this by dropping the gamma correction in
2430
                * this case and doing it in the palette; this will result in
2431
                * duplicate palette entries, but that's better than the
2432
                * alternative of double gamma correction.
2433
                *
2434
                * NOTE: PNGv3: check the resolved result of all the potentially
2435
                * different colour space chunks.
2436
                */
2437
0
               if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2438
0
                  png_ptr->num_trans > 0) &&
2439
0
                  png_gamma_not_sRGB(gamma) != 0)
2440
0
               {
2441
0
                  cmap_entries = (unsigned int)make_gray_file_colormap(display);
2442
0
                  data_encoding = P_FILE;
2443
0
               }
2444
2445
0
               else
2446
0
                  cmap_entries = (unsigned int)make_gray_colormap(display);
2447
2448
               /* But if the input has alpha or transparency it must be removed
2449
                */
2450
0
               if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2451
0
                  png_ptr->num_trans > 0)
2452
0
               {
2453
0
                  png_color_16 c;
2454
0
                  png_uint_32 gray = back_g;
2455
2456
                  /* We need to ensure that the application background exists in
2457
                   * the colormap and that completely transparent pixels map to
2458
                   * it.  Achieve this simply by ensuring that the entry
2459
                   * selected for the background really is the background color.
2460
                   */
2461
0
                  if (data_encoding == P_FILE) /* from the fixup above */
2462
0
                  {
2463
                     /* The app supplied a gray which is in output_encoding, we
2464
                      * need to convert it to a value of the input (P_FILE)
2465
                      * encoding then set this palette entry to the required
2466
                      * output encoding.
2467
                      */
2468
0
                     if (output_encoding == P_sRGB)
2469
0
                        gray = png_sRGB_table[gray]; /* now P_LINEAR */
2470
2471
0
                     gray = PNG_DIV257(png_gamma_16bit_correct(gray, gamma));
2472
                        /* now P_FILE */
2473
2474
                     /* And make sure the corresponding palette entry contains
2475
                      * exactly the required sRGB value.
2476
                      */
2477
0
                     png_create_colormap_entry(display, gray, back_g, back_g,
2478
0
                         back_g, 0/*unused*/, output_encoding);
2479
0
                  }
2480
2481
0
                  else if (output_encoding == P_LINEAR)
2482
0
                  {
2483
0
                     gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2484
2485
                     /* And make sure the corresponding palette entry matches.
2486
                      */
2487
0
                     png_create_colormap_entry(display, gray, back_g, back_g,
2488
0
                        back_g, 0/*unused*/, P_LINEAR);
2489
0
                  }
2490
2491
                  /* The background passed to libpng, however, must be the
2492
                   * output (normally sRGB) value.
2493
                   */
2494
0
                  c.index = 0; /*unused*/
2495
0
                  c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2496
2497
                  /* NOTE: the following is apparently a bug in libpng. Without
2498
                   * it the transparent color recognition in
2499
                   * png_set_background_fixed seems to go wrong.
2500
                   */
2501
0
                  expand_tRNS = 1;
2502
0
                  png_set_background_fixed(png_ptr, &c,
2503
0
                      PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2504
0
                      0/*gamma: not used*/);
2505
0
               }
2506
2507
0
               output_processing = PNG_CMAP_NONE;
2508
0
            }
2509
0
         }
2510
2511
0
         else /* output is color */
2512
0
         {
2513
            /* We could use png_quantize here so long as there is no transparent
2514
             * color or alpha; png_quantize ignores alpha.  Easier overall just
2515
             * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2516
             * Consequently we always want libpng to produce sRGB data.
2517
             */
2518
0
            data_encoding = P_sRGB;
2519
2520
            /* Is there any transparency or alpha? */
2521
0
            if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2522
0
               png_ptr->num_trans > 0)
2523
0
            {
2524
               /* Is there alpha in the output too?  If so all four channels are
2525
                * processed into a special RGB cube with alpha support.
2526
                */
2527
0
               if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2528
0
               {
2529
0
                  png_uint_32 r;
2530
2531
0
                  if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2532
0
                     png_error(png_ptr, "rgb+alpha color-map: too few entries");
2533
2534
0
                  cmap_entries = (unsigned int)make_rgb_colormap(display);
2535
2536
                  /* Add a transparent entry. */
2537
0
                  png_create_colormap_entry(display, cmap_entries, 255, 255,
2538
0
                      255, 0, P_sRGB);
2539
2540
                  /* This is stored as the background index for the processing
2541
                   * algorithm.
2542
                   */
2543
0
                  background_index = cmap_entries++;
2544
2545
                  /* Add 27 r,g,b entries each with alpha 0.5. */
2546
0
                  for (r=0; r<256; r = (r << 1) | 0x7f)
2547
0
                  {
2548
0
                     png_uint_32 g;
2549
2550
0
                     for (g=0; g<256; g = (g << 1) | 0x7f)
2551
0
                     {
2552
0
                        png_uint_32 b;
2553
2554
                        /* This generates components with the values 0, 127 and
2555
                         * 255
2556
                         */
2557
0
                        for (b=0; b<256; b = (b << 1) | 0x7f)
2558
0
                           png_create_colormap_entry(display, cmap_entries++,
2559
0
                               r, g, b, 128, P_sRGB);
2560
0
                     }
2561
0
                  }
2562
2563
0
                  expand_tRNS = 1;
2564
0
                  output_processing = PNG_CMAP_RGB_ALPHA;
2565
0
               }
2566
2567
0
               else
2568
0
               {
2569
                  /* Alpha/transparency must be removed.  The background must
2570
                   * exist in the color map (achieved by setting adding it after
2571
                   * the 666 color-map).  If the standard processing code will
2572
                   * pick up this entry automatically that's all that is
2573
                   * required; libpng can be called to do the background
2574
                   * processing.
2575
                   */
2576
0
                  unsigned int sample_size =
2577
0
                     PNG_IMAGE_SAMPLE_SIZE(output_format);
2578
0
                  png_uint_32 r, g, b; /* sRGB background */
2579
2580
0
                  if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2581
0
                     png_error(png_ptr, "rgb-alpha color-map: too few entries");
2582
2583
0
                  cmap_entries = (unsigned int)make_rgb_colormap(display);
2584
2585
0
                  png_create_colormap_entry(display, cmap_entries, back_r,
2586
0
                      back_g, back_b, 0/*unused*/, output_encoding);
2587
2588
0
                  if (output_encoding == P_LINEAR)
2589
0
                  {
2590
0
                     r = PNG_sRGB_FROM_LINEAR(back_r * 255);
2591
0
                     g = PNG_sRGB_FROM_LINEAR(back_g * 255);
2592
0
                     b = PNG_sRGB_FROM_LINEAR(back_b * 255);
2593
0
                  }
2594
2595
0
                  else
2596
0
                  {
2597
0
                     r = back_r;
2598
0
                     g = back_g;
2599
0
                     b = back_g;
2600
0
                  }
2601
2602
                  /* Compare the newly-created color-map entry with the one the
2603
                   * PNG_CMAP_RGB algorithm will use.  If the two entries don't
2604
                   * match, add the new one and set this as the background
2605
                   * index.
2606
                   */
2607
0
                  if (memcmp((png_const_bytep)display->colormap +
2608
0
                      sample_size * cmap_entries,
2609
0
                      (png_const_bytep)display->colormap +
2610
0
                          sample_size * PNG_RGB_INDEX(r,g,b),
2611
0
                     sample_size) != 0)
2612
0
                  {
2613
                     /* The background color must be added. */
2614
0
                     background_index = cmap_entries++;
2615
2616
                     /* Add 27 r,g,b entries each with created by composing with
2617
                      * the background at alpha 0.5.
2618
                      */
2619
0
                     for (r=0; r<256; r = (r << 1) | 0x7f)
2620
0
                     {
2621
0
                        for (g=0; g<256; g = (g << 1) | 0x7f)
2622
0
                        {
2623
                           /* This generates components with the values 0, 127
2624
                            * and 255
2625
                            */
2626
0
                           for (b=0; b<256; b = (b << 1) | 0x7f)
2627
0
                              png_create_colormap_entry(display, cmap_entries++,
2628
0
                                  png_colormap_compose(display, r, P_sRGB, 128,
2629
0
                                      back_r, output_encoding),
2630
0
                                  png_colormap_compose(display, g, P_sRGB, 128,
2631
0
                                      back_g, output_encoding),
2632
0
                                  png_colormap_compose(display, b, P_sRGB, 128,
2633
0
                                      back_b, output_encoding),
2634
0
                                  0/*unused*/, output_encoding);
2635
0
                        }
2636
0
                     }
2637
2638
0
                     expand_tRNS = 1;
2639
0
                     output_processing = PNG_CMAP_RGB_ALPHA;
2640
0
                  }
2641
2642
0
                  else /* background color is in the standard color-map */
2643
0
                  {
2644
0
                     png_color_16 c;
2645
2646
0
                     c.index = 0; /*unused*/
2647
0
                     c.red = (png_uint_16)back_r;
2648
0
                     c.gray = c.green = (png_uint_16)back_g;
2649
0
                     c.blue = (png_uint_16)back_b;
2650
2651
0
                     png_set_background_fixed(png_ptr, &c,
2652
0
                         PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2653
0
                         0/*gamma: not used*/);
2654
2655
0
                     output_processing = PNG_CMAP_RGB;
2656
0
                  }
2657
0
               }
2658
0
            }
2659
2660
0
            else /* no alpha or transparency in the input */
2661
0
            {
2662
               /* Alpha in the output is irrelevant, simply map the opaque input
2663
                * pixels to the 6x6x6 color-map.
2664
                */
2665
0
               if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
2666
0
                  png_error(png_ptr, "rgb color-map: too few entries");
2667
2668
0
               cmap_entries = (unsigned int)make_rgb_colormap(display);
2669
0
               output_processing = PNG_CMAP_RGB;
2670
0
            }
2671
0
         }
2672
0
         break;
2673
2674
0
      case PNG_COLOR_TYPE_PALETTE:
2675
         /* It's already got a color-map.  It may be necessary to eliminate the
2676
          * tRNS entries though.
2677
          */
2678
0
         {
2679
0
            unsigned int num_trans = png_ptr->num_trans;
2680
0
            png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
2681
0
            png_const_colorp colormap = png_ptr->palette;
2682
0
            int do_background = trans != NULL &&
2683
0
               (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
2684
0
            unsigned int i;
2685
2686
            /* Just in case: */
2687
0
            if (trans == NULL)
2688
0
               num_trans = 0;
2689
2690
0
            output_processing = PNG_CMAP_NONE;
2691
0
            data_encoding = P_FILE; /* Don't change from color-map indices */
2692
0
            cmap_entries = (unsigned int)png_ptr->num_palette;
2693
0
            if (cmap_entries > 256)
2694
0
               cmap_entries = 256;
2695
2696
0
            if (cmap_entries > (unsigned int)image->colormap_entries)
2697
0
               png_error(png_ptr, "palette color-map: too few entries");
2698
2699
0
            for (i=0; i < cmap_entries; ++i)
2700
0
            {
2701
0
               if (do_background != 0 && i < num_trans && trans[i] < 255)
2702
0
               {
2703
0
                  if (trans[i] == 0)
2704
0
                     png_create_colormap_entry(display, i, back_r, back_g,
2705
0
                         back_b, 0, output_encoding);
2706
2707
0
                  else
2708
0
                  {
2709
                     /* Must compose the PNG file color in the color-map entry
2710
                      * on the sRGB color in 'back'.
2711
                      */
2712
0
                     png_create_colormap_entry(display, i,
2713
0
                         png_colormap_compose(display, colormap[i].red,
2714
0
                             P_FILE, trans[i], back_r, output_encoding),
2715
0
                         png_colormap_compose(display, colormap[i].green,
2716
0
                             P_FILE, trans[i], back_g, output_encoding),
2717
0
                         png_colormap_compose(display, colormap[i].blue,
2718
0
                             P_FILE, trans[i], back_b, output_encoding),
2719
0
                         output_encoding == P_LINEAR ? trans[i] * 257U :
2720
0
                             trans[i],
2721
0
                         output_encoding);
2722
0
                  }
2723
0
               }
2724
2725
0
               else
2726
0
                  png_create_colormap_entry(display, i, colormap[i].red,
2727
0
                      colormap[i].green, colormap[i].blue,
2728
0
                      i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
2729
0
            }
2730
2731
            /* The PNG data may have indices packed in fewer than 8 bits, it
2732
             * must be expanded if so.
2733
             */
2734
0
            if (png_ptr->bit_depth < 8)
2735
0
               png_set_packing(png_ptr);
2736
0
         }
2737
0
         break;
2738
2739
0
      default:
2740
0
         png_error(png_ptr, "invalid PNG color type");
2741
         /*NOT REACHED*/
2742
0
   }
2743
2744
   /* Now deal with the output processing */
2745
0
   if (expand_tRNS != 0 && png_ptr->num_trans > 0 &&
2746
0
       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
2747
0
      png_set_tRNS_to_alpha(png_ptr);
2748
2749
0
   switch (data_encoding)
2750
0
   {
2751
0
      case P_sRGB:
2752
         /* Change to 8-bit sRGB */
2753
0
         png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
2754
         /* FALLTHROUGH */
2755
2756
0
      case P_FILE:
2757
0
         if (png_ptr->bit_depth > 8)
2758
0
            png_set_scale_16(png_ptr);
2759
0
         break;
2760
2761
0
#ifdef __GNUC__
2762
0
      default:
2763
0
         png_error(png_ptr, "bad data option (internal error)");
2764
0
#endif
2765
0
   }
2766
2767
0
   if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
2768
0
      png_error(png_ptr, "color map overflow (BAD internal error)");
2769
2770
0
   image->colormap_entries = cmap_entries;
2771
2772
   /* Double check using the recorded background index */
2773
0
   switch (output_processing)
2774
0
   {
2775
0
      case PNG_CMAP_NONE:
2776
0
         if (background_index != PNG_CMAP_NONE_BACKGROUND)
2777
0
            goto bad_background;
2778
0
         break;
2779
2780
0
      case PNG_CMAP_GA:
2781
0
         if (background_index != PNG_CMAP_GA_BACKGROUND)
2782
0
            goto bad_background;
2783
0
         break;
2784
2785
0
      case PNG_CMAP_TRANS:
2786
0
         if (background_index >= cmap_entries ||
2787
0
            background_index != PNG_CMAP_TRANS_BACKGROUND)
2788
0
            goto bad_background;
2789
0
         break;
2790
2791
0
      case PNG_CMAP_RGB:
2792
0
         if (background_index != PNG_CMAP_RGB_BACKGROUND)
2793
0
            goto bad_background;
2794
0
         break;
2795
2796
0
      case PNG_CMAP_RGB_ALPHA:
2797
0
         if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
2798
0
            goto bad_background;
2799
0
         break;
2800
2801
0
      default:
2802
0
         png_error(png_ptr, "bad processing option (internal error)");
2803
2804
0
      bad_background:
2805
0
         png_error(png_ptr, "bad background index (internal error)");
2806
0
   }
2807
2808
0
   display->colormap_processing = (int)output_processing;
2809
2810
0
   return 1/*ok*/;
2811
0
}
2812
2813
/* The final part of the color-map read called from png_image_finish_read. */
2814
static int
2815
png_image_read_and_map(png_voidp argument)
2816
0
{
2817
0
   png_image_read_control *display = png_voidcast(png_image_read_control*,
2818
0
       argument);
2819
0
   png_imagep image = display->image;
2820
0
   png_structrp png_ptr = image->opaque->png_ptr;
2821
0
   int passes;
2822
2823
   /* Called when the libpng data must be transformed into the color-mapped
2824
    * form.  There is a local row buffer in display->local and this routine must
2825
    * do the interlace handling.
2826
    */
2827
0
   switch (png_ptr->interlaced)
2828
0
   {
2829
0
      case PNG_INTERLACE_NONE:
2830
0
         passes = 1;
2831
0
         break;
2832
2833
0
      case PNG_INTERLACE_ADAM7:
2834
0
         passes = PNG_INTERLACE_ADAM7_PASSES;
2835
0
         break;
2836
2837
0
      default:
2838
0
         png_error(png_ptr, "unknown interlace type");
2839
0
   }
2840
2841
0
   {
2842
0
      png_uint_32 height = image->height;
2843
0
      png_uint_32 width = image->width;
2844
0
      int proc = display->colormap_processing;
2845
0
      png_bytep first_row = png_voidcast(png_bytep, display->first_row);
2846
0
      ptrdiff_t row_step = display->row_step;
2847
0
      int pass;
2848
2849
0
      for (pass = 0; pass < passes; ++pass)
2850
0
      {
2851
0
         unsigned int startx, stepx, stepy;
2852
0
         png_uint_32 y;
2853
2854
0
         if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
2855
0
         {
2856
            /* The row may be empty for a short image: */
2857
0
            if (PNG_PASS_COLS(width, pass) == 0)
2858
0
               continue;
2859
2860
0
            startx = PNG_PASS_START_COL(pass);
2861
0
            stepx = PNG_PASS_COL_OFFSET(pass);
2862
0
            y = PNG_PASS_START_ROW(pass);
2863
0
            stepy = PNG_PASS_ROW_OFFSET(pass);
2864
0
         }
2865
2866
0
         else
2867
0
         {
2868
0
            y = 0;
2869
0
            startx = 0;
2870
0
            stepx = stepy = 1;
2871
0
         }
2872
2873
0
         for (; y<height; y += stepy)
2874
0
         {
2875
0
            png_bytep inrow = png_voidcast(png_bytep, display->local_row);
2876
0
            png_bytep outrow = first_row + y * row_step;
2877
0
            png_const_bytep end_row = outrow + width;
2878
2879
            /* Read read the libpng data into the temporary buffer. */
2880
0
            png_read_row(png_ptr, inrow, NULL);
2881
2882
            /* Now process the row according to the processing option, note
2883
             * that the caller verifies that the format of the libpng output
2884
             * data is as required.
2885
             */
2886
0
            outrow += startx;
2887
0
            switch (proc)
2888
0
            {
2889
0
               case PNG_CMAP_GA:
2890
0
                  for (; outrow < end_row; outrow += stepx)
2891
0
                  {
2892
                     /* The data is always in the PNG order */
2893
0
                     unsigned int gray = *inrow++;
2894
0
                     unsigned int alpha = *inrow++;
2895
0
                     unsigned int entry;
2896
2897
                     /* NOTE: this code is copied as a comment in
2898
                      * make_ga_colormap above.  Please update the
2899
                      * comment if you change this code!
2900
                      */
2901
0
                     if (alpha > 229) /* opaque */
2902
0
                     {
2903
0
                        entry = (231 * gray + 128) >> 8;
2904
0
                     }
2905
0
                     else if (alpha < 26) /* transparent */
2906
0
                     {
2907
0
                        entry = 231;
2908
0
                     }
2909
0
                     else /* partially opaque */
2910
0
                     {
2911
0
                        entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
2912
0
                     }
2913
2914
0
                     *outrow = (png_byte)entry;
2915
0
                  }
2916
0
                  break;
2917
2918
0
               case PNG_CMAP_TRANS:
2919
0
                  for (; outrow < end_row; outrow += stepx)
2920
0
                  {
2921
0
                     png_byte gray = *inrow++;
2922
0
                     png_byte alpha = *inrow++;
2923
2924
0
                     if (alpha == 0)
2925
0
                        *outrow = PNG_CMAP_TRANS_BACKGROUND;
2926
2927
0
                     else if (gray != PNG_CMAP_TRANS_BACKGROUND)
2928
0
                        *outrow = gray;
2929
2930
0
                     else
2931
0
                        *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
2932
0
                  }
2933
0
                  break;
2934
2935
0
               case PNG_CMAP_RGB:
2936
0
                  for (; outrow < end_row; outrow += stepx)
2937
0
                  {
2938
0
                     *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
2939
0
                     inrow += 3;
2940
0
                  }
2941
0
                  break;
2942
2943
0
               case PNG_CMAP_RGB_ALPHA:
2944
0
                  for (; outrow < end_row; outrow += stepx)
2945
0
                  {
2946
0
                     unsigned int alpha = inrow[3];
2947
2948
                     /* Because the alpha entries only hold alpha==0.5 values
2949
                      * split the processing at alpha==0.25 (64) and 0.75
2950
                      * (196).
2951
                      */
2952
2953
0
                     if (alpha >= 196)
2954
0
                        *outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
2955
0
                            inrow[2]);
2956
2957
0
                     else if (alpha < 64)
2958
0
                        *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
2959
2960
0
                     else
2961
0
                     {
2962
                        /* Likewise there are three entries for each of r, g
2963
                         * and b.  We could select the entry by popcount on
2964
                         * the top two bits on those architectures that
2965
                         * support it, this is what the code below does,
2966
                         * crudely.
2967
                         */
2968
0
                        unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
2969
2970
                        /* Here are how the values map:
2971
                         *
2972
                         * 0x00 .. 0x3f -> 0
2973
                         * 0x40 .. 0xbf -> 1
2974
                         * 0xc0 .. 0xff -> 2
2975
                         *
2976
                         * So, as above with the explicit alpha checks, the
2977
                         * breakpoints are at 64 and 196.
2978
                         */
2979
0
                        if (inrow[0] & 0x80) back_i += 9; /* red */
2980
0
                        if (inrow[0] & 0x40) back_i += 9;
2981
0
                        if (inrow[0] & 0x80) back_i += 3; /* green */
2982
0
                        if (inrow[0] & 0x40) back_i += 3;
2983
0
                        if (inrow[0] & 0x80) back_i += 1; /* blue */
2984
0
                        if (inrow[0] & 0x40) back_i += 1;
2985
2986
0
                        *outrow = (png_byte)back_i;
2987
0
                     }
2988
2989
0
                     inrow += 4;
2990
0
                  }
2991
0
                  break;
2992
2993
0
               default:
2994
0
                  break;
2995
0
            }
2996
0
         }
2997
0
      }
2998
0
   }
2999
3000
0
   return 1;
3001
0
}
3002
3003
static int
3004
png_image_read_colormapped(png_voidp argument)
3005
0
{
3006
0
   png_image_read_control *display = png_voidcast(png_image_read_control*,
3007
0
       argument);
3008
0
   png_imagep image = display->image;
3009
0
   png_controlp control = image->opaque;
3010
0
   png_structrp png_ptr = control->png_ptr;
3011
0
   png_inforp info_ptr = control->info_ptr;
3012
3013
0
   int passes = 0; /* As a flag */
3014
3015
0
   PNG_SKIP_CHUNKS(png_ptr);
3016
3017
   /* Update the 'info' structure and make sure the result is as required; first
3018
    * make sure to turn on the interlace handling if it will be required
3019
    * (because it can't be turned on *after* the call to png_read_update_info!)
3020
    */
3021
0
   if (display->colormap_processing == PNG_CMAP_NONE)
3022
0
      passes = png_set_interlace_handling(png_ptr);
3023
3024
0
   png_read_update_info(png_ptr, info_ptr);
3025
3026
   /* The expected output can be deduced from the colormap_processing option. */
3027
0
   switch (display->colormap_processing)
3028
0
   {
3029
0
      case PNG_CMAP_NONE:
3030
         /* Output must be one channel and one byte per pixel, the output
3031
          * encoding can be anything.
3032
          */
3033
0
         if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
3034
0
            info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
3035
0
            info_ptr->bit_depth == 8)
3036
0
            break;
3037
3038
0
         goto bad_output;
3039
3040
0
      case PNG_CMAP_TRANS:
3041
0
      case PNG_CMAP_GA:
3042
         /* Output must be two channels and the 'G' one must be sRGB, the latter
3043
          * can be checked with an exact number because it should have been set
3044
          * to this number above!
3045
          */
3046
0
         if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
3047
0
            info_ptr->bit_depth == 8 &&
3048
0
            png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3049
0
            image->colormap_entries == 256)
3050
0
            break;
3051
3052
0
         goto bad_output;
3053
3054
0
      case PNG_CMAP_RGB:
3055
         /* Output must be 8-bit sRGB encoded RGB */
3056
0
         if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
3057
0
            info_ptr->bit_depth == 8 &&
3058
0
            png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3059
0
            image->colormap_entries == 216)
3060
0
            break;
3061
3062
0
         goto bad_output;
3063
3064
0
      case PNG_CMAP_RGB_ALPHA:
3065
         /* Output must be 8-bit sRGB encoded RGBA */
3066
0
         if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
3067
0
            info_ptr->bit_depth == 8 &&
3068
0
            png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3069
0
            image->colormap_entries == 244 /* 216 + 1 + 27 */)
3070
0
            break;
3071
3072
0
         goto bad_output;
3073
3074
0
      default:
3075
0
      bad_output:
3076
0
         png_error(png_ptr, "bad color-map processing (internal error)");
3077
0
   }
3078
3079
   /* Now read the rows.  Do this here if it is possible to read directly into
3080
    * the output buffer, otherwise allocate a local row buffer of the maximum
3081
    * size libpng requires and call the relevant processing routine safely.
3082
    */
3083
0
   {
3084
0
      png_voidp first_row = display->buffer;
3085
0
      ptrdiff_t row_step = display->row_stride;
3086
3087
      /* The following adjustment is to ensure that calculations are correct,
3088
       * regardless whether row_step is positive or negative.
3089
       */
3090
0
      if (row_step < 0)
3091
0
      {
3092
0
         char *ptr = png_voidcast(char*, first_row);
3093
0
         ptr += (image->height-1) * (-row_step);
3094
0
         first_row = png_voidcast(png_voidp, ptr);
3095
0
      }
3096
3097
0
      display->first_row = first_row;
3098
0
      display->row_step = row_step;
3099
0
   }
3100
3101
0
   if (passes == 0)
3102
0
   {
3103
0
      int result;
3104
0
      png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
3105
3106
0
      display->local_row = row;
3107
0
      result = png_safe_execute(image, png_image_read_and_map, display);
3108
0
      display->local_row = NULL;
3109
0
      png_free(png_ptr, row);
3110
3111
0
      return result;
3112
0
   }
3113
3114
0
   else
3115
0
   {
3116
0
      ptrdiff_t row_step = display->row_step;
3117
3118
0
      while (--passes >= 0)
3119
0
      {
3120
0
         png_uint_32 y = image->height;
3121
0
         png_bytep row = png_voidcast(png_bytep, display->first_row);
3122
3123
0
         for (; y > 0; --y)
3124
0
         {
3125
0
            png_read_row(png_ptr, row, NULL);
3126
0
            row += row_step;
3127
0
         }
3128
0
      }
3129
3130
0
      return 1;
3131
0
   }
3132
0
}
3133
3134
/* Row reading for interlaced 16-to-8 bit depth conversion with local buffer. */
3135
static int
3136
png_image_read_direct_scaled(png_voidp argument)
3137
0
{
3138
0
   png_image_read_control *display = png_voidcast(png_image_read_control*,
3139
0
       argument);
3140
0
   png_imagep image = display->image;
3141
0
   png_structrp png_ptr = image->opaque->png_ptr;
3142
0
   png_inforp info_ptr = image->opaque->info_ptr;
3143
0
   png_bytep local_row = png_voidcast(png_bytep, display->local_row);
3144
0
   png_bytep first_row = png_voidcast(png_bytep, display->first_row);
3145
0
   ptrdiff_t row_step = display->row_step;
3146
0
   size_t row_bytes = png_get_rowbytes(png_ptr, info_ptr);
3147
0
   int passes;
3148
3149
   /* Handle interlacing. */
3150
0
   switch (png_ptr->interlaced)
3151
0
   {
3152
0
      case PNG_INTERLACE_NONE:
3153
0
         passes = 1;
3154
0
         break;
3155
3156
0
      case PNG_INTERLACE_ADAM7:
3157
0
         passes = PNG_INTERLACE_ADAM7_PASSES;
3158
0
         break;
3159
3160
0
      default:
3161
0
         png_error(png_ptr, "unknown interlace type");
3162
0
   }
3163
3164
   /* Read each pass using local_row as intermediate buffer. */
3165
0
   while (--passes >= 0)
3166
0
   {
3167
0
      png_uint_32 y = image->height;
3168
0
      png_bytep output_row = first_row;
3169
3170
0
      for (; y > 0; --y)
3171
0
      {
3172
         /* Read into local_row (gets transformed 8-bit data). */
3173
0
         png_read_row(png_ptr, local_row, NULL);
3174
3175
         /* Copy from local_row to user buffer.
3176
          * Use row_bytes (i.e. the actual size in bytes of the row data) for
3177
          * copying into output_row. Use row_step for advancing output_row,
3178
          * to respect the caller's stride for padding or negative (bottom-up)
3179
          * layouts.
3180
          */
3181
0
         memcpy(output_row, local_row, row_bytes);
3182
0
         output_row += row_step;
3183
0
      }
3184
0
   }
3185
3186
0
   return 1;
3187
0
}
3188
3189
/* Just the row reading part of png_image_read. */
3190
static int
3191
png_image_read_composite(png_voidp argument)
3192
0
{
3193
0
   png_image_read_control *display = png_voidcast(png_image_read_control*,
3194
0
       argument);
3195
0
   png_imagep image = display->image;
3196
0
   png_structrp png_ptr = image->opaque->png_ptr;
3197
0
   int passes;
3198
3199
0
   switch (png_ptr->interlaced)
3200
0
   {
3201
0
      case PNG_INTERLACE_NONE:
3202
0
         passes = 1;
3203
0
         break;
3204
3205
0
      case PNG_INTERLACE_ADAM7:
3206
0
         passes = PNG_INTERLACE_ADAM7_PASSES;
3207
0
         break;
3208
3209
0
      default:
3210
0
         png_error(png_ptr, "unknown interlace type");
3211
0
   }
3212
3213
0
   {
3214
0
      png_uint_32 height = image->height;
3215
0
      png_uint_32 width = image->width;
3216
0
      ptrdiff_t row_step = display->row_step;
3217
0
      unsigned int channels =
3218
0
          (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
3219
0
      int optimize_alpha = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0;
3220
0
      int pass;
3221
3222
0
      for (pass = 0; pass < passes; ++pass)
3223
0
      {
3224
0
         unsigned int startx, stepx, stepy;
3225
0
         png_uint_32 y;
3226
3227
0
         if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3228
0
         {
3229
            /* The row may be empty for a short image: */
3230
0
            if (PNG_PASS_COLS(width, pass) == 0)
3231
0
               continue;
3232
3233
0
            startx = PNG_PASS_START_COL(pass) * channels;
3234
0
            stepx = PNG_PASS_COL_OFFSET(pass) * channels;
3235
0
            y = PNG_PASS_START_ROW(pass);
3236
0
            stepy = PNG_PASS_ROW_OFFSET(pass);
3237
0
         }
3238
3239
0
         else
3240
0
         {
3241
0
            y = 0;
3242
0
            startx = 0;
3243
0
            stepx = channels;
3244
0
            stepy = 1;
3245
0
         }
3246
3247
0
         for (; y<height; y += stepy)
3248
0
         {
3249
0
            png_bytep inrow = png_voidcast(png_bytep, display->local_row);
3250
0
            png_bytep outrow;
3251
0
            png_const_bytep end_row;
3252
3253
            /* Read the row, which is packed: */
3254
0
            png_read_row(png_ptr, inrow, NULL);
3255
3256
0
            outrow = png_voidcast(png_bytep, display->first_row);
3257
0
            outrow += y * row_step;
3258
0
            end_row = outrow + width * channels;
3259
3260
            /* Now do the composition on each pixel in this row. */
3261
0
            outrow += startx;
3262
0
            for (; outrow < end_row; outrow += stepx)
3263
0
            {
3264
0
               png_byte alpha = inrow[channels];
3265
3266
0
               if (alpha > 0) /* else no change to the output */
3267
0
               {
3268
0
                  unsigned int c;
3269
3270
0
                  for (c=0; c<channels; ++c)
3271
0
                  {
3272
0
                     png_uint_32 component = inrow[c];
3273
3274
0
                     if (alpha < 255) /* else just use component */
3275
0
                     {
3276
0
                        if (optimize_alpha != 0)
3277
0
                        {
3278
                           /* This is PNG_OPTIMIZED_ALPHA, the component value
3279
                            * is a linear 8-bit value.  Combine this with the
3280
                            * current outrow[c] value which is sRGB encoded.
3281
                            * Arithmetic here is 16-bits to preserve the output
3282
                            * values correctly.
3283
                            */
3284
0
                           component *= 257*255; /* =65535 */
3285
0
                           component += (255-alpha)*png_sRGB_table[outrow[c]];
3286
3287
                           /* Clamp to the valid range to defend against
3288
                            * unforeseen cases where the data might be sRGB
3289
                            * instead of linear premultiplied.
3290
                            * (Belt-and-suspenders for CVE-2025-66293.)
3291
                            */
3292
0
                           if (component > 255*65535)
3293
0
                              component = 255*65535;
3294
3295
                           /* So 'component' is scaled by 255*65535 and is
3296
                            * therefore appropriate for the sRGB-to-linear
3297
                            * conversion table.
3298
                            */
3299
0
                           component = PNG_sRGB_FROM_LINEAR(component);
3300
0
                        }
3301
0
                        else
3302
0
                        {
3303
                           /* Compositing was already done on the palette
3304
                            * entries.  The data is sRGB premultiplied on black.
3305
                            * Composite with the background in sRGB space.
3306
                            * This is not gamma-correct, but matches what was
3307
                            * done to the palette.
3308
                            */
3309
0
                           png_uint_32 background = outrow[c];
3310
0
                           component += ((255-alpha) * background + 127) / 255;
3311
0
                           if (component > 255)
3312
0
                              component = 255;
3313
0
                        }
3314
0
                     }
3315
3316
0
                     outrow[c] = (png_byte)component;
3317
0
                  }
3318
0
               }
3319
3320
0
               inrow += channels+1; /* components and alpha channel */
3321
0
            }
3322
0
         }
3323
0
      }
3324
0
   }
3325
3326
0
   return 1;
3327
0
}
3328
3329
/* The do_local_background case; called when all the following transforms are to
3330
 * be done:
3331
 *
3332
 * PNG_RGB_TO_GRAY
3333
 * PNG_COMPOSITE
3334
 * PNG_GAMMA
3335
 *
3336
 * This is a work-around for the fact that both the PNG_RGB_TO_GRAY and
3337
 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
3338
 * correction.  The fix-up is to prevent the PNG_COMPOSITE operation from
3339
 * happening inside libpng, so this routine sees an 8 or 16-bit gray+alpha
3340
 * row and handles the removal or pre-multiplication of the alpha channel.
3341
 */
3342
static int
3343
png_image_read_background(png_voidp argument)
3344
0
{
3345
0
   png_image_read_control *display = png_voidcast(png_image_read_control*,
3346
0
       argument);
3347
0
   png_imagep image = display->image;
3348
0
   png_structrp png_ptr = image->opaque->png_ptr;
3349
0
   png_inforp info_ptr = image->opaque->info_ptr;
3350
0
   png_uint_32 height = image->height;
3351
0
   png_uint_32 width = image->width;
3352
0
   int pass, passes;
3353
3354
   /* Double check the convoluted logic below.  We expect to get here with
3355
    * libpng doing rgb to gray and gamma correction but background processing
3356
    * left to the png_image_read_background function.  The rows libpng produce
3357
    * might be 8 or 16-bit but should always have two channels; gray plus alpha.
3358
    */
3359
0
   if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
3360
0
      png_error(png_ptr, "lost rgb to gray");
3361
3362
0
   if ((png_ptr->transformations & PNG_COMPOSE) != 0)
3363
0
      png_error(png_ptr, "unexpected compose");
3364
3365
0
   if (png_get_channels(png_ptr, info_ptr) != 2)
3366
0
      png_error(png_ptr, "lost/gained channels");
3367
3368
   /* Expect the 8-bit case to always remove the alpha channel */
3369
0
   if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 &&
3370
0
      (image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
3371
0
      png_error(png_ptr, "unexpected 8-bit transformation");
3372
3373
0
   switch (png_ptr->interlaced)
3374
0
   {
3375
0
      case PNG_INTERLACE_NONE:
3376
0
         passes = 1;
3377
0
         break;
3378
3379
0
      case PNG_INTERLACE_ADAM7:
3380
0
         passes = PNG_INTERLACE_ADAM7_PASSES;
3381
0
         break;
3382
3383
0
      default:
3384
0
         png_error(png_ptr, "unknown interlace type");
3385
0
   }
3386
3387
   /* Use direct access to info_ptr here because otherwise the simplified API
3388
    * would require PNG_EASY_ACCESS_SUPPORTED (just for this.)  Note this is
3389
    * checking the value after libpng expansions, not the original value in the
3390
    * PNG.
3391
    */
3392
0
   switch (info_ptr->bit_depth)
3393
0
   {
3394
0
      case 8:
3395
         /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
3396
          * to be removed by composing on a background: either the row if
3397
          * display->background is NULL or display->background->green if not.
3398
          * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3399
          */
3400
0
         {
3401
0
            png_bytep first_row = png_voidcast(png_bytep, display->first_row);
3402
0
            ptrdiff_t row_step = display->row_step;
3403
3404
0
            for (pass = 0; pass < passes; ++pass)
3405
0
            {
3406
0
               unsigned int startx, stepx, stepy;
3407
0
               png_uint_32 y;
3408
3409
0
               if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3410
0
               {
3411
                  /* The row may be empty for a short image: */
3412
0
                  if (PNG_PASS_COLS(width, pass) == 0)
3413
0
                     continue;
3414
3415
0
                  startx = PNG_PASS_START_COL(pass);
3416
0
                  stepx = PNG_PASS_COL_OFFSET(pass);
3417
0
                  y = PNG_PASS_START_ROW(pass);
3418
0
                  stepy = PNG_PASS_ROW_OFFSET(pass);
3419
0
               }
3420
3421
0
               else
3422
0
               {
3423
0
                  y = 0;
3424
0
                  startx = 0;
3425
0
                  stepx = stepy = 1;
3426
0
               }
3427
3428
0
               if (display->background == NULL)
3429
0
               {
3430
0
                  for (; y<height; y += stepy)
3431
0
                  {
3432
0
                     png_bytep inrow = png_voidcast(png_bytep,
3433
0
                         display->local_row);
3434
0
                     png_bytep outrow = first_row + y * row_step;
3435
0
                     png_const_bytep end_row = outrow + width;
3436
3437
                     /* Read the row, which is packed: */
3438
0
                     png_read_row(png_ptr, inrow, NULL);
3439
3440
                     /* Now do the composition on each pixel in this row. */
3441
0
                     outrow += startx;
3442
0
                     for (; outrow < end_row; outrow += stepx)
3443
0
                     {
3444
0
                        png_byte alpha = inrow[1];
3445
3446
0
                        if (alpha > 0) /* else no change to the output */
3447
0
                        {
3448
0
                           png_uint_32 component = inrow[0];
3449
3450
0
                           if (alpha < 255) /* else just use component */
3451
0
                           {
3452
                              /* Since PNG_OPTIMIZED_ALPHA was not set it is
3453
                               * necessary to invert the sRGB transfer
3454
                               * function and multiply the alpha out.
3455
                               */
3456
0
                              component = png_sRGB_table[component] * alpha;
3457
0
                              component += png_sRGB_table[outrow[0]] *
3458
0
                                 (255-alpha);
3459
0
                              component = PNG_sRGB_FROM_LINEAR(component);
3460
0
                           }
3461
3462
0
                           outrow[0] = (png_byte)component;
3463
0
                        }
3464
3465
0
                        inrow += 2; /* gray and alpha channel */
3466
0
                     }
3467
0
                  }
3468
0
               }
3469
3470
0
               else /* constant background value */
3471
0
               {
3472
0
                  png_byte background8 = display->background->green;
3473
0
                  png_uint_16 background = png_sRGB_table[background8];
3474
3475
0
                  for (; y<height; y += stepy)
3476
0
                  {
3477
0
                     png_bytep inrow = png_voidcast(png_bytep,
3478
0
                         display->local_row);
3479
0
                     png_bytep outrow = first_row + y * row_step;
3480
0
                     png_const_bytep end_row = outrow + width;
3481
3482
                     /* Read the row, which is packed: */
3483
0
                     png_read_row(png_ptr, inrow, NULL);
3484
3485
                     /* Now do the composition on each pixel in this row. */
3486
0
                     outrow += startx;
3487
0
                     for (; outrow < end_row; outrow += stepx)
3488
0
                     {
3489
0
                        png_byte alpha = inrow[1];
3490
3491
0
                        if (alpha > 0) /* else use background */
3492
0
                        {
3493
0
                           png_uint_32 component = inrow[0];
3494
3495
0
                           if (alpha < 255) /* else just use component */
3496
0
                           {
3497
0
                              component = png_sRGB_table[component] * alpha;
3498
0
                              component += background * (255-alpha);
3499
0
                              component = PNG_sRGB_FROM_LINEAR(component);
3500
0
                           }
3501
3502
0
                           outrow[0] = (png_byte)component;
3503
0
                        }
3504
3505
0
                        else
3506
0
                           outrow[0] = background8;
3507
3508
0
                        inrow += 2; /* gray and alpha channel */
3509
0
                     }
3510
0
                  }
3511
0
               }
3512
0
            }
3513
0
         }
3514
0
         break;
3515
3516
0
      case 16:
3517
         /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3518
          * still be done and, maybe, the alpha channel removed.  This code also
3519
          * handles the alpha-first option.
3520
          */
3521
0
         {
3522
0
            png_uint_16p first_row = png_voidcast(png_uint_16p,
3523
0
                display->first_row);
3524
            /* The division by two is safe because the caller passed in a
3525
             * stride which was multiplied by 2 (below) to get row_step.
3526
             */
3527
0
            ptrdiff_t row_step = display->row_step / 2;
3528
0
            unsigned int preserve_alpha = (image->format &
3529
0
                PNG_FORMAT_FLAG_ALPHA) != 0;
3530
0
            unsigned int outchannels = 1U+preserve_alpha;
3531
0
            int swap_alpha = 0;
3532
3533
0
#           ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
3534
0
               if (preserve_alpha != 0 &&
3535
0
                   (image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
3536
0
                  swap_alpha = 1;
3537
0
#           endif
3538
3539
0
            for (pass = 0; pass < passes; ++pass)
3540
0
            {
3541
0
               unsigned int startx, stepx, stepy;
3542
0
               png_uint_32 y;
3543
3544
               /* The 'x' start and step are adjusted to output components here.
3545
                */
3546
0
               if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3547
0
               {
3548
                  /* The row may be empty for a short image: */
3549
0
                  if (PNG_PASS_COLS(width, pass) == 0)
3550
0
                     continue;
3551
3552
0
                  startx = PNG_PASS_START_COL(pass) * outchannels;
3553
0
                  stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
3554
0
                  y = PNG_PASS_START_ROW(pass);
3555
0
                  stepy = PNG_PASS_ROW_OFFSET(pass);
3556
0
               }
3557
3558
0
               else
3559
0
               {
3560
0
                  y = 0;
3561
0
                  startx = 0;
3562
0
                  stepx = outchannels;
3563
0
                  stepy = 1;
3564
0
               }
3565
3566
0
               for (; y<height; y += stepy)
3567
0
               {
3568
0
                  png_const_uint_16p inrow;
3569
0
                  png_uint_16p outrow = first_row + y * row_step;
3570
0
                  png_uint_16p end_row = outrow + width * outchannels;
3571
3572
                  /* Read the row, which is packed: */
3573
0
                  png_read_row(png_ptr, png_voidcast(png_bytep,
3574
0
                      display->local_row), NULL);
3575
0
                  inrow = png_voidcast(png_const_uint_16p, display->local_row);
3576
3577
                  /* Now do the pre-multiplication on each pixel in this row.
3578
                   */
3579
0
                  outrow += startx;
3580
0
                  for (; outrow < end_row; outrow += stepx)
3581
0
                  {
3582
0
                     png_uint_32 component = inrow[0];
3583
0
                     png_uint_16 alpha = inrow[1];
3584
3585
0
                     if (alpha > 0) /* else 0 */
3586
0
                     {
3587
0
                        if (alpha < 65535) /* else just use component */
3588
0
                        {
3589
0
                           component *= alpha;
3590
0
                           component += 32767;
3591
0
                           component /= 65535;
3592
0
                        }
3593
0
                     }
3594
3595
0
                     else
3596
0
                        component = 0;
3597
3598
0
                     outrow[swap_alpha] = (png_uint_16)component;
3599
0
                     if (preserve_alpha != 0)
3600
0
                        outrow[1 ^ swap_alpha] = alpha;
3601
3602
0
                     inrow += 2; /* components and alpha channel */
3603
0
                  }
3604
0
               }
3605
0
            }
3606
0
         }
3607
0
         break;
3608
3609
0
#ifdef __GNUC__
3610
0
      default:
3611
0
         png_error(png_ptr, "unexpected bit depth");
3612
0
#endif
3613
0
   }
3614
3615
0
   return 1;
3616
0
}
3617
3618
/* The guts of png_image_finish_read as a png_safe_execute callback. */
3619
static int
3620
png_image_read_direct(png_voidp argument)
3621
0
{
3622
0
   png_image_read_control *display = png_voidcast(png_image_read_control*,
3623
0
       argument);
3624
0
   png_imagep image = display->image;
3625
0
   png_structrp png_ptr = image->opaque->png_ptr;
3626
0
   png_inforp info_ptr = image->opaque->info_ptr;
3627
3628
0
   png_uint_32 format = image->format;
3629
0
   int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
3630
0
   int do_local_compose = 0;
3631
0
   int do_local_background = 0; /* to avoid double gamma correction bug */
3632
0
   int do_local_scale = 0; /* for interlaced 16-to-8 bit conversion */
3633
0
   int passes = 0;
3634
3635
   /* Add transforms to ensure the correct output format is produced then check
3636
    * that the required implementation support is there.  Always expand; always
3637
    * need 8 bits minimum, no palette and expanded tRNS.
3638
    */
3639
0
   png_set_expand(png_ptr);
3640
3641
   /* Now check the format to see if it was modified. */
3642
0
   {
3643
0
      png_uint_32 base_format = png_image_format(png_ptr) &
3644
0
         ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
3645
0
      png_uint_32 change = format ^ base_format;
3646
0
      png_fixed_point output_gamma;
3647
0
      int mode; /* alpha mode */
3648
3649
      /* Do this first so that we have a record if rgb to gray is happening. */
3650
0
      if ((change & PNG_FORMAT_FLAG_COLOR) != 0)
3651
0
      {
3652
         /* gray<->color transformation required. */
3653
0
         if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
3654
0
            png_set_gray_to_rgb(png_ptr);
3655
3656
0
         else
3657
0
         {
3658
            /* libpng can't do both rgb to gray and
3659
             * background/pre-multiplication if there is also significant gamma
3660
             * correction, because both operations require linear colors and
3661
             * the code only supports one transform doing the gamma correction.
3662
             * Handle this by doing the pre-multiplication or background
3663
             * operation in this code, if necessary.
3664
             *
3665
             * TODO: fix this by rewriting pngrtran.c (!)
3666
             *
3667
             * For the moment (given that fixing this in pngrtran.c is an
3668
             * enormous change) 'do_local_background' is used to indicate that
3669
             * the problem exists.
3670
             */
3671
0
            if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
3672
0
               do_local_background = 1/*maybe*/;
3673
3674
0
            png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
3675
0
                PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
3676
0
         }
3677
3678
0
         change &= ~PNG_FORMAT_FLAG_COLOR;
3679
0
      }
3680
3681
      /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
3682
       */
3683
0
      {
3684
         /* This is safe but should no longer be necessary as
3685
          * png_ptr->default_gamma should have been set after the
3686
          * info-before-IDAT was read in png_image_read_header.
3687
          *
3688
          * TODO: 1.8: remove this and see what happens.
3689
          */
3690
0
         png_fixed_point input_gamma_default;
3691
3692
0
         if ((base_format & PNG_FORMAT_FLAG_LINEAR) != 0 &&
3693
0
             (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
3694
0
            input_gamma_default = PNG_GAMMA_LINEAR;
3695
0
         else
3696
0
            input_gamma_default = PNG_DEFAULT_sRGB;
3697
3698
         /* Call png_set_alpha_mode to set the default for the input gamma; the
3699
          * output gamma is set by a second call below.
3700
          */
3701
0
         png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
3702
0
      }
3703
3704
0
      if (linear != 0)
3705
0
      {
3706
         /* If there *is* an alpha channel in the input it must be multiplied
3707
          * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3708
          */
3709
0
         if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
3710
0
            mode = PNG_ALPHA_STANDARD; /* associated alpha */
3711
3712
0
         else
3713
0
            mode = PNG_ALPHA_PNG;
3714
3715
0
         output_gamma = PNG_GAMMA_LINEAR;
3716
0
      }
3717
3718
0
      else
3719
0
      {
3720
0
         mode = PNG_ALPHA_PNG;
3721
0
         output_gamma = PNG_DEFAULT_sRGB;
3722
0
      }
3723
3724
0
      if ((change & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0)
3725
0
      {
3726
0
         mode = PNG_ALPHA_OPTIMIZED;
3727
0
         change &= ~PNG_FORMAT_FLAG_ASSOCIATED_ALPHA;
3728
0
      }
3729
3730
      /* If 'do_local_background' is set check for the presence of gamma
3731
       * correction; this is part of the work-round for the libpng bug
3732
       * described above.
3733
       *
3734
       * TODO: fix libpng and remove this.
3735
       */
3736
0
      if (do_local_background != 0)
3737
0
      {
3738
0
         png_fixed_point gtest;
3739
3740
         /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3741
          * gamma correction, the screen gamma hasn't been set on png_struct
3742
          * yet; it's set below.  png_struct::gamma, however, is set to the
3743
          * final value.
3744
          */
3745
0
         if (png_muldiv(&gtest, output_gamma,
3746
0
                  png_resolve_file_gamma(png_ptr), PNG_FP_1) != 0 &&
3747
0
             png_gamma_significant(gtest) == 0)
3748
0
            do_local_background = 0;
3749
3750
0
         else if (mode == PNG_ALPHA_STANDARD)
3751
0
         {
3752
0
            do_local_background = 2/*required*/;
3753
0
            mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
3754
0
         }
3755
3756
         /* else leave as 1 for the checks below */
3757
0
      }
3758
3759
      /* If the bit-depth changes then handle that here. */
3760
0
      if ((change & PNG_FORMAT_FLAG_LINEAR) != 0)
3761
0
      {
3762
0
         if (linear != 0 /*16-bit output*/)
3763
0
            png_set_expand_16(png_ptr);
3764
3765
0
         else /* 8-bit output */
3766
0
         {
3767
0
            png_set_scale_16(png_ptr);
3768
3769
            /* For interlaced images, use local_row buffer to avoid overflow
3770
             * in png_combine_row() which writes using IHDR bit-depth.
3771
             */
3772
0
            if (png_ptr->interlaced != 0)
3773
0
               do_local_scale = 1;
3774
0
         }
3775
3776
0
         change &= ~PNG_FORMAT_FLAG_LINEAR;
3777
0
      }
3778
3779
      /* Now the background/alpha channel changes. */
3780
0
      if ((change & PNG_FORMAT_FLAG_ALPHA) != 0)
3781
0
      {
3782
         /* Removing an alpha channel requires composition for the 8-bit
3783
          * formats; for the 16-bit it is already done, above, by the
3784
          * pre-multiplication and the channel just needs to be stripped.
3785
          */
3786
0
         if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
3787
0
         {
3788
            /* If RGB->gray is happening the alpha channel must be left and the
3789
             * operation completed locally.
3790
             *
3791
             * TODO: fix libpng and remove this.
3792
             */
3793
0
            if (do_local_background != 0)
3794
0
               do_local_background = 2/*required*/;
3795
3796
            /* 16-bit output: just remove the channel */
3797
0
            else if (linear != 0) /* compose on black (well, pre-multiply) */
3798
0
               png_set_strip_alpha(png_ptr);
3799
3800
            /* 8-bit output: do an appropriate compose */
3801
0
            else if (display->background != NULL)
3802
0
            {
3803
0
               png_color_16 c;
3804
3805
0
               c.index = 0; /*unused*/
3806
0
               c.red = display->background->red;
3807
0
               c.green = display->background->green;
3808
0
               c.blue = display->background->blue;
3809
0
               c.gray = display->background->green;
3810
3811
               /* This is always an 8-bit sRGB value, using the 'green' channel
3812
                * for gray is much better than calculating the luminance here;
3813
                * we can get off-by-one errors in that calculation relative to
3814
                * the app expectations and that will show up in transparent
3815
                * pixels.
3816
                */
3817
0
               png_set_background_fixed(png_ptr, &c,
3818
0
                   PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
3819
0
                   0/*gamma: not used*/);
3820
0
            }
3821
3822
0
            else /* compose on row: implemented below. */
3823
0
            {
3824
0
               do_local_compose = 1;
3825
               /* This leaves the alpha channel in the output, so it has to be
3826
                * removed by the code below.  Set the encoding to the 'OPTIMIZE'
3827
                * one so the code only has to hack on the pixels that require
3828
                * composition.
3829
                */
3830
0
               mode = PNG_ALPHA_OPTIMIZED;
3831
0
            }
3832
0
         }
3833
3834
0
         else /* output needs an alpha channel */
3835
0
         {
3836
            /* This is tricky because it happens before the swap operation has
3837
             * been accomplished; however, the swap does *not* swap the added
3838
             * alpha channel (weird API), so it must be added in the correct
3839
             * place.
3840
             */
3841
0
            png_uint_32 filler; /* opaque filler */
3842
0
            int where;
3843
3844
0
            if (linear != 0)
3845
0
               filler = 65535;
3846
3847
0
            else
3848
0
               filler = 255;
3849
3850
0
#ifdef PNG_FORMAT_AFIRST_SUPPORTED
3851
0
            if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
3852
0
            {
3853
0
               where = PNG_FILLER_BEFORE;
3854
0
               change &= ~PNG_FORMAT_FLAG_AFIRST;
3855
0
            }
3856
3857
0
            else
3858
0
#endif
3859
0
            where = PNG_FILLER_AFTER;
3860
3861
0
            png_set_add_alpha(png_ptr, filler, where);
3862
0
         }
3863
3864
         /* This stops the (irrelevant) call to swap_alpha below. */
3865
0
         change &= ~PNG_FORMAT_FLAG_ALPHA;
3866
0
      }
3867
3868
      /* Now set the alpha mode correctly; this is always done, even if there is
3869
       * no alpha channel in either the input or the output because it correctly
3870
       * sets the output gamma.
3871
       */
3872
0
      png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
3873
3874
0
#     ifdef PNG_FORMAT_BGR_SUPPORTED
3875
0
         if ((change & PNG_FORMAT_FLAG_BGR) != 0)
3876
0
         {
3877
            /* Check only the output format; PNG is never BGR; don't do this if
3878
             * the output is gray, but fix up the 'format' value in that case.
3879
             */
3880
0
            if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
3881
0
               png_set_bgr(png_ptr);
3882
3883
0
            else
3884
0
               format &= ~PNG_FORMAT_FLAG_BGR;
3885
3886
0
            change &= ~PNG_FORMAT_FLAG_BGR;
3887
0
         }
3888
0
#     endif
3889
3890
0
#     ifdef PNG_FORMAT_AFIRST_SUPPORTED
3891
0
         if ((change & PNG_FORMAT_FLAG_AFIRST) != 0)
3892
0
         {
3893
            /* Only relevant if there is an alpha channel - it's particularly
3894
             * important to handle this correctly because do_local_compose may
3895
             * be set above and then libpng will keep the alpha channel for this
3896
             * code to remove.
3897
             */
3898
0
            if ((format & PNG_FORMAT_FLAG_ALPHA) != 0)
3899
0
            {
3900
               /* Disable this if doing a local background,
3901
                * TODO: remove this when local background is no longer required.
3902
                */
3903
0
               if (do_local_background != 2)
3904
0
                  png_set_swap_alpha(png_ptr);
3905
0
            }
3906
3907
0
            else
3908
0
               format &= ~PNG_FORMAT_FLAG_AFIRST;
3909
3910
0
            change &= ~PNG_FORMAT_FLAG_AFIRST;
3911
0
         }
3912
0
#     endif
3913
3914
      /* If the *output* is 16-bit then we need to check for a byte-swap on this
3915
       * architecture.
3916
       */
3917
0
      if (linear != 0)
3918
0
      {
3919
0
         png_uint_16 le = 0x0001;
3920
3921
0
         if ((*(png_const_bytep) & le) != 0)
3922
0
            png_set_swap(png_ptr);
3923
0
      }
3924
3925
      /* If change is not now 0 some transformation is missing - error out. */
3926
0
      if (change != 0)
3927
0
         png_error(png_ptr, "png_read_image: unsupported transformation");
3928
0
   }
3929
3930
0
   PNG_SKIP_CHUNKS(png_ptr);
3931
3932
   /* Update the 'info' structure and make sure the result is as required; first
3933
    * make sure to turn on the interlace handling if it will be required
3934
    * (because it can't be turned on *after* the call to png_read_update_info!)
3935
    *
3936
    * TODO: remove the do_local_background fixup below.
3937
    */
3938
0
   if (do_local_compose == 0 && do_local_background != 2)
3939
0
      passes = png_set_interlace_handling(png_ptr);
3940
3941
0
   png_read_update_info(png_ptr, info_ptr);
3942
3943
0
   {
3944
0
      png_uint_32 info_format = 0;
3945
3946
0
      if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
3947
0
         info_format |= PNG_FORMAT_FLAG_COLOR;
3948
3949
0
      if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
3950
0
      {
3951
         /* do_local_compose removes this channel below. */
3952
0
         if (do_local_compose == 0)
3953
0
         {
3954
            /* do_local_background does the same if required. */
3955
0
            if (do_local_background != 2 ||
3956
0
               (format & PNG_FORMAT_FLAG_ALPHA) != 0)
3957
0
               info_format |= PNG_FORMAT_FLAG_ALPHA;
3958
0
         }
3959
0
      }
3960
3961
0
      else if (do_local_compose != 0) /* internal error */
3962
0
         png_error(png_ptr, "png_image_read: alpha channel lost");
3963
3964
0
      if ((format & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0) {
3965
0
         info_format |= PNG_FORMAT_FLAG_ASSOCIATED_ALPHA;
3966
0
      }
3967
3968
0
      if (info_ptr->bit_depth == 16)
3969
0
         info_format |= PNG_FORMAT_FLAG_LINEAR;
3970
3971
0
#ifdef PNG_FORMAT_BGR_SUPPORTED
3972
0
      if ((png_ptr->transformations & PNG_BGR) != 0)
3973
0
         info_format |= PNG_FORMAT_FLAG_BGR;
3974
0
#endif
3975
3976
0
#ifdef PNG_FORMAT_AFIRST_SUPPORTED
3977
0
         if (do_local_background == 2)
3978
0
         {
3979
0
            if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
3980
0
               info_format |= PNG_FORMAT_FLAG_AFIRST;
3981
0
         }
3982
3983
0
         if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
3984
0
            ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
3985
0
            (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
3986
0
         {
3987
0
            if (do_local_background == 2)
3988
0
               png_error(png_ptr, "unexpected alpha swap transformation");
3989
3990
0
            info_format |= PNG_FORMAT_FLAG_AFIRST;
3991
0
         }
3992
0
#     endif
3993
3994
      /* This is actually an internal error. */
3995
0
      if (info_format != format)
3996
0
         png_error(png_ptr, "png_read_image: invalid transformations");
3997
0
   }
3998
3999
   /* Now read the rows.  If do_local_compose is set then it is necessary to use
4000
    * a local row buffer.  The output will be GA, RGBA or BGRA and must be
4001
    * converted to G, RGB or BGR as appropriate.  The 'local_row' member of the
4002
    * display acts as a flag.
4003
    */
4004
0
   {
4005
0
      png_voidp first_row = display->buffer;
4006
0
      ptrdiff_t row_step = display->row_stride;
4007
4008
0
      if (linear != 0)
4009
0
         row_step *= 2;
4010
4011
      /* The following adjustment is to ensure that calculations are correct,
4012
       * regardless whether row_step is positive or negative.
4013
       */
4014
0
      if (row_step < 0)
4015
0
      {
4016
0
         char *ptr = png_voidcast(char*, first_row);
4017
0
         ptr += (image->height - 1) * (-row_step);
4018
0
         first_row = png_voidcast(png_voidp, ptr);
4019
0
      }
4020
4021
0
      display->first_row = first_row;
4022
0
      display->row_step = row_step;
4023
0
   }
4024
4025
0
   if (do_local_compose != 0)
4026
0
   {
4027
0
      int result;
4028
0
      png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
4029
4030
0
      display->local_row = row;
4031
0
      result = png_safe_execute(image, png_image_read_composite, display);
4032
0
      display->local_row = NULL;
4033
0
      png_free(png_ptr, row);
4034
4035
0
      return result;
4036
0
   }
4037
4038
0
   else if (do_local_background == 2)
4039
0
   {
4040
0
      int result;
4041
0
      png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
4042
4043
0
      display->local_row = row;
4044
0
      result = png_safe_execute(image, png_image_read_background, display);
4045
0
      display->local_row = NULL;
4046
0
      png_free(png_ptr, row);
4047
4048
0
      return result;
4049
0
   }
4050
4051
0
   else if (do_local_scale != 0)
4052
0
   {
4053
      /* For interlaced 16-to-8 conversion, use an intermediate row buffer
4054
       * to avoid buffer overflows in png_combine_row. The local_row is sized
4055
       * for the transformed (8-bit) output, preventing the overflow that would
4056
       * occur if png_combine_row wrote 16-bit data directly to the user buffer.
4057
       */
4058
0
      int result;
4059
0
      png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
4060
4061
0
      display->local_row = row;
4062
0
      result = png_safe_execute(image, png_image_read_direct_scaled, display);
4063
0
      display->local_row = NULL;
4064
0
      png_free(png_ptr, row);
4065
4066
0
      return result;
4067
0
   }
4068
4069
0
   else
4070
0
   {
4071
0
      ptrdiff_t row_step = display->row_step;
4072
4073
0
      while (--passes >= 0)
4074
0
      {
4075
0
         png_uint_32 y = image->height;
4076
0
         png_bytep row = png_voidcast(png_bytep, display->first_row);
4077
4078
0
         for (; y > 0; --y)
4079
0
         {
4080
0
            png_read_row(png_ptr, row, NULL);
4081
0
            row += row_step;
4082
0
         }
4083
0
      }
4084
4085
0
      return 1;
4086
0
   }
4087
0
}
4088
4089
int PNGAPI
4090
png_image_finish_read(png_imagep image, png_const_colorp background,
4091
    void *buffer, png_int_32 row_stride, void *colormap)
4092
0
{
4093
0
   if (image != NULL && image->version == PNG_IMAGE_VERSION)
4094
0
   {
4095
      /* Check for row_stride overflow.  This check is not performed on the
4096
       * original PNG format because it may not occur in the output PNG format
4097
       * and libpng deals with the issues of reading the original.
4098
       */
4099
0
      unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format);
4100
4101
      /* The following checks just the 'row_stride' calculation to ensure it
4102
       * fits in a signed 32-bit value.  Because channels/components can be
4103
       * either 1 or 2 bytes in size the length of a row can still overflow 32
4104
       * bits; this is just to verify that the 'row_stride' argument can be
4105
       * represented.
4106
       */
4107
0
      if (image->width <= 0x7fffffffU/channels) /* no overflow */
4108
0
      {
4109
0
         png_uint_32 check;
4110
0
         png_uint_32 png_row_stride = image->width * channels;
4111
4112
0
         if (row_stride == 0)
4113
0
            row_stride = (png_int_32)/*SAFE*/png_row_stride;
4114
4115
0
         if (row_stride < 0)
4116
0
            check = (png_uint_32)(-row_stride);
4117
4118
0
         else
4119
0
            check = (png_uint_32)row_stride;
4120
4121
         /* This verifies 'check', the absolute value of the actual stride
4122
          * passed in and detects overflow in the application calculation (i.e.
4123
          * if the app did actually pass in a non-zero 'row_stride'.
4124
          */
4125
0
         if (image->opaque != NULL && buffer != NULL && check >= png_row_stride)
4126
0
         {
4127
            /* Now check for overflow of the image buffer calculation; this
4128
             * limits the whole image size to 32 bits for API compatibility with
4129
             * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.
4130
             *
4131
             * The PNG_IMAGE_BUFFER_SIZE macro is:
4132
             *
4133
             *    (PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)*height*(row_stride))
4134
             *
4135
             * And the component size is always 1 or 2, so make sure that the
4136
             * number of *bytes* that the application is saying are available
4137
             * does actually fit into a 32-bit number.
4138
             *
4139
             * NOTE: this will be changed in 1.7 because PNG_IMAGE_BUFFER_SIZE
4140
             * will be changed to use png_alloc_size_t; bigger images can be
4141
             * accommodated on 64-bit systems.
4142
             */
4143
0
            if (image->height <=
4144
0
                0xffffffffU/PNG_IMAGE_PIXEL_COMPONENT_SIZE(image->format)/check)
4145
0
            {
4146
0
               if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
4147
0
                  (image->colormap_entries > 0 && colormap != NULL))
4148
0
               {
4149
0
                  int result;
4150
0
                  png_image_read_control display;
4151
4152
0
                  memset(&display, 0, (sizeof display));
4153
0
                  display.image = image;
4154
0
                  display.buffer = buffer;
4155
0
                  display.row_stride = row_stride;
4156
0
                  display.colormap = colormap;
4157
0
                  display.background = background;
4158
0
                  display.local_row = NULL;
4159
4160
                  /* Choose the correct 'end' routine; for the color-map case
4161
                   * all the setup has already been done.
4162
                   */
4163
0
                  if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0)
4164
0
                     result =
4165
0
                         png_safe_execute(image,
4166
0
                             png_image_read_colormap, &display) &&
4167
0
                             png_safe_execute(image,
4168
0
                             png_image_read_colormapped, &display);
4169
4170
0
                  else
4171
0
                     result =
4172
0
                        png_safe_execute(image,
4173
0
                            png_image_read_direct, &display);
4174
4175
0
                  png_image_free(image);
4176
0
                  return result;
4177
0
               }
4178
4179
0
               else
4180
0
                  return png_image_error(image,
4181
0
                      "png_image_finish_read[color-map]: no color-map");
4182
0
            }
4183
4184
0
            else
4185
0
               return png_image_error(image,
4186
0
                   "png_image_finish_read: image too large");
4187
0
         }
4188
4189
0
         else
4190
0
            return png_image_error(image,
4191
0
                "png_image_finish_read: invalid argument");
4192
0
      }
4193
4194
0
      else
4195
0
         return png_image_error(image,
4196
0
             "png_image_finish_read: row_stride too large");
4197
0
   }
4198
4199
0
   else if (image != NULL)
4200
0
      return png_image_error(image,
4201
0
          "png_image_finish_read: damaged PNG_IMAGE_VERSION");
4202
4203
0
   return 0;
4204
0
}
4205
4206
#endif /* SIMPLIFIED_READ */
4207
#endif /* READ */