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"
38 #include <wx/msw/pngread.h>
39 #include <wx/msw/dibutils.h>
45 extern "C" void png_read_init
PNGARG((png_structp png_ptr
));
46 extern "C" void png_write_init
PNGARG((png_structp png_ptr
));
48 #ifndef GlobalAllocPtr
49 #define GlobalPtrHandle(lp) \
50 ((HGLOBAL)GlobalHandle(lp))
52 #define GlobalLockPtr(lp) \
53 ((BOOL)GlobalLock(GlobalPtrHandle(lp)))
54 #define GlobalUnlockPtr(lp) \
55 GlobalUnlock(GlobalPtrHandle(lp))
57 #define GlobalAllocPtr(flags, cb) \
58 (GlobalLock(GlobalAlloc((flags), (cb))))
59 #define GlobalReAllocPtr(lp, cbNew, flags) \
60 (GlobalUnlockPtr(lp), GlobalLock(GlobalReAlloc(GlobalPtrHandle(lp) , (cbNew), (flags))))
61 #define GlobalFreePtr(lp) \
62 (GlobalUnlockPtr(lp), (BOOL)GlobalFree(GlobalPtrHandle(lp)))
67 ima_png_error(png_struct
*png_ptr
, char *message
)
69 // wxMessageBox(message, "PNG error");
71 longjmp(png_ptr
->jmpbuf
, 1);
75 // static wxGifReaderIter* iter;
76 wxPalette
*wxCopyPalette(const wxPalette
*cmap
);
78 wxPNGReader::wxPNGReader(void)
81 RawImage
= NULL
; // Image data
83 Width
= 0; Height
= 0; // Dimensions
84 Depth
= 0; // (bits x pixel)
85 ColorType
= 0; // Bit 1 = Palette used
89 EfeWidth
= 0; // Efective Width
97 wxPNGReader::wxPNGReader ( char* ImageFileName
)
101 RawImage
= NULL
; // Image data
103 Width
= 0; Height
= 0; // Dimensions
104 Depth
= 0; // (bits x pixel)
105 ColorType
= 0; // Bit 1 = Palette used
106 // Bit 2 = Color used
107 // Bit 3 = Alpha used
109 EfeWidth
= 0; // Efective Width
115 imageOK
= ReadFile (ImageFileName
);
119 wxPNGReader::Create(int width
, int height
, int depth
, int colortype
)
121 Width
= width
; Height
= height
; Depth
= depth
;
122 ColorType
= (colortype
>=0) ? colortype
: ((Depth
>8) ? COLORTYPE_COLOR
: 0);
130 if (lpbi
= DibCreate(Depth
, Width
, Height
)) {
131 RawImage
= (ImagePointerType
)DibPtr(lpbi
);
132 EfeWidth
= (long)(((long)Width
*Depth
+ 31) / 32) * 4;
137 wxPNGReader::~wxPNGReader ( )
146 int wxPNGReader::GetIndex(int x
, int y
)
148 if (!Inside(x
, y
) || (Depth
>8)) return -1;
150 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
151 int index
= (int)(*ImagePointer
);
155 bool wxPNGReader::GetRGB(int x
, int y
, byte
* r
, byte
* g
, byte
* b
)
157 if (!Inside(x
, y
)) return FALSE
;
160 return Palette
->GetRGB(GetIndex(x
, y
), r
, g
, b
);
161 /* PALETTEENTRY entry;
162 ::GetPaletteEntries((HPALETTE) Palette->GetHPALETTE(), GetIndex(x, y), 1, &entry);
165 *b = entry.peBlue; */
167 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
168 *b
= ImagePointer
[0];
169 *g
= ImagePointer
[1];
170 *r
= ImagePointer
[2];
176 bool wxPNGReader::SetIndex(int x
, int y
, int index
)
178 if (!Inside(x
, y
) || (Depth
>8)) return FALSE
;
180 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
181 *ImagePointer
= index
;
186 bool wxPNGReader::SetRGB(int x
, int y
, byte r
, byte g
, byte b
)
188 if (!Inside(x
, y
)) return FALSE
;
190 if (ColorType
& COLORTYPE_PALETTE
)
192 if (!Palette
) return FALSE
;
193 SetIndex(x
, y
, Palette
->GetPixel(r
, g
, b
));
196 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
205 bool wxPNGReader::SetPalette(wxPalette
* colourmap
)
209 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
211 return (DibSetUsage(lpbi
, (HPALETTE
) Palette
->GetHPALETTE(), WXIMA_COLORS
) != 0);
215 wxPNGReader::SetPalette(int n
, byte
*r
, byte
*g
, byte
*b
)
217 Palette
= new wxPalette();
223 Palette
->Create(n
, r
, g
, b
);
224 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
225 return (DibSetUsage(lpbi
, (HPALETTE
) Palette
->GetHPALETTE(), WXIMA_COLORS
) != 0);
229 wxPNGReader::SetPalette(int n
, rgb_color_struct
*rgb_struct
)
231 Palette
= new wxPalette();
235 byte r
[256], g
[256], b
[256];
237 for(int i
=0; i
<n
; i
++)
239 r
[i
] = rgb_struct
[i
].red
;
240 g
[i
] = rgb_struct
[i
].green
;
241 b
[i
] = rgb_struct
[i
].blue
;
243 // Added by JACS copying from Andrew Davison's additions
244 // to GIF-reading code
245 // Make transparency colour black...
247 r
[bgindex
] = g
[bgindex
] = b
[bgindex
] = 0;
249 Palette
->Create(n
, r
, g
, b
);
250 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
251 return (DibSetUsage(lpbi
, (HPALETTE
) Palette
->GetHPALETTE(), WXIMA_COLORS
) != 0);
254 void wxPNGReader::NullData()
260 wxBitmap
* wxPNGReader::GetBitmap(void)
262 wxBitmap
*bitmap
= new wxBitmap
;
263 if ( InstantiateBitmap(bitmap
) )
272 bool wxPNGReader::InstantiateBitmap(wxBitmap
*bitmap
)
274 HDC dc
= ::CreateCompatibleDC(NULL
);
278 // tmpBitmap is a dummy, to satisfy ::CreateCompatibleDC (it
279 // is a memory dc that must have a bitmap selected into it)
280 HDC dc2
= GetDC(NULL
);
281 HBITMAP tmpBitmap
= ::CreateCompatibleBitmap(dc2
, GetWidth(), GetHeight());
282 ReleaseDC(NULL
, dc2
);
283 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(dc
, tmpBitmap
);
287 HPALETTE oldPal
= ::SelectPalette(dc
, (HPALETTE
) Palette
->GetHPALETTE(), FALSE
);
288 ::RealizePalette(dc
);
291 HBITMAP hBitmap
= ::CreateDIBitmap(dc
, lpbi
,
292 CBM_INIT
, RawImage
, (LPBITMAPINFO
) lpbi
, DIB_PAL_COLORS
);
294 ::SelectPalette(dc
, NULL
, TRUE
);
295 ::SelectObject(dc
, oldBitmap
);
296 ::DeleteObject(tmpBitmap
);
301 bitmap
->SetHBITMAP((WXHBITMAP
) hBitmap
);
302 bitmap
->SetWidth(GetWidth());
303 bitmap
->SetHeight(GetHeight());
304 bitmap
->SetDepth(GetDepth());
305 if ( GetDepth() > 1 && Palette
)
306 bitmap
->SetPalette(*Palette
);
310 // Make a mask if appropriate
313 wxMask
*mask
= CreateMask();
314 bitmap
->SetMask(mask
);
329 wxPalette
*wxCopyPalette(const wxPalette
*cmap
)
331 // To get number of entries...
333 ::GetObject((HPALETTE
) cmap
->GetHPALETTE(), sizeof(WORD
), &count
);
335 LOGPALETTE
* logPal
= (LOGPALETTE
*)
336 new BYTE
[sizeof(LOGPALETTE
) + count
*sizeof(PALETTEENTRY
)];
337 logPal
->palVersion
= 0x300;
338 logPal
->palNumEntries
= count
;
339 ::GetPaletteEntries((HPALETTE
) cmap
->GetHPALETTE(), 0, count
, logPal
->palPalEntry
);
341 HPALETTE hPalette
= ::CreatePalette(logPal
);
344 wxPalette
*newCmap
= new wxPalette
;
345 newCmap
->SetHPALETTE((WXHPALETTE
) hPalette
);
349 wxMask
*wxPNGReader::CreateMask(void)
351 HBITMAP hBitmap
= ::CreateBitmap(GetWidth(), GetHeight(), 1, 1, NULL
);
353 HDC dc
= ::CreateCompatibleDC(NULL
);
354 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(dc
, hBitmap
);
356 int bgIndex
= GetBGIndex();
360 for (x
=0; x
<GetWidth(); x
++)
362 for (y
=0; y
<GetHeight(); y
++)
364 int index
= GetIndex(x
, y
);
365 if ( index
== bgIndex
)
366 ::SetPixel(dc
, x
, GetHeight() - y
- 1, RGB(0, 0, 0));
368 ::SetPixel(dc
, x
, GetHeight() - y
- 1, RGB(255, 255, 255));
372 ::SelectObject(dc
, oldBitmap
);
373 wxMask
*mask
= new wxMask
;
374 mask
->SetMaskBitmap((WXHBITMAP
) hBitmap
);
378 bool wxPNGReader::ReadFile(char * ImageFileName
)
383 strcpy(filename
, ImageFileName
);
388 wxPNGReaderIter
iter(this);
391 fp
= fopen(filename
, "rb");
395 /* allocate the necessary structures */
396 png_ptr
= new (png_struct
);
403 info_ptr
= new (png_info
);
410 /* set error handling */
411 if (setjmp(png_ptr
->jmpbuf
))
413 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
418 /* If we get here, we had a problem reading the file */
421 //png_set_error(ima_png_error, NULL);
423 /* initialize the structures, info first for error handling */
424 png_info_init(info_ptr
);
425 png_read_init(png_ptr
);
427 /* set up the input control */
428 png_init_io(png_ptr
, fp
);
430 /* read the file information */
431 png_read_info(png_ptr
, info_ptr
);
433 /* allocate the memory to hold the image using the fields
435 png_color_16 my_background
={ 0, 31, 127, 255, 0 };
437 if (info_ptr
->valid
& PNG_INFO_bKGD
)
439 png_set_background(png_ptr
, &(info_ptr
->background
),
440 PNG_BACKGROUND_GAMMA_FILE
, 1, 1.0);
441 if ( info_ptr
->num_palette
> 0 )
442 bgindex
= info_ptr
->background
.index
;
445 png_set_background(png_ptr
, &my_background
,
446 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
448 // Added by JACS: guesswork!
449 if ( info_ptr
->num_trans
!= 0 )
450 bgindex
= info_ptr
->num_trans
- 1 ;
453 /* tell libpng to strip 16 bit depth files down to 8 bits */
454 if (info_ptr
->bit_depth
== 16)
455 png_set_strip_16(png_ptr
);
457 int pixel_depth
=(info_ptr
->pixel_depth
<24) ? info_ptr
->pixel_depth
: 24;
458 Create(info_ptr
->width
, info_ptr
->height
, pixel_depth
,
459 info_ptr
->color_type
);
461 if (info_ptr
->num_palette
>0)
463 SetPalette((int)info_ptr
->num_palette
, (rgb_color_struct
*)info_ptr
->palette
);
466 int row_stride
= info_ptr
->width
* ((pixel_depth
+7)>>3);
467 // printf("P = %d D = %d RS= %d ", info_ptr->num_palette, info_ptr->pixel_depth,row_stride);
468 // printf("CT = %d TRS = %d BD= %d ", info_ptr->color_type, info_ptr->valid & PNG_INFO_tRNS,info_ptr->bit_depth);
470 byte
*row_pointers
= new byte
[row_stride
];
472 /* turn on interlace handling */
473 if (info_ptr
->interlace_type
)
474 number_passes
= png_set_interlace_handling(png_ptr
);
477 // printf("NP = %d ", number_passes);
479 for (int pass
=0; pass
< number_passes
; pass
++) {
483 // (unsigned char *)iter.GetRow();
484 if (info_ptr
->interlace_type
) {
486 iter
.GetRow(row_pointers
, row_stride
);
487 png_read_row(png_ptr
, row_pointers
, NULL
);
490 png_read_row(png_ptr
, row_pointers
, NULL
);
492 iter
.SetRow(row_pointers
, row_stride
);
494 } while(iter
.PrevRow());
495 // printf("Y=%d ",y);
497 delete[] row_pointers
;
499 /* read the rest of the file, getting any additional chunks
501 png_read_end(png_ptr
, info_ptr
);
503 /* clean up after the read, and free any memory allocated */
504 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
506 /* free the structures */
518 /* write a png file */
520 bool wxPNGReader::SaveFile(char * ImageFileName
)
523 strcpy(filename
, ImageFileName
);
525 wxPNGReaderIter
iter(this);
531 fp
= fopen(filename
, "wb");
535 /* allocate the necessary structures */
536 png_ptr
= new (png_struct
);
543 info_ptr
= new (png_info
);
551 /* set error handling */
552 if (setjmp(png_ptr
->jmpbuf
))
554 png_write_destroy(png_ptr
);
559 /* If we get here, we had a problem reading the file */
562 //png_set_error(ima_png_error, NULL);
564 // printf("writig pg %s ", filename);
565 /* initialize the structures */
566 png_info_init(info_ptr
);
567 png_write_init(png_ptr
);
569 int row_stride
= GetWidth() * ((GetDepth()+7)>>3);
570 /* set up the output control */
571 png_init_io(png_ptr
, fp
);
573 /* set the file information here */
574 info_ptr
->width
= GetWidth();
575 info_ptr
->height
= GetHeight();
576 info_ptr
->pixel_depth
= GetDepth();
577 info_ptr
->channels
= (GetDepth()>8) ? 3: 1;
578 info_ptr
->bit_depth
= GetDepth()/info_ptr
->channels
;
579 info_ptr
->color_type
= GetColorType();
580 info_ptr
->compression_type
= info_ptr
->filter_type
= info_ptr
->interlace_type
=0;
582 info_ptr
->rowbytes
= row_stride
;
585 // 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);
586 /* set the palette if there is one */
587 if ((GetColorType() & COLORTYPE_PALETTE
) && GetPalette())
589 // printf("writing paleta[%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
590 info_ptr
->valid
|= PNG_INFO_PLTE
;
591 info_ptr
->palette
= new png_color
[256];
592 info_ptr
->num_palette
= 256;
593 for (int i
=0; i
<256; i
++)
594 GetPalette()->GetRGB(i
, &info_ptr
->palette
[i
].red
, &info_ptr
->palette
[i
].green
, &info_ptr
->palette
[i
].blue
);
596 // printf("Paleta [%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
599 /* optional significant bit chunk */
600 // info_ptr->valid |= PNG_INFO_sBIT;
601 // info_ptr->sig_bit = true_bit_depth;
603 /* optional gamma chunk */
604 // info_ptr->valid |= PNG_INFO_gAMA;
605 // info_ptr->gamma = gamma;
607 /* other optional chunks */
609 /* write the file information */
610 png_write_info(png_ptr
, info_ptr
);
612 /* set up the transformations you want. Note that these are
613 all optional. Only call them if you want them */
615 /* shift the pixels up to a legal bit depth and fill in
616 as appropriate to correctly scale the image */
617 // png_set_shift(png_ptr, &(info_ptr->sig_bit));
619 /* pack pixels into bytes */
620 // png_set_packing(png_ptr);
622 /* flip bgr pixels to rgb */
623 // png_set_bgr(png_ptr);
625 /* swap bytes of 16 bit files to most significant bit first */
626 // png_set_swap(png_ptr);
628 /* get rid of filler bytes, pack rgb into 3 bytes */
629 // png_set_rgbx(png_ptr);
631 /* If you are only writing one row at a time, this works */
633 byte
*row_pointers
= new byte
[row_stride
];
636 // (unsigned char *)iter.GetRow();
637 iter
.GetRow(row_pointers
, row_stride
);
638 png_write_row(png_ptr
, row_pointers
);
639 } while(iter
.PrevRow());
641 delete[] row_pointers
;
643 /* write the rest of the file */
644 png_write_end(png_ptr
, info_ptr
);
646 /* clean up after the write, and free any memory allocated */
647 png_write_destroy(png_ptr
);
649 /* if you malloced the palette, free it here */
650 if (info_ptr
->palette
)
651 delete[] (info_ptr
->palette
);
653 /* free the structures */
664 static int Power(int x
, int y
)
668 for ( i
= 0; i
< y
; i
++)
675 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
676 'C', 'D', 'E', 'F' };
678 static void DecToHex(int dec
, char *buf
)
680 int firstDigit
= (int)(dec
/16.0);
681 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
682 buf
[0] = hexArray
[firstDigit
];
683 buf
[1] = hexArray
[secondDigit
];
688 bool wxPNGReader::SaveXPM(char *filename
, char *name
)
692 strcpy(nameStr
, name
);
695 strcpy(nameStr
, filename
);
696 wxStripExtension(nameStr
);
699 if ( GetDepth() > 4 )
701 // Only a depth of 4 and below allowed
708 ofstream
str(filename
);
712 int noColours
= Power(2, GetDepth());
715 str
<< "/* XPM */\n";
716 str
<< "static char * " << nameStr
<< "_xpm[] = {\n";
717 str
<< "\"" << GetWidth() << " " << GetHeight() << " " << noColours
<< " 1\",\n";
720 int base
= 97 ; // start from 'a'
722 unsigned char red
, green
, blue
;
725 for ( i
= 0; i
< noColours
; i
++)
727 str
<< "\"" << (char)(base
+ i
) << " c #";
728 GetPalette()->GetRGB(i
, &red
, &green
, &blue
);
729 DecToHex(red
, hexBuf
);
731 DecToHex(green
, hexBuf
);
733 DecToHex(blue
, hexBuf
);
740 for ( y
= 0; y
< GetHeight(); y
++)
743 for ( x
= 0; x
< GetWidth(); x
++)
745 int index
= GetIndex(x
, y
);
746 str
<< (char)(base
+ index
) ;
757 #include <wx/msw/pnghand.h>
759 IMPLEMENT_DYNAMIC_CLASS(wxPNGFileHandler
, wxBitmapHandler
)
761 bool wxPNGFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
762 int desiredWidth
, int desiredHeight
)
765 if (reader
.ReadFile((char*) (const char*) name
))
767 return reader
.InstantiateBitmap(bitmap
);
773 bool wxPNGFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*pal
)