This repository has been archived on 2023-11-11. You can view files and clone it, but cannot push or open issues or pull requests.
libpng/pngtrans.c

229 lines
5.3 KiB
C
Raw Normal View History

1995-07-20 07:43:20 +00:00
/* pngtrans.c - transforms the data in a row
routines used by both readers and writers
1997-01-17 07:34:35 +00:00
libpng 1.0 beta 4 - version 0.90
1995-07-20 07:43:20 +00:00
For conditions of distribution and use, see copyright notice in png.h
1996-01-26 07:38:47 +00:00
Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
1997-01-17 07:34:35 +00:00
January 10, 1997
1995-07-20 07:43:20 +00:00
*/
#define PNG_INTERNAL
#include "png.h"
1995-09-26 10:22:39 +00:00
#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
1995-07-20 07:43:20 +00:00
/* turn on bgr to rgb mapping */
void
1995-12-19 09:22:19 +00:00
png_set_bgr(png_structp png_ptr)
1995-07-20 07:43:20 +00:00
{
png_ptr->transformations |= PNG_BGR;
}
1995-09-26 10:22:39 +00:00
#endif
1995-07-20 07:43:20 +00:00
1995-09-26 10:22:39 +00:00
#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
1995-07-20 07:43:20 +00:00
/* turn on 16 bit byte swapping */
void
1995-12-19 09:22:19 +00:00
png_set_swap(png_structp png_ptr)
1995-07-20 07:43:20 +00:00
{
if (png_ptr->bit_depth == 16)
png_ptr->transformations |= PNG_SWAP_BYTES;
}
1995-09-26 10:22:39 +00:00
#endif
1995-07-20 07:43:20 +00:00
1995-09-26 10:22:39 +00:00
#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
1995-07-20 07:43:20 +00:00
/* turn on pixel packing */
void
1995-12-19 09:22:19 +00:00
png_set_packing(png_structp png_ptr)
1995-07-20 07:43:20 +00:00
{
if (png_ptr->bit_depth < 8)
{
png_ptr->transformations |= PNG_PACK;
png_ptr->usr_bit_depth = 8;
}
}
1995-09-26 10:22:39 +00:00
#endif
1995-07-20 07:43:20 +00:00
1995-09-26 10:22:39 +00:00
#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
1995-07-20 07:43:20 +00:00
void
1995-12-19 09:22:19 +00:00
png_set_shift(png_structp png_ptr, png_color_8p true_bits)
1995-07-20 07:43:20 +00:00
{
png_ptr->transformations |= PNG_SHIFT;
png_ptr->shift = *true_bits;
}
1995-09-26 10:22:39 +00:00
#endif
1995-07-20 07:43:20 +00:00
1995-09-26 10:22:39 +00:00
#if defined(PNG_READ_INTERLACING_SUPPORTED) || defined(PNG_WRITE_INTERLACING_SUPPORTED)
1995-07-20 07:43:20 +00:00
int
1995-12-19 09:22:19 +00:00
png_set_interlace_handling(png_structp png_ptr)
1995-07-20 07:43:20 +00:00
{
if (png_ptr->interlaced)
{
png_ptr->transformations |= PNG_INTERLACE;
return 7;
}
return 1;
}
1995-09-26 10:22:39 +00:00
#endif
1995-07-20 07:43:20 +00:00
1995-09-26 10:22:39 +00:00
#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
1995-07-20 07:43:20 +00:00
void
1997-01-17 07:34:35 +00:00
png_set_filler(png_structp png_ptr, png_byte filler, int filler_loc)
1995-07-20 07:43:20 +00:00
{
1995-09-26 10:22:39 +00:00
png_ptr->transformations |= PNG_FILLER;
1997-01-17 07:34:35 +00:00
png_ptr->filler = filler;
1996-06-05 20:50:50 +00:00
if (filler_loc == PNG_FILLER_AFTER)
png_ptr->flags |= PNG_FLAG_FILLER_AFTER;
else
png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER;
1995-07-20 07:43:20 +00:00
if (png_ptr->color_type == PNG_COLOR_TYPE_RGB &&
png_ptr->bit_depth == 8)
png_ptr->usr_channels = 4;
}
1997-01-17 07:34:35 +00:00
/* Old functions kept around for compatability purposes. They will be
* removed at some time in the future, so don't use them. You should
* use png_set_filler() above instead. We set filler bytes to 0xff in
* case they are mistakenly used as PNG alpha (0xff is fully opaque). */
1995-09-26 10:22:39 +00:00
void
1995-12-19 09:22:19 +00:00
png_set_rgbx(png_structp png_ptr)
1995-09-26 10:22:39 +00:00
{
1997-01-17 07:34:35 +00:00
png_set_filler(png_ptr, (png_byte)0xff, PNG_FILLER_AFTER);
1995-09-26 10:22:39 +00:00
}
1995-07-20 07:43:20 +00:00
void
1995-12-19 09:22:19 +00:00
png_set_xrgb(png_structp png_ptr)
1995-07-20 07:43:20 +00:00
{
1997-01-17 07:34:35 +00:00
png_set_filler(png_ptr, (png_byte)0xff, PNG_FILLER_BEFORE);
1995-07-20 07:43:20 +00:00
}
1995-09-26 10:22:39 +00:00
#endif
1995-07-20 07:43:20 +00:00
1995-09-26 10:22:39 +00:00
#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
1995-07-20 07:43:20 +00:00
void
1995-12-19 09:22:19 +00:00
png_set_invert_mono(png_structp png_ptr)
1995-07-20 07:43:20 +00:00
{
png_ptr->transformations |= PNG_INVERT_MONO;
}
/* invert monocrome grayscale data */
void
1995-12-19 09:22:19 +00:00
png_do_invert(png_row_infop row_info, png_bytep row)
1995-07-20 07:43:20 +00:00
{
if (row && row_info && row_info->bit_depth == 1 &&
row_info->color_type == PNG_COLOR_TYPE_GRAY)
{
1995-12-19 09:22:19 +00:00
png_bytep rp;
1995-07-20 07:43:20 +00:00
png_uint_32 i;
for (i = 0, rp = row;
i < row_info->rowbytes;
i++, rp++)
{
1996-01-16 07:51:56 +00:00
*rp = (png_byte)(~(*rp));
1995-07-20 07:43:20 +00:00
}
}
}
1995-09-26 10:22:39 +00:00
#endif
1995-07-20 07:43:20 +00:00
1995-09-26 10:22:39 +00:00
#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
1995-07-20 07:43:20 +00:00
/* swaps byte order on 16 bit depth images */
void
1995-12-19 09:22:19 +00:00
png_do_swap(png_row_infop row_info, png_bytep row)
1995-07-20 07:43:20 +00:00
{
if (row && row_info && row_info->bit_depth == 16)
{
1995-12-19 09:22:19 +00:00
png_bytep rp;
1995-09-26 10:22:39 +00:00
png_byte t;
1995-07-20 07:43:20 +00:00
png_uint_32 i;
for (i = 0, rp = row;
i < row_info->width * row_info->channels;
i++, rp += 2)
{
t = *rp;
*rp = *(rp + 1);
*(rp + 1) = t;
}
}
}
1995-09-26 10:22:39 +00:00
#endif
1995-07-20 07:43:20 +00:00
1995-09-26 10:22:39 +00:00
#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
1995-07-20 07:43:20 +00:00
/* swaps red and blue */
void
1995-12-19 09:22:19 +00:00
png_do_bgr(png_row_infop row_info, png_bytep row)
1995-07-20 07:43:20 +00:00
{
if (row && row_info && (row_info->color_type & 2))
{
if (row_info->color_type == 2 && row_info->bit_depth == 8)
{
1995-12-19 09:22:19 +00:00
png_bytep rp;
1995-09-26 10:22:39 +00:00
png_byte t;
1995-07-20 07:43:20 +00:00
png_uint_32 i;
for (i = 0, rp = row;
i < row_info->width;
i++, rp += 3)
{
t = *rp;
*rp = *(rp + 2);
*(rp + 2) = t;
}
}
else if (row_info->color_type == 6 && row_info->bit_depth == 8)
{
1995-12-19 09:22:19 +00:00
png_bytep rp;
1995-09-26 10:22:39 +00:00
png_byte t;
1995-07-20 07:43:20 +00:00
png_uint_32 i;
for (i = 0, rp = row;
i < row_info->width;
i++, rp += 4)
{
t = *rp;
*rp = *(rp + 2);
*(rp + 2) = t;
}
}
else if (row_info->color_type == 2 && row_info->bit_depth == 16)
{
1995-12-19 09:22:19 +00:00
png_bytep rp;
1995-09-26 10:22:39 +00:00
png_byte t[2];
1995-07-20 07:43:20 +00:00
png_uint_32 i;
for (i = 0, rp = row;
i < row_info->width;
i++, rp += 6)
{
t[0] = *rp;
t[1] = *(rp + 1);
*rp = *(rp + 4);
*(rp + 1) = *(rp + 5);
*(rp + 4) = t[0];
*(rp + 5) = t[1];
}
}
else if (row_info->color_type == 6 && row_info->bit_depth == 16)
{
1996-01-26 07:38:47 +00:00
png_bytep rp;
1995-09-26 10:22:39 +00:00
png_byte t[2];
1995-07-20 07:43:20 +00:00
png_uint_32 i;
for (i = 0, rp = row;
i < row_info->width;
i++, rp += 8)
{
t[0] = *rp;
t[1] = *(rp + 1);
*rp = *(rp + 4);
*(rp + 1) = *(rp + 5);
*(rp + 4) = t[0];
*(rp + 5) = t[1];
}
}
}
}
1995-09-26 10:22:39 +00:00
#endif
1995-07-20 07:43:20 +00:00