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