4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 // This program converts compressed PNG to uncompressed RAW for faster
33 int main(int argc, char *argv[])
35 if(argc < 2) return 1;
37 for(argc--; argc > 0; argc--)
45 char variable[1024], header_fn[1024], output_fn[1024], *suffix, *prefix;
47 in = fopen(argv[argc], "rb");
50 // Replace .png with .raw
51 strcpy(output_fn, argv[argc]);
52 suffix = strrchr(output_fn, '.');
53 if(suffix) *suffix = 0;
54 strcat(output_fn, ".raw");
56 out = fopen(output_fn, "w");
65 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
66 png_infop info_ptr = png_create_info_struct(png_ptr);
69 png_init_io(png_ptr, in);
70 png_read_info(png_ptr, info_ptr);
72 w = png_get_image_width(png_ptr, info_ptr);
73 h = png_get_image_height(png_ptr, info_ptr);
75 int src_color_model = png_get_color_type(png_ptr, info_ptr);
76 switch(src_color_model)
78 case PNG_COLOR_TYPE_RGB:
82 case PNG_COLOR_TYPE_GRAY_ALPHA:
83 printf("pngtoraw %d: %s is alpha\n", __LINE__, argv[argc]);
87 case PNG_COLOR_TYPE_RGB_ALPHA:
93 unsigned char *buffer = (unsigned char*)malloc(w * h * components);
94 unsigned char **rows = (unsigned char**)malloc(sizeof(unsigned char*) * h);
95 for(i = 0; i < h; i++)
96 rows[i] = buffer + w * components * i;
97 png_read_image(png_ptr, rows);
98 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
105 fputc((w & 0xff), out);
106 fputc(((w >> 8) & 0xff), out);
107 fputc(((w >> 16) & 0xff), out);
108 fputc(((w >> 24) & 0xff), out);
111 fputc((h & 0xff), out);
112 fputc(((h >> 8) & 0xff), out);
113 fputc(((h >> 16) & 0xff), out);
114 fputc(((h >> 24) & 0xff), out);
116 fputc((components & 0xff), out);
117 fputc(((components >> 8) & 0xff), out);
118 fputc(((components >> 16) & 0xff), out);
119 fputc(((components >> 24) & 0xff), out);
121 fwrite(buffer, w * h * components, 1, out);