added wxImage::ConvertAlphaToMask
[wxWidgets.git] / src / common / image.cpp
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "image.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #include "wx/defs.h"
22
23 #if wxUSE_IMAGE
24
25 #include "wx/image.h"
26 #include "wx/bitmap.h"
27 #include "wx/debug.h"
28 #include "wx/log.h"
29 #include "wx/app.h"
30 #include "wx/filefn.h"
31 #include "wx/wfstream.h"
32 #include "wx/intl.h"
33 #include "wx/module.h"
34 #include "wx/hash.h"
35 #include "wx/utils.h"
36
37 // For memcpy
38 #include <string.h>
39 #include <math.h>
40
41 #ifdef __SALFORDC__
42 #undef FAR
43 #endif
44
45
46 //-----------------------------------------------------------------------------
47 // wxImage
48 //-----------------------------------------------------------------------------
49
50 class wxImageRefData: public wxObjectRefData
51 {
52 public:
53 wxImageRefData();
54 virtual ~wxImageRefData();
55
56 int m_width;
57 int m_height;
58 unsigned char *m_data;
59
60 bool m_hasMask;
61 unsigned char m_maskRed,m_maskGreen,m_maskBlue;
62
63 // alpha channel data, may be NULL for the formats without alpha support
64 unsigned char *m_alpha;
65
66 bool m_ok;
67 bool m_static;
68
69 #if wxUSE_PALETTE
70 wxPalette m_palette;
71 #endif // wxUSE_PALETTE
72
73 wxArrayString m_optionNames;
74 wxArrayString m_optionValues;
75
76 DECLARE_NO_COPY_CLASS(wxImageRefData)
77 };
78
79 wxImageRefData::wxImageRefData()
80 {
81 m_width = 0;
82 m_height = 0;
83 m_data =
84 m_alpha = (unsigned char *) NULL;
85
86 m_maskRed = 0;
87 m_maskGreen = 0;
88 m_maskBlue = 0;
89 m_hasMask = false;
90
91 m_ok = false;
92 m_static = false;
93 }
94
95 wxImageRefData::~wxImageRefData()
96 {
97 if ( !m_static )
98 free( m_data );
99
100 free(m_alpha);
101 }
102
103 wxList wxImage::sm_handlers;
104
105 wxImage wxNullImage;
106
107 //-----------------------------------------------------------------------------
108
109 #define M_IMGDATA ((wxImageRefData *)m_refData)
110
111 IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject)
112
113 wxImage::wxImage()
114 {
115 }
116
117 wxImage::wxImage( int width, int height, bool clear )
118 {
119 Create( width, height, clear );
120 }
121
122 wxImage::wxImage( int width, int height, unsigned char* data, bool static_data )
123 {
124 Create( width, height, data, static_data );
125 }
126
127 wxImage::wxImage( const wxString& name, long type, int index )
128 {
129 LoadFile( name, type, index );
130 }
131
132 wxImage::wxImage( const wxString& name, const wxString& mimetype, int index )
133 {
134 LoadFile( name, mimetype, index );
135 }
136
137 #if wxUSE_STREAMS
138 wxImage::wxImage( wxInputStream& stream, long type, int index )
139 {
140 LoadFile( stream, type, index );
141 }
142
143 wxImage::wxImage( wxInputStream& stream, const wxString& mimetype, int index )
144 {
145 LoadFile( stream, mimetype, index );
146 }
147 #endif // wxUSE_STREAMS
148
149 wxImage::wxImage( const wxImage& image )
150 : wxObject()
151 {
152 Ref(image);
153 }
154
155 wxImage::wxImage( const wxImage* image )
156 {
157 if (image) Ref(*image);
158 }
159
160 bool wxImage::Create( int width, int height, bool clear )
161 {
162 UnRef();
163
164 m_refData = new wxImageRefData();
165
166 M_IMGDATA->m_data = (unsigned char *) malloc( width*height*3 );
167 if (!M_IMGDATA->m_data)
168 {
169 UnRef();
170 return false;
171 }
172
173 if (clear)
174 memset(M_IMGDATA->m_data, 0, width*height*3);
175
176 M_IMGDATA->m_width = width;
177 M_IMGDATA->m_height = height;
178 M_IMGDATA->m_ok = true;
179
180 return true;
181 }
182
183 bool wxImage::Create( int width, int height, unsigned char* data, bool static_data )
184 {
185 UnRef();
186
187 wxCHECK_MSG( data, false, _T("NULL data in wxImage::Create") );
188
189 m_refData = new wxImageRefData();
190
191 M_IMGDATA->m_data = data;
192 M_IMGDATA->m_width = width;
193 M_IMGDATA->m_height = height;
194 M_IMGDATA->m_ok = true;
195 M_IMGDATA->m_static = static_data;
196
197 return true;
198 }
199
200 void wxImage::Destroy()
201 {
202 UnRef();
203 }
204
205 wxImage wxImage::Copy() const
206 {
207 wxImage image;
208
209 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
210
211 image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false );
212
213 unsigned char *data = image.GetData();
214
215 wxCHECK_MSG( data, image, wxT("unable to create image") );
216
217 image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
218 image.SetMask( M_IMGDATA->m_hasMask );
219
220 memcpy( data, GetData(), M_IMGDATA->m_width*M_IMGDATA->m_height*3 );
221
222 // also copy the image options
223 wxImageRefData *imgData = (wxImageRefData *)image.m_refData;
224 imgData->m_optionNames = M_IMGDATA->m_optionNames;
225 imgData->m_optionValues = M_IMGDATA->m_optionValues;
226
227 return image;
228 }
229
230 wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const
231 {
232 if( xFactor == 1 && yFactor == 1 )
233 return Copy() ;
234
235 wxImage image;
236
237 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
238
239 // can't scale to/from 0 size
240 wxCHECK_MSG( (xFactor > 0) && (yFactor > 0), image,
241 wxT("invalid new image size") );
242
243 long old_height = M_IMGDATA->m_height,
244 old_width = M_IMGDATA->m_width;
245
246 wxCHECK_MSG( (old_height > 0) && (old_width > 0), image,
247 wxT("invalid old image size") );
248
249 long width = old_width / xFactor ;
250 long height = old_height / yFactor ;
251
252 image.Create( width, height, false );
253
254 char unsigned *data = image.GetData();
255
256 wxCHECK_MSG( data, image, wxT("unable to create image") );
257
258 bool hasMask = false ;
259 unsigned char maskRed = 0;
260 unsigned char maskGreen = 0;
261 unsigned char maskBlue =0 ;
262 if (M_IMGDATA->m_hasMask)
263 {
264 hasMask = true ;
265 maskRed = M_IMGDATA->m_maskRed;
266 maskGreen = M_IMGDATA->m_maskGreen;
267 maskBlue =M_IMGDATA->m_maskBlue ;
268
269 image.SetMaskColour( M_IMGDATA->m_maskRed,
270 M_IMGDATA->m_maskGreen,
271 M_IMGDATA->m_maskBlue );
272 }
273 char unsigned *source_data = M_IMGDATA->m_data;
274 char unsigned *target_data = data;
275
276 for (long y = 0; y < height; y++)
277 {
278 for (long x = 0; x < width; x++)
279 {
280 unsigned long avgRed = 0 ;
281 unsigned long avgGreen = 0;
282 unsigned long avgBlue = 0;
283 unsigned long counter = 0 ;
284 // determine average
285 for ( int y1 = 0 ; y1 < yFactor ; ++y1 )
286 {
287 long y_offset = (y * yFactor + y1) * old_width;
288 for ( int x1 = 0 ; x1 < xFactor ; ++x1 )
289 {
290 unsigned char *pixel = source_data + 3 * ( y_offset + x * xFactor + x1 ) ;
291 unsigned char red = pixel[0] ;
292 unsigned char green = pixel[1] ;
293 unsigned char blue = pixel[2] ;
294 if ( !hasMask || red != maskRed || green != maskGreen || blue != maskBlue )
295 {
296 avgRed += red ;
297 avgGreen += green ;
298 avgBlue += blue ;
299 counter++ ;
300 }
301 }
302 }
303 if ( counter == 0 )
304 {
305 *(target_data++) = M_IMGDATA->m_maskRed ;
306 *(target_data++) = M_IMGDATA->m_maskGreen ;
307 *(target_data++) = M_IMGDATA->m_maskBlue ;
308 }
309 else
310 {
311 *(target_data++) = (unsigned char)(avgRed / counter);
312 *(target_data++) = (unsigned char)(avgGreen / counter);
313 *(target_data++) = (unsigned char)(avgBlue / counter);
314 }
315 }
316 }
317
318 // In case this is a cursor, make sure the hotspot is scalled accordingly:
319 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) )
320 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X,
321 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X))/xFactor);
322 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) )
323 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y,
324 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y))/yFactor);
325
326 return image;
327 }
328
329 wxImage wxImage::Scale( int width, int height ) const
330 {
331 wxImage image;
332
333 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
334
335 // can't scale to/from 0 size
336 wxCHECK_MSG( (width > 0) && (height > 0), image,
337 wxT("invalid new image size") );
338
339 long old_height = M_IMGDATA->m_height,
340 old_width = M_IMGDATA->m_width;
341 wxCHECK_MSG( (old_height > 0) && (old_width > 0), image,
342 wxT("invalid old image size") );
343
344 if ( old_width % width == 0 && old_width >= width &&
345 old_height % height == 0 && old_height >= height )
346 {
347 return ShrinkBy( old_width / width , old_height / height ) ;
348 }
349 image.Create( width, height, false );
350
351 unsigned char *data = image.GetData();
352
353 wxCHECK_MSG( data, image, wxT("unable to create image") );
354
355 if (M_IMGDATA->m_hasMask)
356 {
357 image.SetMaskColour( M_IMGDATA->m_maskRed,
358 M_IMGDATA->m_maskGreen,
359 M_IMGDATA->m_maskBlue );
360 }
361
362 unsigned char *source_data = M_IMGDATA->m_data;
363 unsigned char *target_data = data;
364
365 long x_delta = (old_width<<16) / width;
366 long y_delta = (old_height<<16) / height;
367
368 unsigned char* dest_pixel = target_data;
369
370 long y = 0;
371 for ( long j = 0; j < height; j++ )
372 {
373 unsigned char* src_line = &source_data[(y>>16)*old_width*3];
374
375 long x = 0;
376 for ( long i = 0; i < width; i++ )
377 {
378 unsigned char* src_pixel = &src_line[(x>>16)*3];
379 dest_pixel[0] = src_pixel[0];
380 dest_pixel[1] = src_pixel[1];
381 dest_pixel[2] = src_pixel[2];
382 dest_pixel += 3;
383 x += x_delta;
384 }
385
386 y += y_delta;
387 }
388
389 // In case this is a cursor, make sure the hotspot is scalled accordingly:
390 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) )
391 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X,
392 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X)*width)/old_width);
393 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) )
394 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y,
395 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y)*height)/old_height);
396
397 return image;
398 }
399
400 wxImage wxImage::Rotate90( bool clockwise ) const
401 {
402 wxImage image;
403
404 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
405
406 image.Create( M_IMGDATA->m_height, M_IMGDATA->m_width, false );
407
408 unsigned char *data = image.GetData();
409
410 wxCHECK_MSG( data, image, wxT("unable to create image") );
411
412 if (M_IMGDATA->m_hasMask)
413 image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
414
415 long height = M_IMGDATA->m_height;
416 long width = M_IMGDATA->m_width;
417
418 unsigned char *source_data = M_IMGDATA->m_data;
419 unsigned char *target_data;
420
421 for (long j = 0; j < height; j++)
422 {
423 for (long i = 0; i < width; i++)
424 {
425 if (clockwise)
426 target_data = data + (((i+1)*height) - j - 1)*3;
427 else
428 target_data = data + ((height*(width-1)) + j - (i*height))*3;
429 memcpy( target_data, source_data, 3 );
430 source_data += 3;
431 }
432 }
433
434 return image;
435 }
436
437 wxImage wxImage::Mirror( bool horizontally ) const
438 {
439 wxImage image;
440
441 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
442
443 image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false );
444
445 unsigned char *data = image.GetData();
446
447 wxCHECK_MSG( data, image, wxT("unable to create image") );
448
449 if (M_IMGDATA->m_hasMask)
450 image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
451
452 long height = M_IMGDATA->m_height;
453 long width = M_IMGDATA->m_width;
454
455 unsigned char *source_data = M_IMGDATA->m_data;
456 unsigned char *target_data;
457
458 if (horizontally)
459 {
460 for (long j = 0; j < height; j++)
461 {
462 data += width*3;
463 target_data = data-3;
464 for (long i = 0; i < width; i++)
465 {
466 memcpy( target_data, source_data, 3 );
467 source_data += 3;
468 target_data -= 3;
469 }
470 }
471 }
472 else
473 {
474 for (long i = 0; i < height; i++)
475 {
476 target_data = data + 3*width*(height-1-i);
477 memcpy( target_data, source_data, (size_t)3*width );
478 source_data += 3*width;
479 }
480 }
481
482 return image;
483 }
484
485 wxImage wxImage::GetSubImage( const wxRect &rect ) const
486 {
487 wxImage image;
488
489 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
490
491 wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) && (rect.GetRight()<=GetWidth()) && (rect.GetBottom()<=GetHeight()),
492 image, wxT("invalid subimage size") );
493
494 int subwidth=rect.GetWidth();
495 const int subheight=rect.GetHeight();
496
497 image.Create( subwidth, subheight, false );
498
499 unsigned char *subdata = image.GetData(), *data=GetData();
500
501 wxCHECK_MSG( subdata, image, wxT("unable to create image") );
502
503 if (M_IMGDATA->m_hasMask)
504 image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
505
506 const int subleft=3*rect.GetLeft();
507 const int width=3*GetWidth();
508 subwidth*=3;
509
510 data+=rect.GetTop()*width+subleft;
511
512 for (long j = 0; j < subheight; ++j)
513 {
514 memcpy( subdata, data, subwidth);
515 subdata+=subwidth;
516 data+=width;
517 }
518
519 return image;
520 }
521
522 void wxImage::Paste( const wxImage &image, int x, int y )
523 {
524 wxCHECK_RET( Ok(), wxT("invalid image") );
525 wxCHECK_RET( image.Ok(), wxT("invalid image") );
526
527 int xx = 0;
528 int yy = 0;
529 int width = image.GetWidth();
530 int height = image.GetHeight();
531
532 if (x < 0)
533 {
534 xx = -x;
535 width += x;
536 }
537 if (y < 0)
538 {
539 yy = -y;
540 height += y;
541 }
542
543 if ((x+xx)+width > M_IMGDATA->m_width)
544 width = M_IMGDATA->m_width - (x+xx);
545 if ((y+yy)+height > M_IMGDATA->m_height)
546 height = M_IMGDATA->m_height - (y+yy);
547
548 if (width < 1) return;
549 if (height < 1) return;
550
551 if ((!HasMask() && !image.HasMask()) ||
552 ((HasMask() && image.HasMask() &&
553 (GetMaskRed()==image.GetMaskRed()) &&
554 (GetMaskGreen()==image.GetMaskGreen()) &&
555 (GetMaskBlue()==image.GetMaskBlue()))))
556 {
557 width *= 3;
558 unsigned char* source_data = image.GetData() + xx*3 + yy*3*image.GetWidth();
559 int source_step = image.GetWidth()*3;
560
561 unsigned char* target_data = GetData() + (x+xx)*3 + (y+yy)*3*M_IMGDATA->m_width;
562 int target_step = M_IMGDATA->m_width*3;
563 for (int j = 0; j < height; j++)
564 {
565 memcpy( target_data, source_data, width );
566 source_data += source_step;
567 target_data += target_step;
568 }
569 return;
570 }
571
572 if (!HasMask() && image.HasMask())
573 {
574 unsigned char r = image.GetMaskRed();
575 unsigned char g = image.GetMaskGreen();
576 unsigned char b = image.GetMaskBlue();
577
578 width *= 3;
579 unsigned char* source_data = image.GetData() + xx*3 + yy*3*image.GetWidth();
580 int source_step = image.GetWidth()*3;
581
582 unsigned char* target_data = GetData() + (x+xx)*3 + (y+yy)*3*M_IMGDATA->m_width;
583 int target_step = M_IMGDATA->m_width*3;
584
585 for (int j = 0; j < height; j++)
586 {
587 for (int i = 0; i < width; i+=3)
588 {
589 if ((source_data[i] != r) &&
590 (source_data[i+1] != g) &&
591 (source_data[i+2] != b))
592 {
593 memcpy( target_data+i, source_data+i, 3 );
594 }
595 }
596 source_data += source_step;
597 target_data += target_step;
598 }
599 }
600 }
601
602 void wxImage::Replace( unsigned char r1, unsigned char g1, unsigned char b1,
603 unsigned char r2, unsigned char g2, unsigned char b2 )
604 {
605 wxCHECK_RET( Ok(), wxT("invalid image") );
606
607 unsigned char *data = GetData();
608
609 const int w = GetWidth();
610 const int h = GetHeight();
611
612 for (int j = 0; j < h; j++)
613 for (int i = 0; i < w; i++)
614 {
615 if ((data[0] == r1) && (data[1] == g1) && (data[2] == b1))
616 {
617 data[0] = r2;
618 data[1] = g2;
619 data[2] = b2;
620 }
621 data += 3;
622 }
623 }
624
625 wxImage wxImage::ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const
626 {
627 wxImage image;
628
629 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
630
631 image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false );
632
633 unsigned char *data = image.GetData();
634
635 wxCHECK_MSG( data, image, wxT("unable to create image") );
636
637 if (M_IMGDATA->m_hasMask)
638 {
639 if (M_IMGDATA->m_maskRed == r && M_IMGDATA->m_maskGreen == g &&
640 M_IMGDATA->m_maskBlue == b)
641 image.SetMaskColour( 255, 255, 255 );
642 else
643 image.SetMaskColour( 0, 0, 0 );
644 }
645
646 long size = M_IMGDATA->m_height * M_IMGDATA->m_width;
647
648 unsigned char *srcd = M_IMGDATA->m_data;
649 unsigned char *tard = image.GetData();
650
651 for ( long i = 0; i < size; i++, srcd += 3, tard += 3 )
652 {
653 if (srcd[0] == r && srcd[1] == g && srcd[2] == b)
654 tard[0] = tard[1] = tard[2] = 255;
655 else
656 tard[0] = tard[1] = tard[2] = 0;
657 }
658
659 return image;
660 }
661
662 void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b )
663 {
664 wxCHECK_RET( Ok(), wxT("invalid image") );
665
666 int w = M_IMGDATA->m_width;
667 int h = M_IMGDATA->m_height;
668
669 wxCHECK_RET( (x>=0) && (y>=0) && (x<w) && (y<h), wxT("invalid image index") );
670
671 long pos = (y * w + x) * 3;
672
673 M_IMGDATA->m_data[ pos ] = r;
674 M_IMGDATA->m_data[ pos+1 ] = g;
675 M_IMGDATA->m_data[ pos+2 ] = b;
676 }
677
678 unsigned char wxImage::GetRed( int x, int y ) const
679 {
680 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
681
682 int w = M_IMGDATA->m_width;
683 int h = M_IMGDATA->m_height;
684
685 wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, wxT("invalid image index") );
686
687 long pos = (y * w + x) * 3;
688
689 return M_IMGDATA->m_data[pos];
690 }
691
692 unsigned char wxImage::GetGreen( int x, int y ) const
693 {
694 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
695
696 int w = M_IMGDATA->m_width;
697 int h = M_IMGDATA->m_height;
698
699 wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, wxT("invalid image index") );
700
701 long pos = (y * w + x) * 3;
702
703 return M_IMGDATA->m_data[pos+1];
704 }
705
706 unsigned char wxImage::GetBlue( int x, int y ) const
707 {
708 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
709
710 int w = M_IMGDATA->m_width;
711 int h = M_IMGDATA->m_height;
712
713 wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, wxT("invalid image index") );
714
715 long pos = (y * w + x) * 3;
716
717 return M_IMGDATA->m_data[pos+2];
718 }
719
720 bool wxImage::Ok() const
721 {
722 // image of 0 width or height can't be considered ok - at least because it
723 // causes crashes in ConvertToBitmap() if we don't catch it in time
724 wxImageRefData *data = M_IMGDATA;
725 return data && data->m_ok && data->m_width && data->m_height;
726 }
727
728 unsigned char *wxImage::GetData() const
729 {
730 wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") );
731
732 return M_IMGDATA->m_data;
733 }
734
735 void wxImage::SetData( unsigned char *data )
736 {
737 wxCHECK_RET( Ok(), wxT("invalid image") );
738
739 wxImageRefData *newRefData = new wxImageRefData();
740
741 newRefData->m_width = M_IMGDATA->m_width;
742 newRefData->m_height = M_IMGDATA->m_height;
743 newRefData->m_data = data;
744 newRefData->m_ok = true;
745 newRefData->m_maskRed = M_IMGDATA->m_maskRed;
746 newRefData->m_maskGreen = M_IMGDATA->m_maskGreen;
747 newRefData->m_maskBlue = M_IMGDATA->m_maskBlue;
748 newRefData->m_hasMask = M_IMGDATA->m_hasMask;
749
750 UnRef();
751
752 m_refData = newRefData;
753 }
754
755 void wxImage::SetData( unsigned char *data, int new_width, int new_height )
756 {
757 wxImageRefData *newRefData = new wxImageRefData();
758
759 if (m_refData)
760 {
761 newRefData->m_width = new_width;
762 newRefData->m_height = new_height;
763 newRefData->m_data = data;
764 newRefData->m_ok = true;
765 newRefData->m_maskRed = M_IMGDATA->m_maskRed;
766 newRefData->m_maskGreen = M_IMGDATA->m_maskGreen;
767 newRefData->m_maskBlue = M_IMGDATA->m_maskBlue;
768 newRefData->m_hasMask = M_IMGDATA->m_hasMask;
769 }
770 else
771 {
772 newRefData->m_width = new_width;
773 newRefData->m_height = new_height;
774 newRefData->m_data = data;
775 newRefData->m_ok = true;
776 }
777
778 UnRef();
779
780 m_refData = newRefData;
781 }
782
783 // ----------------------------------------------------------------------------
784 // alpha channel support
785 // ----------------------------------------------------------------------------
786
787 void wxImage::SetAlpha(int x, int y, unsigned char alpha)
788 {
789 wxCHECK_RET( Ok() && HasAlpha(), wxT("invalid image or no alpha channel") );
790
791 int w = M_IMGDATA->m_width,
792 h = M_IMGDATA->m_height;
793
794 wxCHECK_RET( x >=0 && y >= 0 && x < w && y < h, wxT("invalid image index") );
795
796 M_IMGDATA->m_alpha[y*w + x] = alpha;
797 }
798
799 unsigned char wxImage::GetAlpha(int x, int y) const
800 {
801 wxCHECK_MSG( Ok() && HasAlpha(), 0, wxT("invalid image or no alpha channel") );
802
803 int w = M_IMGDATA->m_width,
804 h = M_IMGDATA->m_height;
805
806 wxCHECK_MSG( x >=0 && y >= 0 && x < w && y < h, 0, wxT("invalid image index") );
807
808 return M_IMGDATA->m_alpha[y*w + x];
809 }
810
811 void wxImage::SetAlpha( unsigned char *alpha )
812 {
813 wxCHECK_RET( Ok(), wxT("invalid image") );
814
815 if ( !alpha )
816 {
817 alpha = (unsigned char *)malloc(M_IMGDATA->m_width*M_IMGDATA->m_height);
818 }
819
820 free(M_IMGDATA->m_alpha);
821 M_IMGDATA->m_alpha = alpha;
822 }
823
824 unsigned char *wxImage::GetAlpha() const
825 {
826 wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") );
827
828 return M_IMGDATA->m_alpha;
829 }
830
831 // ----------------------------------------------------------------------------
832 // mask support
833 // ----------------------------------------------------------------------------
834
835 void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b )
836 {
837 wxCHECK_RET( Ok(), wxT("invalid image") );
838
839 M_IMGDATA->m_maskRed = r;
840 M_IMGDATA->m_maskGreen = g;
841 M_IMGDATA->m_maskBlue = b;
842 M_IMGDATA->m_hasMask = true;
843 }
844
845 unsigned char wxImage::GetMaskRed() const
846 {
847 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
848
849 return M_IMGDATA->m_maskRed;
850 }
851
852 unsigned char wxImage::GetMaskGreen() const
853 {
854 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
855
856 return M_IMGDATA->m_maskGreen;
857 }
858
859 unsigned char wxImage::GetMaskBlue() const
860 {
861 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
862
863 return M_IMGDATA->m_maskBlue;
864 }
865
866 void wxImage::SetMask( bool mask )
867 {
868 wxCHECK_RET( Ok(), wxT("invalid image") );
869
870 M_IMGDATA->m_hasMask = mask;
871 }
872
873 bool wxImage::HasMask() const
874 {
875 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
876
877 return M_IMGDATA->m_hasMask;
878 }
879
880 int wxImage::GetWidth() const
881 {
882 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
883
884 return M_IMGDATA->m_width;
885 }
886
887 int wxImage::GetHeight() const
888 {
889 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
890
891 return M_IMGDATA->m_height;
892 }
893
894 bool wxImage::SetMaskFromImage(const wxImage& mask,
895 unsigned char mr, unsigned char mg, unsigned char mb)
896 {
897 // check that the images are the same size
898 if ( (M_IMGDATA->m_height != mask.GetHeight() ) || (M_IMGDATA->m_width != mask.GetWidth () ) )
899 {
900 wxLogError( _("Image and mask have different sizes.") );
901 return false;
902 }
903
904 // find unused colour
905 unsigned char r,g,b ;
906 if (!FindFirstUnusedColour(&r, &g, &b))
907 {
908 wxLogError( _("No unused colour in image being masked.") );
909 return false ;
910 }
911
912 unsigned char *imgdata = GetData();
913 unsigned char *maskdata = mask.GetData();
914
915 const int w = GetWidth();
916 const int h = GetHeight();
917
918 for (int j = 0; j < h; j++)
919 {
920 for (int i = 0; i < w; i++)
921 {
922 if ((maskdata[0] == mr) && (maskdata[1] == mg) && (maskdata[2] == mb))
923 {
924 imgdata[0] = r;
925 imgdata[1] = g;
926 imgdata[2] = b;
927 }
928 imgdata += 3;
929 maskdata += 3;
930 }
931 }
932
933 SetMaskColour(r, g, b);
934 SetMask(true);
935
936 return true;
937 }
938
939 bool wxImage::ConvertAlphaToMask(unsigned threshold)
940 {
941 if (!HasAlpha())
942 return true;
943
944 unsigned char mr, mg, mb;
945 if (!FindFirstUnusedColour(&mr, &mg, &mb))
946 {
947 wxLogError( _("No unused colour in image being masked.") );
948 return false;
949 }
950
951 SetMask(true);
952 SetMaskColour(mr, mg, mb);
953
954 unsigned char *imgdata = GetData();
955 unsigned char *alphadata = GetAlpha();
956
957 size_t w = GetWidth();
958 size_t h = GetHeight();
959
960 for (size_t y = 0; y < h; y++)
961 {
962 for (size_t x = 0; x < w; x++, imgdata += 3, alphadata++)
963 {
964 if (*alphadata < threshold)
965 {
966 imgdata[0] = mr;
967 imgdata[1] = mg;
968 imgdata[2] = mb;
969 }
970 }
971 }
972
973 free(M_IMGDATA->m_alpha);
974 M_IMGDATA->m_alpha = NULL;
975 }
976
977 #if wxUSE_PALETTE
978
979 // Palette functions
980
981 bool wxImage::HasPalette() const
982 {
983 if (!Ok())
984 return false;
985
986 return M_IMGDATA->m_palette.Ok();
987 }
988
989 const wxPalette& wxImage::GetPalette() const
990 {
991 wxCHECK_MSG( Ok(), wxNullPalette, wxT("invalid image") );
992
993 return M_IMGDATA->m_palette;
994 }
995
996 void wxImage::SetPalette(const wxPalette& palette)
997 {
998 wxCHECK_RET( Ok(), wxT("invalid image") );
999
1000 M_IMGDATA->m_palette = palette;
1001 }
1002
1003 #endif // wxUSE_PALETTE
1004
1005 // Option functions (arbitrary name/value mapping)
1006 void wxImage::SetOption(const wxString& name, const wxString& value)
1007 {
1008 wxCHECK_RET( Ok(), wxT("invalid image") );
1009
1010 int idx = M_IMGDATA->m_optionNames.Index(name, false);
1011 if (idx == wxNOT_FOUND)
1012 {
1013 M_IMGDATA->m_optionNames.Add(name);
1014 M_IMGDATA->m_optionValues.Add(value);
1015 }
1016 else
1017 {
1018 M_IMGDATA->m_optionNames[idx] = name;
1019 M_IMGDATA->m_optionValues[idx] = value;
1020 }
1021 }
1022
1023 void wxImage::SetOption(const wxString& name, int value)
1024 {
1025 wxString valStr;
1026 valStr.Printf(wxT("%d"), value);
1027 SetOption(name, valStr);
1028 }
1029
1030 wxString wxImage::GetOption(const wxString& name) const
1031 {
1032 wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid image") );
1033
1034 int idx = M_IMGDATA->m_optionNames.Index(name, false);
1035 if (idx == wxNOT_FOUND)
1036 return wxEmptyString;
1037 else
1038 return M_IMGDATA->m_optionValues[idx];
1039 }
1040
1041 int wxImage::GetOptionInt(const wxString& name) const
1042 {
1043 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1044
1045 return wxAtoi(GetOption(name));
1046 }
1047
1048 bool wxImage::HasOption(const wxString& name) const
1049 {
1050 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1051
1052 return (M_IMGDATA->m_optionNames.Index(name, false) != wxNOT_FOUND);
1053 }
1054
1055 bool wxImage::LoadFile( const wxString& filename, long type, int index )
1056 {
1057 #if wxUSE_STREAMS
1058 if (wxFileExists(filename))
1059 {
1060 wxFileInputStream stream(filename);
1061 wxBufferedInputStream bstream( stream );
1062 return LoadFile(bstream, type, index);
1063 }
1064 else
1065 {
1066 wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
1067
1068 return false;
1069 }
1070 #else // !wxUSE_STREAMS
1071 return false;
1072 #endif // wxUSE_STREAMS
1073 }
1074
1075 bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype, int index )
1076 {
1077 #if wxUSE_STREAMS
1078 if (wxFileExists(filename))
1079 {
1080 wxFileInputStream stream(filename);
1081 wxBufferedInputStream bstream( stream );
1082 return LoadFile(bstream, mimetype, index);
1083 }
1084 else
1085 {
1086 wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
1087
1088 return false;
1089 }
1090 #else // !wxUSE_STREAMS
1091 return false;
1092 #endif // wxUSE_STREAMS
1093 }
1094
1095
1096
1097 bool wxImage::SaveFile( const wxString& filename ) const
1098 {
1099 wxString ext = filename.AfterLast('.').Lower();
1100
1101 wxImageHandler * pHandler = FindHandler(ext, -1);
1102 if (pHandler)
1103 {
1104 SaveFile(filename, pHandler->GetType());
1105 return true;
1106 }
1107
1108 wxLogError(_("Can't save image to file '%s': unknown extension."), filename.c_str());
1109
1110 return false;
1111 }
1112
1113 bool wxImage::SaveFile( const wxString& filename, int type ) const
1114 {
1115 #if wxUSE_STREAMS
1116 ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
1117
1118 wxFileOutputStream stream(filename);
1119
1120 if ( stream.IsOk() )
1121 {
1122 wxBufferedOutputStream bstream( stream );
1123 return SaveFile(bstream, type);
1124 }
1125 #endif // wxUSE_STREAMS
1126
1127 return false;
1128 }
1129
1130 bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) const
1131 {
1132 #if wxUSE_STREAMS
1133 ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
1134
1135 wxFileOutputStream stream(filename);
1136
1137 if ( stream.IsOk() )
1138 {
1139 wxBufferedOutputStream bstream( stream );
1140 return SaveFile(bstream, mimetype);
1141 }
1142 #endif // wxUSE_STREAMS
1143
1144 return false;
1145 }
1146
1147 bool wxImage::CanRead( const wxString &name )
1148 {
1149 #if wxUSE_STREAMS
1150 wxFileInputStream stream(name);
1151 return CanRead(stream);
1152 #else
1153 return false;
1154 #endif
1155 }
1156
1157 int wxImage::GetImageCount( const wxString &name, long type )
1158 {
1159 #if wxUSE_STREAMS
1160 wxFileInputStream stream(name);
1161 if (stream.Ok())
1162 return GetImageCount(stream, type);
1163 #endif
1164
1165 return 0;
1166 }
1167
1168 #if wxUSE_STREAMS
1169
1170 bool wxImage::CanRead( wxInputStream &stream )
1171 {
1172 const wxList& list = GetHandlers();
1173
1174 for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() )
1175 {
1176 wxImageHandler *handler=(wxImageHandler*)node->GetData();
1177 if (handler->CanRead( stream ))
1178 return true;
1179 }
1180
1181 return false;
1182 }
1183
1184 int wxImage::GetImageCount( wxInputStream &stream, long type )
1185 {
1186 wxImageHandler *handler;
1187
1188 if ( type == wxBITMAP_TYPE_ANY )
1189 {
1190 wxList &list=GetHandlers();
1191
1192 for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext())
1193 {
1194 handler=(wxImageHandler*)node->GetData();
1195 if ( handler->CanRead(stream) )
1196 return handler->GetImageCount(stream);
1197
1198 }
1199
1200 wxLogWarning(_("No handler found for image type."));
1201 return 0;
1202 }
1203
1204 handler = FindHandler(type);
1205
1206 if ( !handler )
1207 {
1208 wxLogWarning(_("No image handler for type %d defined."), type);
1209 return false;
1210 }
1211
1212 if ( handler->CanRead(stream) )
1213 {
1214 return handler->GetImageCount(stream);
1215 }
1216 else
1217 {
1218 wxLogError(_("Image file is not of type %d."), type);
1219 return 0;
1220 }
1221 }
1222
1223 bool wxImage::LoadFile( wxInputStream& stream, long type, int index )
1224 {
1225 UnRef();
1226
1227 m_refData = new wxImageRefData;
1228
1229 wxImageHandler *handler;
1230
1231 if ( type == wxBITMAP_TYPE_ANY )
1232 {
1233 wxList &list=GetHandlers();
1234
1235 for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() )
1236 {
1237 handler=(wxImageHandler*)node->GetData();
1238 if ( handler->CanRead(stream) )
1239 return handler->LoadFile(this, stream, true/*verbose*/, index);
1240
1241 }
1242
1243 wxLogWarning( _("No handler found for image type.") );
1244 return false;
1245 }
1246
1247 handler = FindHandler(type);
1248
1249 if (handler == 0)
1250 {
1251 wxLogWarning( _("No image handler for type %d defined."), type );
1252
1253 return false;
1254 }
1255
1256 return handler->LoadFile(this, stream, true/*verbose*/, index);
1257 }
1258
1259 bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int index )
1260 {
1261 UnRef();
1262
1263 m_refData = new wxImageRefData;
1264
1265 wxImageHandler *handler = FindHandlerMime(mimetype);
1266
1267 if (handler == 0)
1268 {
1269 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
1270
1271 return false;
1272 }
1273
1274 return handler->LoadFile( this, stream, true/*verbose*/, index );
1275 }
1276
1277 bool wxImage::SaveFile( wxOutputStream& stream, int type ) const
1278 {
1279 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1280
1281 wxImageHandler *handler = FindHandler(type);
1282
1283 if (handler == 0)
1284 {
1285 wxLogWarning( _("No image handler for type %d defined."), type );
1286
1287 return false;
1288 }
1289
1290 return handler->SaveFile( (wxImage*)this, stream );
1291 }
1292
1293 bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const
1294 {
1295 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1296
1297 wxImageHandler *handler = FindHandlerMime(mimetype);
1298
1299 if (handler == 0)
1300 {
1301 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
1302
1303 return false;
1304 }
1305
1306 return handler->SaveFile( (wxImage*)this, stream );
1307 }
1308 #endif // wxUSE_STREAMS
1309
1310 void wxImage::AddHandler( wxImageHandler *handler )
1311 {
1312 // Check for an existing handler of the type being added.
1313 if (FindHandler( handler->GetType() ) == 0)
1314 {
1315 sm_handlers.Append( handler );
1316 }
1317 else
1318 {
1319 // This is not documented behaviour, merely the simplest 'fix'
1320 // for preventing duplicate additions. If someone ever has
1321 // a good reason to add and remove duplicate handlers (and they
1322 // may) we should probably refcount the duplicates.
1323 // also an issue in InsertHandler below.
1324
1325 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1326 handler->GetName().c_str() );
1327 delete handler;
1328 }
1329 }
1330
1331 void wxImage::InsertHandler( wxImageHandler *handler )
1332 {
1333 // Check for an existing handler of the type being added.
1334 if (FindHandler( handler->GetType() ) == 0)
1335 {
1336 sm_handlers.Insert( handler );
1337 }
1338 else
1339 {
1340 // see AddHandler for additional comments.
1341 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1342 handler->GetName().c_str() );
1343 delete handler;
1344 }
1345 }
1346
1347 bool wxImage::RemoveHandler( const wxString& name )
1348 {
1349 wxImageHandler *handler = FindHandler(name);
1350 if (handler)
1351 {
1352 sm_handlers.DeleteObject(handler);
1353 delete handler;
1354 return true;
1355 }
1356 else
1357 return false;
1358 }
1359
1360 wxImageHandler *wxImage::FindHandler( const wxString& name )
1361 {
1362 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1363 while (node)
1364 {
1365 wxImageHandler *handler = (wxImageHandler*)node->GetData();
1366 if (handler->GetName().Cmp(name) == 0) return handler;
1367
1368 node = node->GetNext();
1369 }
1370 return 0;
1371 }
1372
1373 wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType )
1374 {
1375 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1376 while (node)
1377 {
1378 wxImageHandler *handler = (wxImageHandler*)node->GetData();
1379 if ( (handler->GetExtension().Cmp(extension) == 0) &&
1380 (bitmapType == -1 || handler->GetType() == bitmapType) )
1381 return handler;
1382 node = node->GetNext();
1383 }
1384 return 0;
1385 }
1386
1387 wxImageHandler *wxImage::FindHandler( long bitmapType )
1388 {
1389 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1390 while (node)
1391 {
1392 wxImageHandler *handler = (wxImageHandler *)node->GetData();
1393 if (handler->GetType() == bitmapType) return handler;
1394 node = node->GetNext();
1395 }
1396 return 0;
1397 }
1398
1399 wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
1400 {
1401 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1402 while (node)
1403 {
1404 wxImageHandler *handler = (wxImageHandler *)node->GetData();
1405 if (handler->GetMimeType().IsSameAs(mimetype, false)) return handler;
1406 node = node->GetNext();
1407 }
1408 return 0;
1409 }
1410
1411 void wxImage::InitStandardHandlers()
1412 {
1413 #if wxUSE_STREAMS
1414 AddHandler(new wxBMPHandler);
1415 #endif // wxUSE_STREAMS
1416 }
1417
1418 void wxImage::CleanUpHandlers()
1419 {
1420 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1421 while (node)
1422 {
1423 wxImageHandler *handler = (wxImageHandler *)node->GetData();
1424 wxList::compatibility_iterator next = node->GetNext();
1425 delete handler;
1426 node = next;
1427 }
1428
1429 sm_handlers.Clear();
1430 }
1431
1432 wxString wxImage::GetImageExtWildcard()
1433 {
1434 wxString fmts;
1435
1436 wxList& Handlers = wxImage::GetHandlers();
1437 wxList::compatibility_iterator Node = Handlers.GetFirst();
1438 while ( Node )
1439 {
1440 wxImageHandler* Handler = (wxImageHandler*)Node->GetData();
1441 fmts += wxT("*.") + Handler->GetExtension();
1442 Node = Node->GetNext();
1443 if ( Node ) fmts += wxT(";");
1444 }
1445
1446 return wxT("(") + fmts + wxT(")|") + fmts;
1447 }
1448
1449 //-----------------------------------------------------------------------------
1450 // wxImageHandler
1451 //-----------------------------------------------------------------------------
1452
1453 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject)
1454
1455 #if wxUSE_STREAMS
1456 bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) )
1457 {
1458 return false;
1459 }
1460
1461 bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
1462 {
1463 return false;
1464 }
1465
1466 int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) )
1467 {
1468 return 1;
1469 }
1470
1471 bool wxImageHandler::CanRead( const wxString& name )
1472 {
1473 if (wxFileExists(name))
1474 {
1475 wxFileInputStream stream(name);
1476 return CanRead(stream);
1477 }
1478
1479 wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() );
1480
1481 return false;
1482 }
1483
1484 bool wxImageHandler::CallDoCanRead(wxInputStream& stream)
1485 {
1486 off_t posOld = stream.TellI();
1487 if ( posOld == wxInvalidOffset )
1488 {
1489 // can't test unseekable stream
1490 return false;
1491 }
1492
1493 bool ok = DoCanRead(stream);
1494
1495 // restore the old position to be able to test other formats and so on
1496 if ( stream.SeekI(posOld) == wxInvalidOffset )
1497 {
1498 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1499
1500 // reading would fail anyhow as we're not at the right position
1501 return false;
1502 }
1503
1504 return ok;
1505 }
1506
1507 #endif // wxUSE_STREAMS
1508
1509 // ----------------------------------------------------------------------------
1510 // image histogram stuff
1511 // ----------------------------------------------------------------------------
1512
1513 bool
1514 wxImageHistogram::FindFirstUnusedColour(unsigned char *r,
1515 unsigned char *g,
1516 unsigned char *b,
1517 unsigned char r2,
1518 unsigned char b2,
1519 unsigned char g2) const
1520 {
1521 unsigned long key = MakeKey(r2, g2, b2);
1522
1523 while ( find(key) != end() )
1524 {
1525 // color already used
1526 r2++;
1527 if ( r2 >= 255 )
1528 {
1529 r2 = 0;
1530 g2++;
1531 if ( g2 >= 255 )
1532 {
1533 g2 = 0;
1534 b2++;
1535 if ( b2 >= 255 )
1536 {
1537 wxLogError(_("No unused colour in image.") );
1538 return false;
1539 }
1540 }
1541 }
1542
1543 key = MakeKey(r2, g2, b2);
1544 }
1545
1546 if ( r )
1547 *r = r2;
1548 if ( g )
1549 *g = g2;
1550 if ( b )
1551 *b = b2;
1552
1553 return true;
1554 }
1555
1556 bool
1557 wxImage::FindFirstUnusedColour(unsigned char *r,
1558 unsigned char *g,
1559 unsigned char *b,
1560 unsigned char r2,
1561 unsigned char b2,
1562 unsigned char g2) const
1563 {
1564 wxImageHistogram histogram;
1565
1566 ComputeHistogram(histogram);
1567
1568 return histogram.FindFirstUnusedColour(r, g, b, r2, g2, b2);
1569 }
1570
1571
1572
1573 // GRG, Dic/99
1574 // Counts and returns the number of different colours. Optionally stops
1575 // when it exceeds 'stopafter' different colours. This is useful, for
1576 // example, to see if the image can be saved as 8-bit (256 colour or
1577 // less, in this case it would be invoked as CountColours(256)). Default
1578 // value for stopafter is -1 (don't care).
1579 //
1580 unsigned long wxImage::CountColours( unsigned long stopafter ) const
1581 {
1582 wxHashTable h;
1583 wxObject dummy;
1584 unsigned char r, g, b;
1585 unsigned char *p;
1586 unsigned long size, nentries, key;
1587
1588 p = GetData();
1589 size = GetWidth() * GetHeight();
1590 nentries = 0;
1591
1592 for (unsigned long j = 0; (j < size) && (nentries <= stopafter) ; j++)
1593 {
1594 r = *(p++);
1595 g = *(p++);
1596 b = *(p++);
1597 key = wxImageHistogram::MakeKey(r, g, b);
1598
1599 if (h.Get(key) == NULL)
1600 {
1601 h.Put(key, &dummy);
1602 nentries++;
1603 }
1604 }
1605
1606 return nentries;
1607 }
1608
1609
1610 unsigned long wxImage::ComputeHistogram( wxImageHistogram &h ) const
1611 {
1612 unsigned char *p = GetData();
1613 unsigned long nentries = 0;
1614
1615 h.clear();
1616
1617 const unsigned long size = GetWidth() * GetHeight();
1618
1619 unsigned char r, g, b;
1620 for ( unsigned long n = 0; n < size; n++ )
1621 {
1622 r = *p++;
1623 g = *p++;
1624 b = *p++;
1625
1626 wxImageHistogramEntry& entry = h[wxImageHistogram::MakeKey(r, g, b)];
1627
1628 if ( entry.value++ == 0 )
1629 entry.index = nentries++;
1630 }
1631
1632 return nentries;
1633 }
1634
1635 /*
1636 * Rotation code by Carlos Moreno
1637 */
1638
1639 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1640 // does exactly the same thing. And I also got rid of wxRotationPixel
1641 // bacause of potential problems in architectures where alignment
1642 // is an issue, so I had to rewrite parts of the code.
1643
1644 static const double gs_Epsilon = 1e-10;
1645
1646 static inline int wxCint (double x)
1647 {
1648 return (x > 0) ? (int) (x + 0.5) : (int) (x - 0.5);
1649 }
1650
1651
1652 // Auxiliary function to rotate a point (x,y) with respect to point p0
1653 // make it inline and use a straight return to facilitate optimization
1654 // also, the function receives the sine and cosine of the angle to avoid
1655 // repeating the time-consuming calls to these functions -- sin/cos can
1656 // be computed and stored in the calling function.
1657
1658 inline wxRealPoint rotated_point (const wxRealPoint & p, double cos_angle, double sin_angle, const wxRealPoint & p0)
1659 {
1660 return wxRealPoint (p0.x + (p.x - p0.x) * cos_angle - (p.y - p0.y) * sin_angle,
1661 p0.y + (p.y - p0.y) * cos_angle + (p.x - p0.x) * sin_angle);
1662 }
1663
1664 inline wxRealPoint rotated_point (double x, double y, double cos_angle, double sin_angle, const wxRealPoint & p0)
1665 {
1666 return rotated_point (wxRealPoint(x,y), cos_angle, sin_angle, p0);
1667 }
1668
1669 wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool interpolating, wxPoint * offset_after_rotation) const
1670 {
1671 int i;
1672 angle = -angle; // screen coordinates are a mirror image of "real" coordinates
1673
1674 // Create pointer-based array to accelerate access to wxImage's data
1675 unsigned char ** data = new unsigned char * [GetHeight()];
1676
1677 data[0] = GetData();
1678
1679 for (i = 1; i < GetHeight(); i++)
1680 data[i] = data[i - 1] + (3 * GetWidth());
1681
1682 // precompute coefficients for rotation formula
1683 // (sine and cosine of the angle)
1684 const double cos_angle = cos(angle);
1685 const double sin_angle = sin(angle);
1686
1687 // Create new Image to store the result
1688 // First, find rectangle that covers the rotated image; to do that,
1689 // rotate the four corners
1690
1691 const wxRealPoint p0(centre_of_rotation.x, centre_of_rotation.y);
1692
1693 wxRealPoint p1 = rotated_point (0, 0, cos_angle, sin_angle, p0);
1694 wxRealPoint p2 = rotated_point (0, GetHeight(), cos_angle, sin_angle, p0);
1695 wxRealPoint p3 = rotated_point (GetWidth(), 0, cos_angle, sin_angle, p0);
1696 wxRealPoint p4 = rotated_point (GetWidth(), GetHeight(), cos_angle, sin_angle, p0);
1697
1698 int x1 = (int) floor (wxMin (wxMin(p1.x, p2.x), wxMin(p3.x, p4.x)));
1699 int y1 = (int) floor (wxMin (wxMin(p1.y, p2.y), wxMin(p3.y, p4.y)));
1700 int x2 = (int) ceil (wxMax (wxMax(p1.x, p2.x), wxMax(p3.x, p4.x)));
1701 int y2 = (int) ceil (wxMax (wxMax(p1.y, p2.y), wxMax(p3.y, p4.y)));
1702
1703 wxImage rotated (x2 - x1 + 1, y2 - y1 + 1, false);
1704
1705 if (offset_after_rotation != NULL)
1706 {
1707 *offset_after_rotation = wxPoint (x1, y1);
1708 }
1709
1710 // GRG: The rotated (destination) image is always accessed
1711 // sequentially, so there is no need for a pointer-based
1712 // array here (and in fact it would be slower).
1713 //
1714 unsigned char * dst = rotated.GetData();
1715
1716 // GRG: if the original image has a mask, use its RGB values
1717 // as the blank pixel, else, fall back to default (black).
1718 //
1719 unsigned char blank_r = 0;
1720 unsigned char blank_g = 0;
1721 unsigned char blank_b = 0;
1722
1723 if (HasMask())
1724 {
1725 blank_r = GetMaskRed();
1726 blank_g = GetMaskGreen();
1727 blank_b = GetMaskBlue();
1728 rotated.SetMaskColour( blank_r, blank_g, blank_b );
1729 }
1730
1731 // Now, for each point of the rotated image, find where it came from, by
1732 // performing an inverse rotation (a rotation of -angle) and getting the
1733 // pixel at those coordinates
1734
1735 // GRG: I've taken the (interpolating) test out of the loops, so that
1736 // it is done only once, instead of repeating it for each pixel.
1737
1738 int x;
1739 if (interpolating)
1740 {
1741 for (int y = 0; y < rotated.GetHeight(); y++)
1742 {
1743 for (x = 0; x < rotated.GetWidth(); x++)
1744 {
1745 wxRealPoint src = rotated_point (x + x1, y + y1, cos_angle, -sin_angle, p0);
1746
1747 if (-0.25 < src.x && src.x < GetWidth() - 0.75 &&
1748 -0.25 < src.y && src.y < GetHeight() - 0.75)
1749 {
1750 // interpolate using the 4 enclosing grid-points. Those
1751 // points can be obtained using floor and ceiling of the
1752 // exact coordinates of the point
1753 // C.M. 2000-02-17: when the point is near the border, special care is required.
1754
1755 int x1, y1, x2, y2;
1756
1757 if (0 < src.x && src.x < GetWidth() - 1)
1758 {
1759 x1 = wxCint(floor(src.x));
1760 x2 = wxCint(ceil(src.x));
1761 }
1762 else // else means that x is near one of the borders (0 or width-1)
1763 {
1764 x1 = x2 = wxCint (src.x);
1765 }
1766
1767 if (0 < src.y && src.y < GetHeight() - 1)
1768 {
1769 y1 = wxCint(floor(src.y));
1770 y2 = wxCint(ceil(src.y));
1771 }
1772 else
1773 {
1774 y1 = y2 = wxCint (src.y);
1775 }
1776
1777 // get four points and the distances (square of the distance,
1778 // for efficiency reasons) for the interpolation formula
1779
1780 // GRG: Do not calculate the points until they are
1781 // really needed -- this way we can calculate
1782 // just one, instead of four, if d1, d2, d3
1783 // or d4 are < gs_Epsilon
1784
1785 const double d1 = (src.x - x1) * (src.x - x1) + (src.y - y1) * (src.y - y1);
1786 const double d2 = (src.x - x2) * (src.x - x2) + (src.y - y1) * (src.y - y1);
1787 const double d3 = (src.x - x2) * (src.x - x2) + (src.y - y2) * (src.y - y2);
1788 const double d4 = (src.x - x1) * (src.x - x1) + (src.y - y2) * (src.y - y2);
1789
1790 // Now interpolate as a weighted average of the four surrounding
1791 // points, where the weights are the distances to each of those points
1792
1793 // If the point is exactly at one point of the grid of the source
1794 // image, then don't interpolate -- just assign the pixel
1795
1796 if (d1 < gs_Epsilon) // d1,d2,d3,d4 are positive -- no need for abs()
1797 {
1798 unsigned char *p = data[y1] + (3 * x1);
1799 *(dst++) = *(p++);
1800 *(dst++) = *(p++);
1801 *(dst++) = *(p++);
1802 }
1803 else if (d2 < gs_Epsilon)
1804 {
1805 unsigned char *p = data[y1] + (3 * x2);
1806 *(dst++) = *(p++);
1807 *(dst++) = *(p++);
1808 *(dst++) = *(p++);
1809 }
1810 else if (d3 < gs_Epsilon)
1811 {
1812 unsigned char *p = data[y2] + (3 * x2);
1813 *(dst++) = *(p++);
1814 *(dst++) = *(p++);
1815 *(dst++) = *(p++);
1816 }
1817 else if (d4 < gs_Epsilon)
1818 {
1819 unsigned char *p = data[y2] + (3 * x1);
1820 *(dst++) = *(p++);
1821 *(dst++) = *(p++);
1822 *(dst++) = *(p++);
1823 }
1824 else
1825 {
1826 // weights for the weighted average are proportional to the inverse of the distance
1827 unsigned char *v1 = data[y1] + (3 * x1);
1828 unsigned char *v2 = data[y1] + (3 * x2);
1829 unsigned char *v3 = data[y2] + (3 * x2);
1830 unsigned char *v4 = data[y2] + (3 * x1);
1831
1832 const double w1 = 1/d1, w2 = 1/d2, w3 = 1/d3, w4 = 1/d4;
1833
1834 // GRG: Unrolled.
1835
1836 *(dst++) = (unsigned char)
1837 ( (w1 * *(v1++) + w2 * *(v2++) +
1838 w3 * *(v3++) + w4 * *(v4++)) /
1839 (w1 + w2 + w3 + w4) );
1840 *(dst++) = (unsigned char)
1841 ( (w1 * *(v1++) + w2 * *(v2++) +
1842 w3 * *(v3++) + w4 * *(v4++)) /
1843 (w1 + w2 + w3 + w4) );
1844 *(dst++) = (unsigned char)
1845 ( (w1 * *v1 + w2 * *v2 +
1846 w3 * *v3 + w4 * *v4) /
1847 (w1 + w2 + w3 + w4) );
1848 }
1849 }
1850 else
1851 {
1852 *(dst++) = blank_r;
1853 *(dst++) = blank_g;
1854 *(dst++) = blank_b;
1855 }
1856 }
1857 }
1858 }
1859 else // not interpolating
1860 {
1861 for (int y = 0; y < rotated.GetHeight(); y++)
1862 {
1863 for (x = 0; x < rotated.GetWidth(); x++)
1864 {
1865 wxRealPoint src = rotated_point (x + x1, y + y1, cos_angle, -sin_angle, p0);
1866
1867 const int xs = wxCint (src.x); // wxCint rounds to the
1868 const int ys = wxCint (src.y); // closest integer
1869
1870 if (0 <= xs && xs < GetWidth() &&
1871 0 <= ys && ys < GetHeight())
1872 {
1873 unsigned char *p = data[ys] + (3 * xs);
1874 *(dst++) = *(p++);
1875 *(dst++) = *(p++);
1876 *(dst++) = *p;
1877 }
1878 else
1879 {
1880 *(dst++) = blank_r;
1881 *(dst++) = blank_g;
1882 *(dst++) = blank_b;
1883 }
1884 }
1885 }
1886 }
1887
1888 delete [] data;
1889
1890 return rotated;
1891 }
1892
1893
1894
1895
1896
1897 // A module to allow wxImage initialization/cleanup
1898 // without calling these functions from app.cpp or from
1899 // the user's application.
1900
1901 class wxImageModule: public wxModule
1902 {
1903 DECLARE_DYNAMIC_CLASS(wxImageModule)
1904 public:
1905 wxImageModule() {}
1906 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
1907 void OnExit() { wxImage::CleanUpHandlers(); };
1908 };
1909
1910 IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule)
1911
1912
1913 #endif // wxUSE_IMAGE