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