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