*** empty log message ***
[desktopswitcher.git] / png.cpp
blob6f7cabc4aecded310e7e109d78cf6cfc37cd1282
1 /////////////////////////////////////////////////////////////////////////////////
2 //
3 // $Id: png.cpp,v 1.3 2002/06/29 16:00:28 nedko Exp $
4 //
5 // DESCRIPTION:
6 //
7 // NOTES:
8 //
9 /////////////////////////////////////////////////////////////////////////////////
11 #include "ph.h"
12 #include "png.h"
14 #define NOT_LOADED_BITMAP_HEIGHT 30
15 #define NOT_LOADED_BITMAP_WIDTH 400
16 #define NOT_LOADED_BITMAP_RED 0x80
17 #define NOT_LOADED_BITMAP_GREEN 0xE0
18 #define NOT_LOADED_BITMAP_BLUE 0xFF
19 #define NOT_LOADED_BITMAP_ALPHA 0xE0
21 CPNGImage::CPNGImage()
23 m_nWidth = NOT_LOADED_BITMAP_WIDTH;
24 m_nHeight = NOT_LOADED_BITMAP_HEIGHT;
25 m_pData = NULL;
28 CPNGImage::~CPNGImage()
30 if (m_pData)
31 delete m_pData;
34 void PNGAPI
35 CPNGImage::PNG_ErrorHandler PNGARG((png_structp s, png_const_charp str))
37 ::MessageBox(NULL,str,"PNG Error",MB_OK);
40 bool
41 CPNGImage::LoadPNGImage(const char *pszPath)
43 png_structp png_ptr;
44 png_infop info_ptr;
45 unsigned int sig_read = 0;
47 FILE *fp = fopen(pszPath, "rb");
48 if (!fp)
50 return false;
53 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
54 NULL,
55 PNG_ErrorHandler,
56 PNG_ErrorHandler);
58 if (png_ptr == NULL)
60 fclose(fp);
61 return false;
64 info_ptr = png_create_info_struct(png_ptr);
65 if (info_ptr == NULL)
67 fclose(fp);
68 png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
69 return false;
72 if (setjmp(png_jmpbuf(png_ptr)))
74 /* Free all of the memory associated with the png_ptr and info_ptr */
75 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
76 fclose(fp);
77 /* If we get here, we had a problem reading the file */
78 return false;
81 png_init_io(png_ptr, fp);
83 /* If we have already read some of the signature */
84 png_set_sig_bytes(png_ptr, sig_read);
86 png_read_png(png_ptr,
87 info_ptr,
88 PNG_TRANSFORM_IDENTITY,
89 png_voidp_NULL);
91 m_nHeight = png_get_image_height(png_ptr, info_ptr);
92 m_nWidth = png_get_image_width(png_ptr, info_ptr);
94 m_pData = new unsigned char[m_nWidth*m_nHeight*4];
95 png_bytep *row_pointers = png_get_rows(png_ptr, info_ptr);
96 for (unsigned int y = 0 ; y < m_nHeight; y++)
98 memcpy(m_pData + m_nWidth * 4 * y,row_pointers[y],m_nWidth*4);
101 /* clean up after the read, and free any memory allocated - REQUIRED */
102 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
104 /* close the file */
105 fclose(fp);
107 return true;
110 unsigned int
111 CPNGImage::GetWidth()
113 return m_nWidth;
116 unsigned int
117 CPNGImage::GetHeight()
119 return m_nHeight;
122 bool
123 CPNGImage::SelectBitmap(HDC hMemoryDC)
125 BITMAPV4HEADER BIH;
126 ZeroMemory(&BIH,sizeof(BIH));
127 BIH.bV4Size = sizeof(BIH);
128 HBITMAP hBitmap;
129 unsigned int x,y;
130 unsigned char *pPixel;
132 if (m_pData)
134 hBitmap = CreateBitmap(m_nWidth,m_nHeight,1,32,m_pData);
136 else
138 unsigned char *pInternalData = new unsigned char[m_nWidth*m_nHeight*4];
139 for (y = 0 ; y < m_nHeight; y++)
141 pPixel= pInternalData + m_nWidth * 4 * y;
143 for (x = 0 ; x < m_nWidth ; x++)
145 pPixel[0]= NOT_LOADED_BITMAP_BLUE;
146 pPixel[1]= NOT_LOADED_BITMAP_GREEN;
147 pPixel[2]= NOT_LOADED_BITMAP_RED;
148 pPixel[3]= NOT_LOADED_BITMAP_ALPHA;
150 pPixel+= 4;
154 hBitmap = CreateBitmap(m_nWidth,m_nHeight,1,32,pInternalData);
156 delete pInternalData;
159 if (!hBitmap)
160 return false;
162 if (!GetDIBits(hMemoryDC, hBitmap, 0, 0, NULL, (BITMAPINFO *)&BIH, DIB_RGB_COLORS))
163 return false;
165 BIH.bV4RedMask = 0x00FF0000;
166 BIH.bV4GreenMask = 0x0000FF00;
167 BIH.bV4BlueMask = 0x000000FF;
168 BIH.bV4AlphaMask = 0xFF000000;
170 unsigned char *pData = new unsigned char[BIH.bV4SizeImage];
171 if (!GetDIBits(hMemoryDC, hBitmap, 0, BIH.bV4Height, pData, (BITMAPINFO *)&BIH, DIB_RGB_COLORS))
173 delete pData;
174 return false;
177 for (y = 0 ; y < BIH.bV4Height; y++)
179 pPixel= pData + BIH.bV4Width * 4 * y;
181 for (x = 0 ; x < BIH.bV4Width ; x++)
183 pPixel[0]= (unsigned __int8)(((unsigned int)pPixel[0])*pPixel[3]/255);
184 pPixel[1]= (unsigned __int8)(((unsigned int)pPixel[1])*pPixel[3]/255);
185 pPixel[2]= (unsigned __int8)(((unsigned int)pPixel[2])*pPixel[3]/255);
186 pPixel+= 4;
190 if (!SetDIBits(hMemoryDC, hBitmap, 0, BIH.bV4Height, pData, (BITMAPINFO *)&BIH, DIB_RGB_COLORS))
192 delete pData;
193 return false;
196 delete pData;
198 SelectObject(hMemoryDC, hBitmap);
200 return true;
203 /////////////////////////////////////////////////////////////////////////////////
205 // Modifications log:
207 // !!! WARNING !!! Following lines are automatically updated by the CVS system.
209 // $Log: png.cpp,v $
210 // Revision 1.3 2002/06/29 16:00:28 nedko
211 // *** empty log message ***
213 // Revision 1.2 2002/06/29 15:20:10 nedko
214 // *** empty log message ***
216 // Revision 1.1 2002/06/29 15:03:33 nedko
217 // *** empty log message ***
219 /////////////////////////////////////////////////////////////////////////////////