comparison 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
comparison
equal deleted inserted replaced
23192:072e49028a76 23193:7857af1ca50b
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include <string.h> 3 #include <string.h>
4 4
5 #include <png.h>
6
7 #include "mp_msg.h" 5 #include "mp_msg.h"
8 #include "help_mp.h" 6 #include "help_mp.h"
9 #include "bitmap.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 "libvo/fastmemcpy.h"
10 14
11 int pngRead( unsigned char * fname,txSample * bf ) 15 int pngRead( unsigned char * fname,txSample * bf )
12 { 16 {
13 unsigned char header[8]; 17 int decode_ok;
14 png_structp png; 18 void *data;
15 png_infop info; 19 int len;
16 png_infop endinfo; 20 AVCodecContext *avctx;
17 png_bytep * row_p; 21 AVFrame *frame;
18 png_bytep palette = NULL;
19 int color;
20 png_uint_32 i;
21 22
22 FILE *fp=fopen( fname,"rb" ); 23 FILE *fp=fopen( fname,"rb" );
23 if ( !fp ) 24 if ( !fp )
24 { 25 {
25 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] file read error ( %s )\n",fname ); 26 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] file read error ( %s )\n",fname );
26 return 1; 27 return 1;
27 } 28 }
28 29
29 fread( header,1,8,fp ); 30 fseek(fp, 0, SEEK_END);
30 if ( !png_check_sig( header,8 ) ) return 1; 31 len = ftell(fp);
31 32 if (len > 50 * 1024 * 1024) return 2;
32 png=png_create_read_struct( PNG_LIBPNG_VER_STRING,NULL,NULL,NULL ); 33 data = malloc(len + FF_INPUT_BUFFER_PADDING_SIZE);
33 info=png_create_info_struct( png ); 34 fseek(fp, 0, SEEK_SET);
34 endinfo=png_create_info_struct( png ); 35 fread(data, len, 1, fp);
35 36 fclose(fp);
36 png_init_io( png,fp ); 37 avctx = avcodec_alloc_context();
37 png_set_sig_bytes( png,8 ); 38 frame = avcodec_alloc_frame();
38 png_read_info( png,info ); 39 avcodec_open(avctx, &png_decoder);
39 png_get_IHDR( png,info,&bf->Width,&bf->Height,&bf->BPP,&color,NULL,NULL,NULL ); 40 avcodec_decode_video(avctx, frame, &decode_ok, data, len);
40 41 memset(bf, 0, sizeof(*bf));
41 row_p=malloc( sizeof( png_bytep ) * bf->Height ); 42 switch (avctx->pix_fmt) {
42 if ( !row_p ) 43 case PIX_FMT_GRAY8: bf->BPP = 8; break;
43 { 44 case PIX_FMT_GRAY16BE: bf->BPP = 16; break;
44 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] not enough memory for row buffer\n" ); 45 case PIX_FMT_RGB24: bf->BPP = 24; break;
45 return 2; 46 case PIX_FMT_RGB32: bf->BPP = 32; break;
46 } 47 default: bf->BPP = 0; break;
47 bf->Image=(png_bytep)malloc( png_get_rowbytes( png,info ) * bf->Height ); 48 }
48 if ( !bf->Image ) 49 if (decode_ok && bf->BPP) {
49 { 50 int bpl;
50 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] not enough memory for image buffer\n" ); 51 bf->Width = avctx->width; bf->Height = avctx->height;
51 return 2; 52 bpl = bf->Width * (bf->BPP / 8);
52 } 53 bf->ImageSize = bpl * bf->Height;
53 for ( i=0; i < bf->Height; i++ ) row_p[i]=&bf->Image[png_get_rowbytes( png,info ) * i]; 54 bf->Image = malloc(bf->ImageSize);
54 55 memcpy_pic(bf->Image, frame->data[0], bpl, bf->Height, bpl, frame->linesize[0]);
55 png_read_image( png,row_p ); 56 }
56 free( row_p ); 57 avcodec_close(avctx);
57 58 av_freep(&frame);
58 #if 0 59 av_freep(&avctx);
59 if ( color == PNG_COLOR_TYPE_PALETTE )
60 {
61 int cols;
62 png_get_PLTE( png,info,(png_colorp *)&palette,&cols );
63 }
64 #endif
65
66 if ( color&PNG_COLOR_MASK_ALPHA )
67 {
68 if ( color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY_ALPHA ) bf->BPP*=2;
69 else bf->BPP*=4;
70 }
71 else
72 {
73 if ( color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY ) bf->BPP*=1;
74 else bf->BPP*=3;
75 }
76
77 png_read_end( png,endinfo );
78 png_destroy_read_struct( &png,&info,&endinfo );
79
80 if ( fclose( fp ) != 0 )
81 {
82 free( bf->Image );
83 free( palette );
84 return 1;
85 }
86 bf->ImageSize=bf->Width * bf->Height * ( bf->BPP / 8 );
87 60
88 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] filename: %s.\n",fname ); 61 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] filename: %s.\n",fname );
89 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] size: %dx%d bits: %d\n",bf->Width,bf->Height,bf->BPP ); 62 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] size: %dx%d bits: %d\n",bf->Width,bf->Height,bf->BPP );
90 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] imagesize: %lu\n",bf->ImageSize ); 63 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] imagesize: %lu\n",bf->ImageSize );
91 return 0; 64 return !(decode_ok && bf->BPP);
92 } 65 }
93 66
94 int conv24to32( txSample * bf ) 67 int conv24to32( txSample * bf )
95 { 68 {
96 unsigned char * tmpImage; 69 unsigned char * tmpImage;