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