]> git.saurik.com Git - wxWidgets.git/blob - src/common/image.cpp
Commited latest SciTech changes into CVS. This includes updates to the
[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 wxLogError(_("Can't open file '%s'"), filename);
771 return FALSE;
772 }
773 wxInputStream *stream = file->GetStream();
774 if (!stream) {
775 wxLogError(_("Can't open stream for file '%s'"), filename);
776 return FALSE;
777 }
778 return LoadFile(*stream, type);
779 #else // !wxUSE_STREAMS
780 return FALSE;
781 #endif // wxUSE_STREAMS
782 }
783
784 bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype )
785 {
786 #if wxUSE_STREAMS
787 // We want to use wxFileSystem for virtual FS compatibility
788 wxFileSystem fsys;
789 wxFSFile *file = fsys.OpenFile(filename);
790 if (!file) {
791 wxLogError(_("Can't open file '%s'"), filename);
792 return FALSE;
793 }
794 wxInputStream *stream = file->GetStream();
795 if (!stream) {
796 wxLogError(_("Can't open stream for file '%s'"), filename);
797 return FALSE;
798 }
799 return LoadFile(*stream, mimetype);
800 #else // !wxUSE_STREAMS
801 return FALSE;
802 #endif // wxUSE_STREAMS
803 }
804
805 bool wxImage::SaveFile( const wxString& filename, int type )
806 {
807 #if wxUSE_STREAMS
808 wxFileOutputStream stream(filename);
809
810 if ( stream.LastError() == wxStream_NOERROR )
811 {
812 wxBufferedOutputStream bstream( stream );
813 return SaveFile(bstream, type);
814 }
815 #endif // wxUSE_STREAMS
816
817 return FALSE;
818 }
819
820 bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype )
821 {
822 #if wxUSE_STREAMS
823 wxFileOutputStream stream(filename);
824
825 if ( stream.LastError() == wxStream_NOERROR )
826 {
827 wxBufferedOutputStream bstream( stream );
828 return SaveFile(bstream, mimetype);
829 }
830 #endif // wxUSE_STREAMS
831
832 return FALSE;
833 }
834
835 bool wxImage::CanRead( const wxString &name )
836 {
837 #if wxUSE_STREAMS
838 wxFileInputStream stream(name);
839 return CanRead(stream);
840 #else
841 return FALSE;
842 #endif
843 }
844
845 #if wxUSE_STREAMS
846
847 bool wxImage::CanRead( wxInputStream &stream )
848 {
849 wxList &list=GetHandlers();
850
851 for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
852 {
853 wxImageHandler *handler=(wxImageHandler*)node->GetData();
854 if (handler->CanRead( stream ))
855 return TRUE;
856 }
857
858 return FALSE;
859 }
860
861 bool wxImage::LoadFile( wxInputStream& stream, long type )
862 {
863 UnRef();
864
865 m_refData = new wxImageRefData;
866
867 wxImageHandler *handler;
868
869 if (type==wxBITMAP_TYPE_ANY)
870 {
871 wxList &list=GetHandlers();
872
873 for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
874 {
875 handler=(wxImageHandler*)node->GetData();
876 if (handler->CanRead( stream ))
877 return handler->LoadFile( this, stream );
878
879 }
880
881 wxLogWarning( _("No handler found for image type.") );
882 return FALSE;
883 }
884
885 handler = FindHandler(type);
886
887 if (handler == NULL)
888 {
889 wxLogWarning( _("No image handler for type %d defined."), type );
890
891 return FALSE;
892 }
893
894 return handler->LoadFile( this, stream );
895 }
896
897 bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype )
898 {
899 UnRef();
900
901 m_refData = new wxImageRefData;
902
903 wxImageHandler *handler = FindHandlerMime(mimetype);
904
905 if (handler == NULL)
906 {
907 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
908
909 return FALSE;
910 }
911
912 return handler->LoadFile( this, stream );
913 }
914
915 bool wxImage::SaveFile( wxOutputStream& stream, int type )
916 {
917 wxCHECK_MSG( Ok(), FALSE, wxT("invalid image") );
918
919 wxImageHandler *handler = FindHandler(type);
920
921 if (handler == NULL)
922 {
923 wxLogWarning( _("No image handler for type %d defined."), type );
924
925 return FALSE;
926 }
927
928 return handler->SaveFile( this, stream );
929 }
930
931 bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype )
932 {
933 wxCHECK_MSG( Ok(), FALSE, wxT("invalid image") );
934
935 wxImageHandler *handler = FindHandlerMime(mimetype);
936
937 if (handler == NULL)
938 {
939 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
940
941 return FALSE;
942 }
943
944 return handler->SaveFile( this, stream );
945 }
946 #endif // wxUSE_STREAMS
947
948 void wxImage::AddHandler( wxImageHandler *handler )
949 {
950 // make sure that the memory will be freed at the program end
951 sm_handlers.DeleteContents(TRUE);
952
953 sm_handlers.Append( handler );
954 }
955
956 void wxImage::InsertHandler( wxImageHandler *handler )
957 {
958 // make sure that the memory will be freed at the program end
959 sm_handlers.DeleteContents(TRUE);
960
961 sm_handlers.Insert( handler );
962 }
963
964 bool wxImage::RemoveHandler( const wxString& name )
965 {
966 wxImageHandler *handler = FindHandler(name);
967 if (handler)
968 {
969 sm_handlers.DeleteObject(handler);
970 return TRUE;
971 }
972 else
973 return FALSE;
974 }
975
976 wxImageHandler *wxImage::FindHandler( const wxString& name )
977 {
978 wxNode *node = sm_handlers.First();
979 while (node)
980 {
981 wxImageHandler *handler = (wxImageHandler*)node->Data();
982 if (handler->GetName().Cmp(name) == 0) return handler;
983
984 node = node->Next();
985 }
986 return (wxImageHandler *)NULL;
987 }
988
989 wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType )
990 {
991 wxNode *node = sm_handlers.First();
992 while (node)
993 {
994 wxImageHandler *handler = (wxImageHandler*)node->Data();
995 if ( (handler->GetExtension().Cmp(extension) == 0) &&
996 (bitmapType == -1 || handler->GetType() == bitmapType) )
997 return handler;
998 node = node->Next();
999 }
1000 return (wxImageHandler*)NULL;
1001 }
1002
1003 wxImageHandler *wxImage::FindHandler( long bitmapType )
1004 {
1005 wxNode *node = sm_handlers.First();
1006 while (node)
1007 {
1008 wxImageHandler *handler = (wxImageHandler *)node->Data();
1009 if (handler->GetType() == bitmapType) return handler;
1010 node = node->Next();
1011 }
1012 return NULL;
1013 }
1014
1015 wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
1016 {
1017 wxNode *node = sm_handlers.First();
1018 while (node)
1019 {
1020 wxImageHandler *handler = (wxImageHandler *)node->Data();
1021 if (handler->GetMimeType().IsSameAs(mimetype, FALSE)) return handler;
1022 node = node->Next();
1023 }
1024 return NULL;
1025 }
1026
1027 void wxImage::InitStandardHandlers()
1028 {
1029 AddHandler(new wxBMPHandler);
1030 #if !defined(__WXGTK__) && !defined(__WXMOTIF__)
1031 AddHandler(new wxXPMHandler);
1032 #endif
1033 }
1034
1035 void wxImage::CleanUpHandlers()
1036 {
1037 wxNode *node = sm_handlers.First();
1038 while (node)
1039 {
1040 wxImageHandler *handler = (wxImageHandler *)node->Data();
1041 wxNode *next = node->Next();
1042 delete handler;
1043 delete node;
1044 node = next;
1045 }
1046 }
1047
1048 //-----------------------------------------------------------------------------
1049 // wxImageHandler
1050 //-----------------------------------------------------------------------------
1051
1052 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject)
1053
1054 #if wxUSE_STREAMS
1055 bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) )
1056 {
1057 return FALSE;
1058 }
1059
1060 bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
1061 {
1062 return FALSE;
1063 }
1064
1065 int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) )
1066 {
1067 return 1;
1068 }
1069
1070 bool wxImageHandler::CanRead( const wxString& name )
1071 {
1072 if (wxFileExists(name))
1073 {
1074 wxFileInputStream stream(name);
1075 return CanRead(stream);
1076 }
1077
1078 else {
1079 wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() );
1080
1081 return FALSE;
1082 }
1083 // return FALSE;
1084 }
1085
1086 #endif // wxUSE_STREAMS
1087
1088
1089
1090 //-----------------------------------------------------------------------------
1091 // wxBitmap convertion routines
1092 //-----------------------------------------------------------------------------
1093
1094 #if wxUSE_GUI
1095
1096 #ifdef __WXGTK__
1097 wxBitmap wxImage::ConvertToMonoBitmap( unsigned char red, unsigned char green, unsigned char blue ) const
1098 {
1099 wxImage mono = this->ConvertToMono( red, green, blue );
1100 wxBitmap bitmap( mono, 1 );
1101 return bitmap;
1102 }
1103 #endif
1104
1105 wxBitmap wxImage::ConvertToBitmap() const
1106 {
1107 wxBitmap bitmap( *this );
1108 return bitmap;
1109 }
1110
1111 wxImage::wxImage( const wxBitmap &bitmap )
1112 {
1113 *this = bitmap.ConvertToImage();
1114 }
1115
1116 #endif
1117
1118
1119
1120 // A module to allow wxImage initialization/cleanup
1121 // without calling these functions from app.cpp or from
1122 // the user's application.
1123
1124 class wxImageModule: public wxModule
1125 {
1126 DECLARE_DYNAMIC_CLASS(wxImageModule)
1127 public:
1128 wxImageModule() {}
1129 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE; };
1130 void OnExit() { wxImage::CleanUpHandlers(); };
1131 };
1132
1133 IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule)
1134
1135
1136 //-----------------------------------------------------------------------------
1137
1138 // GRG, Dic/99
1139 // Counts and returns the number of different colours. Optionally stops
1140 // when it exceeds 'stopafter' different colours. This is useful, for
1141 // example, to see if the image can be saved as 8-bit (256 colour or
1142 // less, in this case it would be invoked as CountColours(256)). Default
1143 // value for stopafter is -1 (don't care).
1144 //
1145 unsigned long wxImage::CountColours( unsigned long stopafter )
1146 {
1147 wxHashTable h;
1148 wxObject dummy;
1149 unsigned char r, g, b;
1150 unsigned char *p;
1151 unsigned long size, nentries, key;
1152
1153 p = GetData();
1154 size = GetWidth() * GetHeight();
1155 nentries = 0;
1156
1157 for (unsigned long j = 0; (j < size) && (nentries <= stopafter) ; j++)
1158 {
1159 r = *(p++);
1160 g = *(p++);
1161 b = *(p++);
1162 key = (r << 16) | (g << 8) | b;
1163
1164 if (h.Get(key) == NULL)
1165 {
1166 h.Put(key, &dummy);
1167 nentries++;
1168 }
1169 }
1170
1171 return nentries;
1172 }
1173
1174
1175 // GRG, Dic/99
1176 // Computes the histogram of the image and fills a hash table, indexed
1177 // with integer keys built as 0xRRGGBB, containing wxHNode objects. Each
1178 // wxHNode contains an 'index' (useful to build a palette with the image
1179 // colours) and a 'value', which is the number of pixels in the image with
1180 // that colour.
1181 //
1182 unsigned long wxImage::ComputeHistogram( wxHashTable &h )
1183 {
1184 unsigned char r, g, b;
1185 unsigned char *p;
1186 unsigned long size, nentries, key;
1187 wxHNode *hnode;
1188
1189 p = GetData();
1190 size = GetWidth() * GetHeight();
1191 nentries = 0;
1192
1193 for (unsigned long j = 0; j < size; j++)
1194 {
1195 r = *(p++);
1196 g = *(p++);
1197 b = *(p++);
1198 key = (r << 16) | (g << 8) | b;
1199
1200 hnode = (wxHNode *) h.Get(key);
1201
1202 if (hnode)
1203 hnode->value++;
1204 else
1205 {
1206 hnode = new wxHNode();
1207 hnode->index = nentries++;
1208 hnode->value = 1;
1209
1210 h.Put(key, (wxObject *)hnode);
1211 }
1212 }
1213
1214 return nentries;
1215 }
1216
1217 /*
1218 * Rotation code by Carlos Moreno
1219 */
1220
1221 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1222 // does exactly the same thing. And I also got rid of wxRotationPixel
1223 // bacause of potential problems in architectures where alignment
1224 // is an issue, so I had to rewrite parts of the code.
1225
1226 static const double gs_Epsilon = 1e-10;
1227
1228 static inline int wxCint (double x)
1229 {
1230 return (x > 0) ? (int) (x + 0.5) : (int) (x - 0.5);
1231 }
1232
1233
1234 // Auxiliary function to rotate a point (x,y) with respect to point p0
1235 // make it inline and use a straight return to facilitate optimization
1236 // also, the function receives the sine and cosine of the angle to avoid
1237 // repeating the time-consuming calls to these functions -- sin/cos can
1238 // be computed and stored in the calling function.
1239
1240 inline wxRealPoint rotated_point (const wxRealPoint & p, double cos_angle, double sin_angle, const wxRealPoint & p0)
1241 {
1242 return wxRealPoint (p0.x + (p.x - p0.x) * cos_angle - (p.y - p0.y) * sin_angle,
1243 p0.y + (p.y - p0.y) * cos_angle + (p.x - p0.x) * sin_angle);
1244 }
1245
1246 inline wxRealPoint rotated_point (double x, double y, double cos_angle, double sin_angle, const wxRealPoint & p0)
1247 {
1248 return rotated_point (wxRealPoint(x,y), cos_angle, sin_angle, p0);
1249 }
1250
1251 wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool interpolating, wxPoint * offset_after_rotation) const
1252 {
1253 int i;
1254 angle = -angle; // screen coordinates are a mirror image of "real" coordinates
1255
1256 // Create pointer-based array to accelerate access to wxImage's data
1257 unsigned char ** data = new unsigned char * [GetHeight()];
1258
1259 data[0] = GetData();
1260
1261 for (i = 1; i < GetHeight(); i++)
1262 data[i] = data[i - 1] + (3 * GetWidth());
1263
1264 // precompute coefficients for rotation formula
1265 // (sine and cosine of the angle)
1266 const double cos_angle = cos(angle);
1267 const double sin_angle = sin(angle);
1268
1269 // Create new Image to store the result
1270 // First, find rectangle that covers the rotated image; to do that,
1271 // rotate the four corners
1272
1273 const wxRealPoint p0(centre_of_rotation.x, centre_of_rotation.y);
1274
1275 wxRealPoint p1 = rotated_point (0, 0, cos_angle, sin_angle, p0);
1276 wxRealPoint p2 = rotated_point (0, GetHeight(), cos_angle, sin_angle, p0);
1277 wxRealPoint p3 = rotated_point (GetWidth(), 0, cos_angle, sin_angle, p0);
1278 wxRealPoint p4 = rotated_point (GetWidth(), GetHeight(), cos_angle, sin_angle, p0);
1279
1280 int x1 = (int) floor (wxMin (wxMin(p1.x, p2.x), wxMin(p3.x, p4.x)));
1281 int y1 = (int) floor (wxMin (wxMin(p1.y, p2.y), wxMin(p3.y, p4.y)));
1282 int x2 = (int) ceil (wxMax (wxMax(p1.x, p2.x), wxMax(p3.x, p4.x)));
1283 int y2 = (int) ceil (wxMax (wxMax(p1.y, p2.y), wxMax(p3.y, p4.y)));
1284
1285 wxImage rotated (x2 - x1 + 1, y2 - y1 + 1);
1286
1287 if (offset_after_rotation != NULL)
1288 {
1289 *offset_after_rotation = wxPoint (x1, y1);
1290 }
1291
1292 // GRG: The rotated (destination) image is always accessed
1293 // sequentially, so there is no need for a pointer-based
1294 // array here (and in fact it would be slower).
1295 //
1296 unsigned char * dst = rotated.GetData();
1297
1298 // GRG: if the original image has a mask, use its RGB values
1299 // as the blank pixel, else, fall back to default (black).
1300 //
1301 unsigned char blank_r = 0;
1302 unsigned char blank_g = 0;
1303 unsigned char blank_b = 0;
1304
1305 if (HasMask())
1306 {
1307 blank_r = GetMaskRed();
1308 blank_g = GetMaskGreen();
1309 blank_b = GetMaskBlue();
1310 rotated.SetMaskColour( blank_r, blank_g, blank_b );
1311 }
1312
1313 // Now, for each point of the rotated image, find where it came from, by
1314 // performing an inverse rotation (a rotation of -angle) and getting the
1315 // pixel at those coordinates
1316
1317 // GRG: I've taken the (interpolating) test out of the loops, so that
1318 // it is done only once, instead of repeating it for each pixel.
1319
1320 int x;
1321 if (interpolating)
1322 {
1323 for (int y = 0; y < rotated.GetHeight(); y++)
1324 {
1325 for (x = 0; x < rotated.GetWidth(); x++)
1326 {
1327 wxRealPoint src = rotated_point (x + x1, y + y1, cos_angle, -sin_angle, p0);
1328
1329 if (-0.25 < src.x && src.x < GetWidth() - 0.75 &&
1330 -0.25 < src.y && src.y < GetHeight() - 0.75)
1331 {
1332 // interpolate using the 4 enclosing grid-points. Those
1333 // points can be obtained using floor and ceiling of the
1334 // exact coordinates of the point
1335 // C.M. 2000-02-17: when the point is near the border, special care is required.
1336
1337 int x1, y1, x2, y2;
1338
1339 if (0 < src.x && src.x < GetWidth() - 1)
1340 {
1341 x1 = wxCint(floor(src.x));
1342 x2 = wxCint(ceil(src.x));
1343 }
1344 else // else means that x is near one of the borders (0 or width-1)
1345 {
1346 x1 = x2 = wxCint (src.x);
1347 }
1348
1349 if (0 < src.y && src.y < GetHeight() - 1)
1350 {
1351 y1 = wxCint(floor(src.y));
1352 y2 = wxCint(ceil(src.y));
1353 }
1354 else
1355 {
1356 y1 = y2 = wxCint (src.y);
1357 }
1358
1359 // get four points and the distances (square of the distance,
1360 // for efficiency reasons) for the interpolation formula
1361
1362 // GRG: Do not calculate the points until they are
1363 // really needed -- this way we can calculate
1364 // just one, instead of four, if d1, d2, d3
1365 // or d4 are < gs_Epsilon
1366
1367 const double d1 = (src.x - x1) * (src.x - x1) + (src.y - y1) * (src.y - y1);
1368 const double d2 = (src.x - x2) * (src.x - x2) + (src.y - y1) * (src.y - y1);
1369 const double d3 = (src.x - x2) * (src.x - x2) + (src.y - y2) * (src.y - y2);
1370 const double d4 = (src.x - x1) * (src.x - x1) + (src.y - y2) * (src.y - y2);
1371
1372 // Now interpolate as a weighted average of the four surrounding
1373 // points, where the weights are the distances to each of those points
1374
1375 // If the point is exactly at one point of the grid of the source
1376 // image, then don't interpolate -- just assign the pixel
1377
1378 if (d1 < gs_Epsilon) // d1,d2,d3,d4 are positive -- no need for abs()
1379 {
1380 unsigned char *p = data[y1] + (3 * x1);
1381 *(dst++) = *(p++);
1382 *(dst++) = *(p++);
1383 *(dst++) = *(p++);
1384 }
1385 else if (d2 < gs_Epsilon)
1386 {
1387 unsigned char *p = data[y1] + (3 * x2);
1388 *(dst++) = *(p++);
1389 *(dst++) = *(p++);
1390 *(dst++) = *(p++);
1391 }
1392 else if (d3 < gs_Epsilon)
1393 {
1394 unsigned char *p = data[y2] + (3 * x2);
1395 *(dst++) = *(p++);
1396 *(dst++) = *(p++);
1397 *(dst++) = *(p++);
1398 }
1399 else if (d4 < gs_Epsilon)
1400 {
1401 unsigned char *p = data[y2] + (3 * x1);
1402 *(dst++) = *(p++);
1403 *(dst++) = *(p++);
1404 *(dst++) = *(p++);
1405 }
1406 else
1407 {
1408 // weights for the weighted average are proportional to the inverse of the distance
1409 unsigned char *v1 = data[y1] + (3 * x1);
1410 unsigned char *v2 = data[y1] + (3 * x2);
1411 unsigned char *v3 = data[y2] + (3 * x2);
1412 unsigned char *v4 = data[y2] + (3 * x1);
1413
1414 const double w1 = 1/d1, w2 = 1/d2, w3 = 1/d3, w4 = 1/d4;
1415
1416 // GRG: Unrolled.
1417
1418 *(dst++) = (unsigned char)
1419 ( (w1 * *(v1++) + w2 * *(v2++) +
1420 w3 * *(v3++) + w4 * *(v4++)) /
1421 (w1 + w2 + w3 + w4) );
1422 *(dst++) = (unsigned char)
1423 ( (w1 * *(v1++) + w2 * *(v2++) +
1424 w3 * *(v3++) + w4 * *(v4++)) /
1425 (w1 + w2 + w3 + w4) );
1426 *(dst++) = (unsigned char)
1427 ( (w1 * *(v1++) + w2 * *(v2++) +
1428 w3 * *(v3++) + w4 * *(v4++)) /
1429 (w1 + w2 + w3 + w4) );
1430 }
1431 }
1432 else
1433 {
1434 *(dst++) = blank_r;
1435 *(dst++) = blank_g;
1436 *(dst++) = blank_b;
1437 }
1438 }
1439 }
1440 }
1441 else // not interpolating
1442 {
1443 for (int y = 0; y < rotated.GetHeight(); y++)
1444 {
1445 for (x = 0; x < rotated.GetWidth(); x++)
1446 {
1447 wxRealPoint src = rotated_point (x + x1, y + y1, cos_angle, -sin_angle, p0);
1448
1449 const int xs = wxCint (src.x); // wxCint rounds to the
1450 const int ys = wxCint (src.y); // closest integer
1451
1452 if (0 <= xs && xs < GetWidth() &&
1453 0 <= ys && ys < GetHeight())
1454 {
1455 unsigned char *p = data[ys] + (3 * xs);
1456 *(dst++) = *(p++);
1457 *(dst++) = *(p++);
1458 *(dst++) = *(p++);
1459 }
1460 else
1461 {
1462 *(dst++) = blank_r;
1463 *(dst++) = blank_g;
1464 *(dst++) = blank_b;
1465 }
1466 }
1467 }
1468 }
1469
1470 delete [] data;
1471
1472 return rotated;
1473 }
1474
1475 #endif // wxUSE_IMAGE