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