]>
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 | #endif | |
33 | ||
34 | #include <windows.h> | |
35 | #include <wx/palette.h> | |
36 | #include <wx/bitmap.h> | |
37 | #include <wx/msw/pngread.h> | |
38 | #include <wx/msw/dibutils.h> | |
39 | ||
40 | extern "C" { | |
41 | #include "../png/png.h" | |
42 | } | |
43 | ||
44 | extern "C" void png_read_init PNGARG((png_structp png_ptr)); | |
45 | extern "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 | ||
65 | void | |
66 | ima_png_error(png_struct *png_ptr, char *message) | |
67 | { | |
68 | // wxMessageBox(message, "PNG error"); | |
69 | ||
70 | longjmp(png_ptr->jmpbuf, 1); | |
71 | } | |
72 | ||
73 | ||
74 | // static wxGifReaderIter* iter; | |
75 | wxPalette *wxCopyPalette(const wxPalette *cmap); | |
76 | ||
77 | wxPNGReader::wxPNGReader() | |
78 | { | |
79 | filetype = 0; | |
80 | RawImage = NULL; // Image data | |
81 | ||
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 | |
87 | ||
88 | EfeWidth = 0; // Efective Width | |
89 | ||
90 | lpbi = NULL; | |
91 | bgindex = -1; | |
92 | Palette = 0; | |
93 | imageOK = FALSE; | |
94 | } | |
95 | ||
96 | wxPNGReader::wxPNGReader ( wxChar* ImageFileName ) | |
97 | { | |
98 | imageOK = FALSE; | |
99 | filetype = 0; | |
100 | RawImage = NULL; // Image data | |
101 | ||
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 | |
107 | ||
108 | EfeWidth = 0; // Efective Width | |
109 | ||
110 | lpbi = NULL; | |
111 | bgindex = -1; | |
112 | Palette = 0; | |
113 | ||
114 | imageOK = ReadFile (ImageFileName); | |
115 | } | |
116 | ||
117 | void | |
118 | wxPNGReader::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) { | |
124 | #ifdef __WIN16__ | |
125 | GlobalFreePtr((unsigned int) lpbi); | |
126 | #else | |
127 | GlobalFreePtr(lpbi); | |
128 | #endif | |
129 | // delete Palette; | |
130 | } | |
131 | RawImage = 0; | |
132 | Palette = 0; | |
133 | lpbi = DibCreate(Depth, Width, Height); | |
134 | if (lpbi) { | |
135 | RawImage = (ImagePointerType)DibPtr(lpbi); | |
136 | EfeWidth = (long)(((long)Width*Depth + 31) / 32) * 4; | |
137 | imageOK = TRUE; | |
138 | } | |
139 | } | |
140 | ||
141 | wxPNGReader::~wxPNGReader ( ) | |
142 | { | |
143 | if (lpbi) { | |
144 | #ifdef __WIN16__ | |
145 | GlobalFreePtr((unsigned int) lpbi); | |
146 | #else | |
147 | GlobalFreePtr(lpbi); | |
148 | #endif | |
149 | delete Palette; | |
150 | } | |
151 | } | |
152 | ||
153 | ||
154 | int 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 | ||
163 | bool wxPNGReader::GetRGB(int x, int y, byte* r, byte* g, byte* b) | |
164 | { | |
165 | if (!Inside(x, y)) return FALSE; | |
166 | ||
167 | if (Palette) { | |
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; */ | |
174 | } else { | |
175 | ImagePointerType ImagePointer = RawImage + EfeWidth*y + (x*Depth >> 3); | |
176 | *b = ImagePointer[0]; | |
177 | *g = ImagePointer[1]; | |
178 | *r = ImagePointer[2]; | |
179 | } | |
180 | return TRUE; | |
181 | } | |
182 | ||
183 | ||
184 | bool 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 | ||
194 | bool 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 | { | |
200 | if (!Palette) return FALSE; | |
201 | SetIndex(x, y, Palette->GetPixel(r, g, b)); | |
202 | ||
203 | } else { | |
204 | ImagePointerType ImagePointer = RawImage + EfeWidth*y + (x*Depth >> 3); | |
205 | ImagePointer[0] = b; | |
206 | ImagePointer[1] = g; | |
207 | ImagePointer[2] = r; | |
208 | } | |
209 | ||
210 | return TRUE; | |
211 | } | |
212 | ||
213 | bool wxPNGReader::SetPalette(wxPalette* colourmap) | |
214 | { | |
215 | if (!colourmap) | |
216 | return FALSE; | |
217 | ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR); | |
218 | Palette = colourmap; | |
219 | return (DibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0); | |
220 | } | |
221 | ||
222 | bool | |
223 | wxPNGReader::SetPalette(int n, byte *r, byte *g, byte *b) | |
224 | { | |
225 | Palette = new wxPalette(); | |
226 | if (!Palette) | |
227 | return FALSE; | |
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 | ||
236 | bool | |
237 | wxPNGReader::SetPalette(int n, rgb_color_struct *rgb_struct) | |
238 | { | |
239 | Palette = new wxPalette(); | |
240 | if (!Palette) | |
241 | return FALSE; | |
242 | ||
243 | byte r[256], g[256], b[256]; | |
244 | ||
245 | for(int i=0; i<n; i++) | |
246 | { | |
247 | r[i] = rgb_struct[i].red; | |
248 | g[i] = rgb_struct[i].green; | |
249 | b[i] = rgb_struct[i].blue; | |
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 | ||
262 | void wxPNGReader::NullData() | |
263 | { | |
264 | lpbi = NULL; | |
265 | Palette = NULL; | |
266 | } | |
267 | ||
268 | wxBitmap* wxPNGReader::GetBitmap() | |
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 | ||
280 | bool wxPNGReader::InstantiateBitmap(wxBitmap *bitmap) | |
281 | { | |
282 | HDC dc = ::CreateCompatibleDC(NULL); | |
283 | ||
284 | if (dc) | |
285 | { | |
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); | |
291 | HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, tmpBitmap); | |
292 | ||
293 | if ( Palette ) | |
294 | { | |
295 | ::SelectPalette(dc, (HPALETTE) Palette->GetHPALETTE(), FALSE); | |
296 | ::RealizePalette(dc); | |
297 | } | |
298 | ||
299 | HBITMAP hBitmap = ::CreateDIBitmap(dc, lpbi, | |
300 | CBM_INIT, RawImage, (LPBITMAPINFO) lpbi, DIB_PAL_COLORS); | |
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 | } | |
324 | return TRUE; | |
325 | } | |
326 | else | |
327 | { | |
328 | return FALSE; | |
329 | } | |
330 | } | |
331 | else | |
332 | { | |
333 | return FALSE; | |
334 | } | |
335 | } | |
336 | ||
337 | wxPalette *wxCopyPalette(const wxPalette *cmap) | |
338 | { | |
339 | // To get number of entries... | |
340 | WORD count = 0; | |
341 | ::GetObject((HPALETTE) cmap->GetHPALETTE(), sizeof(WORD), &count); | |
342 | ||
343 | LOGPALETTE* logPal = (LOGPALETTE*) | |
344 | new BYTE[sizeof(LOGPALETTE) + count*sizeof(PALETTEENTRY)]; | |
345 | logPal->palVersion = 0x300; | |
346 | logPal->palNumEntries = count; | |
347 | ::GetPaletteEntries((HPALETTE) cmap->GetHPALETTE(), 0, count, logPal->palPalEntry); | |
348 | ||
349 | HPALETTE hPalette = ::CreatePalette(logPal); | |
350 | delete[] logPal; | |
351 | ||
352 | wxPalette *newCmap = new wxPalette; | |
353 | newCmap->SetHPALETTE((WXHPALETTE) hPalette); | |
354 | return newCmap; | |
355 | } | |
356 | ||
357 | wxMask *wxPNGReader::CreateMask() | |
358 | { | |
359 | HBITMAP hBitmap = ::CreateBitmap(GetWidth(), GetHeight(), 1, 1, NULL); | |
360 | ||
361 | HDC dc = ::CreateCompatibleDC(NULL); | |
362 | HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, hBitmap); | |
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 | ||
378 | } | |
379 | } | |
380 | ::SelectObject(dc, oldBitmap); | |
381 | wxMask *mask = new wxMask; | |
382 | mask->SetMaskBitmap((WXHBITMAP) hBitmap); | |
383 | return mask; | |
384 | } | |
385 | ||
386 | bool wxPNGReader::ReadFile(wxChar * ImageFileName) | |
387 | { | |
388 | int number_passes; | |
389 | ||
390 | if (ImageFileName) | |
391 | wxStrcpy(filename, ImageFileName); | |
392 | ||
393 | FILE *fp; | |
394 | png_struct *png_ptr; | |
395 | png_info *info_ptr; | |
396 | ||
397 | /* open the file */ | |
398 | fp = fopen(wxConvFile.cWX2MB(filename), "rb"); | |
399 | if (!fp) | |
400 | return FALSE; | |
401 | ||
402 | /* allocate the necessary structures */ | |
403 | png_ptr = new (png_struct); | |
404 | if (!png_ptr) | |
405 | { | |
406 | fclose(fp); | |
407 | return FALSE; | |
408 | } | |
409 | ||
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); | |
424 | ||
425 | /* If we get here, we had a problem reading the file */ | |
426 | return FALSE; | |
427 | } | |
428 | //png_set_error(ima_png_error, NULL); | |
429 | ||
430 | /* initialize the structures, info first for error handling */ | |
431 | png_info_init(info_ptr); | |
432 | png_read_init(png_ptr); | |
433 | ||
434 | /* set up the input control */ | |
435 | png_init_io(png_ptr, fp); | |
436 | ||
437 | /* read the file information */ | |
438 | png_read_info(png_ptr, info_ptr); | |
439 | ||
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 }; | |
443 | ||
444 | if (info_ptr->valid & PNG_INFO_bKGD) | |
445 | { | |
446 | png_set_background(png_ptr, &(info_ptr->background), | |
447 | PNG_BACKGROUND_GAMMA_FILE, 1, 1.0); | |
448 | if ( info_ptr->num_palette > 0 ) | |
449 | bgindex = info_ptr->background.index; | |
450 | } | |
451 | else { | |
452 | png_set_background(png_ptr, &my_background, | |
453 | PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); | |
454 | ||
455 | // Added by JACS: guesswork! | |
456 | if ( info_ptr->num_trans != 0 ) | |
457 | bgindex = info_ptr->num_trans - 1 ; | |
458 | } | |
459 | ||
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); | |
463 | ||
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); | |
467 | ||
468 | if (info_ptr->num_palette>0) | |
469 | { | |
470 | SetPalette((int)info_ptr->num_palette, (rgb_color_struct*)info_ptr->palette); | |
471 | } | |
472 | ||
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); | |
476 | ||
477 | byte *row_pointers = new byte[row_stride]; | |
478 | ||
479 | /* turn on interlace handling */ | |
480 | if (info_ptr->interlace_type) | |
481 | number_passes = png_set_interlace_handling(png_ptr); | |
482 | else | |
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); | |
503 | ||
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; | |
512 | ||
513 | /* read the rest of the file, getting any additional chunks | |
514 | in info_ptr */ | |
515 | png_read_end(png_ptr, info_ptr); | |
516 | ||
517 | /* clean up after the read, and free any memory allocated */ | |
518 | png_read_destroy(png_ptr, info_ptr, (png_info *)0); | |
519 | ||
520 | /* free the structures */ | |
521 | delete(png_ptr); | |
522 | delete(info_ptr); | |
523 | ||
524 | /* close the file */ | |
525 | fclose(fp); | |
526 | ||
527 | /* that's it */ | |
528 | return TRUE; | |
529 | } | |
530 | ||
531 | ||
532 | /* write a png file */ | |
533 | ||
534 | bool wxPNGReader::SaveFile(wxChar * ImageFileName) | |
535 | { | |
536 | if (ImageFileName) | |
537 | wxStrcpy(filename, ImageFileName); | |
538 | ||
539 | wxPNGReaderIter iter(this); | |
540 | FILE *fp; | |
541 | png_struct *png_ptr; | |
542 | png_info *info_ptr; | |
543 | ||
544 | /* open the file */ | |
545 | fp = fopen(wxConvFile.cWX2MB(filename), "wb"); | |
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 | } | |
576 | //png_set_error(ima_png_error, NULL); | |
577 | ||
578 | // printf("writig pg %s ", filename); | |
579 | /* initialize the structures */ | |
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 */ | |
585 | png_init_io(png_ptr, fp); | |
586 | ||
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; | |
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); | |
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()); | |
611 | ||
612 | ||
613 | /* optional significant bit chunk */ | |
614 | // info_ptr->valid |= PNG_INFO_sBIT; | |
615 | // info_ptr->sig_bit = true_bit_depth; | |
616 | ||
617 | /* optional gamma chunk */ | |
618 | // info_ptr->valid |= PNG_INFO_gAMA; | |
619 | // info_ptr->gamma = gamma; | |
620 | ||
621 | /* other optional chunks */ | |
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 | ||
629 | /* shift the pixels up to a legal bit depth and fill in | |
630 | as appropriate to correctly scale the image */ | |
631 | // png_set_shift(png_ptr, &(info_ptr->sig_bit)); | |
632 | ||
633 | /* pack pixels into bytes */ | |
634 | // png_set_packing(png_ptr); | |
635 | ||
636 | /* flip bgr pixels to rgb */ | |
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 | ||
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 | ||
655 | delete[] row_pointers; | |
656 | ||
657 | /* write the rest of the file */ | |
658 | png_write_end(png_ptr, info_ptr); | |
659 | ||
660 | /* clean up after the write, and free any memory allocated */ | |
661 | png_write_destroy(png_ptr); | |
662 | ||
663 | /* if you malloced the palette, free it here */ | |
664 | if (info_ptr->palette) | |
665 | delete[] (info_ptr->palette); | |
666 | ||
667 | /* free the structures */ | |
668 | delete(png_ptr); | |
669 | delete(info_ptr); | |
670 | ||
671 | /* close the file */ | |
672 | fclose(fp); | |
673 | ||
674 | /* that's it */ | |
675 | return TRUE; | |
676 | } | |
677 | ||
678 | static 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 | ||
689 | static char hexArray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', | |
690 | 'C', 'D', 'E', 'F' }; | |
691 | ||
692 | static 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 | } | |
700 | ||
701 | ||
702 | bool wxPNGReader::SaveXPM(wxChar *filename, wxChar *name) | |
703 | { | |
704 | wxChar nameStr[256]; | |
705 | if ( name ) | |
706 | wxStrcpy(nameStr, name); | |
707 | else | |
708 | { | |
709 | wxStrcpy(nameStr, filename); | |
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 | ||
722 | ofstream str(wxConvFile.cWX2MB(filename)); | |
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 | ||
773 | IMPLEMENT_DYNAMIC_CLASS(wxPNGFileHandler, wxBitmapHandler) | |
774 | ||
775 | bool wxPNGFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
776 | int desiredWidth, int desiredHeight) | |
777 | { | |
778 | wxPNGReader reader; | |
779 | if (reader.ReadFile(WXSTRINGCAST name)) | |
780 | { | |
781 | return reader.InstantiateBitmap(bitmap); | |
782 | } | |
783 | else | |
784 | return FALSE; | |
785 | } | |
786 | ||
787 | bool wxPNGFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *pal) | |
788 | { | |
789 | return FALSE; | |
790 | } | |
791 | ||
792 |