Use correct printf length modifiers, fixes the following warnings:
[mplayer/greg.git] / gui / bitmap.c
blobf81e3e74aceb5a828298a66e272031e8486b19c4
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "mp_msg.h"
6 #include "help_mp.h"
7 #include "bitmap.h"
8 #ifdef USE_LIBAVCODEC_SO
9 #include <ffmpeg/avcodec.h>
10 #else
11 #include "libavcodec/avcodec.h"
12 #endif
13 #include "libavutil/intreadwrite.h"
14 #include "libvo/fastmemcpy.h"
16 static int pngRead( unsigned char * fname,txSample * bf )
18 int decode_ok;
19 void *data;
20 int len;
21 AVCodecContext *avctx;
22 AVFrame *frame;
24 FILE *fp=fopen( fname,"rb" );
25 if ( !fp )
27 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] file read error ( %s )\n",fname );
28 return 1;
31 fseek(fp, 0, SEEK_END);
32 len = ftell(fp);
33 if (len > 50 * 1024 * 1024) return 2;
34 data = malloc(len + FF_INPUT_BUFFER_PADDING_SIZE);
35 fseek(fp, 0, SEEK_SET);
36 fread(data, len, 1, fp);
37 fclose(fp);
38 avctx = avcodec_alloc_context();
39 frame = avcodec_alloc_frame();
40 avcodec_register_all();
41 avcodec_open(avctx, avcodec_find_decoder(CODEC_ID_PNG));
42 avcodec_decode_video(avctx, frame, &decode_ok, data, len);
43 memset(bf, 0, sizeof(*bf));
44 switch (avctx->pix_fmt) {
45 case PIX_FMT_GRAY8: bf->BPP = 8; break;
46 case PIX_FMT_GRAY16BE: bf->BPP = 16; break;
47 case PIX_FMT_RGB24: bf->BPP = 24; break;
48 case PIX_FMT_RGB32: bf->BPP = 32; break;
49 default: bf->BPP = 0; break;
51 if (decode_ok && bf->BPP) {
52 int bpl;
53 bf->Width = avctx->width; bf->Height = avctx->height;
54 bpl = bf->Width * (bf->BPP / 8);
55 bf->ImageSize = bpl * bf->Height;
56 bf->Image = malloc(bf->ImageSize);
57 memcpy_pic(bf->Image, frame->data[0], bpl, bf->Height, bpl, frame->linesize[0]);
59 avcodec_close(avctx);
60 av_freep(&frame);
61 av_freep(&avctx);
63 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] filename: %s.\n",fname );
64 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] size: %dx%d bits: %d\n",bf->Width,bf->Height,bf->BPP );
65 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] imagesize: %lu\n",bf->ImageSize );
66 return !(decode_ok && bf->BPP);
69 static int conv24to32( txSample * bf )
71 unsigned char * tmpImage;
72 int i,c;
74 if ( bf->BPP == 24 )
76 tmpImage=bf->Image;
77 bf->ImageSize=bf->Width * bf->Height * 4;
78 bf->BPP=32;
79 if ( ( bf->Image=calloc( 1, bf->ImageSize ) ) == NULL )
81 free( tmpImage );
82 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] not enough memory for image\n" );
83 return 1;
85 for ( c=0,i=0; c < bf->ImageSize; c += 4, i += 3)
87 *(uint32_t *)&bf->Image[c] = AV_RB24(&tmpImage[i]);
89 free( tmpImage );
91 return 0;
94 static void Normalize( txSample * bf )
96 int i;
97 #ifndef WORDS_BIGENDIAN
98 for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i+3]=0;
99 #else
100 for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i]=0;
101 #endif
104 static unsigned char tmp[512];
106 static unsigned char * fExist( unsigned char * fname )
108 FILE * fl;
109 unsigned char ext[][6] = { ".png\0",".PNG\0" };
110 int i;
112 fl=fopen( fname,"rb" );
113 if ( fl != NULL )
115 fclose( fl );
116 return fname;
118 for ( i=0;i<2;i++ )
120 snprintf( tmp,511,"%s%s",fname,ext[i] );
121 fl=fopen( tmp,"rb" );
122 if ( fl != NULL )
124 fclose( fl );
125 return tmp;
128 return NULL;
131 int bpRead( char * fname, txSample * bf )
133 fname=fExist( fname );
134 if ( fname == NULL ) return -2;
135 if ( pngRead( fname,bf ) )
137 mp_dbg( MSGT_GPLAYER,MSGL_FATAL,"[bitmap] unknown file type ( %s )\n",fname );
138 return -5;
140 if ( bf->BPP < 24 )
142 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] Sorry, only 24 and 32 bpp bitmaps are supported.\n" );
143 return -1;
145 if ( conv24to32( bf ) ) return -8;
146 Normalize( bf );
147 return 0;
150 void Convert32to1( txSample * in,txSample * out,int adaptivlimit )
152 out->Width=in->Width;
153 out->Height=in->Height;
154 out->BPP=1;
155 out->ImageSize=(out->Width * out->Height + 7) / 8;
156 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[c32to1] imagesize: %d\n",out->ImageSize );
157 out->Image=calloc( 1,out->ImageSize );
158 if ( out->Image == NULL ) mp_msg( MSGT_GPLAYER,MSGL_WARN,MSGTR_NotEnoughMemoryC32To1 );
160 int i,b,c=0; unsigned int * buf = NULL; unsigned char tmp = 0; int nothaveshape = 1;
161 buf=(unsigned int *)in->Image;
162 for ( b=0,i=0;i < (int)(out->Width * out->Height);i++ )
164 if ( (int)buf[i] != adaptivlimit ) tmp=( tmp >> 1 )|128;
165 else { tmp=tmp >> 1; buf[i]=nothaveshape=0; }
166 if ( b++ == 7 ) { out->Image[c++]=tmp; tmp=b=0; }
168 if ( b ) out->Image[c]=tmp;
169 if ( nothaveshape ) { free( out->Image ); out->Image=NULL; }