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