]> git.saurik.com Git - wxWidgets.git/blame - src/common/image.cpp
Cleaned up some and started using path joins and normalization instead
[wxWidgets.git] / src / common / image.cpp
CommitLineData
01111366
RR
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
0b4f9ee3
UU
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
edccf428 18 #pragma hdrstop
0b4f9ee3
UU
19#endif
20
c96ea657
VS
21#include "wx/defs.h"
22
23#if wxUSE_IMAGE
24
01111366 25#include "wx/image.h"
99c67c77 26#include "wx/bitmap.h"
01111366
RR
27#include "wx/debug.h"
28#include "wx/log.h"
f6fcbb63 29#include "wx/app.h"
dc86cb34 30#include "wx/filefn.h"
3d05544e 31#include "wx/wfstream.h"
b75867a6 32#include "wx/intl.h"
a91b47e8 33#include "wx/module.h"
01111366 34
58a8ab88
JS
35// For memcpy
36#include <string.h>
7a632f10 37#include <math.h>
58a8ab88 38
a3ef5bf5 39#ifdef __SALFORDC__
edccf428 40 #undef FAR
a3ef5bf5
JS
41#endif
42
2432b92d 43
01111366
RR
44//-----------------------------------------------------------------------------
45// wxImage
46//-----------------------------------------------------------------------------
47
48class wxImageRefData: public wxObjectRefData
49{
fd0eed64 50public:
edccf428
VZ
51 wxImageRefData();
52 ~wxImageRefData();
c7abc967 53
dbda9e86
JS
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;
f6bcfd97 60 bool m_static;
5e5437e0
JS
61 wxPalette m_palette;
62 wxArrayString m_optionNames;
63 wxArrayString m_optionValues;
01111366
RR
64};
65
edccf428 66wxImageRefData::wxImageRefData()
01111366 67{
fd0eed64
RR
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;
f6bcfd97 76 m_static = FALSE;
01111366
RR
77}
78
edccf428 79wxImageRefData::~wxImageRefData()
01111366 80{
f6bcfd97 81 if (m_data && !m_static)
58c837a4 82 free( m_data );
01111366
RR
83}
84
85wxList wxImage::sm_handlers;
86
fec19ea9
VS
87wxImage wxNullImage;
88
01111366
RR
89//-----------------------------------------------------------------------------
90
91#define M_IMGDATA ((wxImageRefData *)m_refData)
92
5e5437e0 93IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject)
01111366
RR
94
95wxImage::wxImage()
96{
97}
98
99wxImage::wxImage( int width, int height )
100{
fd0eed64 101 Create( width, height );
01111366
RR
102}
103
f6bcfd97
BP
104wxImage::wxImage( int width, int height, unsigned char* data, bool static_data )
105{
106 Create( width, height, data, static_data );
107}
108
01111366
RR
109wxImage::wxImage( const wxString& name, long type )
110{
fd0eed64 111 LoadFile( name, type );
01111366
RR
112}
113
9e9ee68e
VS
114wxImage::wxImage( const wxString& name, const wxString& mimetype )
115{
116 LoadFile( name, mimetype );
117}
118
e02afc7a 119#if wxUSE_STREAMS
3d05544e
JS
120wxImage::wxImage( wxInputStream& stream, long type )
121{
122 LoadFile( stream, type );
123}
9e9ee68e
VS
124
125wxImage::wxImage( wxInputStream& stream, const wxString& mimetype )
126{
127 LoadFile( stream, mimetype );
128}
e02afc7a 129#endif // wxUSE_STREAMS
3d05544e 130
4698648f
VZ
131wxImage::wxImage( const wxImage& image )
132{
133 Ref(image);
01111366
RR
134}
135
4698648f
VZ
136wxImage::wxImage( const wxImage* image )
137{
138 if (image) Ref(*image);
01111366
RR
139}
140
141void wxImage::Create( int width, int height )
142{
aadaf841
GRG
143 UnRef();
144
fd0eed64 145 m_refData = new wxImageRefData();
c7abc967 146
fd0eed64
RR
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;
c7abc967 151
fd0eed64
RR
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 }
01111366
RR
160}
161
f6bcfd97
BP
162void 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
01111366
RR
182void wxImage::Destroy()
183{
fd0eed64 184 UnRef();
01111366
RR
185}
186
f6bcfd97
BP
187wxImage 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
ce9a75d2 207wxImage wxImage::Scale( int width, int height ) const
4bc67cc5
RR
208{
209 wxImage image;
c7abc967 210
223d09f6 211 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
c7abc967 212
6721fc38
VZ
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") );
c7abc967 221
4bc67cc5 222 image.Create( width, height );
c7abc967 223
4bc67cc5 224 char unsigned *data = image.GetData();
c7abc967 225
223d09f6 226 wxCHECK_MSG( data, image, wxT("unable to create image") );
c7abc967 227
4bc67cc5 228 if (M_IMGDATA->m_hasMask)
6721fc38
VZ
229 {
230 image.SetMaskColour( M_IMGDATA->m_maskRed,
231 M_IMGDATA->m_maskGreen,
232 M_IMGDATA->m_maskBlue );
233 }
c7abc967 234
6e13c196
RR
235 char unsigned *source_data = M_IMGDATA->m_data;
236 char unsigned *target_data = data;
c7abc967 237
eeca3a46
RR
238#if 0
239 // This is nonsense, RR.
240
6721fc38
VZ
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++ )
4bc67cc5 249 {
6721fc38
VZ
250 // don't crash for images with height == 1
251 long y_offset = height ? (j * old_height / height)* old_old_width : 0;
c7abc967 252
6721fc38 253 for ( long i = 0; i <= width; i++ )
4698648f 254 {
6721fc38
VZ
255 long x_offset = width ? (i * old_width) / width : 0;
256
257 memcpy( target_data, source_data + 3*(y_offset + x_offset), 3 );
6e13c196 258 target_data += 3;
4698648f 259 }
4bc67cc5 260 }
eeca3a46
RR
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
c7abc967 275
4bc67cc5
RR
276 return image;
277}
4698648f 278
f6bcfd97
BP
279wxImage 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
316wxImage 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);
3ca6a5f0 356 memcpy( target_data, source_data, (size_t)3*width );
f6bcfd97
BP
357 source_data += 3*width;
358 }
359 }
360
361 return image;
362}
363
7b2471a0
SB
364wxImage wxImage::GetSubImage( const wxRect &rect ) const
365{
366 wxImage image;
367
223d09f6 368 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
7b2471a0 369
58c837a4
RR
370 wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) && (rect.GetRight()<=GetWidth()) && (rect.GetBottom()<=GetHeight()),
371 image, wxT("invalid subimage size") );
7b2471a0
SB
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
223d09f6 380 wxCHECK_MSG( subdata, image, wxT("unable to create image") );
7b2471a0
SB
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;
995612e2 388
7b2471a0
SB
389 data+=rect.GetTop()*width+subleft;
390
391 for (long j = 0; j < subheight; ++j)
392 {
393 memcpy( subdata, data, subwidth);
394 subdata+=subwidth;
995612e2 395 data+=width;
7b2471a0
SB
396 }
397
398 return image;
399}
400
f6bcfd97
BP
401void 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 }
aa21b509 448 return;
f6bcfd97 449 }
33ac7e6f 450
aa21b509 451 if (!HasMask() && image.HasMask())
f6bcfd97 452 {
aa21b509
RR
453 unsigned char r = image.GetMaskRed();
454 unsigned char g = image.GetMaskGreen();
455 unsigned char b = image.GetMaskBlue();
33ac7e6f 456
aa21b509
RR
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;
33ac7e6f 463
aa21b509
RR
464 for (int j = 0; j < height; j++)
465 {
466 for (int i = 0; i < width; i+=3)
467 {
33ac7e6f
KB
468 if ((source_data[i] != r) &&
469 (source_data[i+1] != g) &&
aa21b509
RR
470 (source_data[i+2] != b))
471 {
472 memcpy( target_data+i, source_data+i, 3 );
473 }
33ac7e6f 474 }
aa21b509
RR
475 source_data += source_step;
476 target_data += target_step;
477 }
f6bcfd97
BP
478 }
479}
480
be25e480
RR
481void 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();
06b466c7 487
be25e480
RR
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++)
069d0f27
VZ
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 }
be25e480
RR
502}
503
f515c25a 504wxImage wxImage::ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const
fec19ea9
VS
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
ef539066
RR
541void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b )
542{
223d09f6 543 wxCHECK_RET( Ok(), wxT("invalid image") );
c7abc967 544
ef539066
RR
545 int w = M_IMGDATA->m_width;
546 int h = M_IMGDATA->m_height;
c7abc967 547
223d09f6 548 wxCHECK_RET( (x>=0) && (y>=0) && (x<w) && (y<h), wxT("invalid image index") );
c7abc967 549
ef539066 550 long pos = (y * w + x) * 3;
c7abc967 551
ef539066
RR
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
f6bcfd97 557unsigned char wxImage::GetRed( int x, int y ) const
ef539066 558{
223d09f6 559 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 560
ef539066
RR
561 int w = M_IMGDATA->m_width;
562 int h = M_IMGDATA->m_height;
c7abc967 563
223d09f6 564 wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, wxT("invalid image index") );
c7abc967 565
ef539066 566 long pos = (y * w + x) * 3;
c7abc967 567
ef539066
RR
568 return M_IMGDATA->m_data[pos];
569}
570
f6bcfd97 571unsigned char wxImage::GetGreen( int x, int y ) const
ef539066 572{
223d09f6 573 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 574
ef539066
RR
575 int w = M_IMGDATA->m_width;
576 int h = M_IMGDATA->m_height;
c7abc967 577
223d09f6 578 wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, wxT("invalid image index") );
c7abc967 579
ef539066 580 long pos = (y * w + x) * 3;
c7abc967 581
ef539066
RR
582 return M_IMGDATA->m_data[pos+1];
583}
584
f6bcfd97 585unsigned char wxImage::GetBlue( int x, int y ) const
ef539066 586{
223d09f6 587 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 588
ef539066
RR
589 int w = M_IMGDATA->m_width;
590 int h = M_IMGDATA->m_height;
c7abc967 591
223d09f6 592 wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, wxT("invalid image index") );
c7abc967 593
ef539066 594 long pos = (y * w + x) * 3;
c7abc967 595
ef539066
RR
596 return M_IMGDATA->m_data[pos+2];
597}
4698648f
VZ
598
599bool wxImage::Ok() const
600{
de8c48cf
VZ
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;
01111366
RR
605}
606
607char unsigned *wxImage::GetData() const
608{
223d09f6 609 wxCHECK_MSG( Ok(), (char unsigned *)NULL, wxT("invalid image") );
c7abc967 610
fd0eed64 611 return M_IMGDATA->m_data;
01111366
RR
612}
613
58a8ab88 614void wxImage::SetData( char unsigned *data )
01111366 615{
223d09f6 616 wxCHECK_RET( Ok(), wxT("invalid image") );
58a8ab88 617
ed58dbea
RR
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;
995612e2 628
ed58dbea 629 UnRef();
995612e2 630
ed58dbea 631 m_refData = newRefData;
01111366
RR
632}
633
f6bcfd97
BP
634void 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
01111366
RR
662void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b )
663{
223d09f6 664 wxCHECK_RET( Ok(), wxT("invalid image") );
c7abc967 665
fd0eed64
RR
666 M_IMGDATA->m_maskRed = r;
667 M_IMGDATA->m_maskGreen = g;
668 M_IMGDATA->m_maskBlue = b;
669 M_IMGDATA->m_hasMask = TRUE;
01111366
RR
670}
671
672unsigned char wxImage::GetMaskRed() const
673{
223d09f6 674 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 675
fd0eed64 676 return M_IMGDATA->m_maskRed;
01111366
RR
677}
678
679unsigned char wxImage::GetMaskGreen() const
680{
223d09f6 681 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 682
fd0eed64 683 return M_IMGDATA->m_maskGreen;
01111366
RR
684}
685
686unsigned char wxImage::GetMaskBlue() const
687{
223d09f6 688 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 689
fd0eed64 690 return M_IMGDATA->m_maskBlue;
01111366 691}
4698648f 692
01111366
RR
693void wxImage::SetMask( bool mask )
694{
223d09f6 695 wxCHECK_RET( Ok(), wxT("invalid image") );
c7abc967 696
fd0eed64 697 M_IMGDATA->m_hasMask = mask;
01111366
RR
698}
699
700bool wxImage::HasMask() const
701{
223d09f6 702 wxCHECK_MSG( Ok(), FALSE, wxT("invalid image") );
c7abc967 703
fd0eed64 704 return M_IMGDATA->m_hasMask;
01111366
RR
705}
706
4698648f
VZ
707int wxImage::GetWidth() const
708{
223d09f6 709 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 710
4698648f 711 return M_IMGDATA->m_width;
01111366
RR
712}
713
4698648f
VZ
714int wxImage::GetHeight() const
715{
223d09f6 716 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 717
4698648f 718 return M_IMGDATA->m_height;
01111366
RR
719}
720
5e5437e0
JS
721// Palette functions
722
723bool wxImage::HasPalette() const
724{
725 if (!Ok())
726 return FALSE;
727
728 return M_IMGDATA->m_palette.Ok();
729}
730
731const wxPalette& wxImage::GetPalette() const
732{
733 wxCHECK_MSG( Ok(), wxNullPalette, wxT("invalid image") );
734
735 return M_IMGDATA->m_palette;
736}
737
738void 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)
746void 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
763void wxImage::SetOption(const wxString& name, int value)
764{
765 wxString valStr;
766 valStr.Printf(wxT("%d"), value);
767 SetOption(name, valStr);
768}
769
770wxString 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
781int wxImage::GetOptionInt(const wxString& name) const
782{
783 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
784
785 return wxAtoi(GetOption(name));
786}
787
788bool 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
01111366
RR
795bool wxImage::LoadFile( const wxString& filename, long type )
796{
e02afc7a 797#if wxUSE_STREAMS
d80207c3 798 if (wxFileExists(filename))
6c28fd33 799 {
d80207c3
KB
800 wxFileInputStream stream(filename);
801 wxBufferedInputStream bstream( stream );
802 return LoadFile(bstream, type);
6c28fd33 803 }
d80207c3 804 else
6c28fd33 805 {
d80207c3
KB
806 wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
807
716cd410 808 return FALSE;
6c28fd33 809 }
9e9ee68e
VS
810#else // !wxUSE_STREAMS
811 return FALSE;
812#endif // wxUSE_STREAMS
813}
814
815bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype )
816{
817#if wxUSE_STREAMS
d80207c3 818 if (wxFileExists(filename))
6c28fd33 819 {
d80207c3
KB
820 wxFileInputStream stream(filename);
821 wxBufferedInputStream bstream( stream );
822 return LoadFile(bstream, mimetype);
6c28fd33 823 }
d80207c3 824 else
6c28fd33 825 {
d80207c3
KB
826 wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
827
716cd410 828 return FALSE;
6c28fd33 829 }
e02afc7a 830#else // !wxUSE_STREAMS
dbda9e86 831 return FALSE;
e02afc7a 832#endif // wxUSE_STREAMS
1ccbb61a
VZ
833}
834
835bool wxImage::SaveFile( const wxString& filename, int type )
836{
e02afc7a 837#if wxUSE_STREAMS
1ccbb61a 838 wxFileOutputStream stream(filename);
9e9ee68e 839
1ccbb61a 840 if ( stream.LastError() == wxStream_NOERROR )
1b055864 841 {
069d0f27 842 wxBufferedOutputStream bstream( stream );
1b055864
RR
843 return SaveFile(bstream, type);
844 }
e02afc7a 845#endif // wxUSE_STREAMS
3ca6a5f0
BP
846
847 return FALSE;
3d05544e 848}
01111366 849
9e9ee68e
VS
850bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype )
851{
852#if wxUSE_STREAMS
853 wxFileOutputStream stream(filename);
c7abc967 854
9e9ee68e 855 if ( stream.LastError() == wxStream_NOERROR )
1b055864 856 {
069d0f27 857 wxBufferedOutputStream bstream( stream );
1b055864
RR
858 return SaveFile(bstream, mimetype);
859 }
9e9ee68e 860#endif // wxUSE_STREAMS
3ca6a5f0
BP
861
862 return FALSE;
9e9ee68e
VS
863}
864
87202f78
SB
865bool 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
e02afc7a 875#if wxUSE_STREAMS
deb2fec0 876
87202f78
SB
877bool wxImage::CanRead( wxInputStream &stream )
878{
879 wxList &list=GetHandlers();
004fd0c8 880
87202f78 881 for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
004fd0c8 882 {
87202f78
SB
883 wxImageHandler *handler=(wxImageHandler*)node->GetData();
884 if (handler->CanRead( stream ))
069d0f27 885 return TRUE;
87202f78
SB
886 }
887
888 return FALSE;
889}
890
3d05544e
JS
891bool wxImage::LoadFile( wxInputStream& stream, long type )
892{
893 UnRef();
c7abc967 894
fd0eed64 895 m_refData = new wxImageRefData;
c7abc967 896
deb2fec0
SB
897 wxImageHandler *handler;
898
899 if (type==wxBITMAP_TYPE_ANY)
900 {
995612e2 901 wxList &list=GetHandlers();
deb2fec0 902
995612e2
VZ
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 );
7b2471a0 908
995612e2 909 }
deb2fec0 910
58c837a4 911 wxLogWarning( _("No handler found for image type.") );
995612e2 912 return FALSE;
deb2fec0
SB
913 }
914
915 handler = FindHandler(type);
c7abc967 916
4698648f 917 if (handler == NULL)
fd0eed64 918 {
58c837a4 919 wxLogWarning( _("No image handler for type %d defined."), type );
c7abc967 920
fd0eed64
RR
921 return FALSE;
922 }
c7abc967 923
3d05544e 924 return handler->LoadFile( this, stream );
01111366
RR
925}
926
9e9ee68e
VS
927bool 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 {
58c837a4 937 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
9e9ee68e
VS
938
939 return FALSE;
940 }
941
942 return handler->LoadFile( this, stream );
943}
944
3d05544e 945bool wxImage::SaveFile( wxOutputStream& stream, int type )
01111366 946{
223d09f6 947 wxCHECK_MSG( Ok(), FALSE, wxT("invalid image") );
c7abc967 948
fd0eed64 949 wxImageHandler *handler = FindHandler(type);
c7abc967 950
4698648f 951 if (handler == NULL)
fd0eed64 952 {
58c837a4 953 wxLogWarning( _("No image handler for type %d defined."), type );
9e9ee68e
VS
954
955 return FALSE;
956 }
957
958 return handler->SaveFile( this, stream );
959}
960
961bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype )
962{
223d09f6 963 wxCHECK_MSG( Ok(), FALSE, wxT("invalid image") );
c7abc967 964
9e9ee68e 965 wxImageHandler *handler = FindHandlerMime(mimetype);
c7abc967 966
9e9ee68e
VS
967 if (handler == NULL)
968 {
58c837a4 969 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
c7abc967 970
dbda9e86 971 return FALSE;
fd0eed64 972 }
c7abc967 973
3d05544e 974 return handler->SaveFile( this, stream );
01111366 975}
e02afc7a 976#endif // wxUSE_STREAMS
01111366
RR
977
978void wxImage::AddHandler( wxImageHandler *handler )
979{
4698648f
VZ
980 // make sure that the memory will be freed at the program end
981 sm_handlers.DeleteContents(TRUE);
c7abc967 982
01111366
RR
983 sm_handlers.Append( handler );
984}
985
986void wxImage::InsertHandler( wxImageHandler *handler )
987{
4698648f
VZ
988 // make sure that the memory will be freed at the program end
989 sm_handlers.DeleteContents(TRUE);
c7abc967 990
01111366
RR
991 sm_handlers.Insert( handler );
992}
993
994bool wxImage::RemoveHandler( const wxString& name )
995{
fd0eed64
RR
996 wxImageHandler *handler = FindHandler(name);
997 if (handler)
998 {
999 sm_handlers.DeleteObject(handler);
1000 return TRUE;
1001 }
1002 else
1003 return FALSE;
01111366
RR
1004}
1005
1006wxImageHandler *wxImage::FindHandler( const wxString& name )
1007{
fd0eed64
RR
1008 wxNode *node = sm_handlers.First();
1009 while (node)
1010 {
1011 wxImageHandler *handler = (wxImageHandler*)node->Data();
ce3ed50d 1012 if (handler->GetName().Cmp(name) == 0) return handler;
c7abc967 1013
fd0eed64
RR
1014 node = node->Next();
1015 }
1016 return (wxImageHandler *)NULL;
01111366
RR
1017}
1018
1019wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType )
1020{
fd0eed64
RR
1021 wxNode *node = sm_handlers.First();
1022 while (node)
1023 {
1024 wxImageHandler *handler = (wxImageHandler*)node->Data();
ce3ed50d 1025 if ( (handler->GetExtension().Cmp(extension) == 0) &&
fd0eed64 1026 (bitmapType == -1 || handler->GetType() == bitmapType) )
dbda9e86 1027 return handler;
fd0eed64
RR
1028 node = node->Next();
1029 }
1030 return (wxImageHandler*)NULL;
01111366
RR
1031}
1032
1033wxImageHandler *wxImage::FindHandler( long bitmapType )
1034{
fd0eed64
RR
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
9e9ee68e
VS
1045wxImageHandler *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
fd0eed64
RR
1057void wxImage::InitStandardHandlers()
1058{
66e23ad2 1059 AddHandler(new wxBMPHandler);
9731a5fe 1060#if wxUSE_XPM && !defined(__WXGTK__) && !defined(__WXMOTIF__)
66e23ad2
VS
1061 AddHandler(new wxXPMHandler);
1062#endif
01111366
RR
1063}
1064
1065void wxImage::CleanUpHandlers()
1066{
fd0eed64
RR
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 }
01111366
RR
1076}
1077
1078//-----------------------------------------------------------------------------
1079// wxImageHandler
1080//-----------------------------------------------------------------------------
1081
63d963a1 1082IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject)
01111366 1083
e02afc7a 1084#if wxUSE_STREAMS
700ec454 1085bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) )
01111366 1086{
fd0eed64 1087 return FALSE;
01111366
RR
1088}
1089
deb2fec0 1090bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
01111366 1091{
fd0eed64 1092 return FALSE;
01111366 1093}
0828c087 1094
700ec454
RR
1095int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) )
1096{
1097 return 1;
1098}
1099
0828c087
VS
1100bool wxImageHandler::CanRead( const wxString& name )
1101{
0828c087
VS
1102 if (wxFileExists(name))
1103 {
1104 wxFileInputStream stream(name);
1105 return CanRead(stream);
1106 }
1107
1108 else {
58c837a4 1109 wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() );
0828c087
VS
1110
1111 return FALSE;
1112 }
68874acf 1113// return FALSE;
0828c087
VS
1114}
1115
e02afc7a 1116#endif // wxUSE_STREAMS
01111366 1117
fec19ea9
VS
1118
1119
01111366 1120//-----------------------------------------------------------------------------
fec19ea9 1121// wxBitmap convertion routines
01111366
RR
1122//-----------------------------------------------------------------------------
1123
fec19ea9 1124#if wxUSE_GUI
e3554471 1125
fec19ea9 1126#ifdef __WXGTK__
f515c25a 1127wxBitmap wxImage::ConvertToMonoBitmap( unsigned char red, unsigned char green, unsigned char blue ) const
e3554471 1128{
fec19ea9
VS
1129 wxImage mono = this->ConvertToMono( red, green, blue );
1130 wxBitmap bitmap( mono, 1 );
bba6f3bd 1131 return bitmap;
e3554471 1132}
03e11df5 1133#endif
7c74e7fe 1134
7c74e7fe
SC
1135wxBitmap wxImage::ConvertToBitmap() const
1136{
fec19ea9 1137 wxBitmap bitmap( *this );
5fde6fcc 1138 return bitmap;
7c74e7fe
SC
1139}
1140
1141wxImage::wxImage( const wxBitmap &bitmap )
1142{
fec19ea9 1143 *this = bitmap.ConvertToImage();
82ea63e6
RR
1144}
1145
ba0730de 1146#endif
c7abc967 1147
c7abc967 1148
004fd0c8 1149
a91b47e8
JS
1150// A module to allow wxImage initialization/cleanup
1151// without calling these functions from app.cpp or from
1152// the user's application.
1153
1154class wxImageModule: public wxModule
1155{
1156DECLARE_DYNAMIC_CLASS(wxImageModule)
1157public:
1158 wxImageModule() {}
1159 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE; };
1160 void OnExit() { wxImage::CleanUpHandlers(); };
1161};
1162
1163IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule)
c9d01afd
GRG
1164
1165
1166//-----------------------------------------------------------------------------
1167
89d00456
GRG
1168// GRG, Dic/99
1169// Counts and returns the number of different colours. Optionally stops
cc9f7d79
GRG
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).
89d00456 1174//
cc9f7d79 1175unsigned long wxImage::CountColours( unsigned long stopafter )
89d00456
GRG
1176{
1177 wxHashTable h;
ad30de59 1178 wxObject dummy;
33ac7e6f 1179 unsigned char r, g, b;
eeca3a46 1180 unsigned char *p;
89d00456
GRG
1181 unsigned long size, nentries, key;
1182
1183 p = GetData();
1184 size = GetWidth() * GetHeight();
1185 nentries = 0;
1186
cc9f7d79 1187 for (unsigned long j = 0; (j < size) && (nentries <= stopafter) ; j++)
89d00456
GRG
1188 {
1189 r = *(p++);
1190 g = *(p++);
1191 b = *(p++);
1192 key = (r << 16) | (g << 8) | b;
1193
ad30de59 1194 if (h.Get(key) == NULL)
89d00456 1195 {
ad30de59 1196 h.Put(key, &dummy);
89d00456
GRG
1197 nentries++;
1198 }
1199 }
1200
89d00456
GRG
1201 return nentries;
1202}
1203
1204
c9d01afd
GRG
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.
89d00456 1211//
c9d01afd
GRG
1212unsigned long wxImage::ComputeHistogram( wxHashTable &h )
1213{
33ac7e6f 1214 unsigned char r, g, b;
eeca3a46 1215 unsigned char *p;
c9d01afd
GRG
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();
97fdfcc9 1237 hnode->index = nentries++;
c9d01afd
GRG
1238 hnode->value = 1;
1239
1240 h.Put(key, (wxObject *)hnode);
1241 }
1242 }
1243
1244 return nentries;
1245}
1246
7a632f10
JS
1247/*
1248 * Rotation code by Carlos Moreno
1249 */
1250
b5c91ac6
GRG
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.
7a632f10 1255
7a632f10
JS
1256static const double gs_Epsilon = 1e-10;
1257
1258static 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
b5c91ac6 1270inline wxRealPoint rotated_point (const wxRealPoint & p, double cos_angle, double sin_angle, const wxRealPoint & p0)
7a632f10 1271{
b5c91ac6
GRG
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);
7a632f10
JS
1274}
1275
b5c91ac6 1276inline wxRealPoint rotated_point (double x, double y, double cos_angle, double sin_angle, const wxRealPoint & p0)
7a632f10 1277{
b5c91ac6 1278 return rotated_point (wxRealPoint(x,y), cos_angle, sin_angle, p0);
7a632f10
JS
1279}
1280
1281wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool interpolating, wxPoint * offset_after_rotation) const
1282{
7a632f10
JS
1283 int i;
1284 angle = -angle; // screen coordinates are a mirror image of "real" coordinates
1285
ad30de59 1286 // Create pointer-based array to accelerate access to wxImage's data
b5c91ac6 1287 unsigned char ** data = new unsigned char * [GetHeight()];
7a632f10 1288
b5c91ac6 1289 data[0] = GetData();
7a632f10 1290
b5c91ac6
GRG
1291 for (i = 1; i < GetHeight(); i++)
1292 data[i] = data[i - 1] + (3 * GetWidth());
7a632f10 1293
b5c91ac6 1294 // precompute coefficients for rotation formula
ad30de59 1295 // (sine and cosine of the angle)
7a632f10
JS
1296 const double cos_angle = cos(angle);
1297 const double sin_angle = sin(angle);
1298
ad30de59
GRG
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
7a632f10 1302
b5c91ac6 1303 const wxRealPoint p0(centre_of_rotation.x, centre_of_rotation.y);
7a632f10 1304
b5c91ac6
GRG
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);
7a632f10 1309
57c1c6cb
BJ
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)));
57c1c6cb
BJ
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)));
7a632f10
JS
1314
1315 wxImage rotated (x2 - x1 + 1, y2 - y1 + 1);
1316
1317 if (offset_after_rotation != NULL)
1318 {
06b466c7 1319 *offset_after_rotation = wxPoint (x1, y1);
7a632f10
JS
1320 }
1321
b5c91ac6
GRG
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();
7a632f10 1327
ad30de59
GRG
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 //
b5c91ac6
GRG
1331 unsigned char blank_r = 0;
1332 unsigned char blank_g = 0;
1333 unsigned char blank_b = 0;
ad30de59
GRG
1334
1335 if (HasMask())
1336 {
b5c91ac6
GRG
1337 blank_r = GetMaskRed();
1338 blank_g = GetMaskGreen();
1339 blank_b = GetMaskBlue();
1340 rotated.SetMaskColour( blank_r, blank_g, blank_b );
ad30de59
GRG
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
b5c91ac6
GRG
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.
7a632f10
JS
1349
1350 int x;
b5c91ac6 1351 if (interpolating)
7a632f10
JS
1352 {
1353 for (int y = 0; y < rotated.GetHeight(); y++)
1354 {
b5c91ac6 1355 for (x = 0; x < rotated.GetWidth(); x++)
7a632f10 1356 {
b5c91ac6
GRG
1357 wxRealPoint src = rotated_point (x + x1, y + y1, cos_angle, -sin_angle, p0);
1358
f2506310
JS
1359 if (-0.25 < src.x && src.x < GetWidth() - 0.75 &&
1360 -0.25 < src.y && src.y < GetHeight() - 0.75)
7a632f10 1361 {
ad30de59
GRG
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
f2506310 1365 // C.M. 2000-02-17: when the point is near the border, special care is required.
7a632f10 1366
f2506310
JS
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 }
7a632f10 1388
ad30de59
GRG
1389 // get four points and the distances (square of the distance,
1390 // for efficiency reasons) for the interpolation formula
b5c91ac6
GRG
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
7a632f10
JS
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
ad30de59
GRG
1402 // Now interpolate as a weighted average of the four surrounding
1403 // points, where the weights are the distances to each of those points
7a632f10 1404
ad30de59
GRG
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
7a632f10 1407
06b466c7 1408 if (d1 < gs_Epsilon) // d1,d2,d3,d4 are positive -- no need for abs()
7a632f10 1409 {
b5c91ac6
GRG
1410 unsigned char *p = data[y1] + (3 * x1);
1411 *(dst++) = *(p++);
1412 *(dst++) = *(p++);
1413 *(dst++) = *(p++);
7a632f10
JS
1414 }
1415 else if (d2 < gs_Epsilon)
1416 {
b5c91ac6
GRG
1417 unsigned char *p = data[y1] + (3 * x2);
1418 *(dst++) = *(p++);
1419 *(dst++) = *(p++);
1420 *(dst++) = *(p++);
7a632f10
JS
1421 }
1422 else if (d3 < gs_Epsilon)
1423 {
b5c91ac6
GRG
1424 unsigned char *p = data[y2] + (3 * x2);
1425 *(dst++) = *(p++);
1426 *(dst++) = *(p++);
1427 *(dst++) = *(p++);
7a632f10
JS
1428 }
1429 else if (d4 < gs_Epsilon)
1430 {
b5c91ac6
GRG
1431 unsigned char *p = data[y2] + (3 * x1);
1432 *(dst++) = *(p++);
1433 *(dst++) = *(p++);
1434 *(dst++) = *(p++);
7a632f10
JS
1435 }
1436 else
1437 {
06b466c7 1438 // weights for the weighted average are proportional to the inverse of the distance
b5c91ac6
GRG
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
06b466c7
VZ
1444 const double w1 = 1/d1, w2 = 1/d2, w3 = 1/d3, w4 = 1/d4;
1445
b5c91ac6
GRG
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) );
7a632f10
JS
1460 }
1461 }
1462 else
1463 {
b5c91ac6
GRG
1464 *(dst++) = blank_r;
1465 *(dst++) = blank_g;
1466 *(dst++) = blank_b;
7a632f10
JS
1467 }
1468 }
b5c91ac6
GRG
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++)
7a632f10 1476 {
b5c91ac6
GRG
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
457e6c54 1480 const int ys = wxCint (src.y); // closest integer
7a632f10 1481
b5c91ac6
GRG
1482 if (0 <= xs && xs < GetWidth() &&
1483 0 <= ys && ys < GetHeight())
7a632f10 1484 {
b5c91ac6
GRG
1485 unsigned char *p = data[ys] + (3 * xs);
1486 *(dst++) = *(p++);
1487 *(dst++) = *(p++);
1488 *(dst++) = *(p++);
7a632f10
JS
1489 }
1490 else
1491 {
b5c91ac6
GRG
1492 *(dst++) = blank_r;
1493 *(dst++) = blank_g;
1494 *(dst++) = blank_b;
7a632f10
JS
1495 }
1496 }
1497 }
1498 }
1499
4aff28fc 1500 delete [] data;
4aff28fc 1501
7a632f10
JS
1502 return rotated;
1503}
c9d01afd 1504
c96ea657 1505#endif // wxUSE_IMAGE