]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/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 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #ifdef __BORLANDC__ | |
14 | #pragma hdrstop | |
15 | #endif | |
16 | ||
17 | #if wxUSE_IMAGE | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/log.h" | |
21 | #include "wx/app.h" | |
22 | #include "wx/hash.h" | |
23 | #endif | |
24 | ||
25 | #include "wx/image.h" | |
26 | #include "wx/bitmap.h" | |
27 | #include "wx/debug.h" | |
28 | #include "wx/filefn.h" | |
29 | #include "wx/wfstream.h" | |
30 | #include "wx/intl.h" | |
31 | #include "wx/module.h" | |
32 | #include "wx/utils.h" | |
33 | #include "wx/math.h" | |
34 | ||
35 | #if wxUSE_XPM | |
36 | #include "wx/xpmdecod.h" | |
37 | #endif | |
38 | ||
39 | // For memcpy | |
40 | #include <string.h> | |
41 | ||
42 | //----------------------------------------------------------------------------- | |
43 | // wxImage | |
44 | //----------------------------------------------------------------------------- | |
45 | ||
46 | class wxImageRefData: public wxObjectRefData | |
47 | { | |
48 | public: | |
49 | wxImageRefData(); | |
50 | virtual ~wxImageRefData(); | |
51 | ||
52 | int m_width; | |
53 | int m_height; | |
54 | unsigned char *m_data; | |
55 | ||
56 | bool m_hasMask; | |
57 | unsigned char m_maskRed,m_maskGreen,m_maskBlue; | |
58 | ||
59 | // alpha channel data, may be NULL for the formats without alpha support | |
60 | unsigned char *m_alpha; | |
61 | ||
62 | bool m_ok; | |
63 | ||
64 | // if true, m_data is pointer to static data and shouldn't be freed | |
65 | bool m_static; | |
66 | ||
67 | // same as m_static but for m_alpha | |
68 | bool m_staticAlpha; | |
69 | ||
70 | #if wxUSE_PALETTE | |
71 | wxPalette m_palette; | |
72 | #endif // wxUSE_PALETTE | |
73 | ||
74 | wxArrayString m_optionNames; | |
75 | wxArrayString m_optionValues; | |
76 | ||
77 | DECLARE_NO_COPY_CLASS(wxImageRefData) | |
78 | }; | |
79 | ||
80 | wxImageRefData::wxImageRefData() | |
81 | { | |
82 | m_width = 0; | |
83 | m_height = 0; | |
84 | m_data = | |
85 | m_alpha = (unsigned char *) NULL; | |
86 | ||
87 | m_maskRed = 0; | |
88 | m_maskGreen = 0; | |
89 | m_maskBlue = 0; | |
90 | m_hasMask = false; | |
91 | ||
92 | m_ok = false; | |
93 | m_static = | |
94 | m_staticAlpha = false; | |
95 | } | |
96 | ||
97 | wxImageRefData::~wxImageRefData() | |
98 | { | |
99 | if ( !m_static ) | |
100 | free( m_data ); | |
101 | if ( !m_staticAlpha ) | |
102 | free( m_alpha ); | |
103 | } | |
104 | ||
105 | wxList wxImage::sm_handlers; | |
106 | ||
107 | wxImage wxNullImage; | |
108 | ||
109 | //----------------------------------------------------------------------------- | |
110 | ||
111 | #define M_IMGDATA ((wxImageRefData *)m_refData) | |
112 | ||
113 | IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject) | |
114 | ||
115 | wxImage::wxImage( int width, int height, bool clear ) | |
116 | { | |
117 | Create( width, height, clear ); | |
118 | } | |
119 | ||
120 | wxImage::wxImage( int width, int height, unsigned char* data, bool static_data ) | |
121 | { | |
122 | Create( width, height, data, static_data ); | |
123 | } | |
124 | ||
125 | wxImage::wxImage( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data ) | |
126 | { | |
127 | Create( width, height, data, alpha, static_data ); | |
128 | } | |
129 | ||
130 | wxImage::wxImage( const wxString& name, long type, int index ) | |
131 | { | |
132 | LoadFile( name, type, index ); | |
133 | } | |
134 | ||
135 | wxImage::wxImage( const wxString& name, const wxString& mimetype, int index ) | |
136 | { | |
137 | LoadFile( name, mimetype, index ); | |
138 | } | |
139 | ||
140 | #if wxUSE_STREAMS | |
141 | wxImage::wxImage( wxInputStream& stream, long type, int index ) | |
142 | { | |
143 | LoadFile( stream, type, index ); | |
144 | } | |
145 | ||
146 | wxImage::wxImage( wxInputStream& stream, const wxString& mimetype, int index ) | |
147 | { | |
148 | LoadFile( stream, mimetype, index ); | |
149 | } | |
150 | #endif // wxUSE_STREAMS | |
151 | ||
152 | wxImage::wxImage( const char** xpmData ) | |
153 | { | |
154 | Create(xpmData); | |
155 | } | |
156 | ||
157 | wxImage::wxImage( char** xpmData ) | |
158 | { | |
159 | Create((const char**) xpmData); | |
160 | } | |
161 | ||
162 | bool wxImage::Create( const char** xpmData ) | |
163 | { | |
164 | #if wxUSE_XPM | |
165 | UnRef(); | |
166 | ||
167 | wxXPMDecoder decoder; | |
168 | (*this) = decoder.ReadData(xpmData); | |
169 | return Ok(); | |
170 | #else | |
171 | return false; | |
172 | #endif | |
173 | } | |
174 | ||
175 | bool wxImage::Create( int width, int height, bool clear ) | |
176 | { | |
177 | UnRef(); | |
178 | ||
179 | m_refData = new wxImageRefData(); | |
180 | ||
181 | M_IMGDATA->m_data = (unsigned char *) malloc( width*height*3 ); | |
182 | if (!M_IMGDATA->m_data) | |
183 | { | |
184 | UnRef(); | |
185 | return false; | |
186 | } | |
187 | ||
188 | if (clear) | |
189 | memset(M_IMGDATA->m_data, 0, width*height*3); | |
190 | ||
191 | M_IMGDATA->m_width = width; | |
192 | M_IMGDATA->m_height = height; | |
193 | M_IMGDATA->m_ok = true; | |
194 | ||
195 | return true; | |
196 | } | |
197 | ||
198 | bool wxImage::Create( int width, int height, unsigned char* data, bool static_data ) | |
199 | { | |
200 | UnRef(); | |
201 | ||
202 | wxCHECK_MSG( data, false, _T("NULL data in wxImage::Create") ); | |
203 | ||
204 | m_refData = new wxImageRefData(); | |
205 | ||
206 | M_IMGDATA->m_data = data; | |
207 | M_IMGDATA->m_width = width; | |
208 | M_IMGDATA->m_height = height; | |
209 | M_IMGDATA->m_ok = true; | |
210 | M_IMGDATA->m_static = static_data; | |
211 | ||
212 | return true; | |
213 | } | |
214 | ||
215 | bool wxImage::Create( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data ) | |
216 | { | |
217 | UnRef(); | |
218 | ||
219 | wxCHECK_MSG( data, false, _T("NULL data in wxImage::Create") ); | |
220 | ||
221 | m_refData = new wxImageRefData(); | |
222 | ||
223 | M_IMGDATA->m_data = data; | |
224 | M_IMGDATA->m_alpha = alpha; | |
225 | M_IMGDATA->m_width = width; | |
226 | M_IMGDATA->m_height = height; | |
227 | M_IMGDATA->m_ok = true; | |
228 | M_IMGDATA->m_static = static_data; | |
229 | ||
230 | return true; | |
231 | } | |
232 | ||
233 | void wxImage::Destroy() | |
234 | { | |
235 | UnRef(); | |
236 | } | |
237 | ||
238 | wxImage wxImage::Copy() const | |
239 | { | |
240 | wxImage image; | |
241 | ||
242 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
243 | ||
244 | image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false ); | |
245 | ||
246 | unsigned char *data = image.GetData(); | |
247 | ||
248 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
249 | ||
250 | image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); | |
251 | image.SetMask( M_IMGDATA->m_hasMask ); | |
252 | ||
253 | memcpy( data, GetData(), M_IMGDATA->m_width*M_IMGDATA->m_height*3 ); | |
254 | ||
255 | wxImageRefData *imgData = (wxImageRefData *)image.m_refData; | |
256 | ||
257 | // also copy the alpha channel | |
258 | if (HasAlpha()) | |
259 | { | |
260 | image.SetAlpha(); | |
261 | unsigned char* alpha = image.GetAlpha(); | |
262 | memcpy( alpha, GetAlpha(), M_IMGDATA->m_width*M_IMGDATA->m_height ); | |
263 | } | |
264 | ||
265 | // also copy the image options | |
266 | imgData->m_optionNames = M_IMGDATA->m_optionNames; | |
267 | imgData->m_optionValues = M_IMGDATA->m_optionValues; | |
268 | ||
269 | return image; | |
270 | } | |
271 | ||
272 | wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const | |
273 | { | |
274 | if( xFactor == 1 && yFactor == 1 ) | |
275 | return Copy() ; | |
276 | ||
277 | wxImage image; | |
278 | ||
279 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
280 | ||
281 | // can't scale to/from 0 size | |
282 | wxCHECK_MSG( (xFactor > 0) && (yFactor > 0), image, | |
283 | wxT("invalid new image size") ); | |
284 | ||
285 | long old_height = M_IMGDATA->m_height, | |
286 | old_width = M_IMGDATA->m_width; | |
287 | ||
288 | wxCHECK_MSG( (old_height > 0) && (old_width > 0), image, | |
289 | wxT("invalid old image size") ); | |
290 | ||
291 | long width = old_width / xFactor ; | |
292 | long height = old_height / yFactor ; | |
293 | ||
294 | image.Create( width, height, false ); | |
295 | ||
296 | char unsigned *data = image.GetData(); | |
297 | ||
298 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
299 | ||
300 | bool hasMask = false ; | |
301 | unsigned char maskRed = 0; | |
302 | unsigned char maskGreen = 0; | |
303 | unsigned char maskBlue =0 ; | |
304 | ||
305 | unsigned char *source_data = M_IMGDATA->m_data; | |
306 | unsigned char *target_data = data; | |
307 | unsigned char *source_alpha = 0 ; | |
308 | unsigned char *target_alpha = 0 ; | |
309 | if (M_IMGDATA->m_hasMask) | |
310 | { | |
311 | hasMask = true ; | |
312 | maskRed = M_IMGDATA->m_maskRed; | |
313 | maskGreen = M_IMGDATA->m_maskGreen; | |
314 | maskBlue =M_IMGDATA->m_maskBlue ; | |
315 | ||
316 | image.SetMaskColour( M_IMGDATA->m_maskRed, | |
317 | M_IMGDATA->m_maskGreen, | |
318 | M_IMGDATA->m_maskBlue ); | |
319 | } | |
320 | else | |
321 | { | |
322 | source_alpha = M_IMGDATA->m_alpha ; | |
323 | if ( source_alpha ) | |
324 | { | |
325 | image.SetAlpha() ; | |
326 | target_alpha = image.GetAlpha() ; | |
327 | } | |
328 | } | |
329 | ||
330 | for (long y = 0; y < height; y++) | |
331 | { | |
332 | for (long x = 0; x < width; x++) | |
333 | { | |
334 | unsigned long avgRed = 0 ; | |
335 | unsigned long avgGreen = 0; | |
336 | unsigned long avgBlue = 0; | |
337 | unsigned long avgAlpha = 0 ; | |
338 | unsigned long counter = 0 ; | |
339 | // determine average | |
340 | for ( int y1 = 0 ; y1 < yFactor ; ++y1 ) | |
341 | { | |
342 | long y_offset = (y * yFactor + y1) * old_width; | |
343 | for ( int x1 = 0 ; x1 < xFactor ; ++x1 ) | |
344 | { | |
345 | unsigned char *pixel = source_data + 3 * ( y_offset + x * xFactor + x1 ) ; | |
346 | unsigned char red = pixel[0] ; | |
347 | unsigned char green = pixel[1] ; | |
348 | unsigned char blue = pixel[2] ; | |
349 | unsigned char alpha = 255 ; | |
350 | if ( source_alpha ) | |
351 | alpha = *(source_alpha + y_offset + x * xFactor + x1) ; | |
352 | if ( !hasMask || red != maskRed || green != maskGreen || blue != maskBlue ) | |
353 | { | |
354 | if ( alpha > 0 ) | |
355 | { | |
356 | avgRed += red ; | |
357 | avgGreen += green ; | |
358 | avgBlue += blue ; | |
359 | } | |
360 | avgAlpha += alpha ; | |
361 | counter++ ; | |
362 | } | |
363 | } | |
364 | } | |
365 | if ( counter == 0 ) | |
366 | { | |
367 | *(target_data++) = M_IMGDATA->m_maskRed ; | |
368 | *(target_data++) = M_IMGDATA->m_maskGreen ; | |
369 | *(target_data++) = M_IMGDATA->m_maskBlue ; | |
370 | } | |
371 | else | |
372 | { | |
373 | if ( source_alpha ) | |
374 | *(target_alpha++) = (unsigned char)(avgAlpha / counter ) ; | |
375 | *(target_data++) = (unsigned char)(avgRed / counter); | |
376 | *(target_data++) = (unsigned char)(avgGreen / counter); | |
377 | *(target_data++) = (unsigned char)(avgBlue / counter); | |
378 | } | |
379 | } | |
380 | } | |
381 | ||
382 | // In case this is a cursor, make sure the hotspot is scaled accordingly: | |
383 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ) | |
384 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, | |
385 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X))/xFactor); | |
386 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ) | |
387 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, | |
388 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y))/yFactor); | |
389 | ||
390 | return image; | |
391 | } | |
392 | ||
393 | wxImage wxImage::Scale( int width, int height ) const | |
394 | { | |
395 | wxImage image; | |
396 | ||
397 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
398 | ||
399 | // can't scale to/from 0 size | |
400 | wxCHECK_MSG( (width > 0) && (height > 0), image, | |
401 | wxT("invalid new image size") ); | |
402 | ||
403 | long old_height = M_IMGDATA->m_height, | |
404 | old_width = M_IMGDATA->m_width; | |
405 | wxCHECK_MSG( (old_height > 0) && (old_width > 0), image, | |
406 | wxT("invalid old image size") ); | |
407 | ||
408 | if ( old_width % width == 0 && old_width >= width && | |
409 | old_height % height == 0 && old_height >= height ) | |
410 | { | |
411 | return ShrinkBy( old_width / width , old_height / height ) ; | |
412 | } | |
413 | image.Create( width, height, false ); | |
414 | ||
415 | unsigned char *data = image.GetData(); | |
416 | ||
417 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
418 | ||
419 | unsigned char *source_data = M_IMGDATA->m_data; | |
420 | unsigned char *target_data = data; | |
421 | unsigned char *source_alpha = 0 ; | |
422 | unsigned char *target_alpha = 0 ; | |
423 | ||
424 | if (M_IMGDATA->m_hasMask) | |
425 | { | |
426 | image.SetMaskColour( M_IMGDATA->m_maskRed, | |
427 | M_IMGDATA->m_maskGreen, | |
428 | M_IMGDATA->m_maskBlue ); | |
429 | } | |
430 | else | |
431 | { | |
432 | source_alpha = M_IMGDATA->m_alpha ; | |
433 | if ( source_alpha ) | |
434 | { | |
435 | image.SetAlpha() ; | |
436 | target_alpha = image.GetAlpha() ; | |
437 | } | |
438 | } | |
439 | ||
440 | long x_delta = (old_width<<16) / width; | |
441 | long y_delta = (old_height<<16) / height; | |
442 | ||
443 | unsigned char* dest_pixel = target_data; | |
444 | ||
445 | long y = 0; | |
446 | for ( long j = 0; j < height; j++ ) | |
447 | { | |
448 | unsigned char* src_line = &source_data[(y>>16)*old_width*3]; | |
449 | unsigned char* src_alpha_line = source_alpha ? &source_alpha[(y>>16)*old_width] : 0 ; | |
450 | ||
451 | long x = 0; | |
452 | for ( long i = 0; i < width; i++ ) | |
453 | { | |
454 | unsigned char* src_pixel = &src_line[(x>>16)*3]; | |
455 | unsigned char* src_alpha_pixel = source_alpha ? &src_alpha_line[(x>>16)] : 0 ; | |
456 | dest_pixel[0] = src_pixel[0]; | |
457 | dest_pixel[1] = src_pixel[1]; | |
458 | dest_pixel[2] = src_pixel[2]; | |
459 | dest_pixel += 3; | |
460 | if ( source_alpha ) | |
461 | *(target_alpha++) = *src_alpha_pixel ; | |
462 | x += x_delta; | |
463 | } | |
464 | ||
465 | y += y_delta; | |
466 | } | |
467 | ||
468 | // In case this is a cursor, make sure the hotspot is scaled accordingly: | |
469 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ) | |
470 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, | |
471 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X)*width)/old_width); | |
472 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ) | |
473 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, | |
474 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y)*height)/old_height); | |
475 | ||
476 | return image; | |
477 | } | |
478 | ||
479 | wxImage wxImage::Rotate90( bool clockwise ) const | |
480 | { | |
481 | wxImage image; | |
482 | ||
483 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
484 | ||
485 | image.Create( M_IMGDATA->m_height, M_IMGDATA->m_width, false ); | |
486 | ||
487 | unsigned char *data = image.GetData(); | |
488 | ||
489 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
490 | ||
491 | unsigned char *source_data = M_IMGDATA->m_data; | |
492 | unsigned char *target_data; | |
493 | unsigned char *alpha_data = 0 ; | |
494 | unsigned char *source_alpha = 0 ; | |
495 | unsigned char *target_alpha = 0 ; | |
496 | ||
497 | if (M_IMGDATA->m_hasMask) | |
498 | { | |
499 | image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); | |
500 | } | |
501 | else | |
502 | { | |
503 | source_alpha = M_IMGDATA->m_alpha ; | |
504 | if ( source_alpha ) | |
505 | { | |
506 | image.SetAlpha() ; | |
507 | alpha_data = image.GetAlpha() ; | |
508 | } | |
509 | } | |
510 | ||
511 | long height = M_IMGDATA->m_height; | |
512 | long width = M_IMGDATA->m_width; | |
513 | ||
514 | for (long j = 0; j < height; j++) | |
515 | { | |
516 | for (long i = 0; i < width; i++) | |
517 | { | |
518 | if (clockwise) | |
519 | { | |
520 | target_data = data + (((i+1)*height) - j - 1)*3; | |
521 | if(source_alpha) | |
522 | target_alpha = alpha_data + (((i+1)*height) - j - 1); | |
523 | } | |
524 | else | |
525 | { | |
526 | target_data = data + ((height*(width-1)) + j - (i*height))*3; | |
527 | if(source_alpha) | |
528 | target_alpha = alpha_data + ((height*(width-1)) + j - (i*height)); | |
529 | } | |
530 | memcpy( target_data, source_data, 3 ); | |
531 | source_data += 3; | |
532 | ||
533 | if(source_alpha) | |
534 | { | |
535 | memcpy( target_alpha, source_alpha, 1 ); | |
536 | source_alpha += 1; | |
537 | } | |
538 | } | |
539 | } | |
540 | ||
541 | return image; | |
542 | } | |
543 | ||
544 | wxImage wxImage::Mirror( bool horizontally ) const | |
545 | { | |
546 | wxImage image; | |
547 | ||
548 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
549 | ||
550 | image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false ); | |
551 | ||
552 | unsigned char *data = image.GetData(); | |
553 | unsigned char *alpha = NULL; | |
554 | ||
555 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
556 | ||
557 | if (M_IMGDATA->m_alpha != NULL) { | |
558 | image.SetAlpha(); | |
559 | alpha = image.GetAlpha(); | |
560 | wxCHECK_MSG( alpha, image, wxT("unable to create alpha channel") ); | |
561 | } | |
562 | ||
563 | if (M_IMGDATA->m_hasMask) | |
564 | image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); | |
565 | ||
566 | long height = M_IMGDATA->m_height; | |
567 | long width = M_IMGDATA->m_width; | |
568 | ||
569 | unsigned char *source_data = M_IMGDATA->m_data; | |
570 | unsigned char *target_data; | |
571 | ||
572 | if (horizontally) | |
573 | { | |
574 | for (long j = 0; j < height; j++) | |
575 | { | |
576 | data += width*3; | |
577 | target_data = data-3; | |
578 | for (long i = 0; i < width; i++) | |
579 | { | |
580 | memcpy( target_data, source_data, 3 ); | |
581 | source_data += 3; | |
582 | target_data -= 3; | |
583 | } | |
584 | } | |
585 | ||
586 | if (alpha != NULL) | |
587 | { | |
588 | // src_alpha starts at the first pixel and increases by 1 after each step | |
589 | // (a step here is the copy of the alpha value of one pixel) | |
590 | const unsigned char *src_alpha = M_IMGDATA->m_alpha; | |
591 | // dest_alpha starts just beyond the first line, decreases before each step, | |
592 | // and after each line is finished, increases by 2 widths (skipping the line | |
593 | // just copied and the line that will be copied next) | |
594 | unsigned char *dest_alpha = alpha + width; | |
595 | ||
596 | for (long jj = 0; jj < height; ++jj) | |
597 | { | |
598 | for (long i = 0; i < width; ++i) { | |
599 | *(--dest_alpha) = *(src_alpha++); // copy one pixel | |
600 | } | |
601 | dest_alpha += 2 * width; // advance beyond the end of the next line | |
602 | } | |
603 | } | |
604 | } | |
605 | else | |
606 | { | |
607 | for (long i = 0; i < height; i++) | |
608 | { | |
609 | target_data = data + 3*width*(height-1-i); | |
610 | memcpy( target_data, source_data, (size_t)3*width ); | |
611 | source_data += 3*width; | |
612 | } | |
613 | ||
614 | if (alpha != NULL) | |
615 | { | |
616 | // src_alpha starts at the first pixel and increases by 1 width after each step | |
617 | // (a step here is the copy of the alpha channel of an entire line) | |
618 | const unsigned char *src_alpha = M_IMGDATA->m_alpha; | |
619 | // dest_alpha starts just beyond the last line (beyond the whole image) | |
620 | // and decreases by 1 width before each step | |
621 | unsigned char *dest_alpha = alpha + width * height; | |
622 | ||
623 | for (long jj = 0; jj < height; ++jj) | |
624 | { | |
625 | dest_alpha -= width; | |
626 | memcpy( dest_alpha, src_alpha, (size_t)width ); | |
627 | src_alpha += width; | |
628 | } | |
629 | } | |
630 | } | |
631 | ||
632 | return image; | |
633 | } | |
634 | ||
635 | wxImage wxImage::GetSubImage( const wxRect &rect ) const | |
636 | { | |
637 | wxImage image; | |
638 | ||
639 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
640 | ||
641 | wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) && | |
642 | (rect.GetRight()<=GetWidth()) && (rect.GetBottom()<=GetHeight()), | |
643 | image, wxT("invalid subimage size") ); | |
644 | ||
645 | const int subwidth = rect.GetWidth(); | |
646 | const int subheight = rect.GetHeight(); | |
647 | ||
648 | image.Create( subwidth, subheight, false ); | |
649 | ||
650 | const unsigned char *src_data = GetData(); | |
651 | const unsigned char *src_alpha = M_IMGDATA->m_alpha; | |
652 | unsigned char *subdata = image.GetData(); | |
653 | unsigned char *subalpha = NULL; | |
654 | ||
655 | wxCHECK_MSG( subdata, image, wxT("unable to create image") ); | |
656 | ||
657 | if (src_alpha != NULL) { | |
658 | image.SetAlpha(); | |
659 | subalpha = image.GetAlpha(); | |
660 | wxCHECK_MSG( subalpha, image, wxT("unable to create alpha channel")); | |
661 | } | |
662 | ||
663 | if (M_IMGDATA->m_hasMask) | |
664 | image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); | |
665 | ||
666 | const int width = GetWidth(); | |
667 | const int pixsoff = rect.GetLeft() + width * rect.GetTop(); | |
668 | ||
669 | src_data += 3 * pixsoff; | |
670 | src_alpha += pixsoff; // won't be used if was NULL, so this is ok | |
671 | ||
672 | for (long j = 0; j < subheight; ++j) | |
673 | { | |
674 | memcpy( subdata, src_data, 3 * subwidth ); | |
675 | subdata += 3 * subwidth; | |
676 | src_data += 3 * width; | |
677 | if (subalpha != NULL) { | |
678 | memcpy( subalpha, src_alpha, subwidth ); | |
679 | subalpha += subwidth; | |
680 | src_alpha += width; | |
681 | } | |
682 | } | |
683 | ||
684 | return image; | |
685 | } | |
686 | ||
687 | wxImage wxImage::Size( const wxSize& size, const wxPoint& pos, | |
688 | int r_, int g_, int b_ ) const | |
689 | { | |
690 | wxImage image; | |
691 | ||
692 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
693 | wxCHECK_MSG( (size.GetWidth() > 0) && (size.GetHeight() > 0), image, wxT("invalid size") ); | |
694 | ||
695 | int width = GetWidth(), height = GetHeight(); | |
696 | image.Create(size.GetWidth(), size.GetHeight(), false); | |
697 | ||
698 | unsigned char r = (unsigned char)r_; | |
699 | unsigned char g = (unsigned char)g_; | |
700 | unsigned char b = (unsigned char)b_; | |
701 | if ((r_ == -1) && (g_ == -1) && (b_ == -1)) | |
702 | { | |
703 | GetOrFindMaskColour( &r, &g, &b ); | |
704 | image.SetMaskColour(r, g, b); | |
705 | } | |
706 | ||
707 | image.SetRGB(wxRect(), r, g, b); | |
708 | ||
709 | wxRect subRect(pos.x, pos.y, width, height); | |
710 | wxRect finalRect(0, 0, size.GetWidth(), size.GetHeight()); | |
711 | ||
712 | subRect.Intersect(finalRect); | |
713 | ||
714 | if (!subRect.IsEmpty()) | |
715 | { | |
716 | if ((subRect.GetWidth() == width) && (subRect.GetHeight() == height)) | |
717 | image.Paste(*this, pos.x, pos.y); | |
718 | else | |
719 | image.Paste(GetSubImage(subRect), pos.x, pos.y); | |
720 | } | |
721 | ||
722 | return image; | |
723 | } | |
724 | ||
725 | void wxImage::Paste( const wxImage &image, int x, int y ) | |
726 | { | |
727 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
728 | wxCHECK_RET( image.Ok(), wxT("invalid image") ); | |
729 | ||
730 | int xx = 0; | |
731 | int yy = 0; | |
732 | int width = image.GetWidth(); | |
733 | int height = image.GetHeight(); | |
734 | ||
735 | if (x < 0) | |
736 | { | |
737 | xx = -x; | |
738 | width += x; | |
739 | } | |
740 | if (y < 0) | |
741 | { | |
742 | yy = -y; | |
743 | height += y; | |
744 | } | |
745 | ||
746 | if ((x+xx)+width > M_IMGDATA->m_width) | |
747 | width = M_IMGDATA->m_width - (x+xx); | |
748 | if ((y+yy)+height > M_IMGDATA->m_height) | |
749 | height = M_IMGDATA->m_height - (y+yy); | |
750 | ||
751 | if (width < 1) return; | |
752 | if (height < 1) return; | |
753 | ||
754 | if ((!HasMask() && !image.HasMask()) || | |
755 | (HasMask() && !image.HasMask()) || | |
756 | ((HasMask() && image.HasMask() && | |
757 | (GetMaskRed()==image.GetMaskRed()) && | |
758 | (GetMaskGreen()==image.GetMaskGreen()) && | |
759 | (GetMaskBlue()==image.GetMaskBlue())))) | |
760 | { | |
761 | width *= 3; | |
762 | unsigned char* source_data = image.GetData() + xx*3 + yy*3*image.GetWidth(); | |
763 | int source_step = image.GetWidth()*3; | |
764 | ||
765 | unsigned char* target_data = GetData() + (x+xx)*3 + (y+yy)*3*M_IMGDATA->m_width; | |
766 | int target_step = M_IMGDATA->m_width*3; | |
767 | for (int j = 0; j < height; j++) | |
768 | { | |
769 | memcpy( target_data, source_data, width ); | |
770 | source_data += source_step; | |
771 | target_data += target_step; | |
772 | } | |
773 | return; | |
774 | } | |
775 | ||
776 | if (!HasMask() && image.HasMask()) | |
777 | { | |
778 | unsigned char r = image.GetMaskRed(); | |
779 | unsigned char g = image.GetMaskGreen(); | |
780 | unsigned char b = image.GetMaskBlue(); | |
781 | ||
782 | width *= 3; | |
783 | unsigned char* source_data = image.GetData() + xx*3 + yy*3*image.GetWidth(); | |
784 | int source_step = image.GetWidth()*3; | |
785 | ||
786 | unsigned char* target_data = GetData() + (x+xx)*3 + (y+yy)*3*M_IMGDATA->m_width; | |
787 | int target_step = M_IMGDATA->m_width*3; | |
788 | ||
789 | for (int j = 0; j < height; j++) | |
790 | { | |
791 | for (int i = 0; i < width; i+=3) | |
792 | { | |
793 | if ((source_data[i] != r) && | |
794 | (source_data[i+1] != g) && | |
795 | (source_data[i+2] != b)) | |
796 | { | |
797 | memcpy( target_data+i, source_data+i, 3 ); | |
798 | } | |
799 | } | |
800 | source_data += source_step; | |
801 | target_data += target_step; | |
802 | } | |
803 | } | |
804 | } | |
805 | ||
806 | void wxImage::Replace( unsigned char r1, unsigned char g1, unsigned char b1, | |
807 | unsigned char r2, unsigned char g2, unsigned char b2 ) | |
808 | { | |
809 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
810 | ||
811 | unsigned char *data = GetData(); | |
812 | ||
813 | const int w = GetWidth(); | |
814 | const int h = GetHeight(); | |
815 | ||
816 | for (int j = 0; j < h; j++) | |
817 | for (int i = 0; i < w; i++) | |
818 | { | |
819 | if ((data[0] == r1) && (data[1] == g1) && (data[2] == b1)) | |
820 | { | |
821 | data[0] = r2; | |
822 | data[1] = g2; | |
823 | data[2] = b2; | |
824 | } | |
825 | data += 3; | |
826 | } | |
827 | } | |
828 | ||
829 | wxImage wxImage::ConvertToGreyscale( double lr, double lg, double lb ) const | |
830 | { | |
831 | wxImage image; | |
832 | ||
833 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
834 | ||
835 | image.Create(M_IMGDATA->m_width, M_IMGDATA->m_height, false); | |
836 | ||
837 | unsigned char *dest = image.GetData(); | |
838 | ||
839 | wxCHECK_MSG( dest, image, wxT("unable to create image") ); | |
840 | ||
841 | unsigned char *src = M_IMGDATA->m_data; | |
842 | bool hasMask = M_IMGDATA->m_hasMask; | |
843 | unsigned char maskRed = M_IMGDATA->m_maskRed; | |
844 | unsigned char maskGreen = M_IMGDATA->m_maskGreen; | |
845 | unsigned char maskBlue = M_IMGDATA->m_maskBlue; | |
846 | ||
847 | if ( hasMask ) | |
848 | image.SetMaskColour(maskRed, maskGreen, maskBlue); | |
849 | ||
850 | const long size = M_IMGDATA->m_width * M_IMGDATA->m_height; | |
851 | for ( long i = 0; i < size; i++, src += 3, dest += 3 ) | |
852 | { | |
853 | // don't modify the mask | |
854 | if ( hasMask && src[0] == maskRed && src[1] == maskGreen && src[2] == maskBlue ) | |
855 | { | |
856 | memcpy(dest, src, 3); | |
857 | } | |
858 | else | |
859 | { | |
860 | // calculate the luma | |
861 | double luma = (src[0] * lr + src[1] * lg + src[2] * lb) + 0.5; | |
862 | dest[0] = dest[1] = dest[2] = wx_static_cast(unsigned char, luma); | |
863 | } | |
864 | } | |
865 | ||
866 | return image; | |
867 | } | |
868 | ||
869 | wxImage wxImage::ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const | |
870 | { | |
871 | wxImage image; | |
872 | ||
873 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
874 | ||
875 | image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false ); | |
876 | ||
877 | unsigned char *data = image.GetData(); | |
878 | ||
879 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
880 | ||
881 | if (M_IMGDATA->m_hasMask) | |
882 | { | |
883 | if (M_IMGDATA->m_maskRed == r && M_IMGDATA->m_maskGreen == g && | |
884 | M_IMGDATA->m_maskBlue == b) | |
885 | image.SetMaskColour( 255, 255, 255 ); | |
886 | else | |
887 | image.SetMaskColour( 0, 0, 0 ); | |
888 | } | |
889 | ||
890 | long size = M_IMGDATA->m_height * M_IMGDATA->m_width; | |
891 | ||
892 | unsigned char *srcd = M_IMGDATA->m_data; | |
893 | unsigned char *tard = image.GetData(); | |
894 | ||
895 | for ( long i = 0; i < size; i++, srcd += 3, tard += 3 ) | |
896 | { | |
897 | if (srcd[0] == r && srcd[1] == g && srcd[2] == b) | |
898 | tard[0] = tard[1] = tard[2] = 255; | |
899 | else | |
900 | tard[0] = tard[1] = tard[2] = 0; | |
901 | } | |
902 | ||
903 | return image; | |
904 | } | |
905 | ||
906 | int wxImage::GetWidth() const | |
907 | { | |
908 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
909 | ||
910 | return M_IMGDATA->m_width; | |
911 | } | |
912 | ||
913 | int wxImage::GetHeight() const | |
914 | { | |
915 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
916 | ||
917 | return M_IMGDATA->m_height; | |
918 | } | |
919 | ||
920 | long wxImage::XYToIndex(int x, int y) const | |
921 | { | |
922 | if ( Ok() && | |
923 | x >= 0 && y >= 0 && | |
924 | x < M_IMGDATA->m_width && y < M_IMGDATA->m_height ) | |
925 | { | |
926 | return y*M_IMGDATA->m_width + x; | |
927 | } | |
928 | ||
929 | return -1; | |
930 | } | |
931 | ||
932 | void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b ) | |
933 | { | |
934 | long pos = XYToIndex(x, y); | |
935 | wxCHECK_RET( pos != -1, wxT("invalid image coordinates") ); | |
936 | ||
937 | pos *= 3; | |
938 | ||
939 | M_IMGDATA->m_data[ pos ] = r; | |
940 | M_IMGDATA->m_data[ pos+1 ] = g; | |
941 | M_IMGDATA->m_data[ pos+2 ] = b; | |
942 | } | |
943 | ||
944 | void wxImage::SetRGB( const wxRect& rect_, unsigned char r, unsigned char g, unsigned char b ) | |
945 | { | |
946 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
947 | ||
948 | wxRect rect(rect_); | |
949 | wxRect imageRect(0, 0, GetWidth(), GetHeight()); | |
950 | if ( rect == wxRect() ) | |
951 | { | |
952 | rect = imageRect; | |
953 | } | |
954 | else | |
955 | { | |
956 | wxCHECK_RET( imageRect.Inside(rect.GetTopLeft()) && | |
957 | imageRect.Inside(rect.GetBottomRight()), | |
958 | wxT("invalid bounding rectangle") ); | |
959 | } | |
960 | ||
961 | int x1 = rect.GetLeft(), | |
962 | y1 = rect.GetTop(), | |
963 | x2 = rect.GetRight() + 1, | |
964 | y2 = rect.GetBottom() + 1; | |
965 | ||
966 | unsigned char *data wxDUMMY_INITIALIZE(NULL); | |
967 | int x, y, width = GetWidth(); | |
968 | for (y = y1; y < y2; y++) | |
969 | { | |
970 | data = M_IMGDATA->m_data + (y*width + x1)*3; | |
971 | for (x = x1; x < x2; x++) | |
972 | { | |
973 | *data++ = r; | |
974 | *data++ = g; | |
975 | *data++ = b; | |
976 | } | |
977 | } | |
978 | } | |
979 | ||
980 | unsigned char wxImage::GetRed( int x, int y ) const | |
981 | { | |
982 | long pos = XYToIndex(x, y); | |
983 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
984 | ||
985 | pos *= 3; | |
986 | ||
987 | return M_IMGDATA->m_data[pos]; | |
988 | } | |
989 | ||
990 | unsigned char wxImage::GetGreen( int x, int y ) const | |
991 | { | |
992 | long pos = XYToIndex(x, y); | |
993 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
994 | ||
995 | pos *= 3; | |
996 | ||
997 | return M_IMGDATA->m_data[pos+1]; | |
998 | } | |
999 | ||
1000 | unsigned char wxImage::GetBlue( int x, int y ) const | |
1001 | { | |
1002 | long pos = XYToIndex(x, y); | |
1003 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
1004 | ||
1005 | pos *= 3; | |
1006 | ||
1007 | return M_IMGDATA->m_data[pos+2]; | |
1008 | } | |
1009 | ||
1010 | bool wxImage::Ok() const | |
1011 | { | |
1012 | // image of 0 width or height can't be considered ok - at least because it | |
1013 | // causes crashes in ConvertToBitmap() if we don't catch it in time | |
1014 | wxImageRefData *data = M_IMGDATA; | |
1015 | return data && data->m_ok && data->m_width && data->m_height; | |
1016 | } | |
1017 | ||
1018 | unsigned char *wxImage::GetData() const | |
1019 | { | |
1020 | wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") ); | |
1021 | ||
1022 | return M_IMGDATA->m_data; | |
1023 | } | |
1024 | ||
1025 | void wxImage::SetData( unsigned char *data, bool static_data ) | |
1026 | { | |
1027 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1028 | ||
1029 | wxImageRefData *newRefData = new wxImageRefData(); | |
1030 | ||
1031 | newRefData->m_width = M_IMGDATA->m_width; | |
1032 | newRefData->m_height = M_IMGDATA->m_height; | |
1033 | newRefData->m_data = data; | |
1034 | newRefData->m_ok = true; | |
1035 | newRefData->m_maskRed = M_IMGDATA->m_maskRed; | |
1036 | newRefData->m_maskGreen = M_IMGDATA->m_maskGreen; | |
1037 | newRefData->m_maskBlue = M_IMGDATA->m_maskBlue; | |
1038 | newRefData->m_hasMask = M_IMGDATA->m_hasMask; | |
1039 | newRefData->m_static = static_data; | |
1040 | ||
1041 | UnRef(); | |
1042 | ||
1043 | m_refData = newRefData; | |
1044 | } | |
1045 | ||
1046 | void wxImage::SetData( unsigned char *data, int new_width, int new_height, bool static_data ) | |
1047 | { | |
1048 | wxImageRefData *newRefData = new wxImageRefData(); | |
1049 | ||
1050 | if (m_refData) | |
1051 | { | |
1052 | newRefData->m_width = new_width; | |
1053 | newRefData->m_height = new_height; | |
1054 | newRefData->m_data = data; | |
1055 | newRefData->m_ok = true; | |
1056 | newRefData->m_maskRed = M_IMGDATA->m_maskRed; | |
1057 | newRefData->m_maskGreen = M_IMGDATA->m_maskGreen; | |
1058 | newRefData->m_maskBlue = M_IMGDATA->m_maskBlue; | |
1059 | newRefData->m_hasMask = M_IMGDATA->m_hasMask; | |
1060 | } | |
1061 | else | |
1062 | { | |
1063 | newRefData->m_width = new_width; | |
1064 | newRefData->m_height = new_height; | |
1065 | newRefData->m_data = data; | |
1066 | newRefData->m_ok = true; | |
1067 | } | |
1068 | newRefData->m_static = static_data; | |
1069 | ||
1070 | UnRef(); | |
1071 | ||
1072 | m_refData = newRefData; | |
1073 | } | |
1074 | ||
1075 | // ---------------------------------------------------------------------------- | |
1076 | // alpha channel support | |
1077 | // ---------------------------------------------------------------------------- | |
1078 | ||
1079 | void wxImage::SetAlpha(int x, int y, unsigned char alpha) | |
1080 | { | |
1081 | wxCHECK_RET( HasAlpha(), wxT("no alpha channel") ); | |
1082 | ||
1083 | long pos = XYToIndex(x, y); | |
1084 | wxCHECK_RET( pos != -1, wxT("invalid image coordinates") ); | |
1085 | ||
1086 | M_IMGDATA->m_alpha[pos] = alpha; | |
1087 | } | |
1088 | ||
1089 | unsigned char wxImage::GetAlpha(int x, int y) const | |
1090 | { | |
1091 | wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") ); | |
1092 | ||
1093 | long pos = XYToIndex(x, y); | |
1094 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
1095 | ||
1096 | return M_IMGDATA->m_alpha[pos]; | |
1097 | } | |
1098 | ||
1099 | bool | |
1100 | wxImage::ConvertColourToAlpha(unsigned char r, unsigned char g, unsigned char b) | |
1101 | { | |
1102 | SetAlpha(NULL); | |
1103 | ||
1104 | const int w = M_IMGDATA->m_width; | |
1105 | const int h = M_IMGDATA->m_height; | |
1106 | ||
1107 | unsigned char *alpha = GetAlpha(); | |
1108 | unsigned char *data = GetData(); | |
1109 | ||
1110 | for ( int y = 0; y < h; y++ ) | |
1111 | { | |
1112 | for ( int x = 0; x < w; x++ ) | |
1113 | { | |
1114 | *alpha++ = *data; | |
1115 | *data++ = r; | |
1116 | *data++ = g; | |
1117 | *data++ = b; | |
1118 | } | |
1119 | } | |
1120 | ||
1121 | return true; | |
1122 | } | |
1123 | ||
1124 | void wxImage::SetAlpha( unsigned char *alpha, bool static_data ) | |
1125 | { | |
1126 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1127 | ||
1128 | if ( !alpha ) | |
1129 | { | |
1130 | alpha = (unsigned char *)malloc(M_IMGDATA->m_width*M_IMGDATA->m_height); | |
1131 | } | |
1132 | ||
1133 | free(M_IMGDATA->m_alpha); | |
1134 | M_IMGDATA->m_alpha = alpha; | |
1135 | M_IMGDATA->m_staticAlpha = static_data; | |
1136 | } | |
1137 | ||
1138 | unsigned char *wxImage::GetAlpha() const | |
1139 | { | |
1140 | wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") ); | |
1141 | ||
1142 | return M_IMGDATA->m_alpha; | |
1143 | } | |
1144 | ||
1145 | void wxImage::InitAlpha() | |
1146 | { | |
1147 | wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") ); | |
1148 | ||
1149 | // initialize memory for alpha channel | |
1150 | SetAlpha(); | |
1151 | ||
1152 | unsigned char *alpha = M_IMGDATA->m_alpha; | |
1153 | const size_t lenAlpha = M_IMGDATA->m_width * M_IMGDATA->m_height; | |
1154 | ||
1155 | if ( HasMask() ) | |
1156 | { | |
1157 | // use the mask to initialize the alpha channel. | |
1158 | const unsigned char * const alphaEnd = alpha + lenAlpha; | |
1159 | ||
1160 | const unsigned char mr = M_IMGDATA->m_maskRed; | |
1161 | const unsigned char mg = M_IMGDATA->m_maskGreen; | |
1162 | const unsigned char mb = M_IMGDATA->m_maskBlue; | |
1163 | for ( unsigned char *src = M_IMGDATA->m_data; | |
1164 | alpha < alphaEnd; | |
1165 | src += 3, alpha++ ) | |
1166 | { | |
1167 | *alpha = (src[0] == mr && src[1] == mg && src[2] == mb) | |
1168 | ? wxIMAGE_ALPHA_TRANSPARENT | |
1169 | : wxIMAGE_ALPHA_OPAQUE; | |
1170 | } | |
1171 | ||
1172 | M_IMGDATA->m_hasMask = false; | |
1173 | } | |
1174 | else // no mask | |
1175 | { | |
1176 | // make the image fully opaque | |
1177 | memset(alpha, wxIMAGE_ALPHA_OPAQUE, lenAlpha); | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | // ---------------------------------------------------------------------------- | |
1182 | // mask support | |
1183 | // ---------------------------------------------------------------------------- | |
1184 | ||
1185 | void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b ) | |
1186 | { | |
1187 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1188 | ||
1189 | M_IMGDATA->m_maskRed = r; | |
1190 | M_IMGDATA->m_maskGreen = g; | |
1191 | M_IMGDATA->m_maskBlue = b; | |
1192 | M_IMGDATA->m_hasMask = true; | |
1193 | } | |
1194 | ||
1195 | bool wxImage::GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned char *b ) const | |
1196 | { | |
1197 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
1198 | ||
1199 | if (M_IMGDATA->m_hasMask) | |
1200 | { | |
1201 | if (r) *r = M_IMGDATA->m_maskRed; | |
1202 | if (g) *g = M_IMGDATA->m_maskGreen; | |
1203 | if (b) *b = M_IMGDATA->m_maskBlue; | |
1204 | return true; | |
1205 | } | |
1206 | else | |
1207 | { | |
1208 | FindFirstUnusedColour(r, g, b); | |
1209 | return false; | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | unsigned char wxImage::GetMaskRed() const | |
1214 | { | |
1215 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
1216 | ||
1217 | return M_IMGDATA->m_maskRed; | |
1218 | } | |
1219 | ||
1220 | unsigned char wxImage::GetMaskGreen() const | |
1221 | { | |
1222 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
1223 | ||
1224 | return M_IMGDATA->m_maskGreen; | |
1225 | } | |
1226 | ||
1227 | unsigned char wxImage::GetMaskBlue() const | |
1228 | { | |
1229 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
1230 | ||
1231 | return M_IMGDATA->m_maskBlue; | |
1232 | } | |
1233 | ||
1234 | void wxImage::SetMask( bool mask ) | |
1235 | { | |
1236 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1237 | ||
1238 | M_IMGDATA->m_hasMask = mask; | |
1239 | } | |
1240 | ||
1241 | bool wxImage::HasMask() const | |
1242 | { | |
1243 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
1244 | ||
1245 | return M_IMGDATA->m_hasMask; | |
1246 | } | |
1247 | ||
1248 | bool wxImage::IsTransparent(int x, int y, unsigned char threshold) const | |
1249 | { | |
1250 | long pos = XYToIndex(x, y); | |
1251 | wxCHECK_MSG( pos != -1, false, wxT("invalid image coordinates") ); | |
1252 | ||
1253 | // check mask | |
1254 | if ( M_IMGDATA->m_hasMask ) | |
1255 | { | |
1256 | const unsigned char *p = M_IMGDATA->m_data + 3*pos; | |
1257 | if ( p[0] == M_IMGDATA->m_maskRed && | |
1258 | p[1] == M_IMGDATA->m_maskGreen && | |
1259 | p[2] == M_IMGDATA->m_maskBlue ) | |
1260 | { | |
1261 | return true; | |
1262 | } | |
1263 | } | |
1264 | ||
1265 | // then check alpha | |
1266 | if ( M_IMGDATA->m_alpha ) | |
1267 | { | |
1268 | if ( M_IMGDATA->m_alpha[pos] < threshold ) | |
1269 | { | |
1270 | // transparent enough | |
1271 | return true; | |
1272 | } | |
1273 | } | |
1274 | ||
1275 | // not transparent | |
1276 | return false; | |
1277 | } | |
1278 | ||
1279 | bool wxImage::SetMaskFromImage(const wxImage& mask, | |
1280 | unsigned char mr, unsigned char mg, unsigned char mb) | |
1281 | { | |
1282 | // check that the images are the same size | |
1283 | if ( (M_IMGDATA->m_height != mask.GetHeight() ) || (M_IMGDATA->m_width != mask.GetWidth () ) ) | |
1284 | { | |
1285 | wxLogError( _("Image and mask have different sizes.") ); | |
1286 | return false; | |
1287 | } | |
1288 | ||
1289 | // find unused colour | |
1290 | unsigned char r,g,b ; | |
1291 | if (!FindFirstUnusedColour(&r, &g, &b)) | |
1292 | { | |
1293 | wxLogError( _("No unused colour in image being masked.") ); | |
1294 | return false ; | |
1295 | } | |
1296 | ||
1297 | unsigned char *imgdata = GetData(); | |
1298 | unsigned char *maskdata = mask.GetData(); | |
1299 | ||
1300 | const int w = GetWidth(); | |
1301 | const int h = GetHeight(); | |
1302 | ||
1303 | for (int j = 0; j < h; j++) | |
1304 | { | |
1305 | for (int i = 0; i < w; i++) | |
1306 | { | |
1307 | if ((maskdata[0] == mr) && (maskdata[1] == mg) && (maskdata[2] == mb)) | |
1308 | { | |
1309 | imgdata[0] = r; | |
1310 | imgdata[1] = g; | |
1311 | imgdata[2] = b; | |
1312 | } | |
1313 | imgdata += 3; | |
1314 | maskdata += 3; | |
1315 | } | |
1316 | } | |
1317 | ||
1318 | SetMaskColour(r, g, b); | |
1319 | SetMask(true); | |
1320 | ||
1321 | return true; | |
1322 | } | |
1323 | ||
1324 | bool wxImage::ConvertAlphaToMask(unsigned char threshold) | |
1325 | { | |
1326 | if (!HasAlpha()) | |
1327 | return true; | |
1328 | ||
1329 | unsigned char mr, mg, mb; | |
1330 | if (!FindFirstUnusedColour(&mr, &mg, &mb)) | |
1331 | { | |
1332 | wxLogError( _("No unused colour in image being masked.") ); | |
1333 | return false; | |
1334 | } | |
1335 | ||
1336 | SetMask(true); | |
1337 | SetMaskColour(mr, mg, mb); | |
1338 | ||
1339 | unsigned char *imgdata = GetData(); | |
1340 | unsigned char *alphadata = GetAlpha(); | |
1341 | ||
1342 | int w = GetWidth(); | |
1343 | int h = GetHeight(); | |
1344 | ||
1345 | for (int y = 0; y < h; y++) | |
1346 | { | |
1347 | for (int x = 0; x < w; x++, imgdata += 3, alphadata++) | |
1348 | { | |
1349 | if (*alphadata < threshold) | |
1350 | { | |
1351 | imgdata[0] = mr; | |
1352 | imgdata[1] = mg; | |
1353 | imgdata[2] = mb; | |
1354 | } | |
1355 | } | |
1356 | } | |
1357 | ||
1358 | free(M_IMGDATA->m_alpha); | |
1359 | M_IMGDATA->m_alpha = NULL; | |
1360 | ||
1361 | return true; | |
1362 | } | |
1363 | ||
1364 | // ---------------------------------------------------------------------------- | |
1365 | // Palette functions | |
1366 | // ---------------------------------------------------------------------------- | |
1367 | ||
1368 | #if wxUSE_PALETTE | |
1369 | ||
1370 | bool wxImage::HasPalette() const | |
1371 | { | |
1372 | if (!Ok()) | |
1373 | return false; | |
1374 | ||
1375 | return M_IMGDATA->m_palette.Ok(); | |
1376 | } | |
1377 | ||
1378 | const wxPalette& wxImage::GetPalette() const | |
1379 | { | |
1380 | wxCHECK_MSG( Ok(), wxNullPalette, wxT("invalid image") ); | |
1381 | ||
1382 | return M_IMGDATA->m_palette; | |
1383 | } | |
1384 | ||
1385 | void wxImage::SetPalette(const wxPalette& palette) | |
1386 | { | |
1387 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1388 | ||
1389 | M_IMGDATA->m_palette = palette; | |
1390 | } | |
1391 | ||
1392 | #endif // wxUSE_PALETTE | |
1393 | ||
1394 | // ---------------------------------------------------------------------------- | |
1395 | // Option functions (arbitrary name/value mapping) | |
1396 | // ---------------------------------------------------------------------------- | |
1397 | ||
1398 | void wxImage::SetOption(const wxString& name, const wxString& value) | |
1399 | { | |
1400 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1401 | ||
1402 | int idx = M_IMGDATA->m_optionNames.Index(name, false); | |
1403 | if (idx == wxNOT_FOUND) | |
1404 | { | |
1405 | M_IMGDATA->m_optionNames.Add(name); | |
1406 | M_IMGDATA->m_optionValues.Add(value); | |
1407 | } | |
1408 | else | |
1409 | { | |
1410 | M_IMGDATA->m_optionNames[idx] = name; | |
1411 | M_IMGDATA->m_optionValues[idx] = value; | |
1412 | } | |
1413 | } | |
1414 | ||
1415 | void wxImage::SetOption(const wxString& name, int value) | |
1416 | { | |
1417 | wxString valStr; | |
1418 | valStr.Printf(wxT("%d"), value); | |
1419 | SetOption(name, valStr); | |
1420 | } | |
1421 | ||
1422 | wxString wxImage::GetOption(const wxString& name) const | |
1423 | { | |
1424 | wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid image") ); | |
1425 | ||
1426 | int idx = M_IMGDATA->m_optionNames.Index(name, false); | |
1427 | if (idx == wxNOT_FOUND) | |
1428 | return wxEmptyString; | |
1429 | else | |
1430 | return M_IMGDATA->m_optionValues[idx]; | |
1431 | } | |
1432 | ||
1433 | int wxImage::GetOptionInt(const wxString& name) const | |
1434 | { | |
1435 | return wxAtoi(GetOption(name)); | |
1436 | } | |
1437 | ||
1438 | bool wxImage::HasOption(const wxString& name) const | |
1439 | { | |
1440 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
1441 | ||
1442 | return (M_IMGDATA->m_optionNames.Index(name, false) != wxNOT_FOUND); | |
1443 | } | |
1444 | ||
1445 | // ---------------------------------------------------------------------------- | |
1446 | // image I/O | |
1447 | // ---------------------------------------------------------------------------- | |
1448 | ||
1449 | bool wxImage::LoadFile( const wxString& filename, long type, int index ) | |
1450 | { | |
1451 | #if wxUSE_STREAMS | |
1452 | if (wxFileExists(filename)) | |
1453 | { | |
1454 | wxFileInputStream stream(filename); | |
1455 | wxBufferedInputStream bstream( stream ); | |
1456 | return LoadFile(bstream, type, index); | |
1457 | } | |
1458 | else | |
1459 | { | |
1460 | wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() ); | |
1461 | ||
1462 | return false; | |
1463 | } | |
1464 | #else // !wxUSE_STREAMS | |
1465 | return false; | |
1466 | #endif // wxUSE_STREAMS | |
1467 | } | |
1468 | ||
1469 | bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype, int index ) | |
1470 | { | |
1471 | #if wxUSE_STREAMS | |
1472 | if (wxFileExists(filename)) | |
1473 | { | |
1474 | wxFileInputStream stream(filename); | |
1475 | wxBufferedInputStream bstream( stream ); | |
1476 | return LoadFile(bstream, mimetype, index); | |
1477 | } | |
1478 | else | |
1479 | { | |
1480 | wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() ); | |
1481 | ||
1482 | return false; | |
1483 | } | |
1484 | #else // !wxUSE_STREAMS | |
1485 | return false; | |
1486 | #endif // wxUSE_STREAMS | |
1487 | } | |
1488 | ||
1489 | ||
1490 | ||
1491 | bool wxImage::SaveFile( const wxString& filename ) const | |
1492 | { | |
1493 | wxString ext = filename.AfterLast('.').Lower(); | |
1494 | ||
1495 | wxImageHandler * pHandler = FindHandler(ext, -1); | |
1496 | if (pHandler) | |
1497 | { | |
1498 | SaveFile(filename, pHandler->GetType()); | |
1499 | return true; | |
1500 | } | |
1501 | ||
1502 | wxLogError(_("Can't save image to file '%s': unknown extension."), filename.c_str()); | |
1503 | ||
1504 | return false; | |
1505 | } | |
1506 | ||
1507 | bool wxImage::SaveFile( const wxString& filename, int type ) const | |
1508 | { | |
1509 | #if wxUSE_STREAMS | |
1510 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
1511 | ||
1512 | ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename); | |
1513 | ||
1514 | wxFileOutputStream stream(filename); | |
1515 | ||
1516 | if ( stream.IsOk() ) | |
1517 | { | |
1518 | wxBufferedOutputStream bstream( stream ); | |
1519 | return SaveFile(bstream, type); | |
1520 | } | |
1521 | #endif // wxUSE_STREAMS | |
1522 | ||
1523 | return false; | |
1524 | } | |
1525 | ||
1526 | bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) const | |
1527 | { | |
1528 | #if wxUSE_STREAMS | |
1529 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
1530 | ||
1531 | ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename); | |
1532 | ||
1533 | wxFileOutputStream stream(filename); | |
1534 | ||
1535 | if ( stream.IsOk() ) | |
1536 | { | |
1537 | wxBufferedOutputStream bstream( stream ); | |
1538 | return SaveFile(bstream, mimetype); | |
1539 | } | |
1540 | #endif // wxUSE_STREAMS | |
1541 | ||
1542 | return false; | |
1543 | } | |
1544 | ||
1545 | bool wxImage::CanRead( const wxString &name ) | |
1546 | { | |
1547 | #if wxUSE_STREAMS | |
1548 | wxFileInputStream stream(name); | |
1549 | return CanRead(stream); | |
1550 | #else | |
1551 | return false; | |
1552 | #endif | |
1553 | } | |
1554 | ||
1555 | int wxImage::GetImageCount( const wxString &name, long type ) | |
1556 | { | |
1557 | #if wxUSE_STREAMS | |
1558 | wxFileInputStream stream(name); | |
1559 | if (stream.Ok()) | |
1560 | return GetImageCount(stream, type); | |
1561 | #endif | |
1562 | ||
1563 | return 0; | |
1564 | } | |
1565 | ||
1566 | #if wxUSE_STREAMS | |
1567 | ||
1568 | bool wxImage::CanRead( wxInputStream &stream ) | |
1569 | { | |
1570 | const wxList& list = GetHandlers(); | |
1571 | ||
1572 | for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() ) | |
1573 | { | |
1574 | wxImageHandler *handler=(wxImageHandler*)node->GetData(); | |
1575 | if (handler->CanRead( stream )) | |
1576 | return true; | |
1577 | } | |
1578 | ||
1579 | return false; | |
1580 | } | |
1581 | ||
1582 | int wxImage::GetImageCount( wxInputStream &stream, long type ) | |
1583 | { | |
1584 | wxImageHandler *handler; | |
1585 | ||
1586 | if ( type == wxBITMAP_TYPE_ANY ) | |
1587 | { | |
1588 | wxList &list=GetHandlers(); | |
1589 | ||
1590 | for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext()) | |
1591 | { | |
1592 | handler=(wxImageHandler*)node->GetData(); | |
1593 | if ( handler->CanRead(stream) ) | |
1594 | return handler->GetImageCount(stream); | |
1595 | ||
1596 | } | |
1597 | ||
1598 | wxLogWarning(_("No handler found for image type.")); | |
1599 | return 0; | |
1600 | } | |
1601 | ||
1602 | handler = FindHandler(type); | |
1603 | ||
1604 | if ( !handler ) | |
1605 | { | |
1606 | wxLogWarning(_("No image handler for type %d defined."), type); | |
1607 | return false; | |
1608 | } | |
1609 | ||
1610 | if ( handler->CanRead(stream) ) | |
1611 | { | |
1612 | return handler->GetImageCount(stream); | |
1613 | } | |
1614 | else | |
1615 | { | |
1616 | wxLogError(_("Image file is not of type %d."), type); | |
1617 | return 0; | |
1618 | } | |
1619 | } | |
1620 | ||
1621 | bool wxImage::LoadFile( wxInputStream& stream, long type, int index ) | |
1622 | { | |
1623 | UnRef(); | |
1624 | ||
1625 | m_refData = new wxImageRefData; | |
1626 | ||
1627 | wxImageHandler *handler; | |
1628 | ||
1629 | if ( type == wxBITMAP_TYPE_ANY ) | |
1630 | { | |
1631 | wxList &list=GetHandlers(); | |
1632 | ||
1633 | for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() ) | |
1634 | { | |
1635 | handler=(wxImageHandler*)node->GetData(); | |
1636 | if ( handler->CanRead(stream) ) | |
1637 | return handler->LoadFile(this, stream, true/*verbose*/, index); | |
1638 | ||
1639 | } | |
1640 | ||
1641 | wxLogWarning( _("No handler found for image type.") ); | |
1642 | return false; | |
1643 | } | |
1644 | ||
1645 | handler = FindHandler(type); | |
1646 | ||
1647 | if (handler == 0) | |
1648 | { | |
1649 | wxLogWarning( _("No image handler for type %d defined."), type ); | |
1650 | ||
1651 | return false; | |
1652 | } | |
1653 | ||
1654 | if (stream.IsSeekable() && !handler->CanRead(stream)) | |
1655 | { | |
1656 | wxLogError(_("Image file is not of type %d."), type); | |
1657 | return false; | |
1658 | } | |
1659 | else | |
1660 | return handler->LoadFile(this, stream, true/*verbose*/, index); | |
1661 | } | |
1662 | ||
1663 | bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int index ) | |
1664 | { | |
1665 | UnRef(); | |
1666 | ||
1667 | m_refData = new wxImageRefData; | |
1668 | ||
1669 | wxImageHandler *handler = FindHandlerMime(mimetype); | |
1670 | ||
1671 | if (handler == 0) | |
1672 | { | |
1673 | wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() ); | |
1674 | ||
1675 | return false; | |
1676 | } | |
1677 | ||
1678 | if (stream.IsSeekable() && !handler->CanRead(stream)) | |
1679 | { | |
1680 | wxLogError(_("Image file is not of type %s."), (const wxChar*) mimetype); | |
1681 | return false; | |
1682 | } | |
1683 | else | |
1684 | return handler->LoadFile( this, stream, true/*verbose*/, index ); | |
1685 | } | |
1686 | ||
1687 | bool wxImage::SaveFile( wxOutputStream& stream, int type ) const | |
1688 | { | |
1689 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
1690 | ||
1691 | wxImageHandler *handler = FindHandler(type); | |
1692 | if ( !handler ) | |
1693 | { | |
1694 | wxLogWarning( _("No image handler for type %d defined."), type ); | |
1695 | ||
1696 | return false; | |
1697 | } | |
1698 | ||
1699 | return handler->SaveFile( (wxImage*)this, stream ); | |
1700 | } | |
1701 | ||
1702 | bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const | |
1703 | { | |
1704 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
1705 | ||
1706 | wxImageHandler *handler = FindHandlerMime(mimetype); | |
1707 | if ( !handler ) | |
1708 | { | |
1709 | wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() ); | |
1710 | ||
1711 | return false; | |
1712 | } | |
1713 | ||
1714 | return handler->SaveFile( (wxImage*)this, stream ); | |
1715 | } | |
1716 | #endif // wxUSE_STREAMS | |
1717 | ||
1718 | // ---------------------------------------------------------------------------- | |
1719 | // image I/O handlers | |
1720 | // ---------------------------------------------------------------------------- | |
1721 | ||
1722 | void wxImage::AddHandler( wxImageHandler *handler ) | |
1723 | { | |
1724 | // Check for an existing handler of the type being added. | |
1725 | if (FindHandler( handler->GetType() ) == 0) | |
1726 | { | |
1727 | sm_handlers.Append( handler ); | |
1728 | } | |
1729 | else | |
1730 | { | |
1731 | // This is not documented behaviour, merely the simplest 'fix' | |
1732 | // for preventing duplicate additions. If someone ever has | |
1733 | // a good reason to add and remove duplicate handlers (and they | |
1734 | // may) we should probably refcount the duplicates. | |
1735 | // also an issue in InsertHandler below. | |
1736 | ||
1737 | wxLogDebug( _T("Adding duplicate image handler for '%s'"), | |
1738 | handler->GetName().c_str() ); | |
1739 | delete handler; | |
1740 | } | |
1741 | } | |
1742 | ||
1743 | void wxImage::InsertHandler( wxImageHandler *handler ) | |
1744 | { | |
1745 | // Check for an existing handler of the type being added. | |
1746 | if (FindHandler( handler->GetType() ) == 0) | |
1747 | { | |
1748 | sm_handlers.Insert( handler ); | |
1749 | } | |
1750 | else | |
1751 | { | |
1752 | // see AddHandler for additional comments. | |
1753 | wxLogDebug( _T("Inserting duplicate image handler for '%s'"), | |
1754 | handler->GetName().c_str() ); | |
1755 | delete handler; | |
1756 | } | |
1757 | } | |
1758 | ||
1759 | bool wxImage::RemoveHandler( const wxString& name ) | |
1760 | { | |
1761 | wxImageHandler *handler = FindHandler(name); | |
1762 | if (handler) | |
1763 | { | |
1764 | sm_handlers.DeleteObject(handler); | |
1765 | delete handler; | |
1766 | return true; | |
1767 | } | |
1768 | else | |
1769 | return false; | |
1770 | } | |
1771 | ||
1772 | wxImageHandler *wxImage::FindHandler( const wxString& name ) | |
1773 | { | |
1774 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); | |
1775 | while (node) | |
1776 | { | |
1777 | wxImageHandler *handler = (wxImageHandler*)node->GetData(); | |
1778 | if (handler->GetName().Cmp(name) == 0) return handler; | |
1779 | ||
1780 | node = node->GetNext(); | |
1781 | } | |
1782 | return 0; | |
1783 | } | |
1784 | ||
1785 | wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType ) | |
1786 | { | |
1787 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); | |
1788 | while (node) | |
1789 | { | |
1790 | wxImageHandler *handler = (wxImageHandler*)node->GetData(); | |
1791 | if ( (handler->GetExtension().Cmp(extension) == 0) && | |
1792 | (bitmapType == -1 || handler->GetType() == bitmapType) ) | |
1793 | return handler; | |
1794 | node = node->GetNext(); | |
1795 | } | |
1796 | return 0; | |
1797 | } | |
1798 | ||
1799 | wxImageHandler *wxImage::FindHandler( long bitmapType ) | |
1800 | { | |
1801 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); | |
1802 | while (node) | |
1803 | { | |
1804 | wxImageHandler *handler = (wxImageHandler *)node->GetData(); | |
1805 | if (handler->GetType() == bitmapType) return handler; | |
1806 | node = node->GetNext(); | |
1807 | } | |
1808 | return 0; | |
1809 | } | |
1810 | ||
1811 | wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype ) | |
1812 | { | |
1813 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); | |
1814 | while (node) | |
1815 | { | |
1816 | wxImageHandler *handler = (wxImageHandler *)node->GetData(); | |
1817 | if (handler->GetMimeType().IsSameAs(mimetype, false)) return handler; | |
1818 | node = node->GetNext(); | |
1819 | } | |
1820 | return 0; | |
1821 | } | |
1822 | ||
1823 | void wxImage::InitStandardHandlers() | |
1824 | { | |
1825 | #if wxUSE_STREAMS | |
1826 | AddHandler(new wxBMPHandler); | |
1827 | #endif // wxUSE_STREAMS | |
1828 | } | |
1829 | ||
1830 | void wxImage::CleanUpHandlers() | |
1831 | { | |
1832 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); | |
1833 | while (node) | |
1834 | { | |
1835 | wxImageHandler *handler = (wxImageHandler *)node->GetData(); | |
1836 | wxList::compatibility_iterator next = node->GetNext(); | |
1837 | delete handler; | |
1838 | node = next; | |
1839 | } | |
1840 | ||
1841 | sm_handlers.Clear(); | |
1842 | } | |
1843 | ||
1844 | wxString wxImage::GetImageExtWildcard() | |
1845 | { | |
1846 | wxString fmts; | |
1847 | ||
1848 | wxList& Handlers = wxImage::GetHandlers(); | |
1849 | wxList::compatibility_iterator Node = Handlers.GetFirst(); | |
1850 | while ( Node ) | |
1851 | { | |
1852 | wxImageHandler* Handler = (wxImageHandler*)Node->GetData(); | |
1853 | fmts += wxT("*.") + Handler->GetExtension(); | |
1854 | Node = Node->GetNext(); | |
1855 | if ( Node ) fmts += wxT(";"); | |
1856 | } | |
1857 | ||
1858 | return wxT("(") + fmts + wxT(")|") + fmts; | |
1859 | } | |
1860 | ||
1861 | wxImage::HSVValue wxImage::RGBtoHSV(const RGBValue& rgb) | |
1862 | { | |
1863 | const double red = rgb.red / 255.0, | |
1864 | green = rgb.green / 255.0, | |
1865 | blue = rgb.blue / 255.0; | |
1866 | ||
1867 | // find the min and max intensity (and remember which one was it for the | |
1868 | // latter) | |
1869 | double minimumRGB = red; | |
1870 | if ( green < minimumRGB ) | |
1871 | minimumRGB = green; | |
1872 | if ( blue < minimumRGB ) | |
1873 | minimumRGB = blue; | |
1874 | ||
1875 | enum { RED, GREEN, BLUE } chMax = RED; | |
1876 | double maximumRGB = red; | |
1877 | if ( green > maximumRGB ) | |
1878 | { | |
1879 | chMax = GREEN; | |
1880 | maximumRGB = green; | |
1881 | } | |
1882 | if ( blue > maximumRGB ) | |
1883 | { | |
1884 | chMax = BLUE; | |
1885 | maximumRGB = blue; | |
1886 | } | |
1887 | ||
1888 | const double value = maximumRGB; | |
1889 | ||
1890 | double hue = 0.0, saturation; | |
1891 | const double deltaRGB = maximumRGB - minimumRGB; | |
1892 | if ( wxIsNullDouble(deltaRGB) ) | |
1893 | { | |
1894 | // Gray has no color | |
1895 | hue = 0.0; | |
1896 | saturation = 0.0; | |
1897 | } | |
1898 | else | |
1899 | { | |
1900 | switch ( chMax ) | |
1901 | { | |
1902 | case RED: | |
1903 | hue = (green - blue) / deltaRGB; | |
1904 | break; | |
1905 | ||
1906 | case GREEN: | |
1907 | hue = 2.0 + (blue - red) / deltaRGB; | |
1908 | break; | |
1909 | ||
1910 | case BLUE: | |
1911 | hue = 4.0 + (red - green) / deltaRGB; | |
1912 | break; | |
1913 | ||
1914 | default: | |
1915 | wxFAIL_MSG(wxT("hue not specified")); | |
1916 | break; | |
1917 | } | |
1918 | ||
1919 | hue /= 6.0; | |
1920 | ||
1921 | if ( hue < 0.0 ) | |
1922 | hue += 1.0; | |
1923 | ||
1924 | saturation = deltaRGB / maximumRGB; | |
1925 | } | |
1926 | ||
1927 | return HSVValue(hue, saturation, value); | |
1928 | } | |
1929 | ||
1930 | wxImage::RGBValue wxImage::HSVtoRGB(const HSVValue& hsv) | |
1931 | { | |
1932 | double red, green, blue; | |
1933 | ||
1934 | if ( wxIsNullDouble(hsv.saturation) ) | |
1935 | { | |
1936 | // Grey | |
1937 | red = hsv.value; | |
1938 | green = hsv.value; | |
1939 | blue = hsv.value; | |
1940 | } | |
1941 | else // not grey | |
1942 | { | |
1943 | double hue = hsv.hue * 6.0; // sector 0 to 5 | |
1944 | int i = (int)floor(hue); | |
1945 | double f = hue - i; // fractional part of h | |
1946 | double p = hsv.value * (1.0 - hsv.saturation); | |
1947 | ||
1948 | switch (i) | |
1949 | { | |
1950 | case 0: | |
1951 | red = hsv.value; | |
1952 | green = hsv.value * (1.0 - hsv.saturation * (1.0 - f)); | |
1953 | blue = p; | |
1954 | break; | |
1955 | ||
1956 | case 1: | |
1957 | red = hsv.value * (1.0 - hsv.saturation * f); | |
1958 | green = hsv.value; | |
1959 | blue = p; | |
1960 | break; | |
1961 | ||
1962 | case 2: | |
1963 | red = p; | |
1964 | green = hsv.value; | |
1965 | blue = hsv.value * (1.0 - hsv.saturation * (1.0 - f)); | |
1966 | break; | |
1967 | ||
1968 | case 3: | |
1969 | red = p; | |
1970 | green = hsv.value * (1.0 - hsv.saturation * f); | |
1971 | blue = hsv.value; | |
1972 | break; | |
1973 | ||
1974 | case 4: | |
1975 | red = hsv.value * (1.0 - hsv.saturation * (1.0 - f)); | |
1976 | green = p; | |
1977 | blue = hsv.value; | |
1978 | break; | |
1979 | ||
1980 | default: // case 5: | |
1981 | red = hsv.value; | |
1982 | green = p; | |
1983 | blue = hsv.value * (1.0 - hsv.saturation * f); | |
1984 | break; | |
1985 | } | |
1986 | } | |
1987 | ||
1988 | return RGBValue((unsigned char)(red * 255.0), | |
1989 | (unsigned char)(green * 255.0), | |
1990 | (unsigned char)(blue * 255.0)); | |
1991 | } | |
1992 | ||
1993 | /* | |
1994 | * Rotates the hue of each pixel of the image. angle is a double in the range | |
1995 | * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees | |
1996 | */ | |
1997 | void wxImage::RotateHue(double angle) | |
1998 | { | |
1999 | unsigned char *srcBytePtr; | |
2000 | unsigned char *dstBytePtr; | |
2001 | unsigned long count; | |
2002 | wxImage::HSVValue hsv; | |
2003 | wxImage::RGBValue rgb; | |
2004 | ||
2005 | wxASSERT (angle >= -1.0 && angle <= 1.0); | |
2006 | count = M_IMGDATA->m_width * M_IMGDATA->m_height; | |
2007 | if ( count > 0 && !wxIsNullDouble(angle) ) | |
2008 | { | |
2009 | srcBytePtr = M_IMGDATA->m_data; | |
2010 | dstBytePtr = srcBytePtr; | |
2011 | do | |
2012 | { | |
2013 | rgb.red = *srcBytePtr++; | |
2014 | rgb.green = *srcBytePtr++; | |
2015 | rgb.blue = *srcBytePtr++; | |
2016 | hsv = RGBtoHSV(rgb); | |
2017 | ||
2018 | hsv.hue = hsv.hue + angle; | |
2019 | if (hsv.hue > 1.0) | |
2020 | hsv.hue = hsv.hue - 1.0; | |
2021 | else if (hsv.hue < 0.0) | |
2022 | hsv.hue = hsv.hue + 1.0; | |
2023 | ||
2024 | rgb = HSVtoRGB(hsv); | |
2025 | *dstBytePtr++ = rgb.red; | |
2026 | *dstBytePtr++ = rgb.green; | |
2027 | *dstBytePtr++ = rgb.blue; | |
2028 | } while (--count != 0); | |
2029 | } | |
2030 | } | |
2031 | ||
2032 | //----------------------------------------------------------------------------- | |
2033 | // wxImageHandler | |
2034 | //----------------------------------------------------------------------------- | |
2035 | ||
2036 | IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject) | |
2037 | ||
2038 | #if wxUSE_STREAMS | |
2039 | bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) ) | |
2040 | { | |
2041 | return false; | |
2042 | } | |
2043 | ||
2044 | bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) ) | |
2045 | { | |
2046 | return false; | |
2047 | } | |
2048 | ||
2049 | int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) ) | |
2050 | { | |
2051 | return 1; | |
2052 | } | |
2053 | ||
2054 | bool wxImageHandler::CanRead( const wxString& name ) | |
2055 | { | |
2056 | if (wxFileExists(name)) | |
2057 | { | |
2058 | wxFileInputStream stream(name); | |
2059 | return CanRead(stream); | |
2060 | } | |
2061 | ||
2062 | wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() ); | |
2063 | ||
2064 | return false; | |
2065 | } | |
2066 | ||
2067 | bool wxImageHandler::CallDoCanRead(wxInputStream& stream) | |
2068 | { | |
2069 | wxFileOffset posOld = stream.TellI(); | |
2070 | if ( posOld == wxInvalidOffset ) | |
2071 | { | |
2072 | // can't test unseekable stream | |
2073 | return false; | |
2074 | } | |
2075 | ||
2076 | bool ok = DoCanRead(stream); | |
2077 | ||
2078 | // restore the old position to be able to test other formats and so on | |
2079 | if ( stream.SeekI(posOld) == wxInvalidOffset ) | |
2080 | { | |
2081 | wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!")); | |
2082 | ||
2083 | // reading would fail anyhow as we're not at the right position | |
2084 | return false; | |
2085 | } | |
2086 | ||
2087 | return ok; | |
2088 | } | |
2089 | ||
2090 | #endif // wxUSE_STREAMS | |
2091 | ||
2092 | // ---------------------------------------------------------------------------- | |
2093 | // image histogram stuff | |
2094 | // ---------------------------------------------------------------------------- | |
2095 | ||
2096 | bool | |
2097 | wxImageHistogram::FindFirstUnusedColour(unsigned char *r, | |
2098 | unsigned char *g, | |
2099 | unsigned char *b, | |
2100 | unsigned char r2, | |
2101 | unsigned char b2, | |
2102 | unsigned char g2) const | |
2103 | { | |
2104 | unsigned long key = MakeKey(r2, g2, b2); | |
2105 | ||
2106 | while ( find(key) != end() ) | |
2107 | { | |
2108 | // color already used | |
2109 | r2++; | |
2110 | if ( r2 >= 255 ) | |
2111 | { | |
2112 | r2 = 0; | |
2113 | g2++; | |
2114 | if ( g2 >= 255 ) | |
2115 | { | |
2116 | g2 = 0; | |
2117 | b2++; | |
2118 | if ( b2 >= 255 ) | |
2119 | { | |
2120 | wxLogError(_("No unused colour in image.") ); | |
2121 | return false; | |
2122 | } | |
2123 | } | |
2124 | } | |
2125 | ||
2126 | key = MakeKey(r2, g2, b2); | |
2127 | } | |
2128 | ||
2129 | if ( r ) | |
2130 | *r = r2; | |
2131 | if ( g ) | |
2132 | *g = g2; | |
2133 | if ( b ) | |
2134 | *b = b2; | |
2135 | ||
2136 | return true; | |
2137 | } | |
2138 | ||
2139 | bool | |
2140 | wxImage::FindFirstUnusedColour(unsigned char *r, | |
2141 | unsigned char *g, | |
2142 | unsigned char *b, | |
2143 | unsigned char r2, | |
2144 | unsigned char b2, | |
2145 | unsigned char g2) const | |
2146 | { | |
2147 | wxImageHistogram histogram; | |
2148 | ||
2149 | ComputeHistogram(histogram); | |
2150 | ||
2151 | return histogram.FindFirstUnusedColour(r, g, b, r2, g2, b2); | |
2152 | } | |
2153 | ||
2154 | ||
2155 | ||
2156 | // GRG, Dic/99 | |
2157 | // Counts and returns the number of different colours. Optionally stops | |
2158 | // when it exceeds 'stopafter' different colours. This is useful, for | |
2159 | // example, to see if the image can be saved as 8-bit (256 colour or | |
2160 | // less, in this case it would be invoked as CountColours(256)). Default | |
2161 | // value for stopafter is -1 (don't care). | |
2162 | // | |
2163 | unsigned long wxImage::CountColours( unsigned long stopafter ) const | |
2164 | { | |
2165 | wxHashTable h; | |
2166 | wxObject dummy; | |
2167 | unsigned char r, g, b; | |
2168 | unsigned char *p; | |
2169 | unsigned long size, nentries, key; | |
2170 | ||
2171 | p = GetData(); | |
2172 | size = GetWidth() * GetHeight(); | |
2173 | nentries = 0; | |
2174 | ||
2175 | for (unsigned long j = 0; (j < size) && (nentries <= stopafter) ; j++) | |
2176 | { | |
2177 | r = *(p++); | |
2178 | g = *(p++); | |
2179 | b = *(p++); | |
2180 | key = wxImageHistogram::MakeKey(r, g, b); | |
2181 | ||
2182 | if (h.Get(key) == NULL) | |
2183 | { | |
2184 | h.Put(key, &dummy); | |
2185 | nentries++; | |
2186 | } | |
2187 | } | |
2188 | ||
2189 | return nentries; | |
2190 | } | |
2191 | ||
2192 | ||
2193 | unsigned long wxImage::ComputeHistogram( wxImageHistogram &h ) const | |
2194 | { | |
2195 | unsigned char *p = GetData(); | |
2196 | unsigned long nentries = 0; | |
2197 | ||
2198 | h.clear(); | |
2199 | ||
2200 | const unsigned long size = GetWidth() * GetHeight(); | |
2201 | ||
2202 | unsigned char r, g, b; | |
2203 | for ( unsigned long n = 0; n < size; n++ ) | |
2204 | { | |
2205 | r = *p++; | |
2206 | g = *p++; | |
2207 | b = *p++; | |
2208 | ||
2209 | wxImageHistogramEntry& entry = h[wxImageHistogram::MakeKey(r, g, b)]; | |
2210 | ||
2211 | if ( entry.value++ == 0 ) | |
2212 | entry.index = nentries++; | |
2213 | } | |
2214 | ||
2215 | return nentries; | |
2216 | } | |
2217 | ||
2218 | /* | |
2219 | * Rotation code by Carlos Moreno | |
2220 | */ | |
2221 | ||
2222 | // GRG: I've removed wxRotationPoint - we already have wxRealPoint which | |
2223 | // does exactly the same thing. And I also got rid of wxRotationPixel | |
2224 | // bacause of potential problems in architectures where alignment | |
2225 | // is an issue, so I had to rewrite parts of the code. | |
2226 | ||
2227 | static const double gs_Epsilon = 1e-10; | |
2228 | ||
2229 | static inline int wxCint (double x) | |
2230 | { | |
2231 | return (x > 0) ? (int) (x + 0.5) : (int) (x - 0.5); | |
2232 | } | |
2233 | ||
2234 | ||
2235 | // Auxiliary function to rotate a point (x,y) with respect to point p0 | |
2236 | // make it inline and use a straight return to facilitate optimization | |
2237 | // also, the function receives the sine and cosine of the angle to avoid | |
2238 | // repeating the time-consuming calls to these functions -- sin/cos can | |
2239 | // be computed and stored in the calling function. | |
2240 | ||
2241 | inline wxRealPoint rotated_point (const wxRealPoint & p, double cos_angle, double sin_angle, const wxRealPoint & p0) | |
2242 | { | |
2243 | return wxRealPoint (p0.x + (p.x - p0.x) * cos_angle - (p.y - p0.y) * sin_angle, | |
2244 | p0.y + (p.y - p0.y) * cos_angle + (p.x - p0.x) * sin_angle); | |
2245 | } | |
2246 | ||
2247 | inline wxRealPoint rotated_point (double x, double y, double cos_angle, double sin_angle, const wxRealPoint & p0) | |
2248 | { | |
2249 | return rotated_point (wxRealPoint(x,y), cos_angle, sin_angle, p0); | |
2250 | } | |
2251 | ||
2252 | wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool interpolating, wxPoint * offset_after_rotation) const | |
2253 | { | |
2254 | int i; | |
2255 | angle = -angle; // screen coordinates are a mirror image of "real" coordinates | |
2256 | ||
2257 | bool has_alpha = HasAlpha(); | |
2258 | ||
2259 | // Create pointer-based array to accelerate access to wxImage's data | |
2260 | unsigned char ** data = new unsigned char * [GetHeight()]; | |
2261 | data[0] = GetData(); | |
2262 | for (i = 1; i < GetHeight(); i++) | |
2263 | data[i] = data[i - 1] + (3 * GetWidth()); | |
2264 | ||
2265 | // Same for alpha channel | |
2266 | unsigned char ** alpha = NULL; | |
2267 | if (has_alpha) | |
2268 | { | |
2269 | alpha = new unsigned char * [GetHeight()]; | |
2270 | alpha[0] = GetAlpha(); | |
2271 | for (i = 1; i < GetHeight(); i++) | |
2272 | alpha[i] = alpha[i - 1] + GetWidth(); | |
2273 | } | |
2274 | ||
2275 | // precompute coefficients for rotation formula | |
2276 | // (sine and cosine of the angle) | |
2277 | const double cos_angle = cos(angle); | |
2278 | const double sin_angle = sin(angle); | |
2279 | ||
2280 | // Create new Image to store the result | |
2281 | // First, find rectangle that covers the rotated image; to do that, | |
2282 | // rotate the four corners | |
2283 | ||
2284 | const wxRealPoint p0(centre_of_rotation.x, centre_of_rotation.y); | |
2285 | ||
2286 | wxRealPoint p1 = rotated_point (0, 0, cos_angle, sin_angle, p0); | |
2287 | wxRealPoint p2 = rotated_point (0, GetHeight(), cos_angle, sin_angle, p0); | |
2288 | wxRealPoint p3 = rotated_point (GetWidth(), 0, cos_angle, sin_angle, p0); | |
2289 | wxRealPoint p4 = rotated_point (GetWidth(), GetHeight(), cos_angle, sin_angle, p0); | |
2290 | ||
2291 | int x1a = (int) floor (wxMin (wxMin(p1.x, p2.x), wxMin(p3.x, p4.x))); | |
2292 | int y1a = (int) floor (wxMin (wxMin(p1.y, p2.y), wxMin(p3.y, p4.y))); | |
2293 | int x2a = (int) ceil (wxMax (wxMax(p1.x, p2.x), wxMax(p3.x, p4.x))); | |
2294 | int y2a = (int) ceil (wxMax (wxMax(p1.y, p2.y), wxMax(p3.y, p4.y))); | |
2295 | ||
2296 | // Create rotated image | |
2297 | wxImage rotated (x2a - x1a + 1, y2a - y1a + 1, false); | |
2298 | // With alpha channel | |
2299 | if (has_alpha) | |
2300 | rotated.SetAlpha(); | |
2301 | ||
2302 | if (offset_after_rotation != NULL) | |
2303 | { | |
2304 | *offset_after_rotation = wxPoint (x1a, y1a); | |
2305 | } | |
2306 | ||
2307 | // GRG: The rotated (destination) image is always accessed | |
2308 | // sequentially, so there is no need for a pointer-based | |
2309 | // array here (and in fact it would be slower). | |
2310 | // | |
2311 | unsigned char * dst = rotated.GetData(); | |
2312 | ||
2313 | unsigned char * alpha_dst = NULL; | |
2314 | if (has_alpha) | |
2315 | alpha_dst = rotated.GetAlpha(); | |
2316 | ||
2317 | // GRG: if the original image has a mask, use its RGB values | |
2318 | // as the blank pixel, else, fall back to default (black). | |
2319 | // | |
2320 | unsigned char blank_r = 0; | |
2321 | unsigned char blank_g = 0; | |
2322 | unsigned char blank_b = 0; | |
2323 | ||
2324 | if (HasMask()) | |
2325 | { | |
2326 | blank_r = GetMaskRed(); | |
2327 | blank_g = GetMaskGreen(); | |
2328 | blank_b = GetMaskBlue(); | |
2329 | rotated.SetMaskColour( blank_r, blank_g, blank_b ); | |
2330 | } | |
2331 | ||
2332 | // Now, for each point of the rotated image, find where it came from, by | |
2333 | // performing an inverse rotation (a rotation of -angle) and getting the | |
2334 | // pixel at those coordinates | |
2335 | ||
2336 | // GRG: I've taken the (interpolating) test out of the loops, so that | |
2337 | // it is done only once, instead of repeating it for each pixel. | |
2338 | ||
2339 | int x; | |
2340 | if (interpolating) | |
2341 | { | |
2342 | for (int y = 0; y < rotated.GetHeight(); y++) | |
2343 | { | |
2344 | for (x = 0; x < rotated.GetWidth(); x++) | |
2345 | { | |
2346 | wxRealPoint src = rotated_point (x + x1a, y + y1a, cos_angle, -sin_angle, p0); | |
2347 | ||
2348 | if (-0.25 < src.x && src.x < GetWidth() - 0.75 && | |
2349 | -0.25 < src.y && src.y < GetHeight() - 0.75) | |
2350 | { | |
2351 | // interpolate using the 4 enclosing grid-points. Those | |
2352 | // points can be obtained using floor and ceiling of the | |
2353 | // exact coordinates of the point | |
2354 | int x1, y1, x2, y2; | |
2355 | ||
2356 | if (0 < src.x && src.x < GetWidth() - 1) | |
2357 | { | |
2358 | x1 = wxCint(floor(src.x)); | |
2359 | x2 = wxCint(ceil(src.x)); | |
2360 | } | |
2361 | else // else means that x is near one of the borders (0 or width-1) | |
2362 | { | |
2363 | x1 = x2 = wxCint (src.x); | |
2364 | } | |
2365 | ||
2366 | if (0 < src.y && src.y < GetHeight() - 1) | |
2367 | { | |
2368 | y1 = wxCint(floor(src.y)); | |
2369 | y2 = wxCint(ceil(src.y)); | |
2370 | } | |
2371 | else | |
2372 | { | |
2373 | y1 = y2 = wxCint (src.y); | |
2374 | } | |
2375 | ||
2376 | // get four points and the distances (square of the distance, | |
2377 | // for efficiency reasons) for the interpolation formula | |
2378 | ||
2379 | // GRG: Do not calculate the points until they are | |
2380 | // really needed -- this way we can calculate | |
2381 | // just one, instead of four, if d1, d2, d3 | |
2382 | // or d4 are < gs_Epsilon | |
2383 | ||
2384 | const double d1 = (src.x - x1) * (src.x - x1) + (src.y - y1) * (src.y - y1); | |
2385 | const double d2 = (src.x - x2) * (src.x - x2) + (src.y - y1) * (src.y - y1); | |
2386 | const double d3 = (src.x - x2) * (src.x - x2) + (src.y - y2) * (src.y - y2); | |
2387 | const double d4 = (src.x - x1) * (src.x - x1) + (src.y - y2) * (src.y - y2); | |
2388 | ||
2389 | // Now interpolate as a weighted average of the four surrounding | |
2390 | // points, where the weights are the distances to each of those points | |
2391 | ||
2392 | // If the point is exactly at one point of the grid of the source | |
2393 | // image, then don't interpolate -- just assign the pixel | |
2394 | ||
2395 | if (d1 < gs_Epsilon) // d1,d2,d3,d4 are positive -- no need for abs() | |
2396 | { | |
2397 | unsigned char *p = data[y1] + (3 * x1); | |
2398 | *(dst++) = *(p++); | |
2399 | *(dst++) = *(p++); | |
2400 | *(dst++) = *p; | |
2401 | ||
2402 | if (has_alpha) | |
2403 | *(alpha_dst++) = *(alpha[y1] + x1); | |
2404 | } | |
2405 | else if (d2 < gs_Epsilon) | |
2406 | { | |
2407 | unsigned char *p = data[y1] + (3 * x2); | |
2408 | *(dst++) = *(p++); | |
2409 | *(dst++) = *(p++); | |
2410 | *(dst++) = *p; | |
2411 | ||
2412 | if (has_alpha) | |
2413 | *(alpha_dst++) = *(alpha[y1] + x2); | |
2414 | } | |
2415 | else if (d3 < gs_Epsilon) | |
2416 | { | |
2417 | unsigned char *p = data[y2] + (3 * x2); | |
2418 | *(dst++) = *(p++); | |
2419 | *(dst++) = *(p++); | |
2420 | *(dst++) = *p; | |
2421 | ||
2422 | if (has_alpha) | |
2423 | *(alpha_dst++) = *(alpha[y2] + x2); | |
2424 | } | |
2425 | else if (d4 < gs_Epsilon) | |
2426 | { | |
2427 | unsigned char *p = data[y2] + (3 * x1); | |
2428 | *(dst++) = *(p++); | |
2429 | *(dst++) = *(p++); | |
2430 | *(dst++) = *p; | |
2431 | ||
2432 | if (has_alpha) | |
2433 | *(alpha_dst++) = *(alpha[y2] + x1); | |
2434 | } | |
2435 | else | |
2436 | { | |
2437 | // weights for the weighted average are proportional to the inverse of the distance | |
2438 | unsigned char *v1 = data[y1] + (3 * x1); | |
2439 | unsigned char *v2 = data[y1] + (3 * x2); | |
2440 | unsigned char *v3 = data[y2] + (3 * x2); | |
2441 | unsigned char *v4 = data[y2] + (3 * x1); | |
2442 | ||
2443 | const double w1 = 1/d1, w2 = 1/d2, w3 = 1/d3, w4 = 1/d4; | |
2444 | ||
2445 | // GRG: Unrolled. | |
2446 | ||
2447 | *(dst++) = (unsigned char) | |
2448 | ( (w1 * *(v1++) + w2 * *(v2++) + | |
2449 | w3 * *(v3++) + w4 * *(v4++)) / | |
2450 | (w1 + w2 + w3 + w4) ); | |
2451 | *(dst++) = (unsigned char) | |
2452 | ( (w1 * *(v1++) + w2 * *(v2++) + | |
2453 | w3 * *(v3++) + w4 * *(v4++)) / | |
2454 | (w1 + w2 + w3 + w4) ); | |
2455 | *(dst++) = (unsigned char) | |
2456 | ( (w1 * *v1 + w2 * *v2 + | |
2457 | w3 * *v3 + w4 * *v4) / | |
2458 | (w1 + w2 + w3 + w4) ); | |
2459 | ||
2460 | if (has_alpha) | |
2461 | { | |
2462 | v1 = alpha[y1] + (x1); | |
2463 | v2 = alpha[y1] + (x2); | |
2464 | v3 = alpha[y2] + (x2); | |
2465 | v4 = alpha[y2] + (x1); | |
2466 | ||
2467 | *(alpha_dst++) = (unsigned char) | |
2468 | ( (w1 * *v1 + w2 * *v2 + | |
2469 | w3 * *v3 + w4 * *v4) / | |
2470 | (w1 + w2 + w3 + w4) ); | |
2471 | } | |
2472 | } | |
2473 | } | |
2474 | else | |
2475 | { | |
2476 | *(dst++) = blank_r; | |
2477 | *(dst++) = blank_g; | |
2478 | *(dst++) = blank_b; | |
2479 | ||
2480 | if (has_alpha) | |
2481 | *(alpha_dst++) = 0; | |
2482 | } | |
2483 | } | |
2484 | } | |
2485 | } | |
2486 | else // not interpolating | |
2487 | { | |
2488 | for (int y = 0; y < rotated.GetHeight(); y++) | |
2489 | { | |
2490 | for (x = 0; x < rotated.GetWidth(); x++) | |
2491 | { | |
2492 | wxRealPoint src = rotated_point (x + x1a, y + y1a, cos_angle, -sin_angle, p0); | |
2493 | ||
2494 | const int xs = wxCint (src.x); // wxCint rounds to the | |
2495 | const int ys = wxCint (src.y); // closest integer | |
2496 | ||
2497 | if (0 <= xs && xs < GetWidth() && | |
2498 | 0 <= ys && ys < GetHeight()) | |
2499 | { | |
2500 | unsigned char *p = data[ys] + (3 * xs); | |
2501 | *(dst++) = *(p++); | |
2502 | *(dst++) = *(p++); | |
2503 | *(dst++) = *p; | |
2504 | ||
2505 | if (has_alpha) | |
2506 | *(alpha_dst++) = *(alpha[ys] + (xs)); | |
2507 | } | |
2508 | else | |
2509 | { | |
2510 | *(dst++) = blank_r; | |
2511 | *(dst++) = blank_g; | |
2512 | *(dst++) = blank_b; | |
2513 | ||
2514 | if (has_alpha) | |
2515 | *(alpha_dst++) = 255; | |
2516 | } | |
2517 | } | |
2518 | } | |
2519 | } | |
2520 | ||
2521 | delete [] data; | |
2522 | ||
2523 | if (has_alpha) | |
2524 | delete [] alpha; | |
2525 | ||
2526 | return rotated; | |
2527 | } | |
2528 | ||
2529 | ||
2530 | ||
2531 | ||
2532 | ||
2533 | // A module to allow wxImage initialization/cleanup | |
2534 | // without calling these functions from app.cpp or from | |
2535 | // the user's application. | |
2536 | ||
2537 | class wxImageModule: public wxModule | |
2538 | { | |
2539 | DECLARE_DYNAMIC_CLASS(wxImageModule) | |
2540 | public: | |
2541 | wxImageModule() {} | |
2542 | bool OnInit() { wxImage::InitStandardHandlers(); return true; }; | |
2543 | void OnExit() { wxImage::CleanUpHandlers(); }; | |
2544 | }; | |
2545 | ||
2546 | IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule) | |
2547 | ||
2548 | ||
2549 | #endif // wxUSE_IMAGE |