remove whitespace at eol
[goodguy/history.git] / cinelerra-5.1 / guicast / pngtoraw.c
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22
23
24 // This program converts compressed PNG to uncompressed RAW for faster
25 // theme loading.
26
27 #include <png.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdint.h>
32
33 int main(int argc, char *argv[])
34 {
35         if(argc < 2) return 1;
36
37         for(argc--; argc > 0; argc--)
38         {
39                 FILE *in;
40                 FILE *out;
41                 int w;
42                 int h;
43                 int components;
44                 int i;
45                 char variable[1024], header_fn[1024], output_fn[1024], *suffix, *prefix;
46
47                 in = fopen(argv[argc], "rb");
48                 if(!in) continue;
49
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");
55
56                 out = fopen(output_fn, "w");
57                 if(!out)
58                 {
59                         fclose(in);
60                         continue;
61                 }
62
63
64 // Decompress input
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);
67                 int new_color_model;
68
69                 png_init_io(png_ptr, in);
70                 png_read_info(png_ptr, info_ptr);
71
72                 w = png_get_image_width(png_ptr, info_ptr);
73                 h = png_get_image_height(png_ptr, info_ptr);
74
75                 int src_color_model = png_get_color_type(png_ptr, info_ptr);
76                 switch(src_color_model)
77                 {
78                         case PNG_COLOR_TYPE_RGB:
79                                 components = 3;
80                                 break;
81
82                         case PNG_COLOR_TYPE_GRAY_ALPHA:
83                                 printf("pngtoraw %d: %s is alpha\n", __LINE__, argv[argc]);
84                                 break;
85
86
87                         case PNG_COLOR_TYPE_RGB_ALPHA:
88                         default:
89                                 components = 4;
90                                 break;
91                 }
92
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);
99
100                 fputc('R', out);
101                 fputc('A', out);
102                 fputc('W', out);
103                 fputc(' ', out);
104
105                 fputc((w & 0xff), out);
106                 fputc(((w >> 8) & 0xff), out);
107                 fputc(((w >> 16) & 0xff), out);
108                 fputc(((w >> 24) & 0xff), out);
109
110
111                 fputc((h & 0xff), out);
112                 fputc(((h >> 8) & 0xff), out);
113                 fputc(((h >> 16) & 0xff), out);
114                 fputc(((h >> 24) & 0xff), out);
115
116                 fputc((components & 0xff), out);
117                 fputc(((components >> 8) & 0xff), out);
118                 fputc(((components >> 16) & 0xff), out);
119                 fputc(((components >> 24) & 0xff), out);
120
121                 fwrite(buffer, w * h * components, 1, out);
122                 free(buffer);
123
124                 fclose(out);
125                 fclose(in);
126         }
127 }
128
129
130
131
132
133