Mercurial > mplayer.hg
annotate gui/bitmap.c @ 23193:7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
author | reimar |
---|---|
date | Wed, 02 May 2007 14:39:55 +0000 |
parents | 17bf4f4b0715 |
children | c627c0ec88cf |
rev | line source |
---|---|
23077 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 | |
5 #include "mp_msg.h" | |
6 #include "help_mp.h" | |
7 #include "bitmap.h" | |
23193
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
8 #ifdef USE_LIBAVCODEC_SO |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
9 #include <ffmpeg/avcodec.h> |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
10 #else |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
11 #include "libavcodec/avcodec.h" |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
12 #endif |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
13 #include "libvo/fastmemcpy.h" |
23077 | 14 |
15 int pngRead( unsigned char * fname,txSample * bf ) | |
16 { | |
23193
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
17 int decode_ok; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
18 void *data; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
19 int len; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
20 AVCodecContext *avctx; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
21 AVFrame *frame; |
23077 | 22 |
23 FILE *fp=fopen( fname,"rb" ); | |
24 if ( !fp ) | |
25 { | |
26 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] file read error ( %s )\n",fname ); | |
27 return 1; | |
28 } | |
29 | |
23193
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
30 fseek(fp, 0, SEEK_END); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
31 len = ftell(fp); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
32 if (len > 50 * 1024 * 1024) return 2; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
33 data = malloc(len + FF_INPUT_BUFFER_PADDING_SIZE); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
34 fseek(fp, 0, SEEK_SET); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
35 fread(data, len, 1, fp); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
36 fclose(fp); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
37 avctx = avcodec_alloc_context(); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
38 frame = avcodec_alloc_frame(); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
39 avcodec_open(avctx, &png_decoder); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
40 avcodec_decode_video(avctx, frame, &decode_ok, data, len); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
41 memset(bf, 0, sizeof(*bf)); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
42 switch (avctx->pix_fmt) { |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
43 case PIX_FMT_GRAY8: bf->BPP = 8; break; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
44 case PIX_FMT_GRAY16BE: bf->BPP = 16; break; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
45 case PIX_FMT_RGB24: bf->BPP = 24; break; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
46 case PIX_FMT_RGB32: bf->BPP = 32; break; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
47 default: bf->BPP = 0; break; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
48 } |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
49 if (decode_ok && bf->BPP) { |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
50 int bpl; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
51 bf->Width = avctx->width; bf->Height = avctx->height; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
52 bpl = bf->Width * (bf->BPP / 8); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
53 bf->ImageSize = bpl * bf->Height; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
54 bf->Image = malloc(bf->ImageSize); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
55 memcpy_pic(bf->Image, frame->data[0], bpl, bf->Height, bpl, frame->linesize[0]); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
56 } |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
57 avcodec_close(avctx); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
58 av_freep(&frame); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
59 av_freep(&avctx); |
23077 | 60 |
61 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] filename: %s.\n",fname ); | |
62 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] size: %dx%d bits: %d\n",bf->Width,bf->Height,bf->BPP ); | |
63 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] imagesize: %lu\n",bf->ImageSize ); | |
23193
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
64 return !(decode_ok && bf->BPP); |
23077 | 65 } |
66 | |
67 int conv24to32( txSample * bf ) | |
68 { | |
69 unsigned char * tmpImage; | |
70 int i,c; | |
71 | |
72 if ( bf->BPP == 24 ) | |
73 { | |
74 tmpImage=bf->Image; | |
75 bf->ImageSize=bf->Width * bf->Height * 4; | |
76 bf->BPP=32; | |
77 if ( ( bf->Image=malloc( bf->ImageSize ) ) == NULL ) | |
78 { | |
79 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] not enough memory for image\n" ); | |
80 return 1; | |
81 } | |
82 memset( bf->Image,0,bf->ImageSize ); | |
83 for ( c=0,i=0;i < (int)(bf->Width * bf->Height * 3); ) | |
84 { | |
85 bf->Image[c++]=tmpImage[i++]; //red | |
86 bf->Image[c++]=tmpImage[i++]; //green | |
87 bf->Image[c++]=tmpImage[i++]; c++; //blue | |
88 } | |
89 free( tmpImage ); | |
90 } | |
91 return 0; | |
92 } | |
93 | |
94 void bgr2rgb( txSample * bf ) | |
95 { | |
96 unsigned char c; | |
97 int i; | |
98 | |
99 for ( i=0;i < (int)bf->ImageSize;i+=4 ) | |
100 { | |
101 c=bf->Image[i]; | |
102 bf->Image[i]=bf->Image[i+2]; | |
103 bf->Image[i+2]=c; | |
104 } | |
105 } | |
106 | |
107 void Normalize( txSample * bf ) | |
108 { | |
109 int i; | |
110 #ifndef WORDS_BIGENDIAN | |
111 for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i+3]=0; | |
112 #else | |
113 for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i]=0; | |
114 #endif | |
115 } | |
116 | |
117 unsigned char tmp[512]; | |
118 | |
119 unsigned char * fExist( unsigned char * fname ) | |
120 { | |
121 FILE * fl; | |
122 unsigned char ext[][6] = { ".png\0",".PNG\0" }; | |
123 int i; | |
124 | |
125 fl=fopen( fname,"rb" ); | |
126 if ( fl != NULL ) | |
127 { | |
128 fclose( fl ); | |
129 return fname; | |
130 } | |
131 for ( i=0;i<2;i++ ) | |
132 { | |
133 snprintf( tmp,511,"%s%s",fname,ext[i] ); | |
134 fl=fopen( tmp,"rb" ); | |
135 if ( fl != NULL ) | |
136 { | |
137 fclose( fl ); | |
138 return tmp; | |
139 } | |
140 } | |
141 return NULL; | |
142 } | |
143 | |
144 int bpRead( char * fname, txSample * bf ) | |
145 { | |
146 fname=fExist( fname ); | |
147 if ( fname == NULL ) return -2; | |
148 if ( pngRead( fname,bf ) ) | |
149 { | |
150 mp_dbg( MSGT_GPLAYER,MSGL_FATAL,"[bitmap] unknown file type ( %s )\n",fname ); | |
151 return -5; | |
152 } | |
153 if ( bf->BPP < 24 ) | |
154 { | |
155 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] Sorry, only 24 and 32 bpp bitmaps are supported.\n" ); | |
156 return -1; | |
157 } | |
158 if ( conv24to32( bf ) ) return -8; | |
159 #ifdef WORDS_BIGENDIAN | |
160 swab(bf->Image, bf->Image, bf->ImageSize); | |
161 #endif | |
162 bgr2rgb( bf ); | |
163 Normalize( bf ); | |
164 return 0; | |
165 } | |
166 | |
167 void Convert32to1( txSample * in,txSample * out,int adaptivlimit ) | |
168 { | |
169 out->Width=in->Width; | |
170 out->Height=in->Height; | |
171 out->BPP=1; | |
172 out->ImageSize=(out->Width * out->Height + 7) / 8; | |
173 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[c32to1] imagesize: %d\n",out->ImageSize ); | |
174 out->Image=calloc( 1,out->ImageSize ); | |
175 if ( out->Image == NULL ) mp_msg( MSGT_GPLAYER,MSGL_WARN,MSGTR_NotEnoughMemoryC32To1 ); | |
176 { | |
177 int i,b,c=0; unsigned int * buf = NULL; unsigned char tmp = 0; int nothaveshape = 1; | |
178 buf=(unsigned int *)in->Image; | |
179 for ( b=0,i=0;i < (int)(out->Width * out->Height);i++ ) | |
180 { | |
181 if ( (int)buf[i] != adaptivlimit ) tmp=( tmp >> 1 )|128; | |
182 else { tmp=tmp >> 1; buf[i]=nothaveshape=0; } | |
183 if ( b++ == 7 ) { out->Image[c++]=tmp; tmp=b=0; } | |
184 } | |
185 if ( b ) out->Image[c]=tmp; | |
186 if ( nothaveshape ) { free( out->Image ); out->Image=NULL; } | |
187 } | |
188 } | |
189 | |
190 void Convert1to32( txSample * in,txSample * out ) | |
191 { | |
192 if ( in->Image == NULL ) return; | |
193 out->Width=in->Width; | |
194 out->Height=in->Height; | |
195 out->BPP=32; | |
196 out->ImageSize=out->Width * out->Height * 4; | |
197 out->Image=calloc( 1,out->ImageSize ); | |
198 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[c1to32] imagesize: %d\n",out->ImageSize ); | |
199 if ( out->Image == NULL ) mp_msg( MSGT_GPLAYER,MSGL_WARN,MSGTR_NotEnoughMemoryC1To32 ); | |
200 { | |
201 int i,b,c=0; unsigned int * buf = NULL; unsigned char tmp = 0; | |
202 buf=(unsigned int *)out->Image; | |
203 for ( c=0,i=0;i < (int)(in->Width * in->Height / 8);i++ ) | |
204 { | |
205 tmp=in->Image[i]; | |
206 for ( b=0;b<8;b++ ) | |
207 { | |
208 buf[c]=0; | |
209 if ( tmp&0x1 ) buf[c]=0xffffffff; | |
210 c++; tmp=tmp>>1; | |
211 } | |
212 } | |
213 } | |
214 } |