Changed code that used pts=0 in demux packets to indicate "not known".
[mplayer.git] / TOOLS / png2raw.c
blob935811775c05484724bc6d11cded195db291d22a
1 #define DEBUG
3 #include <stdlib.h>
5 //#include "png.h"
6 #include <png.h>
8 typedef struct _txSample
10 unsigned int Width;
11 unsigned int Height;
12 unsigned int BPP;
13 unsigned long ImageSize;
14 char * Image;
15 } txSample;
17 typedef struct
19 unsigned int Width;
20 unsigned int Height;
21 unsigned int Depth;
22 unsigned int Alpha;
24 unsigned int Components;
25 unsigned char * Data;
26 unsigned char * Palette;
27 } pngRawInfo;
29 int pngLoadRawF( FILE *fp,pngRawInfo *pinfo )
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;
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 );
55 pinfo->Width=width;
56 pinfo->Height=height;
57 pinfo->Depth=depth;
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 );
64 free( row_p );
66 if ( color == PNG_COLOR_TYPE_PALETTE )
68 int cols;
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;
77 pinfo->Alpha=8;
79 else
81 if ( color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY ) pinfo->Components=1;
82 else pinfo->Components=3;
83 pinfo->Alpha=0;
85 pinfo->Data=data;
87 png_read_end( png,endinfo );
88 png_destroy_read_struct( &png,&info,&endinfo );
90 return 0;
93 int pngLoadRaw( const char *filename,pngRawInfo *pinfo )
95 int result;
96 FILE *fp=fopen( filename,"rb" );
98 if ( fp == NULL ) return 0;
99 result=pngLoadRawF( fp,pinfo );
100 if ( fclose( fp ) != 0 )
102 if ( result )
104 free( pinfo->Data );
105 free( pinfo->Palette );
107 return 1;
109 return 0;
112 int pngRead( unsigned char * fname,txSample * bf )
114 pngRawInfo raw;
116 if ( pngLoadRaw( fname,&raw ) )
118 #ifdef DEBUG
119 fprintf( stderr,"[png] file read error ( %s ).\n",fname );
120 #endif
121 return 1;
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 )
129 #ifdef DEBUG
130 fprintf( stderr,"[png] Not enough memory for image buffer.\n" );
131 #endif
132 return 2;
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;
145 static char fname[256];
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];
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;
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;}
165 snprintf(fname,256,"%s.raw",argv[0]);
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);