Skip to content

Commit

Permalink
refactor: Deduplicate the interlace arrays png_pass_*
Browse files Browse the repository at this point in the history
Move the definitions of interlace arrays to the top of their respective
translation units, to reduce their repeated copying in various places of
the codebase.

TODO:
These arrays still exist in three copies, in pngpread.c, in pngrutil.c,
and in pngwutil.c, and they should be further moved to a common place.
  • Loading branch information
ctruta committed Oct 17, 2024
1 parent d9f13d8 commit 620a2b7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 93 deletions.
36 changes: 15 additions & 21 deletions pngpread.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ if (png_ptr->push_length + 4 > png_ptr->buffer_size) \
if (png_ptr->buffer_size < N) \
{ png_push_save_buffer(png_ptr); return; }

#ifdef PNG_READ_INTERLACING_SUPPORTED
/* Arrays to facilitate interlacing - use pass (0 - 6) as index. */

/* Start of interlace block */
static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
/* Offset to next interlace block */
static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
/* Start of interlace block in the y direction */
static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
/* Offset to next interlace block in the y direction */
static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};

/* TODO: Move these arrays to a common utility module to avoid duplication. */
#endif

void PNGAPI
png_process_data(png_structrp png_ptr, png_inforp info_ptr,
png_bytep buffer, size_t buffer_size)
Expand Down Expand Up @@ -983,27 +998,6 @@ png_push_process_row(png_structrp png_ptr)
void /* PRIVATE */
png_read_push_finish_row(png_structrp png_ptr)
{
#ifdef PNG_READ_INTERLACING_SUPPORTED
/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */

/* Start of interlace block */
static const png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};

/* Offset to next interlace block */
static const png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};

/* Start of interlace block in the y direction */
static const png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};

/* Offset to next interlace block in the y direction */
static const png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};

/* Height of interlace block. This is not currently used - if you need
* it, uncomment it here and in png.h
static const png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
*/
#endif

png_ptr->row_number++;
if (png_ptr->row_number < png_ptr->num_rows)
return;
Expand Down
47 changes: 15 additions & 32 deletions pngrutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@

#ifdef PNG_READ_SUPPORTED

#ifdef PNG_READ_INTERLACING_SUPPORTED
/* Arrays to facilitate interlacing - use pass (0 - 6) as index. */

/* Start of interlace block */
static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
/* Offset to next interlace block */
static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
/* Start of interlace block in the y direction */
static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
/* Offset to next interlace block in the y direction */
static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};

/* TODO: Move these arrays to a common utility module to avoid duplication. */
#endif

png_uint_32 PNGAPI
png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
{
Expand Down Expand Up @@ -3717,10 +3732,6 @@ void /* PRIVATE */
png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
png_uint_32 transformations /* Because these may affect the byte layout */)
{
/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
/* Offset to next interlace block */
static const unsigned int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};

png_debug(1, "in png_do_read_interlace");
if (row != NULL && row_info != NULL)
{
Expand Down Expand Up @@ -4346,20 +4357,6 @@ png_read_finish_IDAT(png_structrp png_ptr)
void /* PRIVATE */
png_read_finish_row(png_structrp png_ptr)
{
/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */

/* Start of interlace block */
static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};

/* Offset to next interlace block */
static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};

/* Start of interlace block in the y direction */
static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};

/* Offset to next interlace block in the y direction */
static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};

png_debug(1, "in png_read_finish_row");
png_ptr->row_number++;
if (png_ptr->row_number < png_ptr->num_rows)
Expand Down Expand Up @@ -4411,20 +4408,6 @@ png_read_finish_row(png_structrp png_ptr)
void /* PRIVATE */
png_read_start_row(png_structrp png_ptr)
{
/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */

/* Start of interlace block */
static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};

/* Offset to next interlace block */
static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};

/* Start of interlace block in the y direction */
static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};

/* Offset to next interlace block in the y direction */
static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};

unsigned int max_pixel_depth;
size_t row_bytes;

Expand Down
58 changes: 18 additions & 40 deletions pngwutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,30 @@
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
* and license in png.h
*
* This file contains routines that are only called from within
* libpng itself during the course of writing an image.
*/

#include "pngpriv.h"

#ifdef PNG_WRITE_SUPPORTED

#ifdef PNG_WRITE_INTERLACING_SUPPORTED
/* Arrays to facilitate interlacing - use pass (0 - 6) as index. */

/* Start of interlace block */
static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
/* Offset to next interlace block */
static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
/* Start of interlace block in the y direction */
static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
/* Offset to next interlace block in the y direction */
static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};

/* TODO: Move these arrays to a common utility module to avoid duplication. */
#endif

#ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED
/* Place a 32-bit number into a buffer in PNG byte order. We work
* with unsigned numbers for convenience, although one supported
Expand Down Expand Up @@ -1908,22 +1926,6 @@ png_write_tIME(png_structrp png_ptr, png_const_timep mod_time)
void /* PRIVATE */
png_write_start_row(png_structrp png_ptr)
{
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */

/* Start of interlace block */
static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};

/* Offset to next interlace block */
static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};

/* Start of interlace block in the y direction */
static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};

/* Offset to next interlace block in the y direction */
static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
#endif

png_alloc_size_t buf_size;
int usr_pixel_depth;

Expand Down Expand Up @@ -2023,22 +2025,6 @@ png_write_start_row(png_structrp png_ptr)
void /* PRIVATE */
png_write_finish_row(png_structrp png_ptr)
{
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */

/* Start of interlace block */
static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};

/* Offset to next interlace block */
static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};

/* Start of interlace block in the y direction */
static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};

/* Offset to next interlace block in the y direction */
static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
#endif

png_debug(1, "in png_write_finish_row");

/* Next row */
Expand Down Expand Up @@ -2114,14 +2100,6 @@ png_write_finish_row(png_structrp png_ptr)
void /* PRIVATE */
png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
{
/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */

/* Start of interlace block */
static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};

/* Offset to next interlace block */
static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};

png_debug(1, "in png_do_write_interlace");

/* We don't have to do anything on the last pass (6) */
Expand Down

0 comments on commit 620a2b7

Please sign in to comment.