2001-08-25 21:04:29 +00:00
|
|
|
#include <stdio.h>
|
2001-10-04 18:30:30 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-08-25 21:04:29 +00:00
|
|
|
|
2005-12-08 22:12:57 +00:00
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "help_mp.h"
|
2001-08-25 21:04:29 +00:00
|
|
|
#include "bitmap.h"
|
2007-05-02 14:39:55 +00:00
|
|
|
#include "libavcodec/avcodec.h"
|
2007-05-07 12:33:13 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2007-05-02 14:39:55 +00:00
|
|
|
#include "libvo/fastmemcpy.h"
|
2001-08-25 21:04:29 +00:00
|
|
|
|
2007-05-02 16:24:23 +00:00
|
|
|
static int pngRead( unsigned char * fname,txSample * bf )
|
2002-11-02 17:07:19 +00:00
|
|
|
{
|
2007-05-02 14:39:55 +00:00
|
|
|
int decode_ok;
|
|
|
|
void *data;
|
|
|
|
int len;
|
|
|
|
AVCodecContext *avctx;
|
|
|
|
AVFrame *frame;
|
2002-11-02 17:07:19 +00:00
|
|
|
|
|
|
|
FILE *fp=fopen( fname,"rb" );
|
|
|
|
if ( !fp )
|
|
|
|
{
|
2004-12-15 01:22:24 +00:00
|
|
|
mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] file read error ( %s )\n",fname );
|
2002-11-02 17:07:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-05-02 14:39:55 +00:00
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
len = ftell(fp);
|
|
|
|
if (len > 50 * 1024 * 1024) return 2;
|
|
|
|
data = malloc(len + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
fread(data, len, 1, fp);
|
|
|
|
fclose(fp);
|
|
|
|
avctx = avcodec_alloc_context();
|
|
|
|
frame = avcodec_alloc_frame();
|
2007-06-12 08:38:42 +00:00
|
|
|
avcodec_register_all();
|
|
|
|
avcodec_open(avctx, avcodec_find_decoder(CODEC_ID_PNG));
|
2007-05-02 14:39:55 +00:00
|
|
|
avcodec_decode_video(avctx, frame, &decode_ok, data, len);
|
|
|
|
memset(bf, 0, sizeof(*bf));
|
|
|
|
switch (avctx->pix_fmt) {
|
|
|
|
case PIX_FMT_GRAY8: bf->BPP = 8; break;
|
|
|
|
case PIX_FMT_GRAY16BE: bf->BPP = 16; break;
|
|
|
|
case PIX_FMT_RGB24: bf->BPP = 24; break;
|
|
|
|
case PIX_FMT_RGB32: bf->BPP = 32; break;
|
|
|
|
default: bf->BPP = 0; break;
|
|
|
|
}
|
|
|
|
if (decode_ok && bf->BPP) {
|
|
|
|
int bpl;
|
|
|
|
bf->Width = avctx->width; bf->Height = avctx->height;
|
|
|
|
bpl = bf->Width * (bf->BPP / 8);
|
|
|
|
bf->ImageSize = bpl * bf->Height;
|
|
|
|
bf->Image = malloc(bf->ImageSize);
|
|
|
|
memcpy_pic(bf->Image, frame->data[0], bpl, bf->Height, bpl, frame->linesize[0]);
|
|
|
|
}
|
|
|
|
avcodec_close(avctx);
|
|
|
|
av_freep(&frame);
|
|
|
|
av_freep(&avctx);
|
2002-11-02 17:07:19 +00:00
|
|
|
|
|
|
|
mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] filename: %s.\n",fname );
|
|
|
|
mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] size: %dx%d bits: %d\n",bf->Width,bf->Height,bf->BPP );
|
|
|
|
mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] imagesize: %lu\n",bf->ImageSize );
|
2007-05-02 14:39:55 +00:00
|
|
|
return !(decode_ok && bf->BPP);
|
2002-11-02 17:07:19 +00:00
|
|
|
}
|
|
|
|
|
2007-05-02 16:24:23 +00:00
|
|
|
static int conv24to32( txSample * bf )
|
2001-08-25 21:04:29 +00:00
|
|
|
{
|
|
|
|
unsigned char * tmpImage;
|
|
|
|
int i,c;
|
|
|
|
|
|
|
|
if ( bf->BPP == 24 )
|
|
|
|
{
|
|
|
|
tmpImage=bf->Image;
|
|
|
|
bf->ImageSize=bf->Width * bf->Height * 4;
|
|
|
|
bf->BPP=32;
|
2007-05-02 16:42:31 +00:00
|
|
|
if ( ( bf->Image=calloc( 1, bf->ImageSize ) ) == NULL )
|
2001-08-25 21:04:29 +00:00
|
|
|
{
|
2007-05-02 16:39:35 +00:00
|
|
|
free( tmpImage );
|
2004-12-15 01:22:24 +00:00
|
|
|
mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] not enough memory for image\n" );
|
2001-08-25 21:04:29 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2007-05-07 12:33:13 +00:00
|
|
|
for ( c=0,i=0; c < bf->ImageSize; c += 4, i += 3)
|
2001-08-25 21:04:29 +00:00
|
|
|
{
|
2007-05-07 12:33:13 +00:00
|
|
|
*(uint32_t *)&bf->Image[c] = AV_RB24(&tmpImage[i]);
|
2001-08-25 21:04:29 +00:00
|
|
|
}
|
|
|
|
free( tmpImage );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-02 16:24:23 +00:00
|
|
|
static void Normalize( txSample * bf )
|
2001-08-25 21:04:29 +00:00
|
|
|
{
|
|
|
|
int i;
|
2002-05-12 01:09:10 +00:00
|
|
|
#ifndef WORDS_BIGENDIAN
|
2002-02-23 15:12:55 +00:00
|
|
|
for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i+3]=0;
|
2002-05-12 01:09:10 +00:00
|
|
|
#else
|
|
|
|
for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i]=0;
|
|
|
|
#endif
|
2001-08-25 21:04:29 +00:00
|
|
|
}
|
|
|
|
|
2007-05-02 16:24:23 +00:00
|
|
|
static unsigned char tmp[512];
|
2001-08-25 21:04:29 +00:00
|
|
|
|
2007-05-02 16:24:23 +00:00
|
|
|
static unsigned char * fExist( unsigned char * fname )
|
2001-08-25 21:04:29 +00:00
|
|
|
{
|
|
|
|
FILE * fl;
|
2002-09-04 09:36:03 +00:00
|
|
|
unsigned char ext[][6] = { ".png\0",".PNG\0" };
|
2001-08-25 21:04:29 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
fl=fopen( fname,"rb" );
|
|
|
|
if ( fl != NULL )
|
|
|
|
{
|
|
|
|
fclose( fl );
|
|
|
|
return fname;
|
|
|
|
}
|
2002-09-04 09:36:03 +00:00
|
|
|
for ( i=0;i<2;i++ )
|
2001-08-25 21:04:29 +00:00
|
|
|
{
|
2003-05-23 17:04:24 +00:00
|
|
|
snprintf( tmp,511,"%s%s",fname,ext[i] );
|
2001-08-25 21:04:29 +00:00
|
|
|
fl=fopen( tmp,"rb" );
|
|
|
|
if ( fl != NULL )
|
|
|
|
{
|
|
|
|
fclose( fl );
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bpRead( char * fname, txSample * bf )
|
|
|
|
{
|
|
|
|
fname=fExist( fname );
|
|
|
|
if ( fname == NULL ) return -2;
|
2002-09-04 09:36:03 +00:00
|
|
|
if ( pngRead( fname,bf ) )
|
2001-08-25 21:04:29 +00:00
|
|
|
{
|
2004-12-15 01:22:24 +00:00
|
|
|
mp_dbg( MSGT_GPLAYER,MSGL_FATAL,"[bitmap] unknown file type ( %s )\n",fname );
|
2002-09-04 09:36:03 +00:00
|
|
|
return -5;
|
2001-08-25 21:04:29 +00:00
|
|
|
}
|
|
|
|
if ( bf->BPP < 24 )
|
|
|
|
{
|
2003-05-23 17:04:24 +00:00
|
|
|
mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] Sorry, only 24 and 32 bpp bitmaps are supported.\n" );
|
2001-08-25 21:04:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2003-05-23 17:23:35 +00:00
|
|
|
if ( conv24to32( bf ) ) return -8;
|
2001-08-25 21:04:29 +00:00
|
|
|
Normalize( bf );
|
|
|
|
return 0;
|
|
|
|
}
|
2001-11-05 17:00:42 +00:00
|
|
|
|
|
|
|
void Convert32to1( txSample * in,txSample * out,int adaptivlimit )
|
|
|
|
{
|
|
|
|
out->Width=in->Width;
|
|
|
|
out->Height=in->Height;
|
|
|
|
out->BPP=1;
|
2002-09-10 12:38:19 +00:00
|
|
|
out->ImageSize=(out->Width * out->Height + 7) / 8;
|
2003-05-23 17:23:35 +00:00
|
|
|
mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[c32to1] imagesize: %d\n",out->ImageSize );
|
2006-07-13 16:17:24 +00:00
|
|
|
out->Image=calloc( 1,out->ImageSize );
|
2004-12-15 01:22:24 +00:00
|
|
|
if ( out->Image == NULL ) mp_msg( MSGT_GPLAYER,MSGL_WARN,MSGTR_NotEnoughMemoryC32To1 );
|
2001-11-05 17:00:42 +00:00
|
|
|
{
|
2002-05-20 13:39:23 +00:00
|
|
|
int i,b,c=0; unsigned int * buf = NULL; unsigned char tmp = 0; int nothaveshape = 1;
|
|
|
|
buf=(unsigned int *)in->Image;
|
2002-02-23 15:12:55 +00:00
|
|
|
for ( b=0,i=0;i < (int)(out->Width * out->Height);i++ )
|
2001-11-05 17:00:42 +00:00
|
|
|
{
|
2002-02-23 15:12:55 +00:00
|
|
|
if ( (int)buf[i] != adaptivlimit ) tmp=( tmp >> 1 )|128;
|
2001-11-05 17:00:42 +00:00
|
|
|
else { tmp=tmp >> 1; buf[i]=nothaveshape=0; }
|
|
|
|
if ( b++ == 7 ) { out->Image[c++]=tmp; tmp=b=0; }
|
|
|
|
}
|
|
|
|
if ( b ) out->Image[c]=tmp;
|
|
|
|
if ( nothaveshape ) { free( out->Image ); out->Image=NULL; }
|
|
|
|
}
|
|
|
|
}
|