]> git.saurik.com Git - wxWidgets.git/blame - src/common/image.cpp
minor samples fixes
[wxWidgets.git] / src / common / image.cpp
CommitLineData
01111366
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: image.cpp
3// Purpose: wxImage
4// Author: Robert Roebling
5// RCS-ID: $Id$
6// Copyright: (c) Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "image.h"
12#endif
13
0b4f9ee3
UU
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18#pragma hdrstop
19#endif
20
01111366 21#include "wx/image.h"
99c67c77 22#include "wx/bitmap.h"
01111366
RR
23#include "wx/debug.h"
24#include "wx/log.h"
f6fcbb63 25#include "wx/app.h"
ac57418f 26#ifdef wxUSE_LIBPNG
01111366 27#include "../png/png.h"
ac57418f 28#endif
dc86cb34 29#include "wx/filefn.h"
3d05544e 30#include "wx/wfstream.h"
01111366 31
2432b92d
JS
32#ifdef __WXMSW__
33#include <windows.h>
34#endif
35
01111366
RR
36//-----------------------------------------------------------------------------
37// wxImage
38//-----------------------------------------------------------------------------
39
40class wxImageRefData: public wxObjectRefData
41{
fd0eed64
RR
42
43public:
44 wxImageRefData(void);
45 ~wxImageRefData(void);
4698648f 46
fd0eed64
RR
47 int m_width;
48 int m_height;
49 unsigned char *m_data;
50 bool m_hasMask;
51 unsigned char m_maskRed,m_maskGreen,m_maskBlue;
52 bool m_ok;
01111366
RR
53};
54
55wxImageRefData::wxImageRefData(void)
56{
fd0eed64
RR
57 m_width = 0;
58 m_height = 0;
59 m_data = (unsigned char*) NULL;
60 m_ok = FALSE;
61 m_maskRed = 0;
62 m_maskGreen = 0;
63 m_maskBlue = 0;
64 m_hasMask = FALSE;
01111366
RR
65}
66
67wxImageRefData::~wxImageRefData(void)
68{
fd0eed64 69 if (m_data) free( m_data );
01111366
RR
70}
71
72wxList wxImage::sm_handlers;
73
74//-----------------------------------------------------------------------------
75
76#define M_IMGDATA ((wxImageRefData *)m_refData)
77
78#if !USE_SHARED_LIBRARIES
79IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject)
80#endif
81
82wxImage::wxImage()
83{
84}
85
86wxImage::wxImage( int width, int height )
87{
fd0eed64 88 Create( width, height );
01111366
RR
89}
90
91wxImage::wxImage( const wxString& name, long type )
92{
fd0eed64 93 LoadFile( name, type );
01111366
RR
94}
95
3d05544e
JS
96wxImage::wxImage( wxInputStream& stream, long type )
97{
98 LoadFile( stream, type );
99}
100
4698648f
VZ
101wxImage::wxImage( const wxImage& image )
102{
103 Ref(image);
01111366
RR
104}
105
4698648f
VZ
106wxImage::wxImage( const wxImage* image )
107{
108 if (image) Ref(*image);
01111366
RR
109}
110
111void wxImage::Create( int width, int height )
112{
fd0eed64 113 m_refData = new wxImageRefData();
4698648f 114
fd0eed64
RR
115 M_IMGDATA->m_data = (unsigned char *) malloc( width*height*3 );
116 if (M_IMGDATA->m_data)
117 {
118 for (int l = 0; l < width*height*3; l++) M_IMGDATA->m_data[l] = 0;
4698648f 119
fd0eed64
RR
120 M_IMGDATA->m_width = width;
121 M_IMGDATA->m_height = height;
122 M_IMGDATA->m_ok = TRUE;
123 }
124 else
125 {
126 UnRef();
127 }
01111366
RR
128}
129
130void wxImage::Destroy()
131{
fd0eed64 132 UnRef();
01111366
RR
133}
134
4bc67cc5
RR
135wxImage wxImage::Scale( int width, int height )
136{
137 wxImage image;
4698648f 138
4bc67cc5 139 wxCHECK_MSG( Ok(), image, "invlaid image" );
4698648f 140
4bc67cc5 141 wxCHECK_MSG( (width > 0) && (height > 0), image, "invalid image size" );
4698648f 142
4bc67cc5 143 image.Create( width, height );
4698648f 144
4bc67cc5 145 char unsigned *data = image.GetData();
4698648f 146
4bc67cc5 147 wxCHECK_MSG( data, image, "unable to create image" );
4698648f 148
4bc67cc5
RR
149 if (M_IMGDATA->m_hasMask)
150 image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
4698648f 151
4bc67cc5
RR
152 double xscale = (double)width / (double)M_IMGDATA->m_width;
153 double yscale = (double)height / (double)M_IMGDATA->m_height;
4698648f 154
4bc67cc5
RR
155 for (int j = 0; j < height; j++)
156 {
157 for (int i = 0; i < width; i++)
4698648f
VZ
158 {
159 int new_pos = 3*(j*width + i);
160 int old_pos = 3*((long)(j/yscale)*M_IMGDATA->m_width + (long)(i/xscale));
161 data[ new_pos ] = M_IMGDATA->m_data[ old_pos ];
162 data[ new_pos+1 ] = M_IMGDATA->m_data[ old_pos+1 ];
163 data[ new_pos+2 ] = M_IMGDATA->m_data[ old_pos+2 ];
164 }
4bc67cc5 165 }
4698648f 166
4bc67cc5
RR
167 return image;
168}
4698648f 169
ef539066
RR
170void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b )
171{
172 wxCHECK_RET( Ok(), "invalid image" );
4698648f 173
ef539066
RR
174 int w = M_IMGDATA->m_width;
175 int h = M_IMGDATA->m_height;
4698648f 176
ef539066 177 wxCHECK_RET( (x>=0) && (y>=0) && (x<w) && (y<h), "invalid image index" );
4698648f 178
ef539066 179 long pos = (y * w + x) * 3;
4698648f 180
ef539066
RR
181 M_IMGDATA->m_data[ pos ] = r;
182 M_IMGDATA->m_data[ pos+1 ] = g;
183 M_IMGDATA->m_data[ pos+2 ] = b;
184}
185
186unsigned char wxImage::GetRed( int x, int y )
187{
188 wxCHECK_MSG( Ok(), 0, "invalid image" );
4698648f 189
ef539066
RR
190 int w = M_IMGDATA->m_width;
191 int h = M_IMGDATA->m_height;
4698648f 192
ef539066 193 wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, "invalid image index" );
4698648f 194
ef539066 195 long pos = (y * w + x) * 3;
4698648f 196
ef539066
RR
197 return M_IMGDATA->m_data[pos];
198}
199
200unsigned char wxImage::GetGreen( int x, int y )
201{
202 wxCHECK_MSG( Ok(), 0, "invalid image" );
4698648f 203
ef539066
RR
204 int w = M_IMGDATA->m_width;
205 int h = M_IMGDATA->m_height;
4698648f 206
ef539066 207 wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, "invalid image index" );
4698648f 208
ef539066 209 long pos = (y * w + x) * 3;
4698648f 210
ef539066
RR
211 return M_IMGDATA->m_data[pos+1];
212}
213
214unsigned char wxImage::GetBlue( int x, int y )
215{
216 wxCHECK_MSG( Ok(), 0, "invalid image" );
4698648f 217
ef539066
RR
218 int w = M_IMGDATA->m_width;
219 int h = M_IMGDATA->m_height;
4698648f 220
ef539066 221 wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, "invalid image index" );
4698648f 222
ef539066 223 long pos = (y * w + x) * 3;
4698648f 224
ef539066
RR
225 return M_IMGDATA->m_data[pos+2];
226}
4698648f
VZ
227
228bool wxImage::Ok() const
229{
230 return (M_IMGDATA && M_IMGDATA->m_ok);
01111366
RR
231}
232
233char unsigned *wxImage::GetData() const
234{
4bc67cc5 235 wxCHECK_MSG( Ok(), (char unsigned *)NULL, "invalid image" );
4698648f 236
fd0eed64 237 return M_IMGDATA->m_data;
01111366
RR
238}
239
240void wxImage::SetData( char unsigned *WXUNUSED(data) )
241{
4bc67cc5 242 wxCHECK_RET( Ok(), "invalid image" );
01111366
RR
243}
244
245void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b )
246{
4bc67cc5 247 wxCHECK_RET( Ok(), "invalid image" );
4698648f 248
fd0eed64
RR
249 M_IMGDATA->m_maskRed = r;
250 M_IMGDATA->m_maskGreen = g;
251 M_IMGDATA->m_maskBlue = b;
252 M_IMGDATA->m_hasMask = TRUE;
01111366
RR
253}
254
255unsigned char wxImage::GetMaskRed() const
256{
4bc67cc5 257 wxCHECK_MSG( Ok(), 0, "invalid image" );
4698648f 258
fd0eed64 259 return M_IMGDATA->m_maskRed;
01111366
RR
260}
261
262unsigned char wxImage::GetMaskGreen() const
263{
4bc67cc5 264 wxCHECK_MSG( Ok(), 0, "invalid image" );
4698648f 265
fd0eed64 266 return M_IMGDATA->m_maskGreen;
01111366
RR
267}
268
269unsigned char wxImage::GetMaskBlue() const
270{
4bc67cc5 271 wxCHECK_MSG( Ok(), 0, "invalid image" );
4698648f 272
fd0eed64 273 return M_IMGDATA->m_maskBlue;
01111366 274}
4698648f 275
01111366
RR
276void wxImage::SetMask( bool mask )
277{
4bc67cc5 278 wxCHECK_RET( Ok(), "invalid image" );
4698648f 279
fd0eed64 280 M_IMGDATA->m_hasMask = mask;
01111366
RR
281}
282
283bool wxImage::HasMask() const
284{
4bc67cc5 285 wxCHECK_MSG( Ok(), FALSE, "invalid image" );
4698648f 286
fd0eed64 287 return M_IMGDATA->m_hasMask;
01111366
RR
288}
289
4698648f
VZ
290int wxImage::GetWidth() const
291{
4bc67cc5 292 wxCHECK_MSG( Ok(), 0, "invalid image" );
4698648f
VZ
293
294 return M_IMGDATA->m_width;
01111366
RR
295}
296
4698648f
VZ
297int wxImage::GetHeight() const
298{
4bc67cc5 299 wxCHECK_MSG( Ok(), 0, "invalid image" );
4698648f
VZ
300
301 return M_IMGDATA->m_height;
01111366
RR
302}
303
304bool wxImage::LoadFile( const wxString& filename, long type )
305{
3d05544e 306 if (wxFileExists(filename))
fd0eed64 307 {
3d05544e
JS
308 wxFileInputStream stream(filename);
309 return LoadFile(stream, type);
310 }
311
312 else {
fd0eed64 313 wxLogWarning( "Image file does not exist." );
dc86cb34 314
fd0eed64
RR
315 return FALSE;
316 }
3d05544e 317}
01111366 318
3d05544e
JS
319bool wxImage::LoadFile( wxInputStream& stream, long type )
320{
321 UnRef();
4698648f 322
fd0eed64 323 m_refData = new wxImageRefData;
01111366 324
fd0eed64 325 wxImageHandler *handler = FindHandler(type);
01111366 326
4698648f 327 if (handler == NULL)
fd0eed64
RR
328 {
329 wxLogWarning( "No image handler for type %d defined.", type );
01111366 330
fd0eed64
RR
331 return FALSE;
332 }
01111366 333
3d05544e 334 return handler->LoadFile( this, stream );
01111366
RR
335}
336
337bool wxImage::SaveFile( const wxString& filename, int type )
3d05544e
JS
338{
339 wxFileOutputStream stream(filename);
340
341 if ( stream.LastError() == wxStream_NOERROR )
342 return SaveFile(stream, type);
343 else
344 return FALSE;
345}
346
347bool wxImage::SaveFile( wxOutputStream& stream, int type )
01111366 348{
4bc67cc5 349 wxCHECK_MSG( Ok(), FALSE, "invalid image" );
4698648f 350
fd0eed64 351 wxImageHandler *handler = FindHandler(type);
01111366 352
4698648f 353 if (handler == NULL)
fd0eed64
RR
354 {
355 wxLogWarning( "No image handler for type %d defined.", type );
01111366 356
fd0eed64
RR
357 return FALSE;
358 }
01111366 359
3d05544e 360 return handler->SaveFile( this, stream );
01111366
RR
361}
362
363void wxImage::AddHandler( wxImageHandler *handler )
364{
4698648f
VZ
365 // make sure that the memory will be freed at the program end
366 sm_handlers.DeleteContents(TRUE);
367
01111366
RR
368 sm_handlers.Append( handler );
369}
370
371void wxImage::InsertHandler( wxImageHandler *handler )
372{
4698648f
VZ
373 // make sure that the memory will be freed at the program end
374 sm_handlers.DeleteContents(TRUE);
375
01111366
RR
376 sm_handlers.Insert( handler );
377}
378
379bool wxImage::RemoveHandler( const wxString& name )
380{
fd0eed64
RR
381 wxImageHandler *handler = FindHandler(name);
382 if (handler)
383 {
384 sm_handlers.DeleteObject(handler);
385 return TRUE;
386 }
387 else
388 return FALSE;
01111366
RR
389}
390
391wxImageHandler *wxImage::FindHandler( const wxString& name )
392{
fd0eed64
RR
393 wxNode *node = sm_handlers.First();
394 while (node)
395 {
396 wxImageHandler *handler = (wxImageHandler*)node->Data();
397 if (handler->GetName() == name) return handler;
398 node = node->Next();
399 }
400 return (wxImageHandler *)NULL;
01111366
RR
401}
402
403wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType )
404{
fd0eed64
RR
405 wxNode *node = sm_handlers.First();
406 while (node)
407 {
408 wxImageHandler *handler = (wxImageHandler*)node->Data();
409 if ( handler->GetExtension() == extension &&
410 (bitmapType == -1 || handler->GetType() == bitmapType) )
411 return handler;
412 node = node->Next();
413 }
414 return (wxImageHandler*)NULL;
01111366
RR
415}
416
417wxImageHandler *wxImage::FindHandler( long bitmapType )
418{
fd0eed64
RR
419 wxNode *node = sm_handlers.First();
420 while (node)
421 {
422 wxImageHandler *handler = (wxImageHandler *)node->Data();
423 if (handler->GetType() == bitmapType) return handler;
424 node = node->Next();
425 }
426 return NULL;
427}
428
429void wxImage::InitStandardHandlers()
430{
431 AddHandler( new wxBMPHandler );
ac57418f 432#ifdef wxUSE_LIBPNG
fd0eed64 433 AddHandler( new wxPNGHandler );
ac57418f 434#endif
01111366
RR
435}
436
437void wxImage::CleanUpHandlers()
438{
fd0eed64
RR
439 wxNode *node = sm_handlers.First();
440 while (node)
441 {
442 wxImageHandler *handler = (wxImageHandler *)node->Data();
443 wxNode *next = node->Next();
444 delete handler;
445 delete node;
446 node = next;
447 }
01111366
RR
448}
449
450//-----------------------------------------------------------------------------
451// wxImageHandler
452//-----------------------------------------------------------------------------
453
454#if !USE_SHARED_LIBRARIES
455IMPLEMENT_DYNAMIC_CLASS(wxImageHandler,wxObject)
456#endif
457
3d05544e 458bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream) )
01111366 459{
fd0eed64 460 return FALSE;
01111366
RR
461}
462
3d05544e 463bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream) )
01111366 464{
fd0eed64 465 return FALSE;
01111366
RR
466}
467
468//-----------------------------------------------------------------------------
469// wxPNGHandler
470//-----------------------------------------------------------------------------
471
ac57418f
RR
472#ifdef wxUSE_LIBPNG
473
01111366
RR
474#if !USE_SHARED_LIBRARIES
475IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler,wxImageHandler)
476#endif
3d05544e
JS
477
478
479static void _PNG_stream_reader( png_structp png_ptr, png_bytep data, png_size_t length )
480{
481 ((wxInputStream*) png_get_io_ptr( png_ptr )) -> Read(data, length);
482}
4698648f 483
3d05544e 484static void _PNG_stream_writer( png_structp png_ptr, png_bytep data, png_size_t length )
01111366 485{
3d05544e
JS
486 ((wxOutputStream*) png_get_io_ptr( png_ptr )) -> Write(data, length);
487}
4698648f 488
3d05544e
JS
489bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream )
490{
4698648f
VZ
491 // VZ: as this function uses setjmp() the only fool proof error handling
492 // method is to use goto (setjmp is not really C++ dtors friendly...)
702ca7c0 493 image->Destroy();
702ca7c0 494
4698648f
VZ
495 unsigned int i;
496 unsigned char **lines = NULL;
497 png_infop info_ptr = NULL;
498
499 png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
500 (voidp) NULL,
501 (png_error_ptr) NULL,
502 (png_error_ptr) NULL );
503 if (!png_ptr)
504 goto error;
505
506 info_ptr = png_create_info_struct( png_ptr );
702ca7c0 507 if (!info_ptr)
4698648f 508 goto error;
702ca7c0
RR
509
510 if (setjmp(png_ptr->jmpbuf))
4698648f 511 goto error;
702ca7c0
RR
512
513 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4698648f
VZ
514 goto error;
515
3d05544e 516 png_set_read_fn( png_ptr, &stream, _PNG_stream_reader);
4698648f 517
702ca7c0
RR
518 png_uint_32 width,height;
519 int bit_depth,color_type,interlace_type;
520
521 png_read_info( png_ptr, info_ptr );
522 png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, (int*) NULL, (int*) NULL );
4698648f
VZ
523
524 if (color_type == PNG_COLOR_TYPE_PALETTE)
525 png_set_expand( png_ptr );
526
702ca7c0
RR
527 png_set_strip_16( png_ptr );
528 png_set_packing( png_ptr );
4698648f
VZ
529 if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS))
530 png_set_expand( png_ptr );
702ca7c0 531 png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
4698648f 532
702ca7c0 533 image->Create( width, height );
4698648f 534
702ca7c0 535 if (!image->Ok())
4698648f
VZ
536 goto error;
537
538 lines = (unsigned char **)malloc( height * sizeof(unsigned char *) );
702ca7c0 539 if (lines == NULL)
4698648f
VZ
540 goto error;
541
542 for (i = 0; i < height; i++)
702ca7c0
RR
543 {
544 if ((lines[i] = (unsigned char *)malloc(width * (sizeof(unsigned char) * 4))) == NULL)
545 {
4698648f
VZ
546 for ( unsigned int n = 0; n < i; n++ )
547 free( lines[n] );
548 goto error;
702ca7c0
RR
549 }
550 }
4698648f
VZ
551
552 // loaded successfully!
702ca7c0 553 {
4698648f
VZ
554 int transp = 0;
555 png_read_image( png_ptr, lines );
556 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
557 unsigned char *ptr = image->GetData();
558 if ((color_type == PNG_COLOR_TYPE_GRAY) ||
559 (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
560 {
561 for (unsigned int y = 0; y < height; y++)
562 {
563 unsigned char *ptr2 = lines[y];
564 for (unsigned int x = 0; x < width; x++)
565 {
566 unsigned char r = *ptr2++;
567 unsigned char a = *ptr2++;
568 if (a < 128)
569 {
570 *ptr++ = 255;
571 *ptr++ = 0;
572 *ptr++ = 255;
573 transp = 1;
574 }
575 else
576 {
577 *ptr++ = r;
578 *ptr++ = r;
579 *ptr++ = r;
580 }
581 }
582 }
583 }
584 else
585 {
586 for (unsigned int y = 0; y < height; y++)
587 {
588 unsigned char *ptr2 = lines[y];
589 for (unsigned int x = 0; x < width; x++)
590 {
591 unsigned char r = *ptr2++;
592 unsigned char g = *ptr2++;
593 unsigned char b = *ptr2++;
594 unsigned char a = *ptr2++;
595 if (a < 128)
596 {
597 *ptr++ = 255;
598 *ptr++ = 0;
599 *ptr++ = 255;
600 transp = 1;
601 }
602 else
603 {
604 if ((r == 255) && (g == 0) && (b == 255)) r = 254;
605 *ptr++ = r;
606 *ptr++ = g;
607 *ptr++ = b;
608 }
609 }
610 }
611 }
612
613 for ( unsigned int j = 0; j < height; j++ )
614 free( lines[j] );
615 free( lines );
616
617 if (transp)
618 {
619 image->SetMaskColour( 255, 0, 255 );
620 }
621 else
622 {
623 image->SetMask( FALSE );
624 }
702ca7c0 625 }
4698648f
VZ
626
627 return TRUE;
628
629error:
630 wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
631
632 if ( image->Ok() )
702ca7c0 633 {
4698648f 634 image->Destroy();
702ca7c0 635 }
4698648f
VZ
636
637 if ( lines )
702ca7c0 638 {
4698648f 639 free( lines );
702ca7c0 640 }
4698648f
VZ
641
642 if ( png_ptr )
702ca7c0 643 {
4698648f
VZ
644 if ( info_ptr )
645 {
646 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
647 free(info_ptr);
648 }
649 else
650 png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
702ca7c0 651 }
4698648f 652 return FALSE;
01111366
RR
653}
654
655
3d05544e 656bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream )
01111366 657{
3d05544e
JS
658 {
659 png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
4698648f
VZ
660 if (!png_ptr)
661 {
662 return FALSE;
01111366 663 }
4698648f 664
01111366
RR
665 png_infop info_ptr = png_create_info_struct(png_ptr);
666 if (info_ptr == NULL)
667 {
3d05544e
JS
668 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
669 return FALSE;
01111366 670 }
4698648f 671
01111366
RR
672 if (setjmp(png_ptr->jmpbuf))
673 {
3d05544e
JS
674 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
675 return FALSE;
01111366 676 }
4698648f 677
3d05544e
JS
678 png_set_write_fn( png_ptr, &stream, _PNG_stream_writer, NULL);
679
01111366 680 png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), 8,
4698648f
VZ
681 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
682 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
702ca7c0 683
01111366
RR
684 png_color_8 sig_bit;
685 sig_bit.red = 8;
686 sig_bit.green = 8;
687 sig_bit.blue = 8;
688 sig_bit.alpha = 8;
689 png_set_sBIT( png_ptr, info_ptr, &sig_bit );
690 png_write_info( png_ptr, info_ptr );
691 png_set_shift( png_ptr, &sig_bit );
692 png_set_packing( png_ptr );
4698648f 693
01111366
RR
694 unsigned char *data = (unsigned char *)malloc( image->GetWidth()*4 );
695 if (!data)
696 {
3d05544e
JS
697 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
698 return FALSE;
01111366 699 }
4698648f 700
01111366
RR
701 for (int y = 0; y < image->GetHeight(); y++)
702 {
702ca7c0
RR
703 unsigned char *ptr = image->GetData() + (y * image->GetWidth() * 3);
704 for (int x = 0; x < image->GetWidth(); x++)
705 {
4698648f
VZ
706 data[(x << 2) + 0] = *ptr++;
707 data[(x << 2) + 1] = *ptr++;
708 data[(x << 2) + 2] = *ptr++;
709 if ((data[(x << 2) + 0] == image->GetMaskRed()) &&
710 (data[(x << 2) + 1] == image->GetMaskGreen()) &&
711 (data[(x << 2) + 2] == image->GetMaskBlue()))
712 {
702ca7c0 713 data[(x << 2) + 3] = 0;
4698648f
VZ
714 }
715 else
716 {
717 data[(x << 2) + 3] = 255;
718 }
702ca7c0
RR
719 }
720 png_bytep row_ptr = data;
721 png_write_rows( png_ptr, &row_ptr, 1 );
01111366 722 }
4698648f 723
01111366
RR
724 free(data);
725 png_write_end( png_ptr, info_ptr );
4698648f 726 png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );
3d05544e
JS
727 }
728 return TRUE;
01111366
RR
729}
730
4698648f 731#endif
ac57418f
RR
732
733 // wxUSE_LIBPNG
734
01111366
RR
735//-----------------------------------------------------------------------------
736// wxBMPHandler
737//-----------------------------------------------------------------------------
738
739#if !USE_SHARED_LIBRARIES
740IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler,wxImageHandler)
741#endif
4698648f 742
3d05544e 743bool wxBMPHandler::LoadFile( wxImage *image, wxInputStream& stream )
01111366 744{
01111366
RR
745 unsigned char *data, *ptr;
746 int done, i, bpp, planes, comp, ncolors, line, column,
747 linesize, linepos, rshift = 0, gshift = 0, bshift = 0;
e3554471 748 unsigned char aByte;
01111366
RR
749 short int word;
750 long int dbuf[4], dword, rmask = 0, gmask = 0, bmask = 0, offset,
751 size;
3d05544e 752 off_t start_offset = stream.TellI();
01111366
RR
753 signed char bbuf[4];
754 struct _cmap
755 {
4698648f 756 unsigned char r, g, b;
01111366
RR
757 }
758 *cmap = NULL;
0b4f9ee3 759#ifndef BI_RGB
01111366
RR
760#define BI_RGB 0
761#define BI_RLE8 1
762#define BI_RLE4 2
62448488
JS
763#endif
764
765#ifndef BI_BITFIELDS
01111366 766#define BI_BITFIELDS 3
0b4f9ee3 767#endif
01111366
RR
768
769 image->Destroy();
770
01111366 771 done = 0;
4698648f
VZ
772 /*
773 * Reading the bmp header
01111366
RR
774 */
775
3d05544e 776 stream.Read(&bbuf, 2);
01111366 777
3d05544e 778 stream.Read(dbuf, 4 * 4);
01111366
RR
779
780 size = dbuf[0];
781 offset = dbuf[2];
782
3d05544e 783 stream.Read(dbuf, 4 * 2);
01111366
RR
784 int width = (int)dbuf[0];
785 int height = (int)dbuf[1];
786 if (width > 32767)
787 {
4698648f
VZ
788 wxLogError( "Image width > 32767 pixels for file\n" );
789 return FALSE;
01111366
RR
790 }
791 if (height > 32767)
792 {
4698648f
VZ
793 wxLogError( "Image height > 32767 pixels for file\n" );
794 return FALSE;
01111366 795 }
3d05544e 796 stream.Read(&word, 2);
01111366 797 planes = (int)word;
3d05544e 798 stream.Read(&word, 2);
01111366
RR
799 bpp = (int)word;
800 if (bpp != 1 && bpp != 4 && bpp != 8 && bpp && 16 && bpp != 24 && bpp != 32)
801 {
4698648f
VZ
802 wxLogError( "unknown bitdepth in file\n" );
803 return FALSE;
01111366 804 }
3d05544e 805 stream.Read(dbuf, 4 * 4);
01111366
RR
806 comp = (int)dbuf[0];
807 if (comp != BI_RGB && comp != BI_RLE4 && comp != BI_RLE8 && comp != BI_BITFIELDS)
808 {
06cfab17 809 wxLogError( "unknown encoding in Windows BMP file\n" );
4698648f 810 return FALSE;
01111366 811 }
3d05544e 812 stream.Read(dbuf, 4 * 2);
01111366
RR
813 ncolors = (int)dbuf[0];
814 if (ncolors == 0)
815 ncolors = 1 << bpp;
816 /* some more sanity checks */
817 if (((comp == BI_RLE4) && (bpp != 4)) || ((comp == BI_RLE8) && (bpp != 8)) || ((comp == BI_BITFIELDS) && (bpp != 16 && bpp != 32)))
818 {
06cfab17 819 wxLogError( "encoding of BMP doesn't match bitdepth\n" );
4698648f 820 return FALSE;
01111366
RR
821 }
822 if (bpp < 16)
823 {
4698648f 824 cmap = (struct _cmap *)malloc(sizeof(struct _cmap) * ncolors);
01111366 825
4698648f
VZ
826 if (!cmap)
827 {
828 wxLogError( "Cannot allocate RAM for color map in BMP file\n" );
829 return FALSE;
830 }
01111366
RR
831 }
832 else
833 cmap = NULL;
4698648f
VZ
834
835 image->Create( width, height );
01111366
RR
836 ptr = image->GetData();
837 if (!ptr)
838 {
06cfab17 839 wxLogError( "Cannot allocate RAM for RGB data in file\n" );
4698648f
VZ
840 if (cmap)
841 free(cmap);
842 return FALSE;
01111366
RR
843 }
844
845 /*
846 * Reading the palette, if it exists.
847 */
848 if (bpp < 16 && ncolors != 0)
849 {
4698648f
VZ
850 for (i = 0; i < ncolors; i++)
851 {
852 stream.Read(bbuf, 4);
853 cmap[i].b = bbuf[0];
854 cmap[i].g = bbuf[1];
855 cmap[i].r = bbuf[2];
856 }
01111366
RR
857 }
858 else if (bpp == 16 || bpp == 32)
859 {
4698648f
VZ
860 if (comp == BI_BITFIELDS)
861 {
862 int bit = 0;
863
864 stream.Read(dbuf, 4 * 3);
865 bmask = dbuf[0];
866 gmask = dbuf[1];
867 rmask = dbuf[2];
868 /* find shift amount.. ugly, but i can't think of a better way */
869 for (bit = 0; bit < bpp; bit++)
870 {
871 if (bmask & (1 << bit))
872 bshift = bit;
873 if (gmask & (1 << bit))
874 gshift = bit;
875 if (rmask & (1 << bit))
876 rshift = bit;
877 }
878 }
879 else if (bpp == 16)
880 {
881 rmask = 0x7C00;
882 gmask = 0x03E0;
883 bmask = 0x001F;
884 rshift = 10;
885 gshift = 5;
886 bshift = 0;
887 }
888 else if (bpp == 32)
889 {
890 rmask = 0x00FF0000;
891 gmask = 0x0000FF00;
892 bmask = 0x000000FF;
893 rshift = 16;
894 gshift = 8;
895 bshift = 0;
896 }
01111366
RR
897 }
898
899 /*
bbe0af5b 900 * Reading the image data
01111366 901 */
3d05544e 902 stream.SeekI(start_offset + offset);
01111366
RR
903 data = ptr;
904
905 /* set the whole image to the background color */
906 if (bpp < 16 && (comp == BI_RLE4 || comp == BI_RLE8))
907 {
4698648f
VZ
908 for (i = 0; i < width * height; i++)
909 {
910 *ptr++ = cmap[0].r;
911 *ptr++ = cmap[0].g;
912 *ptr++ = cmap[0].b;
913 }
914 ptr = data;
01111366
RR
915 }
916 line = 0;
917 column = 0;
918#define poffset (line * width * 3 + column * 3)
919
920 /*
921 * BMPs are stored upside down... hmmmmmmmmmm....
922 */
923
924 linesize = ((width * bpp + 31) / 32) * 4;
925 for (line = (height - 1); line >= 0; line--)
926 {
4698648f
VZ
927 linepos = 0;
928 for (column = 0; column < width;)
929 {
930 if (bpp < 16)
931 {
932 int index;
933
934 linepos++;
935 aByte = stream.GetC();
936 if (bpp == 1)
937 {
938 int bit = 0;
939
940 for (bit = 0; bit < 8; bit++)
941 {
942 index = ((aByte & (0x80 >> bit)) ? 1 : 0);
943 ptr[poffset] = cmap[index].r;
944 ptr[poffset + 1] = cmap[index].g;
945 ptr[poffset + 2] = cmap[index].b;
946 column++;
947 }
948 }
949 else if (bpp == 4)
950 {
951 if (comp == BI_RLE4)
952 {
953 wxLogError( "can't deal with 4bit encoded yet.\n");
954 image->Destroy();
955 free(cmap);
956 return FALSE;
957 }
958 else
959 {
960 int nibble = 0;
961
962 for (nibble = 0; nibble < 2; nibble++)
963 {
964 index = ((aByte & (0xF0 >> nibble * 4)) >> (!nibble * 4));
965 if (index >= 16)
966 index = 15;
967 ptr[poffset] = cmap[index].r;
968 ptr[poffset + 1] = cmap[index].g;
969 ptr[poffset + 2] = cmap[index].b;
970 column++;
971 }
972 }
973 }
974 else if (bpp == 8)
975 {
976 if (comp == BI_RLE8)
977 {
978 unsigned char first;
979
980 first = aByte;
981 aByte = stream.GetC();
982 if (first == 0)
983 {
984 if (aByte == 0)
985 {
01111366 986/* column = width; */
4698648f
VZ
987 }
988 else if (aByte == 1)
989 {
990 column = width;
991 line = -1;
992 }
993 else if (aByte == 2)
994 {
995 aByte = stream.GetC();
996 column += aByte;
997 linepos = column * bpp / 8;
998 aByte = stream.GetC();
999 line += aByte;
1000 }
1001 else
1002 {
1003 int absolute = aByte;
1004
1005 for (i = 0; i < absolute; i++)
1006 {
1007 linepos++;
1008 aByte = stream.GetC();
1009 ptr[poffset] = cmap[aByte].r;
1010 ptr[poffset + 1] = cmap[aByte].g;
1011 ptr[poffset + 2] = cmap[aByte].b;
1012 column++;
1013 }
1014 if (absolute & 0x01)
1015 aByte = stream.GetC();
1016 }
1017 }
1018 else
1019 {
1020 for (i = 0; i < first; i++)
1021 {
1022 ptr[poffset] = cmap[aByte].r;
1023 ptr[poffset + 1] = cmap[aByte].g;
1024 ptr[poffset + 2] = cmap[aByte].b;
1025 column++;
1026 linepos++;
1027 }
1028 }
1029 }
1030 else
1031 {
1032 ptr[poffset] = cmap[aByte].r;
1033 ptr[poffset + 1] = cmap[aByte].g;
1034 ptr[poffset + 2] = cmap[aByte].b;
1035 column++;
1036 linepos += size;
1037 }
1038 }
1039 }
1040 else if (bpp == 24)
1041 {
1042 stream.Read(&bbuf, 3);
1043 linepos += 3;
1044 ptr[poffset] = (unsigned char)bbuf[2];
1045 ptr[poffset + 1] = (unsigned char)bbuf[1];
1046 ptr[poffset + 2] = (unsigned char)bbuf[0];
1047 column++;
1048 }
1049 else if (bpp == 16)
1050 {
1051 unsigned char temp;
1052
1053 stream.Read(&word, 2);
1054 linepos += 2;
1055 temp = (word & rmask) >> rshift;
1056 ptr[poffset] = temp;
1057 temp = (word & gmask) >> gshift;
1058 ptr[poffset + 1] = temp;
1059 temp = (word & bmask) >> gshift;
1060 ptr[poffset + 2] = temp;
1061 column++;
1062 }
1063 else
1064 {
1065 unsigned char temp;
1066
1067 stream.Read(&dword, 4);
1068 linepos += 4;
1069 temp = (dword & rmask) >> rshift;
1070 ptr[poffset] = temp;
1071 temp = (dword & gmask) >> gshift;
1072 ptr[poffset + 1] = temp;
1073 temp = (dword & bmask) >> bshift;
1074 ptr[poffset + 2] = temp;
1075 column++;
1076 }
1077 }
1078 while ((linepos < linesize) && (comp != 1) && (comp != 2))
1079 {
1080 stream.Read(&aByte, 1);
1081 linepos += 1;
1082 if (stream.LastError() != wxStream_NOERROR)
1083 break;
1084 }
01111366
RR
1085 }
1086 if (cmap) free(cmap);
4698648f 1087
01111366 1088 image->SetMask( FALSE );
4698648f 1089
01111366
RR
1090 return TRUE;
1091}
1092
e3554471
JS
1093#ifdef __WXMSW__
1094
1095wxBitmap wxImage::ConvertToBitmap() const
1096{
4698648f 1097
e3554471
JS
1098 wxBitmap bitmap;
1099 wxCHECK_MSG( Ok(), bitmap, "invalid image" );
1100 int width = GetWidth();
1101 int height = GetHeight();
1102 bitmap.SetWidth( width );
1103 bitmap.SetHeight( height );
1104 bitmap.SetDepth( wxDisplayDepth() );
1105
1106 int headersize = sizeof(BITMAPINFOHEADER);
1107 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
1108 wxCHECK_MSG( lpDIBh, bitmap, "could not allocate memory for DIB header" );
1109
1110// Fill in the DIB header
1111 lpDIBh->bmiHeader.biSize = headersize;
1112 lpDIBh->bmiHeader.biWidth = width;
1113 lpDIBh->bmiHeader.biHeight = -height;
1114 lpDIBh->bmiHeader.biSizeImage = width * height * 3;
1115
1116 lpDIBh->bmiHeader.biPlanes = 1;
1117 lpDIBh->bmiHeader.biBitCount = 24;
1118 lpDIBh->bmiHeader.biCompression = BI_RGB;
1119 lpDIBh->bmiHeader.biClrUsed = 0;
1120
1121// These seem not needed for our purpose here.
1122// lpDIBh->bmiHeader.biClrImportant = 0;
1123// lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1124// lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1125
1126 unsigned char *lpBits = (unsigned char *) malloc( width*height*3 );
1127 if( !lpBits )
1128 {
1129 wxFAIL_MSG( "could not allocate memory for DIB" );
1130 free( lpDIBh );
1131 return bitmap;
1132 }
1133
1134 unsigned char *data = GetData();
1135
1136 unsigned char *ptdata = data, *ptbits = lpBits;
1137 for( int i=0; i<width*height; i++ )
1138 {
1139 *(ptbits++) = *(ptdata+2);
1140 *(ptbits++) = *(ptdata+1);
1141 *(ptbits++) = *(ptdata );
1142 ptdata += 3;
1143 }
4698648f 1144
e3554471
JS
1145 HDC hdc = ::GetDC(NULL);
1146
1147 HBITMAP hbitmap;
1148 hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
4698648f 1149
e3554471
JS
1150// The above line is equivalent to the following two lines.
1151// hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1152// ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
1153// or the following lines
1154// hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1155// HDC memdc = ::CreateCompatibleDC( hdc );
4698648f 1156// ::SelectObject( memdc, hbitmap);
e3554471 1157// ::SetDIBitsToDevice( memdc, 0, 0, width, height,
4698648f
VZ
1158// 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
1159// ::SelectObject( memdc, 0 );
1160// ::DeleteDC( memdc );
e3554471
JS
1161
1162 bitmap.SetHBITMAP( (WXHBITMAP) hbitmap );
1163
1164 if( HasMask() )
1165 {
1166 unsigned char r = GetMaskRed();
1167 unsigned char g = GetMaskGreen();
1168 unsigned char b = GetMaskBlue();
1169 unsigned char zero = 0, one = 255;
1170 ptdata = data;
1171 ptbits = lpBits;
1172 for( int i=0; i<width*height; i++ )
1173 {
1174 if( (*(ptdata++)!=r) | (*(ptdata++)!=g) | (*(ptdata++)!=b) )
e3554471
JS
1175 {
1176 *(ptbits++) = one;
1177 *(ptbits++) = one;
1178 *(ptbits++) = one;
1179 }
650368d0
UA
1180 else
1181 {
1182 *(ptbits++) = zero;
1183 *(ptbits++) = zero;
1184 *(ptbits++) = zero;
1185 }
e3554471
JS
1186 }
1187 hbitmap = ::CreateBitmap( (WORD)width, (WORD)height, 1, 1, NULL );
1188 ::SetDIBits( hdc, hbitmap, 0, (WORD)height, lpBits, lpDIBh, DIB_RGB_COLORS);
650368d0
UA
1189 wxMask *mask = new wxMask();
1190 mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
1191 bitmap.SetMask( mask );
e3554471
JS
1192
1193/* The following can also be used but is slow to run
1194 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
650368d0
UA
1195 wxMask *mask = new wxMask( bitmap, colour );
1196 bitmap.SetMask( mask );
e3554471
JS
1197*/
1198 }
1199
4698648f 1200 ::ReleaseDC(NULL, hdc);
e3554471
JS
1201 free(lpDIBh);
1202 free(lpBits);
1203
1204 if( bitmap.GetHBITMAP() )
1205 bitmap.SetOk( TRUE );
1206 else
1207 bitmap.SetOk( FALSE );
4698648f 1208
e3554471
JS
1209 return bitmap;
1210}
1211
1212
1213wxImage::wxImage( const wxBitmap &bitmap )
1214{
1215 if( !bitmap.Ok() )
1216 {
1217 wxFAIL_MSG( "invalid bitmap" );
1218 return;
1219 }
1220
1221 int width = bitmap.GetWidth();
1222 int height = bitmap.GetHeight();
4698648f 1223 Create( width, height );
e3554471
JS
1224 unsigned char *data = GetData();
1225 if( !data )
1226 {
1227 wxFAIL_MSG( "could not allocate data for image" );
1228 return;
1229 }
4698648f 1230
e3554471
JS
1231 int headersize = sizeof(BITMAPINFOHEADER);
1232 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
1233 if( !lpDIBh )
1234 {
1235 wxFAIL_MSG( "could not allocate data for DIB header" );
1236 free( data );
1237 return;
1238 }
1239
1240// Fill in the DIB header
1241 lpDIBh->bmiHeader.biSize = headersize;
1242 lpDIBh->bmiHeader.biWidth = width;
1243 lpDIBh->bmiHeader.biHeight = -height;
1244 lpDIBh->bmiHeader.biSizeImage = width * height * 3;
1245
1246 lpDIBh->bmiHeader.biPlanes = 1;
1247 lpDIBh->bmiHeader.biBitCount = 24;
1248 lpDIBh->bmiHeader.biCompression = BI_RGB;
1249 lpDIBh->bmiHeader.biClrUsed = 0;
1250
1251// These seem not needed for our purpose here.
1252// lpDIBh->bmiHeader.biClrImportant = 0;
1253// lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1254// lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1255
1256 unsigned char *lpBits = (unsigned char *) malloc( width*height*3 );
1257 if( !lpBits )
1258 {
1259 wxFAIL_MSG( "could not allocate data for DIB" );
1260 free( data );
1261 free( lpDIBh );
1262 return;
1263 }
4698648f 1264
e3554471
JS
1265 HBITMAP hbitmap;
1266 hbitmap = (HBITMAP) bitmap.GetHBITMAP();
1267 HDC hdc = ::GetDC(NULL);
1268 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
4698648f 1269
e3554471
JS
1270 unsigned char *ptdata = data, *ptbits = lpBits;
1271 for( int i=0; i<width*height; i++ )
1272 {
1273 *(ptdata++) = *(ptbits+2);
1274 *(ptdata++) = *(ptbits+1);
1275 *(ptdata++) = *(ptbits );
1276 ptbits += 3;
4698648f
VZ
1277 }
1278
e3554471
JS
1279 if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() )
1280 {
1281 hbitmap = (HBITMAP) bitmap.GetMask()->GetMaskBitmap();
1282 HDC memdc = ::CreateCompatibleDC( hdc );
1283 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
1284 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
1285 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
4698648f 1286 ::DeleteDC( memdc );
e3554471
JS
1287 unsigned char r=16, g=16, b=16; // background set to RGB(16,16,16)
1288 ptdata = data;
1289 ptbits = lpBits;
1290 for( int i=0; i<width*height; i++ )
1291 {
1292 if( *ptbits != 0 )
1293 {
1294 *(ptdata++) = r;
1295 *(ptdata++) = g;
1296 *(ptdata++) = b;
1297 }
1298 ptbits += 3;
4698648f 1299 }
e3554471 1300 SetMaskColour( r, g, b );
4698648f
VZ
1301 }
1302
1303 ::ReleaseDC(NULL, hdc);
e3554471
JS
1304 free(lpDIBh);
1305 free(lpBits);
1306}
1307
1308#endif
1309
99c67c77
RR
1310#ifdef __WXGTK__
1311
83624f79
RR
1312#include "gtk/gtk.h"
1313#include "gdk/gdk.h"
1314#include "gdk/gdkx.h"
1315
99c67c77
RR
1316wxBitmap wxImage::ConvertToBitmap() const
1317{
1318 wxBitmap bitmap;
1319
1320 wxCHECK_MSG( Ok(), bitmap, "invalid image" );
1321
1322 int width = GetWidth();
1323 int height = GetHeight();
1324
1325 bitmap.SetHeight( height );
1326 bitmap.SetWidth( width );
1327
1328 // Create picture
1329
1330 GdkImage *data_image =
1331 gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), width, height );
1332
1333 bitmap.SetPixmap( gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, -1 ) );
1334
1335 // Create mask
1336
1337 GdkImage *mask_image = (GdkImage*) NULL;
1338
1339 if (HasMask())
1340 {
1341 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1342
1343 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1344
4698648f
VZ
1345 wxMask *mask = new wxMask();
1346 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
99c67c77 1347
4698648f 1348 bitmap.SetMask( mask );
99c67c77
RR
1349 }
1350
1351 // Retrieve depth
1352
1353 GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
1354 if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
1355 int bpp = visual->depth;
4698648f 1356
06cfab17 1357 bitmap.SetDepth( bpp );
4698648f 1358
99c67c77
RR
1359 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
1360 if (bpp < 8) bpp = 8;
1361
1362 // Render
1363
1364 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
1365 byte_order b_o = RGB;
1366
1367 if (bpp >= 24)
1368 {
1369 GdkVisual *visual = gdk_visual_get_system();
1370 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
1371 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RGB;
1372 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
1373 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
1374 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
1375 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
1376 }
1377
1378 int r_mask = GetMaskRed();
1379 int g_mask = GetMaskGreen();
1380 int b_mask = GetMaskBlue();
1381
1382 unsigned char* data = GetData();
1383
1384 int index = 0;
1385 for (int y = 0; y < height; y++)
1386 {
1387 for (int x = 0; x < width; x++)
1388 {
1389 int r = data[index];
4698648f 1390 index++;
99c67c77 1391 int g = data[index];
4698648f 1392 index++;
99c67c77 1393 int b = data[index];
4698648f
VZ
1394 index++;
1395
1396 if (HasMask())
1397 {
1398 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1399 gdk_image_put_pixel( mask_image, x, y, 1 );
1400 else
1401 gdk_image_put_pixel( mask_image, x, y, 0 );
1402 }
1403
1404 if (HasMask())
1405 {
1406 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1407 gdk_image_put_pixel( mask_image, x, y, 1 );
1408 else
1409 gdk_image_put_pixel( mask_image, x, y, 0 );
1410 }
1411
1412 switch (bpp)
1413 {
1414 case 8:
1415 {
f6fcbb63 1416 int pixel = -1;
4698648f
VZ
1417 if (wxTheApp->m_colorCube)
1418 {
1419 pixel = wxTheApp->m_colorCube
1420 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1421 }
f6fcbb63 1422 else
4698648f
VZ
1423 {
1424 GdkColormap *cmap = gtk_widget_get_default_colormap();
f6fcbb63
RR
1425 GdkColor *colors = cmap->colors;
1426 int max = 3 * (65536);
1427
1428 for (int i = 0; i < cmap->size; i++)
1429 {
1430 int rdiff = (r << 8) - colors[i].red;
1431 int gdiff = (g << 8) - colors[i].green;
1432 int bdiff = (b << 8) - colors[i].blue;
1433 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
1434 if (sum < max) { pixel = i; max = sum; }
4698648f 1435 }
99c67c77
RR
1436 }
1437
4698648f
VZ
1438 gdk_image_put_pixel( data_image, x, y, pixel );
1439
1440 break;
1441 }
1442 case 15:
1443 {
1444 guint32 pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3);
1445 gdk_image_put_pixel( data_image, x, y, pixel );
1446 break;
1447 }
1448 case 16:
1449 {
1450 guint32 pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3);
1451 gdk_image_put_pixel( data_image, x, y, pixel );
1452 break;
1453 }
1454 case 32:
1455 case 24:
1456 {
1457 guint32 pixel = 0;
1458 switch (b_o)
1459 {
1460 case RGB: pixel = (r << 16) | (g << 8) | b; break;
1461 case RBG: pixel = (r << 16) | (b << 8) | g; break;
1462 case BRG: pixel = (b << 16) | (r << 8) | g; break;
1463 case BGR: pixel = (b << 16) | (g << 8) | r; break;
1464 case GRB: pixel = (g << 16) | (r << 8) | b; break;
1465 case GBR: pixel = (g << 16) | (b << 8) | r; break;
1466 }
1467 gdk_image_put_pixel( data_image, x, y, pixel );
1468 }
1469 default: break;
1470 }
99c67c77
RR
1471 } // for
1472 } // for
1473
1474 // Blit picture
1475
1476 GdkGC *data_gc = gdk_gc_new( bitmap.GetPixmap() );
1477
1478 gdk_draw_image( bitmap.GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
1479
1480 gdk_image_destroy( data_image );
1481 gdk_gc_unref( data_gc );
1482
1483 // Blit mask
1484
1485 if (HasMask())
1486 {
1487 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1488
1489 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1490
1491 gdk_image_destroy( mask_image );
1492 gdk_gc_unref( mask_gc );
1493 }
1494
1495 return bitmap;
1496}
1497
1498wxImage::wxImage( const wxBitmap &bitmap )
1499{
1500 wxCHECK_RET( bitmap.Ok(), "invalid bitmap" );
1501
1502 GdkImage *gdk_image = gdk_image_get( bitmap.GetPixmap(),
1503 0, 0,
4698648f 1504 bitmap.GetWidth(), bitmap.GetHeight() );
99c67c77
RR
1505
1506 wxCHECK_RET( gdk_image, "couldn't create image" );
1507
1508 Create( bitmap.GetWidth(), bitmap.GetHeight() );
1509 char unsigned *data = GetData();
1510
1511 if (!data)
1512 {
1513 gdk_image_destroy( gdk_image );
1514 wxFAIL_MSG( "couldn't create image" );
4698648f 1515 return;
99c67c77
RR
1516 }
1517
1518 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1519 if (bitmap.GetMask())
1520 {
1521 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
4698648f
VZ
1522 0, 0,
1523 bitmap.GetWidth(), bitmap.GetHeight() );
99c67c77 1524
4698648f 1525 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
99c67c77
RR
1526 }
1527
1528 GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
1529 if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
1530 int bpp = visual->depth;
1531 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
1532
1533 GdkColormap *cmap = gtk_widget_get_default_colormap();
1534
1535 long pos = 0;
1536 for (int j = 0; j < bitmap.GetHeight(); j++)
1537 {
1538 for (int i = 0; i < bitmap.GetWidth(); i++)
1539 {
1540 int pixel = gdk_image_get_pixel( gdk_image, i, j );
1541 if (bpp <= 8)
1542 {
1543 data[pos] = cmap->colors[pixel].red >> 8;
1544 data[pos+1] = cmap->colors[pixel].green >> 8;
1545 data[pos+2] = cmap->colors[pixel].blue >> 8;
1546 } else if (bpp == 15)
1547 {
1548 data[pos] = (pixel >> 7) & 0xf8;
1549 data[pos+1] = (pixel >> 2) & 0xf8;
1550 data[pos+2] = (pixel << 3) & 0xf8;
1551 } else if (bpp == 16)
1552 {
1553 data[pos] = (pixel >> 8) & 0xf8;
1554 data[pos+1] = (pixel >> 3) & 0xfc;
1555 data[pos+2] = (pixel << 3) & 0xf8;
1556 } else
1557 {
1558 data[pos] = (pixel >> 16) & 0xff;
1559 data[pos+1] = (pixel >> 8) & 0xff;
1560 data[pos+2] = pixel & 0xff;
1561 }
1562
4698648f
VZ
1563 if (gdk_image_mask)
1564 {
1565 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1566 if (mask_pixel == 0)
1567 {
99c67c77
RR
1568 data[pos] = 16;
1569 data[pos+1] = 16;
1570 data[pos+2] = 16;
4698648f
VZ
1571 }
1572 }
99c67c77
RR
1573
1574 pos += 3;
1575 }
1576 }
1577
1578 gdk_image_destroy( gdk_image );
1579 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1580}
1581
1582#endif
ee4c6942
JS
1583
1584// TODO
1585
1586#ifdef __WXMOTIF__
1587wxBitmap wxImage::ConvertToBitmap() const
1588{
1589 wxFAIL_MSG("Sorry, wxImage::ConvertToBitmap isn't implemented for wxMotif yet.");
1590 return wxNullBitmap;
1591}
1592
1593wxImage::wxImage( const wxBitmap &bitmap )
1594{
1595 wxFAIL_MSG("Sorry, wxImage::wxImage(const wxBitmap&) isn't implemented for wxMotif yet.");
1596}
1597#endif