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