1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implements a PNG reader class + handler
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
32 #include "wx/msgdlg.h"
33 #include "wx/palette.h"
34 #include "wx/bitmap.h"
35 #include "wx/mac/pnghand.h"
36 #include "wx/mac/pngread.h"
37 #include "wx/mac/private.h"
43 extern "C" void png_read_init
PNGARG((png_structp png_ptr
));
44 extern "C" void png_write_init
PNGARG((png_structp png_ptr
));
46 extern CTabHandle
wxMacCreateColorTable( int numColors
) ;
47 extern void wxMacDestroyColorTable( CTabHandle colors
) ;
48 extern void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
) ;
49 extern GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
) ;
50 extern void wxMacDestroyGWorld( GWorldPtr gw
) ;
53 ima_png_error(png_struct
*png_ptr
, char *message
)
55 wxMessageBox(wxString::FromAscii(message
), wxT("PNG error"));
56 longjmp(png_ptr
->jmpbuf
, 1);
60 // static wxGifReaderIter* iter;
61 wxPalette
*wxCopyPalette(const wxPalette
*cmap
);
63 wxPNGReader::wxPNGReader(void)
66 RawImage
= NULL
; // Image data
68 Width
= 0; Height
= 0; // Dimensions
69 Depth
= 0; // (bits x pixel)
70 ColorType
= 0; // Bit 1 = Palette used
74 EfeWidth
= 0; // Efective Width
82 wxPNGReader::wxPNGReader ( char* ImageFileName
)
86 RawImage
= NULL
; // Image data
88 Width
= 0; Height
= 0; // Dimensions
89 Depth
= 0; // (bits x pixel)
90 ColorType
= 0; // Bit 1 = m_palette used
94 EfeWidth
= 0; // Efective Width
100 imageOK
= ReadFile (ImageFileName
);
104 wxPNGReader::Create(int width
, int height
, int depth
, int colortype
)
106 Width
= width
; Height
= height
; Depth
= depth
;
107 ColorType
= (colortype
>=0) ? colortype
: ((Depth
>8) ? COLORTYPE_COLOR
: 0);
114 wxMacDestroyGWorld( (GWorldPtr
) lpbi
) ;
116 lpbi
= wxMacCreateGWorld( Width
, Height
, Depth
);
119 EfeWidth
= (long)(((long)Width
*Depth
+ 31) / 32) * 4;
120 int bitwidth
= width
;
121 if ( EfeWidth
> bitwidth
)
122 bitwidth
= EfeWidth
;
124 RawImage
= (byte
*) new char[ ( bitwidth
* Height
* ((Depth
+7)>>3) ) ];
129 wxPNGReader::~wxPNGReader ( )
131 if (RawImage
!= NULL
) {
136 wxMacDestroyGWorld( (GWorldPtr
) lpbi
) ;
139 if (m_palette
!= NULL
) {
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 m_palette
->GetRGB(GetIndex(x
, y
), r
, g
, b
);
161 /* PALETTEENTRY entry;
162 ::GetPaletteEntries((HPALETTE) m_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 (!m_palette
) return FALSE
;
193 SetIndex(x
, y
, m_palette
->GetPixel(r
, g
, b
));
196 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
205 bool wxPNGReader::SetPalette(wxPalette
* colourmap
)
210 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
211 m_palette
= new wxPalette( *colourmap
);
213 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
217 wxPNGReader::SetPalette(int n
, byte
*r
, byte
*g
, byte
*b
)
220 m_palette
= new wxPalette();
226 m_palette
->Create(n
, r
, g
, b
);
227 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
229 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
233 wxPNGReader::SetPalette(int n
, rgb_color_struct
*rgb_struct
)
236 m_palette
= new wxPalette();
240 byte r
[256], g
[256], b
[256];
242 for(int i
=0; i
<n
; i
++)
244 r
[i
] = rgb_struct
[i
].red
;
245 g
[i
] = rgb_struct
[i
].green
;
246 b
[i
] = rgb_struct
[i
].blue
;
248 // Added by JACS copying from Andrew Davison's additions
249 // to GIF-reading code
250 // Make transparency colour black...
252 r
[bgindex
] = g
[bgindex
] = b
[bgindex
] = 0;
254 m_palette
->Create(n
, r
, g
, b
);
255 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
257 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
260 void wxPNGReader::NullData()
263 wxMacDestroyGWorld( (GWorldPtr
) lpbi
) ;
266 if (m_palette
!= NULL
) {
272 wxBitmap
* wxPNGReader::GetBitmap(void)
274 wxBitmap
*bitmap
= new wxBitmap
;
275 if ( InstantiateBitmap(bitmap
) )
284 bool wxPNGReader::InstantiateBitmap(wxBitmap
*bitmap
)
288 bitmap
->SetHBITMAP((WXHBITMAP
) lpbi
);
289 bitmap
->SetWidth(GetWidth());
290 bitmap
->SetHeight(GetHeight());
291 bitmap
->SetDepth(GetDepth());
292 if ( GetDepth() > 1 && m_palette
)
293 bitmap
->SetPalette(*m_palette
);
297 // Make a mask if appropriate
301 wxMask *mask = CreateMask();
302 bitmap->SetMask(mask);
305 lpbi
= NULL
; // bitmap has taken over ownership
313 HDC dc = ::CreateCompatibleDC(NULL);
317 // tmpBitmap is a dummy, to satisfy ::CreateCompatibleDC (it
318 // is a memory dc that must have a bitmap selected into it)
319 HDC dc2 = GetDC(NULL);
320 HBITMAP tmpBitmap = ::CreateCompatibleBitmap(dc2, GetWidth(), GetHeight());
321 ReleaseDC(NULL, dc2);
322 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, tmpBitmap);
326 HPALETTE oldPal = ::SelectPalette(dc, (HPALETTE) m_palette->GetHPALETTE(), FALSE);
327 ::RealizePalette(dc);
330 HBITMAP hBitmap = ::CreateDIBitmap(dc, lpbi,
331 CBM_INIT, RawImage, (LPBITMAPINFO) lpbi, DIB_PAL_COLORS);
333 ::SelectPalette(dc, NULL, TRUE);
334 ::SelectObject(dc, oldBitmap);
335 ::DeleteObject(tmpBitmap);
340 bitmap->SetHBITMAP((WXHBITMAP) hBitmap);
341 bitmap->SetWidth(GetWidth());
342 bitmap->SetHeight(GetHeight());
343 bitmap->SetDepth(GetDepth());
344 if ( GetDepth() > 1 && m_palette )
345 bitmap->SetPalette(*m_palette);
349 // Make a mask if appropriate
352 wxMask *mask = CreateMask();
353 bitmap->SetMask(mask);
370 wxPalette
*wxCopyPalette(const wxPalette
*cmap
)
372 wxPalette
*newCmap
= new wxPalette( *cmap
) ;
376 wxMask
*wxPNGReader::CreateMask(void)
379 HBITMAP hBitmap = ::CreateBitmap(GetWidth(), GetHeight(), 1, 1, NULL);
381 HDC dc = ::CreateCompatibleDC(NULL);
382 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, hBitmap);
384 int bgIndex = GetBGIndex();
388 for (x=0; x<GetWidth(); x++)
390 for (y=0; y<GetHeight(); y++)
392 int index = GetIndex(x, y);
393 if ( index == bgIndex )
394 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(0, 0, 0));
396 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(255, 255, 255));
400 ::SelectObject(dc, oldBitmap);
401 wxMask *mask = new wxMask;
402 mask->SetMaskBitmap((WXHBITMAP) hBitmap);
408 bool wxPNGReader::ReadFile(char * ImageFileName
)
413 strcpy(filename
, ImageFileName
);
418 wxPNGReaderIter
iter(this);
421 fp
= fopen( ImageFileName
, "rb" );
426 /* allocate the necessary structures */
427 png_ptr
= new (png_struct
);
434 info_ptr
= new (png_info
);
441 /* set error handling */
442 if (setjmp(png_ptr
->jmpbuf
))
444 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
449 /* If we get here, we had a problem reading the file */
452 //png_set_error(ima_png_error, NULL);
454 /* initialize the structures, info first for error handling */
455 png_info_init(info_ptr
);
456 png_read_init(png_ptr
);
458 /* set up the input control */
459 png_init_io(png_ptr
, fp
);
461 /* read the file information */
462 png_read_info(png_ptr
, info_ptr
);
464 /* allocate the memory to hold the image using the fields
466 png_color_16 my_background
={ 0, 31, 127, 255, 0 };
468 if (info_ptr
->valid
& PNG_INFO_bKGD
)
470 png_set_background(png_ptr
, &(info_ptr
->background
),
471 PNG_BACKGROUND_GAMMA_FILE
, 1, 1.0);
472 if ( info_ptr
->num_palette
> 0 )
473 bgindex
= info_ptr
->background
.index
;
476 png_set_background(png_ptr
, &my_background
,
477 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
479 // Added by JACS: guesswork!
480 if ( info_ptr
->num_trans
!= 0 )
481 bgindex
= info_ptr
->num_trans
- 1 ;
484 /* tell libpng to strip 16 bit depth files down to 8 bits */
485 if (info_ptr
->bit_depth
== 16)
486 png_set_strip_16(png_ptr
);
488 int pixel_depth
=(info_ptr
->pixel_depth
<24) ? info_ptr
->pixel_depth
: 24;
489 Create(info_ptr
->width
, info_ptr
->height
, pixel_depth
,
490 info_ptr
->color_type
);
492 if (info_ptr
->num_palette
>0)
494 SetPalette((int)info_ptr
->num_palette
, (rgb_color_struct
*)info_ptr
->palette
);
497 int row_stride
= info_ptr
->width
* ((pixel_depth
+7)>>3);
498 // printf("P = %d D = %d RS= %d ", info_ptr->num_palette, info_ptr->pixel_depth,row_stride);
499 // printf("CT = %d TRS = %d BD= %d ", info_ptr->color_type, info_ptr->valid & PNG_INFO_tRNS,info_ptr->bit_depth);
501 byte
*row_pointers
= new byte
[row_stride
];
503 /* turn on interlace handling */
504 if (info_ptr
->interlace_type
)
505 number_passes
= png_set_interlace_handling(png_ptr
);
508 // printf("NP = %d ", number_passes);
510 for (int pass
=0; pass
< number_passes
; pass
++)
515 GDHandle origDevice
;
517 GetGWorld( &origPort
, &origDevice
) ;
519 SetGWorld( (GWorldPtr
) lpbi
, NULL
) ;
522 // (unsigned char *)iter.GetRow();
523 if (info_ptr
->interlace_type
)
526 iter
.GetRow(row_pointers
, row_stride
);
527 png_read_row(png_ptr
, row_pointers
, NULL
);
530 png_read_row(png_ptr
, row_pointers
, NULL
);
532 if ( info_ptr
->palette
)
534 if ( pixel_depth
== 8 )
536 for ( size_t i
= 0 ; i
< info_ptr
->width
; ++i
)
538 png_color_struct
* color
;
541 int index
= row_pointers
[i
] ;
542 color
= &info_ptr
->palette
[index
] ;
543 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
544 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
545 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
546 SetCPixel( i
, y
, &col
);
549 png_color_struct* color ;
551 unsigned char* p = &row_pointers[0] ;
555 color = &info_ptr->palette[index] ;
556 col.red = (color->red << 8) | color->red ;
557 col.green = (color->green << 8) | color->green ;
558 col.blue = (color->blue << 8) | color->blue ;
559 RGBForeColor( &col ) ;
560 col.red = col.green = col.blue = 0xFFFF ;
561 RGBBackColor( &col ) ;
562 for ( int i = 0 ; i < info_ptr->width ; ++i , ++p)
568 color = &info_ptr->palette[index] ;
569 col.red = (((int)color->red) << 8) | ((int)color->red) ;
570 col.green = (((int)color->green) << 8) | ((int)color->green) ;
571 col.blue = (((int)color->blue) << 8) | ((int)color->blue) ;
572 RGBForeColor( &col ) ;
575 LineTo( info_ptr->width , y ) ;
580 for ( size_t i
= 0 ; i
< info_ptr
->width
; ++i
)
582 png_color_struct
* color
;
585 int byte
= ( i
* pixel_depth
) / 8 ;
586 int offset
= ( 8 - pixel_depth
) - ( i
* pixel_depth
) % 8 ;
588 int index
= ( row_pointers
[byte
] >> offset
) & ( 0xFF >> ( 8 - pixel_depth
) );
589 color
= &info_ptr
->palette
[index
] ;
590 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
591 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
592 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
593 SetCPixel( i
, y
, &col
);
599 for ( size_t i
= 0 ; i
< info_ptr
->width
; ++i
)
601 png_color_struct
* color
;
603 color
=(png_color_struct
*) (&row_pointers
[i
*3]) ;
604 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
605 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
606 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
607 SetCPixel( i
, y
, &col
);
611 iter
.SetRow(row_pointers
, row_stride
);
614 while(iter
.PrevRow());
615 SetGWorld( origPort
, origDevice
) ;
617 // printf("Y=%d ",y);
619 delete[] row_pointers
;
621 /* read the rest of the file, getting any additional chunks
623 png_read_end(png_ptr
, info_ptr
);
625 /* clean up after the read, and free any memory allocated */
626 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
628 /* free the structures */
640 /* write a png file */
642 bool wxPNGReader::SaveFile(char * ImageFileName
)
645 strcpy(filename
, ImageFileName
);
647 wxPNGReaderIter
iter(this);
653 fp
= fopen(filename
, "wb");
657 /* allocate the necessary structures */
658 png_ptr
= new (png_struct
);
665 info_ptr
= new (png_info
);
673 /* set error handling */
674 if (setjmp(png_ptr
->jmpbuf
))
676 png_write_destroy(png_ptr
);
681 /* If we get here, we had a problem reading the file */
684 //png_set_error(ima_png_error, NULL);
686 // printf("writig pg %s ", filename);
687 /* initialize the structures */
688 png_info_init(info_ptr
);
689 png_write_init(png_ptr
);
691 int row_stride
= GetWidth() * ((GetDepth()+7)>>3);
692 /* set up the output control */
693 png_init_io(png_ptr
, fp
);
695 /* set the file information here */
696 info_ptr
->width
= GetWidth();
697 info_ptr
->height
= GetHeight();
698 info_ptr
->pixel_depth
= GetDepth();
699 info_ptr
->channels
= (GetDepth()>8) ? 3: 1;
700 info_ptr
->bit_depth
= GetDepth()/info_ptr
->channels
;
701 info_ptr
->color_type
= GetColorType();
702 info_ptr
->compression_type
= info_ptr
->filter_type
= info_ptr
->interlace_type
=0;
704 info_ptr
->rowbytes
= row_stride
;
707 // 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);
708 /* set the palette if there is one */
709 if ((GetColorType() & COLORTYPE_PALETTE
) && GetPalette())
711 // printf("writing paleta[%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
712 info_ptr
->valid
|= PNG_INFO_PLTE
;
713 info_ptr
->palette
= new png_color
[256];
714 info_ptr
->num_palette
= 256;
715 for (int i
=0; i
<256; i
++)
716 GetPalette()->GetRGB(i
, &info_ptr
->palette
[i
].red
, &info_ptr
->palette
[i
].green
, &info_ptr
->palette
[i
].blue
);
718 // printf("Paleta [%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
721 /* optional significant bit chunk */
722 // info_ptr->valid |= PNG_INFO_sBIT;
723 // info_ptr->sig_bit = true_bit_depth;
725 /* optional gamma chunk */
726 // info_ptr->valid |= PNG_INFO_gAMA;
727 // info_ptr->gamma = gamma;
729 /* other optional chunks */
731 /* write the file information */
732 png_write_info(png_ptr
, info_ptr
);
734 /* set up the transformations you want. Note that these are
735 all optional. Only call them if you want them */
737 /* shift the pixels up to a legal bit depth and fill in
738 as appropriate to correctly scale the image */
739 // png_set_shift(png_ptr, &(info_ptr->sig_bit));
741 /* pack pixels into bytes */
742 // png_set_packing(png_ptr);
744 /* flip bgr pixels to rgb */
745 // png_set_bgr(png_ptr);
747 /* swap bytes of 16 bit files to most significant bit first */
748 // png_set_swap(png_ptr);
750 /* get rid of filler bytes, pack rgb into 3 bytes */
751 // png_set_rgbx(png_ptr);
753 /* If you are only writing one row at a time, this works */
755 byte
*row_pointers
= new byte
[row_stride
];
758 // (unsigned char *)iter.GetRow();
759 iter
.GetRow(row_pointers
, row_stride
);
760 png_write_row(png_ptr
, row_pointers
);
761 } while(iter
.PrevRow());
763 delete[] row_pointers
;
765 /* write the rest of the file */
766 png_write_end(png_ptr
, info_ptr
);
768 /* clean up after the write, and free any memory allocated */
769 png_write_destroy(png_ptr
);
771 /* if you malloced the palette, free it here */
772 if (info_ptr
->palette
)
773 delete[] (info_ptr
->palette
);
775 /* free the structures */
786 static int Power(int x
, int y
)
790 for ( i
= 0; i
< y
; i
++)
797 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
798 'C', 'D', 'E', 'F' };
800 static void DecToHex(int dec
, char *buf
)
802 int firstDigit
= (int)(dec
/16.0);
803 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
804 buf
[0] = hexArray
[firstDigit
];
805 buf
[1] = hexArray
[secondDigit
];
810 bool wxPNGReader::SaveXPM(char *filename
, char *name
)
814 strcpy(nameStr
, name
);
817 wxString str
= wxString::FromAscii(filename
) ;
818 wxStripExtension( str
) ;
819 strcpy(nameStr
, str
.ToAscii() );
822 if ( GetDepth() > 4 )
824 // Only a depth of 4 and below allowed
831 wxSTD ofstream
str(filename
);
835 int noColours
= Power(2, GetDepth());
838 str
<< "/* XPM */\n";
839 str
<< "static char * " << nameStr
<< "_xpm[] = {\n";
840 str
<< "\"" << GetWidth() << " " << GetHeight() << " " << noColours
<< " 1\",\n";
843 int base
= 97 ; // start from 'a'
845 unsigned char red
, green
, blue
;
848 for ( i
= 0; i
< noColours
; i
++)
850 str
<< "\"" << (char)(base
+ i
) << " c #";
851 GetPalette()->GetRGB(i
, &red
, &green
, &blue
);
852 DecToHex(red
, hexBuf
);
854 DecToHex(green
, hexBuf
);
856 DecToHex(blue
, hexBuf
);
863 for ( y
= 0; y
< GetHeight(); y
++)
866 for ( x
= 0; x
< GetWidth(); x
++)
868 int index
= GetIndex(x
, y
);
869 str
<< (char)(base
+ index
) ;
881 IMPLEMENT_DYNAMIC_CLASS(wxPNGFileHandler
, wxBitmapHandler
)
883 bool wxPNGFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
884 int desiredWidth
, int desiredHeight
)
887 if (reader
.ReadFile( (char*)(const char*) name
.ToAscii() ) )
889 return reader
.InstantiateBitmap(bitmap
);
895 bool wxPNGFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*pal
)