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