Include wx/module.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / common / image.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #if wxUSE_IMAGE
18
19 #include "wx/image.h"
20
21 #ifndef WX_PRECOMP
22 #include "wx/log.h"
23 #include "wx/app.h"
24 #include "wx/hash.h"
25 #include "wx/utils.h"
26 #include "wx/bitmap.h"
27 #include "wx/math.h"
28 #include "wx/module.h"
29 #endif
30
31 #include "wx/filefn.h"
32 #include "wx/wfstream.h"
33 #include "wx/intl.h"
34
35 #if wxUSE_XPM
36 #include "wx/xpmdecod.h"
37 #endif
38
39 // For memcpy
40 #include <string.h>
41
42 // make the code compile with either wxFile*Stream or wxFFile*Stream:
43 #define HAS_FILE_STREAMS (wxUSE_STREAMS && (wxUSE_FILE || wxUSE_FFILE))
44
45 #if HAS_FILE_STREAMS
46 #if wxUSE_FILE
47 typedef wxFileInputStream wxImageFileInputStream;
48 typedef wxFileOutputStream wxImageFileOutputStream;
49 #elif wxUSE_FFILE
50 typedef wxFFileInputStream wxImageFileInputStream;
51 typedef wxFFileOutputStream wxImageFileOutputStream;
52 #endif // wxUSE_FILE/wxUSE_FFILE
53 #endif // HAS_FILE_STREAMS
54
55 //-----------------------------------------------------------------------------
56 // wxImage
57 //-----------------------------------------------------------------------------
58
59 class wxImageRefData: public wxObjectRefData
60 {
61 public:
62 wxImageRefData();
63 virtual ~wxImageRefData();
64
65 int m_width;
66 int m_height;
67 unsigned char *m_data;
68
69 bool m_hasMask;
70 unsigned char m_maskRed,m_maskGreen,m_maskBlue;
71
72 // alpha channel data, may be NULL for the formats without alpha support
73 unsigned char *m_alpha;
74
75 bool m_ok;
76
77 // if true, m_data is pointer to static data and shouldn't be freed
78 bool m_static;
79
80 // same as m_static but for m_alpha
81 bool m_staticAlpha;
82
83 #if wxUSE_PALETTE
84 wxPalette m_palette;
85 #endif // wxUSE_PALETTE
86
87 wxArrayString m_optionNames;
88 wxArrayString m_optionValues;
89
90 DECLARE_NO_COPY_CLASS(wxImageRefData)
91 };
92
93 wxImageRefData::wxImageRefData()
94 {
95 m_width = 0;
96 m_height = 0;
97 m_data =
98 m_alpha = (unsigned char *) NULL;
99
100 m_maskRed = 0;
101 m_maskGreen = 0;
102 m_maskBlue = 0;
103 m_hasMask = false;
104
105 m_ok = false;
106 m_static =
107 m_staticAlpha = false;
108 }
109
110 wxImageRefData::~wxImageRefData()
111 {
112 if ( !m_static )
113 free( m_data );
114 if ( !m_staticAlpha )
115 free( m_alpha );
116 }
117
118 wxList wxImage::sm_handlers;
119
120 wxImage wxNullImage;
121
122 //-----------------------------------------------------------------------------
123
124 #define M_IMGDATA wx_static_cast(wxImageRefData*, m_refData)
125
126 IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject)
127
128 wxImage::wxImage( int width, int height, bool clear )
129 {
130 Create( width, height, clear );
131 }
132
133 wxImage::wxImage( int width, int height, unsigned char* data, bool static_data )
134 {
135 Create( width, height, data, static_data );
136 }
137
138 wxImage::wxImage( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data )
139 {
140 Create( width, height, data, alpha, static_data );
141 }
142
143 wxImage::wxImage( const wxString& name, long type, int index )
144 {
145 LoadFile( name, type, index );
146 }
147
148 wxImage::wxImage( const wxString& name, const wxString& mimetype, int index )
149 {
150 LoadFile( name, mimetype, index );
151 }
152
153 #if wxUSE_STREAMS
154 wxImage::wxImage( wxInputStream& stream, long type, int index )
155 {
156 LoadFile( stream, type, index );
157 }
158
159 wxImage::wxImage( wxInputStream& stream, const wxString& mimetype, int index )
160 {
161 LoadFile( stream, mimetype, index );
162 }
163 #endif // wxUSE_STREAMS
164
165 wxImage::wxImage( const char** xpmData )
166 {
167 Create(xpmData);
168 }
169
170 wxImage::wxImage( char** xpmData )
171 {
172 Create((const char**) xpmData);
173 }
174
175 bool wxImage::Create( const char** xpmData )
176 {
177 #if wxUSE_XPM
178 UnRef();
179
180 wxXPMDecoder decoder;
181 (*this) = decoder.ReadData(xpmData);
182 return Ok();
183 #else
184 return false;
185 #endif
186 }
187
188 bool wxImage::Create( int width, int height, bool clear )
189 {
190 UnRef();
191
192 m_refData = new wxImageRefData();
193
194 M_IMGDATA->m_data = (unsigned char *) malloc( width*height*3 );
195 if (!M_IMGDATA->m_data)
196 {
197 UnRef();
198 return false;
199 }
200
201 if (clear)
202 memset(M_IMGDATA->m_data, 0, width*height*3);
203
204 M_IMGDATA->m_width = width;
205 M_IMGDATA->m_height = height;
206 M_IMGDATA->m_ok = true;
207
208 return true;
209 }
210
211 bool wxImage::Create( int width, int height, unsigned char* data, bool static_data )
212 {
213 UnRef();
214
215 wxCHECK_MSG( data, false, _T("NULL data in wxImage::Create") );
216
217 m_refData = new wxImageRefData();
218
219 M_IMGDATA->m_data = data;
220 M_IMGDATA->m_width = width;
221 M_IMGDATA->m_height = height;
222 M_IMGDATA->m_ok = true;
223 M_IMGDATA->m_static = static_data;
224
225 return true;
226 }
227
228 bool wxImage::Create( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data )
229 {
230 UnRef();
231
232 wxCHECK_MSG( data, false, _T("NULL data in wxImage::Create") );
233
234 m_refData = new wxImageRefData();
235
236 M_IMGDATA->m_data = data;
237 M_IMGDATA->m_alpha = alpha;
238 M_IMGDATA->m_width = width;
239 M_IMGDATA->m_height = height;
240 M_IMGDATA->m_ok = true;
241 M_IMGDATA->m_static = static_data;
242
243 return true;
244 }
245
246 void wxImage::Destroy()
247 {
248 UnRef();
249 }
250
251 wxObjectRefData* wxImage::CreateRefData() const
252 {
253 return new wxImageRefData;
254 }
255
256 wxObjectRefData* wxImage::CloneRefData(const wxObjectRefData* that) const
257 {
258 const wxImageRefData* refData = wx_static_cast(const wxImageRefData*, that);
259 wxCHECK_MSG(refData->m_ok, NULL, wxT("invalid image") );
260
261 wxImageRefData* refData_new = new wxImageRefData;
262 refData_new->m_width = refData->m_width;
263 refData_new->m_height = refData->m_height;
264 refData_new->m_maskRed = refData->m_maskRed;
265 refData_new->m_maskGreen = refData->m_maskGreen;
266 refData_new->m_maskBlue = refData->m_maskBlue;
267 refData_new->m_hasMask = refData->m_hasMask;
268 refData_new->m_ok = true;
269 unsigned size = unsigned(refData->m_width) * unsigned(refData->m_height);
270 if (refData->m_alpha != NULL)
271 {
272 refData_new->m_alpha = (unsigned char*)malloc(size);
273 memcpy(refData_new->m_alpha, refData->m_alpha, size);
274 }
275 size *= 3;
276 refData_new->m_data = (unsigned char*)malloc(size);
277 memcpy(refData_new->m_data, refData->m_data, size);
278 #if wxUSE_PALETTE
279 refData_new->m_palette = refData->m_palette;
280 #endif
281 refData_new->m_optionNames = refData->m_optionNames;
282 refData_new->m_optionValues = refData->m_optionValues;
283 return refData_new;
284 }
285
286 wxImage wxImage::Copy() const
287 {
288 wxImage image;
289
290 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
291
292 image.m_refData = CloneRefData(m_refData);
293
294 return image;
295 }
296
297 wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const
298 {
299 if( xFactor == 1 && yFactor == 1 )
300 return *this;
301
302 wxImage image;
303
304 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
305
306 // can't scale to/from 0 size
307 wxCHECK_MSG( (xFactor > 0) && (yFactor > 0), image,
308 wxT("invalid new image size") );
309
310 long old_height = M_IMGDATA->m_height,
311 old_width = M_IMGDATA->m_width;
312
313 wxCHECK_MSG( (old_height > 0) && (old_width > 0), image,
314 wxT("invalid old image size") );
315
316 long width = old_width / xFactor ;
317 long height = old_height / yFactor ;
318
319 image.Create( width, height, false );
320
321 char unsigned *data = image.GetData();
322
323 wxCHECK_MSG( data, image, wxT("unable to create image") );
324
325 bool hasMask = false ;
326 unsigned char maskRed = 0;
327 unsigned char maskGreen = 0;
328 unsigned char maskBlue =0 ;
329
330 unsigned char *source_data = M_IMGDATA->m_data;
331 unsigned char *target_data = data;
332 unsigned char *source_alpha = 0 ;
333 unsigned char *target_alpha = 0 ;
334 if (M_IMGDATA->m_hasMask)
335 {
336 hasMask = true ;
337 maskRed = M_IMGDATA->m_maskRed;
338 maskGreen = M_IMGDATA->m_maskGreen;
339 maskBlue =M_IMGDATA->m_maskBlue ;
340
341 image.SetMaskColour( M_IMGDATA->m_maskRed,
342 M_IMGDATA->m_maskGreen,
343 M_IMGDATA->m_maskBlue );
344 }
345 else
346 {
347 source_alpha = M_IMGDATA->m_alpha ;
348 if ( source_alpha )
349 {
350 image.SetAlpha() ;
351 target_alpha = image.GetAlpha() ;
352 }
353 }
354
355 for (long y = 0; y < height; y++)
356 {
357 for (long x = 0; x < width; x++)
358 {
359 unsigned long avgRed = 0 ;
360 unsigned long avgGreen = 0;
361 unsigned long avgBlue = 0;
362 unsigned long avgAlpha = 0 ;
363 unsigned long counter = 0 ;
364 // determine average
365 for ( int y1 = 0 ; y1 < yFactor ; ++y1 )
366 {
367 long y_offset = (y * yFactor + y1) * old_width;
368 for ( int x1 = 0 ; x1 < xFactor ; ++x1 )
369 {
370 unsigned char *pixel = source_data + 3 * ( y_offset + x * xFactor + x1 ) ;
371 unsigned char red = pixel[0] ;
372 unsigned char green = pixel[1] ;
373 unsigned char blue = pixel[2] ;
374 unsigned char alpha = 255 ;
375 if ( source_alpha )
376 alpha = *(source_alpha + y_offset + x * xFactor + x1) ;
377 if ( !hasMask || red != maskRed || green != maskGreen || blue != maskBlue )
378 {
379 if ( alpha > 0 )
380 {
381 avgRed += red ;
382 avgGreen += green ;
383 avgBlue += blue ;
384 }
385 avgAlpha += alpha ;
386 counter++ ;
387 }
388 }
389 }
390 if ( counter == 0 )
391 {
392 *(target_data++) = M_IMGDATA->m_maskRed ;
393 *(target_data++) = M_IMGDATA->m_maskGreen ;
394 *(target_data++) = M_IMGDATA->m_maskBlue ;
395 }
396 else
397 {
398 if ( source_alpha )
399 *(target_alpha++) = (unsigned char)(avgAlpha / counter ) ;
400 *(target_data++) = (unsigned char)(avgRed / counter);
401 *(target_data++) = (unsigned char)(avgGreen / counter);
402 *(target_data++) = (unsigned char)(avgBlue / counter);
403 }
404 }
405 }
406
407 // In case this is a cursor, make sure the hotspot is scaled accordingly:
408 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) )
409 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X,
410 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X))/xFactor);
411 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) )
412 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y,
413 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y))/yFactor);
414
415 return image;
416 }
417
418 wxImage wxImage::Scale( int width, int height ) const
419 {
420 wxImage image;
421
422 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
423
424 // can't scale to/from 0 size
425 wxCHECK_MSG( (width > 0) && (height > 0), image,
426 wxT("invalid new image size") );
427
428 long old_height = M_IMGDATA->m_height,
429 old_width = M_IMGDATA->m_width;
430 wxCHECK_MSG( (old_height > 0) && (old_width > 0), image,
431 wxT("invalid old image size") );
432
433 if ( old_width % width == 0 && old_width >= width &&
434 old_height % height == 0 && old_height >= height )
435 {
436 return ShrinkBy( old_width / width , old_height / height ) ;
437 }
438 image.Create( width, height, false );
439
440 unsigned char *data = image.GetData();
441
442 wxCHECK_MSG( data, image, wxT("unable to create image") );
443
444 unsigned char *source_data = M_IMGDATA->m_data;
445 unsigned char *target_data = data;
446 unsigned char *source_alpha = 0 ;
447 unsigned char *target_alpha = 0 ;
448
449 if (M_IMGDATA->m_hasMask)
450 {
451 image.SetMaskColour( M_IMGDATA->m_maskRed,
452 M_IMGDATA->m_maskGreen,
453 M_IMGDATA->m_maskBlue );
454 }
455 else
456 {
457 source_alpha = M_IMGDATA->m_alpha ;
458 if ( source_alpha )
459 {
460 image.SetAlpha() ;
461 target_alpha = image.GetAlpha() ;
462 }
463 }
464
465 long x_delta = (old_width<<16) / width;
466 long y_delta = (old_height<<16) / height;
467
468 unsigned char* dest_pixel = target_data;
469
470 long y = 0;
471 for ( long j = 0; j < height; j++ )
472 {
473 unsigned char* src_line = &source_data[(y>>16)*old_width*3];
474 unsigned char* src_alpha_line = source_alpha ? &source_alpha[(y>>16)*old_width] : 0 ;
475
476 long x = 0;
477 for ( long i = 0; i < width; i++ )
478 {
479 unsigned char* src_pixel = &src_line[(x>>16)*3];
480 unsigned char* src_alpha_pixel = source_alpha ? &src_alpha_line[(x>>16)] : 0 ;
481 dest_pixel[0] = src_pixel[0];
482 dest_pixel[1] = src_pixel[1];
483 dest_pixel[2] = src_pixel[2];
484 dest_pixel += 3;
485 if ( source_alpha )
486 *(target_alpha++) = *src_alpha_pixel ;
487 x += x_delta;
488 }
489
490 y += y_delta;
491 }
492
493 // In case this is a cursor, make sure the hotspot is scaled accordingly:
494 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) )
495 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X,
496 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X)*width)/old_width);
497 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) )
498 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y,
499 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y)*height)/old_height);
500
501 return image;
502 }
503
504 wxImage wxImage::Rotate90( bool clockwise ) const
505 {
506 wxImage image;
507
508 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
509
510 image.Create( M_IMGDATA->m_height, M_IMGDATA->m_width, false );
511
512 unsigned char *data = image.GetData();
513
514 wxCHECK_MSG( data, image, wxT("unable to create image") );
515
516 unsigned char *source_data = M_IMGDATA->m_data;
517 unsigned char *target_data;
518 unsigned char *alpha_data = 0 ;
519 unsigned char *source_alpha = 0 ;
520 unsigned char *target_alpha = 0 ;
521
522 if (M_IMGDATA->m_hasMask)
523 {
524 image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
525 }
526 else
527 {
528 source_alpha = M_IMGDATA->m_alpha ;
529 if ( source_alpha )
530 {
531 image.SetAlpha() ;
532 alpha_data = image.GetAlpha() ;
533 }
534 }
535
536 long height = M_IMGDATA->m_height;
537 long width = M_IMGDATA->m_width;
538
539 for (long j = 0; j < height; j++)
540 {
541 for (long i = 0; i < width; i++)
542 {
543 if (clockwise)
544 {
545 target_data = data + (((i+1)*height) - j - 1)*3;
546 if(source_alpha)
547 target_alpha = alpha_data + (((i+1)*height) - j - 1);
548 }
549 else
550 {
551 target_data = data + ((height*(width-1)) + j - (i*height))*3;
552 if(source_alpha)
553 target_alpha = alpha_data + ((height*(width-1)) + j - (i*height));
554 }
555 memcpy( target_data, source_data, 3 );
556 source_data += 3;
557
558 if(source_alpha)
559 {
560 memcpy( target_alpha, source_alpha, 1 );
561 source_alpha += 1;
562 }
563 }
564 }
565
566 return image;
567 }
568
569 wxImage wxImage::Mirror( bool horizontally ) const
570 {
571 wxImage image;
572
573 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
574
575 image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false );
576
577 unsigned char *data = image.GetData();
578 unsigned char *alpha = NULL;
579
580 wxCHECK_MSG( data, image, wxT("unable to create image") );
581
582 if (M_IMGDATA->m_alpha != NULL) {
583 image.SetAlpha();
584 alpha = image.GetAlpha();
585 wxCHECK_MSG( alpha, image, wxT("unable to create alpha channel") );
586 }
587
588 if (M_IMGDATA->m_hasMask)
589 image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
590
591 long height = M_IMGDATA->m_height;
592 long width = M_IMGDATA->m_width;
593
594 unsigned char *source_data = M_IMGDATA->m_data;
595 unsigned char *target_data;
596
597 if (horizontally)
598 {
599 for (long j = 0; j < height; j++)
600 {
601 data += width*3;
602 target_data = data-3;
603 for (long i = 0; i < width; i++)
604 {
605 memcpy( target_data, source_data, 3 );
606 source_data += 3;
607 target_data -= 3;
608 }
609 }
610
611 if (alpha != NULL)
612 {
613 // src_alpha starts at the first pixel and increases by 1 after each step
614 // (a step here is the copy of the alpha value of one pixel)
615 const unsigned char *src_alpha = M_IMGDATA->m_alpha;
616 // dest_alpha starts just beyond the first line, decreases before each step,
617 // and after each line is finished, increases by 2 widths (skipping the line
618 // just copied and the line that will be copied next)
619 unsigned char *dest_alpha = alpha + width;
620
621 for (long jj = 0; jj < height; ++jj)
622 {
623 for (long i = 0; i < width; ++i) {
624 *(--dest_alpha) = *(src_alpha++); // copy one pixel
625 }
626 dest_alpha += 2 * width; // advance beyond the end of the next line
627 }
628 }
629 }
630 else
631 {
632 for (long i = 0; i < height; i++)
633 {
634 target_data = data + 3*width*(height-1-i);
635 memcpy( target_data, source_data, (size_t)3*width );
636 source_data += 3*width;
637 }
638
639 if (alpha != NULL)
640 {
641 // src_alpha starts at the first pixel and increases by 1 width after each step
642 // (a step here is the copy of the alpha channel of an entire line)
643 const unsigned char *src_alpha = M_IMGDATA->m_alpha;
644 // dest_alpha starts just beyond the last line (beyond the whole image)
645 // and decreases by 1 width before each step
646 unsigned char *dest_alpha = alpha + width * height;
647
648 for (long jj = 0; jj < height; ++jj)
649 {
650 dest_alpha -= width;
651 memcpy( dest_alpha, src_alpha, (size_t)width );
652 src_alpha += width;
653 }
654 }
655 }
656
657 return image;
658 }
659
660 wxImage wxImage::GetSubImage( const wxRect &rect ) const
661 {
662 wxImage image;
663
664 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
665
666 wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) &&
667 (rect.GetRight()<=GetWidth()) && (rect.GetBottom()<=GetHeight()),
668 image, wxT("invalid subimage size") );
669
670 const int subwidth = rect.GetWidth();
671 const int subheight = rect.GetHeight();
672
673 image.Create( subwidth, subheight, false );
674
675 const unsigned char *src_data = GetData();
676 const unsigned char *src_alpha = M_IMGDATA->m_alpha;
677 unsigned char *subdata = image.GetData();
678 unsigned char *subalpha = NULL;
679
680 wxCHECK_MSG( subdata, image, wxT("unable to create image") );
681
682 if (src_alpha != NULL) {
683 image.SetAlpha();
684 subalpha = image.GetAlpha();
685 wxCHECK_MSG( subalpha, image, wxT("unable to create alpha channel"));
686 }
687
688 if (M_IMGDATA->m_hasMask)
689 image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
690
691 const int width = GetWidth();
692 const int pixsoff = rect.GetLeft() + width * rect.GetTop();
693
694 src_data += 3 * pixsoff;
695 src_alpha += pixsoff; // won't be used if was NULL, so this is ok
696
697 for (long j = 0; j < subheight; ++j)
698 {
699 memcpy( subdata, src_data, 3 * subwidth );
700 subdata += 3 * subwidth;
701 src_data += 3 * width;
702 if (subalpha != NULL) {
703 memcpy( subalpha, src_alpha, subwidth );
704 subalpha += subwidth;
705 src_alpha += width;
706 }
707 }
708
709 return image;
710 }
711
712 wxImage wxImage::Size( const wxSize& size, const wxPoint& pos,
713 int r_, int g_, int b_ ) const
714 {
715 wxImage image;
716
717 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
718 wxCHECK_MSG( (size.GetWidth() > 0) && (size.GetHeight() > 0), image, wxT("invalid size") );
719
720 int width = GetWidth(), height = GetHeight();
721 image.Create(size.GetWidth(), size.GetHeight(), false);
722
723 unsigned char r = (unsigned char)r_;
724 unsigned char g = (unsigned char)g_;
725 unsigned char b = (unsigned char)b_;
726 if ((r_ == -1) && (g_ == -1) && (b_ == -1))
727 {
728 GetOrFindMaskColour( &r, &g, &b );
729 image.SetMaskColour(r, g, b);
730 }
731
732 image.SetRGB(wxRect(), r, g, b);
733
734 wxRect subRect(pos.x, pos.y, width, height);
735 wxRect finalRect(0, 0, size.GetWidth(), size.GetHeight());
736 if (pos.x < 0)
737 finalRect.width -= pos.x;
738 if (pos.y < 0)
739 finalRect.height -= pos.y;
740
741 subRect.Intersect(finalRect);
742
743 if (!subRect.IsEmpty())
744 {
745 if ((subRect.GetWidth() == width) && (subRect.GetHeight() == height))
746 image.Paste(*this, pos.x, pos.y);
747 else
748 image.Paste(GetSubImage(subRect), pos.x, pos.y);
749 }
750
751 return image;
752 }
753
754 void wxImage::Paste( const wxImage &image, int x, int y )
755 {
756 wxCHECK_RET( Ok(), wxT("invalid image") );
757 wxCHECK_RET( image.Ok(), wxT("invalid image") );
758
759 AllocExclusive();
760
761 int xx = 0;
762 int yy = 0;
763 int width = image.GetWidth();
764 int height = image.GetHeight();
765
766 if (x < 0)
767 {
768 xx = -x;
769 width += x;
770 }
771 if (y < 0)
772 {
773 yy = -y;
774 height += y;
775 }
776
777 if ((x+xx)+width > M_IMGDATA->m_width)
778 width = M_IMGDATA->m_width - (x+xx);
779 if ((y+yy)+height > M_IMGDATA->m_height)
780 height = M_IMGDATA->m_height - (y+yy);
781
782 if (width < 1) return;
783 if (height < 1) return;
784
785 if ((!HasMask() && !image.HasMask()) ||
786 (HasMask() && !image.HasMask()) ||
787 ((HasMask() && image.HasMask() &&
788 (GetMaskRed()==image.GetMaskRed()) &&
789 (GetMaskGreen()==image.GetMaskGreen()) &&
790 (GetMaskBlue()==image.GetMaskBlue()))))
791 {
792 width *= 3;
793 unsigned char* source_data = image.GetData() + xx*3 + yy*3*image.GetWidth();
794 int source_step = image.GetWidth()*3;
795
796 unsigned char* target_data = GetData() + (x+xx)*3 + (y+yy)*3*M_IMGDATA->m_width;
797 int target_step = M_IMGDATA->m_width*3;
798 for (int j = 0; j < height; j++)
799 {
800 memcpy( target_data, source_data, width );
801 source_data += source_step;
802 target_data += target_step;
803 }
804 return;
805 }
806
807 if (!HasMask() && image.HasMask())
808 {
809 unsigned char r = image.GetMaskRed();
810 unsigned char g = image.GetMaskGreen();
811 unsigned char b = image.GetMaskBlue();
812
813 width *= 3;
814 unsigned char* source_data = image.GetData() + xx*3 + yy*3*image.GetWidth();
815 int source_step = image.GetWidth()*3;
816
817 unsigned char* target_data = GetData() + (x+xx)*3 + (y+yy)*3*M_IMGDATA->m_width;
818 int target_step = M_IMGDATA->m_width*3;
819
820 for (int j = 0; j < height; j++)
821 {
822 for (int i = 0; i < width; i+=3)
823 {
824 if ((source_data[i] != r) &&
825 (source_data[i+1] != g) &&
826 (source_data[i+2] != b))
827 {
828 memcpy( target_data+i, source_data+i, 3 );
829 }
830 }
831 source_data += source_step;
832 target_data += target_step;
833 }
834 }
835 }
836
837 void wxImage::Replace( unsigned char r1, unsigned char g1, unsigned char b1,
838 unsigned char r2, unsigned char g2, unsigned char b2 )
839 {
840 wxCHECK_RET( Ok(), wxT("invalid image") );
841
842 AllocExclusive();
843
844 unsigned char *data = GetData();
845
846 const int w = GetWidth();
847 const int h = GetHeight();
848
849 for (int j = 0; j < h; j++)
850 for (int i = 0; i < w; i++)
851 {
852 if ((data[0] == r1) && (data[1] == g1) && (data[2] == b1))
853 {
854 data[0] = r2;
855 data[1] = g2;
856 data[2] = b2;
857 }
858 data += 3;
859 }
860 }
861
862 wxImage wxImage::ConvertToGreyscale( double lr, double lg, double lb ) const
863 {
864 wxImage image;
865
866 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
867
868 image.Create(M_IMGDATA->m_width, M_IMGDATA->m_height, false);
869
870 unsigned char *dest = image.GetData();
871
872 wxCHECK_MSG( dest, image, wxT("unable to create image") );
873
874 unsigned char *src = M_IMGDATA->m_data;
875 bool hasMask = M_IMGDATA->m_hasMask;
876 unsigned char maskRed = M_IMGDATA->m_maskRed;
877 unsigned char maskGreen = M_IMGDATA->m_maskGreen;
878 unsigned char maskBlue = M_IMGDATA->m_maskBlue;
879
880 if ( hasMask )
881 image.SetMaskColour(maskRed, maskGreen, maskBlue);
882
883 const long size = M_IMGDATA->m_width * M_IMGDATA->m_height;
884 for ( long i = 0; i < size; i++, src += 3, dest += 3 )
885 {
886 // don't modify the mask
887 if ( hasMask && src[0] == maskRed && src[1] == maskGreen && src[2] == maskBlue )
888 {
889 memcpy(dest, src, 3);
890 }
891 else
892 {
893 // calculate the luma
894 double luma = (src[0] * lr + src[1] * lg + src[2] * lb) + 0.5;
895 dest[0] = dest[1] = dest[2] = wx_static_cast(unsigned char, luma);
896 }
897 }
898
899 // copy the alpha channel, if any
900 if (HasAlpha())
901 {
902 const size_t alphaSize = GetWidth() * GetHeight();
903 unsigned char *alpha = (unsigned char*)malloc(alphaSize);
904 memcpy(alpha, GetAlpha(), alphaSize);
905 image.InitAlpha();
906 image.SetAlpha(alpha);
907 }
908
909 return image;
910 }
911
912 wxImage wxImage::ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const
913 {
914 wxImage image;
915
916 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
917
918 image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false );
919
920 unsigned char *data = image.GetData();
921
922 wxCHECK_MSG( data, image, wxT("unable to create image") );
923
924 if (M_IMGDATA->m_hasMask)
925 {
926 if (M_IMGDATA->m_maskRed == r && M_IMGDATA->m_maskGreen == g &&
927 M_IMGDATA->m_maskBlue == b)
928 image.SetMaskColour( 255, 255, 255 );
929 else
930 image.SetMaskColour( 0, 0, 0 );
931 }
932
933 long size = M_IMGDATA->m_height * M_IMGDATA->m_width;
934
935 unsigned char *srcd = M_IMGDATA->m_data;
936 unsigned char *tard = image.GetData();
937
938 for ( long i = 0; i < size; i++, srcd += 3, tard += 3 )
939 {
940 if (srcd[0] == r && srcd[1] == g && srcd[2] == b)
941 tard[0] = tard[1] = tard[2] = 255;
942 else
943 tard[0] = tard[1] = tard[2] = 0;
944 }
945
946 return image;
947 }
948
949 int wxImage::GetWidth() const
950 {
951 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
952
953 return M_IMGDATA->m_width;
954 }
955
956 int wxImage::GetHeight() const
957 {
958 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
959
960 return M_IMGDATA->m_height;
961 }
962
963 long wxImage::XYToIndex(int x, int y) const
964 {
965 if ( Ok() &&
966 x >= 0 && y >= 0 &&
967 x < M_IMGDATA->m_width && y < M_IMGDATA->m_height )
968 {
969 return y*M_IMGDATA->m_width + x;
970 }
971
972 return -1;
973 }
974
975 void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b )
976 {
977 long pos = XYToIndex(x, y);
978 wxCHECK_RET( pos != -1, wxT("invalid image coordinates") );
979
980 AllocExclusive();
981
982 pos *= 3;
983
984 M_IMGDATA->m_data[ pos ] = r;
985 M_IMGDATA->m_data[ pos+1 ] = g;
986 M_IMGDATA->m_data[ pos+2 ] = b;
987 }
988
989 void wxImage::SetRGB( const wxRect& rect_, unsigned char r, unsigned char g, unsigned char b )
990 {
991 wxCHECK_RET( Ok(), wxT("invalid image") );
992
993 AllocExclusive();
994
995 wxRect rect(rect_);
996 wxRect imageRect(0, 0, GetWidth(), GetHeight());
997 if ( rect == wxRect() )
998 {
999 rect = imageRect;
1000 }
1001 else
1002 {
1003 wxCHECK_RET( imageRect.Inside(rect.GetTopLeft()) &&
1004 imageRect.Inside(rect.GetBottomRight()),
1005 wxT("invalid bounding rectangle") );
1006 }
1007
1008 int x1 = rect.GetLeft(),
1009 y1 = rect.GetTop(),
1010 x2 = rect.GetRight() + 1,
1011 y2 = rect.GetBottom() + 1;
1012
1013 unsigned char *data wxDUMMY_INITIALIZE(NULL);
1014 int x, y, width = GetWidth();
1015 for (y = y1; y < y2; y++)
1016 {
1017 data = M_IMGDATA->m_data + (y*width + x1)*3;
1018 for (x = x1; x < x2; x++)
1019 {
1020 *data++ = r;
1021 *data++ = g;
1022 *data++ = b;
1023 }
1024 }
1025 }
1026
1027 unsigned char wxImage::GetRed( int x, int y ) const
1028 {
1029 long pos = XYToIndex(x, y);
1030 wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") );
1031
1032 pos *= 3;
1033
1034 return M_IMGDATA->m_data[pos];
1035 }
1036
1037 unsigned char wxImage::GetGreen( int x, int y ) const
1038 {
1039 long pos = XYToIndex(x, y);
1040 wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") );
1041
1042 pos *= 3;
1043
1044 return M_IMGDATA->m_data[pos+1];
1045 }
1046
1047 unsigned char wxImage::GetBlue( int x, int y ) const
1048 {
1049 long pos = XYToIndex(x, y);
1050 wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") );
1051
1052 pos *= 3;
1053
1054 return M_IMGDATA->m_data[pos+2];
1055 }
1056
1057 bool wxImage::Ok() const
1058 {
1059 // image of 0 width or height can't be considered ok - at least because it
1060 // causes crashes in ConvertToBitmap() if we don't catch it in time
1061 wxImageRefData *data = M_IMGDATA;
1062 return data && data->m_ok && data->m_width && data->m_height;
1063 }
1064
1065 unsigned char *wxImage::GetData() const
1066 {
1067 wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") );
1068
1069 return M_IMGDATA->m_data;
1070 }
1071
1072 void wxImage::SetData( unsigned char *data, bool static_data )
1073 {
1074 wxCHECK_RET( Ok(), wxT("invalid image") );
1075
1076 wxImageRefData *newRefData = new wxImageRefData();
1077
1078 newRefData->m_width = M_IMGDATA->m_width;
1079 newRefData->m_height = M_IMGDATA->m_height;
1080 newRefData->m_data = data;
1081 newRefData->m_ok = true;
1082 newRefData->m_maskRed = M_IMGDATA->m_maskRed;
1083 newRefData->m_maskGreen = M_IMGDATA->m_maskGreen;
1084 newRefData->m_maskBlue = M_IMGDATA->m_maskBlue;
1085 newRefData->m_hasMask = M_IMGDATA->m_hasMask;
1086 newRefData->m_static = static_data;
1087
1088 UnRef();
1089
1090 m_refData = newRefData;
1091 }
1092
1093 void wxImage::SetData( unsigned char *data, int new_width, int new_height, bool static_data )
1094 {
1095 wxImageRefData *newRefData = new wxImageRefData();
1096
1097 if (m_refData)
1098 {
1099 newRefData->m_width = new_width;
1100 newRefData->m_height = new_height;
1101 newRefData->m_data = data;
1102 newRefData->m_ok = true;
1103 newRefData->m_maskRed = M_IMGDATA->m_maskRed;
1104 newRefData->m_maskGreen = M_IMGDATA->m_maskGreen;
1105 newRefData->m_maskBlue = M_IMGDATA->m_maskBlue;
1106 newRefData->m_hasMask = M_IMGDATA->m_hasMask;
1107 }
1108 else
1109 {
1110 newRefData->m_width = new_width;
1111 newRefData->m_height = new_height;
1112 newRefData->m_data = data;
1113 newRefData->m_ok = true;
1114 }
1115 newRefData->m_static = static_data;
1116
1117 UnRef();
1118
1119 m_refData = newRefData;
1120 }
1121
1122 // ----------------------------------------------------------------------------
1123 // alpha channel support
1124 // ----------------------------------------------------------------------------
1125
1126 void wxImage::SetAlpha(int x, int y, unsigned char alpha)
1127 {
1128 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1129
1130 long pos = XYToIndex(x, y);
1131 wxCHECK_RET( pos != -1, wxT("invalid image coordinates") );
1132
1133 AllocExclusive();
1134
1135 M_IMGDATA->m_alpha[pos] = alpha;
1136 }
1137
1138 unsigned char wxImage::GetAlpha(int x, int y) const
1139 {
1140 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1141
1142 long pos = XYToIndex(x, y);
1143 wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") );
1144
1145 return M_IMGDATA->m_alpha[pos];
1146 }
1147
1148 bool
1149 wxImage::ConvertColourToAlpha(unsigned char r, unsigned char g, unsigned char b)
1150 {
1151 SetAlpha(NULL);
1152
1153 const int w = M_IMGDATA->m_width;
1154 const int h = M_IMGDATA->m_height;
1155
1156 unsigned char *alpha = GetAlpha();
1157 unsigned char *data = GetData();
1158
1159 for ( int y = 0; y < h; y++ )
1160 {
1161 for ( int x = 0; x < w; x++ )
1162 {
1163 *alpha++ = *data;
1164 *data++ = r;
1165 *data++ = g;
1166 *data++ = b;
1167 }
1168 }
1169
1170 return true;
1171 }
1172
1173 void wxImage::SetAlpha( unsigned char *alpha, bool static_data )
1174 {
1175 wxCHECK_RET( Ok(), wxT("invalid image") );
1176
1177 AllocExclusive();
1178
1179 if ( !alpha )
1180 {
1181 alpha = (unsigned char *)malloc(M_IMGDATA->m_width*M_IMGDATA->m_height);
1182 }
1183
1184 free(M_IMGDATA->m_alpha);
1185 M_IMGDATA->m_alpha = alpha;
1186 M_IMGDATA->m_staticAlpha = static_data;
1187 }
1188
1189 unsigned char *wxImage::GetAlpha() const
1190 {
1191 wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") );
1192
1193 return M_IMGDATA->m_alpha;
1194 }
1195
1196 void wxImage::InitAlpha()
1197 {
1198 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1199
1200 // initialize memory for alpha channel
1201 SetAlpha();
1202
1203 unsigned char *alpha = M_IMGDATA->m_alpha;
1204 const size_t lenAlpha = M_IMGDATA->m_width * M_IMGDATA->m_height;
1205
1206 if ( HasMask() )
1207 {
1208 // use the mask to initialize the alpha channel.
1209 const unsigned char * const alphaEnd = alpha + lenAlpha;
1210
1211 const unsigned char mr = M_IMGDATA->m_maskRed;
1212 const unsigned char mg = M_IMGDATA->m_maskGreen;
1213 const unsigned char mb = M_IMGDATA->m_maskBlue;
1214 for ( unsigned char *src = M_IMGDATA->m_data;
1215 alpha < alphaEnd;
1216 src += 3, alpha++ )
1217 {
1218 *alpha = (src[0] == mr && src[1] == mg && src[2] == mb)
1219 ? wxIMAGE_ALPHA_TRANSPARENT
1220 : wxIMAGE_ALPHA_OPAQUE;
1221 }
1222
1223 M_IMGDATA->m_hasMask = false;
1224 }
1225 else // no mask
1226 {
1227 // make the image fully opaque
1228 memset(alpha, wxIMAGE_ALPHA_OPAQUE, lenAlpha);
1229 }
1230 }
1231
1232 // ----------------------------------------------------------------------------
1233 // mask support
1234 // ----------------------------------------------------------------------------
1235
1236 void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b )
1237 {
1238 wxCHECK_RET( Ok(), wxT("invalid image") );
1239
1240 AllocExclusive();
1241
1242 M_IMGDATA->m_maskRed = r;
1243 M_IMGDATA->m_maskGreen = g;
1244 M_IMGDATA->m_maskBlue = b;
1245 M_IMGDATA->m_hasMask = true;
1246 }
1247
1248 bool wxImage::GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned char *b ) const
1249 {
1250 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1251
1252 if (M_IMGDATA->m_hasMask)
1253 {
1254 if (r) *r = M_IMGDATA->m_maskRed;
1255 if (g) *g = M_IMGDATA->m_maskGreen;
1256 if (b) *b = M_IMGDATA->m_maskBlue;
1257 return true;
1258 }
1259 else
1260 {
1261 FindFirstUnusedColour(r, g, b);
1262 return false;
1263 }
1264 }
1265
1266 unsigned char wxImage::GetMaskRed() const
1267 {
1268 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1269
1270 return M_IMGDATA->m_maskRed;
1271 }
1272
1273 unsigned char wxImage::GetMaskGreen() const
1274 {
1275 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1276
1277 return M_IMGDATA->m_maskGreen;
1278 }
1279
1280 unsigned char wxImage::GetMaskBlue() const
1281 {
1282 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1283
1284 return M_IMGDATA->m_maskBlue;
1285 }
1286
1287 void wxImage::SetMask( bool mask )
1288 {
1289 wxCHECK_RET( Ok(), wxT("invalid image") );
1290
1291 AllocExclusive();
1292
1293 M_IMGDATA->m_hasMask = mask;
1294 }
1295
1296 bool wxImage::HasMask() const
1297 {
1298 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1299
1300 return M_IMGDATA->m_hasMask;
1301 }
1302
1303 bool wxImage::IsTransparent(int x, int y, unsigned char threshold) const
1304 {
1305 long pos = XYToIndex(x, y);
1306 wxCHECK_MSG( pos != -1, false, wxT("invalid image coordinates") );
1307
1308 // check mask
1309 if ( M_IMGDATA->m_hasMask )
1310 {
1311 const unsigned char *p = M_IMGDATA->m_data + 3*pos;
1312 if ( p[0] == M_IMGDATA->m_maskRed &&
1313 p[1] == M_IMGDATA->m_maskGreen &&
1314 p[2] == M_IMGDATA->m_maskBlue )
1315 {
1316 return true;
1317 }
1318 }
1319
1320 // then check alpha
1321 if ( M_IMGDATA->m_alpha )
1322 {
1323 if ( M_IMGDATA->m_alpha[pos] < threshold )
1324 {
1325 // transparent enough
1326 return true;
1327 }
1328 }
1329
1330 // not transparent
1331 return false;
1332 }
1333
1334 bool wxImage::SetMaskFromImage(const wxImage& mask,
1335 unsigned char mr, unsigned char mg, unsigned char mb)
1336 {
1337 // check that the images are the same size
1338 if ( (M_IMGDATA->m_height != mask.GetHeight() ) || (M_IMGDATA->m_width != mask.GetWidth () ) )
1339 {
1340 wxLogError( _("Image and mask have different sizes.") );
1341 return false;
1342 }
1343
1344 // find unused colour
1345 unsigned char r,g,b ;
1346 if (!FindFirstUnusedColour(&r, &g, &b))
1347 {
1348 wxLogError( _("No unused colour in image being masked.") );
1349 return false ;
1350 }
1351
1352 AllocExclusive();
1353
1354 unsigned char *imgdata = GetData();
1355 unsigned char *maskdata = mask.GetData();
1356
1357 const int w = GetWidth();
1358 const int h = GetHeight();
1359
1360 for (int j = 0; j < h; j++)
1361 {
1362 for (int i = 0; i < w; i++)
1363 {
1364 if ((maskdata[0] == mr) && (maskdata[1] == mg) && (maskdata[2] == mb))
1365 {
1366 imgdata[0] = r;
1367 imgdata[1] = g;
1368 imgdata[2] = b;
1369 }
1370 imgdata += 3;
1371 maskdata += 3;
1372 }
1373 }
1374
1375 SetMaskColour(r, g, b);
1376 SetMask(true);
1377
1378 return true;
1379 }
1380
1381 bool wxImage::ConvertAlphaToMask(unsigned char threshold)
1382 {
1383 if (!HasAlpha())
1384 return true;
1385
1386 unsigned char mr, mg, mb;
1387 if (!FindFirstUnusedColour(&mr, &mg, &mb))
1388 {
1389 wxLogError( _("No unused colour in image being masked.") );
1390 return false;
1391 }
1392
1393 AllocExclusive();
1394
1395 SetMask(true);
1396 SetMaskColour(mr, mg, mb);
1397
1398 unsigned char *imgdata = GetData();
1399 unsigned char *alphadata = GetAlpha();
1400
1401 int w = GetWidth();
1402 int h = GetHeight();
1403
1404 for (int y = 0; y < h; y++)
1405 {
1406 for (int x = 0; x < w; x++, imgdata += 3, alphadata++)
1407 {
1408 if (*alphadata < threshold)
1409 {
1410 imgdata[0] = mr;
1411 imgdata[1] = mg;
1412 imgdata[2] = mb;
1413 }
1414 }
1415 }
1416
1417 free(M_IMGDATA->m_alpha);
1418 M_IMGDATA->m_alpha = NULL;
1419
1420 return true;
1421 }
1422
1423 // ----------------------------------------------------------------------------
1424 // Palette functions
1425 // ----------------------------------------------------------------------------
1426
1427 #if wxUSE_PALETTE
1428
1429 bool wxImage::HasPalette() const
1430 {
1431 if (!Ok())
1432 return false;
1433
1434 return M_IMGDATA->m_palette.Ok();
1435 }
1436
1437 const wxPalette& wxImage::GetPalette() const
1438 {
1439 wxCHECK_MSG( Ok(), wxNullPalette, wxT("invalid image") );
1440
1441 return M_IMGDATA->m_palette;
1442 }
1443
1444 void wxImage::SetPalette(const wxPalette& palette)
1445 {
1446 wxCHECK_RET( Ok(), wxT("invalid image") );
1447
1448 AllocExclusive();
1449
1450 M_IMGDATA->m_palette = palette;
1451 }
1452
1453 #endif // wxUSE_PALETTE
1454
1455 // ----------------------------------------------------------------------------
1456 // Option functions (arbitrary name/value mapping)
1457 // ----------------------------------------------------------------------------
1458
1459 void wxImage::SetOption(const wxString& name, const wxString& value)
1460 {
1461 wxCHECK_RET( Ok(), wxT("invalid image") );
1462
1463 AllocExclusive();
1464
1465 int idx = M_IMGDATA->m_optionNames.Index(name, false);
1466 if (idx == wxNOT_FOUND)
1467 {
1468 M_IMGDATA->m_optionNames.Add(name);
1469 M_IMGDATA->m_optionValues.Add(value);
1470 }
1471 else
1472 {
1473 M_IMGDATA->m_optionNames[idx] = name;
1474 M_IMGDATA->m_optionValues[idx] = value;
1475 }
1476 }
1477
1478 void wxImage::SetOption(const wxString& name, int value)
1479 {
1480 wxString valStr;
1481 valStr.Printf(wxT("%d"), value);
1482 SetOption(name, valStr);
1483 }
1484
1485 wxString wxImage::GetOption(const wxString& name) const
1486 {
1487 wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid image") );
1488
1489 int idx = M_IMGDATA->m_optionNames.Index(name, false);
1490 if (idx == wxNOT_FOUND)
1491 return wxEmptyString;
1492 else
1493 return M_IMGDATA->m_optionValues[idx];
1494 }
1495
1496 int wxImage::GetOptionInt(const wxString& name) const
1497 {
1498 return wxAtoi(GetOption(name));
1499 }
1500
1501 bool wxImage::HasOption(const wxString& name) const
1502 {
1503 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1504
1505 return (M_IMGDATA->m_optionNames.Index(name, false) != wxNOT_FOUND);
1506 }
1507
1508 // ----------------------------------------------------------------------------
1509 // image I/O
1510 // ----------------------------------------------------------------------------
1511
1512 bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
1513 long WXUNUSED_UNLESS_STREAMS(type),
1514 int WXUNUSED_UNLESS_STREAMS(index) )
1515 {
1516 #if HAS_FILE_STREAMS
1517 if (wxFileExists(filename))
1518 {
1519 wxImageFileInputStream stream(filename);
1520 wxBufferedInputStream bstream( stream );
1521 return LoadFile(bstream, type, index);
1522 }
1523 else
1524 {
1525 wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
1526
1527 return false;
1528 }
1529 #else // !HAS_FILE_STREAMS
1530 return false;
1531 #endif // HAS_FILE_STREAMS
1532 }
1533
1534 bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
1535 const wxString& WXUNUSED_UNLESS_STREAMS(mimetype),
1536 int WXUNUSED_UNLESS_STREAMS(index) )
1537 {
1538 #if HAS_FILE_STREAMS
1539 if (wxFileExists(filename))
1540 {
1541 wxImageFileInputStream stream(filename);
1542 wxBufferedInputStream bstream( stream );
1543 return LoadFile(bstream, mimetype, index);
1544 }
1545 else
1546 {
1547 wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
1548
1549 return false;
1550 }
1551 #else // !HAS_FILE_STREAMS
1552 return false;
1553 #endif // HAS_FILE_STREAMS
1554 }
1555
1556
1557
1558 bool wxImage::SaveFile( const wxString& filename ) const
1559 {
1560 wxString ext = filename.AfterLast('.').Lower();
1561
1562 wxImageHandler * pHandler = FindHandler(ext, -1);
1563 if (pHandler)
1564 {
1565 SaveFile(filename, pHandler->GetType());
1566 return true;
1567 }
1568
1569 wxLogError(_("Can't save image to file '%s': unknown extension."), filename.c_str());
1570
1571 return false;
1572 }
1573
1574 bool wxImage::SaveFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
1575 int WXUNUSED_UNLESS_STREAMS(type) ) const
1576 {
1577 #if HAS_FILE_STREAMS
1578 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1579
1580 ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
1581
1582 wxImageFileOutputStream stream(filename);
1583
1584 if ( stream.IsOk() )
1585 {
1586 wxBufferedOutputStream bstream( stream );
1587 return SaveFile(bstream, type);
1588 }
1589 #endif // HAS_FILE_STREAMS
1590
1591 return false;
1592 }
1593
1594 bool wxImage::SaveFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
1595 const wxString& WXUNUSED_UNLESS_STREAMS(mimetype) ) const
1596 {
1597 #if HAS_FILE_STREAMS
1598 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1599
1600 ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
1601
1602 wxImageFileOutputStream stream(filename);
1603
1604 if ( stream.IsOk() )
1605 {
1606 wxBufferedOutputStream bstream( stream );
1607 return SaveFile(bstream, mimetype);
1608 }
1609 #endif // HAS_FILE_STREAMS
1610
1611 return false;
1612 }
1613
1614 bool wxImage::CanRead( const wxString& WXUNUSED_UNLESS_STREAMS(name) )
1615 {
1616 #if HAS_FILE_STREAMS
1617 wxImageFileInputStream stream(name);
1618 return CanRead(stream);
1619 #else
1620 return false;
1621 #endif
1622 }
1623
1624 int wxImage::GetImageCount( const wxString& WXUNUSED_UNLESS_STREAMS(name),
1625 long WXUNUSED_UNLESS_STREAMS(type) )
1626 {
1627 #if HAS_FILE_STREAMS
1628 wxImageFileInputStream stream(name);
1629 if (stream.Ok())
1630 return GetImageCount(stream, type);
1631 #endif
1632
1633 return 0;
1634 }
1635
1636 #if wxUSE_STREAMS
1637
1638 bool wxImage::CanRead( wxInputStream &stream )
1639 {
1640 const wxList& list = GetHandlers();
1641
1642 for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() )
1643 {
1644 wxImageHandler *handler=(wxImageHandler*)node->GetData();
1645 if (handler->CanRead( stream ))
1646 return true;
1647 }
1648
1649 return false;
1650 }
1651
1652 int wxImage::GetImageCount( wxInputStream &stream, long type )
1653 {
1654 wxImageHandler *handler;
1655
1656 if ( type == wxBITMAP_TYPE_ANY )
1657 {
1658 wxList &list=GetHandlers();
1659
1660 for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext())
1661 {
1662 handler=(wxImageHandler*)node->GetData();
1663 if ( handler->CanRead(stream) )
1664 return handler->GetImageCount(stream);
1665
1666 }
1667
1668 wxLogWarning(_("No handler found for image type."));
1669 return 0;
1670 }
1671
1672 handler = FindHandler(type);
1673
1674 if ( !handler )
1675 {
1676 wxLogWarning(_("No image handler for type %ld defined."), type);
1677 return false;
1678 }
1679
1680 if ( handler->CanRead(stream) )
1681 {
1682 return handler->GetImageCount(stream);
1683 }
1684 else
1685 {
1686 wxLogError(_("Image file is not of type %ld."), type);
1687 return 0;
1688 }
1689 }
1690
1691 bool wxImage::LoadFile( wxInputStream& stream, long type, int index )
1692 {
1693 UnRef();
1694
1695 m_refData = new wxImageRefData;
1696
1697 wxImageHandler *handler;
1698
1699 if ( type == wxBITMAP_TYPE_ANY )
1700 {
1701 wxList &list=GetHandlers();
1702
1703 for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() )
1704 {
1705 handler=(wxImageHandler*)node->GetData();
1706 if ( handler->CanRead(stream) )
1707 return handler->LoadFile(this, stream, true/*verbose*/, index);
1708
1709 }
1710
1711 wxLogWarning( _("No handler found for image type.") );
1712 return false;
1713 }
1714
1715 handler = FindHandler(type);
1716
1717 if (handler == 0)
1718 {
1719 wxLogWarning( _("No image handler for type %ld defined."), type );
1720
1721 return false;
1722 }
1723
1724 if (stream.IsSeekable() && !handler->CanRead(stream))
1725 {
1726 wxLogError(_("Image file is not of type %ld."), type);
1727 return false;
1728 }
1729 else
1730 return handler->LoadFile(this, stream, true/*verbose*/, index);
1731 }
1732
1733 bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int index )
1734 {
1735 UnRef();
1736
1737 m_refData = new wxImageRefData;
1738
1739 wxImageHandler *handler = FindHandlerMime(mimetype);
1740
1741 if (handler == 0)
1742 {
1743 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
1744
1745 return false;
1746 }
1747
1748 if (stream.IsSeekable() && !handler->CanRead(stream))
1749 {
1750 wxLogError(_("Image file is not of type %s."), (const wxChar*) mimetype);
1751 return false;
1752 }
1753 else
1754 return handler->LoadFile( this, stream, true/*verbose*/, index );
1755 }
1756
1757 bool wxImage::SaveFile( wxOutputStream& stream, int type ) const
1758 {
1759 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1760
1761 wxImageHandler *handler = FindHandler(type);
1762 if ( !handler )
1763 {
1764 wxLogWarning( _("No image handler for type %d defined."), type );
1765
1766 return false;
1767 }
1768
1769 return handler->SaveFile( (wxImage*)this, stream );
1770 }
1771
1772 bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const
1773 {
1774 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1775
1776 wxImageHandler *handler = FindHandlerMime(mimetype);
1777 if ( !handler )
1778 {
1779 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
1780
1781 return false;
1782 }
1783
1784 return handler->SaveFile( (wxImage*)this, stream );
1785 }
1786 #endif // wxUSE_STREAMS
1787
1788 // ----------------------------------------------------------------------------
1789 // image I/O handlers
1790 // ----------------------------------------------------------------------------
1791
1792 void wxImage::AddHandler( wxImageHandler *handler )
1793 {
1794 // Check for an existing handler of the type being added.
1795 if (FindHandler( handler->GetType() ) == 0)
1796 {
1797 sm_handlers.Append( handler );
1798 }
1799 else
1800 {
1801 // This is not documented behaviour, merely the simplest 'fix'
1802 // for preventing duplicate additions. If someone ever has
1803 // a good reason to add and remove duplicate handlers (and they
1804 // may) we should probably refcount the duplicates.
1805 // also an issue in InsertHandler below.
1806
1807 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1808 handler->GetName().c_str() );
1809 delete handler;
1810 }
1811 }
1812
1813 void wxImage::InsertHandler( wxImageHandler *handler )
1814 {
1815 // Check for an existing handler of the type being added.
1816 if (FindHandler( handler->GetType() ) == 0)
1817 {
1818 sm_handlers.Insert( handler );
1819 }
1820 else
1821 {
1822 // see AddHandler for additional comments.
1823 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1824 handler->GetName().c_str() );
1825 delete handler;
1826 }
1827 }
1828
1829 bool wxImage::RemoveHandler( const wxString& name )
1830 {
1831 wxImageHandler *handler = FindHandler(name);
1832 if (handler)
1833 {
1834 sm_handlers.DeleteObject(handler);
1835 delete handler;
1836 return true;
1837 }
1838 else
1839 return false;
1840 }
1841
1842 wxImageHandler *wxImage::FindHandler( const wxString& name )
1843 {
1844 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1845 while (node)
1846 {
1847 wxImageHandler *handler = (wxImageHandler*)node->GetData();
1848 if (handler->GetName().Cmp(name) == 0) return handler;
1849
1850 node = node->GetNext();
1851 }
1852 return 0;
1853 }
1854
1855 wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType )
1856 {
1857 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1858 while (node)
1859 {
1860 wxImageHandler *handler = (wxImageHandler*)node->GetData();
1861 if ( (handler->GetExtension().Cmp(extension) == 0) &&
1862 (bitmapType == -1 || handler->GetType() == bitmapType) )
1863 return handler;
1864 node = node->GetNext();
1865 }
1866 return 0;
1867 }
1868
1869 wxImageHandler *wxImage::FindHandler( long bitmapType )
1870 {
1871 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1872 while (node)
1873 {
1874 wxImageHandler *handler = (wxImageHandler *)node->GetData();
1875 if (handler->GetType() == bitmapType) return handler;
1876 node = node->GetNext();
1877 }
1878 return 0;
1879 }
1880
1881 wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
1882 {
1883 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1884 while (node)
1885 {
1886 wxImageHandler *handler = (wxImageHandler *)node->GetData();
1887 if (handler->GetMimeType().IsSameAs(mimetype, false)) return handler;
1888 node = node->GetNext();
1889 }
1890 return 0;
1891 }
1892
1893 void wxImage::InitStandardHandlers()
1894 {
1895 #if wxUSE_STREAMS
1896 AddHandler(new wxBMPHandler);
1897 #endif // wxUSE_STREAMS
1898 }
1899
1900 void wxImage::CleanUpHandlers()
1901 {
1902 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1903 while (node)
1904 {
1905 wxImageHandler *handler = (wxImageHandler *)node->GetData();
1906 wxList::compatibility_iterator next = node->GetNext();
1907 delete handler;
1908 node = next;
1909 }
1910
1911 sm_handlers.Clear();
1912 }
1913
1914 wxString wxImage::GetImageExtWildcard()
1915 {
1916 wxString fmts;
1917
1918 wxList& Handlers = wxImage::GetHandlers();
1919 wxList::compatibility_iterator Node = Handlers.GetFirst();
1920 while ( Node )
1921 {
1922 wxImageHandler* Handler = (wxImageHandler*)Node->GetData();
1923 fmts += wxT("*.") + Handler->GetExtension();
1924 Node = Node->GetNext();
1925 if ( Node ) fmts += wxT(";");
1926 }
1927
1928 return wxT("(") + fmts + wxT(")|") + fmts;
1929 }
1930
1931 wxImage::HSVValue wxImage::RGBtoHSV(const RGBValue& rgb)
1932 {
1933 const double red = rgb.red / 255.0,
1934 green = rgb.green / 255.0,
1935 blue = rgb.blue / 255.0;
1936
1937 // find the min and max intensity (and remember which one was it for the
1938 // latter)
1939 double minimumRGB = red;
1940 if ( green < minimumRGB )
1941 minimumRGB = green;
1942 if ( blue < minimumRGB )
1943 minimumRGB = blue;
1944
1945 enum { RED, GREEN, BLUE } chMax = RED;
1946 double maximumRGB = red;
1947 if ( green > maximumRGB )
1948 {
1949 chMax = GREEN;
1950 maximumRGB = green;
1951 }
1952 if ( blue > maximumRGB )
1953 {
1954 chMax = BLUE;
1955 maximumRGB = blue;
1956 }
1957
1958 const double value = maximumRGB;
1959
1960 double hue = 0.0, saturation;
1961 const double deltaRGB = maximumRGB - minimumRGB;
1962 if ( wxIsNullDouble(deltaRGB) )
1963 {
1964 // Gray has no color
1965 hue = 0.0;
1966 saturation = 0.0;
1967 }
1968 else
1969 {
1970 switch ( chMax )
1971 {
1972 case RED:
1973 hue = (green - blue) / deltaRGB;
1974 break;
1975
1976 case GREEN:
1977 hue = 2.0 + (blue - red) / deltaRGB;
1978 break;
1979
1980 case BLUE:
1981 hue = 4.0 + (red - green) / deltaRGB;
1982 break;
1983
1984 default:
1985 wxFAIL_MSG(wxT("hue not specified"));
1986 break;
1987 }
1988
1989 hue /= 6.0;
1990
1991 if ( hue < 0.0 )
1992 hue += 1.0;
1993
1994 saturation = deltaRGB / maximumRGB;
1995 }
1996
1997 return HSVValue(hue, saturation, value);
1998 }
1999
2000 wxImage::RGBValue wxImage::HSVtoRGB(const HSVValue& hsv)
2001 {
2002 double red, green, blue;
2003
2004 if ( wxIsNullDouble(hsv.saturation) )
2005 {
2006 // Grey
2007 red = hsv.value;
2008 green = hsv.value;
2009 blue = hsv.value;
2010 }
2011 else // not grey
2012 {
2013 double hue = hsv.hue * 6.0; // sector 0 to 5
2014 int i = (int)floor(hue);
2015 double f = hue - i; // fractional part of h
2016 double p = hsv.value * (1.0 - hsv.saturation);
2017
2018 switch (i)
2019 {
2020 case 0:
2021 red = hsv.value;
2022 green = hsv.value * (1.0 - hsv.saturation * (1.0 - f));
2023 blue = p;
2024 break;
2025
2026 case 1:
2027 red = hsv.value * (1.0 - hsv.saturation * f);
2028 green = hsv.value;
2029 blue = p;
2030 break;
2031
2032 case 2:
2033 red = p;
2034 green = hsv.value;
2035 blue = hsv.value * (1.0 - hsv.saturation * (1.0 - f));
2036 break;
2037
2038 case 3:
2039 red = p;
2040 green = hsv.value * (1.0 - hsv.saturation * f);
2041 blue = hsv.value;
2042 break;
2043
2044 case 4:
2045 red = hsv.value * (1.0 - hsv.saturation * (1.0 - f));
2046 green = p;
2047 blue = hsv.value;
2048 break;
2049
2050 default: // case 5:
2051 red = hsv.value;
2052 green = p;
2053 blue = hsv.value * (1.0 - hsv.saturation * f);
2054 break;
2055 }
2056 }
2057
2058 return RGBValue((unsigned char)(red * 255.0),
2059 (unsigned char)(green * 255.0),
2060 (unsigned char)(blue * 255.0));
2061 }
2062
2063 /*
2064 * Rotates the hue of each pixel of the image. angle is a double in the range
2065 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2066 */
2067 void wxImage::RotateHue(double angle)
2068 {
2069 AllocExclusive();
2070
2071 unsigned char *srcBytePtr;
2072 unsigned char *dstBytePtr;
2073 unsigned long count;
2074 wxImage::HSVValue hsv;
2075 wxImage::RGBValue rgb;
2076
2077 wxASSERT (angle >= -1.0 && angle <= 1.0);
2078 count = M_IMGDATA->m_width * M_IMGDATA->m_height;
2079 if ( count > 0 && !wxIsNullDouble(angle) )
2080 {
2081 srcBytePtr = M_IMGDATA->m_data;
2082 dstBytePtr = srcBytePtr;
2083 do
2084 {
2085 rgb.red = *srcBytePtr++;
2086 rgb.green = *srcBytePtr++;
2087 rgb.blue = *srcBytePtr++;
2088 hsv = RGBtoHSV(rgb);
2089
2090 hsv.hue = hsv.hue + angle;
2091 if (hsv.hue > 1.0)
2092 hsv.hue = hsv.hue - 1.0;
2093 else if (hsv.hue < 0.0)
2094 hsv.hue = hsv.hue + 1.0;
2095
2096 rgb = HSVtoRGB(hsv);
2097 *dstBytePtr++ = rgb.red;
2098 *dstBytePtr++ = rgb.green;
2099 *dstBytePtr++ = rgb.blue;
2100 } while (--count != 0);
2101 }
2102 }
2103
2104 //-----------------------------------------------------------------------------
2105 // wxImageHandler
2106 //-----------------------------------------------------------------------------
2107
2108 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject)
2109
2110 #if wxUSE_STREAMS
2111 bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) )
2112 {
2113 return false;
2114 }
2115
2116 bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
2117 {
2118 return false;
2119 }
2120
2121 int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) )
2122 {
2123 return 1;
2124 }
2125
2126 bool wxImageHandler::CanRead( const wxString& name )
2127 {
2128 if (wxFileExists(name))
2129 {
2130 wxImageFileInputStream stream(name);
2131 return CanRead(stream);
2132 }
2133
2134 wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() );
2135
2136 return false;
2137 }
2138
2139 bool wxImageHandler::CallDoCanRead(wxInputStream& stream)
2140 {
2141 wxFileOffset posOld = stream.TellI();
2142 if ( posOld == wxInvalidOffset )
2143 {
2144 // can't test unseekable stream
2145 return false;
2146 }
2147
2148 bool ok = DoCanRead(stream);
2149
2150 // restore the old position to be able to test other formats and so on
2151 if ( stream.SeekI(posOld) == wxInvalidOffset )
2152 {
2153 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2154
2155 // reading would fail anyhow as we're not at the right position
2156 return false;
2157 }
2158
2159 return ok;
2160 }
2161
2162 #endif // wxUSE_STREAMS
2163
2164 // ----------------------------------------------------------------------------
2165 // image histogram stuff
2166 // ----------------------------------------------------------------------------
2167
2168 bool
2169 wxImageHistogram::FindFirstUnusedColour(unsigned char *r,
2170 unsigned char *g,
2171 unsigned char *b,
2172 unsigned char r2,
2173 unsigned char b2,
2174 unsigned char g2) const
2175 {
2176 unsigned long key = MakeKey(r2, g2, b2);
2177
2178 while ( find(key) != end() )
2179 {
2180 // color already used
2181 r2++;
2182 if ( r2 >= 255 )
2183 {
2184 r2 = 0;
2185 g2++;
2186 if ( g2 >= 255 )
2187 {
2188 g2 = 0;
2189 b2++;
2190 if ( b2 >= 255 )
2191 {
2192 wxLogError(_("No unused colour in image.") );
2193 return false;
2194 }
2195 }
2196 }
2197
2198 key = MakeKey(r2, g2, b2);
2199 }
2200
2201 if ( r )
2202 *r = r2;
2203 if ( g )
2204 *g = g2;
2205 if ( b )
2206 *b = b2;
2207
2208 return true;
2209 }
2210
2211 bool
2212 wxImage::FindFirstUnusedColour(unsigned char *r,
2213 unsigned char *g,
2214 unsigned char *b,
2215 unsigned char r2,
2216 unsigned char b2,
2217 unsigned char g2) const
2218 {
2219 wxImageHistogram histogram;
2220
2221 ComputeHistogram(histogram);
2222
2223 return histogram.FindFirstUnusedColour(r, g, b, r2, g2, b2);
2224 }
2225
2226
2227
2228 // GRG, Dic/99
2229 // Counts and returns the number of different colours. Optionally stops
2230 // when it exceeds 'stopafter' different colours. This is useful, for
2231 // example, to see if the image can be saved as 8-bit (256 colour or
2232 // less, in this case it would be invoked as CountColours(256)). Default
2233 // value for stopafter is -1 (don't care).
2234 //
2235 unsigned long wxImage::CountColours( unsigned long stopafter ) const
2236 {
2237 wxHashTable h;
2238 wxObject dummy;
2239 unsigned char r, g, b;
2240 unsigned char *p;
2241 unsigned long size, nentries, key;
2242
2243 p = GetData();
2244 size = GetWidth() * GetHeight();
2245 nentries = 0;
2246
2247 for (unsigned long j = 0; (j < size) && (nentries <= stopafter) ; j++)
2248 {
2249 r = *(p++);
2250 g = *(p++);
2251 b = *(p++);
2252 key = wxImageHistogram::MakeKey(r, g, b);
2253
2254 if (h.Get(key) == NULL)
2255 {
2256 h.Put(key, &dummy);
2257 nentries++;
2258 }
2259 }
2260
2261 return nentries;
2262 }
2263
2264
2265 unsigned long wxImage::ComputeHistogram( wxImageHistogram &h ) const
2266 {
2267 unsigned char *p = GetData();
2268 unsigned long nentries = 0;
2269
2270 h.clear();
2271
2272 const unsigned long size = GetWidth() * GetHeight();
2273
2274 unsigned char r, g, b;
2275 for ( unsigned long n = 0; n < size; n++ )
2276 {
2277 r = *p++;
2278 g = *p++;
2279 b = *p++;
2280
2281 wxImageHistogramEntry& entry = h[wxImageHistogram::MakeKey(r, g, b)];
2282
2283 if ( entry.value++ == 0 )
2284 entry.index = nentries++;
2285 }
2286
2287 return nentries;
2288 }
2289
2290 /*
2291 * Rotation code by Carlos Moreno
2292 */
2293
2294 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2295 // does exactly the same thing. And I also got rid of wxRotationPixel
2296 // bacause of potential problems in architectures where alignment
2297 // is an issue, so I had to rewrite parts of the code.
2298
2299 static const double gs_Epsilon = 1e-10;
2300
2301 static inline int wxCint (double x)
2302 {
2303 return (x > 0) ? (int) (x + 0.5) : (int) (x - 0.5);
2304 }
2305
2306
2307 // Auxiliary function to rotate a point (x,y) with respect to point p0
2308 // make it inline and use a straight return to facilitate optimization
2309 // also, the function receives the sine and cosine of the angle to avoid
2310 // repeating the time-consuming calls to these functions -- sin/cos can
2311 // be computed and stored in the calling function.
2312
2313 inline wxRealPoint rotated_point (const wxRealPoint & p, double cos_angle, double sin_angle, const wxRealPoint & p0)
2314 {
2315 return wxRealPoint (p0.x + (p.x - p0.x) * cos_angle - (p.y - p0.y) * sin_angle,
2316 p0.y + (p.y - p0.y) * cos_angle + (p.x - p0.x) * sin_angle);
2317 }
2318
2319 inline wxRealPoint rotated_point (double x, double y, double cos_angle, double sin_angle, const wxRealPoint & p0)
2320 {
2321 return rotated_point (wxRealPoint(x,y), cos_angle, sin_angle, p0);
2322 }
2323
2324 wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool interpolating, wxPoint * offset_after_rotation) const
2325 {
2326 int i;
2327 angle = -angle; // screen coordinates are a mirror image of "real" coordinates
2328
2329 bool has_alpha = HasAlpha();
2330
2331 // Create pointer-based array to accelerate access to wxImage's data
2332 unsigned char ** data = new unsigned char * [GetHeight()];
2333 data[0] = GetData();
2334 for (i = 1; i < GetHeight(); i++)
2335 data[i] = data[i - 1] + (3 * GetWidth());
2336
2337 // Same for alpha channel
2338 unsigned char ** alpha = NULL;
2339 if (has_alpha)
2340 {
2341 alpha = new unsigned char * [GetHeight()];
2342 alpha[0] = GetAlpha();
2343 for (i = 1; i < GetHeight(); i++)
2344 alpha[i] = alpha[i - 1] + GetWidth();
2345 }
2346
2347 // precompute coefficients for rotation formula
2348 // (sine and cosine of the angle)
2349 const double cos_angle = cos(angle);
2350 const double sin_angle = sin(angle);
2351
2352 // Create new Image to store the result
2353 // First, find rectangle that covers the rotated image; to do that,
2354 // rotate the four corners
2355
2356 const wxRealPoint p0(centre_of_rotation.x, centre_of_rotation.y);
2357
2358 wxRealPoint p1 = rotated_point (0, 0, cos_angle, sin_angle, p0);
2359 wxRealPoint p2 = rotated_point (0, GetHeight(), cos_angle, sin_angle, p0);
2360 wxRealPoint p3 = rotated_point (GetWidth(), 0, cos_angle, sin_angle, p0);
2361 wxRealPoint p4 = rotated_point (GetWidth(), GetHeight(), cos_angle, sin_angle, p0);
2362
2363 int x1a = (int) floor (wxMin (wxMin(p1.x, p2.x), wxMin(p3.x, p4.x)));
2364 int y1a = (int) floor (wxMin (wxMin(p1.y, p2.y), wxMin(p3.y, p4.y)));
2365 int x2a = (int) ceil (wxMax (wxMax(p1.x, p2.x), wxMax(p3.x, p4.x)));
2366 int y2a = (int) ceil (wxMax (wxMax(p1.y, p2.y), wxMax(p3.y, p4.y)));
2367
2368 // Create rotated image
2369 wxImage rotated (x2a - x1a + 1, y2a - y1a + 1, false);
2370 // With alpha channel
2371 if (has_alpha)
2372 rotated.SetAlpha();
2373
2374 if (offset_after_rotation != NULL)
2375 {
2376 *offset_after_rotation = wxPoint (x1a, y1a);
2377 }
2378
2379 // GRG: The rotated (destination) image is always accessed
2380 // sequentially, so there is no need for a pointer-based
2381 // array here (and in fact it would be slower).
2382 //
2383 unsigned char * dst = rotated.GetData();
2384
2385 unsigned char * alpha_dst = NULL;
2386 if (has_alpha)
2387 alpha_dst = rotated.GetAlpha();
2388
2389 // GRG: if the original image has a mask, use its RGB values
2390 // as the blank pixel, else, fall back to default (black).
2391 //
2392 unsigned char blank_r = 0;
2393 unsigned char blank_g = 0;
2394 unsigned char blank_b = 0;
2395
2396 if (HasMask())
2397 {
2398 blank_r = GetMaskRed();
2399 blank_g = GetMaskGreen();
2400 blank_b = GetMaskBlue();
2401 rotated.SetMaskColour( blank_r, blank_g, blank_b );
2402 }
2403
2404 // Now, for each point of the rotated image, find where it came from, by
2405 // performing an inverse rotation (a rotation of -angle) and getting the
2406 // pixel at those coordinates
2407
2408 // GRG: I've taken the (interpolating) test out of the loops, so that
2409 // it is done only once, instead of repeating it for each pixel.
2410
2411 int x;
2412 if (interpolating)
2413 {
2414 for (int y = 0; y < rotated.GetHeight(); y++)
2415 {
2416 for (x = 0; x < rotated.GetWidth(); x++)
2417 {
2418 wxRealPoint src = rotated_point (x + x1a, y + y1a, cos_angle, -sin_angle, p0);
2419
2420 if (-0.25 < src.x && src.x < GetWidth() - 0.75 &&
2421 -0.25 < src.y && src.y < GetHeight() - 0.75)
2422 {
2423 // interpolate using the 4 enclosing grid-points. Those
2424 // points can be obtained using floor and ceiling of the
2425 // exact coordinates of the point
2426 int x1, y1, x2, y2;
2427
2428 if (0 < src.x && src.x < GetWidth() - 1)
2429 {
2430 x1 = wxCint(floor(src.x));
2431 x2 = wxCint(ceil(src.x));
2432 }
2433 else // else means that x is near one of the borders (0 or width-1)
2434 {
2435 x1 = x2 = wxCint (src.x);
2436 }
2437
2438 if (0 < src.y && src.y < GetHeight() - 1)
2439 {
2440 y1 = wxCint(floor(src.y));
2441 y2 = wxCint(ceil(src.y));
2442 }
2443 else
2444 {
2445 y1 = y2 = wxCint (src.y);
2446 }
2447
2448 // get four points and the distances (square of the distance,
2449 // for efficiency reasons) for the interpolation formula
2450
2451 // GRG: Do not calculate the points until they are
2452 // really needed -- this way we can calculate
2453 // just one, instead of four, if d1, d2, d3
2454 // or d4 are < gs_Epsilon
2455
2456 const double d1 = (src.x - x1) * (src.x - x1) + (src.y - y1) * (src.y - y1);
2457 const double d2 = (src.x - x2) * (src.x - x2) + (src.y - y1) * (src.y - y1);
2458 const double d3 = (src.x - x2) * (src.x - x2) + (src.y - y2) * (src.y - y2);
2459 const double d4 = (src.x - x1) * (src.x - x1) + (src.y - y2) * (src.y - y2);
2460
2461 // Now interpolate as a weighted average of the four surrounding
2462 // points, where the weights are the distances to each of those points
2463
2464 // If the point is exactly at one point of the grid of the source
2465 // image, then don't interpolate -- just assign the pixel
2466
2467 if (d1 < gs_Epsilon) // d1,d2,d3,d4 are positive -- no need for abs()
2468 {
2469 unsigned char *p = data[y1] + (3 * x1);
2470 *(dst++) = *(p++);
2471 *(dst++) = *(p++);
2472 *(dst++) = *p;
2473
2474 if (has_alpha)
2475 *(alpha_dst++) = *(alpha[y1] + x1);
2476 }
2477 else if (d2 < gs_Epsilon)
2478 {
2479 unsigned char *p = data[y1] + (3 * x2);
2480 *(dst++) = *(p++);
2481 *(dst++) = *(p++);
2482 *(dst++) = *p;
2483
2484 if (has_alpha)
2485 *(alpha_dst++) = *(alpha[y1] + x2);
2486 }
2487 else if (d3 < gs_Epsilon)
2488 {
2489 unsigned char *p = data[y2] + (3 * x2);
2490 *(dst++) = *(p++);
2491 *(dst++) = *(p++);
2492 *(dst++) = *p;
2493
2494 if (has_alpha)
2495 *(alpha_dst++) = *(alpha[y2] + x2);
2496 }
2497 else if (d4 < gs_Epsilon)
2498 {
2499 unsigned char *p = data[y2] + (3 * x1);
2500 *(dst++) = *(p++);
2501 *(dst++) = *(p++);
2502 *(dst++) = *p;
2503
2504 if (has_alpha)
2505 *(alpha_dst++) = *(alpha[y2] + x1);
2506 }
2507 else
2508 {
2509 // weights for the weighted average are proportional to the inverse of the distance
2510 unsigned char *v1 = data[y1] + (3 * x1);
2511 unsigned char *v2 = data[y1] + (3 * x2);
2512 unsigned char *v3 = data[y2] + (3 * x2);
2513 unsigned char *v4 = data[y2] + (3 * x1);
2514
2515 const double w1 = 1/d1, w2 = 1/d2, w3 = 1/d3, w4 = 1/d4;
2516
2517 // GRG: Unrolled.
2518
2519 *(dst++) = (unsigned char)
2520 ( (w1 * *(v1++) + w2 * *(v2++) +
2521 w3 * *(v3++) + w4 * *(v4++)) /
2522 (w1 + w2 + w3 + w4) );
2523 *(dst++) = (unsigned char)
2524 ( (w1 * *(v1++) + w2 * *(v2++) +
2525 w3 * *(v3++) + w4 * *(v4++)) /
2526 (w1 + w2 + w3 + w4) );
2527 *(dst++) = (unsigned char)
2528 ( (w1 * *v1 + w2 * *v2 +
2529 w3 * *v3 + w4 * *v4) /
2530 (w1 + w2 + w3 + w4) );
2531
2532 if (has_alpha)
2533 {
2534 v1 = alpha[y1] + (x1);
2535 v2 = alpha[y1] + (x2);
2536 v3 = alpha[y2] + (x2);
2537 v4 = alpha[y2] + (x1);
2538
2539 *(alpha_dst++) = (unsigned char)
2540 ( (w1 * *v1 + w2 * *v2 +
2541 w3 * *v3 + w4 * *v4) /
2542 (w1 + w2 + w3 + w4) );
2543 }
2544 }
2545 }
2546 else
2547 {
2548 *(dst++) = blank_r;
2549 *(dst++) = blank_g;
2550 *(dst++) = blank_b;
2551
2552 if (has_alpha)
2553 *(alpha_dst++) = 0;
2554 }
2555 }
2556 }
2557 }
2558 else // not interpolating
2559 {
2560 for (int y = 0; y < rotated.GetHeight(); y++)
2561 {
2562 for (x = 0; x < rotated.GetWidth(); x++)
2563 {
2564 wxRealPoint src = rotated_point (x + x1a, y + y1a, cos_angle, -sin_angle, p0);
2565
2566 const int xs = wxCint (src.x); // wxCint rounds to the
2567 const int ys = wxCint (src.y); // closest integer
2568
2569 if (0 <= xs && xs < GetWidth() &&
2570 0 <= ys && ys < GetHeight())
2571 {
2572 unsigned char *p = data[ys] + (3 * xs);
2573 *(dst++) = *(p++);
2574 *(dst++) = *(p++);
2575 *(dst++) = *p;
2576
2577 if (has_alpha)
2578 *(alpha_dst++) = *(alpha[ys] + (xs));
2579 }
2580 else
2581 {
2582 *(dst++) = blank_r;
2583 *(dst++) = blank_g;
2584 *(dst++) = blank_b;
2585
2586 if (has_alpha)
2587 *(alpha_dst++) = 255;
2588 }
2589 }
2590 }
2591 }
2592
2593 delete [] data;
2594
2595 if (has_alpha)
2596 delete [] alpha;
2597
2598 return rotated;
2599 }
2600
2601
2602
2603
2604
2605 // A module to allow wxImage initialization/cleanup
2606 // without calling these functions from app.cpp or from
2607 // the user's application.
2608
2609 class wxImageModule: public wxModule
2610 {
2611 DECLARE_DYNAMIC_CLASS(wxImageModule)
2612 public:
2613 wxImageModule() {}
2614 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2615 void OnExit() { wxImage::CleanUpHandlers(); };
2616 };
2617
2618 IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule)
2619
2620
2621 #endif // wxUSE_IMAGE