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