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