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