8 typedef struct _txSample
13 unsigned long ImageSize
;
24 unsigned int Components
;
26 unsigned char * Palette
;
29 int pngLoadRawF( FILE *fp
,pngRawInfo
*pinfo
)
31 unsigned char header
[8];
37 png_uint_32 width
,height
;
41 if ( pinfo
== NULL
) return 1;
43 fread( header
,1,8,fp
);
44 if ( !png_check_sig( header
,8 ) ) return 1;
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
);
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
);
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
];
63 png_read_image( png
,row_p
);
66 if ( color
== PNG_COLOR_TYPE_PALETTE
)
69 png_get_PLTE( png
,info
,( png_colorp
* ) &pinfo
->Palette
,&cols
);
71 else pinfo
->Palette
=NULL
;
73 if ( color
&PNG_COLOR_MASK_ALPHA
)
75 if ( color
&PNG_COLOR_MASK_PALETTE
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
) pinfo
->Components
=2;
76 else pinfo
->Components
=4;
81 if ( color
&PNG_COLOR_MASK_PALETTE
|| color
== PNG_COLOR_TYPE_GRAY
) pinfo
->Components
=1;
82 else pinfo
->Components
=3;
87 png_read_end( png
,endinfo
);
88 png_destroy_read_struct( &png
,&info
,&endinfo
);
93 int pngLoadRaw( const char *filename
,pngRawInfo
*pinfo
)
96 FILE *fp
=fopen( filename
,"rb" );
98 if ( fp
== NULL
) return 0;
99 result
=pngLoadRawF( fp
,pinfo
);
100 if ( fclose( fp
) != 0 )
105 free( pinfo
->Palette
);
112 int pngRead( unsigned char * fname
,txSample
* bf
)
116 if ( pngLoadRaw( fname
,&raw
) )
119 fprintf( stderr
,"[png] file read error ( %s ).\n",fname
);
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
)
130 fprintf( stderr
,"[png] Not enough memory for image buffer.\n" );
134 memcpy( bf
->Image
,raw
.Data
,bf
->ImageSize
);
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");
145 static char fname
[256];
147 static unsigned char rawhead
[32]={'m','h','w','a','n','h',0,4,
151 static unsigned char rawpal
[3*256];
153 int main(int argc
,char* argv
[]){
157 for(i
=0;i
<256;i
++) rawpal
[i
*3]=rawpal
[i
*3+1]=rawpal
[i
*3+2]=i
;
159 if(argc
<2) {printf("Usage: png2raw file1 [file2...]\n");exit(1);}
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;}
165 snprintf(fname
,256,"%s.raw",argv
[0]);
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
);