jzimagegif.cpp
来自「君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图」· C++ 代码 · 共 947 行 · 第 1/2 页
CPP
947 行
// JzImageGif.cpp: implementation of the CJzImageGif class.
//
//////////////////////////////////////////////////////////////////////
#include "JzImageIter.h"
#include "JzImageGIF.h"
#include "JzImageInfo.h"
#if CXIMAGE_SUPPORT_GIF
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern "C"
{
#if JZ4740_PMP16
void (*ivDeflateGifImage)( void ) = 0;
void (*ivdelay)( int d) = 0;
int (*ivIsGifQuit)() = 0;
void SetGifFunction(void **fun)
{
//unsigned int *dfun = (unsigned int *)fun;
ivDeflateGifImage = (void (*)())fun[0];
ivdelay = (void (*)(int))fun[1];
ivIsGifQuit = (int (*)())fun[2];
}
#else
extern void (ivDeflateGifImage)( void );
extern void (ivdelay)( int d);
extern int (ivIsGifQuit)();
#endif
}
CJzImageGIF::CJzImageGIF()
{
}
CJzImageGIF::~CJzImageGIF()
{
//Destroy();
}
void CJzImageGIF:: giftest( void )
{
static int i = 1;
void *dd;
dd = malloc(1024 * 1024 * 6 - 100);
free(dd);
}
bool CJzImageGIF::Decode(CxFile *fp)
{
/* AD - for transparency */
BYTE *pbuffer;
int method = 0;
struct_dscgif dscgif;
struct_image image;
struct_TabCol TabCol;
if (fp == NULL) return false;
info.nBkgndIndex = -1;
fp->Read(&dscgif,/*sizeof(dscgif)*/13,1);
//if (strncmp(dscgif.header,"GIF8",3)!=0) {
if (strncmp(dscgif.header,"GIF8",4)!=0) return FALSE;
//printf(" 11 dscgif.scrwidth: %d, dscgif.scrheight: %d\n", dscgif.scrwidth, dscgif.scrheight );
if (info.nEscape == -1) {
// Return output dimensions only
head.biWidth = dscgif.scrwidth;
head.biHeight = dscgif.scrheight;
info.dwWidth = head.biWidth;
info.dwHeight = head.biHeight;
info.wBpp = 8;
return true;
}
/* AD - for interlace */
TabCol.sogct = (short)(1 << ((dscgif.pflds & 0x07)+1));
TabCol.colres = (short)(((dscgif.pflds & 0x70) >> 3) + 1);
// assume that the image is a truecolor-gif if
// 1) no global color map found
// 2) (image.w, image.h) of the 1st image != (dscgif.scrwidth, dscgif.scrheight)
long bTrueColor=0;
CJzImage* imaRGB=NULL;
// Global colour map?
if (dscgif.pflds & 0x80)
fp->Read(TabCol.paleta,sizeof(struct rgb_color)*TabCol.sogct,1);
else
bTrueColor++; //first chance for a truecolor gif
long first_transparent_index;
int iImage = 0;
info.nNumFrames=get_num_frames(fp,&TabCol,&dscgif);
//printf(" cpp 11 ======== head.biWidth: %d\n", head.biWidth);
{
IVSETIMAGEINFO ivSetImageInfo = ivGetImageInfoCallback();
if( ivSetImageInfo )
{
IVIMAGEINFO ImageInfo;
ImageInfo.w = dscgif.scrwidth;
ImageInfo.h = dscgif.scrheight;
ImageInfo.bpp = TabCol.colres;
ImageInfo.reserve1 = info.nNumFrames;
ImageInfo.reserve2 = info.dwFrameDelay;
ivSetImageInfo( &ImageInfo );
info.dwimaWidth = ImageInfo.w;
info.dwimaHeight = ImageInfo.h;
ivscale = ImageInfo.ivscale;
pbuffer = (BYTE *)ImageInfo.pImage;
info.nFrame = ImageInfo.nframe-1; // ImageInfo.reserve: from 1 to info.nFrame
if( pbuffer == NULL ) return FALSE;
//printf("cpp========== info.nFrame: %d\n", info.nFrame);
}
}
//info.nFrame = info.nNumFrames - 1;
//info.nFrame = 0;
if ((info.nFrame<0)||(info.nFrame>=info.nNumFrames)) return false;
//it cannot be a true color GIF with only one frame
if (info.nNumFrames == 1)
bTrueColor=0;
char ch;
bool bPreviousWasNull = true;
int prevdispmeth = 0;
//pRgb = NULL;
//CreatePalette( GetPaletteSize() );
//printf(" ======== GetPaletteSize(): %d =============\n", GetPaletteSize());
for (BOOL bContinue = TRUE; bContinue; )
{
if (fp->Read(&ch, sizeof(ch), 1) != 1) {break;}
if (info.nEscape > 0) return false; // <vho> - cancel decoding
if (bPreviousWasNull || ch==0)
{
switch (ch)
{
case '!': // extension
{
bContinue = DecodeExtension(fp);
break;
}
case ',': // image
{
//assert(sizeof(image) == 9);
fp->Read(&image,sizeof(image),1);
//avoid byte order problems with Solaris <candan>
BYTE *byteData = (BYTE *) & image;
image.l = byteData[0]+byteData[1]*256;
image.t = byteData[2]+byteData[3]*256;
image.w = byteData[4]+byteData[5]*256;
image.h = byteData[6]+byteData[7]*256;
if (((image.l + image.w) > dscgif.scrwidth)||((image.t + image.h) > dscgif.scrheight))
break;
// check if it could be a truecolor gif
if ((iImage==0) && (image.w != dscgif.scrwidth) && (image.h != dscgif.scrheight))
{
printf("======it could be a truecolor gif===== \n");
bTrueColor++;
}
// Local colour map?
if (image.pf & 0x80) {
TabCol.sogct = (short)(1 << ((image.pf & 0x07) +1));
//assert(3 == sizeof(struct rgb_color));
fp->Read(TabCol.paleta,sizeof(struct rgb_color)*TabCol.sogct,1);
//log << "Local colour map" << endl;
}
int bpp; //<DP> select the correct bit per pixel value
if (TabCol.sogct <= 2) bpp = 1;
else if (TabCol.sogct <= 16) bpp = 4;
else bpp = 8;
CJzImageGIF backimage;
backimage.CopyInfo(*this);
//printf(" 22 dscgif.scrwidth: %d, dscgif.scrheight: %d\n", dscgif.scrwidth, dscgif.scrheight );
if (iImage==0){
//first frame: build image background
backimage.Create(dscgif.scrwidth, dscgif.scrheight, bpp, CXIMAGE_FORMAT_GIF);
first_transparent_index = info.nBkgndIndex;
backimage.Clear((BYTE)gifgce.transpcolindex);
} else {
//generic frame: handle disposal method from previous one
/*Values : 0 - No disposal specified. The decoder is
not required to take any action.
1 - Do not dispose. The graphic is to be left
in place.
2 - Restore to background color. The area used by the
graphic must be restored to the background color.
3 - Restore to previous. The decoder is required to
restore the area overwritten by the graphic with
what was there prior to rendering the graphic.
*/
backimage.Copy(*this);
if (prevdispmeth==2)
{
method = 2;
backimage.Clear((BYTE)first_transparent_index);
}
}
//active frame
Create(image.w, image.h, bpp, CXIMAGE_FORMAT_GIF);
if ((image.pf & 0x80) || (dscgif.pflds & 0x80)) {
unsigned char r[256], g[256], b[256];
int i, has_white = 0;
for (i=0; i < TabCol.sogct; i++) {
r[i] = TabCol.paleta[i].r;
g[i] = TabCol.paleta[i].g;
b[i] = TabCol.paleta[i].b;
if (RGB(r[i],g[i],b[i]) == 0xFFFFFF) has_white = 1;
}
// Force transparency colour white...
//if (0) if (info.nBkgndIndex != -1)
// r[info.nBkgndIndex] = g[info.nBkgndIndex] = b[info.nBkgndIndex] = 255;
// Fill in with white // AD
if (info.nBkgndIndex != -1) {
while (i < 256) {
has_white = 1;
r[i] = g[i] = b[i] = 255;
i++;
}
}
// Force last colour to white... // AD
//if ((info.nBkgndIndex != -1) && !has_white) {
// r[255] = g[255] = b[255] = 255;
//}
SetPalette((info.nBkgndIndex != -1 ? 256 : TabCol.sogct), r, g, b);
}
CImageIterator* iter = new CImageIterator(this);
iter->Upset();
int badcode=0;
ibf = GIFBUFTAM+1;
interlaced = image.pf & 0x40;
iheight = image.h;
istep = 8;
iypos = 0;
ipass = 0;
long pos_start = fp->Tell();
//if (interlaced) log << "Interlaced" << endl;
decoder(fp, iter, image.w, badcode);
delete iter;
if (info.nEscape) return false; // <vho> - cancel decoding
if (bTrueColor<2 ){ //standard GIF: mix frame with background
backimage.GifMix(*this,image);
backimage.SetTransIndex(first_transparent_index);
//backimage.SetPalette(GetPalette());
//printf(" cpp ======== backimage.GetPalette: %x\n", backimage.GetPalette());
//printf(" cpp ======== GetPalette: %x\n", GetPalette());
//printf(" cpp ======== backimage.GetPaletteSize: %x\n", backimage.GetPaletteSize());
memcpy(backimage.GetPalette(),GetPalette(),backimage.GetPaletteSize());
//memcpy((void *)pRgb,(void *)GetPalette(),backimage.GetPaletteSize());
Transfer(backimage);
} else { //it's a truecolor gif!
//force full image decoding
info.nFrame=info.nNumFrames-1;
//build the RGB image
if (imaRGB==NULL) imaRGB = new CJzImage(dscgif.scrwidth,dscgif.scrheight,24,CXIMAGE_FORMAT_GIF);
//copy the partial image into the full RGB image
for(long y=0;y<image.h;y++){
for (long x=0;x<image.w;x++){
imaRGB->SetPixelColor(x+image.l,dscgif.scrheight-1-image.t-y,GetPixelColor(x,image.h-y-1));
}
}
}
// show gif for Jz
prevdispmeth = gifgce.dispmeth;
//restore the correct position in the file for the next image
if (badcode){
seek_next_image(fp,pos_start);
} else {
fp->Seek(-(ibfmax - ibf - 1), SEEK_CUR);
}
//printf(" ==== info.nFrame: %d, iImage: %d\n", info.nFrame, iImage );
GIFShowFrame( pbuffer, image, method );
if( info.nNumFrames == 1 ) gifgce.delaytime = 1;
ivdelay( gifgce.delaytime*10 );
if( ivIsGifQuit() )
bContinue=false;
if (info.nFrame==iImage) bContinue=false;
else
{
iImage++;
}
break;
}
case ';': //terminator
bContinue=false;
break;
default:
bPreviousWasNull = (ch==0);
break;
}
}
}
if (bTrueColor>=2 && imaRGB){
if (gifgce.transpcolflag){
imaRGB->SetTransColor(GetPaletteColor((BYTE)info.nBkgndIndex));
imaRGB->SetTransIndex(0);
}
Transfer(*imaRGB);
}
//if( pRgb ) delete [] pRgb;
delete imaRGB;
Destroy();
//printf("cpp================ quit gif decode ============\n");
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool CJzImageGIF::DecodeExtension(CxFile *fp)
{
bool bContinue;
unsigned char count;
unsigned char fc;
bContinue = (1 == fp->Read(&fc, sizeof(fc), 1));
if (bContinue) {
/* AD - for transparency */
if (fc == 0xF9) {
bContinue = (1 == fp->Read(&count, sizeof(count), 1));
if (bContinue) {
//assert(sizeof(gifgce) == 4);
bContinue = (count == fp->Read(&gifgce, 1, sizeof(gifgce)));
if (bContinue) {
if (gifgce.transpcolflag) info.nBkgndIndex = gifgce.transpcolindex;
info.dwFrameDelay = gifgce.delaytime;
m_dispmeth = gifgce.dispmeth;
} } }
if (fc == 0xFE) { //<DP> Comment block
bContinue = (1 == fp->Read(&count, sizeof(count), 1));
if (bContinue) {
bContinue = (1 == fp->Read(m_comment, count, 1));
m_comment[count]='\0';
} }
if (fc == 0xFF) { //<DP> Application Extension block
bContinue = (1 == fp->Read(&count, sizeof(count), 1));
if (bContinue) {
bContinue = (count==11);
if (bContinue){
char AppID[11];
bContinue = (1 == fp->Read(AppID, count, 1));
if (bContinue) {
bContinue = (1 == fp->Read(&count, sizeof(count), 1));
if (bContinue) {
BYTE* dati = (BYTE*)malloc(count);
bContinue = (dati!=NULL);
if (bContinue){
bContinue = (1 == fp->Read(dati, count, 1));
if (count>2){
m_loops = dati[1]+256*dati[2];
}
}
free(dati);
} } } } }
while (bContinue && fp->Read(&count, sizeof(count), 1) && count) {
//log << "Skipping " << count << " bytes" << endl;
fp->Seek(count, SEEK_CUR);
}
}
return bContinue;
}
// - This external (machine specific) function is expected to return
// either the next BYTE from the GIF file, or a negative error number.
int CJzImageGIF::get_byte(CxFile* file)
{
if (ibf>=GIFBUFTAM){
// FW 06/02/98 >>>
ibfmax = file->Read( buf , 1 , GIFBUFTAM) ;
if( ibfmax < GIFBUFTAM ) buf[ ibfmax ] = 255 ;
// FW 06/02/98 <<<
ibf = 0;
}
if (ibf>=ibfmax) return -1; //<DP> avoid overflows
return buf[ibf++];
}
////////////////////////////////////////////////////////////////////////////////
/* - This function takes a full line of pixels (one BYTE per pixel) and
* displays them (or does whatever your program wants with them...). It
* should return zero, or negative if an error or some other event occurs
* which would require aborting the decode process... Note that the length
* passed will almost always be equal to the line length passed to the
* decoder function, with the sole exception occurring when an ending code
* occurs in an odd place in the GIF file... In any case, linelen will be
* equal to the number of pixels passed...
*/
int CJzImageGIF::out_line(CImageIterator* iter, unsigned char *pixels, int linelen)
{
//<DP> for 1 & 4 bpp images, the pixels are compressed
if (head.biBitCount < 8){
for(long x=0;x<head.biWidth;x++){
BYTE pos;
BYTE* iDst= pixels + (x*head.biBitCount >> 3);
if (head.biBitCount==4){
pos = (BYTE)(4*(1-x%2));
*iDst &= ~(0x0F<<pos);
*iDst |= ((pixels[x] & 0x0F)<<pos);
} else if (head.biBitCount==1){
pos = (BYTE)(7-x%8);
*iDst &= ~(0x01<<pos);
*iDst |= ((pixels[x] & 0x01)<<pos);
}
}
}
/* AD - for interlace */
if (interlaced)
{
iter->SetY(iheight-iypos-1);
iter->SetRow(pixels, linelen);
if ((iypos += istep) >= iheight) {
do {
if (ipass++ > 0) istep /= 2;
iypos = istep / 2;
}
while (iypos > iheight);
}
return 0;
}
else
{
if (iter->ItOK()) {
iter->SetRow(pixels, linelen);
(void)iter->PrevRow();
return 0;
} else {
// puts("chafeo");
return -1;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?