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