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