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