*** empty log message ***
[desktopswitcher.git] / png.cpp
blobe3d324a824082977d5fa9be813eb2048a99cd342
1 /////////////////////////////////////////////////////////////////////////////////
2 //
3 // $Id: png.cpp,v 1.1 2002/06/29 15:03:33 nedko Exp $
4 //
5 // DESCRIPTION:
6 //
7 // NOTES:
8 //
9 /////////////////////////////////////////////////////////////////////////////////
11 #include "ph.h"
12 #include "png.h"
14 #define NOT_LOADED_BITMAP_HEIGHT 50
15 #define NOT_LOADED_BITMAP_WIDTH 300
17 CPNGImage::CPNGImage()
19 m_nWidth = 0;
20 m_nHeight = 0;
21 m_pData = NULL;
24 CPNGImage::~CPNGImage()
28 void PNGAPI
29 CPNGImage::PNG_ErrorHandler PNGARG((png_structp s, png_const_charp str))
31 ::MessageBox(NULL,str,"PNG Error",MB_OK);
34 bool
35 CPNGImage::LoadPNGImage(const char *pszPath)
37 png_structp png_ptr;
38 png_infop info_ptr;
39 unsigned int sig_read = 0;
41 FILE *fp = fopen(pszPath, "rb");
42 if (!fp)
44 return false;
47 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
48 NULL,
49 PNG_ErrorHandler,
50 PNG_ErrorHandler);
52 if (png_ptr == NULL)
54 fclose(fp);
55 return false;
58 info_ptr = png_create_info_struct(png_ptr);
59 if (info_ptr == NULL)
61 fclose(fp);
62 png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
63 return false;
66 if (setjmp(png_jmpbuf(png_ptr)))
68 /* Free all of the memory associated with the png_ptr and info_ptr */
69 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
70 fclose(fp);
71 /* If we get here, we had a problem reading the file */
72 return false;
75 png_init_io(png_ptr, fp);
77 /* If we have already read some of the signature */
78 png_set_sig_bytes(png_ptr, sig_read);
80 png_read_png(png_ptr,
81 info_ptr,
82 PNG_TRANSFORM_IDENTITY,
83 png_voidp_NULL);
86 /* clean up after the read, and free any memory allocated - REQUIRED */
87 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
89 /* close the file */
90 fclose(fp);
92 return true;
95 unsigned int
96 CPNGImage::GetWidth()
98 if (m_pData)
99 return m_nWidth;
100 else
101 return NOT_LOADED_BITMAP_WIDTH;
104 unsigned int
105 CPNGImage::GetHeight()
107 if (m_pData)
108 return m_nHeight;
109 else
110 return NOT_LOADED_BITMAP_HEIGHT;
113 bool
114 CPNGImage::SelectBitmap(HDC hMemoryDC)
116 BITMAPV4HEADER BIH;
117 ZeroMemory(&BIH,sizeof(BIH));
118 BIH.bV4Size = sizeof(BIH);
119 unsigned int x,y;
120 unsigned char *pPixel;
122 unsigned char *pInternalData = new unsigned char[327*45*4];
123 for (y = 0 ; y < 45; y++)
125 pPixel= pInternalData + 327 * 4 * y;
127 for (x = 0 ; x < 327 ; x++)
129 pPixel[0]= 0xFF; // blue
130 pPixel[1]= 0xFF; // green
131 pPixel[2]= 0xFF; // red
132 pPixel[3]= 180; // alpha
133 pPixel+= 4;
137 HBITMAP hBitmap = CreateBitmap(NOT_LOADED_BITMAP_WIDTH,NOT_LOADED_BITMAP_HEIGHT,1,32,pInternalData);
139 // delete pInternalData;
141 if (!hBitmap)
142 return false;
144 if (!GetDIBits(hMemoryDC, hBitmap, 0, 0, NULL, (BITMAPINFO *)&BIH, DIB_RGB_COLORS))
145 return false;
147 BIH.bV4RedMask = 0x00FF0000;
148 BIH.bV4GreenMask = 0x0000FF00;
149 BIH.bV4BlueMask = 0x000000FF;
150 BIH.bV4AlphaMask = 0xFF000000;
152 unsigned char *pData = new unsigned char[BIH.bV4SizeImage];
153 if (!GetDIBits(hMemoryDC, hBitmap, 0, BIH.bV4Height, pData, (BITMAPINFO *)&BIH, DIB_RGB_COLORS))
155 delete pData;
156 return false;
159 for (y = 0 ; y < BIH.bV4Height; y++)
161 pPixel= pData + BIH.bV4Width * 4 * y;
163 for (x = 0 ; x < BIH.bV4Width ; x++)
165 pPixel[0]= (unsigned __int8)(((unsigned int)pPixel[0])*pPixel[3]/255);
166 pPixel[1]= (unsigned __int8)(((unsigned int)pPixel[1])*pPixel[3]/255);
167 pPixel[2]= (unsigned __int8)(((unsigned int)pPixel[2])*pPixel[3]/255);
168 pPixel+= 4;
172 if (!SetDIBits(hMemoryDC, hBitmap, 0, BIH.bV4Height, pData, (BITMAPINFO *)&BIH, DIB_RGB_COLORS))
174 delete pData;
175 return false;
178 delete pData;
180 SelectObject(hMemoryDC, hBitmap);
182 return true;
185 /////////////////////////////////////////////////////////////////////////////////
187 // Modifications log:
189 // !!! WARNING !!! Following lines are automatically updated by the CVS system.
191 // $Log: png.cpp,v $
192 // Revision 1.1 2002/06/29 15:03:33 nedko
193 // *** empty log message ***
195 /////////////////////////////////////////////////////////////////////////////////