Mercurial > mplayer.hg
annotate gui/bitmap.c @ 26178:5ba88511ec0a
Make av_class a pointer to const.
Patch by Takis.
author | benoit |
---|---|
date | Mon, 10 Mar 2008 14:30:49 +0000 |
parents | 1318e956c092 |
children | b0a7b35b78d2 |
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 #include "libavcodec/avcodec.h" |
23225
794aba782ebd
Fix Gui colors for 32 bit png images after switch to libavcodec decoding
reimar
parents:
23200
diff
changeset
|
9 #include "libavutil/intreadwrite.h" |
23193
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
10 #include "libvo/fastmemcpy.h" |
23077 | 11 |
23196
019bfce0c0dc
Make functions and variables not used outside bitmap.c static
reimar
parents:
23195
diff
changeset
|
12 static int pngRead( unsigned char * fname,txSample * bf ) |
23077 | 13 { |
23193
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
14 int decode_ok; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
15 void *data; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
16 int len; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
17 AVCodecContext *avctx; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
18 AVFrame *frame; |
23077 | 19 |
20 FILE *fp=fopen( fname,"rb" ); | |
21 if ( !fp ) | |
22 { | |
23 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] file read error ( %s )\n",fname ); | |
24 return 1; | |
25 } | |
26 | |
23193
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
27 fseek(fp, 0, SEEK_END); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
28 len = ftell(fp); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
29 if (len > 50 * 1024 * 1024) return 2; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
30 data = malloc(len + FF_INPUT_BUFFER_PADDING_SIZE); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
31 fseek(fp, 0, SEEK_SET); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
32 fread(data, len, 1, fp); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
33 fclose(fp); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
34 avctx = avcodec_alloc_context(); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
35 frame = avcodec_alloc_frame(); |
23526 | 36 avcodec_register_all(); |
37 avcodec_open(avctx, avcodec_find_decoder(CODEC_ID_PNG)); | |
23193
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
38 avcodec_decode_video(avctx, frame, &decode_ok, data, len); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
39 memset(bf, 0, sizeof(*bf)); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
40 switch (avctx->pix_fmt) { |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
41 case PIX_FMT_GRAY8: bf->BPP = 8; break; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
42 case PIX_FMT_GRAY16BE: bf->BPP = 16; break; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
43 case PIX_FMT_RGB24: bf->BPP = 24; break; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
44 case PIX_FMT_RGB32: bf->BPP = 32; break; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
45 default: bf->BPP = 0; break; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
46 } |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
47 if (decode_ok && bf->BPP) { |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
48 int bpl; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
49 bf->Width = avctx->width; bf->Height = avctx->height; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
50 bpl = bf->Width * (bf->BPP / 8); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
51 bf->ImageSize = bpl * bf->Height; |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
52 bf->Image = malloc(bf->ImageSize); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
53 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
|
54 } |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
55 avcodec_close(avctx); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
56 av_freep(&frame); |
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
57 av_freep(&avctx); |
23077 | 58 |
59 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] filename: %s.\n",fname ); | |
60 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] size: %dx%d bits: %d\n",bf->Width,bf->Height,bf->BPP ); | |
61 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
|
62 return !(decode_ok && bf->BPP); |
23077 | 63 } |
64 | |
23196
019bfce0c0dc
Make functions and variables not used outside bitmap.c static
reimar
parents:
23195
diff
changeset
|
65 static int conv24to32( txSample * bf ) |
23077 | 66 { |
67 unsigned char * tmpImage; | |
68 int i,c; | |
69 | |
70 if ( bf->BPP == 24 ) | |
71 { | |
72 tmpImage=bf->Image; | |
73 bf->ImageSize=bf->Width * bf->Height * 4; | |
74 bf->BPP=32; | |
23200 | 75 if ( ( bf->Image=calloc( 1, bf->ImageSize ) ) == NULL ) |
23077 | 76 { |
23199 | 77 free( tmpImage ); |
23077 | 78 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] not enough memory for image\n" ); |
79 return 1; | |
80 } | |
23225
794aba782ebd
Fix Gui colors for 32 bit png images after switch to libavcodec decoding
reimar
parents:
23200
diff
changeset
|
81 for ( c=0,i=0; c < bf->ImageSize; c += 4, i += 3) |
23077 | 82 { |
23225
794aba782ebd
Fix Gui colors for 32 bit png images after switch to libavcodec decoding
reimar
parents:
23200
diff
changeset
|
83 *(uint32_t *)&bf->Image[c] = AV_RB24(&tmpImage[i]); |
23077 | 84 } |
85 free( tmpImage ); | |
86 } | |
87 return 0; | |
88 } | |
89 | |
23196
019bfce0c0dc
Make functions and variables not used outside bitmap.c static
reimar
parents:
23195
diff
changeset
|
90 static void Normalize( txSample * bf ) |
23077 | 91 { |
92 int i; | |
93 #ifndef WORDS_BIGENDIAN | |
94 for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i+3]=0; | |
95 #else | |
96 for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i]=0; | |
97 #endif | |
98 } | |
99 | |
23196
019bfce0c0dc
Make functions and variables not used outside bitmap.c static
reimar
parents:
23195
diff
changeset
|
100 static unsigned char tmp[512]; |
23077 | 101 |
23196
019bfce0c0dc
Make functions and variables not used outside bitmap.c static
reimar
parents:
23195
diff
changeset
|
102 static unsigned char * fExist( unsigned char * fname ) |
23077 | 103 { |
104 FILE * fl; | |
105 unsigned char ext[][6] = { ".png\0",".PNG\0" }; | |
106 int i; | |
107 | |
108 fl=fopen( fname,"rb" ); | |
109 if ( fl != NULL ) | |
110 { | |
111 fclose( fl ); | |
112 return fname; | |
113 } | |
114 for ( i=0;i<2;i++ ) | |
115 { | |
116 snprintf( tmp,511,"%s%s",fname,ext[i] ); | |
117 fl=fopen( tmp,"rb" ); | |
118 if ( fl != NULL ) | |
119 { | |
120 fclose( fl ); | |
121 return tmp; | |
122 } | |
123 } | |
124 return NULL; | |
125 } | |
126 | |
127 int bpRead( char * fname, txSample * bf ) | |
128 { | |
129 fname=fExist( fname ); | |
130 if ( fname == NULL ) return -2; | |
131 if ( pngRead( fname,bf ) ) | |
132 { | |
133 mp_dbg( MSGT_GPLAYER,MSGL_FATAL,"[bitmap] unknown file type ( %s )\n",fname ); | |
134 return -5; | |
135 } | |
136 if ( bf->BPP < 24 ) | |
137 { | |
138 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] Sorry, only 24 and 32 bpp bitmaps are supported.\n" ); | |
139 return -1; | |
140 } | |
141 if ( conv24to32( bf ) ) return -8; | |
142 Normalize( bf ); | |
143 return 0; | |
144 } | |
145 | |
146 void Convert32to1( txSample * in,txSample * out,int adaptivlimit ) | |
147 { | |
148 out->Width=in->Width; | |
149 out->Height=in->Height; | |
150 out->BPP=1; | |
151 out->ImageSize=(out->Width * out->Height + 7) / 8; | |
152 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[c32to1] imagesize: %d\n",out->ImageSize ); | |
153 out->Image=calloc( 1,out->ImageSize ); | |
154 if ( out->Image == NULL ) mp_msg( MSGT_GPLAYER,MSGL_WARN,MSGTR_NotEnoughMemoryC32To1 ); | |
155 { | |
156 int i,b,c=0; unsigned int * buf = NULL; unsigned char tmp = 0; int nothaveshape = 1; | |
157 buf=(unsigned int *)in->Image; | |
158 for ( b=0,i=0;i < (int)(out->Width * out->Height);i++ ) | |
159 { | |
160 if ( (int)buf[i] != adaptivlimit ) tmp=( tmp >> 1 )|128; | |
161 else { tmp=tmp >> 1; buf[i]=nothaveshape=0; } | |
162 if ( b++ == 7 ) { out->Image[c++]=tmp; tmp=b=0; } | |
163 } | |
164 if ( b ) out->Image[c]=tmp; | |
165 if ( nothaveshape ) { free( out->Image ); out->Image=NULL; } | |
166 } | |
167 } |