3 * Purpose: Platform Independent ICON Image Class Loader and Writer (MS version)
4 * 07/Aug/2001 Davide Pizzolato - www.xdp.it
5 * CxImage version 6.0.0 02/Feb/2008
10 #if CXIMAGE_SUPPORT_ICO
12 ////////////////////////////////////////////////////////////////////////////////
13 #if CXIMAGE_SUPPORT_DECODE
14 ////////////////////////////////////////////////////////////////////////////////
15 bool CxImageICO::Decode(CxFile *hFile)
17 if (hFile==NULL) return false;
19 DWORD off = hFile->Tell(); //<yuandi>
20 int page=info.nFrame; //internal icon structure indexes
22 // read the first part of the header
23 ICONHEADER icon_header;
24 hFile->Read(&icon_header,sizeof(ICONHEADER),1);
26 icon_header.idType = ntohs(icon_header.idType);
27 icon_header.idCount = ntohs(icon_header.idCount);
29 // check if it's an icon or a cursor
30 if ((icon_header.idReserved == 0) && ((icon_header.idType == 1)||(icon_header.idType == 2))) {
32 info.nNumFrames = icon_header.idCount;
34 // load the icon descriptions
35 ICONDIRENTRY *icon_list = (ICONDIRENTRY *)malloc(icon_header.idCount * sizeof(ICONDIRENTRY));
37 for (c = 0; c < icon_header.idCount; c++) {
38 hFile->Read(icon_list + c, sizeof(ICONDIRENTRY), 1);
40 icon_list[c].wPlanes = ntohs(icon_list[c].wPlanes);
41 icon_list[c].wBitCount = ntohs(icon_list[c].wBitCount);
42 icon_list[c].dwBytesInRes = ntohl(icon_list[c].dwBytesInRes);
43 icon_list[c].dwImageOffset = ntohl(icon_list[c].dwImageOffset);
46 if ((page>=0)&&(page<icon_header.idCount)){
48 if (info.nEscape == -1) {
49 // Return output dimensions only
50 head.biWidth = icon_list[page].bWidth;
51 head.biHeight = icon_list[page].bHeight;
52 #if CXIMAGE_SUPPORT_PNG
53 if (head.biWidth==0 && head.biHeight==0)
54 { // Vista icon support
55 hFile->Seek(off + icon_list[page].dwImageOffset, SEEK_SET);
58 if (png.Decode(hFile,CXIMAGE_FORMAT_PNG)){
60 info.nNumFrames = icon_header.idCount;
63 #endif //CXIMAGE_SUPPORT_PNG
65 info.dwType = CXIMAGE_FORMAT_ICO;
69 // get the bit count for the colors in the icon <CoreyRLucier>
71 hFile->Seek(off + icon_list[page].dwImageOffset, SEEK_SET);
73 if (icon_list[page].bWidth==0 && icon_list[page].bHeight==0)
74 { // Vista icon support
75 #if CXIMAGE_SUPPORT_PNG
77 if (png.Decode(hFile,CXIMAGE_FORMAT_PNG)){
79 info.nNumFrames = icon_header.idCount;
81 SetType(CXIMAGE_FORMAT_ICO);
82 #endif //CXIMAGE_SUPPORT_PNG
86 hFile->Read(&bih,sizeof(BITMAPINFOHEADER),1);
92 // allocate memory for one icon
93 Create(icon_list[page].bWidth,icon_list[page].bHeight, c, CXIMAGE_FORMAT_ICO); //image creation
98 hFile->Read(pal,bih.biClrUsed*sizeof(RGBQUAD), 1);
100 hFile->Read(pal,head.biClrUsed*sizeof(RGBQUAD), 1);
102 SetPalette(pal,head.biClrUsed); //palette assign
106 hFile->Read(info.pImage, head.biSizeImage, 1);
107 } else { // 32 bit icon
108 BYTE* buf=(BYTE*)malloc(4*head.biHeight*head.biWidth);
110 hFile->Read(buf, 4*head.biHeight*head.biWidth, 1);
111 #if CXIMAGE_SUPPORT_ALPHA
112 if (!AlphaIsValid()) AlphaCreate();
113 #endif //CXIMAGE_SUPPORT_ALPHA
114 for (long y = 0; y < head.biHeight; y++) {
115 BYTE* dst = GetBits(y);
116 for(long x=0;x<head.biWidth;x++){
120 #if CXIMAGE_SUPPORT_ALPHA
121 AlphaSet(x,y,src[3]);
122 #endif //CXIMAGE_SUPPORT_ALPHA
128 // apply the AND and XOR masks
129 int maskwdt = ((head.biWidth+31) / 32) * 4; //line width of AND mask (always 1 Bpp)
130 int masksize = head.biHeight * maskwdt; //size of mask
131 BYTE *mask = (BYTE *)malloc(masksize);
132 if (hFile->Read(mask, masksize, 1)){
134 bool bGoodMask=false;
135 for (int im=0;im<masksize;im++){
143 #if CXIMAGE_SUPPORT_ALPHA
144 bool bNeedAlpha = false;
145 if (!AlphaIsValid()){
148 bNeedAlpha=true; //32bit icon
151 for (y = 0; y < head.biHeight; y++) {
152 for (x = 0; x < head.biWidth; x++) {
153 if (((mask[y*maskwdt+(x>>3)]>>(7-x%8))&0x01)){
159 if (!bNeedAlpha) AlphaDelete();
160 #endif //CXIMAGE_SUPPORT_ALPHA
162 //check if there is only one transparent color
164 long* pcc = (long*)&cc;
165 long* pct = (long*)&ct;
168 for (y = 0; y < head.biHeight; y++){
169 for (x = 0; x < head.biWidth; x++){
170 if (((mask[y*maskwdt+(x>>3)] >> (7-x%8)) & 0x01)){
171 cc = GetPixelColor(x,y,false);
172 if (nTransColors==0){
173 nTransIndex = GetPixelIndex(x,y);
184 if (nTransColors==1){
186 SetTransIndex(nTransIndex);
187 #if CXIMAGE_SUPPORT_ALPHA
188 AlphaDelete(); //because we have a unique transparent color in the image
189 #endif //CXIMAGE_SUPPORT_ALPHA
192 // <vho> - Transparency support w/o Alpha support
193 if (c <= 8){ // only for icons with less than 256 colors (XP icons need alpha).
195 // find a color index, which is not used in the image
196 // it is almost sure to find one, bcs. nobody uses all possible colors for an icon
198 BYTE colorsUsed[256];
199 memset(colorsUsed, 0, sizeof(colorsUsed));
201 for (y = 0; y < head.biHeight; y++){
202 for (x = 0; x < head.biWidth; x++){
203 colorsUsed[BlindGetPixelIndex(x,y)] = 1;
208 for (x = (int)(head.biClrUsed-1); x>=0 ; x--){
209 if (colorsUsed[x] == 0){
210 iTransIdx = x; // this one is not in use. we may use it as transparent color
215 // Go thru image and set unused color as transparent index if needed
217 bool bNeedTrans = false;
218 for (y = 0; y < head.biHeight; y++){
219 for (x = 0; x < head.biWidth; x++){
220 // AND mask (Each Byte represents 8 Pixels)
221 if (((mask[y*maskwdt+(x>>3)] >> (7-x%8)) & 0x01)){
222 // AND mask is set (!=0). This is a transparent part
223 SetPixelIndex(x, y, (BYTE)iTransIdx);
228 // set transparent index if needed
229 if (bNeedTrans) SetTransIndex(iTransIdx);
230 #if CXIMAGE_SUPPORT_ALPHA
231 AlphaDelete(); //because we have a transparent color in the palette
232 #endif //CXIMAGE_SUPPORT_ALPHA
236 SetTransIndex(0); //empty mask, set black as transparent color
243 // icon has been loaded successfully!
250 ////////////////////////////////////////////////////////////////////////////////
251 #endif //CXIMAGE_SUPPORT_DECODE
252 ////////////////////////////////////////////////////////////////////////////////
253 #if CXIMAGE_SUPPORT_ENCODE
254 ////////////////////////////////////////////////////////////////////////////////
256 bool CxImageICO::Encode(CxFile * hFile, CxImage ** pImages, int nPageCount)
260 if (hFile==NULL) cx_throw("invalid file pointer");
261 if (pImages==NULL || nPageCount<=0) cx_throw("multipage ICO, no images!");
264 for (i=0; i<nPageCount; i++){
265 if (pImages[i]==NULL)
266 cx_throw("Bad image pointer");
267 if (!(pImages[i]->IsValid()))
268 cx_throw("Empty image");
272 for (i=0; i<nPageCount; i++){ //write headers
273 ghost.Ghost(pImages[i]);
274 ghost.info.nNumFrames = nPageCount;
276 if (!ghost.Encode(hFile,false,nPageCount))
277 cx_throw("Error writing ICO file header");
279 if (!ghost.Encode(hFile,true,nPageCount))
280 cx_throw("Error saving ICO image header");
282 for (i=0; i<nPageCount; i++){ //write bodies
283 ghost.Ghost(pImages[i]);
284 ghost.info.nNumFrames = nPageCount;
285 if (!ghost.Encode(hFile,true,i))
286 cx_throw("Error saving ICO body");
290 if (strcmp(message,"")) strncpy(info.szLastError,message,255);
295 ////////////////////////////////////////////////////////////////////////////////
296 bool CxImageICO::Encode(CxFile * hFile, bool bAppend, int nPageCount)
298 if (EncodeSafeCheck(hFile)) return false;
300 #if CXIMAGE_SUPPORT_PNG == 0
301 //check format limits
302 if ((head.biWidth>255)||(head.biHeight>255)){
303 strcpy(info.szLastError,"Can't save this image as icon");
308 //prepare the palette struct
309 RGBQUAD* pal=GetPalette();
310 if (head.biBitCount<=8 && pal==NULL) return false;
312 int maskwdt=((head.biWidth+31)/32)*4; //mask line width
313 int masksize=head.biHeight * maskwdt; //size of mask
314 int bitcount=head.biBitCount;
315 int imagesize=head.biSizeImage;
316 #if CXIMAGE_SUPPORT_ALPHA
317 if (AlphaIsValid() && head.biClrUsed==0){
319 imagesize=4*head.biHeight*head.biWidth;
323 //fill the icon headers
324 int nPages = nPageCount;
325 if (nPages<1) nPages = 1;
327 ICONHEADER icon_header={0,1,nPages};
330 m_dwImageOffset = sizeof(ICONHEADER) + nPages * sizeof(ICONDIRENTRY);
332 DWORD dwBytesInRes = sizeof(BITMAPINFOHEADER)+head.biClrUsed*sizeof(RGBQUAD)+imagesize+masksize;
334 ICONDIRENTRY icon_list={
337 (BYTE)head.biClrUsed,
344 BITMAPINFOHEADER bi={
345 sizeof(BITMAPINFOHEADER),
354 #if CXIMAGE_SUPPORT_PNG // Vista icon support
357 if (head.biWidth>255 || head.biHeight>255){
358 icon_list.bWidth = icon_list.bHeight = 0;
360 png.Encode(&memfile,CXIMAGE_FORMAT_PNG);
361 icon_list.dwBytesInRes = dwBytesInRes = memfile.Size();
363 #endif //CXIMAGE_SUPPORT_PNG
366 icon_header.idType = ntohs(icon_header.idType);
367 icon_header.idCount = ntohs(icon_header.idCount);
368 hFile->Write(&icon_header,sizeof(ICONHEADER),1); //write the file header
369 icon_header.idType = ntohs(icon_header.idType);
370 icon_header.idCount = ntohs(icon_header.idCount);
374 if ((bAppend && nPageCount==info.nNumFrames) || (!bAppend && nPageCount==0)){
375 icon_list.wPlanes = ntohs(icon_list.wPlanes);
376 icon_list.wBitCount = ntohs(icon_list.wBitCount);
377 icon_list.dwBytesInRes = ntohl(icon_list.dwBytesInRes);
378 icon_list.dwImageOffset = ntohl(icon_list.dwImageOffset);
379 hFile->Write(&icon_list,sizeof(ICONDIRENTRY),1); //write the image entry
380 icon_list.wPlanes = ntohs(icon_list.wPlanes);
381 icon_list.wBitCount = ntohs(icon_list.wBitCount);
382 icon_list.dwBytesInRes = ntohl(icon_list.dwBytesInRes);
383 icon_list.dwImageOffset = ntohl(icon_list.dwImageOffset);
385 m_dwImageOffset += dwBytesInRes; //update offset for next header
388 if ((bAppend && nPageCount<info.nNumFrames) || (!bAppend && nPageCount==0))
390 #if CXIMAGE_SUPPORT_PNG
391 if (icon_list.bWidth==0 && icon_list.bHeight==0) { // Vista icon support
392 hFile->Write(memfile.GetBuffer(false),dwBytesInRes,1);
394 #endif //CXIMAGE_SUPPORT_PNG
397 hFile->Write(&bi,sizeof(BITMAPINFOHEADER),1); //write the image header
400 bool bTransparent = info.nBkgndIndex >= 0;
401 RGBQUAD ct = GetTransColor();
403 if (bTransparent) SetPaletteColor((BYTE)info.nBkgndIndex,0,0,0,0);
404 hFile->Write(pal,head.biClrUsed*sizeof(RGBQUAD),1); //write palette
405 if (bTransparent) SetPaletteColor((BYTE)info.nBkgndIndex,ct);
408 #if CXIMAGE_SUPPORT_ALPHA
409 if (AlphaIsValid() && head.biClrUsed==0){
410 BYTE* buf=(BYTE*)malloc(imagesize);
412 for (long y = 0; y < head.biHeight; y++) {
413 BYTE* src = GetBits(y);
414 for(long x=0;x<head.biWidth;x++){
418 *dst++=AlphaGet(x,y);
421 hFile->Write(buf,imagesize, 1);
424 hFile->Write(info.pImage,imagesize,1); //write image
427 hFile->Write(info.pImage,imagesize,1); //write image
430 //save transparency mask
431 BYTE* mask=(BYTE*)calloc(masksize,1); //create empty AND/XOR masks
432 if (!mask) return false;
434 //prepare the variables to build the mask
438 long* pc = (long*)&c;
439 long* pct= (long*)&ct;
440 #if CXIMAGE_SUPPORT_ALPHA
441 bool bAlphaPaletteIsValid = AlphaPaletteIsValid();
442 bool bAlphaIsValid = AlphaIsValid();
445 for (int y = 0; y < head.biHeight; y++) {
446 for (int x = 0; x < head.biWidth; x++) {
448 #if CXIMAGE_SUPPORT_ALPHA
449 if (bAlphaIsValid && AlphaGet(x,y)==0) i=1;
450 if (bAlphaPaletteIsValid && BlindGetPixelColor(x,y).rgbReserved==0) i=1;
452 c=GetPixelColor(x,y,false);
453 if (bTransparent && *pc==*pct) i=1;
454 iDst = mask + y*maskwdt + (x>>3);
456 *iDst &= ~(0x01<<pos);
457 *iDst |= ((i & 0x01)<<pos);
460 //write AND/XOR masks
461 hFile->Write(mask,masksize,1);
468 ////////////////////////////////////////////////////////////////////////////////
469 #endif // CXIMAGE_SUPPORT_ENCODE
470 ////////////////////////////////////////////////////////////////////////////////
471 #endif // CXIMAGE_SUPPORT_ICO