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