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