]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/pnghand.cpp
Tidied space and tabs in wxMac files
[wxWidgets.git] / src / mac / carbon / pnghand.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: pnghand.cpp
3 // Purpose: Implements a PNG reader class + handler
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 # pragma implementation "pngread.h"
14 # pragma implementation "pnghand.h"
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 # pragma hdrstop
22 #endif
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #if wxUSE_IOSTREAMH
29 # include <fstream.h>
30 #else
31 # include <fstream>
32 #endif
33
34 #ifndef __DARWIN__
35 # include <windows.h>
36 #endif
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"
43
44 extern "C" {
45 #include "png.h"
46 }
47
48 extern "C" void png_read_init PNGARG((png_structp png_ptr));
49 extern "C" void png_write_init PNGARG((png_structp png_ptr));
50
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 ) ;
56
57 void
58 ima_png_error(png_struct *png_ptr, char *message)
59 {
60 wxMessageBox(message, "PNG error");
61 longjmp(png_ptr->jmpbuf, 1);
62 }
63
64
65 // static wxGifReaderIter* iter;
66 wxPalette *wxCopyPalette(const wxPalette *cmap);
67
68 wxPNGReader::wxPNGReader(void)
69 {
70 filetype = 0;
71 RawImage = NULL; // Image data
72
73 Width = 0; Height = 0; // Dimensions
74 Depth = 0; // (bits x pixel)
75 ColorType = 0; // Bit 1 = Palette used
76 // Bit 2 = Color used
77 // Bit 3 = Alpha used
78
79 EfeWidth = 0; // Efective Width
80
81 lpbi = NULL;
82 bgindex = -1;
83 m_palette = 0;
84 imageOK = FALSE;
85 }
86
87 wxPNGReader::wxPNGReader ( char* ImageFileName )
88 {
89 imageOK = FALSE;
90 filetype = 0;
91 RawImage = NULL; // Image data
92
93 Width = 0; Height = 0; // Dimensions
94 Depth = 0; // (bits x pixel)
95 ColorType = 0; // Bit 1 = m_palette used
96 // Bit 2 = Color used
97 // Bit 3 = Alpha used
98
99 EfeWidth = 0; // Efective Width
100
101 lpbi = NULL;
102 bgindex = -1;
103 m_palette = 0;
104
105 imageOK = ReadFile (ImageFileName);
106 }
107
108 void
109 wxPNGReader::Create(int width, int height, int depth, int colortype)
110 {
111 Width = width; Height = height; Depth = depth;
112 ColorType = (colortype>=0) ? colortype: ((Depth>8) ? COLORTYPE_COLOR: 0);
113 delete m_palette;
114 m_palette = NULL;
115 delete[] RawImage;
116 RawImage = NULL;
117
118 if (lpbi) {
119 wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
120 }
121 lpbi = wxMacCreateGWorld( Width , Height , Depth);
122 if (lpbi)
123 {
124 EfeWidth = (long)(((long)Width*Depth + 31) / 32) * 4;
125 int bitwidth = width ;
126 if ( EfeWidth > bitwidth )
127 bitwidth = EfeWidth ;
128
129 RawImage = (byte*) new char[ ( bitwidth * Height * ((Depth+7)>>3) ) ];
130 imageOK = TRUE;
131 }
132 }
133
134 wxPNGReader::~wxPNGReader ( )
135 {
136 if (RawImage != NULL) {
137 delete[] RawImage ;
138 RawImage = NULL;
139 }
140 if (lpbi) {
141 wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
142 lpbi = NULL;
143 }
144 if (m_palette != NULL) {
145 delete m_palette;
146 m_palette = NULL;
147 }
148 }
149
150
151 int wxPNGReader::GetIndex(int x, int y)
152 {
153 if (!Inside(x, y) || (Depth>8)) return -1;
154
155 ImagePointerType ImagePointer = RawImage + EfeWidth*y + (x*Depth >> 3);
156 int index = (int)(*ImagePointer);
157 return index;
158 }
159
160 bool wxPNGReader::GetRGB(int x, int y, byte* r, byte* g, byte* b)
161 {
162 if (!Inside(x, y)) return FALSE;
163
164 if (m_palette) {
165 return m_palette->GetRGB(GetIndex(x, y), r, g, b);
166 /* PALETTEENTRY entry;
167 ::GetPaletteEntries((HPALETTE) m_palette->GetHPALETTE(), GetIndex(x, y), 1, &entry);
168 *r = entry.peRed;
169 *g = entry.peGreen;
170 *b = entry.peBlue; */
171 } else {
172 ImagePointerType ImagePointer = RawImage + EfeWidth*y + (x*Depth >> 3);
173 *b = ImagePointer[0];
174 *g = ImagePointer[1];
175 *r = ImagePointer[2];
176 }
177 return TRUE;
178 }
179
180
181 bool wxPNGReader::SetIndex(int x, int y, int index)
182 {
183 if (!Inside(x, y) || (Depth>8)) return FALSE;
184
185 ImagePointerType ImagePointer = RawImage + EfeWidth*y + (x*Depth >> 3);
186 *ImagePointer = index;
187
188 return TRUE;
189 }
190
191 bool wxPNGReader::SetRGB(int x, int y, byte r, byte g, byte b)
192 {
193 if (!Inside(x, y)) return FALSE;
194
195 if (ColorType & COLORTYPE_PALETTE)
196 {
197 if (!m_palette) return FALSE;
198 SetIndex(x, y, m_palette->GetPixel(r, g, b));
199
200 } else {
201 ImagePointerType ImagePointer = RawImage + EfeWidth*y + (x*Depth >> 3);
202 ImagePointer[0] = b;
203 ImagePointer[1] = g;
204 ImagePointer[2] = r;
205 }
206
207 return TRUE;
208 }
209
210 bool wxPNGReader::SetPalette(wxPalette* colourmap)
211 {
212 delete m_palette ;
213 if (!colourmap)
214 return FALSE;
215 ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR);
216 m_palette = new wxPalette( *colourmap );
217 return true ;
218 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
219 }
220
221 bool
222 wxPNGReader::SetPalette(int n, byte *r, byte *g, byte *b)
223 {
224 delete m_palette ;
225 m_palette = new wxPalette();
226 if (!m_palette)
227 return FALSE;
228
229 if (!g) g = r;
230 if (!b) b = g;
231 m_palette->Create(n, r, g, b);
232 ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR);
233 return true ;
234 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
235 }
236
237 bool
238 wxPNGReader::SetPalette(int n, rgb_color_struct *rgb_struct)
239 {
240 delete m_palette ;
241 m_palette = new wxPalette();
242 if (!m_palette)
243 return FALSE;
244
245 byte r[256], g[256], b[256];
246
247 for(int i=0; i<n; i++)
248 {
249 r[i] = rgb_struct[i].red;
250 g[i] = rgb_struct[i].green;
251 b[i] = rgb_struct[i].blue;
252 }
253 // Added by JACS copying from Andrew Davison's additions
254 // to GIF-reading code
255 // Make transparency colour black...
256 if (bgindex != -1)
257 r[bgindex] = g[bgindex] = b[bgindex] = 0;
258
259 m_palette->Create(n, r, g, b);
260 ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR);
261 return true ;
262 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
263 }
264
265 void wxPNGReader::NullData()
266 {
267 if (lpbi) {
268 wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
269 lpbi = NULL;
270 }
271 if (m_palette != NULL) {
272 delete m_palette;
273 m_palette = NULL;
274 }
275 }
276
277 wxBitmap* wxPNGReader::GetBitmap(void)
278 {
279 wxBitmap *bitmap = new wxBitmap;
280 if ( InstantiateBitmap(bitmap) )
281 return bitmap;
282 else
283 {
284 delete bitmap;
285 return NULL;
286 }
287 }
288
289 bool wxPNGReader::InstantiateBitmap(wxBitmap *bitmap)
290 {
291 if ( lpbi )
292 {
293 bitmap->SetHBITMAP((WXHBITMAP) lpbi);
294 bitmap->SetWidth(GetWidth());
295 bitmap->SetHeight(GetHeight());
296 bitmap->SetDepth(GetDepth());
297 if ( GetDepth() > 1 && m_palette )
298 bitmap->SetPalette(*m_palette);
299 bitmap->SetOk(TRUE);
300
301
302 // Make a mask if appropriate
303 /*
304 if ( bgindex > -1 )
305 {
306 wxMask *mask = CreateMask();
307 bitmap->SetMask(mask);
308 }
309 */
310 lpbi = NULL ; // bitmap has taken over ownership
311 return TRUE;
312 }
313 else
314 {
315 return FALSE;
316 }
317 /*
318 HDC dc = ::CreateCompatibleDC(NULL);
319
320 if (dc)
321 {
322 // tmpBitmap is a dummy, to satisfy ::CreateCompatibleDC (it
323 // is a memory dc that must have a bitmap selected into it)
324 HDC dc2 = GetDC(NULL);
325 HBITMAP tmpBitmap = ::CreateCompatibleBitmap(dc2, GetWidth(), GetHeight());
326 ReleaseDC(NULL, dc2);
327 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, tmpBitmap);
328
329 if ( m_palette )
330 {
331 HPALETTE oldPal = ::SelectPalette(dc, (HPALETTE) m_palette->GetHPALETTE(), FALSE);
332 ::RealizePalette(dc);
333 }
334
335 HBITMAP hBitmap = ::CreateDIBitmap(dc, lpbi,
336 CBM_INIT, RawImage, (LPBITMAPINFO) lpbi, DIB_PAL_COLORS);
337
338 ::SelectPalette(dc, NULL, TRUE);
339 ::SelectObject(dc, oldBitmap);
340 ::DeleteObject(tmpBitmap);
341 ::DeleteDC(dc);
342
343 if ( hBitmap )
344 {
345 bitmap->SetHBITMAP((WXHBITMAP) hBitmap);
346 bitmap->SetWidth(GetWidth());
347 bitmap->SetHeight(GetHeight());
348 bitmap->SetDepth(GetDepth());
349 if ( GetDepth() > 1 && m_palette )
350 bitmap->SetPalette(*m_palette);
351 bitmap->SetOk(TRUE);
352
353
354 // Make a mask if appropriate
355 if ( bgindex > -1 )
356 {
357 wxMask *mask = CreateMask();
358 bitmap->SetMask(mask);
359 }
360 return TRUE;
361 }
362 else
363 {
364 return FALSE;
365 }
366 }
367 else
368 {
369 return FALSE;
370 }
371 */
372 return false ;
373 }
374
375 wxPalette *wxCopyPalette(const wxPalette *cmap)
376 {
377 wxPalette *newCmap = new wxPalette( *cmap ) ;
378 return newCmap;
379 }
380
381 wxMask *wxPNGReader::CreateMask(void)
382 {
383 /*
384 HBITMAP hBitmap = ::CreateBitmap(GetWidth(), GetHeight(), 1, 1, NULL);
385
386 HDC dc = ::CreateCompatibleDC(NULL);
387 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, hBitmap);
388
389 int bgIndex = GetBGIndex();
390
391 int x,y;
392
393 for (x=0; x<GetWidth(); x++)
394 {
395 for (y=0; y<GetHeight(); y++)
396 {
397 int index = GetIndex(x, y);
398 if ( index == bgIndex )
399 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(0, 0, 0));
400 else
401 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(255, 255, 255));
402
403 }
404 }
405 ::SelectObject(dc, oldBitmap);
406 wxMask *mask = new wxMask;
407 mask->SetMaskBitmap((WXHBITMAP) hBitmap);
408 return mask;
409 */
410 return NULL ;
411 }
412
413 bool wxPNGReader::ReadFile(char * ImageFileName)
414 {
415 int number_passes;
416
417 if (ImageFileName)
418 strcpy(filename, ImageFileName);
419
420 FILE *fp;
421 png_struct *png_ptr;
422 png_info *info_ptr;
423 wxPNGReaderIter iter(this);
424
425 /* open the file */
426 fp = fopen( ImageFileName , "rb" );
427
428 if (!fp)
429 return FALSE;
430
431 /* allocate the necessary structures */
432 png_ptr = new (png_struct);
433 if (!png_ptr)
434 {
435 fclose(fp);
436 return FALSE;
437 }
438
439 info_ptr = new (png_info);
440 if (!info_ptr)
441 {
442 fclose(fp);
443 delete png_ptr;
444 return FALSE;
445 }
446 /* set error handling */
447 if (setjmp(png_ptr->jmpbuf))
448 {
449 png_read_destroy(png_ptr, info_ptr, (png_info *)0);
450 fclose(fp);
451 delete png_ptr;
452 delete info_ptr;
453
454 /* If we get here, we had a problem reading the file */
455 return FALSE;
456 }
457 //png_set_error(ima_png_error, NULL);
458
459 /* initialize the structures, info first for error handling */
460 png_info_init(info_ptr);
461 png_read_init(png_ptr);
462
463 /* set up the input control */
464 png_init_io(png_ptr, fp);
465
466 /* read the file information */
467 png_read_info(png_ptr, info_ptr);
468
469 /* allocate the memory to hold the image using the fields
470 of png_info. */
471 png_color_16 my_background={ 0, 31, 127, 255, 0 };
472
473 if (info_ptr->valid & PNG_INFO_bKGD)
474 {
475 png_set_background(png_ptr, &(info_ptr->background),
476 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
477 if ( info_ptr->num_palette > 0 )
478 bgindex = info_ptr->background.index;
479 }
480 else {
481 png_set_background(png_ptr, &my_background,
482 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
483
484 // Added by JACS: guesswork!
485 if ( info_ptr->num_trans != 0 )
486 bgindex = info_ptr->num_trans - 1 ;
487 }
488
489 /* tell libpng to strip 16 bit depth files down to 8 bits */
490 if (info_ptr->bit_depth == 16)
491 png_set_strip_16(png_ptr);
492
493 int pixel_depth=(info_ptr->pixel_depth<24) ? info_ptr->pixel_depth: 24;
494 Create(info_ptr->width, info_ptr->height, pixel_depth,
495 info_ptr->color_type);
496
497 if (info_ptr->num_palette>0)
498 {
499 SetPalette((int)info_ptr->num_palette, (rgb_color_struct*)info_ptr->palette);
500 }
501
502 int row_stride = info_ptr->width * ((pixel_depth+7)>>3);
503 // printf("P = %d D = %d RS= %d ", info_ptr->num_palette, info_ptr->pixel_depth,row_stride);
504 // printf("CT = %d TRS = %d BD= %d ", info_ptr->color_type, info_ptr->valid & PNG_INFO_tRNS,info_ptr->bit_depth);
505
506 byte *row_pointers = new byte[row_stride];
507
508 /* turn on interlace handling */
509 if (info_ptr->interlace_type)
510 number_passes = png_set_interlace_handling(png_ptr);
511 else
512 number_passes = 1;
513 // printf("NP = %d ", number_passes);
514
515 for (int pass=0; pass< number_passes; pass++)
516 {
517 iter.upset();
518 int y=0;
519 CGrafPtr origPort ;
520 GDHandle origDevice ;
521
522 GetGWorld( &origPort , &origDevice ) ;
523 // ignore shapedc
524 SetGWorld( (GWorldPtr) lpbi , NULL ) ;
525 do
526 {
527 // (unsigned char *)iter.GetRow();
528 if (info_ptr->interlace_type)
529 {
530 if (pass>0)
531 iter.GetRow(row_pointers, row_stride);
532 png_read_row(png_ptr, row_pointers, NULL);
533 }
534 else
535 png_read_row(png_ptr, row_pointers, NULL);
536
537 if ( info_ptr->palette )
538 {
539 if ( pixel_depth == 8 )
540 {
541 for ( size_t i = 0 ; i < info_ptr->width ; ++i )
542 {
543 png_color_struct* color ;
544 RGBColor col ;
545
546 int index = row_pointers[i] ;
547 color = &info_ptr->palette[index] ;
548 col.red = (((int)color->red) << 8) | ((int)color->red) ;
549 col.green = (((int)color->green) << 8) | ((int)color->green) ;
550 col.blue = (((int)color->blue) << 8) | ((int)color->blue) ;
551 SetCPixel( i, y, &col);
552 }
553 /*
554 png_color_struct* color ;
555 RGBColor col ;
556 unsigned char* p = &row_pointers[0] ;
557 PenNormal() ;
558 MoveTo( 0 , y ) ;
559 int index = *p ;
560 color = &info_ptr->palette[index] ;
561 col.red = (color->red << 8) | color->red ;
562 col.green = (color->green << 8) | color->green ;
563 col.blue = (color->blue << 8) | color->blue ;
564 RGBForeColor( &col ) ;
565 col.red = col.green = col.blue = 0xFFFF ;
566 RGBBackColor( &col ) ;
567 for ( int i = 0 ; i < info_ptr->width ; ++i , ++p)
568 {
569 if ( *p != index )
570 {
571 LineTo( i , y ) ;
572 index = *p ;
573 color = &info_ptr->palette[index] ;
574 col.red = (((int)color->red) << 8) | ((int)color->red) ;
575 col.green = (((int)color->green) << 8) | ((int)color->green) ;
576 col.blue = (((int)color->blue) << 8) | ((int)color->blue) ;
577 RGBForeColor( &col ) ;
578 }
579 }
580 LineTo( info_ptr->width , y ) ;
581 */
582 }
583 else
584 {
585 for ( size_t i = 0 ; i < info_ptr->width ; ++i )
586 {
587 png_color_struct* color ;
588 RGBColor col ;
589
590 int byte = ( i * pixel_depth ) / 8 ;
591 int offset = ( 8 - pixel_depth ) - ( i * pixel_depth ) % 8 ;
592
593 int index = ( row_pointers[byte] >> offset ) & ( 0xFF >> ( 8 - pixel_depth ) );
594 color = &info_ptr->palette[index] ;
595 col.red = (((int)color->red) << 8) | ((int)color->red) ;
596 col.green = (((int)color->green) << 8) | ((int)color->green) ;
597 col.blue = (((int)color->blue) << 8) | ((int)color->blue) ;
598 SetCPixel( i, y, &col);
599 }
600 }
601 }
602 else
603 {
604 for ( size_t i = 0 ; i < info_ptr->width ; ++i )
605 {
606 png_color_struct* color ;
607 RGBColor col ;
608 color =(png_color_struct*) (&row_pointers[i*3]) ;
609 col.red = (((int)color->red) << 8) | ((int)color->red) ;
610 col.green = (((int)color->green) << 8) | ((int)color->green) ;
611 col.blue = (((int)color->blue) << 8) | ((int)color->blue) ;
612 SetCPixel( i, y, &col);
613 }
614 }
615 if (number_passes)
616 iter.SetRow(row_pointers, row_stride);
617 y++;
618 }
619 while(iter.PrevRow());
620 SetGWorld( origPort , origDevice ) ;
621
622 // printf("Y=%d ",y);
623 }
624 delete[] row_pointers;
625
626 /* read the rest of the file, getting any additional chunks
627 in info_ptr */
628 png_read_end(png_ptr, info_ptr);
629
630 /* clean up after the read, and free any memory allocated */
631 png_read_destroy(png_ptr, info_ptr, (png_info *)0);
632
633 /* free the structures */
634 delete png_ptr;
635 delete info_ptr;
636
637 /* close the file */
638 fclose(fp);
639
640 /* that's it */
641 return TRUE;
642 }
643
644
645 /* write a png file */
646
647 bool wxPNGReader::SaveFile(char * ImageFileName)
648 {
649 if (ImageFileName)
650 strcpy(filename, ImageFileName);
651
652 wxPNGReaderIter iter(this);
653 FILE *fp;
654 png_struct *png_ptr;
655 png_info *info_ptr;
656
657 /* open the file */
658 fp = fopen(filename, "wb");
659 if (!fp)
660 return FALSE;
661
662 /* allocate the necessary structures */
663 png_ptr = new (png_struct);
664 if (!png_ptr)
665 {
666 fclose(fp);
667 return FALSE;
668 }
669
670 info_ptr = new (png_info);
671 if (!info_ptr)
672 {
673 fclose(fp);
674 delete png_ptr;
675 return FALSE;
676 }
677
678 /* set error handling */
679 if (setjmp(png_ptr->jmpbuf))
680 {
681 png_write_destroy(png_ptr);
682 fclose(fp);
683 delete png_ptr;
684 delete info_ptr;
685
686 /* If we get here, we had a problem reading the file */
687 return FALSE;
688 }
689 //png_set_error(ima_png_error, NULL);
690
691 // printf("writig pg %s ", filename);
692 /* initialize the structures */
693 png_info_init(info_ptr);
694 png_write_init(png_ptr);
695
696 int row_stride = GetWidth() * ((GetDepth()+7)>>3);
697 /* set up the output control */
698 png_init_io(png_ptr, fp);
699
700 /* set the file information here */
701 info_ptr->width = GetWidth();
702 info_ptr->height = GetHeight();
703 info_ptr->pixel_depth = GetDepth();
704 info_ptr->channels = (GetDepth()>8) ? 3: 1;
705 info_ptr->bit_depth = GetDepth()/info_ptr->channels;
706 info_ptr->color_type = GetColorType();
707 info_ptr->compression_type = info_ptr->filter_type = info_ptr->interlace_type=0;
708 info_ptr->valid = 0;
709 info_ptr->rowbytes = row_stride;
710
711
712 // 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);
713 /* set the palette if there is one */
714 if ((GetColorType() & COLORTYPE_PALETTE) && GetPalette())
715 {
716 // printf("writing paleta[%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
717 info_ptr->valid |= PNG_INFO_PLTE;
718 info_ptr->palette = new png_color[256];
719 info_ptr->num_palette = 256;
720 for (int i=0; i<256; i++)
721 GetPalette()->GetRGB(i, &info_ptr->palette[i].red, &info_ptr->palette[i].green, &info_ptr->palette[i].blue);
722 }
723 // printf("Paleta [%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
724
725
726 /* optional significant bit chunk */
727 // info_ptr->valid |= PNG_INFO_sBIT;
728 // info_ptr->sig_bit = true_bit_depth;
729
730 /* optional gamma chunk */
731 // info_ptr->valid |= PNG_INFO_gAMA;
732 // info_ptr->gamma = gamma;
733
734 /* other optional chunks */
735
736 /* write the file information */
737 png_write_info(png_ptr, info_ptr);
738
739 /* set up the transformations you want. Note that these are
740 all optional. Only call them if you want them */
741
742 /* shift the pixels up to a legal bit depth and fill in
743 as appropriate to correctly scale the image */
744 // png_set_shift(png_ptr, &(info_ptr->sig_bit));
745
746 /* pack pixels into bytes */
747 // png_set_packing(png_ptr);
748
749 /* flip bgr pixels to rgb */
750 // png_set_bgr(png_ptr);
751
752 /* swap bytes of 16 bit files to most significant bit first */
753 // png_set_swap(png_ptr);
754
755 /* get rid of filler bytes, pack rgb into 3 bytes */
756 // png_set_rgbx(png_ptr);
757
758 /* If you are only writing one row at a time, this works */
759
760 byte *row_pointers = new byte[row_stride];
761 iter.upset();
762 do {
763 // (unsigned char *)iter.GetRow();
764 iter.GetRow(row_pointers, row_stride);
765 png_write_row(png_ptr, row_pointers);
766 } while(iter.PrevRow());
767
768 delete[] row_pointers;
769
770 /* write the rest of the file */
771 png_write_end(png_ptr, info_ptr);
772
773 /* clean up after the write, and free any memory allocated */
774 png_write_destroy(png_ptr);
775
776 /* if you malloced the palette, free it here */
777 if (info_ptr->palette)
778 delete[] (info_ptr->palette);
779
780 /* free the structures */
781 delete png_ptr;
782 delete info_ptr;
783
784 /* close the file */
785 fclose(fp);
786
787 /* that's it */
788 return TRUE;
789 }
790
791 static int Power(int x, int y)
792 {
793 int z = 1;
794 int i;
795 for ( i = 0; i < y; i++)
796 {
797 z *= x;
798 }
799 return z;
800 }
801
802 static char hexArray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
803 'C', 'D', 'E', 'F' };
804
805 static void DecToHex(int dec, char *buf)
806 {
807 int firstDigit = (int)(dec/16.0);
808 int secondDigit = (int)(dec - (firstDigit*16.0));
809 buf[0] = hexArray[firstDigit];
810 buf[1] = hexArray[secondDigit];
811 buf[2] = 0;
812 }
813
814
815 bool wxPNGReader::SaveXPM(char *filename, char *name)
816 {
817 char nameStr[256];
818 if ( name )
819 strcpy(nameStr, name);
820 else
821 {
822 strcpy(nameStr, filename);
823 wxStripExtension(nameStr);
824 }
825
826 if ( GetDepth() > 4 )
827 {
828 // Only a depth of 4 and below allowed
829 return FALSE;
830 }
831
832 if ( !GetPalette() )
833 return FALSE;
834
835 wxSTD ofstream str(filename);
836 if ( str.bad() )
837 return FALSE;
838
839 int noColours = Power(2, GetDepth());
840
841 // Output header
842 str << "/* XPM */\n";
843 str << "static char * " << nameStr << "_xpm[] = {\n";
844 str << "\"" << GetWidth() << " " << GetHeight() << " " << noColours << " 1\",\n";
845
846 // Output colourmap
847 int base = 97 ; // start from 'a'
848
849 unsigned char red, green, blue;
850 char hexBuf[4];
851 int i;
852 for ( i = 0; i < noColours; i ++)
853 {
854 str << "\"" << (char)(base + i) << " c #";
855 GetPalette()->GetRGB(i, &red, &green, &blue);
856 DecToHex(red, hexBuf);
857 str << hexBuf;
858 DecToHex(green, hexBuf);
859 str << hexBuf;
860 DecToHex(blue, hexBuf);
861 str << hexBuf;
862 str << "\",\n";
863 }
864
865 // Output the data
866 int x, y;
867 for ( y = 0; y < GetHeight(); y++)
868 {
869 str << "\"";
870 for ( x = 0; x < GetWidth(); x++)
871 {
872 int index = GetIndex(x, y);
873 str << (char)(base + index) ;
874 }
875 str << "\",\n";
876 }
877
878 str << "};\n";
879 str.flush();
880
881 return TRUE;
882 }
883
884
885 IMPLEMENT_DYNAMIC_CLASS(wxPNGFileHandler, wxBitmapHandler)
886
887 bool wxPNGFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
888 int desiredWidth, int desiredHeight)
889 {
890 wxPNGReader reader;
891 if (reader.ReadFile((char*) (const char*) name))
892 {
893 return reader.InstantiateBitmap(bitmap);
894 }
895 else
896 return FALSE;
897 }
898
899 bool wxPNGFileHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *pal)
900 {
901 return FALSE;
902 }
903
904