annotate TOOLS/png2raw.c @ 16019:1307465ee8ff

When using --enable-* options you are on your own.
author reimar
date Wed, 20 Jul 2005 07:40:15 +0000
parents bf973bffe240
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
366
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
1 #define DEBUG
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
2
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
3 #include <stdlib.h>
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
4
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
5 //#include "png.h"
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
6 #include <png.h>
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
7
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
8 typedef struct _txSample
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
9 {
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
10 unsigned int Width;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
11 unsigned int Height;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
12 unsigned int BPP;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
13 unsigned long ImageSize;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
14 char * Image;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
15 } txSample;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
16
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
17 typedef struct
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
18 {
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
19 unsigned int Width;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
20 unsigned int Height;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
21 unsigned int Depth;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
22 unsigned int Alpha;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
23
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
24 unsigned int Components;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
25 unsigned char * Data;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
26 unsigned char * Palette;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
27 } pngRawInfo;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
28
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
29 int pngLoadRawF( FILE *fp,pngRawInfo *pinfo )
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
30 {
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
31 unsigned char header[8];
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
32 png_structp png;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
33 png_infop info;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
34 png_infop endinfo;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
35 png_bytep data;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
36 png_bytep * row_p;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
37 png_uint_32 width,height;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
38 int depth,color;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
39 png_uint_32 i;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
40
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
41 if ( pinfo == NULL ) return 1;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
42
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
43 fread( header,1,8,fp );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
44 if ( !png_check_sig( header,8 ) ) return 1;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
45
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
46 png=png_create_read_struct( PNG_LIBPNG_VER_STRING,NULL,NULL,NULL );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
47 info=png_create_info_struct( png );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
48 endinfo=png_create_info_struct( png );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
49
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
50 png_init_io( png,fp );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
51 png_set_sig_bytes( png,8 );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
52 png_read_info( png,info );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
53 png_get_IHDR( png,info,&width,&height,&depth,&color,NULL,NULL,NULL );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
54
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
55 pinfo->Width=width;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
56 pinfo->Height=height;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
57 pinfo->Depth=depth;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
58
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
59 data=( png_bytep ) malloc( png_get_rowbytes( png,info )*height );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
60 row_p=( png_bytep * ) malloc( sizeof( png_bytep )*height );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
61 for ( i=0; i < height; i++ ) row_p[i]=&data[png_get_rowbytes( png,info )*i];
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
62
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
63 png_read_image( png,row_p );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
64 free( row_p );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
65
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
66 if ( color == PNG_COLOR_TYPE_PALETTE )
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
67 {
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
68 int cols;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
69 png_get_PLTE( png,info,( png_colorp * ) &pinfo->Palette,&cols );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
70 }
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
71 else pinfo->Palette=NULL;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
72
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
73 if ( color&PNG_COLOR_MASK_ALPHA )
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
74 {
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
75 if ( color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY_ALPHA ) pinfo->Components=2;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
76 else pinfo->Components=4;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
77 pinfo->Alpha=8;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
78 }
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
79 else
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
80 {
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
81 if ( color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY ) pinfo->Components=1;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
82 else pinfo->Components=3;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
83 pinfo->Alpha=0;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
84 }
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
85 pinfo->Data=data;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
86
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
87 png_read_end( png,endinfo );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
88 png_destroy_read_struct( &png,&info,&endinfo );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
89
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
90 return 0;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
91 }
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
92
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
93 int pngLoadRaw( const char *filename,pngRawInfo *pinfo )
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
94 {
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
95 int result;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
96 FILE *fp=fopen( filename,"rb" );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
97
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
98 if ( fp == NULL ) return 0;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
99 result=pngLoadRawF( fp,pinfo );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
100 if ( fclose( fp ) != 0 )
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
101 {
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
102 if ( result )
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
103 {
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
104 free( pinfo->Data );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
105 free( pinfo->Palette );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
106 }
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
107 return 1;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
108 }
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
109 return 0;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
110 }
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
111
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
112 int pngRead( unsigned char * fname,txSample * bf )
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
113 {
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
114 pngRawInfo raw;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
115
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
116 if ( pngLoadRaw( fname,&raw ) )
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
117 {
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
118 #ifdef DEBUG
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
119 fprintf( stderr,"[png] file read error ( %s ).\n",fname );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
120 #endif
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
121 return 1;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
122 }
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
123 bf->Width=raw.Width;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
124 bf->Height=raw.Height;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
125 bf->BPP=( raw.Depth * raw.Components ) + raw.Alpha;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
126 bf->ImageSize=bf->Width * bf->Height * ( bf->BPP / 8 );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
127 if ( ( bf->Image=malloc( bf->ImageSize ) ) == NULL )
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
128 {
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
129 #ifdef DEBUG
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
130 fprintf( stderr,"[png] Not enough memory for image buffer.\n" );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
131 #endif
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
132 return 2;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
133 }
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
134 memcpy( bf->Image,raw.Data,bf->ImageSize );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
135 free( raw.Data );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
136 #ifdef DEBUG
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
137 fprintf( stderr,"[png] filename: %s.\n",fname );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
138 fprintf( stderr,"[png] size: %dx%d bits: %d\n",bf->Width,bf->Height,bf->BPP );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
139 fprintf( stderr,"[png] imagesize: %lu\n",bf->ImageSize );
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
140 fprintf( stderr,"Palette: %s\n",raw.Palette?"yes":"no");
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
141 #endif
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
142 return 0;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
143 }
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
144
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
145 static char fname[256];
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
146
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
147 static unsigned char rawhead[32]={'m','h','w','a','n','h',0,4,
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
148 0,0,0,0,1,0,0,0,
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
149 0,0,0,0,0,0,0,0,
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
150 0,0,0,0,0,0,0,0};
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
151 static unsigned char rawpal[3*256];
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
152
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
153 int main(int argc,char* argv[]){
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
154 txSample ize;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
155 FILE *f;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
156 int i;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
157 for(i=0;i<256;i++) rawpal[i*3]=rawpal[i*3+1]=rawpal[i*3+2]=i;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
158
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
159 if(argc<2) {printf("Usage: png2raw file1 [file2...]\n");exit(1);}
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
160 while(argc>1){
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
161 ++argv;--argc;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
162 printf("Converting %s...\n",argv[0]);
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
163 if(pngRead(argv[0],&ize)) continue;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
164 if(ize.BPP!=8){ printf("Invalid BPP: %d\n",ize.BPP);continue;}
1096
bf973bffe240 some possible buffer overflow places fixed
al3x
parents: 366
diff changeset
165 snprintf(fname,256,"%s.raw",argv[0]);
366
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
166 f=fopen(fname,"wb");
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
167 rawhead[8]=ize.Width>>8;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
168 rawhead[9]=ize.Width&255;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
169 rawhead[10]=ize.Height>>8;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
170 rawhead[11]=ize.Height&255;
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
171 fwrite(rawhead,32,1,f);
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
172 fwrite(rawpal,3*256,1,f);
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
173 fwrite(ize.Image,ize.ImageSize,1,f);
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
174 fclose(f);
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
175
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
176 }
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
177
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
178
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
179
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
180 }
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
181
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
182
00e10ede5234 png->raw image converter
arpi_esp
parents:
diff changeset
183