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