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