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