X64 transport [Part 5] (Update plugins.cpp)
[xy_vsfilter.git] / src / apps / mplayerc / libpng.c
blobd484d61424b82375b60a82696072216b84c1f1fe
1 /*
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)
8 * any later version.
9 *
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
22 #include <stdio.h>
23 #include <malloc.h>
24 #include "libpng.h"
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);
32 png->pos += length;
35 unsigned char* DecompressPNG(struct png_t* png, int* w, int* h)
37 png_structp png_ptr;
38 png_infop info_ptr;
39 png_infop end_info;
41 unsigned char* pic;
42 unsigned char* row;
43 unsigned int x, y, c;
45 if(png_sig_cmp(png->data, 0, 8) != 0)
46 return NULL;
48 png->pos = 8;
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);
52 if(!png_ptr)
53 return NULL;
55 png_set_read_fn(png_ptr, (png_voidp)png, read_data_fn);
57 info_ptr = png_create_info_struct(png_ptr);
58 if(!info_ptr)
60 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
61 return NULL;
64 end_info = png_create_info_struct(png_ptr);
65 if(!end_info)
67 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
68 return NULL;
71 if(setjmp(png_jmpbuf(png_ptr)))
73 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
74 return NULL;
77 png_set_sig_bytes(png_ptr, 8);
79 png_read_png(
80 png_ptr, info_ptr,
81 PNG_TRANSFORM_STRIP_16 |
82 PNG_TRANSFORM_STRIP_ALPHA |
83 PNG_TRANSFORM_PACKING |
84 PNG_TRANSFORM_EXPAND |
85 PNG_TRANSFORM_BGR,
86 NULL);
88 if(png_get_channels(png_ptr, info_ptr) != 3)
90 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
91 return NULL;
94 pic = calloc(info_ptr->width * info_ptr->height, 4);
96 *w = info_ptr->width;
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);
114 return pic;