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