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