-void LoadImage (const char *filename, unsigned char **pic, int *width, int *height)
-{
- png_byte** row_pointers;
- unsigned char *fbuffer = NULL;
- png_bytep p_fbuffer;
-
- int nLen = g_FileSystemTable.m_pfnLoadFile( (char *)filename, (void **)&fbuffer, 0 );
- if (nLen == -1)
- return;
-
- p_fbuffer = fbuffer;
-
- // the reading glue
- // http://www.libpng.org/pub/png/libpng-manual.html
-
- png_structp png_ptr = png_create_read_struct
- (PNG_LIBPNG_VER_STRING, png_voidp_NULL,
- user_error_fn, user_warning_fn);
- if (!png_ptr)
- {
- g_FuncTable.m_pfnSysPrintf ("libpng error: png_create_read_struct\n");
- return;
- }
-
- png_infop info_ptr = png_create_info_struct(png_ptr);
- if (!info_ptr) {
- png_destroy_read_struct(&png_ptr,
- png_infopp_NULL, png_infopp_NULL);
- g_FuncTable.m_pfnSysPrintf ("libpng error: png_create_info_struct (info_ptr)\n");
- return;
- }
-
- png_infop end_info = png_create_info_struct(png_ptr);
- if (!end_info) {
- png_destroy_read_struct(&png_ptr, &info_ptr,
- png_infopp_NULL);
- g_FuncTable.m_pfnSysPrintf ("libpng error: png_create_info_struct (end_info)\n");
- return;
- }
-
- // configure the read function
- png_set_read_fn(png_ptr, (voidp)&p_fbuffer, (png_rw_ptr)&user_read_data);
-
- if (setjmp(png_ptr->jmpbuf)) {
- png_destroy_read_struct(&png_ptr, &info_ptr,
- &end_info);
- if (*pic)
- {
- g_free(*pic);
- free(row_pointers);
+ if (png_get_bKGD(png_ptr, info_ptr, &image_background)) {
+ png_set_background(png_ptr, image_background,
+ PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
+ } else {
+ png_set_background(png_ptr, &my_background,
+ PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
+ }
+
+ // Add alpha byte after each RGB triplet
+ png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
+ }
+
+ // read the sucker in one chunk
+ png_read_update_info(png_ptr, info_ptr);
+
+ color_type = png_get_color_type(png_ptr, info_ptr);
+ bit_depth = png_get_bit_depth(png_ptr, info_ptr);
+
+ int width = png_get_image_width(png_ptr, info_ptr);
+ int height = png_get_image_height(png_ptr, info_ptr);
+
+ // allocate the pixel buffer, and the row pointers
+ RGBAImage *image = new RGBAImage(width, height);
+
+ row_pointers = (png_byte **) malloc((height) * sizeof(png_byte *));
+
+ int i;
+ for (i = 0; i < (height); i++) {
+ row_pointers[i] = (png_byte *) (image->getRGBAPixels()) + i * 4 * (width);