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