📄 ximagif.cpp
字号:
/* * File: ximagif.cpp * Purpose: Platform Independent GIF Image Class Loader and Writer * 07/Aug/2001 Davide Pizzolato - www.xdp.it * CxImage version 5.99c 17/Oct/2004 */#include "ximagif.h"#if CXIMAGE_SUPPORT_GIF#include "ximaiter.h"#if CXIMAGE_SUPPORT_WINCE #define assert(s)#else #include <assert.h>#endifbool CxImageGIF::CheckFormat(BYTE * buffer, DWORD size, basic_image_information* basic_info){ struct_dscgif *dscgif; if (size<sizeof(*dscgif)) return false; dscgif = (struct_dscgif *) buffer; if (strncmp(dscgif->header,"GIF8",4)!=0) return false; create_basic_image_information(CXIMAGE_FORMAT_GIF,ltohs(dscgif->scrwidth),ltohs(dscgif->scrheight),basic_info); return true;}////////////////////////////////////////////////////////////////////////////////bool CxImageGIF::Decode(CxFile *fp){ /* AD - for transparency */ struct_dscgif dscgif; struct_image image; struct_TabCol TabCol; if (fp == NULL) return false; fp->Read(&dscgif,/*sizeof(dscgif)*/13,1); //if (strncmp(dscgif.header,"GIF8",3)!=0) { if (strncmp(dscgif.header,"GIF8",4)!=0) return FALSE; // Avoid Byte order problem with Mac dscgif.scrheight = ltohs(dscgif.scrheight); dscgif.scrwidth = ltohs(dscgif.scrwidth); if (info.nEscape == -1) { // Return output dimensions only head.biWidth = dscgif.scrwidth; head.biHeight = dscgif.scrheight; 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; CxImage* 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); 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; CxImage *previousFrame = NULL; 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;*/ image.l = ltohs(image.l); image.t = ltohs(image.t); image.w = ltohs(image.w); image.h = ltohs(image.h); 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)) 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; CxImageGIF backimage; backimage.CopyInfo(*this); 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); previousFrame = new CxImage(backimage); previousFrame->RetreiveSingleFrame(); } 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. */ if (prevdispmeth==2){ backimage.Copy(*this,false,false,false); backimage.Clear((BYTE)first_transparent_index); } else if (prevdispmeth==3) { backimage.Copy(*this,false,false,false); backimage.Create(previousFrame->GetWidth(), previousFrame->GetHeight(), previousFrame->GetBpp(),CXIMAGE_FORMAT_GIF); memcpy(backimage.GetDIB(),previousFrame->GetDIB(), backimage.GetSize()); backimage.AlphaSet(*previousFrame); } else { backimage.Copy(*this); } } //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()); 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 CxImage(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)); } } } prevdispmeth = (gifgce.flags >> 2) & 0x7; //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); } if (info.bGetAllFrames) { if (iImage == 0) { DestroyGifFrames(); info.GifFrames = new CxImage*[info.nNumFrames]; for(int frameIdx = 0; frameIdx < info.nNumFrames; frameIdx++){ info.GifFrames[frameIdx] = NULL; } } if(imaRGB) info.GifFrames[iImage] = new CxImage(*imaRGB); else info.GifFrames[iImage] = new CxImage(*this); info.GifFrames[iImage]->RetreiveSingleFrame(); } if (prevdispmeth <= 1) { delete previousFrame; previousFrame = new CxImage(*this); previousFrame->RetreiveSingleFrame(); } 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.flags & 0x1){ imaRGB->SetTransColor(GetPaletteColor((BYTE)info.nBkgndIndex)); imaRGB->SetTransIndex(0); } Transfer(*imaRGB); } delete imaRGB; delete previousFrame; return true;}////////////////////////////////////////////////////////////////////////////////bool CxImageGIF::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))); // Avoid Byte order problem with Mac gifgce.delaytime = ltohs(gifgce.delaytime); //fprintf(stderr, "Transparency block get, valid ? %u, Has transparency ? %u\n",bContinue, gifgce.flags & 0x1); //fprintf(stderr, "transpcolflag %u : userinputflag %u : dispmeth %u : res %u\n", gifgce.flags & 0x1, (gifgce.flags >> 1) & 0x1, (gifgce.flags >> 2) & 0x7, (gifgce.flags >> 5) & 0x7 ); if (bContinue) { if (gifgce.flags & 0x1) info.nBkgndIndex = gifgce.transpcolindex; info.dwFrameDelay = gifgce.delaytime; SetDisposalMethod((gifgce.flags >> 2) & 0x7); } } } 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 CxImageGIF::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 CxImageGIF::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); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -