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