]>
Commit | Line | Data |
---|---|---|
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 | ||
48 | class wxImageRefData: public wxObjectRefData | |
49 | { | |
fd0eed64 | 50 | public: |
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 | 68 | wxImageRefData::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 | 81 | wxImageRefData::~wxImageRefData() |
01111366 | 82 | { |
f6bcfd97 | 83 | if (m_data && !m_static) |
58c837a4 | 84 | free( m_data ); |
01111366 RR |
85 | } |
86 | ||
87 | wxList wxImage::sm_handlers; | |
88 | ||
fec19ea9 VS |
89 | wxImage wxNullImage; |
90 | ||
01111366 RR |
91 | //----------------------------------------------------------------------------- |
92 | ||
93 | #define M_IMGDATA ((wxImageRefData *)m_refData) | |
94 | ||
5e5437e0 | 95 | IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject) |
01111366 RR |
96 | |
97 | wxImage::wxImage() | |
98 | { | |
99 | } | |
100 | ||
101 | wxImage::wxImage( int width, int height ) | |
102 | { | |
fd0eed64 | 103 | Create( width, height ); |
01111366 RR |
104 | } |
105 | ||
f6bcfd97 BP |
106 | wxImage::wxImage( int width, int height, unsigned char* data, bool static_data ) |
107 | { | |
108 | Create( width, height, data, static_data ); | |
109 | } | |
110 | ||
60d43ad8 | 111 | wxImage::wxImage( const wxString& name, long type, int index ) |
01111366 | 112 | { |
60d43ad8 | 113 | LoadFile( name, type, index ); |
01111366 RR |
114 | } |
115 | ||
60d43ad8 | 116 | wxImage::wxImage( const wxString& name, const wxString& mimetype, int index ) |
9e9ee68e | 117 | { |
60d43ad8 | 118 | LoadFile( name, mimetype, index ); |
9e9ee68e VS |
119 | } |
120 | ||
e02afc7a | 121 | #if wxUSE_STREAMS |
60d43ad8 | 122 | wxImage::wxImage( wxInputStream& stream, long type, int index ) |
3d05544e | 123 | { |
60d43ad8 | 124 | LoadFile( stream, type, index ); |
3d05544e | 125 | } |
9e9ee68e | 126 | |
60d43ad8 | 127 | wxImage::wxImage( wxInputStream& stream, const wxString& mimetype, int index ) |
9e9ee68e | 128 | { |
60d43ad8 | 129 | LoadFile( stream, mimetype, index ); |
9e9ee68e | 130 | } |
e02afc7a | 131 | #endif // wxUSE_STREAMS |
3d05544e | 132 | |
4698648f VZ |
133 | wxImage::wxImage( const wxImage& image ) |
134 | { | |
135 | Ref(image); | |
01111366 RR |
136 | } |
137 | ||
4698648f VZ |
138 | wxImage::wxImage( const wxImage* image ) |
139 | { | |
140 | if (image) Ref(*image); | |
01111366 RR |
141 | } |
142 | ||
143 | void 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 |
164 | void 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 |
184 | void wxImage::Destroy() |
185 | { | |
fd0eed64 | 186 | UnRef(); |
01111366 RR |
187 | } |
188 | ||
f6bcfd97 BP |
189 | wxImage 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 | 209 | wxImage 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 |
281 | wxImage 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 | ||
318 | wxImage 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 |
366 | wxImage 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 |
403 | void 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 |
483 | void 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 | 506 | wxImage 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 |
543 | void 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 | 559 | unsigned 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 | 573 | unsigned 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 | 587 | unsigned 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 | |
601 | bool 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 | ||
609 | char 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 | 616 | void 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 |
636 | void 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 |
664 | void 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 | ||
674 | unsigned 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 | ||
681 | unsigned 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 | ||
688 | unsigned 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 |
695 | void 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 | ||
702 | bool 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 |
709 | int 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 |
716 | int 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 | ||
52b64b0a | 723 | |
1f5b2017 VS |
724 | bool wxImage::FindFirstUnusedColour( |
725 | unsigned char *r, unsigned char *g, unsigned char *b, | |
726 | unsigned char startR, unsigned char startG, unsigned char startB) | |
52b64b0a RR |
727 | { |
728 | wxHashTable hTable; | |
729 | unsigned long key; | |
730 | ||
731 | ComputeHistogram( hTable ); | |
732 | ||
1f5b2017 VS |
733 | unsigned char r2 = startR; |
734 | unsigned char g2 = startG; | |
735 | unsigned char b2 = startB; | |
52b64b0a RR |
736 | |
737 | key = (r2 << 16) | (g2 << 8) | b2; | |
738 | ||
739 | while ( (wxHNode *) hTable.Get(key) ) | |
740 | { | |
741 | // color already used | |
1f5b2017 | 742 | r2++; |
52b64b0a RR |
743 | if ( r2 >= 255 ) |
744 | { | |
745 | r2 = 0; | |
1f5b2017 | 746 | g2++; |
52b64b0a RR |
747 | if ( g2 >= 255 ) |
748 | { | |
1f5b2017 VS |
749 | g2 = 0; |
750 | b2++; | |
52b64b0a RR |
751 | if ( b2 >= 255 ) |
752 | { | |
753 | wxLogError( _("GetUnusedColour:: No Unused Color in image ") ); | |
754 | return FALSE; | |
755 | } | |
756 | } | |
757 | } | |
758 | ||
759 | key = (r2 << 16) | (g2 << 8) | b2; | |
760 | } | |
761 | ||
1f5b2017 VS |
762 | if (r) *r = r2; |
763 | if (g) *g = g2; | |
764 | if (b) *b = b2; | |
765 | ||
52b64b0a RR |
766 | return TRUE; |
767 | } | |
768 | ||
769 | ||
1f5b2017 VS |
770 | bool wxImage::SetMaskFromImage(const wxImage& mask, |
771 | unsigned char mr, unsigned char mg, unsigned char mb) | |
52b64b0a | 772 | { |
52b64b0a RR |
773 | // check that the images are the same size |
774 | if ( (M_IMGDATA->m_height != mask.GetHeight() ) || (M_IMGDATA->m_width != mask.GetWidth () ) ) | |
775 | { | |
776 | wxLogError( _("Image and Mask have different sizes") ); | |
777 | return FALSE; | |
778 | } | |
779 | ||
780 | // find unused colour | |
781 | unsigned char r,g,b ; | |
1f5b2017 | 782 | if (!FindFirstUnusedColour(&r, &g, &b)) |
52b64b0a RR |
783 | { |
784 | wxLogError( _("No Unused Color in image being masked") ); | |
785 | return FALSE ; | |
786 | } | |
787 | ||
788 | char unsigned *imgdata = GetData(); | |
789 | char unsigned *maskdata = mask.GetData(); | |
790 | ||
791 | const int w = GetWidth(); | |
792 | const int h = GetHeight(); | |
793 | ||
794 | for (int j = 0; j < h; j++) | |
1f5b2017 | 795 | { |
52b64b0a RR |
796 | for (int i = 0; i < w; i++) |
797 | { | |
1f5b2017 | 798 | if ((maskdata[0] == mr) && (maskdata[1] == mg) && (maskdata[2] == mb)) |
52b64b0a RR |
799 | { |
800 | imgdata[0] = r; | |
801 | imgdata[1] = g; | |
802 | imgdata[2] = b; | |
803 | } | |
804 | imgdata += 3; | |
805 | maskdata += 3; | |
806 | } | |
1f5b2017 | 807 | } |
52b64b0a | 808 | |
1f5b2017 VS |
809 | SetMaskColour(r, g, b); |
810 | SetMask(TRUE); | |
52b64b0a RR |
811 | |
812 | return TRUE; | |
813 | } | |
814 | ||
d275c7eb VZ |
815 | #if wxUSE_PALETTE |
816 | ||
5e5437e0 JS |
817 | // Palette functions |
818 | ||
819 | bool wxImage::HasPalette() const | |
820 | { | |
821 | if (!Ok()) | |
822 | return FALSE; | |
823 | ||
824 | return M_IMGDATA->m_palette.Ok(); | |
825 | } | |
826 | ||
827 | const wxPalette& wxImage::GetPalette() const | |
828 | { | |
829 | wxCHECK_MSG( Ok(), wxNullPalette, wxT("invalid image") ); | |
830 | ||
831 | return M_IMGDATA->m_palette; | |
832 | } | |
833 | ||
834 | void wxImage::SetPalette(const wxPalette& palette) | |
835 | { | |
836 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
837 | ||
838 | M_IMGDATA->m_palette = palette; | |
839 | } | |
840 | ||
d275c7eb VZ |
841 | #endif // wxUSE_PALETTE |
842 | ||
5e5437e0 JS |
843 | // Option functions (arbitrary name/value mapping) |
844 | void wxImage::SetOption(const wxString& name, const wxString& value) | |
845 | { | |
846 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
847 | ||
848 | int idx = M_IMGDATA->m_optionNames.Index(name, FALSE); | |
849 | if (idx == wxNOT_FOUND) | |
850 | { | |
851 | M_IMGDATA->m_optionNames.Add(name); | |
852 | M_IMGDATA->m_optionValues.Add(value); | |
853 | } | |
854 | else | |
855 | { | |
856 | M_IMGDATA->m_optionNames[idx] = name; | |
857 | M_IMGDATA->m_optionValues[idx] = value; | |
858 | } | |
859 | } | |
860 | ||
861 | void wxImage::SetOption(const wxString& name, int value) | |
862 | { | |
863 | wxString valStr; | |
864 | valStr.Printf(wxT("%d"), value); | |
865 | SetOption(name, valStr); | |
866 | } | |
867 | ||
868 | wxString wxImage::GetOption(const wxString& name) const | |
869 | { | |
870 | wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid image") ); | |
871 | ||
872 | int idx = M_IMGDATA->m_optionNames.Index(name, FALSE); | |
873 | if (idx == wxNOT_FOUND) | |
874 | return wxEmptyString; | |
875 | else | |
876 | return M_IMGDATA->m_optionValues[idx]; | |
877 | } | |
878 | ||
879 | int wxImage::GetOptionInt(const wxString& name) const | |
880 | { | |
881 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
882 | ||
883 | return wxAtoi(GetOption(name)); | |
884 | } | |
885 | ||
886 | bool wxImage::HasOption(const wxString& name) const | |
887 | { | |
888 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid image") ); | |
889 | ||
890 | return (M_IMGDATA->m_optionNames.Index(name, FALSE) != wxNOT_FOUND); | |
891 | } | |
892 | ||
60d43ad8 | 893 | bool wxImage::LoadFile( const wxString& filename, long type, int index ) |
01111366 | 894 | { |
e02afc7a | 895 | #if wxUSE_STREAMS |
d80207c3 | 896 | if (wxFileExists(filename)) |
6c28fd33 | 897 | { |
d80207c3 KB |
898 | wxFileInputStream stream(filename); |
899 | wxBufferedInputStream bstream( stream ); | |
60d43ad8 | 900 | return LoadFile(bstream, type, index); |
6c28fd33 | 901 | } |
d80207c3 | 902 | else |
6c28fd33 | 903 | { |
d80207c3 KB |
904 | wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() ); |
905 | ||
716cd410 | 906 | return FALSE; |
6c28fd33 | 907 | } |
9e9ee68e VS |
908 | #else // !wxUSE_STREAMS |
909 | return FALSE; | |
910 | #endif // wxUSE_STREAMS | |
911 | } | |
912 | ||
60d43ad8 | 913 | bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype, int index ) |
9e9ee68e VS |
914 | { |
915 | #if wxUSE_STREAMS | |
d80207c3 | 916 | if (wxFileExists(filename)) |
6c28fd33 | 917 | { |
d80207c3 KB |
918 | wxFileInputStream stream(filename); |
919 | wxBufferedInputStream bstream( stream ); | |
60d43ad8 | 920 | return LoadFile(bstream, mimetype, index); |
6c28fd33 | 921 | } |
d80207c3 | 922 | else |
6c28fd33 | 923 | { |
d80207c3 KB |
924 | wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() ); |
925 | ||
716cd410 | 926 | return FALSE; |
6c28fd33 | 927 | } |
e02afc7a | 928 | #else // !wxUSE_STREAMS |
dbda9e86 | 929 | return FALSE; |
e02afc7a | 930 | #endif // wxUSE_STREAMS |
1ccbb61a VZ |
931 | } |
932 | ||
933 | bool wxImage::SaveFile( const wxString& filename, int type ) | |
934 | { | |
e02afc7a | 935 | #if wxUSE_STREAMS |
1ccbb61a | 936 | wxFileOutputStream stream(filename); |
9e9ee68e | 937 | |
1ccbb61a | 938 | if ( stream.LastError() == wxStream_NOERROR ) |
1b055864 | 939 | { |
069d0f27 | 940 | wxBufferedOutputStream bstream( stream ); |
1b055864 RR |
941 | return SaveFile(bstream, type); |
942 | } | |
e02afc7a | 943 | #endif // wxUSE_STREAMS |
3ca6a5f0 BP |
944 | |
945 | return FALSE; | |
3d05544e | 946 | } |
01111366 | 947 | |
9e9ee68e VS |
948 | bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) |
949 | { | |
950 | #if wxUSE_STREAMS | |
951 | wxFileOutputStream stream(filename); | |
c7abc967 | 952 | |
9e9ee68e | 953 | if ( stream.LastError() == wxStream_NOERROR ) |
1b055864 | 954 | { |
069d0f27 | 955 | wxBufferedOutputStream bstream( stream ); |
1b055864 RR |
956 | return SaveFile(bstream, mimetype); |
957 | } | |
9e9ee68e | 958 | #endif // wxUSE_STREAMS |
3ca6a5f0 BP |
959 | |
960 | return FALSE; | |
9e9ee68e VS |
961 | } |
962 | ||
87202f78 SB |
963 | bool wxImage::CanRead( const wxString &name ) |
964 | { | |
965 | #if wxUSE_STREAMS | |
966 | wxFileInputStream stream(name); | |
967 | return CanRead(stream); | |
968 | #else | |
969 | return FALSE; | |
970 | #endif | |
971 | } | |
972 | ||
649d13e8 | 973 | int wxImage::GetImageCount( const wxString &name, long type ) |
60d43ad8 VS |
974 | { |
975 | #if wxUSE_STREAMS | |
976 | wxFileInputStream stream(name); | |
649d13e8 | 977 | return GetImageCount(stream, type); |
60d43ad8 VS |
978 | #else |
979 | return 0; | |
980 | #endif | |
981 | } | |
982 | ||
e02afc7a | 983 | #if wxUSE_STREAMS |
deb2fec0 | 984 | |
87202f78 SB |
985 | bool wxImage::CanRead( wxInputStream &stream ) |
986 | { | |
60d43ad8 | 987 | wxList &list=GetHandlers(); |
004fd0c8 | 988 | |
60d43ad8 | 989 | for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() ) |
004fd0c8 | 990 | { |
60d43ad8 VS |
991 | wxImageHandler *handler=(wxImageHandler*)node->GetData(); |
992 | if (handler->CanRead( stream )) | |
993 | return TRUE; | |
87202f78 SB |
994 | } |
995 | ||
60d43ad8 VS |
996 | return FALSE; |
997 | } | |
998 | ||
649d13e8 | 999 | int wxImage::GetImageCount( wxInputStream &stream, long type ) |
60d43ad8 VS |
1000 | { |
1001 | wxImageHandler *handler; | |
1002 | ||
1003 | if ( type == wxBITMAP_TYPE_ANY ) | |
1004 | { | |
1005 | wxList &list=GetHandlers(); | |
1006 | ||
1007 | for (wxList::Node *node = list.GetFirst(); node; node = node->GetNext()) | |
1008 | { | |
1009 | handler=(wxImageHandler*)node->GetData(); | |
1010 | if ( handler->CanRead(stream) ) | |
649d13e8 | 1011 | return handler->GetImageCount(stream); |
60d43ad8 VS |
1012 | |
1013 | } | |
1014 | ||
1015 | wxLogWarning(_("No handler found for image type.")); | |
1016 | return 0; | |
1017 | } | |
1018 | ||
1019 | handler = FindHandler(type); | |
1020 | ||
1021 | if ( !handler ) | |
1022 | { | |
1023 | wxLogWarning(_("No image handler for type %d defined."), type); | |
1024 | return FALSE; | |
1025 | } | |
1026 | ||
1027 | if ( handler->CanRead(stream) ) | |
1028 | { | |
649d13e8 | 1029 | return handler->GetImageCount(stream); |
60d43ad8 VS |
1030 | } |
1031 | else | |
1032 | { | |
1033 | wxLogError(_("Image file is not of type %d."), type); | |
1034 | return 0; | |
1035 | } | |
87202f78 SB |
1036 | } |
1037 | ||
60d43ad8 | 1038 | bool wxImage::LoadFile( wxInputStream& stream, long type, int index ) |
3d05544e JS |
1039 | { |
1040 | UnRef(); | |
c7abc967 | 1041 | |
fd0eed64 | 1042 | m_refData = new wxImageRefData; |
c7abc967 | 1043 | |
deb2fec0 SB |
1044 | wxImageHandler *handler; |
1045 | ||
60d43ad8 | 1046 | if ( type == wxBITMAP_TYPE_ANY ) |
deb2fec0 | 1047 | { |
995612e2 | 1048 | wxList &list=GetHandlers(); |
deb2fec0 | 1049 | |
995612e2 VZ |
1050 | for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() ) |
1051 | { | |
1052 | handler=(wxImageHandler*)node->GetData(); | |
60d43ad8 VS |
1053 | if ( handler->CanRead(stream) ) |
1054 | return handler->LoadFile(this, stream, TRUE/*verbose*/, index); | |
7b2471a0 | 1055 | |
995612e2 | 1056 | } |
deb2fec0 | 1057 | |
58c837a4 | 1058 | wxLogWarning( _("No handler found for image type.") ); |
995612e2 | 1059 | return FALSE; |
deb2fec0 SB |
1060 | } |
1061 | ||
1062 | handler = FindHandler(type); | |
c7abc967 | 1063 | |
4698648f | 1064 | if (handler == NULL) |
fd0eed64 | 1065 | { |
58c837a4 | 1066 | wxLogWarning( _("No image handler for type %d defined."), type ); |
c7abc967 | 1067 | |
fd0eed64 RR |
1068 | return FALSE; |
1069 | } | |
c7abc967 | 1070 | |
60d43ad8 | 1071 | return handler->LoadFile(this, stream, TRUE/*verbose*/, index); |
01111366 RR |
1072 | } |
1073 | ||
60d43ad8 | 1074 | bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int index ) |
9e9ee68e VS |
1075 | { |
1076 | UnRef(); | |
1077 | ||
1078 | m_refData = new wxImageRefData; | |
1079 | ||
1080 | wxImageHandler *handler = FindHandlerMime(mimetype); | |
1081 | ||
1082 | if (handler == NULL) | |
1083 | { | |
58c837a4 | 1084 | wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() ); |
9e9ee68e VS |
1085 | |
1086 | return FALSE; | |
1087 | } | |
1088 | ||
649d13e8 | 1089 | return handler->LoadFile( this, stream, TRUE/*verbose*/, index ); |
9e9ee68e VS |
1090 | } |
1091 | ||
3d05544e | 1092 | bool wxImage::SaveFile( wxOutputStream& stream, int type ) |
01111366 | 1093 | { |
223d09f6 | 1094 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid image") ); |
c7abc967 | 1095 | |
fd0eed64 | 1096 | wxImageHandler *handler = FindHandler(type); |
c7abc967 | 1097 | |
4698648f | 1098 | if (handler == NULL) |
fd0eed64 | 1099 | { |
58c837a4 | 1100 | wxLogWarning( _("No image handler for type %d defined."), type ); |
9e9ee68e VS |
1101 | |
1102 | return FALSE; | |
1103 | } | |
1104 | ||
1105 | return handler->SaveFile( this, stream ); | |
1106 | } | |
1107 | ||
1108 | bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) | |
1109 | { | |
223d09f6 | 1110 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid image") ); |
c7abc967 | 1111 | |
9e9ee68e | 1112 | wxImageHandler *handler = FindHandlerMime(mimetype); |
c7abc967 | 1113 | |
9e9ee68e VS |
1114 | if (handler == NULL) |
1115 | { | |
58c837a4 | 1116 | wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() ); |
c7abc967 | 1117 | |
dbda9e86 | 1118 | return FALSE; |
fd0eed64 | 1119 | } |
c7abc967 | 1120 | |
3d05544e | 1121 | return handler->SaveFile( this, stream ); |
01111366 | 1122 | } |
e02afc7a | 1123 | #endif // wxUSE_STREAMS |
01111366 RR |
1124 | |
1125 | void wxImage::AddHandler( wxImageHandler *handler ) | |
1126 | { | |
4698648f VZ |
1127 | // make sure that the memory will be freed at the program end |
1128 | sm_handlers.DeleteContents(TRUE); | |
c7abc967 | 1129 | |
01111366 RR |
1130 | sm_handlers.Append( handler ); |
1131 | } | |
1132 | ||
1133 | void wxImage::InsertHandler( wxImageHandler *handler ) | |
1134 | { | |
4698648f VZ |
1135 | // make sure that the memory will be freed at the program end |
1136 | sm_handlers.DeleteContents(TRUE); | |
c7abc967 | 1137 | |
01111366 RR |
1138 | sm_handlers.Insert( handler ); |
1139 | } | |
1140 | ||
1141 | bool wxImage::RemoveHandler( const wxString& name ) | |
1142 | { | |
fd0eed64 RR |
1143 | wxImageHandler *handler = FindHandler(name); |
1144 | if (handler) | |
1145 | { | |
1146 | sm_handlers.DeleteObject(handler); | |
1147 | return TRUE; | |
1148 | } | |
1149 | else | |
1150 | return FALSE; | |
01111366 RR |
1151 | } |
1152 | ||
1153 | wxImageHandler *wxImage::FindHandler( const wxString& name ) | |
1154 | { | |
fd0eed64 RR |
1155 | wxNode *node = sm_handlers.First(); |
1156 | while (node) | |
1157 | { | |
1158 | wxImageHandler *handler = (wxImageHandler*)node->Data(); | |
ce3ed50d | 1159 | if (handler->GetName().Cmp(name) == 0) return handler; |
c7abc967 | 1160 | |
fd0eed64 RR |
1161 | node = node->Next(); |
1162 | } | |
1163 | return (wxImageHandler *)NULL; | |
01111366 RR |
1164 | } |
1165 | ||
1166 | wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType ) | |
1167 | { | |
fd0eed64 RR |
1168 | wxNode *node = sm_handlers.First(); |
1169 | while (node) | |
1170 | { | |
1171 | wxImageHandler *handler = (wxImageHandler*)node->Data(); | |
ce3ed50d | 1172 | if ( (handler->GetExtension().Cmp(extension) == 0) && |
fd0eed64 | 1173 | (bitmapType == -1 || handler->GetType() == bitmapType) ) |
dbda9e86 | 1174 | return handler; |
fd0eed64 RR |
1175 | node = node->Next(); |
1176 | } | |
1177 | return (wxImageHandler*)NULL; | |
01111366 RR |
1178 | } |
1179 | ||
1180 | wxImageHandler *wxImage::FindHandler( long bitmapType ) | |
1181 | { | |
fd0eed64 RR |
1182 | wxNode *node = sm_handlers.First(); |
1183 | while (node) | |
1184 | { | |
1185 | wxImageHandler *handler = (wxImageHandler *)node->Data(); | |
1186 | if (handler->GetType() == bitmapType) return handler; | |
1187 | node = node->Next(); | |
1188 | } | |
1189 | return NULL; | |
1190 | } | |
1191 | ||
9e9ee68e VS |
1192 | wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype ) |
1193 | { | |
1194 | wxNode *node = sm_handlers.First(); | |
1195 | while (node) | |
1196 | { | |
1197 | wxImageHandler *handler = (wxImageHandler *)node->Data(); | |
1198 | if (handler->GetMimeType().IsSameAs(mimetype, FALSE)) return handler; | |
1199 | node = node->Next(); | |
1200 | } | |
1201 | return NULL; | |
1202 | } | |
1203 | ||
fd0eed64 RR |
1204 | void wxImage::InitStandardHandlers() |
1205 | { | |
77fac225 | 1206 | #if wxUSE_STREAMS |
66e23ad2 | 1207 | AddHandler(new wxBMPHandler); |
77fac225 VZ |
1208 | #endif // wxUSE_STREAMS |
1209 | ||
9731a5fe | 1210 | #if wxUSE_XPM && !defined(__WXGTK__) && !defined(__WXMOTIF__) |
66e23ad2 VS |
1211 | AddHandler(new wxXPMHandler); |
1212 | #endif | |
01111366 RR |
1213 | } |
1214 | ||
1215 | void wxImage::CleanUpHandlers() | |
1216 | { | |
fd0eed64 RR |
1217 | wxNode *node = sm_handlers.First(); |
1218 | while (node) | |
1219 | { | |
1220 | wxImageHandler *handler = (wxImageHandler *)node->Data(); | |
1221 | wxNode *next = node->Next(); | |
1222 | delete handler; | |
1223 | delete node; | |
1224 | node = next; | |
1225 | } | |
01111366 RR |
1226 | } |
1227 | ||
1228 | //----------------------------------------------------------------------------- | |
1229 | // wxImageHandler | |
1230 | //----------------------------------------------------------------------------- | |
1231 | ||
63d963a1 | 1232 | IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject) |
01111366 | 1233 | |
e02afc7a | 1234 | #if wxUSE_STREAMS |
700ec454 | 1235 | bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) ) |
01111366 | 1236 | { |
fd0eed64 | 1237 | return FALSE; |
01111366 RR |
1238 | } |
1239 | ||
deb2fec0 | 1240 | bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) ) |
01111366 | 1241 | { |
fd0eed64 | 1242 | return FALSE; |
01111366 | 1243 | } |
0828c087 | 1244 | |
649d13e8 | 1245 | int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) ) |
700ec454 RR |
1246 | { |
1247 | return 1; | |
1248 | } | |
1249 | ||
0828c087 VS |
1250 | bool wxImageHandler::CanRead( const wxString& name ) |
1251 | { | |
0828c087 VS |
1252 | if (wxFileExists(name)) |
1253 | { | |
1254 | wxFileInputStream stream(name); | |
1255 | return CanRead(stream); | |
1256 | } | |
1257 | ||
1258 | else { | |
58c837a4 | 1259 | wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() ); |
0828c087 VS |
1260 | |
1261 | return FALSE; | |
1262 | } | |
68874acf | 1263 | // return FALSE; |
0828c087 VS |
1264 | } |
1265 | ||
e02afc7a | 1266 | #endif // wxUSE_STREAMS |
01111366 | 1267 | |
fec19ea9 VS |
1268 | |
1269 | ||
01111366 | 1270 | //----------------------------------------------------------------------------- |
fec19ea9 | 1271 | // wxBitmap convertion routines |
01111366 RR |
1272 | //----------------------------------------------------------------------------- |
1273 | ||
fec19ea9 | 1274 | #if wxUSE_GUI |
e3554471 | 1275 | |
fec19ea9 | 1276 | #ifdef __WXGTK__ |
f515c25a | 1277 | wxBitmap wxImage::ConvertToMonoBitmap( unsigned char red, unsigned char green, unsigned char blue ) const |
e3554471 | 1278 | { |
fec19ea9 VS |
1279 | wxImage mono = this->ConvertToMono( red, green, blue ); |
1280 | wxBitmap bitmap( mono, 1 ); | |
bba6f3bd | 1281 | return bitmap; |
e3554471 | 1282 | } |
03e11df5 | 1283 | #endif |
7c74e7fe | 1284 | |
7c74e7fe SC |
1285 | wxBitmap wxImage::ConvertToBitmap() const |
1286 | { | |
fec19ea9 | 1287 | wxBitmap bitmap( *this ); |
5fde6fcc | 1288 | return bitmap; |
7c74e7fe SC |
1289 | } |
1290 | ||
1291 | wxImage::wxImage( const wxBitmap &bitmap ) | |
1292 | { | |
fec19ea9 | 1293 | *this = bitmap.ConvertToImage(); |
82ea63e6 RR |
1294 | } |
1295 | ||
ba0730de | 1296 | #endif |
c7abc967 | 1297 | |
c7abc967 | 1298 | |
004fd0c8 | 1299 | |
a91b47e8 JS |
1300 | // A module to allow wxImage initialization/cleanup |
1301 | // without calling these functions from app.cpp or from | |
1302 | // the user's application. | |
1303 | ||
1304 | class wxImageModule: public wxModule | |
1305 | { | |
1306 | DECLARE_DYNAMIC_CLASS(wxImageModule) | |
1307 | public: | |
1308 | wxImageModule() {} | |
1309 | bool OnInit() { wxImage::InitStandardHandlers(); return TRUE; }; | |
1310 | void OnExit() { wxImage::CleanUpHandlers(); }; | |
1311 | }; | |
1312 | ||
1313 | IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule) | |
c9d01afd GRG |
1314 | |
1315 | ||
1316 | //----------------------------------------------------------------------------- | |
1317 | ||
89d00456 GRG |
1318 | // GRG, Dic/99 |
1319 | // Counts and returns the number of different colours. Optionally stops | |
cc9f7d79 GRG |
1320 | // when it exceeds 'stopafter' different colours. This is useful, for |
1321 | // example, to see if the image can be saved as 8-bit (256 colour or | |
1322 | // less, in this case it would be invoked as CountColours(256)). Default | |
1323 | // value for stopafter is -1 (don't care). | |
89d00456 | 1324 | // |
cc9f7d79 | 1325 | unsigned long wxImage::CountColours( unsigned long stopafter ) |
89d00456 GRG |
1326 | { |
1327 | wxHashTable h; | |
ad30de59 | 1328 | wxObject dummy; |
33ac7e6f | 1329 | unsigned char r, g, b; |
eeca3a46 | 1330 | unsigned char *p; |
89d00456 GRG |
1331 | unsigned long size, nentries, key; |
1332 | ||
1333 | p = GetData(); | |
1334 | size = GetWidth() * GetHeight(); | |
1335 | nentries = 0; | |
1336 | ||
cc9f7d79 | 1337 | for (unsigned long j = 0; (j < size) && (nentries <= stopafter) ; j++) |
89d00456 GRG |
1338 | { |
1339 | r = *(p++); | |
1340 | g = *(p++); | |
1341 | b = *(p++); | |
1342 | key = (r << 16) | (g << 8) | b; | |
1343 | ||
ad30de59 | 1344 | if (h.Get(key) == NULL) |
89d00456 | 1345 | { |
ad30de59 | 1346 | h.Put(key, &dummy); |
89d00456 GRG |
1347 | nentries++; |
1348 | } | |
1349 | } | |
1350 | ||
89d00456 GRG |
1351 | return nentries; |
1352 | } | |
1353 | ||
1354 | ||
c9d01afd GRG |
1355 | // GRG, Dic/99 |
1356 | // Computes the histogram of the image and fills a hash table, indexed | |
1357 | // with integer keys built as 0xRRGGBB, containing wxHNode objects. Each | |
1358 | // wxHNode contains an 'index' (useful to build a palette with the image | |
1359 | // colours) and a 'value', which is the number of pixels in the image with | |
1360 | // that colour. | |
89d00456 | 1361 | // |
c9d01afd GRG |
1362 | unsigned long wxImage::ComputeHistogram( wxHashTable &h ) |
1363 | { | |
33ac7e6f | 1364 | unsigned char r, g, b; |
eeca3a46 | 1365 | unsigned char *p; |
c9d01afd GRG |
1366 | unsigned long size, nentries, key; |
1367 | wxHNode *hnode; | |
1368 | ||
1369 | p = GetData(); | |
1370 | size = GetWidth() * GetHeight(); | |
1371 | nentries = 0; | |
1372 | ||
1373 | for (unsigned long j = 0; j < size; j++) | |
1374 | { | |
1375 | r = *(p++); | |
1376 | g = *(p++); | |
1377 | b = *(p++); | |
1378 | key = (r << 16) | (g << 8) | b; | |
1379 | ||
1380 | hnode = (wxHNode *) h.Get(key); | |
1381 | ||
1382 | if (hnode) | |
1383 | hnode->value++; | |
1384 | else | |
1385 | { | |
1386 | hnode = new wxHNode(); | |
97fdfcc9 | 1387 | hnode->index = nentries++; |
c9d01afd GRG |
1388 | hnode->value = 1; |
1389 | ||
1390 | h.Put(key, (wxObject *)hnode); | |
1391 | } | |
1392 | } | |
1393 | ||
1394 | return nentries; | |
1395 | } | |
1396 | ||
7a632f10 JS |
1397 | /* |
1398 | * Rotation code by Carlos Moreno | |
1399 | */ | |
1400 | ||
b5c91ac6 GRG |
1401 | // GRG: I've removed wxRotationPoint - we already have wxRealPoint which |
1402 | // does exactly the same thing. And I also got rid of wxRotationPixel | |
1403 | // bacause of potential problems in architectures where alignment | |
1404 | // is an issue, so I had to rewrite parts of the code. | |
7a632f10 | 1405 | |
7a632f10 JS |
1406 | static const double gs_Epsilon = 1e-10; |
1407 | ||
1408 | static inline int wxCint (double x) | |
1409 | { | |
1410 | return (x > 0) ? (int) (x + 0.5) : (int) (x - 0.5); | |
1411 | } | |
1412 | ||
1413 | ||
1414 | // Auxiliary function to rotate a point (x,y) with respect to point p0 | |
1415 | // make it inline and use a straight return to facilitate optimization | |
1416 | // also, the function receives the sine and cosine of the angle to avoid | |
1417 | // repeating the time-consuming calls to these functions -- sin/cos can | |
1418 | // be computed and stored in the calling function. | |
1419 | ||
b5c91ac6 | 1420 | inline wxRealPoint rotated_point (const wxRealPoint & p, double cos_angle, double sin_angle, const wxRealPoint & p0) |
7a632f10 | 1421 | { |
b5c91ac6 GRG |
1422 | return wxRealPoint (p0.x + (p.x - p0.x) * cos_angle - (p.y - p0.y) * sin_angle, |
1423 | p0.y + (p.y - p0.y) * cos_angle + (p.x - p0.x) * sin_angle); | |
7a632f10 JS |
1424 | } |
1425 | ||
b5c91ac6 | 1426 | inline wxRealPoint rotated_point (double x, double y, double cos_angle, double sin_angle, const wxRealPoint & p0) |
7a632f10 | 1427 | { |
b5c91ac6 | 1428 | return rotated_point (wxRealPoint(x,y), cos_angle, sin_angle, p0); |
7a632f10 JS |
1429 | } |
1430 | ||
1431 | wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool interpolating, wxPoint * offset_after_rotation) const | |
1432 | { | |
7a632f10 JS |
1433 | int i; |
1434 | angle = -angle; // screen coordinates are a mirror image of "real" coordinates | |
1435 | ||
ad30de59 | 1436 | // Create pointer-based array to accelerate access to wxImage's data |
b5c91ac6 | 1437 | unsigned char ** data = new unsigned char * [GetHeight()]; |
7a632f10 | 1438 | |
b5c91ac6 | 1439 | data[0] = GetData(); |
7a632f10 | 1440 | |
b5c91ac6 GRG |
1441 | for (i = 1; i < GetHeight(); i++) |
1442 | data[i] = data[i - 1] + (3 * GetWidth()); | |
7a632f10 | 1443 | |
b5c91ac6 | 1444 | // precompute coefficients for rotation formula |
ad30de59 | 1445 | // (sine and cosine of the angle) |
7a632f10 JS |
1446 | const double cos_angle = cos(angle); |
1447 | const double sin_angle = sin(angle); | |
1448 | ||
ad30de59 GRG |
1449 | // Create new Image to store the result |
1450 | // First, find rectangle that covers the rotated image; to do that, | |
1451 | // rotate the four corners | |
7a632f10 | 1452 | |
b5c91ac6 | 1453 | const wxRealPoint p0(centre_of_rotation.x, centre_of_rotation.y); |
7a632f10 | 1454 | |
b5c91ac6 GRG |
1455 | wxRealPoint p1 = rotated_point (0, 0, cos_angle, sin_angle, p0); |
1456 | wxRealPoint p2 = rotated_point (0, GetHeight(), cos_angle, sin_angle, p0); | |
1457 | wxRealPoint p3 = rotated_point (GetWidth(), 0, cos_angle, sin_angle, p0); | |
1458 | wxRealPoint p4 = rotated_point (GetWidth(), GetHeight(), cos_angle, sin_angle, p0); | |
7a632f10 | 1459 | |
57c1c6cb BJ |
1460 | int x1 = (int) floor (wxMin (wxMin(p1.x, p2.x), wxMin(p3.x, p4.x))); |
1461 | int y1 = (int) floor (wxMin (wxMin(p1.y, p2.y), wxMin(p3.y, p4.y))); | |
57c1c6cb BJ |
1462 | int x2 = (int) ceil (wxMax (wxMax(p1.x, p2.x), wxMax(p3.x, p4.x))); |
1463 | int y2 = (int) ceil (wxMax (wxMax(p1.y, p2.y), wxMax(p3.y, p4.y))); | |
7a632f10 JS |
1464 | |
1465 | wxImage rotated (x2 - x1 + 1, y2 - y1 + 1); | |
1466 | ||
1467 | if (offset_after_rotation != NULL) | |
1468 | { | |
06b466c7 | 1469 | *offset_after_rotation = wxPoint (x1, y1); |
7a632f10 JS |
1470 | } |
1471 | ||
b5c91ac6 GRG |
1472 | // GRG: The rotated (destination) image is always accessed |
1473 | // sequentially, so there is no need for a pointer-based | |
1474 | // array here (and in fact it would be slower). | |
1475 | // | |
1476 | unsigned char * dst = rotated.GetData(); | |
7a632f10 | 1477 | |
ad30de59 GRG |
1478 | // GRG: if the original image has a mask, use its RGB values |
1479 | // as the blank pixel, else, fall back to default (black). | |
1480 | // | |
b5c91ac6 GRG |
1481 | unsigned char blank_r = 0; |
1482 | unsigned char blank_g = 0; | |
1483 | unsigned char blank_b = 0; | |
ad30de59 GRG |
1484 | |
1485 | if (HasMask()) | |
1486 | { | |
b5c91ac6 GRG |
1487 | blank_r = GetMaskRed(); |
1488 | blank_g = GetMaskGreen(); | |
1489 | blank_b = GetMaskBlue(); | |
1490 | rotated.SetMaskColour( blank_r, blank_g, blank_b ); | |
ad30de59 GRG |
1491 | } |
1492 | ||
1493 | // Now, for each point of the rotated image, find where it came from, by | |
1494 | // performing an inverse rotation (a rotation of -angle) and getting the | |
1495 | // pixel at those coordinates | |
1496 | ||
b5c91ac6 GRG |
1497 | // GRG: I've taken the (interpolating) test out of the loops, so that |
1498 | // it is done only once, instead of repeating it for each pixel. | |
7a632f10 JS |
1499 | |
1500 | int x; | |
b5c91ac6 | 1501 | if (interpolating) |
7a632f10 JS |
1502 | { |
1503 | for (int y = 0; y < rotated.GetHeight(); y++) | |
1504 | { | |
b5c91ac6 | 1505 | for (x = 0; x < rotated.GetWidth(); x++) |
7a632f10 | 1506 | { |
b5c91ac6 GRG |
1507 | wxRealPoint src = rotated_point (x + x1, y + y1, cos_angle, -sin_angle, p0); |
1508 | ||
f2506310 JS |
1509 | if (-0.25 < src.x && src.x < GetWidth() - 0.75 && |
1510 | -0.25 < src.y && src.y < GetHeight() - 0.75) | |
7a632f10 | 1511 | { |
ad30de59 GRG |
1512 | // interpolate using the 4 enclosing grid-points. Those |
1513 | // points can be obtained using floor and ceiling of the | |
1514 | // exact coordinates of the point | |
f2506310 | 1515 | // C.M. 2000-02-17: when the point is near the border, special care is required. |
7a632f10 | 1516 | |
f2506310 JS |
1517 | int x1, y1, x2, y2; |
1518 | ||
1519 | if (0 < src.x && src.x < GetWidth() - 1) | |
1520 | { | |
1521 | x1 = wxCint(floor(src.x)); | |
1522 | x2 = wxCint(ceil(src.x)); | |
1523 | } | |
1524 | else // else means that x is near one of the borders (0 or width-1) | |
1525 | { | |
1526 | x1 = x2 = wxCint (src.x); | |
1527 | } | |
1528 | ||
1529 | if (0 < src.y && src.y < GetHeight() - 1) | |
1530 | { | |
1531 | y1 = wxCint(floor(src.y)); | |
1532 | y2 = wxCint(ceil(src.y)); | |
1533 | } | |
1534 | else | |
1535 | { | |
1536 | y1 = y2 = wxCint (src.y); | |
1537 | } | |
7a632f10 | 1538 | |
ad30de59 GRG |
1539 | // get four points and the distances (square of the distance, |
1540 | // for efficiency reasons) for the interpolation formula | |
b5c91ac6 GRG |
1541 | |
1542 | // GRG: Do not calculate the points until they are | |
1543 | // really needed -- this way we can calculate | |
1544 | // just one, instead of four, if d1, d2, d3 | |
1545 | // or d4 are < gs_Epsilon | |
7a632f10 JS |
1546 | |
1547 | const double d1 = (src.x - x1) * (src.x - x1) + (src.y - y1) * (src.y - y1); | |
1548 | const double d2 = (src.x - x2) * (src.x - x2) + (src.y - y1) * (src.y - y1); | |
1549 | const double d3 = (src.x - x2) * (src.x - x2) + (src.y - y2) * (src.y - y2); | |
1550 | const double d4 = (src.x - x1) * (src.x - x1) + (src.y - y2) * (src.y - y2); | |
1551 | ||
ad30de59 GRG |
1552 | // Now interpolate as a weighted average of the four surrounding |
1553 | // points, where the weights are the distances to each of those points | |
7a632f10 | 1554 | |
ad30de59 GRG |
1555 | // If the point is exactly at one point of the grid of the source |
1556 | // image, then don't interpolate -- just assign the pixel | |
7a632f10 | 1557 | |
06b466c7 | 1558 | if (d1 < gs_Epsilon) // d1,d2,d3,d4 are positive -- no need for abs() |
7a632f10 | 1559 | { |
b5c91ac6 GRG |
1560 | unsigned char *p = data[y1] + (3 * x1); |
1561 | *(dst++) = *(p++); | |
1562 | *(dst++) = *(p++); | |
1563 | *(dst++) = *(p++); | |
7a632f10 JS |
1564 | } |
1565 | else if (d2 < gs_Epsilon) | |
1566 | { | |
b5c91ac6 GRG |
1567 | unsigned char *p = data[y1] + (3 * x2); |
1568 | *(dst++) = *(p++); | |
1569 | *(dst++) = *(p++); | |
1570 | *(dst++) = *(p++); | |
7a632f10 JS |
1571 | } |
1572 | else if (d3 < gs_Epsilon) | |
1573 | { | |
b5c91ac6 GRG |
1574 | unsigned char *p = data[y2] + (3 * x2); |
1575 | *(dst++) = *(p++); | |
1576 | *(dst++) = *(p++); | |
1577 | *(dst++) = *(p++); | |
7a632f10 JS |
1578 | } |
1579 | else if (d4 < gs_Epsilon) | |
1580 | { | |
b5c91ac6 GRG |
1581 | unsigned char *p = data[y2] + (3 * x1); |
1582 | *(dst++) = *(p++); | |
1583 | *(dst++) = *(p++); | |
1584 | *(dst++) = *(p++); | |
7a632f10 JS |
1585 | } |
1586 | else | |
1587 | { | |
06b466c7 | 1588 | // weights for the weighted average are proportional to the inverse of the distance |
b5c91ac6 GRG |
1589 | unsigned char *v1 = data[y1] + (3 * x1); |
1590 | unsigned char *v2 = data[y1] + (3 * x2); | |
1591 | unsigned char *v3 = data[y2] + (3 * x2); | |
1592 | unsigned char *v4 = data[y2] + (3 * x1); | |
1593 | ||
06b466c7 VZ |
1594 | const double w1 = 1/d1, w2 = 1/d2, w3 = 1/d3, w4 = 1/d4; |
1595 | ||
b5c91ac6 GRG |
1596 | // GRG: Unrolled. |
1597 | ||
1598 | *(dst++) = (unsigned char) | |
1599 | ( (w1 * *(v1++) + w2 * *(v2++) + | |
1600 | w3 * *(v3++) + w4 * *(v4++)) / | |
1601 | (w1 + w2 + w3 + w4) ); | |
1602 | *(dst++) = (unsigned char) | |
1603 | ( (w1 * *(v1++) + w2 * *(v2++) + | |
1604 | w3 * *(v3++) + w4 * *(v4++)) / | |
1605 | (w1 + w2 + w3 + w4) ); | |
1606 | *(dst++) = (unsigned char) | |
1607 | ( (w1 * *(v1++) + w2 * *(v2++) + | |
1608 | w3 * *(v3++) + w4 * *(v4++)) / | |
1609 | (w1 + w2 + w3 + w4) ); | |
7a632f10 JS |
1610 | } |
1611 | } | |
1612 | else | |
1613 | { | |
b5c91ac6 GRG |
1614 | *(dst++) = blank_r; |
1615 | *(dst++) = blank_g; | |
1616 | *(dst++) = blank_b; | |
7a632f10 JS |
1617 | } |
1618 | } | |
b5c91ac6 GRG |
1619 | } |
1620 | } | |
1621 | else // not interpolating | |
1622 | { | |
1623 | for (int y = 0; y < rotated.GetHeight(); y++) | |
1624 | { | |
1625 | for (x = 0; x < rotated.GetWidth(); x++) | |
7a632f10 | 1626 | { |
b5c91ac6 GRG |
1627 | wxRealPoint src = rotated_point (x + x1, y + y1, cos_angle, -sin_angle, p0); |
1628 | ||
1629 | const int xs = wxCint (src.x); // wxCint rounds to the | |
457e6c54 | 1630 | const int ys = wxCint (src.y); // closest integer |
7a632f10 | 1631 | |
b5c91ac6 GRG |
1632 | if (0 <= xs && xs < GetWidth() && |
1633 | 0 <= ys && ys < GetHeight()) | |
7a632f10 | 1634 | { |
b5c91ac6 GRG |
1635 | unsigned char *p = data[ys] + (3 * xs); |
1636 | *(dst++) = *(p++); | |
1637 | *(dst++) = *(p++); | |
1638 | *(dst++) = *(p++); | |
7a632f10 JS |
1639 | } |
1640 | else | |
1641 | { | |
b5c91ac6 GRG |
1642 | *(dst++) = blank_r; |
1643 | *(dst++) = blank_g; | |
1644 | *(dst++) = blank_b; | |
7a632f10 JS |
1645 | } |
1646 | } | |
1647 | } | |
1648 | } | |
1649 | ||
4aff28fc | 1650 | delete [] data; |
4aff28fc | 1651 | |
7a632f10 JS |
1652 | return rotated; |
1653 | } | |
c9d01afd | 1654 | |
c96ea657 | 1655 | #endif // wxUSE_IMAGE |