1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implements a PNG reader class + handler
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "pngread.h"
14 #pragma implementation "pnghand.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
29 #include <wx/msw/pngread.h>
30 #include <wx/msw/dibutils.h>
36 extern "C" void png_read_init
PNGARG((png_structp png_ptr
));
37 extern "C" void png_write_init
PNGARG((png_structp png_ptr
));
39 #ifndef GlobalAllocPtr
40 #define GlobalPtrHandle(lp) \
41 ((HGLOBAL)GlobalHandle(lp))
43 #define GlobalLockPtr(lp) \
44 ((BOOL)GlobalLock(GlobalPtrHandle(lp)))
45 #define GlobalUnlockPtr(lp) \
46 GlobalUnlock(GlobalPtrHandle(lp))
48 #define GlobalAllocPtr(flags, cb) \
49 (GlobalLock(GlobalAlloc((flags), (cb))))
50 #define GlobalReAllocPtr(lp, cbNew, flags) \
51 (GlobalUnlockPtr(lp), GlobalLock(GlobalReAlloc(GlobalPtrHandle(lp) , (cbNew), (flags))))
52 #define GlobalFreePtr(lp) \
53 (GlobalUnlockPtr(lp), (BOOL)GlobalFree(GlobalPtrHandle(lp)))
58 ima_png_error(png_struct
*png_ptr
, char *message
)
60 // wxMessageBox(message, "PNG error");
62 longjmp(png_ptr
->jmpbuf
, 1);
66 // static wxGifReaderIter* iter;
67 wxPalette
*wxCopyPalette(const wxPalette
*cmap
);
69 wxPNGReader::wxPNGReader(void)
72 RawImage
= NULL
; // Image data
74 Width
= 0; Height
= 0; // Dimensions
75 Depth
= 0; // (bits x pixel)
76 ColorType
= 0; // Bit 1 = Palette used
80 EfeWidth
= 0; // Efective Width
88 wxPNGReader::wxPNGReader ( char* ImageFileName
)
92 RawImage
= NULL
; // Image data
94 Width
= 0; Height
= 0; // Dimensions
95 Depth
= 0; // (bits x pixel)
96 ColorType
= 0; // Bit 1 = Palette used
100 EfeWidth
= 0; // Efective Width
106 imageOK
= ReadFile (ImageFileName
);
110 wxPNGReader::Create(int width
, int height
, int depth
, int colortype
)
112 Width
= width
; Height
= height
; Depth
= depth
;
113 ColorType
= (colortype
>=0) ? colortype
: ((Depth
>8) ? COLORTYPE_COLOR
: 0);
121 if (lpbi
= DibCreate(Depth
, Width
, Height
)) {
122 RawImage
= (ImagePointerType
)DibPtr(lpbi
);
123 EfeWidth
= (long)(((long)Width
*Depth
+ 31) / 32) * 4;
128 wxPNGReader::~wxPNGReader ( )
137 int wxPNGReader::GetIndex(int x
, int y
)
139 if (!Inside(x
, y
) || (Depth
>8)) return -1;
141 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
142 int index
= (int)(*ImagePointer
);
146 bool wxPNGReader::GetRGB(int x
, int y
, byte
* r
, byte
* g
, byte
* b
)
148 if (!Inside(x
, y
)) return FALSE
;
151 return Palette
->GetRGB(GetIndex(x
, y
), r
, g
, b
);
152 /* PALETTEENTRY entry;
153 ::GetPaletteEntries((HPALETTE) Palette->GetHPALETTE(), GetIndex(x, y), 1, &entry);
156 *b = entry.peBlue; */
158 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
159 *b
= ImagePointer
[0];
160 *g
= ImagePointer
[1];
161 *r
= ImagePointer
[2];
167 bool wxPNGReader::SetIndex(int x
, int y
, int index
)
169 if (!Inside(x
, y
) || (Depth
>8)) return FALSE
;
171 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
172 *ImagePointer
= index
;
177 bool wxPNGReader::SetRGB(int x
, int y
, byte r
, byte g
, byte b
)
179 if (!Inside(x
, y
)) return FALSE
;
181 if (ColorType
& COLORTYPE_PALETTE
)
183 if (!Palette
) return FALSE
;
184 SetIndex(x
, y
, Palette
->GetPixel(r
, g
, b
));
187 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
196 bool wxPNGReader::SetPalette(wxPalette
* colourmap
)
200 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
202 return (DibSetUsage(lpbi
, (HPALETTE
) Palette
->GetHPALETTE(), WXIMA_COLORS
) != 0);
206 wxPNGReader::SetPalette(int n
, byte
*r
, byte
*g
, byte
*b
)
208 Palette
= new wxPalette();
214 Palette
->Create(n
, r
, g
, b
);
215 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
216 return (DibSetUsage(lpbi
, (HPALETTE
) Palette
->GetHPALETTE(), WXIMA_COLORS
) != 0);
220 wxPNGReader::SetPalette(int n
, rgb_color_struct
*rgb_struct
)
222 Palette
= new wxPalette();
226 byte r
[256], g
[256], b
[256];
228 for(int i
=0; i
<n
; i
++)
230 r
[i
] = rgb_struct
[i
].red
;
231 g
[i
] = rgb_struct
[i
].green
;
232 b
[i
] = rgb_struct
[i
].blue
;
234 // Added by JACS copying from Andrew Davison's additions
235 // to GIF-reading code
236 // Make transparency colour black...
238 r
[bgindex
] = g
[bgindex
] = b
[bgindex
] = 0;
240 Palette
->Create(n
, r
, g
, b
);
241 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
242 return (DibSetUsage(lpbi
, (HPALETTE
) Palette
->GetHPALETTE(), WXIMA_COLORS
) != 0);
245 void wxPNGReader::NullData()
251 wxBitmap
* wxPNGReader::GetBitmap(void)
253 wxBitmap
*bitmap
= new wxBitmap
;
254 if ( InstantiateBitmap(bitmap
) )
263 bool wxPNGReader::InstantiateBitmap(wxBitmap
*bitmap
)
265 HDC dc
= ::CreateCompatibleDC(NULL
);
269 // tmpBitmap is a dummy, to satisfy ::CreateCompatibleDC (it
270 // is a memory dc that must have a bitmap selected into it)
271 HDC dc2
= GetDC(NULL
);
272 HBITMAP tmpBitmap
= ::CreateCompatibleBitmap(dc2
, GetWidth(), GetHeight());
273 ReleaseDC(NULL
, dc2
);
274 HBITMAP oldBitmap
= ::SelectObject(dc
, tmpBitmap
);
278 HPALETTE oldPal
= ::SelectPalette(dc
, (HPALETTE
) Palette
->GetHPALETTE(), FALSE
);
279 ::RealizePalette(dc
);
282 HBITMAP hBitmap
= ::CreateDIBitmap(dc
, lpbi
,
283 CBM_INIT
, RawImage
, (LPBITMAPINFO
) lpbi
, DIB_PAL_COLORS
);
285 ::SelectPalette(dc
, NULL
, TRUE
);
286 ::SelectObject(dc
, oldBitmap
);
287 ::DeleteObject(tmpBitmap
);
292 bitmap
->SetHBITMAP((WXHBITMAP
) hBitmap
);
293 bitmap
->SetWidth(GetWidth());
294 bitmap
->SetHeight(GetHeight());
295 bitmap
->SetDepth(GetDepth());
296 if ( GetDepth() > 1 && Palette
)
297 bitmap
->SetPalette(*Palette
);
301 // Make a mask if appropriate
304 wxMask
*mask
= CreateMask();
305 bitmap
->SetMask(mask
);
320 wxPalette
*wxCopyPalette(const wxPalette
*cmap
)
322 // To get number of entries...
324 ::GetObject((HPALETTE
) cmap
->GetHPALETTE(), sizeof(WORD
), &count
);
326 LOGPALETTE
* logPal
= (LOGPALETTE
*)
327 new BYTE
[sizeof(LOGPALETTE
) + count
*sizeof(PALETTEENTRY
)];
328 logPal
->palVersion
= 0x300;
329 logPal
->palNumEntries
= count
;
330 ::GetPaletteEntries((HPALETTE
) cmap
->GetHPALETTE(), 0, count
, logPal
->palPalEntry
);
332 HPALETTE hPalette
= ::CreatePalette(logPal
);
335 wxPalette
*newCmap
= new wxPalette
;
336 newCmap
->SetHPALETTE((WXHPALETTE
) hPalette
);
340 wxMask
*wxPNGReader::CreateMask(void)
342 HBITMAP hBitmap
= ::CreateBitmap(GetWidth(), GetHeight(), 1, 1, NULL
);
344 HDC dc
= ::CreateCompatibleDC(NULL
);
345 HBITMAP oldBitmap
= ::SelectObject(dc
, hBitmap
);
347 int bgIndex
= GetBGIndex();
351 for (x
=0; x
<GetWidth(); x
++)
353 for (y
=0; y
<GetHeight(); y
++)
355 int index
= GetIndex(x
, y
);
356 if ( index
== bgIndex
)
357 ::SetPixel(dc
, x
, GetHeight() - y
- 1, RGB(0, 0, 0));
359 ::SetPixel(dc
, x
, GetHeight() - y
- 1, RGB(255, 255, 255));
363 ::SelectObject(dc
, oldBitmap
);
364 wxMask
*mask
= new wxMask
;
365 mask
->SetMaskBitmap((WXHBITMAP
) hBitmap
);
369 bool wxPNGReader::ReadFile(char * ImageFileName
)
374 strcpy(filename
, ImageFileName
);
379 wxPNGReaderIter
iter(this);
382 fp
= fopen(filename
, "rb");
386 /* allocate the necessary structures */
387 png_ptr
= new (png_struct
);
394 info_ptr
= new (png_info
);
401 /* set error handling */
402 if (setjmp(png_ptr
->jmpbuf
))
404 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
409 /* If we get here, we had a problem reading the file */
412 //png_set_error(ima_png_error, NULL);
414 /* initialize the structures, info first for error handling */
415 png_info_init(info_ptr
);
416 png_read_init(png_ptr
);
418 /* set up the input control */
419 png_init_io(png_ptr
, fp
);
421 /* read the file information */
422 png_read_info(png_ptr
, info_ptr
);
424 /* allocate the memory to hold the image using the fields
426 png_color_16 my_background
={ 0, 31, 127, 255, 0 };
428 if (info_ptr
->valid
& PNG_INFO_bKGD
)
430 png_set_background(png_ptr
, &(info_ptr
->background
),
431 PNG_BACKGROUND_GAMMA_FILE
, 1, 1.0);
432 if ( info_ptr
->num_palette
> 0 )
433 bgindex
= info_ptr
->background
.index
;
436 png_set_background(png_ptr
, &my_background
,
437 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
439 // Added by JACS: guesswork!
440 if ( info_ptr
->num_trans
!= 0 )
441 bgindex
= info_ptr
->num_trans
- 1 ;
444 /* tell libpng to strip 16 bit depth files down to 8 bits */
445 if (info_ptr
->bit_depth
== 16)
446 png_set_strip_16(png_ptr
);
448 int pixel_depth
=(info_ptr
->pixel_depth
<24) ? info_ptr
->pixel_depth
: 24;
449 Create(info_ptr
->width
, info_ptr
->height
, pixel_depth
,
450 info_ptr
->color_type
);
452 if (info_ptr
->num_palette
>0)
454 SetPalette((int)info_ptr
->num_palette
, (rgb_color_struct
*)info_ptr
->palette
);
457 int row_stride
= info_ptr
->width
* ((pixel_depth
+7)>>3);
458 // printf("P = %d D = %d RS= %d ", info_ptr->num_palette, info_ptr->pixel_depth,row_stride);
459 // printf("CT = %d TRS = %d BD= %d ", info_ptr->color_type, info_ptr->valid & PNG_INFO_tRNS,info_ptr->bit_depth);
461 byte
*row_pointers
= new byte
[row_stride
];
463 /* turn on interlace handling */
464 if (info_ptr
->interlace_type
)
465 number_passes
= png_set_interlace_handling(png_ptr
);
468 // printf("NP = %d ", number_passes);
470 for (int pass
=0; pass
< number_passes
; pass
++) {
474 // (unsigned char *)iter.GetRow();
475 if (info_ptr
->interlace_type
) {
477 iter
.GetRow(row_pointers
, row_stride
);
478 png_read_row(png_ptr
, row_pointers
, NULL
);
481 png_read_row(png_ptr
, row_pointers
, NULL
);
483 iter
.SetRow(row_pointers
, row_stride
);
485 } while(iter
.PrevRow());
486 // printf("Y=%d ",y);
488 delete[] row_pointers
;
490 /* read the rest of the file, getting any additional chunks
492 png_read_end(png_ptr
, info_ptr
);
494 /* clean up after the read, and free any memory allocated */
495 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
497 /* free the structures */
509 /* write a png file */
511 bool wxPNGReader::SaveFile(char * ImageFileName
)
514 strcpy(filename
, ImageFileName
);
516 wxPNGReaderIter
iter(this);
522 fp
= fopen(filename
, "wb");
526 /* allocate the necessary structures */
527 png_ptr
= new (png_struct
);
534 info_ptr
= new (png_info
);
542 /* set error handling */
543 if (setjmp(png_ptr
->jmpbuf
))
545 png_write_destroy(png_ptr
);
550 /* If we get here, we had a problem reading the file */
553 //png_set_error(ima_png_error, NULL);
555 // printf("writig pg %s ", filename);
556 /* initialize the structures */
557 png_info_init(info_ptr
);
558 png_write_init(png_ptr
);
560 int row_stride
= GetWidth() * ((GetDepth()+7)>>3);
561 /* set up the output control */
562 png_init_io(png_ptr
, fp
);
564 /* set the file information here */
565 info_ptr
->width
= GetWidth();
566 info_ptr
->height
= GetHeight();
567 info_ptr
->pixel_depth
= GetDepth();
568 info_ptr
->channels
= (GetDepth()>8) ? 3: 1;
569 info_ptr
->bit_depth
= GetDepth()/info_ptr
->channels
;
570 info_ptr
->color_type
= GetColorType();
571 info_ptr
->compression_type
= info_ptr
->filter_type
= info_ptr
->interlace_type
=0;
573 info_ptr
->rowbytes
= row_stride
;
576 // printf("P = %d D = %d RS= %d GD= %d CH= %d ", info_ptr->pixel_depth, info_ptr->bit_depth, row_stride, GetDepth(), info_ptr->channels);
577 /* set the palette if there is one */
578 if ((GetColorType() & COLORTYPE_PALETTE
) && GetPalette())
580 // printf("writing paleta[%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
581 info_ptr
->valid
|= PNG_INFO_PLTE
;
582 info_ptr
->palette
= new png_color
[256];
583 info_ptr
->num_palette
= 256;
584 for (int i
=0; i
<256; i
++)
585 GetPalette()->GetRGB(i
, &info_ptr
->palette
[i
].red
, &info_ptr
->palette
[i
].green
, &info_ptr
->palette
[i
].blue
);
587 // printf("Paleta [%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
590 /* optional significant bit chunk */
591 // info_ptr->valid |= PNG_INFO_sBIT;
592 // info_ptr->sig_bit = true_bit_depth;
594 /* optional gamma chunk */
595 // info_ptr->valid |= PNG_INFO_gAMA;
596 // info_ptr->gamma = gamma;
598 /* other optional chunks */
600 /* write the file information */
601 png_write_info(png_ptr
, info_ptr
);
603 /* set up the transformations you want. Note that these are
604 all optional. Only call them if you want them */
606 /* shift the pixels up to a legal bit depth and fill in
607 as appropriate to correctly scale the image */
608 // png_set_shift(png_ptr, &(info_ptr->sig_bit));
610 /* pack pixels into bytes */
611 // png_set_packing(png_ptr);
613 /* flip bgr pixels to rgb */
614 // png_set_bgr(png_ptr);
616 /* swap bytes of 16 bit files to most significant bit first */
617 // png_set_swap(png_ptr);
619 /* get rid of filler bytes, pack rgb into 3 bytes */
620 // png_set_rgbx(png_ptr);
622 /* If you are only writing one row at a time, this works */
624 byte
*row_pointers
= new byte
[row_stride
];
627 // (unsigned char *)iter.GetRow();
628 iter
.GetRow(row_pointers
, row_stride
);
629 png_write_row(png_ptr
, row_pointers
);
630 } while(iter
.PrevRow());
632 delete[] row_pointers
;
634 /* write the rest of the file */
635 png_write_end(png_ptr
, info_ptr
);
637 /* clean up after the write, and free any memory allocated */
638 png_write_destroy(png_ptr
);
640 /* if you malloced the palette, free it here */
641 if (info_ptr
->palette
)
642 delete[] (info_ptr
->palette
);
644 /* free the structures */
655 static int Power(int x
, int y
)
659 for ( i
= 0; i
< y
; i
++)
666 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
667 'C', 'D', 'E', 'F' };
669 static void DecToHex(int dec
, char *buf
)
671 int firstDigit
= (int)(dec
/16.0);
672 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
673 buf
[0] = hexArray
[firstDigit
];
674 buf
[1] = hexArray
[secondDigit
];
679 bool wxPNGReader::SaveXPM(char *filename
, char *name
)
683 strcpy(nameStr
, name
);
686 strcpy(nameStr
, filename
);
687 wxStripExtension(nameStr
);
690 if ( GetDepth() > 4 )
692 // Only a depth of 4 and below allowed
699 ofstream
str(filename
);
703 int noColours
= Power(2, GetDepth());
706 str
<< "/* XPM */\n";
707 str
<< "static char * " << nameStr
<< "_xpm[] = {\n";
708 str
<< "\"" << GetWidth() << " " << GetHeight() << " " << noColours
<< " 1\",\n";
711 int base
= 97 ; // start from 'a'
713 unsigned char red
, green
, blue
;
716 for ( i
= 0; i
< noColours
; i
++)
718 str
<< "\"" << (char)(base
+ i
) << " c #";
719 GetPalette()->GetRGB(i
, &red
, &green
, &blue
);
720 DecToHex(red
, hexBuf
);
722 DecToHex(green
, hexBuf
);
724 DecToHex(blue
, hexBuf
);
731 for ( y
= 0; y
< GetHeight(); y
++)
734 for ( x
= 0; x
< GetWidth(); x
++)
736 int index
= GetIndex(x
, y
);
737 str
<< (char)(base
+ index
) ;
748 #include <wx/msw/pnghand.h>
750 IMPLEMENT_DYNAMIC_CLASS(wxPNGFileHandler
, wxBitmapHandler
)
752 bool wxPNGFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, const long flags
,
753 int desiredWidth
, int desiredHeight
)
756 if (reader
.ReadFile((char*) (const char*) name
))
758 return reader
.InstantiateBitmap(bitmap
);
764 bool wxPNGFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, const int type
, const wxPalette
*pal
)