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