1 /////////////////////////////////////////////////////////////////////////////////
3 // $Id: png.cpp,v 1.3 2002/06/29 16:00:28 nedko Exp $
9 /////////////////////////////////////////////////////////////////////////////////
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
;
28 CPNGImage::~CPNGImage()
35 CPNGImage::PNG_ErrorHandler
PNGARG((png_structp s
, png_const_charp str
))
37 ::MessageBox(NULL
,str
,"PNG Error",MB_OK
);
41 CPNGImage::LoadPNGImage(const char *pszPath
)
45 unsigned int sig_read
= 0;
47 FILE *fp
= fopen(pszPath
, "rb");
53 png_ptr
= png_create_read_struct(PNG_LIBPNG_VER_STRING
,
64 info_ptr
= png_create_info_struct(png_ptr
);
68 png_destroy_read_struct(&png_ptr
, png_infopp_NULL
, png_infopp_NULL
);
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
);
77 /* If we get here, we had a problem reading the file */
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
);
88 PNG_TRANSFORM_IDENTITY
,
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
);
111 CPNGImage::GetWidth()
117 CPNGImage::GetHeight()
123 CPNGImage::SelectBitmap(HDC hMemoryDC
)
126 ZeroMemory(&BIH
,sizeof(BIH
));
127 BIH
.bV4Size
= sizeof(BIH
);
130 unsigned char *pPixel
;
134 hBitmap
= CreateBitmap(m_nWidth
,m_nHeight
,1,32,m_pData
);
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
;
154 hBitmap
= CreateBitmap(m_nWidth
,m_nHeight
,1,32,pInternalData
);
156 delete pInternalData
;
162 if (!GetDIBits(hMemoryDC
, hBitmap
, 0, 0, NULL
, (BITMAPINFO
*)&BIH
, DIB_RGB_COLORS
))
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
))
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);
190 if (!SetDIBits(hMemoryDC
, hBitmap
, 0, BIH
.bV4Height
, pData
, (BITMAPINFO
*)&BIH
, DIB_RGB_COLORS
))
198 SelectObject(hMemoryDC
, hBitmap
);
203 /////////////////////////////////////////////////////////////////////////////////
205 // Modifications log:
207 // !!! WARNING !!! Following lines are automatically updated by the CVS system.
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 /////////////////////////////////////////////////////////////////////////////////