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