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