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