source: svn/newcon3bcm2_21bu/dst/dlib/src/PNG/pngwtran.c

Last change on this file was 76, checked in by megakiss, 10 years ago

1W 대기전력을 만족시키기 위하여 POWEROFF시 튜너를 Standby 상태로 함

  • Property svn:executable set to *
File size: 15.1 KB
Line 
1/*
2 * $Id: //suprahd/releases/suprahd_163/suprahd_ztvapp640_163/drivers/graphics/PNG/lpng102/pngwtran.c#1 $
3 * $Revision: #1 $
4 * $DateTime: 2006/02/24 17:51:46 $
5 * $Change: 42566 $
6 * $Author: pryush.sharma $
7 */
8
9
10/* pngwtran.c - transforms the data in a row for PNG writers
11 *
12 * libpng 1.0.2 - June 14, 1998
13 * For conditions of distribution and use, see copyright notice in png.h
14 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
15 * Copyright (c) 1996, 1997 Andreas Dilger
16 * Copyright (c) 1998, Glenn Randers-Pehrson
17 */
18
19#define PNG_INTERNAL
20#include "png.h"
21
22/* Transform the data according to the user's wishes.  The order of
23 * transformations is significant.
24 */
25void
26png_do_write_transformations(png_structp png_ptr)
27{
28   png_debug(1, "in png_do_write_transformations\n");
29
30#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
31   if (png_ptr->transformations & PNG_USER_TRANSFORM)
32      if(png_ptr->write_user_transform_fn != NULL)
33        (*(png_ptr->write_user_transform_fn)) /* user write transform function */
34          (png_ptr,                    /* png_ptr */
35           &(png_ptr->row_info),       /* row_info:     */
36             /*  png_uint_32 width;          width of row */
37             /*  png_uint_32 rowbytes;       number of bytes in row */
38             /*  png_byte color_type;        color type of pixels */
39             /*  png_byte bit_depth;         bit depth of samples */
40             /*  png_byte channels;          number of channels (1-4) */
41             /*  png_byte pixel_depth;       bits per pixel (depth*channels) */
42           png_ptr->row_buf + 1);      /* start of pixel data for row */
43#endif
44#if defined(PNG_WRITE_FILLER_SUPPORTED)
45   if (png_ptr->transformations & PNG_FILLER)
46      png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
47         png_ptr->flags);
48#endif
49#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
50   if (png_ptr->transformations & PNG_PACKSWAP)
51      png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
52#endif
53#if defined(PNG_WRITE_PACK_SUPPORTED)
54   if (png_ptr->transformations & PNG_PACK)
55      png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
56         (png_uint_32)png_ptr->bit_depth);
57#endif
58#if defined(PNG_WRITE_SHIFT_SUPPORTED)
59   if (png_ptr->transformations & PNG_SHIFT)
60      png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
61         &(png_ptr->shift));
62#endif
63#if defined(PNG_WRITE_SWAP_SUPPORTED)
64   if (png_ptr->transformations & PNG_SWAP_BYTES)
65      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
66#endif
67#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
68   if (png_ptr->transformations & PNG_INVERT_ALPHA)
69      png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
70#endif
71#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
72   if (png_ptr->transformations & PNG_SWAP_ALPHA)
73      png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
74#endif
75#if defined(PNG_WRITE_BGR_SUPPORTED)
76   if (png_ptr->transformations & PNG_BGR)
77      png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
78#endif
79#if defined(PNG_WRITE_INVERT_SUPPORTED)
80   if (png_ptr->transformations & PNG_INVERT_MONO)
81      png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
82#endif
83}
84
85#if defined(PNG_WRITE_PACK_SUPPORTED)
86/* Pack pixels into bytes.  Pass the true bit depth in bit_depth.  The
87 * row_info bit depth should be 8 (one pixel per byte).  The channels
88 * should be 1 (this only happens on grayscale and paletted images).
89 */
90void
91png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
92{
93   png_debug(1, "in png_do_pack\n");
94   if (row_info->bit_depth == 8 &&
95#if defined(PNG_USELESS_TESTS_SUPPORTED)
96       row != NULL && row_info != NULL &&
97#endif
98      row_info->channels == 1)
99   {
100      switch ((int)bit_depth)
101      {
102         case 1:
103         {
104            png_bytep sp, dp;
105            int mask, v;
106            png_uint_32 i;
107            png_uint_32 row_width = row_info->width;
108
109            sp = row;
110            dp = row;
111            mask = 0x80;
112            v = 0;
113           
114            for (i = 0; i < row_width; i++)
115            {
116               if (*sp != 0)
117                  v |= mask;
118               sp++;
119               if (mask > 1)
120                  mask >>= 1;
121               else
122               {
123                  mask = 0x80;
124                  *dp = (png_byte)v;
125                  dp++;
126                  v = 0;
127               }
128            }
129            if (mask != 0x80)
130               *dp = (png_byte)v;
131            break;
132         }
133         case 2:
134         {
135            png_bytep sp, dp;
136            int shift, v;
137            png_uint_32 i;
138            png_uint_32 row_width = row_info->width;
139
140            sp = row;
141            dp = row;
142            shift = 6;
143            v = 0;
144            for (i = 0; i < row_width; i++)
145            {
146               png_byte value;
147
148               value = (png_byte)(*sp & 0x3);
149               v |= (value << shift);
150               if (shift == 0)
151               {
152                  shift = 6;
153                  *dp = (png_byte)v;
154                  dp++;
155                  v = 0;
156               }
157               else
158                  shift -= 2;
159               sp++;
160            }
161            if (shift != 6)
162               *dp = (png_byte)v;
163            break;
164         }
165         case 4:
166         {
167            png_bytep sp, dp;
168            int shift, v;
169            png_uint_32 i;
170            png_uint_32 row_width = row_info->width;
171
172            sp = row;
173            dp = row;
174            shift = 4;
175            v = 0;
176            for (i = 0; i < row_width; i++)
177            {
178               png_byte value;
179
180               value = (png_byte)(*sp & 0xf);
181               v |= (value << shift);
182
183               if (shift == 0)
184               {
185                  shift = 4;
186                  *dp = (png_byte)v;
187                  dp++;
188                  v = 0;
189               }
190               else
191                  shift -= 4;
192
193               sp++;
194            }
195            if (shift != 4)
196               *dp = (png_byte)v;
197            break;
198         }
199      }
200      row_info->bit_depth = (png_byte)bit_depth;
201      row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
202      row_info->rowbytes =
203         ((row_info->width * row_info->pixel_depth + 7) >> 3);
204   }
205}
206#endif
207
208#if defined(PNG_WRITE_SHIFT_SUPPORTED)
209/* Shift pixel values to take advantage of whole range.  Pass the
210 * true number of bits in bit_depth.  The row should be packed
211 * according to row_info->bit_depth.  Thus, if you had a row of
212 * bit depth 4, but the pixels only had values from 0 to 7, you
213 * would pass 3 as bit_depth, and this routine would translate the
214 * data to 0 to 15.
215 */
216void
217png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
218{
219   png_debug(1, "in png_do_shift\n");
220#if defined(PNG_USELESS_TESTS_SUPPORTED)
221   if (row != NULL && row_info != NULL &&
222#else
223   if (
224#endif
225      row_info->color_type != PNG_COLOR_TYPE_PALETTE)
226   {
227      int shift_start[4], shift_dec[4];
228      int channels = 0;
229
230      if (row_info->color_type & PNG_COLOR_MASK_COLOR)
231      {
232         shift_start[channels] = row_info->bit_depth - bit_depth->red;
233         shift_dec[channels] = bit_depth->red;
234         channels++;
235         shift_start[channels] = row_info->bit_depth - bit_depth->green;
236         shift_dec[channels] = bit_depth->green;
237         channels++;
238         shift_start[channels] = row_info->bit_depth - bit_depth->blue;
239         shift_dec[channels] = bit_depth->blue;
240         channels++;
241      }
242      else
243      {
244         shift_start[channels] = row_info->bit_depth - bit_depth->gray;
245         shift_dec[channels] = bit_depth->gray;
246         channels++;
247      }
248      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
249      {
250         shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
251         shift_dec[channels] = bit_depth->alpha;
252         channels++;
253      }
254
255      /* with low row depths, could only be grayscale, so one channel */
256      if (row_info->bit_depth < 8)
257      {
258         png_bytep bp = row;
259         png_uint_32 i;
260         png_byte mask;
261         png_uint_32 row_bytes = row_info->rowbytes;
262
263         if (bit_depth->gray == 1 && row_info->bit_depth == 2)
264            mask = 0x55;
265         else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
266            mask = 0x11;
267         else
268            mask = 0xff;
269
270         for (i = 0; i < row_bytes; i++, bp++)
271         {
272            png_uint_16 v;
273            int j;
274
275            v = *bp;
276            *bp = 0;
277            for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
278            {
279               if (j > 0)
280                  *bp |= (png_byte)((v << j) & 0xff);
281               else
282                  *bp |= (png_byte)((v >> (-j)) & mask);
283            }
284         }
285      }
286      else if (row_info->bit_depth == 8)
287      {
288         png_bytep bp = row;
289         png_uint_32 i;
290         png_uint_32 istop = channels * row_info->width;
291
292         for (i = 0; i < istop; i++, bp++)
293         {
294
295            png_uint_16 v;
296            int j;
297            int c = (int)(i%channels);
298
299            v = *bp;
300            *bp = 0;
301            for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
302            {
303               if (j > 0)
304                  *bp |= (png_byte)((v << j) & 0xff);
305               else
306                  *bp |= (png_byte)((v >> (-j)) & 0xff);
307            }
308         }
309      }
310      else
311      {
312         png_bytep bp;
313         png_uint_32 i;
314         png_uint_32 istop = channels * row_info->width;
315
316         for (bp = row, i = 0; i < istop; i++)
317         {
318            int c = (int)(i%channels);
319            png_uint_16 value, v;
320            int j;
321
322            v = ((png_uint_16)(*bp) << 8) + *(bp + 1);
323            value = 0;
324            for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
325            {
326               if (j > 0)
327                  value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
328               else
329                  value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
330            }
331            *bp++ = (png_byte)(value >> 8);
332            *bp++ = (png_byte)(value & 0xff);
333         }
334      }
335   }
336}
337#endif
338
339#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
340void
341png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
342{
343   png_debug(1, "in png_do_write_swap_alpha\n");
344#if defined(PNG_USELESS_TESTS_SUPPORTED)
345   if (row != NULL && row_info != NULL)
346#endif
347   {
348      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
349      {
350         /* This converts from ARGB to RGBA */
351         if (row_info->bit_depth == 8)
352         {
353            png_bytep sp, dp;
354            png_uint_32 i;
355            png_uint_32 row_width = row_info->width;
356            for (i = 0, sp = dp = row; i < row_width; i++)
357            {
358               png_byte save = *(sp++);
359               *(dp++) = *(sp++);
360               *(dp++) = *(sp++);
361               *(dp++) = *(sp++);
362               *(dp++) = save;
363            }
364         }
365         /* This converts from AARRGGBB to RRGGBBAA */
366         else
367         {
368            png_bytep sp, dp;
369            png_uint_32 i;
370            png_uint_32 row_width = row_info->width;
371
372            for (i = 0, sp = dp = row; i < row_width; i++)
373            {
374               png_byte save[2];
375               save[0] = *(sp++);
376               save[1] = *(sp++);
377               *(dp++) = *(sp++);
378               *(dp++) = *(sp++);
379               *(dp++) = *(sp++);
380               *(dp++) = *(sp++);
381               *(dp++) = *(sp++);
382               *(dp++) = *(sp++);
383               *(dp++) = save[0];
384               *(dp++) = save[1];
385            }
386         }
387      }
388      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
389      {
390         /* This converts from AG to GA */
391         if (row_info->bit_depth == 8)
392         {
393            png_bytep sp, dp;
394            png_uint_32 i;
395            png_uint_32 row_width = row_info->width;
396
397            for (i = 0, sp = dp = row; i < row_width; i++)
398            {
399               png_byte save = *(sp++);
400               *(dp++) = *(sp++);
401               *(dp++) = save;
402            }
403         }
404         /* This converts from AAGG to GGAA */
405         else
406         {
407            png_bytep sp, dp;
408            png_uint_32 i;
409            png_uint_32 row_width = row_info->width;
410
411            for (i = 0, sp = dp = row; i < row_width; i++)
412            {
413               png_byte save[2];
414               save[0] = *(sp++);
415               save[1] = *(sp++);
416               *(dp++) = *(sp++);
417               *(dp++) = *(sp++);
418               *(dp++) = save[0];
419               *(dp++) = save[1];
420            }
421         }
422      }
423   }
424}
425#endif
426
427#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
428void
429png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
430{
431   png_debug(1, "in png_do_write_invert_alpha\n");
432#if defined(PNG_USELESS_TESTS_SUPPORTED)
433   if (row != NULL && row_info != NULL)
434#endif
435   {
436      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
437      {
438         /* This inverts the alpha channel in RGBA */
439         if (row_info->bit_depth == 8)
440         {
441            png_bytep sp, dp;
442            png_uint_32 i;
443            png_uint_32 row_width = row_info->width;
444            for (i = 0, sp = dp = row; i < row_width; i++)
445            {
446               *(dp++) = *(sp++);
447               *(dp++) = *(sp++);
448               *(dp++) = *(sp++);
449               *(dp++) = 255 - *(sp++);
450            }
451         }
452         /* This inverts the alpha channel in RRGGBBAA */
453         else
454         {
455            png_bytep sp, dp;
456            png_uint_32 i;
457            png_uint_32 row_width = row_info->width;
458
459            for (i = 0, sp = dp = row; i < row_width; i++)
460            {
461               *(dp++) = *(sp++);
462               *(dp++) = *(sp++);
463               *(dp++) = *(sp++);
464               *(dp++) = *(sp++);
465               *(dp++) = *(sp++);
466               *(dp++) = *(sp++);
467               *(dp++) = 255 - *(sp++);
468               *(dp++) = 255 - *(sp++);
469            }
470         }
471      }
472      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
473      {
474         /* This inverts the alpha channel in GA */
475         if (row_info->bit_depth == 8)
476         {
477            png_bytep sp, dp;
478            png_uint_32 i;
479            png_uint_32 row_width = row_info->width;
480
481            for (i = 0, sp = dp = row; i < row_width; i++)
482            {
483               *(dp++) = *(sp++);
484               *(dp++) = 255 - *(sp++);
485            }
486         }
487         /* This inverts the alpha channel in GGAA */
488         else
489         {
490            png_bytep sp, dp;
491            png_uint_32 i;
492            png_uint_32 row_width = row_info->width;
493
494            for (i = 0, sp = dp = row; i < row_width; i++)
495            {
496               *(dp++) = *(sp++);
497               *(dp++) = *(sp++);
498               *(dp++) = 255 - *(sp++);
499               *(dp++) = 255 - *(sp++);
500            }
501         }
502      }
503   }
504}
505#endif
Note: See TracBrowser for help on using the repository browser.