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