2 * Copyright (C) 2003-2006 Gabest
3 * http://www.gabest.org
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNU Make; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 #include "..\..\libpng\png.h"
27 static void read_data_fn(png_structp png_ptr
, png_bytep data
, png_size_t length
)
29 struct png_t
* png
= (struct png_t
*)png_get_progressive_ptr(png_ptr
);
30 if(png
->pos
+ length
> png
->size
) png_error(png_ptr
, "Read Error");
31 memcpy(data
, &png
->data
[png
->pos
], length
);
35 unsigned char* DecompressPNG(struct png_t
* png
, int* w
, int* h
)
45 if(png_sig_cmp(png
->data
, 0, 8) != 0)
50 png_ptr
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
51 // (png_voidp)user_error_ptr, user_error_fn, user_warning_fn);
55 png_set_read_fn(png_ptr
, (png_voidp
)png
, read_data_fn
);
57 info_ptr
= png_create_info_struct(png_ptr
);
60 png_destroy_read_struct(&png_ptr
, (png_infopp
)NULL
, (png_infopp
)NULL
);
64 end_info
= png_create_info_struct(png_ptr
);
67 png_destroy_read_struct(&png_ptr
, &info_ptr
, (png_infopp
)NULL
);
71 if(setjmp(png_jmpbuf(png_ptr
)))
73 png_destroy_read_struct(&png_ptr
, &info_ptr
, &end_info
);
77 png_set_sig_bytes(png_ptr
, 8);
81 PNG_TRANSFORM_STRIP_16
|
82 PNG_TRANSFORM_STRIP_ALPHA
|
83 PNG_TRANSFORM_PACKING
|
84 PNG_TRANSFORM_EXPAND
|
88 if(png_get_channels(png_ptr
, info_ptr
) != 3)
90 png_destroy_read_struct(&png_ptr
, &info_ptr
, &end_info
);
94 pic
= calloc(info_ptr
->width
* info_ptr
->height
, 4);
97 *h
= info_ptr
->height
;
99 for(y
= 0; y
< info_ptr
->height
; y
++)
101 row
= &pic
[y
* info_ptr
->width
* 4];
103 for(x
= 0; x
< info_ptr
->width
*3; row
+= 4)
105 for(c
= 0; c
< 3; c
++)
107 row
[c
] = info_ptr
->row_pointers
[y
][x
++];
112 png_destroy_read_struct(&png_ptr
, &info_ptr
, &end_info
);