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