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