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