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"
35 #include <wx/palette.h>
36 #include <wx/bitmap.h>
37 #include <wx/mac/pnghand.h>
38 #include <wx/mac/pngread.h>
44 extern "C" void png_read_init
PNGARG((png_structp png_ptr
));
45 extern "C" void png_write_init
PNGARG((png_structp png_ptr
));
47 extern CTabHandle
wxMacCreateColorTable( int numColors
) ;
48 extern void wxMacDestroyColorTable( CTabHandle colors
) ;
49 extern void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
) ;
50 extern GWorldPtr
wxMacCreateGWorld( int height
, int width
, int depth
) ;
51 extern void wxMacDestroyGWorld( GWorldPtr gw
) ;
54 ima_png_error(png_struct
*png_ptr
, char *message
)
56 wxMessageBox(message
, "PNG error");
57 longjmp(png_ptr
->jmpbuf
, 1);
61 // static wxGifReaderIter* iter;
62 wxPalette
*wxCopyPalette(const wxPalette
*cmap
);
64 wxPNGReader::wxPNGReader(void)
67 RawImage
= NULL
; // Image data
69 Width
= 0; Height
= 0; // Dimensions
70 Depth
= 0; // (bits x pixel)
71 ColorType
= 0; // Bit 1 = Palette used
75 EfeWidth
= 0; // Efective Width
83 wxPNGReader::wxPNGReader ( char* ImageFileName
)
87 RawImage
= NULL
; // Image data
89 Width
= 0; Height
= 0; // Dimensions
90 Depth
= 0; // (bits x pixel)
91 ColorType
= 0; // Bit 1 = Palette used
95 EfeWidth
= 0; // Efective Width
101 imageOK
= ReadFile (ImageFileName
);
105 wxPNGReader::Create(int width
, int height
, int depth
, int colortype
)
107 Width
= width
; Height
= height
; Depth
= depth
;
108 ColorType
= (colortype
>=0) ? colortype
: ((Depth
>8) ? COLORTYPE_COLOR
: 0);
112 wxMacDestroyGWorld( lpbi
) ;
117 if (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 ( )
133 wxMacDestroyGWorld( lpbi
) ;
139 int wxPNGReader::GetIndex(int x
, int y
)
141 if (!Inside(x
, y
) || (Depth
>8)) return -1;
143 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
144 int index
= (int)(*ImagePointer
);
148 bool wxPNGReader::GetRGB(int x
, int y
, byte
* r
, byte
* g
, byte
* b
)
150 if (!Inside(x
, y
)) return FALSE
;
153 return Palette
->GetRGB(GetIndex(x
, y
), r
, g
, b
);
154 /* PALETTEENTRY entry;
155 ::GetPaletteEntries((HPALETTE) Palette->GetHPALETTE(), GetIndex(x, y), 1, &entry);
158 *b = entry.peBlue; */
160 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
161 *b
= ImagePointer
[0];
162 *g
= ImagePointer
[1];
163 *r
= ImagePointer
[2];
169 bool wxPNGReader::SetIndex(int x
, int y
, int index
)
171 if (!Inside(x
, y
) || (Depth
>8)) return FALSE
;
173 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
174 *ImagePointer
= index
;
179 bool wxPNGReader::SetRGB(int x
, int y
, byte r
, byte g
, byte b
)
181 if (!Inside(x
, y
)) return FALSE
;
183 if (ColorType
& COLORTYPE_PALETTE
)
185 if (!Palette
) return FALSE
;
186 SetIndex(x
, y
, Palette
->GetPixel(r
, g
, b
));
189 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
198 bool wxPNGReader::SetPalette(wxPalette
* colourmap
)
202 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
205 // return (DibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
209 wxPNGReader::SetPalette(int n
, byte
*r
, byte
*g
, byte
*b
)
211 Palette
= new wxPalette();
217 Palette
->Create(n
, r
, g
, b
);
218 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
220 // return (DibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
224 wxPNGReader::SetPalette(int n
, rgb_color_struct
*rgb_struct
)
226 Palette
= new wxPalette();
230 byte r
[256], g
[256], b
[256];
232 for(int i
=0; i
<n
; i
++)
234 r
[i
] = rgb_struct
[i
].red
;
235 g
[i
] = rgb_struct
[i
].green
;
236 b
[i
] = rgb_struct
[i
].blue
;
238 // Added by JACS copying from Andrew Davison's additions
239 // to GIF-reading code
240 // Make transparency colour black...
242 r
[bgindex
] = g
[bgindex
] = b
[bgindex
] = 0;
244 Palette
->Create(n
, r
, g
, b
);
245 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
247 // return (DibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
250 void wxPNGReader::NullData()
256 wxBitmap
* wxPNGReader::GetBitmap(void)
258 wxBitmap
*bitmap
= new wxBitmap
;
259 if ( InstantiateBitmap(bitmap
) )
268 bool wxPNGReader::InstantiateBitmap(wxBitmap
*bitmap
)
272 bitmap
->SetHBITMAP((WXHBITMAP
) lpbi
);
273 bitmap
->SetWidth(GetWidth());
274 bitmap
->SetHeight(GetHeight());
275 bitmap
->SetDepth(GetDepth());
276 if ( GetDepth() > 1 && Palette
)
277 bitmap
->SetPalette(*Palette
);
281 // Make a mask if appropriate
285 wxMask *mask = CreateMask();
286 bitmap->SetMask(mask);
289 lpbi
= NULL
; // bitmap has taken over ownership
297 HDC dc = ::CreateCompatibleDC(NULL);
301 // tmpBitmap is a dummy, to satisfy ::CreateCompatibleDC (it
302 // is a memory dc that must have a bitmap selected into it)
303 HDC dc2 = GetDC(NULL);
304 HBITMAP tmpBitmap = ::CreateCompatibleBitmap(dc2, GetWidth(), GetHeight());
305 ReleaseDC(NULL, dc2);
306 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, tmpBitmap);
310 HPALETTE oldPal = ::SelectPalette(dc, (HPALETTE) Palette->GetHPALETTE(), FALSE);
311 ::RealizePalette(dc);
314 HBITMAP hBitmap = ::CreateDIBitmap(dc, lpbi,
315 CBM_INIT, RawImage, (LPBITMAPINFO) lpbi, DIB_PAL_COLORS);
317 ::SelectPalette(dc, NULL, TRUE);
318 ::SelectObject(dc, oldBitmap);
319 ::DeleteObject(tmpBitmap);
324 bitmap->SetHBITMAP((WXHBITMAP) hBitmap);
325 bitmap->SetWidth(GetWidth());
326 bitmap->SetHeight(GetHeight());
327 bitmap->SetDepth(GetDepth());
328 if ( GetDepth() > 1 && Palette )
329 bitmap->SetPalette(*Palette);
333 // Make a mask if appropriate
336 wxMask *mask = CreateMask();
337 bitmap->SetMask(mask);
354 wxPalette
*wxCopyPalette(const wxPalette
*cmap
)
356 wxPalette
*newCmap
= new wxPalette( *cmap
) ;
360 wxMask
*wxPNGReader::CreateMask(void)
363 HBITMAP hBitmap = ::CreateBitmap(GetWidth(), GetHeight(), 1, 1, NULL);
365 HDC dc = ::CreateCompatibleDC(NULL);
366 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, hBitmap);
368 int bgIndex = GetBGIndex();
372 for (x=0; x<GetWidth(); x++)
374 for (y=0; y<GetHeight(); y++)
376 int index = GetIndex(x, y);
377 if ( index == bgIndex )
378 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(0, 0, 0));
380 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(255, 255, 255));
384 ::SelectObject(dc, oldBitmap);
385 wxMask *mask = new wxMask;
386 mask->SetMaskBitmap((WXHBITMAP) hBitmap);
392 bool wxPNGReader::ReadFile(char * ImageFileName
)
397 strcpy(filename
, ImageFileName
);
402 wxPNGReaderIter
iter(this);
405 fp
= fopen(wxUnix2MacFilename( ImageFileName
), "rb");
409 /* allocate the necessary structures */
410 png_ptr
= new (png_struct
);
417 info_ptr
= new (png_info
);
424 /* set error handling */
425 if (setjmp(png_ptr
->jmpbuf
))
427 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
432 /* If we get here, we had a problem reading the file */
435 //png_set_error(ima_png_error, NULL);
437 /* initialize the structures, info first for error handling */
438 png_info_init(info_ptr
);
439 png_read_init(png_ptr
);
441 /* set up the input control */
442 png_init_io(png_ptr
, fp
);
444 /* read the file information */
445 png_read_info(png_ptr
, info_ptr
);
447 /* allocate the memory to hold the image using the fields
449 png_color_16 my_background
={ 0, 31, 127, 255, 0 };
451 if (info_ptr
->valid
& PNG_INFO_bKGD
)
453 png_set_background(png_ptr
, &(info_ptr
->background
),
454 PNG_BACKGROUND_GAMMA_FILE
, 1, 1.0);
455 if ( info_ptr
->num_palette
> 0 )
456 bgindex
= info_ptr
->background
.index
;
459 png_set_background(png_ptr
, &my_background
,
460 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
462 // Added by JACS: guesswork!
463 if ( info_ptr
->num_trans
!= 0 )
464 bgindex
= info_ptr
->num_trans
- 1 ;
467 /* tell libpng to strip 16 bit depth files down to 8 bits */
468 if (info_ptr
->bit_depth
== 16)
469 png_set_strip_16(png_ptr
);
471 int pixel_depth
=(info_ptr
->pixel_depth
<24) ? info_ptr
->pixel_depth
: 24;
472 Create(info_ptr
->width
, info_ptr
->height
, pixel_depth
,
473 info_ptr
->color_type
);
475 if (info_ptr
->num_palette
>0)
477 SetPalette((int)info_ptr
->num_palette
, (rgb_color_struct
*)info_ptr
->palette
);
480 int row_stride
= info_ptr
->width
* ((pixel_depth
+7)>>3);
481 // printf("P = %d D = %d RS= %d ", info_ptr->num_palette, info_ptr->pixel_depth,row_stride);
482 // printf("CT = %d TRS = %d BD= %d ", info_ptr->color_type, info_ptr->valid & PNG_INFO_tRNS,info_ptr->bit_depth);
484 byte
*row_pointers
= new byte
[row_stride
];
486 /* turn on interlace handling */
487 if (info_ptr
->interlace_type
)
488 number_passes
= png_set_interlace_handling(png_ptr
);
491 // printf("NP = %d ", number_passes);
493 for (int pass
=0; pass
< number_passes
; pass
++)
498 GDHandle origDevice
;
500 GetGWorld( &origPort
, &origDevice
) ;
502 SetGWorld( lpbi
, NULL
) ;
505 // (unsigned char *)iter.GetRow();
506 if (info_ptr
->interlace_type
)
509 iter
.GetRow(row_pointers
, row_stride
);
510 png_read_row(png_ptr
, row_pointers
, NULL
);
513 png_read_row(png_ptr
, row_pointers
, NULL
);
515 if ( info_ptr
->palette
)
517 if ( pixel_depth
== 8 )
519 png_color_struct
* color
;
521 unsigned char* p
= &row_pointers
[0] ;
523 unsigned char lastcol
= *p
;
524 color
= &info_ptr
->palette
[lastcol
] ;
525 col
.red
= (color
->red
<< 8) | color
->red
;
526 col
.green
= (color
->green
<< 8) | color
->green
;
527 col
.blue
= (color
->blue
<< 8) | color
->blue
;
528 RGBForeColor( &col
) ;
529 for ( int i
= 0 ; i
< info_ptr
->width
; ++i
, ++p
)
535 color
= &info_ptr
->palette
[lastcol
] ;
536 col
.red
= (color
->red
<< 8) | color
->red
;
537 col
.green
= (color
->green
<< 8) | color
->green
;
538 col
.blue
= (color
->blue
<< 8) | color
->blue
;
539 RGBForeColor( &col
) ;
542 LineTo( info_ptr
->width
- 1 , y
) ;
546 for ( int i
= 0 ; i
< info_ptr
->width
; ++i
)
548 png_color_struct
* color
;
551 int byte
= ( i
* pixel_depth
) / 8 ;
552 int offset
= ( 8 - pixel_depth
) - ( i
* pixel_depth
) % 8 ;
554 int index
= ( row_pointers
[byte
] >> offset
) & ( 0xFF >> ( 8 - pixel_depth
) );
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 SetCPixel( i
, y
, &col
);
565 for ( int i
= 0 ; i
< info_ptr
->width
; ++i
)
567 png_color_struct
* color
;
569 color
=(png_color_struct
*) (&row_pointers
[i
*3]) ;
570 col
.red
= (color
->red
<< 8) | color
->red
;
571 col
.green
= (color
->green
<< 8) | color
->green
;
572 col
.blue
= (color
->blue
<< 8) | color
->blue
;
573 SetCPixel( i
, y
, &col
);
577 iter
.SetRow(row_pointers
, row_stride
);
580 while(iter
.PrevRow());
581 SetGWorld( origPort
, origDevice
) ;
583 // printf("Y=%d ",y);
585 delete[] row_pointers
;
587 /* read the rest of the file, getting any additional chunks
589 png_read_end(png_ptr
, info_ptr
);
591 /* clean up after the read, and free any memory allocated */
592 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
594 /* free the structures */
606 /* write a png file */
608 bool wxPNGReader::SaveFile(char * ImageFileName
)
611 strcpy(filename
, ImageFileName
);
613 wxPNGReaderIter
iter(this);
619 fp
= fopen(filename
, "wb");
623 /* allocate the necessary structures */
624 png_ptr
= new (png_struct
);
631 info_ptr
= new (png_info
);
639 /* set error handling */
640 if (setjmp(png_ptr
->jmpbuf
))
642 png_write_destroy(png_ptr
);
647 /* If we get here, we had a problem reading the file */
650 //png_set_error(ima_png_error, NULL);
652 // printf("writig pg %s ", filename);
653 /* initialize the structures */
654 png_info_init(info_ptr
);
655 png_write_init(png_ptr
);
657 int row_stride
= GetWidth() * ((GetDepth()+7)>>3);
658 /* set up the output control */
659 png_init_io(png_ptr
, fp
);
661 /* set the file information here */
662 info_ptr
->width
= GetWidth();
663 info_ptr
->height
= GetHeight();
664 info_ptr
->pixel_depth
= GetDepth();
665 info_ptr
->channels
= (GetDepth()>8) ? 3: 1;
666 info_ptr
->bit_depth
= GetDepth()/info_ptr
->channels
;
667 info_ptr
->color_type
= GetColorType();
668 info_ptr
->compression_type
= info_ptr
->filter_type
= info_ptr
->interlace_type
=0;
670 info_ptr
->rowbytes
= row_stride
;
673 // 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);
674 /* set the palette if there is one */
675 if ((GetColorType() & COLORTYPE_PALETTE
) && GetPalette())
677 // printf("writing paleta[%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
678 info_ptr
->valid
|= PNG_INFO_PLTE
;
679 info_ptr
->palette
= new png_color
[256];
680 info_ptr
->num_palette
= 256;
681 for (int i
=0; i
<256; i
++)
682 GetPalette()->GetRGB(i
, &info_ptr
->palette
[i
].red
, &info_ptr
->palette
[i
].green
, &info_ptr
->palette
[i
].blue
);
684 // printf("Paleta [%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
687 /* optional significant bit chunk */
688 // info_ptr->valid |= PNG_INFO_sBIT;
689 // info_ptr->sig_bit = true_bit_depth;
691 /* optional gamma chunk */
692 // info_ptr->valid |= PNG_INFO_gAMA;
693 // info_ptr->gamma = gamma;
695 /* other optional chunks */
697 /* write the file information */
698 png_write_info(png_ptr
, info_ptr
);
700 /* set up the transformations you want. Note that these are
701 all optional. Only call them if you want them */
703 /* shift the pixels up to a legal bit depth and fill in
704 as appropriate to correctly scale the image */
705 // png_set_shift(png_ptr, &(info_ptr->sig_bit));
707 /* pack pixels into bytes */
708 // png_set_packing(png_ptr);
710 /* flip bgr pixels to rgb */
711 // png_set_bgr(png_ptr);
713 /* swap bytes of 16 bit files to most significant bit first */
714 // png_set_swap(png_ptr);
716 /* get rid of filler bytes, pack rgb into 3 bytes */
717 // png_set_rgbx(png_ptr);
719 /* If you are only writing one row at a time, this works */
721 byte
*row_pointers
= new byte
[row_stride
];
724 // (unsigned char *)iter.GetRow();
725 iter
.GetRow(row_pointers
, row_stride
);
726 png_write_row(png_ptr
, row_pointers
);
727 } while(iter
.PrevRow());
729 delete[] row_pointers
;
731 /* write the rest of the file */
732 png_write_end(png_ptr
, info_ptr
);
734 /* clean up after the write, and free any memory allocated */
735 png_write_destroy(png_ptr
);
737 /* if you malloced the palette, free it here */
738 if (info_ptr
->palette
)
739 delete[] (info_ptr
->palette
);
741 /* free the structures */
752 static int Power(int x
, int y
)
756 for ( i
= 0; i
< y
; i
++)
763 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
764 'C', 'D', 'E', 'F' };
766 static void DecToHex(int dec
, char *buf
)
768 int firstDigit
= (int)(dec
/16.0);
769 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
770 buf
[0] = hexArray
[firstDigit
];
771 buf
[1] = hexArray
[secondDigit
];
776 bool wxPNGReader::SaveXPM(char *filename
, char *name
)
780 strcpy(nameStr
, name
);
783 strcpy(nameStr
, filename
);
784 wxStripExtension(nameStr
);
787 if ( GetDepth() > 4 )
789 // Only a depth of 4 and below allowed
796 ofstream
str(filename
);
800 int noColours
= Power(2, GetDepth());
803 str
<< "/* XPM */\n";
804 str
<< "static char * " << nameStr
<< "_xpm[] = {\n";
805 str
<< "\"" << GetWidth() << " " << GetHeight() << " " << noColours
<< " 1\",\n";
808 int base
= 97 ; // start from 'a'
810 unsigned char red
, green
, blue
;
813 for ( i
= 0; i
< noColours
; i
++)
815 str
<< "\"" << (char)(base
+ i
) << " c #";
816 GetPalette()->GetRGB(i
, &red
, &green
, &blue
);
817 DecToHex(red
, hexBuf
);
819 DecToHex(green
, hexBuf
);
821 DecToHex(blue
, hexBuf
);
828 for ( y
= 0; y
< GetHeight(); y
++)
831 for ( x
= 0; x
< GetWidth(); x
++)
833 int index
= GetIndex(x
, y
);
834 str
<< (char)(base
+ index
) ;
846 IMPLEMENT_DYNAMIC_CLASS(wxPNGFileHandler
, wxBitmapHandler
)
848 bool wxPNGFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
849 int desiredWidth
, int desiredHeight
)
852 if (reader
.ReadFile((char*) (const char*) name
))
854 return reader
.InstantiateBitmap(bitmap
);
860 bool wxPNGFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*pal
)