pngtrans.c 4.9 KB
Newer Older
G
Guy Schalnat 已提交
1 2 3 4

/* pngtrans.c - transforms the data in a row
   routines used by both readers and writers

G
Guy Schalnat 已提交
5
   libpng 1.0 beta 2 - version 0.81
G
Guy Schalnat 已提交
6 7
   For conditions of distribution and use, see copyright notice in png.h
   Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
G
Guy Schalnat 已提交
8
   August 24, 1995
G
Guy Schalnat 已提交
9 10 11 12 13
   */

#define PNG_INTERNAL
#include "png.h"

G
Guy Schalnat 已提交
14
#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
G
Guy Schalnat 已提交
15 16 17 18 19 20
/* turn on bgr to rgb mapping */
void
png_set_bgr(png_struct *png_ptr)
{
   png_ptr->transformations |= PNG_BGR;
}
G
Guy Schalnat 已提交
21
#endif
G
Guy Schalnat 已提交
22

G
Guy Schalnat 已提交
23
#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
G
Guy Schalnat 已提交
24 25 26 27 28 29 30
/* turn on 16 bit byte swapping */
void
png_set_swap(png_struct *png_ptr)
{
   if (png_ptr->bit_depth == 16)
      png_ptr->transformations |= PNG_SWAP_BYTES;
}
G
Guy Schalnat 已提交
31
#endif
G
Guy Schalnat 已提交
32

G
Guy Schalnat 已提交
33
#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
G
Guy Schalnat 已提交
34 35 36 37 38 39 40 41 42 43
/* turn on pixel packing */
void
png_set_packing(png_struct *png_ptr)
{
   if (png_ptr->bit_depth < 8)
   {
      png_ptr->transformations |= PNG_PACK;
      png_ptr->usr_bit_depth = 8;
   }
}
G
Guy Schalnat 已提交
44
#endif
G
Guy Schalnat 已提交
45

G
Guy Schalnat 已提交
46
#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
G
Guy Schalnat 已提交
47 48 49 50 51 52
void
png_set_shift(png_struct *png_ptr, png_color_8 *true_bits)
{
   png_ptr->transformations |= PNG_SHIFT;
   png_ptr->shift = *true_bits;
}
G
Guy Schalnat 已提交
53
#endif
G
Guy Schalnat 已提交
54

G
Guy Schalnat 已提交
55
#if defined(PNG_READ_INTERLACING_SUPPORTED) || defined(PNG_WRITE_INTERLACING_SUPPORTED)
G
Guy Schalnat 已提交
56 57 58 59 60 61 62 63 64 65 66
int
png_set_interlace_handling(png_struct *png_ptr)
{
   if (png_ptr->interlaced)
   {
      png_ptr->transformations |= PNG_INTERLACE;
      return 7;
   }

   return 1;
}
G
Guy Schalnat 已提交
67
#endif
G
Guy Schalnat 已提交
68

G
Guy Schalnat 已提交
69
#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
G
Guy Schalnat 已提交
70
void
G
Guy Schalnat 已提交
71
png_set_filler(png_struct *png_ptr, int filler, int filler_loc)
G
Guy Schalnat 已提交
72
{
G
Guy Schalnat 已提交
73 74 75
   png_ptr->transformations |= PNG_FILLER;
   png_ptr->filler = (png_byte)filler;
   png_ptr->filler_loc = (png_byte)filler_loc;
G
Guy Schalnat 已提交
76 77 78 79 80
   if (png_ptr->color_type == PNG_COLOR_TYPE_RGB &&
      png_ptr->bit_depth == 8)
      png_ptr->usr_channels = 4;
}

G
Guy Schalnat 已提交
81 82 83 84 85 86 87
/* old functions kept around for compatability purposes */
void
png_set_rgbx(png_struct *png_ptr)
{
   png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
}

G
Guy Schalnat 已提交
88 89 90
void
png_set_xrgb(png_struct *png_ptr)
{
G
Guy Schalnat 已提交
91
   png_set_filler(png_ptr, 0xff, PNG_FILLER_BEFORE);
G
Guy Schalnat 已提交
92
}
G
Guy Schalnat 已提交
93
#endif
G
Guy Schalnat 已提交
94

G
Guy Schalnat 已提交
95
#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
G
Guy Schalnat 已提交
96 97 98 99 100 101 102 103
void
png_set_invert_mono(png_struct *png_ptr)
{
   png_ptr->transformations |= PNG_INVERT_MONO;
}

/* invert monocrome grayscale data */
void
G
Guy Schalnat 已提交
104
png_do_invert(png_row_info *row_info, png_bytef *row)
G
Guy Schalnat 已提交
105 106 107 108
{
   if (row && row_info && row_info->bit_depth == 1 &&
      row_info->color_type == PNG_COLOR_TYPE_GRAY)
   {
G
Guy Schalnat 已提交
109
      png_bytef *rp;
G
Guy Schalnat 已提交
110 111 112 113 114 115 116 117 118 119
      png_uint_32 i;

      for (i = 0, rp = row;
         i < row_info->rowbytes;
         i++, rp++)
      {
         *rp = ~(*rp);
      }
   }
}
G
Guy Schalnat 已提交
120
#endif
G
Guy Schalnat 已提交
121

G
Guy Schalnat 已提交
122
#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
G
Guy Schalnat 已提交
123 124
/* swaps byte order on 16 bit depth images */
void
G
Guy Schalnat 已提交
125
png_do_swap(png_row_info *row_info, png_bytef *row)
G
Guy Schalnat 已提交
126 127 128
{
   if (row && row_info && row_info->bit_depth == 16)
   {
G
Guy Schalnat 已提交
129 130
      png_bytef *rp;
      png_byte t;
G
Guy Schalnat 已提交
131 132 133 134 135 136 137 138 139 140 141 142
      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;
      }
   }
}
G
Guy Schalnat 已提交
143
#endif
G
Guy Schalnat 已提交
144

G
Guy Schalnat 已提交
145
#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
G
Guy Schalnat 已提交
146 147
/* swaps red and blue */
void
G
Guy Schalnat 已提交
148
png_do_bgr(png_row_info *row_info, png_bytef *row)
G
Guy Schalnat 已提交
149 150 151 152 153
{
   if (row && row_info && (row_info->color_type & 2))
   {
      if (row_info->color_type == 2 && row_info->bit_depth == 8)
      {
G
Guy Schalnat 已提交
154 155
         png_bytef *rp;
         png_byte t;
G
Guy Schalnat 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168
         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)
      {
G
Guy Schalnat 已提交
169 170
         png_bytef *rp;
         png_byte t;
G
Guy Schalnat 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183
         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)
      {
G
Guy Schalnat 已提交
184 185
         png_bytef *rp;
         png_byte t[2];
G
Guy Schalnat 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
         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)
      {
G
Guy Schalnat 已提交
202 203
         png_bytef *rp;
         png_byte t[2];
G
Guy Schalnat 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
         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];
         }
      }
   }
}
G
Guy Schalnat 已提交
220
#endif
G
Guy Schalnat 已提交
221