--- /dev/null
+/*\r
+ * File: ximaico.cpp\r
+ * Purpose: Platform Independent ICON Image Class Loader and Writer (MS version)\r
+ * 07/Aug/2001 Davide Pizzolato - www.xdp.it\r
+ * CxImage version 6.0.0 02/Feb/2008\r
+ */\r
+\r
+#include "ximaico.h"\r
+\r
+#if CXIMAGE_SUPPORT_ICO\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+#if CXIMAGE_SUPPORT_DECODE\r
+////////////////////////////////////////////////////////////////////////////////\r
+bool CxImageICO::Decode(CxFile *hFile)\r
+{\r
+ if (hFile==NULL) return false;\r
+\r
+ DWORD off = hFile->Tell(); //<yuandi>\r
+ int page=info.nFrame; //internal icon structure indexes\r
+\r
+ // read the first part of the header\r
+ ICONHEADER icon_header;\r
+ hFile->Read(&icon_header,sizeof(ICONHEADER),1);\r
+\r
+ icon_header.idType = ntohs(icon_header.idType);\r
+ icon_header.idCount = ntohs(icon_header.idCount);\r
+\r
+ // check if it's an icon or a cursor\r
+ if ((icon_header.idReserved == 0) && ((icon_header.idType == 1)||(icon_header.idType == 2))) {\r
+\r
+ info.nNumFrames = icon_header.idCount;\r
+\r
+ // load the icon descriptions\r
+ ICONDIRENTRY *icon_list = (ICONDIRENTRY *)malloc(icon_header.idCount * sizeof(ICONDIRENTRY));\r
+ int c;\r
+ for (c = 0; c < icon_header.idCount; c++) {\r
+ hFile->Read(icon_list + c, sizeof(ICONDIRENTRY), 1);\r
+\r
+ icon_list[c].wPlanes = ntohs(icon_list[c].wPlanes);\r
+ icon_list[c].wBitCount = ntohs(icon_list[c].wBitCount);\r
+ icon_list[c].dwBytesInRes = ntohl(icon_list[c].dwBytesInRes);\r
+ icon_list[c].dwImageOffset = ntohl(icon_list[c].dwImageOffset);\r
+ }\r
+ \r
+ if ((page>=0)&&(page<icon_header.idCount)){\r
+\r
+ if (info.nEscape == -1) {\r
+ // Return output dimensions only\r
+ head.biWidth = icon_list[page].bWidth;\r
+ head.biHeight = icon_list[page].bHeight;\r
+#if CXIMAGE_SUPPORT_PNG\r
+ if (head.biWidth==0 && head.biHeight==0)\r
+ { // Vista icon support\r
+ hFile->Seek(off + icon_list[page].dwImageOffset, SEEK_SET);\r
+ CxImage png;\r
+ png.SetEscape(-1);\r
+ if (png.Decode(hFile,CXIMAGE_FORMAT_PNG)){\r
+ Transfer(png);\r
+ info.nNumFrames = icon_header.idCount;\r
+ }\r
+ }\r
+#endif //CXIMAGE_SUPPORT_PNG\r
+ free(icon_list);\r
+ info.dwType = CXIMAGE_FORMAT_ICO;\r
+ return true;\r
+ }\r
+\r
+ // get the bit count for the colors in the icon <CoreyRLucier>\r
+ BITMAPINFOHEADER bih;\r
+ hFile->Seek(off + icon_list[page].dwImageOffset, SEEK_SET);\r
+\r
+ if (icon_list[page].bWidth==0 && icon_list[page].bHeight==0)\r
+ { // Vista icon support\r
+#if CXIMAGE_SUPPORT_PNG\r
+ CxImage png;\r
+ if (png.Decode(hFile,CXIMAGE_FORMAT_PNG)){\r
+ Transfer(png);\r
+ info.nNumFrames = icon_header.idCount;\r
+ }\r
+ SetType(CXIMAGE_FORMAT_ICO);\r
+#endif //CXIMAGE_SUPPORT_PNG\r
+ }\r
+ else\r
+ { // standard icon\r
+ hFile->Read(&bih,sizeof(BITMAPINFOHEADER),1);\r
+\r
+ bihtoh(&bih);\r
+\r
+ c = bih.biBitCount;\r
+\r
+ // allocate memory for one icon\r
+ Create(icon_list[page].bWidth,icon_list[page].bHeight, c, CXIMAGE_FORMAT_ICO); //image creation\r
+\r
+ // read the palette\r
+ RGBQUAD pal[256];\r
+ if (bih.biClrUsed)\r
+ hFile->Read(pal,bih.biClrUsed*sizeof(RGBQUAD), 1);\r
+ else\r
+ hFile->Read(pal,head.biClrUsed*sizeof(RGBQUAD), 1);\r
+\r
+ SetPalette(pal,head.biClrUsed); //palette assign\r
+\r
+ //read the icon\r
+ if (c<=24){\r
+ hFile->Read(info.pImage, head.biSizeImage, 1);\r
+ } else { // 32 bit icon\r
+ BYTE* buf=(BYTE*)malloc(4*head.biHeight*head.biWidth);\r
+ BYTE* src = buf;\r
+ hFile->Read(buf, 4*head.biHeight*head.biWidth, 1);\r
+#if CXIMAGE_SUPPORT_ALPHA\r
+ if (!AlphaIsValid()) AlphaCreate();\r
+#endif //CXIMAGE_SUPPORT_ALPHA\r
+ for (long y = 0; y < head.biHeight; y++) {\r
+ BYTE* dst = GetBits(y);\r
+ for(long x=0;x<head.biWidth;x++){\r
+ *dst++=src[0];\r
+ *dst++=src[1];\r
+ *dst++=src[2];\r
+#if CXIMAGE_SUPPORT_ALPHA\r
+ AlphaSet(x,y,src[3]);\r
+#endif //CXIMAGE_SUPPORT_ALPHA\r
+ src+=4;\r
+ }\r
+ }\r
+ free(buf);\r
+ }\r
+ // apply the AND and XOR masks\r
+ int maskwdt = ((head.biWidth+31) / 32) * 4; //line width of AND mask (always 1 Bpp)\r
+ int masksize = head.biHeight * maskwdt; //size of mask\r
+ BYTE *mask = (BYTE *)malloc(masksize);\r
+ if (hFile->Read(mask, masksize, 1)){\r
+\r
+ bool bGoodMask=false;\r
+ for (int im=0;im<masksize;im++){\r
+ if (mask[im]!=255){\r
+ bGoodMask=true;\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (bGoodMask){\r
+#if CXIMAGE_SUPPORT_ALPHA\r
+ bool bNeedAlpha = false;\r
+ if (!AlphaIsValid()){\r
+ AlphaCreate();\r
+ } else { \r
+ bNeedAlpha=true; //32bit icon\r
+ }\r
+ int x,y;\r
+ for (y = 0; y < head.biHeight; y++) {\r
+ for (x = 0; x < head.biWidth; x++) {\r
+ if (((mask[y*maskwdt+(x>>3)]>>(7-x%8))&0x01)){\r
+ AlphaSet(x,y,0);\r
+ bNeedAlpha=true;\r
+ }\r
+ }\r
+ }\r
+ if (!bNeedAlpha) AlphaDelete();\r
+#endif //CXIMAGE_SUPPORT_ALPHA\r
+\r
+ //check if there is only one transparent color\r
+ RGBQUAD cc,ct;\r
+ long* pcc = (long*)&cc;\r
+ long* pct = (long*)&ct;\r
+ int nTransColors=0;\r
+ int nTransIndex=0;\r
+ for (y = 0; y < head.biHeight; y++){\r
+ for (x = 0; x < head.biWidth; x++){\r
+ if (((mask[y*maskwdt+(x>>3)] >> (7-x%8)) & 0x01)){\r
+ cc = GetPixelColor(x,y,false);\r
+ if (nTransColors==0){\r
+ nTransIndex = GetPixelIndex(x,y);\r
+ nTransColors++;\r
+ ct = cc;\r
+ } else {\r
+ if (*pct!=*pcc){\r
+ nTransColors++;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ if (nTransColors==1){\r
+ SetTransColor(ct);\r
+ SetTransIndex(nTransIndex);\r
+#if CXIMAGE_SUPPORT_ALPHA\r
+ AlphaDelete(); //because we have a unique transparent color in the image\r
+#endif //CXIMAGE_SUPPORT_ALPHA\r
+ }\r
+\r
+ // <vho> - Transparency support w/o Alpha support\r
+ if (c <= 8){ // only for icons with less than 256 colors (XP icons need alpha).\r
+ \r
+ // find a color index, which is not used in the image\r
+ // it is almost sure to find one, bcs. nobody uses all possible colors for an icon\r
+\r
+ BYTE colorsUsed[256];\r
+ memset(colorsUsed, 0, sizeof(colorsUsed));\r
+\r
+ for (y = 0; y < head.biHeight; y++){\r
+ for (x = 0; x < head.biWidth; x++){\r
+ colorsUsed[BlindGetPixelIndex(x,y)] = 1;\r
+ }\r
+ }\r
+\r
+ int iTransIdx = -1;\r
+ for (x = (int)(head.biClrUsed-1); x>=0 ; x--){\r
+ if (colorsUsed[x] == 0){\r
+ iTransIdx = x; // this one is not in use. we may use it as transparent color\r
+ break;\r
+ }\r
+ }\r
+\r
+ // Go thru image and set unused color as transparent index if needed\r
+ if (iTransIdx >= 0){\r
+ bool bNeedTrans = false;\r
+ for (y = 0; y < head.biHeight; y++){\r
+ for (x = 0; x < head.biWidth; x++){\r
+ // AND mask (Each Byte represents 8 Pixels)\r
+ if (((mask[y*maskwdt+(x>>3)] >> (7-x%8)) & 0x01)){\r
+ // AND mask is set (!=0). This is a transparent part\r
+ SetPixelIndex(x, y, (BYTE)iTransIdx);\r
+ bNeedTrans = true;\r
+ }\r
+ }\r
+ }\r
+ // set transparent index if needed\r
+ if (bNeedTrans) SetTransIndex(iTransIdx);\r
+#if CXIMAGE_SUPPORT_ALPHA\r
+ AlphaDelete(); //because we have a transparent color in the palette\r
+#endif //CXIMAGE_SUPPORT_ALPHA\r
+ }\r
+ }\r
+ } else {\r
+ SetTransIndex(0); //empty mask, set black as transparent color\r
+ Negative();\r
+ }\r
+ } \r
+ free(mask);\r
+ }\r
+ free(icon_list);\r
+ // icon has been loaded successfully!\r
+ return true;\r
+ }\r
+ free(icon_list);\r
+ }\r
+ return false;\r
+}\r
+////////////////////////////////////////////////////////////////////////////////\r
+#endif //CXIMAGE_SUPPORT_DECODE\r
+////////////////////////////////////////////////////////////////////////////////\r
+#if CXIMAGE_SUPPORT_ENCODE\r
+////////////////////////////////////////////////////////////////////////////////\r
+// Thanks to <Alas>\r
+bool CxImageICO::Encode(CxFile * hFile, CxImage ** pImages, int nPageCount)\r
+{\r
+ cx_try\r
+ {\r
+ if (hFile==NULL) cx_throw("invalid file pointer");\r
+ if (pImages==NULL || nPageCount<=0) cx_throw("multipage ICO, no images!");\r
+\r
+ int i;\r
+ for (i=0; i<nPageCount; i++){\r
+ if (pImages[i]==NULL)\r
+ cx_throw("Bad image pointer");\r
+ if (!(pImages[i]->IsValid()))\r
+ cx_throw("Empty image");\r
+ }\r
+\r
+ CxImageICO ghost;\r
+ for (i=0; i<nPageCount; i++){ //write headers\r
+ ghost.Ghost(pImages[i]);\r
+ ghost.info.nNumFrames = nPageCount;\r
+ if (i==0) {\r
+ if (!ghost.Encode(hFile,false,nPageCount))\r
+ cx_throw("Error writing ICO file header");\r
+ }\r
+ if (!ghost.Encode(hFile,true,nPageCount)) \r
+ cx_throw("Error saving ICO image header");\r
+ }\r
+ for (i=0; i<nPageCount; i++){ //write bodies\r
+ ghost.Ghost(pImages[i]);\r
+ ghost.info.nNumFrames = nPageCount;\r
+ if (!ghost.Encode(hFile,true,i)) \r
+ cx_throw("Error saving ICO body");\r
+ }\r
+\r
+ } cx_catch {\r
+ if (strcmp(message,"")) strncpy(info.szLastError,message,255);\r
+ return false;\r
+ }\r
+ return true;\r
+}\r
+////////////////////////////////////////////////////////////////////////////////\r
+bool CxImageICO::Encode(CxFile * hFile, bool bAppend, int nPageCount)\r
+{\r
+ if (EncodeSafeCheck(hFile)) return false;\r
+\r
+#if CXIMAGE_SUPPORT_PNG == 0\r
+ //check format limits\r
+ if ((head.biWidth>255)||(head.biHeight>255)){\r
+ strcpy(info.szLastError,"Can't save this image as icon");\r
+ return false;\r
+ }\r
+#endif\r
+\r
+ //prepare the palette struct\r
+ RGBQUAD* pal=GetPalette();\r
+ if (head.biBitCount<=8 && pal==NULL) return false;\r
+\r
+ int maskwdt=((head.biWidth+31)/32)*4; //mask line width\r
+ int masksize=head.biHeight * maskwdt; //size of mask\r
+ int bitcount=head.biBitCount;\r
+ int imagesize=head.biSizeImage;\r
+#if CXIMAGE_SUPPORT_ALPHA\r
+ if (AlphaIsValid() && head.biClrUsed==0){\r
+ bitcount=32;\r
+ imagesize=4*head.biHeight*head.biWidth;\r
+ }\r
+#endif\r
+\r
+ //fill the icon headers\r
+ int nPages = nPageCount;\r
+ if (nPages<1) nPages = 1;\r
+\r
+ ICONHEADER icon_header={0,1,nPages};\r
+\r
+ if (!bAppend)\r
+ m_dwImageOffset = sizeof(ICONHEADER) + nPages * sizeof(ICONDIRENTRY);\r
+\r
+ DWORD dwBytesInRes = sizeof(BITMAPINFOHEADER)+head.biClrUsed*sizeof(RGBQUAD)+imagesize+masksize;\r
+\r
+ ICONDIRENTRY icon_list={\r
+ (BYTE)head.biWidth,\r
+ (BYTE)head.biHeight,\r
+ (BYTE)head.biClrUsed,\r
+ 0, 0,\r
+ (WORD)bitcount,\r
+ dwBytesInRes,\r
+ m_dwImageOffset\r
+ };\r
+\r
+ BITMAPINFOHEADER bi={\r
+ sizeof(BITMAPINFOHEADER),\r
+ head.biWidth,\r
+ 2*head.biHeight,\r
+ 1,\r
+ (WORD)bitcount,\r
+ 0, imagesize,\r
+ 0, 0, 0, 0\r
+ };\r
+\r
+#if CXIMAGE_SUPPORT_PNG // Vista icon support\r
+ CxImage png(*this);\r
+ CxMemFile memfile;\r
+ if (head.biWidth>255 || head.biHeight>255){\r
+ icon_list.bWidth = icon_list.bHeight = 0;\r
+ memfile.Open();\r
+ png.Encode(&memfile,CXIMAGE_FORMAT_PNG);\r
+ icon_list.dwBytesInRes = dwBytesInRes = memfile.Size();\r
+ }\r
+#endif //CXIMAGE_SUPPORT_PNG\r
+\r
+ if (!bAppend){\r
+ icon_header.idType = ntohs(icon_header.idType);\r
+ icon_header.idCount = ntohs(icon_header.idCount);\r
+ hFile->Write(&icon_header,sizeof(ICONHEADER),1); //write the file header\r
+ icon_header.idType = ntohs(icon_header.idType);\r
+ icon_header.idCount = ntohs(icon_header.idCount);\r
+ }\r
+\r
+\r
+ if ((bAppend && nPageCount==info.nNumFrames) || (!bAppend && nPageCount==0)){\r
+ icon_list.wPlanes = ntohs(icon_list.wPlanes);\r
+ icon_list.wBitCount = ntohs(icon_list.wBitCount);\r
+ icon_list.dwBytesInRes = ntohl(icon_list.dwBytesInRes);\r
+ icon_list.dwImageOffset = ntohl(icon_list.dwImageOffset);\r
+ hFile->Write(&icon_list,sizeof(ICONDIRENTRY),1); //write the image entry\r
+ icon_list.wPlanes = ntohs(icon_list.wPlanes);\r
+ icon_list.wBitCount = ntohs(icon_list.wBitCount);\r
+ icon_list.dwBytesInRes = ntohl(icon_list.dwBytesInRes);\r
+ icon_list.dwImageOffset = ntohl(icon_list.dwImageOffset);\r
+\r
+ m_dwImageOffset += dwBytesInRes; //update offset for next header\r
+ }\r
+\r
+ if ((bAppend && nPageCount<info.nNumFrames) || (!bAppend && nPageCount==0))\r
+ {\r
+#if CXIMAGE_SUPPORT_PNG\r
+ if (icon_list.bWidth==0 && icon_list.bHeight==0) { // Vista icon support\r
+ hFile->Write(memfile.GetBuffer(false),dwBytesInRes,1);\r
+ } else\r
+#endif //CXIMAGE_SUPPORT_PNG\r
+ { // standard icon\r
+ bihtoh(&bi);\r
+ hFile->Write(&bi,sizeof(BITMAPINFOHEADER),1); //write the image header\r
+ bihtoh(&bi);\r
+\r
+ bool bTransparent = info.nBkgndIndex >= 0;\r
+ RGBQUAD ct = GetTransColor();\r
+ if (pal){\r
+ if (bTransparent) SetPaletteColor((BYTE)info.nBkgndIndex,0,0,0,0);\r
+ hFile->Write(pal,head.biClrUsed*sizeof(RGBQUAD),1); //write palette\r
+ if (bTransparent) SetPaletteColor((BYTE)info.nBkgndIndex,ct);\r
+ }\r
+\r
+#if CXIMAGE_SUPPORT_ALPHA\r
+ if (AlphaIsValid() && head.biClrUsed==0){\r
+ BYTE* buf=(BYTE*)malloc(imagesize);\r
+ BYTE* dst = buf;\r
+ for (long y = 0; y < head.biHeight; y++) {\r
+ BYTE* src = GetBits(y);\r
+ for(long x=0;x<head.biWidth;x++){\r
+ *dst++=*src++;\r
+ *dst++=*src++;\r
+ *dst++=*src++;\r
+ *dst++=AlphaGet(x,y);\r
+ }\r
+ }\r
+ hFile->Write(buf,imagesize, 1);\r
+ free(buf);\r
+ } else {\r
+ hFile->Write(info.pImage,imagesize,1); //write image\r
+ }\r
+#else\r
+ hFile->Write(info.pImage,imagesize,1); //write image\r
+#endif\r
+\r
+ //save transparency mask\r
+ BYTE* mask=(BYTE*)calloc(masksize,1); //create empty AND/XOR masks\r
+ if (!mask) return false;\r
+\r
+ //prepare the variables to build the mask\r
+ BYTE* iDst;\r
+ int pos,i;\r
+ RGBQUAD c={0,0,0,0};\r
+ long* pc = (long*)&c;\r
+ long* pct= (long*)&ct;\r
+#if CXIMAGE_SUPPORT_ALPHA\r
+ bool bAlphaPaletteIsValid = AlphaPaletteIsValid();\r
+ bool bAlphaIsValid = AlphaIsValid();\r
+#endif\r
+ //build the mask\r
+ for (int y = 0; y < head.biHeight; y++) {\r
+ for (int x = 0; x < head.biWidth; x++) {\r
+ i=0;\r
+#if CXIMAGE_SUPPORT_ALPHA\r
+ if (bAlphaIsValid && AlphaGet(x,y)==0) i=1;\r
+ if (bAlphaPaletteIsValid && BlindGetPixelColor(x,y).rgbReserved==0) i=1;\r
+#endif\r
+ c=GetPixelColor(x,y,false);\r
+ if (bTransparent && *pc==*pct) i=1;\r
+ iDst = mask + y*maskwdt + (x>>3);\r
+ pos = 7-x%8;\r
+ *iDst &= ~(0x01<<pos);\r
+ *iDst |= ((i & 0x01)<<pos);\r
+ }\r
+ }\r
+ //write AND/XOR masks\r
+ hFile->Write(mask,masksize,1);\r
+ free(mask);\r
+ }\r
+ }\r
+\r
+ return true;\r
+}\r
+////////////////////////////////////////////////////////////////////////////////\r
+#endif // CXIMAGE_SUPPORT_ENCODE\r
+////////////////////////////////////////////////////////////////////////////////\r
+#endif // CXIMAGE_SUPPORT_ICO\r
+\r