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