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