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