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"
37 #include "wx/msgdlg.h"
38 #include "wx/palette.h"
39 #include "wx/bitmap.h"
40 #include "wx/mac/pnghand.h"
41 #include "wx/mac/pngread.h"
42 #include "wx/mac/private.h"
48 extern "C" void png_read_init
PNGARG((png_structp png_ptr
));
49 extern "C" void png_write_init
PNGARG((png_structp png_ptr
));
51 extern CTabHandle
wxMacCreateColorTable( int numColors
) ;
52 extern void wxMacDestroyColorTable( CTabHandle colors
) ;
53 extern void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
) ;
54 extern GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
) ;
55 extern void wxMacDestroyGWorld( GWorldPtr gw
) ;
58 ima_png_error(png_struct
*png_ptr
, char *message
)
60 wxMessageBox(message
, "PNG error");
61 longjmp(png_ptr
->jmpbuf
, 1);
65 // static wxGifReaderIter* iter;
66 wxPalette
*wxCopyPalette(const wxPalette
*cmap
);
68 wxPNGReader::wxPNGReader(void)
71 RawImage
= NULL
; // Image data
73 Width
= 0; Height
= 0; // Dimensions
74 Depth
= 0; // (bits x pixel)
75 ColorType
= 0; // Bit 1 = Palette used
79 EfeWidth
= 0; // Efective Width
87 wxPNGReader::wxPNGReader ( char* ImageFileName
)
91 RawImage
= NULL
; // Image data
93 Width
= 0; Height
= 0; // Dimensions
94 Depth
= 0; // (bits x pixel)
95 ColorType
= 0; // Bit 1 = m_palette used
99 EfeWidth
= 0; // Efective Width
105 imageOK
= ReadFile (ImageFileName
);
109 wxPNGReader::Create(int width
, int height
, int depth
, int colortype
)
111 Width
= width
; Height
= height
; Depth
= depth
;
112 ColorType
= (colortype
>=0) ? colortype
: ((Depth
>8) ? COLORTYPE_COLOR
: 0);
120 wxMacDestroyGWorld( (GWorldPtr
) lpbi
) ;
122 lpbi
= wxMacCreateGWorld( Width
, Height
, Depth
);
125 EfeWidth
= (long)(((long)Width
*Depth
+ 31) / 32) * 4;
126 int bitwidth
= width
;
127 if ( EfeWidth
> bitwidth
)
128 bitwidth
= EfeWidth
;
130 RawImage
= (byte
*) new char[ ( bitwidth
* Height
* ((Depth
+7)>>3) ) ];
135 wxPNGReader::~wxPNGReader ( )
139 wxMacDestroyGWorld( (GWorldPtr
) lpbi
) ;
145 int wxPNGReader::GetIndex(int x
, int y
)
147 if (!Inside(x
, y
) || (Depth
>8)) return -1;
149 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
150 int index
= (int)(*ImagePointer
);
154 bool wxPNGReader::GetRGB(int x
, int y
, byte
* r
, byte
* g
, byte
* b
)
156 if (!Inside(x
, y
)) return FALSE
;
159 return m_palette
->GetRGB(GetIndex(x
, y
), r
, g
, b
);
160 /* PALETTEENTRY entry;
161 ::GetPaletteEntries((HPALETTE) m_palette->GetHPALETTE(), GetIndex(x, y), 1, &entry);
164 *b = entry.peBlue; */
166 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
167 *b
= ImagePointer
[0];
168 *g
= ImagePointer
[1];
169 *r
= ImagePointer
[2];
175 bool wxPNGReader::SetIndex(int x
, int y
, int index
)
177 if (!Inside(x
, y
) || (Depth
>8)) return FALSE
;
179 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
180 *ImagePointer
= index
;
185 bool wxPNGReader::SetRGB(int x
, int y
, byte r
, byte g
, byte b
)
187 if (!Inside(x
, y
)) return FALSE
;
189 if (ColorType
& COLORTYPE_PALETTE
)
191 if (!m_palette
) return FALSE
;
192 SetIndex(x
, y
, m_palette
->GetPixel(r
, g
, b
));
195 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
204 bool wxPNGReader::SetPalette(wxPalette
* colourmap
)
209 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
210 m_palette
= new wxPalette( *colourmap
);
212 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
216 wxPNGReader::SetPalette(int n
, byte
*r
, byte
*g
, byte
*b
)
219 m_palette
= new wxPalette();
225 m_palette
->Create(n
, r
, g
, b
);
226 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
228 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
232 wxPNGReader::SetPalette(int n
, rgb_color_struct
*rgb_struct
)
235 m_palette
= new wxPalette();
239 byte r
[256], g
[256], b
[256];
241 for(int i
=0; i
<n
; i
++)
243 r
[i
] = rgb_struct
[i
].red
;
244 g
[i
] = rgb_struct
[i
].green
;
245 b
[i
] = rgb_struct
[i
].blue
;
247 // Added by JACS copying from Andrew Davison's additions
248 // to GIF-reading code
249 // Make transparency colour black...
251 r
[bgindex
] = g
[bgindex
] = b
[bgindex
] = 0;
253 m_palette
->Create(n
, r
, g
, b
);
254 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
256 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
259 void wxPNGReader::NullData()
262 wxMacDestroyGWorld( (GWorldPtr
) lpbi
) ;
269 wxBitmap
* wxPNGReader::GetBitmap(void)
271 wxBitmap
*bitmap
= new wxBitmap
;
272 if ( InstantiateBitmap(bitmap
) )
281 bool wxPNGReader::InstantiateBitmap(wxBitmap
*bitmap
)
285 bitmap
->SetHBITMAP((WXHBITMAP
) lpbi
);
286 bitmap
->SetWidth(GetWidth());
287 bitmap
->SetHeight(GetHeight());
288 bitmap
->SetDepth(GetDepth());
289 if ( GetDepth() > 1 && m_palette
)
290 bitmap
->SetPalette(*m_palette
);
294 // Make a mask if appropriate
298 wxMask *mask = CreateMask();
299 bitmap->SetMask(mask);
302 lpbi
= NULL
; // bitmap has taken over ownership
310 HDC dc = ::CreateCompatibleDC(NULL);
314 // tmpBitmap is a dummy, to satisfy ::CreateCompatibleDC (it
315 // is a memory dc that must have a bitmap selected into it)
316 HDC dc2 = GetDC(NULL);
317 HBITMAP tmpBitmap = ::CreateCompatibleBitmap(dc2, GetWidth(), GetHeight());
318 ReleaseDC(NULL, dc2);
319 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, tmpBitmap);
323 HPALETTE oldPal = ::SelectPalette(dc, (HPALETTE) m_palette->GetHPALETTE(), FALSE);
324 ::RealizePalette(dc);
327 HBITMAP hBitmap = ::CreateDIBitmap(dc, lpbi,
328 CBM_INIT, RawImage, (LPBITMAPINFO) lpbi, DIB_PAL_COLORS);
330 ::SelectPalette(dc, NULL, TRUE);
331 ::SelectObject(dc, oldBitmap);
332 ::DeleteObject(tmpBitmap);
337 bitmap->SetHBITMAP((WXHBITMAP) hBitmap);
338 bitmap->SetWidth(GetWidth());
339 bitmap->SetHeight(GetHeight());
340 bitmap->SetDepth(GetDepth());
341 if ( GetDepth() > 1 && m_palette )
342 bitmap->SetPalette(*m_palette);
346 // Make a mask if appropriate
349 wxMask *mask = CreateMask();
350 bitmap->SetMask(mask);
367 wxPalette
*wxCopyPalette(const wxPalette
*cmap
)
369 wxPalette
*newCmap
= new wxPalette( *cmap
) ;
373 wxMask
*wxPNGReader::CreateMask(void)
376 HBITMAP hBitmap = ::CreateBitmap(GetWidth(), GetHeight(), 1, 1, NULL);
378 HDC dc = ::CreateCompatibleDC(NULL);
379 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, hBitmap);
381 int bgIndex = GetBGIndex();
385 for (x=0; x<GetWidth(); x++)
387 for (y=0; y<GetHeight(); y++)
389 int index = GetIndex(x, y);
390 if ( index == bgIndex )
391 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(0, 0, 0));
393 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(255, 255, 255));
397 ::SelectObject(dc, oldBitmap);
398 wxMask *mask = new wxMask;
399 mask->SetMaskBitmap((WXHBITMAP) hBitmap);
405 bool wxPNGReader::ReadFile(char * ImageFileName
)
410 strcpy(filename
, ImageFileName
);
415 wxPNGReaderIter
iter(this);
418 fp
= fopen( ImageFileName
, "rb" );
423 /* allocate the necessary structures */
424 png_ptr
= new (png_struct
);
431 info_ptr
= new (png_info
);
438 /* set error handling */
439 if (setjmp(png_ptr
->jmpbuf
))
441 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
446 /* If we get here, we had a problem reading the file */
449 //png_set_error(ima_png_error, NULL);
451 /* initialize the structures, info first for error handling */
452 png_info_init(info_ptr
);
453 png_read_init(png_ptr
);
455 /* set up the input control */
456 png_init_io(png_ptr
, fp
);
458 /* read the file information */
459 png_read_info(png_ptr
, info_ptr
);
461 /* allocate the memory to hold the image using the fields
463 png_color_16 my_background
={ 0, 31, 127, 255, 0 };
465 if (info_ptr
->valid
& PNG_INFO_bKGD
)
467 png_set_background(png_ptr
, &(info_ptr
->background
),
468 PNG_BACKGROUND_GAMMA_FILE
, 1, 1.0);
469 if ( info_ptr
->num_palette
> 0 )
470 bgindex
= info_ptr
->background
.index
;
473 png_set_background(png_ptr
, &my_background
,
474 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
476 // Added by JACS: guesswork!
477 if ( info_ptr
->num_trans
!= 0 )
478 bgindex
= info_ptr
->num_trans
- 1 ;
481 /* tell libpng to strip 16 bit depth files down to 8 bits */
482 if (info_ptr
->bit_depth
== 16)
483 png_set_strip_16(png_ptr
);
485 int pixel_depth
=(info_ptr
->pixel_depth
<24) ? info_ptr
->pixel_depth
: 24;
486 Create(info_ptr
->width
, info_ptr
->height
, pixel_depth
,
487 info_ptr
->color_type
);
489 if (info_ptr
->num_palette
>0)
491 SetPalette((int)info_ptr
->num_palette
, (rgb_color_struct
*)info_ptr
->palette
);
494 int row_stride
= info_ptr
->width
* ((pixel_depth
+7)>>3);
495 // printf("P = %d D = %d RS= %d ", info_ptr->num_palette, info_ptr->pixel_depth,row_stride);
496 // printf("CT = %d TRS = %d BD= %d ", info_ptr->color_type, info_ptr->valid & PNG_INFO_tRNS,info_ptr->bit_depth);
498 byte
*row_pointers
= new byte
[row_stride
];
500 /* turn on interlace handling */
501 if (info_ptr
->interlace_type
)
502 number_passes
= png_set_interlace_handling(png_ptr
);
505 // printf("NP = %d ", number_passes);
507 for (int pass
=0; pass
< number_passes
; pass
++)
512 GDHandle origDevice
;
514 GetGWorld( &origPort
, &origDevice
) ;
516 SetGWorld( (GWorldPtr
) lpbi
, NULL
) ;
519 // (unsigned char *)iter.GetRow();
520 if (info_ptr
->interlace_type
)
523 iter
.GetRow(row_pointers
, row_stride
);
524 png_read_row(png_ptr
, row_pointers
, NULL
);
527 png_read_row(png_ptr
, row_pointers
, NULL
);
529 if ( info_ptr
->palette
)
531 if ( pixel_depth
== 8 )
533 for ( int i
= 0 ; i
< info_ptr
->width
; ++i
)
535 png_color_struct
* color
;
538 int index
= row_pointers
[i
] ;
539 color
= &info_ptr
->palette
[index
] ;
540 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
541 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
542 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
543 SetCPixel( i
, y
, &col
);
546 png_color_struct* color ;
548 unsigned char* p = &row_pointers[0] ;
552 color = &info_ptr->palette[index] ;
553 col.red = (color->red << 8) | color->red ;
554 col.green = (color->green << 8) | color->green ;
555 col.blue = (color->blue << 8) | color->blue ;
556 RGBForeColor( &col ) ;
557 col.red = col.green = col.blue = 0xFFFF ;
558 RGBBackColor( &col ) ;
559 for ( int i = 0 ; i < info_ptr->width ; ++i , ++p)
565 color = &info_ptr->palette[index] ;
566 col.red = (((int)color->red) << 8) | ((int)color->red) ;
567 col.green = (((int)color->green) << 8) | ((int)color->green) ;
568 col.blue = (((int)color->blue) << 8) | ((int)color->blue) ;
569 RGBForeColor( &col ) ;
572 LineTo( info_ptr->width , y ) ;
577 for ( int i
= 0 ; i
< info_ptr
->width
; ++i
)
579 png_color_struct
* color
;
582 int byte
= ( i
* pixel_depth
) / 8 ;
583 int offset
= ( 8 - pixel_depth
) - ( i
* pixel_depth
) % 8 ;
585 int index
= ( row_pointers
[byte
] >> offset
) & ( 0xFF >> ( 8 - pixel_depth
) );
586 color
= &info_ptr
->palette
[index
] ;
587 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
588 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
589 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
590 SetCPixel( i
, y
, &col
);
596 for ( int i
= 0 ; i
< info_ptr
->width
; ++i
)
598 png_color_struct
* color
;
600 color
=(png_color_struct
*) (&row_pointers
[i
*3]) ;
601 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
602 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
603 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
604 SetCPixel( i
, y
, &col
);
608 iter
.SetRow(row_pointers
, row_stride
);
611 while(iter
.PrevRow());
612 SetGWorld( origPort
, origDevice
) ;
614 // printf("Y=%d ",y);
616 delete[] row_pointers
;
618 /* read the rest of the file, getting any additional chunks
620 png_read_end(png_ptr
, info_ptr
);
622 /* clean up after the read, and free any memory allocated */
623 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
625 /* free the structures */
637 /* write a png file */
639 bool wxPNGReader::SaveFile(char * ImageFileName
)
642 strcpy(filename
, ImageFileName
);
644 wxPNGReaderIter
iter(this);
650 fp
= fopen(filename
, "wb");
654 /* allocate the necessary structures */
655 png_ptr
= new (png_struct
);
662 info_ptr
= new (png_info
);
670 /* set error handling */
671 if (setjmp(png_ptr
->jmpbuf
))
673 png_write_destroy(png_ptr
);
678 /* If we get here, we had a problem reading the file */
681 //png_set_error(ima_png_error, NULL);
683 // printf("writig pg %s ", filename);
684 /* initialize the structures */
685 png_info_init(info_ptr
);
686 png_write_init(png_ptr
);
688 int row_stride
= GetWidth() * ((GetDepth()+7)>>3);
689 /* set up the output control */
690 png_init_io(png_ptr
, fp
);
692 /* set the file information here */
693 info_ptr
->width
= GetWidth();
694 info_ptr
->height
= GetHeight();
695 info_ptr
->pixel_depth
= GetDepth();
696 info_ptr
->channels
= (GetDepth()>8) ? 3: 1;
697 info_ptr
->bit_depth
= GetDepth()/info_ptr
->channels
;
698 info_ptr
->color_type
= GetColorType();
699 info_ptr
->compression_type
= info_ptr
->filter_type
= info_ptr
->interlace_type
=0;
701 info_ptr
->rowbytes
= row_stride
;
704 // 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);
705 /* set the palette if there is one */
706 if ((GetColorType() & COLORTYPE_PALETTE
) && GetPalette())
708 // printf("writing paleta[%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
709 info_ptr
->valid
|= PNG_INFO_PLTE
;
710 info_ptr
->palette
= new png_color
[256];
711 info_ptr
->num_palette
= 256;
712 for (int i
=0; i
<256; i
++)
713 GetPalette()->GetRGB(i
, &info_ptr
->palette
[i
].red
, &info_ptr
->palette
[i
].green
, &info_ptr
->palette
[i
].blue
);
715 // printf("Paleta [%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
718 /* optional significant bit chunk */
719 // info_ptr->valid |= PNG_INFO_sBIT;
720 // info_ptr->sig_bit = true_bit_depth;
722 /* optional gamma chunk */
723 // info_ptr->valid |= PNG_INFO_gAMA;
724 // info_ptr->gamma = gamma;
726 /* other optional chunks */
728 /* write the file information */
729 png_write_info(png_ptr
, info_ptr
);
731 /* set up the transformations you want. Note that these are
732 all optional. Only call them if you want them */
734 /* shift the pixels up to a legal bit depth and fill in
735 as appropriate to correctly scale the image */
736 // png_set_shift(png_ptr, &(info_ptr->sig_bit));
738 /* pack pixels into bytes */
739 // png_set_packing(png_ptr);
741 /* flip bgr pixels to rgb */
742 // png_set_bgr(png_ptr);
744 /* swap bytes of 16 bit files to most significant bit first */
745 // png_set_swap(png_ptr);
747 /* get rid of filler bytes, pack rgb into 3 bytes */
748 // png_set_rgbx(png_ptr);
750 /* If you are only writing one row at a time, this works */
752 byte
*row_pointers
= new byte
[row_stride
];
755 // (unsigned char *)iter.GetRow();
756 iter
.GetRow(row_pointers
, row_stride
);
757 png_write_row(png_ptr
, row_pointers
);
758 } while(iter
.PrevRow());
760 delete[] row_pointers
;
762 /* write the rest of the file */
763 png_write_end(png_ptr
, info_ptr
);
765 /* clean up after the write, and free any memory allocated */
766 png_write_destroy(png_ptr
);
768 /* if you malloced the palette, free it here */
769 if (info_ptr
->palette
)
770 delete[] (info_ptr
->palette
);
772 /* free the structures */
783 static int Power(int x
, int y
)
787 for ( i
= 0; i
< y
; i
++)
794 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
795 'C', 'D', 'E', 'F' };
797 static void DecToHex(int dec
, char *buf
)
799 int firstDigit
= (int)(dec
/16.0);
800 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
801 buf
[0] = hexArray
[firstDigit
];
802 buf
[1] = hexArray
[secondDigit
];
807 bool wxPNGReader::SaveXPM(char *filename
, char *name
)
811 strcpy(nameStr
, name
);
814 strcpy(nameStr
, filename
);
815 wxStripExtension(nameStr
);
818 if ( GetDepth() > 4 )
820 // Only a depth of 4 and below allowed
827 wxSTD ofstream
str(filename
);
831 int noColours
= Power(2, GetDepth());
834 str
<< "/* XPM */\n";
835 str
<< "static char * " << nameStr
<< "_xpm[] = {\n";
836 str
<< "\"" << GetWidth() << " " << GetHeight() << " " << noColours
<< " 1\",\n";
839 int base
= 97 ; // start from 'a'
841 unsigned char red
, green
, blue
;
844 for ( i
= 0; i
< noColours
; i
++)
846 str
<< "\"" << (char)(base
+ i
) << " c #";
847 GetPalette()->GetRGB(i
, &red
, &green
, &blue
);
848 DecToHex(red
, hexBuf
);
850 DecToHex(green
, hexBuf
);
852 DecToHex(blue
, hexBuf
);
859 for ( y
= 0; y
< GetHeight(); y
++)
862 for ( x
= 0; x
< GetWidth(); x
++)
864 int index
= GetIndex(x
, y
);
865 str
<< (char)(base
+ index
) ;
877 IMPLEMENT_DYNAMIC_CLASS(wxPNGFileHandler
, wxBitmapHandler
)
879 bool wxPNGFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
880 int desiredWidth
, int desiredHeight
)
883 if (reader
.ReadFile((char*) (const char*) name
))
885 return reader
.InstantiateBitmap(bitmap
);
891 bool wxPNGFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*pal
)