]>
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 | ||
01111366 | 21 | #include "wx/image.h" |
99c67c77 | 22 | #include "wx/bitmap.h" |
01111366 RR |
23 | #include "wx/debug.h" |
24 | #include "wx/log.h" | |
f6fcbb63 | 25 | #include "wx/app.h" |
dc86cb34 | 26 | #include "wx/filefn.h" |
3d05544e | 27 | #include "wx/wfstream.h" |
b75867a6 | 28 | #include "wx/intl.h" |
a91b47e8 | 29 | #include "wx/module.h" |
01111366 | 30 | |
58a8ab88 JS |
31 | // For memcpy |
32 | #include <string.h> | |
33 | ||
a3ef5bf5 | 34 | #ifdef __SALFORDC__ |
edccf428 | 35 | #undef FAR |
a3ef5bf5 JS |
36 | #endif |
37 | ||
2432b92d | 38 | #ifdef __WXMSW__ |
edccf428 | 39 | #include "wx/msw/private.h" |
2432b92d JS |
40 | #endif |
41 | ||
01111366 RR |
42 | //----------------------------------------------------------------------------- |
43 | // wxImage | |
44 | //----------------------------------------------------------------------------- | |
45 | ||
46 | class wxImageRefData: public wxObjectRefData | |
47 | { | |
c7abc967 | 48 | |
fd0eed64 | 49 | public: |
edccf428 VZ |
50 | wxImageRefData(); |
51 | ~wxImageRefData(); | |
c7abc967 | 52 | |
dbda9e86 JS |
53 | int m_width; |
54 | int m_height; | |
55 | unsigned char *m_data; | |
56 | bool m_hasMask; | |
57 | unsigned char m_maskRed,m_maskGreen,m_maskBlue; | |
58 | bool m_ok; | |
01111366 RR |
59 | }; |
60 | ||
edccf428 | 61 | wxImageRefData::wxImageRefData() |
01111366 | 62 | { |
fd0eed64 RR |
63 | m_width = 0; |
64 | m_height = 0; | |
65 | m_data = (unsigned char*) NULL; | |
66 | m_ok = FALSE; | |
67 | m_maskRed = 0; | |
68 | m_maskGreen = 0; | |
69 | m_maskBlue = 0; | |
70 | m_hasMask = FALSE; | |
01111366 RR |
71 | } |
72 | ||
edccf428 | 73 | wxImageRefData::~wxImageRefData() |
01111366 | 74 | { |
fd0eed64 | 75 | if (m_data) free( m_data ); |
01111366 RR |
76 | } |
77 | ||
78 | wxList wxImage::sm_handlers; | |
79 | ||
80 | //----------------------------------------------------------------------------- | |
81 | ||
82 | #define M_IMGDATA ((wxImageRefData *)m_refData) | |
83 | ||
84 | #if !USE_SHARED_LIBRARIES | |
edccf428 | 85 | IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject) |
01111366 RR |
86 | #endif |
87 | ||
88 | wxImage::wxImage() | |
89 | { | |
90 | } | |
91 | ||
92 | wxImage::wxImage( int width, int height ) | |
93 | { | |
fd0eed64 | 94 | Create( width, height ); |
01111366 RR |
95 | } |
96 | ||
97 | wxImage::wxImage( const wxString& name, long type ) | |
98 | { | |
fd0eed64 | 99 | LoadFile( name, type ); |
01111366 RR |
100 | } |
101 | ||
9e9ee68e VS |
102 | wxImage::wxImage( const wxString& name, const wxString& mimetype ) |
103 | { | |
104 | LoadFile( name, mimetype ); | |
105 | } | |
106 | ||
e02afc7a | 107 | #if wxUSE_STREAMS |
3d05544e JS |
108 | wxImage::wxImage( wxInputStream& stream, long type ) |
109 | { | |
110 | LoadFile( stream, type ); | |
111 | } | |
9e9ee68e VS |
112 | |
113 | wxImage::wxImage( wxInputStream& stream, const wxString& mimetype ) | |
114 | { | |
115 | LoadFile( stream, mimetype ); | |
116 | } | |
e02afc7a | 117 | #endif // wxUSE_STREAMS |
3d05544e | 118 | |
4698648f VZ |
119 | wxImage::wxImage( const wxImage& image ) |
120 | { | |
121 | Ref(image); | |
01111366 RR |
122 | } |
123 | ||
4698648f VZ |
124 | wxImage::wxImage( const wxImage* image ) |
125 | { | |
126 | if (image) Ref(*image); | |
01111366 RR |
127 | } |
128 | ||
129 | void wxImage::Create( int width, int height ) | |
130 | { | |
fd0eed64 | 131 | m_refData = new wxImageRefData(); |
c7abc967 | 132 | |
fd0eed64 RR |
133 | M_IMGDATA->m_data = (unsigned char *) malloc( width*height*3 ); |
134 | if (M_IMGDATA->m_data) | |
135 | { | |
136 | for (int l = 0; l < width*height*3; l++) M_IMGDATA->m_data[l] = 0; | |
c7abc967 | 137 | |
fd0eed64 RR |
138 | M_IMGDATA->m_width = width; |
139 | M_IMGDATA->m_height = height; | |
140 | M_IMGDATA->m_ok = TRUE; | |
141 | } | |
142 | else | |
143 | { | |
144 | UnRef(); | |
145 | } | |
01111366 RR |
146 | } |
147 | ||
148 | void wxImage::Destroy() | |
149 | { | |
fd0eed64 | 150 | UnRef(); |
01111366 RR |
151 | } |
152 | ||
ce9a75d2 | 153 | wxImage wxImage::Scale( int width, int height ) const |
4bc67cc5 RR |
154 | { |
155 | wxImage image; | |
c7abc967 | 156 | |
50920146 | 157 | wxCHECK_MSG( Ok(), image, _T("invalid image") ); |
c7abc967 | 158 | |
50920146 | 159 | wxCHECK_MSG( (width > 0) && (height > 0), image, _T("invalid image size") ); |
c7abc967 | 160 | |
4bc67cc5 | 161 | image.Create( width, height ); |
c7abc967 | 162 | |
4bc67cc5 | 163 | char unsigned *data = image.GetData(); |
c7abc967 | 164 | |
50920146 | 165 | wxCHECK_MSG( data, image, _T("unable to create image") ); |
c7abc967 | 166 | |
4bc67cc5 RR |
167 | if (M_IMGDATA->m_hasMask) |
168 | image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); | |
c7abc967 | 169 | |
6e13c196 RR |
170 | long old_height = M_IMGDATA->m_height; |
171 | long old_width = M_IMGDATA->m_width; | |
c7abc967 | 172 | |
6e13c196 RR |
173 | char unsigned *source_data = M_IMGDATA->m_data; |
174 | char unsigned *target_data = data; | |
c7abc967 | 175 | |
6e13c196 | 176 | for (long j = 0; j < height; j++) |
4bc67cc5 | 177 | { |
6e13c196 | 178 | long y_offset = (j * old_height / height) * old_width; |
c7abc967 | 179 | |
6e13c196 | 180 | for (long i = 0; i < width; i++) |
4698648f | 181 | { |
c7abc967 VZ |
182 | memcpy( target_data, |
183 | source_data + 3*(y_offset + ((i * old_width )/ width)), | |
dbda9e86 | 184 | 3 ); |
6e13c196 | 185 | target_data += 3; |
4698648f | 186 | } |
4bc67cc5 | 187 | } |
c7abc967 | 188 | |
4bc67cc5 RR |
189 | return image; |
190 | } | |
4698648f | 191 | |
7b2471a0 SB |
192 | wxImage wxImage::GetSubImage( const wxRect &rect ) const |
193 | { | |
194 | wxImage image; | |
195 | ||
196 | wxCHECK_MSG( Ok(), image, _T("invalid image") ); | |
197 | ||
198 | wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) && (rect.GetRight()<=GetWidth()) && (rect.GetBottom()<=GetHeight()) | |
199 | , image, _T("invalid subimage size") ); | |
200 | ||
201 | int subwidth=rect.GetWidth(); | |
202 | const int subheight=rect.GetHeight(); | |
203 | ||
204 | image.Create( subwidth, subheight ); | |
205 | ||
206 | char unsigned *subdata = image.GetData(), *data=GetData(); | |
207 | ||
208 | wxCHECK_MSG( subdata, image, _T("unable to create image") ); | |
209 | ||
210 | if (M_IMGDATA->m_hasMask) | |
211 | image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); | |
212 | ||
213 | const int subleft=3*rect.GetLeft(); | |
214 | const int width=3*GetWidth(); | |
215 | subwidth*=3; | |
216 | ||
217 | data+=rect.GetTop()*width+subleft; | |
218 | ||
219 | for (long j = 0; j < subheight; ++j) | |
220 | { | |
221 | memcpy( subdata, data, subwidth); | |
222 | subdata+=subwidth; | |
223 | data+=width; | |
224 | } | |
225 | ||
226 | return image; | |
227 | } | |
228 | ||
ef539066 RR |
229 | void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b ) |
230 | { | |
50920146 | 231 | wxCHECK_RET( Ok(), _T("invalid image") ); |
c7abc967 | 232 | |
ef539066 RR |
233 | int w = M_IMGDATA->m_width; |
234 | int h = M_IMGDATA->m_height; | |
c7abc967 | 235 | |
50920146 | 236 | wxCHECK_RET( (x>=0) && (y>=0) && (x<w) && (y<h), _T("invalid image index") ); |
c7abc967 | 237 | |
ef539066 | 238 | long pos = (y * w + x) * 3; |
c7abc967 | 239 | |
ef539066 RR |
240 | M_IMGDATA->m_data[ pos ] = r; |
241 | M_IMGDATA->m_data[ pos+1 ] = g; | |
242 | M_IMGDATA->m_data[ pos+2 ] = b; | |
243 | } | |
244 | ||
245 | unsigned char wxImage::GetRed( int x, int y ) | |
246 | { | |
50920146 | 247 | wxCHECK_MSG( Ok(), 0, _T("invalid image") ); |
c7abc967 | 248 | |
ef539066 RR |
249 | int w = M_IMGDATA->m_width; |
250 | int h = M_IMGDATA->m_height; | |
c7abc967 | 251 | |
50920146 | 252 | wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, _T("invalid image index") ); |
c7abc967 | 253 | |
ef539066 | 254 | long pos = (y * w + x) * 3; |
c7abc967 | 255 | |
ef539066 RR |
256 | return M_IMGDATA->m_data[pos]; |
257 | } | |
258 | ||
259 | unsigned char wxImage::GetGreen( int x, int y ) | |
260 | { | |
50920146 | 261 | wxCHECK_MSG( Ok(), 0, _T("invalid image") ); |
c7abc967 | 262 | |
ef539066 RR |
263 | int w = M_IMGDATA->m_width; |
264 | int h = M_IMGDATA->m_height; | |
c7abc967 | 265 | |
50920146 | 266 | wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, _T("invalid image index") ); |
c7abc967 | 267 | |
ef539066 | 268 | long pos = (y * w + x) * 3; |
c7abc967 | 269 | |
ef539066 RR |
270 | return M_IMGDATA->m_data[pos+1]; |
271 | } | |
272 | ||
273 | unsigned char wxImage::GetBlue( int x, int y ) | |
274 | { | |
50920146 | 275 | wxCHECK_MSG( Ok(), 0, _T("invalid image") ); |
c7abc967 | 276 | |
ef539066 RR |
277 | int w = M_IMGDATA->m_width; |
278 | int h = M_IMGDATA->m_height; | |
c7abc967 | 279 | |
50920146 | 280 | wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, _T("invalid image index") ); |
c7abc967 | 281 | |
ef539066 | 282 | long pos = (y * w + x) * 3; |
c7abc967 | 283 | |
ef539066 RR |
284 | return M_IMGDATA->m_data[pos+2]; |
285 | } | |
4698648f VZ |
286 | |
287 | bool wxImage::Ok() const | |
288 | { | |
289 | return (M_IMGDATA && M_IMGDATA->m_ok); | |
01111366 RR |
290 | } |
291 | ||
292 | char unsigned *wxImage::GetData() const | |
293 | { | |
50920146 | 294 | wxCHECK_MSG( Ok(), (char unsigned *)NULL, _T("invalid image") ); |
c7abc967 | 295 | |
fd0eed64 | 296 | return M_IMGDATA->m_data; |
01111366 RR |
297 | } |
298 | ||
58a8ab88 | 299 | void wxImage::SetData( char unsigned *data ) |
01111366 | 300 | { |
50920146 | 301 | wxCHECK_RET( Ok(), _T("invalid image") ); |
58a8ab88 | 302 | |
ed58dbea RR |
303 | wxImageRefData *newRefData = new wxImageRefData(); |
304 | ||
305 | newRefData->m_width = M_IMGDATA->m_width; | |
306 | newRefData->m_height = M_IMGDATA->m_height; | |
307 | newRefData->m_data = data; | |
308 | newRefData->m_ok = TRUE; | |
309 | newRefData->m_maskRed = M_IMGDATA->m_maskRed; | |
310 | newRefData->m_maskGreen = M_IMGDATA->m_maskGreen; | |
311 | newRefData->m_maskBlue = M_IMGDATA->m_maskBlue; | |
312 | newRefData->m_hasMask = M_IMGDATA->m_hasMask; | |
313 | ||
314 | UnRef(); | |
315 | ||
316 | m_refData = newRefData; | |
01111366 RR |
317 | } |
318 | ||
319 | void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b ) | |
320 | { | |
50920146 | 321 | wxCHECK_RET( Ok(), _T("invalid image") ); |
c7abc967 | 322 | |
fd0eed64 RR |
323 | M_IMGDATA->m_maskRed = r; |
324 | M_IMGDATA->m_maskGreen = g; | |
325 | M_IMGDATA->m_maskBlue = b; | |
326 | M_IMGDATA->m_hasMask = TRUE; | |
01111366 RR |
327 | } |
328 | ||
329 | unsigned char wxImage::GetMaskRed() const | |
330 | { | |
50920146 | 331 | wxCHECK_MSG( Ok(), 0, _T("invalid image") ); |
c7abc967 | 332 | |
fd0eed64 | 333 | return M_IMGDATA->m_maskRed; |
01111366 RR |
334 | } |
335 | ||
336 | unsigned char wxImage::GetMaskGreen() const | |
337 | { | |
50920146 | 338 | wxCHECK_MSG( Ok(), 0, _T("invalid image") ); |
c7abc967 | 339 | |
fd0eed64 | 340 | return M_IMGDATA->m_maskGreen; |
01111366 RR |
341 | } |
342 | ||
343 | unsigned char wxImage::GetMaskBlue() const | |
344 | { | |
50920146 | 345 | wxCHECK_MSG( Ok(), 0, _T("invalid image") ); |
c7abc967 | 346 | |
fd0eed64 | 347 | return M_IMGDATA->m_maskBlue; |
01111366 | 348 | } |
4698648f | 349 | |
01111366 RR |
350 | void wxImage::SetMask( bool mask ) |
351 | { | |
50920146 | 352 | wxCHECK_RET( Ok(), _T("invalid image") ); |
c7abc967 | 353 | |
fd0eed64 | 354 | M_IMGDATA->m_hasMask = mask; |
01111366 RR |
355 | } |
356 | ||
357 | bool wxImage::HasMask() const | |
358 | { | |
50920146 | 359 | wxCHECK_MSG( Ok(), FALSE, _T("invalid image") ); |
c7abc967 | 360 | |
fd0eed64 | 361 | return M_IMGDATA->m_hasMask; |
01111366 RR |
362 | } |
363 | ||
4698648f VZ |
364 | int wxImage::GetWidth() const |
365 | { | |
50920146 | 366 | wxCHECK_MSG( Ok(), 0, _T("invalid image") ); |
c7abc967 | 367 | |
4698648f | 368 | return M_IMGDATA->m_width; |
01111366 RR |
369 | } |
370 | ||
4698648f VZ |
371 | int wxImage::GetHeight() const |
372 | { | |
50920146 | 373 | wxCHECK_MSG( Ok(), 0, _T("invalid image") ); |
c7abc967 | 374 | |
4698648f | 375 | return M_IMGDATA->m_height; |
01111366 RR |
376 | } |
377 | ||
378 | bool wxImage::LoadFile( const wxString& filename, long type ) | |
379 | { | |
e02afc7a | 380 | #if wxUSE_STREAMS |
3d05544e | 381 | if (wxFileExists(filename)) |
fd0eed64 | 382 | { |
3d05544e JS |
383 | wxFileInputStream stream(filename); |
384 | return LoadFile(stream, type); | |
385 | } | |
9e9ee68e VS |
386 | |
387 | else { | |
50920146 | 388 | wxLogError( _T("Can't load image from file '%s': file does not exist."), filename.c_str() ); |
9e9ee68e VS |
389 | |
390 | return FALSE; | |
391 | } | |
392 | #else // !wxUSE_STREAMS | |
393 | return FALSE; | |
394 | #endif // wxUSE_STREAMS | |
395 | } | |
396 | ||
397 | bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype ) | |
398 | { | |
399 | #if wxUSE_STREAMS | |
400 | if (wxFileExists(filename)) | |
401 | { | |
402 | wxFileInputStream stream(filename); | |
403 | return LoadFile(stream, mimetype); | |
404 | } | |
c7abc967 | 405 | |
3d05544e | 406 | else { |
50920146 | 407 | wxLogError( _T("Can't load image from file '%s': file does not exist."), filename.c_str() ); |
c7abc967 | 408 | |
fd0eed64 RR |
409 | return FALSE; |
410 | } | |
e02afc7a | 411 | #else // !wxUSE_STREAMS |
dbda9e86 | 412 | return FALSE; |
e02afc7a | 413 | #endif // wxUSE_STREAMS |
1ccbb61a VZ |
414 | } |
415 | ||
416 | bool wxImage::SaveFile( const wxString& filename, int type ) | |
417 | { | |
e02afc7a | 418 | #if wxUSE_STREAMS |
1ccbb61a | 419 | wxFileOutputStream stream(filename); |
9e9ee68e | 420 | |
1ccbb61a VZ |
421 | if ( stream.LastError() == wxStream_NOERROR ) |
422 | return SaveFile(stream, type); | |
423 | else | |
e02afc7a | 424 | #endif // wxUSE_STREAMS |
1ccbb61a | 425 | return FALSE; |
3d05544e | 426 | } |
01111366 | 427 | |
9e9ee68e VS |
428 | bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) |
429 | { | |
430 | #if wxUSE_STREAMS | |
431 | wxFileOutputStream stream(filename); | |
c7abc967 | 432 | |
9e9ee68e VS |
433 | if ( stream.LastError() == wxStream_NOERROR ) |
434 | return SaveFile(stream, mimetype); | |
435 | else | |
436 | #endif // wxUSE_STREAMS | |
437 | return FALSE; | |
438 | } | |
439 | ||
e02afc7a | 440 | #if wxUSE_STREAMS |
deb2fec0 | 441 | |
3d05544e JS |
442 | bool wxImage::LoadFile( wxInputStream& stream, long type ) |
443 | { | |
444 | UnRef(); | |
c7abc967 | 445 | |
fd0eed64 | 446 | m_refData = new wxImageRefData; |
c7abc967 | 447 | |
deb2fec0 SB |
448 | wxImageHandler *handler; |
449 | ||
450 | if (type==wxBITMAP_TYPE_ANY) | |
451 | { | |
deb2fec0 | 452 | wxList &list=GetHandlers(); |
deb2fec0 SB |
453 | |
454 | for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() ) | |
7b2471a0 SB |
455 | { |
456 | handler=(wxImageHandler*)node->GetData(); | |
0828c087 VS |
457 | if (handler->CanRead( stream )) |
458 | return handler->LoadFile( this, stream ); | |
7b2471a0 | 459 | |
deb2fec0 SB |
460 | } |
461 | ||
7b2471a0 | 462 | wxLogWarning( _T("No handler found for this image.") ); |
deb2fec0 SB |
463 | return FALSE; |
464 | } | |
465 | ||
466 | handler = FindHandler(type); | |
c7abc967 | 467 | |
4698648f | 468 | if (handler == NULL) |
fd0eed64 | 469 | { |
50920146 | 470 | wxLogWarning( _T("No image handler for type %d defined."), type ); |
c7abc967 | 471 | |
fd0eed64 RR |
472 | return FALSE; |
473 | } | |
c7abc967 | 474 | |
3d05544e | 475 | return handler->LoadFile( this, stream ); |
01111366 RR |
476 | } |
477 | ||
9e9ee68e VS |
478 | bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype ) |
479 | { | |
480 | UnRef(); | |
481 | ||
482 | m_refData = new wxImageRefData; | |
483 | ||
484 | wxImageHandler *handler = FindHandlerMime(mimetype); | |
485 | ||
486 | if (handler == NULL) | |
487 | { | |
50920146 | 488 | wxLogWarning( _T("No image handler for type %s defined."), mimetype.GetData() ); |
9e9ee68e VS |
489 | |
490 | return FALSE; | |
491 | } | |
492 | ||
493 | return handler->LoadFile( this, stream ); | |
494 | } | |
495 | ||
3d05544e | 496 | bool wxImage::SaveFile( wxOutputStream& stream, int type ) |
01111366 | 497 | { |
50920146 | 498 | wxCHECK_MSG( Ok(), FALSE, _T("invalid image") ); |
c7abc967 | 499 | |
fd0eed64 | 500 | wxImageHandler *handler = FindHandler(type); |
c7abc967 | 501 | |
4698648f | 502 | if (handler == NULL) |
fd0eed64 | 503 | { |
50920146 | 504 | wxLogWarning( _T("No image handler for type %d defined."), type ); |
9e9ee68e VS |
505 | |
506 | return FALSE; | |
507 | } | |
508 | ||
509 | return handler->SaveFile( this, stream ); | |
510 | } | |
511 | ||
512 | bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) | |
513 | { | |
50920146 | 514 | wxCHECK_MSG( Ok(), FALSE, _T("invalid image") ); |
c7abc967 | 515 | |
9e9ee68e | 516 | wxImageHandler *handler = FindHandlerMime(mimetype); |
c7abc967 | 517 | |
9e9ee68e VS |
518 | if (handler == NULL) |
519 | { | |
50920146 | 520 | wxLogWarning( _T("No image handler for type %s defined."), mimetype.GetData() ); |
c7abc967 | 521 | |
dbda9e86 | 522 | return FALSE; |
fd0eed64 | 523 | } |
c7abc967 | 524 | |
3d05544e | 525 | return handler->SaveFile( this, stream ); |
01111366 | 526 | } |
e02afc7a | 527 | #endif // wxUSE_STREAMS |
01111366 RR |
528 | |
529 | void wxImage::AddHandler( wxImageHandler *handler ) | |
530 | { | |
4698648f VZ |
531 | // make sure that the memory will be freed at the program end |
532 | sm_handlers.DeleteContents(TRUE); | |
c7abc967 | 533 | |
01111366 RR |
534 | sm_handlers.Append( handler ); |
535 | } | |
536 | ||
537 | void wxImage::InsertHandler( wxImageHandler *handler ) | |
538 | { | |
4698648f VZ |
539 | // make sure that the memory will be freed at the program end |
540 | sm_handlers.DeleteContents(TRUE); | |
c7abc967 | 541 | |
01111366 RR |
542 | sm_handlers.Insert( handler ); |
543 | } | |
544 | ||
545 | bool wxImage::RemoveHandler( const wxString& name ) | |
546 | { | |
fd0eed64 RR |
547 | wxImageHandler *handler = FindHandler(name); |
548 | if (handler) | |
549 | { | |
550 | sm_handlers.DeleteObject(handler); | |
551 | return TRUE; | |
552 | } | |
553 | else | |
554 | return FALSE; | |
01111366 RR |
555 | } |
556 | ||
557 | wxImageHandler *wxImage::FindHandler( const wxString& name ) | |
558 | { | |
fd0eed64 RR |
559 | wxNode *node = sm_handlers.First(); |
560 | while (node) | |
561 | { | |
562 | wxImageHandler *handler = (wxImageHandler*)node->Data(); | |
ce3ed50d | 563 | if (handler->GetName().Cmp(name) == 0) return handler; |
c7abc967 | 564 | |
fd0eed64 RR |
565 | node = node->Next(); |
566 | } | |
567 | return (wxImageHandler *)NULL; | |
01111366 RR |
568 | } |
569 | ||
570 | wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType ) | |
571 | { | |
fd0eed64 RR |
572 | wxNode *node = sm_handlers.First(); |
573 | while (node) | |
574 | { | |
575 | wxImageHandler *handler = (wxImageHandler*)node->Data(); | |
ce3ed50d | 576 | if ( (handler->GetExtension().Cmp(extension) == 0) && |
fd0eed64 | 577 | (bitmapType == -1 || handler->GetType() == bitmapType) ) |
dbda9e86 | 578 | return handler; |
fd0eed64 RR |
579 | node = node->Next(); |
580 | } | |
581 | return (wxImageHandler*)NULL; | |
01111366 RR |
582 | } |
583 | ||
584 | wxImageHandler *wxImage::FindHandler( long bitmapType ) | |
585 | { | |
fd0eed64 RR |
586 | wxNode *node = sm_handlers.First(); |
587 | while (node) | |
588 | { | |
589 | wxImageHandler *handler = (wxImageHandler *)node->Data(); | |
590 | if (handler->GetType() == bitmapType) return handler; | |
591 | node = node->Next(); | |
592 | } | |
593 | return NULL; | |
594 | } | |
595 | ||
9e9ee68e VS |
596 | wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype ) |
597 | { | |
598 | wxNode *node = sm_handlers.First(); | |
599 | while (node) | |
600 | { | |
601 | wxImageHandler *handler = (wxImageHandler *)node->Data(); | |
602 | if (handler->GetMimeType().IsSameAs(mimetype, FALSE)) return handler; | |
603 | node = node->Next(); | |
604 | } | |
605 | return NULL; | |
606 | } | |
607 | ||
fd0eed64 RR |
608 | void wxImage::InitStandardHandlers() |
609 | { | |
deb2fec0 | 610 | AddHandler( new wxBMPHandler ); |
01111366 RR |
611 | } |
612 | ||
613 | void wxImage::CleanUpHandlers() | |
614 | { | |
fd0eed64 RR |
615 | wxNode *node = sm_handlers.First(); |
616 | while (node) | |
617 | { | |
618 | wxImageHandler *handler = (wxImageHandler *)node->Data(); | |
619 | wxNode *next = node->Next(); | |
620 | delete handler; | |
621 | delete node; | |
622 | node = next; | |
623 | } | |
01111366 RR |
624 | } |
625 | ||
626 | //----------------------------------------------------------------------------- | |
627 | // wxImageHandler | |
628 | //----------------------------------------------------------------------------- | |
629 | ||
630 | #if !USE_SHARED_LIBRARIES | |
631 | IMPLEMENT_DYNAMIC_CLASS(wxImageHandler,wxObject) | |
632 | #endif | |
633 | ||
e02afc7a | 634 | #if wxUSE_STREAMS |
deb2fec0 | 635 | bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose) ) |
01111366 | 636 | { |
fd0eed64 | 637 | return FALSE; |
01111366 RR |
638 | } |
639 | ||
deb2fec0 | 640 | bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) ) |
01111366 | 641 | { |
fd0eed64 | 642 | return FALSE; |
01111366 | 643 | } |
0828c087 VS |
644 | |
645 | bool wxImageHandler::CanRead( wxInputStream& stream ) | |
646 | { | |
647 | return FALSE; | |
648 | } | |
649 | ||
650 | bool wxImageHandler::CanRead( const wxString& name ) | |
651 | { | |
652 | #if wxUSE_STREAMS | |
653 | if (wxFileExists(name)) | |
654 | { | |
655 | wxFileInputStream stream(name); | |
656 | return CanRead(stream); | |
657 | } | |
658 | ||
659 | else { | |
660 | wxLogError( _T("Can't check image format of file '%s': file does not exist."), name.c_str() ); | |
661 | ||
662 | return FALSE; | |
663 | } | |
664 | #else // !wxUSE_STREAMS | |
665 | return FALSE; | |
666 | #endif // wxUSE_STREAMS | |
667 | } | |
668 | ||
669 | ||
670 | ||
e02afc7a | 671 | #endif // wxUSE_STREAMS |
01111366 | 672 | |
01111366 | 673 | //----------------------------------------------------------------------------- |
ce4169a4 | 674 | // MSW conversion routines |
01111366 RR |
675 | //----------------------------------------------------------------------------- |
676 | ||
e3554471 JS |
677 | #ifdef __WXMSW__ |
678 | ||
679 | wxBitmap wxImage::ConvertToBitmap() const | |
680 | { | |
0655ad29 VZ |
681 | if ( !Ok() ) |
682 | return wxNullBitmap; | |
683 | ||
dbda9e86 | 684 | // sizeLimit is the MS upper limit for the DIB size |
c7abc967 VZ |
685 | int sizeLimit = 1024*768*3; |
686 | ||
dbda9e86 | 687 | // width and height of the device-dependent bitmap |
bba6f3bd UA |
688 | int width = GetWidth(); |
689 | int bmpHeight = GetHeight(); | |
c7abc967 | 690 | |
dbda9e86 | 691 | // calc the number of bytes per scanline and padding |
bba6f3bd UA |
692 | int bytePerLine = width*3; |
693 | int sizeDWORD = sizeof( DWORD ); | |
bae41ce1 | 694 | int lineBoundary = bytePerLine % sizeDWORD; |
bba6f3bd | 695 | int padding = 0; |
bae41ce1 | 696 | if( lineBoundary > 0 ) |
bba6f3bd | 697 | { |
bae41ce1 | 698 | padding = sizeDWORD - lineBoundary; |
bba6f3bd UA |
699 | bytePerLine += padding; |
700 | } | |
dbda9e86 | 701 | // calc the number of DIBs and heights of DIBs |
bba6f3bd UA |
702 | int numDIB = 1; |
703 | int hRemain = 0; | |
704 | int height = sizeLimit/bytePerLine; | |
705 | if( height >= bmpHeight ) | |
c7abc967 | 706 | height = bmpHeight; |
bba6f3bd UA |
707 | else |
708 | { | |
bae41ce1 UA |
709 | numDIB = bmpHeight / height; |
710 | hRemain = bmpHeight % height; | |
dbda9e86 | 711 | if( hRemain >0 ) numDIB++; |
bba6f3bd | 712 | } |
c7abc967 | 713 | |
dbda9e86 | 714 | // set bitmap parameters |
bba6f3bd | 715 | wxBitmap bitmap; |
50920146 | 716 | wxCHECK_MSG( Ok(), bitmap, _T("invalid image") ); |
bba6f3bd UA |
717 | bitmap.SetWidth( width ); |
718 | bitmap.SetHeight( bmpHeight ); | |
719 | bitmap.SetDepth( wxDisplayDepth() ); | |
c7abc967 | 720 | |
dbda9e86 | 721 | // create a DIB header |
bba6f3bd UA |
722 | int headersize = sizeof(BITMAPINFOHEADER); |
723 | LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize ); | |
50920146 | 724 | wxCHECK_MSG( lpDIBh, bitmap, _T("could not allocate memory for DIB header") ); |
dbda9e86 | 725 | // Fill in the DIB header |
bba6f3bd UA |
726 | lpDIBh->bmiHeader.biSize = headersize; |
727 | lpDIBh->bmiHeader.biWidth = (DWORD)width; | |
728 | lpDIBh->bmiHeader.biHeight = (DWORD)(-height); | |
729 | lpDIBh->bmiHeader.biSizeImage = bytePerLine*height; | |
dbda9e86 JS |
730 | // the general formula for biSizeImage: |
731 | // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height; | |
bba6f3bd UA |
732 | lpDIBh->bmiHeader.biPlanes = 1; |
733 | lpDIBh->bmiHeader.biBitCount = 24; | |
734 | lpDIBh->bmiHeader.biCompression = BI_RGB; | |
735 | lpDIBh->bmiHeader.biClrUsed = 0; | |
dbda9e86 | 736 | // These seem not really needed for our purpose here. |
bba6f3bd UA |
737 | lpDIBh->bmiHeader.biClrImportant = 0; |
738 | lpDIBh->bmiHeader.biXPelsPerMeter = 0; | |
739 | lpDIBh->bmiHeader.biYPelsPerMeter = 0; | |
dbda9e86 | 740 | // memory for DIB data |
bba6f3bd UA |
741 | unsigned char *lpBits; |
742 | lpBits = (unsigned char *)malloc( lpDIBh->bmiHeader.biSizeImage ); | |
743 | if( !lpBits ) | |
744 | { | |
50920146 | 745 | wxFAIL_MSG( _T("could not allocate memory for DIB") ); |
bba6f3bd UA |
746 | free( lpDIBh ); |
747 | return bitmap; | |
748 | } | |
c7abc967 | 749 | |
dbda9e86 | 750 | // create and set the device-dependent bitmap |
bba6f3bd UA |
751 | HDC hdc = ::GetDC(NULL); |
752 | HDC memdc = ::CreateCompatibleDC( hdc ); | |
753 | HBITMAP hbitmap; | |
754 | hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight ); | |
c7abc967 VZ |
755 | ::SelectObject( memdc, hbitmap); |
756 | ||
dbda9e86 | 757 | // copy image data into DIB data and then into DDB (in a loop) |
bba6f3bd UA |
758 | unsigned char *data = GetData(); |
759 | int i, j, n; | |
760 | int origin = 0; | |
761 | unsigned char *ptdata = data; | |
762 | unsigned char *ptbits; | |
c7abc967 | 763 | |
bba6f3bd UA |
764 | for( n=0; n<numDIB; n++ ) |
765 | { | |
dbda9e86 JS |
766 | if( numDIB > 1 && n == numDIB-1 && hRemain > 0 ) |
767 | { | |
768 | // redefine height and size of the (possibly) last smaller DIB | |
769 | // memory is not reallocated | |
c7abc967 | 770 | height = hRemain; |
bba6f3bd UA |
771 | lpDIBh->bmiHeader.biHeight = (DWORD)(-height); |
772 | lpDIBh->bmiHeader.biSizeImage = bytePerLine*height; | |
dbda9e86 | 773 | } |
bba6f3bd | 774 | ptbits = lpBits; |
c7abc967 | 775 | |
bba6f3bd UA |
776 | for( j=0; j<height; j++ ) |
777 | { | |
778 | for( i=0; i<width; i++ ) | |
779 | { | |
780 | *(ptbits++) = *(ptdata+2); | |
781 | *(ptbits++) = *(ptdata+1); | |
782 | *(ptbits++) = *(ptdata ); | |
783 | ptdata += 3; | |
dbda9e86 JS |
784 | } |
785 | for( i=0; i< padding; i++ ) *(ptbits++) = 0; | |
bba6f3bd UA |
786 | } |
787 | ::StretchDIBits( memdc, 0, origin, width, height,\ | |
dbda9e86 JS |
788 | 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY); |
789 | origin += height; | |
790 | // if numDIB = 1, lines below can also be used | |
791 | // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS ); | |
792 | // The above line is equivalent to the following two lines. | |
793 | // hbitmap = ::CreateCompatibleBitmap( hdc, width, height ); | |
794 | // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS); | |
795 | // or the following lines | |
796 | // hbitmap = ::CreateCompatibleBitmap( hdc, width, height ); | |
797 | // HDC memdc = ::CreateCompatibleDC( hdc ); | |
c7abc967 | 798 | // ::SelectObject( memdc, hbitmap); |
dbda9e86 JS |
799 | // ::SetDIBitsToDevice( memdc, 0, 0, width, height, |
800 | // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS); | |
c7abc967 VZ |
801 | // ::SelectObject( memdc, 0 ); |
802 | // ::DeleteDC( memdc ); | |
e3554471 | 803 | } |
bba6f3bd | 804 | bitmap.SetHBITMAP( (WXHBITMAP) hbitmap ); |
c7abc967 | 805 | |
dbda9e86 | 806 | // similarly, created an mono-bitmap for the possible mask |
bba6f3bd UA |
807 | if( HasMask() ) |
808 | { | |
809 | hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL ); | |
c7abc967 VZ |
810 | ::SelectObject( memdc, hbitmap); |
811 | if( numDIB == 1 ) height = bmpHeight; | |
bba6f3bd UA |
812 | else height = sizeLimit/bytePerLine; |
813 | lpDIBh->bmiHeader.biHeight = (DWORD)(-height); | |
814 | lpDIBh->bmiHeader.biSizeImage = bytePerLine*height; | |
815 | origin = 0; | |
816 | unsigned char r = GetMaskRed(); | |
817 | unsigned char g = GetMaskGreen(); | |
818 | unsigned char b = GetMaskBlue(); | |
819 | unsigned char zero = 0, one = 255; | |
820 | ptdata = data; | |
821 | for( n=0; n<numDIB; n++ ) | |
822 | { | |
823 | if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 ) | |
824 | { | |
dbda9e86 JS |
825 | // redefine height and size of the (possibly) last smaller DIB |
826 | // memory is not reallocated | |
c7abc967 | 827 | height = hRemain; |
bba6f3bd UA |
828 | lpDIBh->bmiHeader.biHeight = (DWORD)(-height); |
829 | lpDIBh->bmiHeader.biSizeImage = bytePerLine*height; | |
dbda9e86 | 830 | } |
bba6f3bd UA |
831 | ptbits = lpBits; |
832 | for( int j=0; j<height; j++ ) | |
833 | { | |
dbda9e86 | 834 | for(i=0; i<width; i++ ) |
bba6f3bd UA |
835 | { |
836 | if( (*(ptdata++)!=r) | (*(ptdata++)!=g) | (*(ptdata++)!=b) ) | |
837 | { | |
838 | *(ptbits++) = one; | |
839 | *(ptbits++) = one; | |
840 | *(ptbits++) = one; | |
841 | } | |
842 | else | |
843 | { | |
844 | *(ptbits++) = zero; | |
845 | *(ptbits++) = zero; | |
846 | *(ptbits++) = zero; | |
847 | } | |
848 | } | |
dbda9e86 | 849 | for( i=0; i< padding; i++ ) *(ptbits++) = zero; |
bba6f3bd UA |
850 | } |
851 | ::StretchDIBits( memdc, 0, origin, width, height,\ | |
dbda9e86 JS |
852 | 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY); |
853 | origin += height; | |
854 | } | |
855 | // create a wxMask object | |
bba6f3bd UA |
856 | wxMask *mask = new wxMask(); |
857 | mask->SetMaskBitmap( (WXHBITMAP) hbitmap ); | |
858 | bitmap.SetMask( mask ); | |
dbda9e86 JS |
859 | // It will be deleted when the wxBitmap object is deleted (as of 01/1999) |
860 | /* The following can also be used but is slow to run | |
bba6f3bd UA |
861 | wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue()); |
862 | wxMask *mask = new wxMask( bitmap, colour ); | |
863 | bitmap.SetMask( mask ); | |
dbda9e86 | 864 | */ |
bba6f3bd | 865 | } |
c7abc967 VZ |
866 | |
867 | // free allocated resources | |
868 | ::SelectObject( memdc, 0 ); | |
869 | ::DeleteDC( memdc ); | |
870 | ::ReleaseDC(NULL, hdc); | |
bba6f3bd UA |
871 | free(lpDIBh); |
872 | free(lpBits); | |
c7abc967 | 873 | |
dbda9e86 | 874 | // check the wxBitmap object |
bba6f3bd UA |
875 | if( bitmap.GetHBITMAP() ) |
876 | bitmap.SetOk( TRUE ); | |
877 | else | |
878 | bitmap.SetOk( FALSE ); | |
c7abc967 | 879 | |
bba6f3bd | 880 | return bitmap; |
e3554471 JS |
881 | } |
882 | ||
e3554471 JS |
883 | wxImage::wxImage( const wxBitmap &bitmap ) |
884 | { | |
dbda9e86 | 885 | // check the bitmap |
bba6f3bd UA |
886 | if( !bitmap.Ok() ) |
887 | { | |
50920146 | 888 | wxFAIL_MSG( _T("invalid bitmap") ); |
bba6f3bd UA |
889 | return; |
890 | } | |
c7abc967 | 891 | |
dbda9e86 | 892 | // create an wxImage object |
bba6f3bd UA |
893 | int width = bitmap.GetWidth(); |
894 | int height = bitmap.GetHeight(); | |
c7abc967 | 895 | Create( width, height ); |
bba6f3bd UA |
896 | unsigned char *data = GetData(); |
897 | if( !data ) | |
898 | { | |
50920146 | 899 | wxFAIL_MSG( _T("could not allocate data for image") ); |
bba6f3bd UA |
900 | return; |
901 | } | |
c7abc967 | 902 | |
dbda9e86 | 903 | // calc the number of bytes per scanline and padding in the DIB |
bba6f3bd UA |
904 | int bytePerLine = width*3; |
905 | int sizeDWORD = sizeof( DWORD ); | |
bae41ce1 | 906 | int lineBoundary = bytePerLine % sizeDWORD; |
bba6f3bd | 907 | int padding = 0; |
bae41ce1 | 908 | if( lineBoundary > 0 ) |
bba6f3bd | 909 | { |
bae41ce1 | 910 | padding = sizeDWORD - lineBoundary; |
bba6f3bd UA |
911 | bytePerLine += padding; |
912 | } | |
c7abc967 | 913 | |
dbda9e86 | 914 | // create a DIB header |
bba6f3bd UA |
915 | int headersize = sizeof(BITMAPINFOHEADER); |
916 | LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize ); | |
917 | if( !lpDIBh ) | |
918 | { | |
50920146 | 919 | wxFAIL_MSG( _T("could not allocate data for DIB header") ); |
bba6f3bd UA |
920 | free( data ); |
921 | return; | |
922 | } | |
dbda9e86 | 923 | // Fill in the DIB header |
bba6f3bd UA |
924 | lpDIBh->bmiHeader.biSize = headersize; |
925 | lpDIBh->bmiHeader.biWidth = width; | |
926 | lpDIBh->bmiHeader.biHeight = -height; | |
927 | lpDIBh->bmiHeader.biSizeImage = bytePerLine * height; | |
928 | lpDIBh->bmiHeader.biPlanes = 1; | |
929 | lpDIBh->bmiHeader.biBitCount = 24; | |
930 | lpDIBh->bmiHeader.biCompression = BI_RGB; | |
931 | lpDIBh->bmiHeader.biClrUsed = 0; | |
dbda9e86 | 932 | // These seem not really needed for our purpose here. |
bba6f3bd UA |
933 | lpDIBh->bmiHeader.biClrImportant = 0; |
934 | lpDIBh->bmiHeader.biXPelsPerMeter = 0; | |
935 | lpDIBh->bmiHeader.biYPelsPerMeter = 0; | |
dbda9e86 | 936 | // memory for DIB data |
bba6f3bd UA |
937 | unsigned char *lpBits; |
938 | lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage ); | |
939 | if( !lpBits ) | |
e3554471 | 940 | { |
50920146 | 941 | wxFAIL_MSG( _T("could not allocate data for DIB") ); |
bba6f3bd UA |
942 | free( data ); |
943 | free( lpDIBh ); | |
944 | return; | |
4698648f | 945 | } |
c7abc967 | 946 | |
dbda9e86 | 947 | // copy data from the device-dependent bitmap to the DIB |
bba6f3bd UA |
948 | HDC hdc = ::GetDC(NULL); |
949 | HBITMAP hbitmap; | |
950 | hbitmap = (HBITMAP) bitmap.GetHBITMAP(); | |
951 | ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS ); | |
c7abc967 | 952 | |
dbda9e86 | 953 | // copy DIB data into the wxImage object |
bba6f3bd UA |
954 | int i, j; |
955 | unsigned char *ptdata = data; | |
956 | unsigned char *ptbits = lpBits; | |
957 | for( i=0; i<height; i++ ) | |
958 | { | |
959 | for( j=0; j<width; j++ ) | |
960 | { | |
961 | *(ptdata++) = *(ptbits+2); | |
962 | *(ptdata++) = *(ptbits+1); | |
963 | *(ptdata++) = *(ptbits ); | |
964 | ptbits += 3; | |
dbda9e86 | 965 | } |
bba6f3bd | 966 | ptbits += padding; |
c7abc967 VZ |
967 | } |
968 | ||
dbda9e86 | 969 | // similarly, set data according to the possible mask bitmap |
bba6f3bd UA |
970 | if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() ) |
971 | { | |
972 | hbitmap = (HBITMAP) bitmap.GetMask()->GetMaskBitmap(); | |
dbda9e86 | 973 | // memory DC created, color set, data copied, and memory DC deleted |
bba6f3bd UA |
974 | HDC memdc = ::CreateCompatibleDC( hdc ); |
975 | ::SetTextColor( memdc, RGB( 0, 0, 0 ) ); | |
976 | ::SetBkColor( memdc, RGB( 255, 255, 255 ) ); | |
977 | ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS ); | |
c7abc967 | 978 | ::DeleteDC( memdc ); |
dbda9e86 | 979 | // background color set to RGB(16,16,16) in consistent with wxGTK |
c7abc967 | 980 | unsigned char r=16, g=16, b=16; |
bba6f3bd UA |
981 | ptdata = data; |
982 | ptbits = lpBits; | |
983 | for( i=0; i<height; i++ ) | |
984 | { | |
985 | for( j=0; j<width; j++ ) | |
986 | { | |
987 | if( *ptbits != 0 ) | |
dbda9e86 JS |
988 | ptdata += 3; |
989 | else | |
bba6f3bd UA |
990 | { |
991 | *(ptdata++) = r; | |
992 | *(ptdata++) = g; | |
993 | *(ptdata++) = b; | |
dbda9e86 | 994 | } |
bba6f3bd UA |
995 | ptbits += 3; |
996 | } | |
997 | ptbits += padding; | |
c7abc967 | 998 | } |
bba6f3bd UA |
999 | SetMaskColour( r, g, b ); |
1000 | SetMask( TRUE ); | |
c7abc967 | 1001 | } |
bba6f3bd UA |
1002 | else |
1003 | { | |
1004 | SetMask( FALSE ); | |
c7abc967 VZ |
1005 | } |
1006 | // free allocated resources | |
1007 | ::ReleaseDC(NULL, hdc); | |
bba6f3bd UA |
1008 | free(lpDIBh); |
1009 | free(lpBits); | |
e3554471 JS |
1010 | } |
1011 | ||
1012 | #endif | |
1013 | ||
ce4169a4 RR |
1014 | //----------------------------------------------------------------------------- |
1015 | // GTK conversion routines | |
1016 | //----------------------------------------------------------------------------- | |
1017 | ||
99c67c77 RR |
1018 | #ifdef __WXGTK__ |
1019 | ||
83624f79 RR |
1020 | #include "gtk/gtk.h" |
1021 | #include "gdk/gdk.h" | |
1022 | #include "gdk/gdkx.h" | |
1023 | ||
ba0730de RR |
1024 | #if (GTK_MINOR_VERSION > 0) |
1025 | #include "gdk/gdkrgb.h" | |
1026 | #endif | |
1027 | ||
99c67c77 RR |
1028 | wxBitmap wxImage::ConvertToBitmap() const |
1029 | { | |
1030 | wxBitmap bitmap; | |
c7abc967 | 1031 | |
50920146 | 1032 | wxCHECK_MSG( Ok(), bitmap, _T("invalid image") ); |
c7abc967 | 1033 | |
99c67c77 RR |
1034 | int width = GetWidth(); |
1035 | int height = GetHeight(); | |
c7abc967 | 1036 | |
99c67c77 RR |
1037 | bitmap.SetHeight( height ); |
1038 | bitmap.SetWidth( width ); | |
c7abc967 | 1039 | |
ba0730de RR |
1040 | bitmap.SetPixmap( gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, -1 ) ); |
1041 | ||
1042 | // Retrieve depth | |
c7abc967 | 1043 | |
ba0730de | 1044 | GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() ); |
b134516c | 1045 | if (visual == NULL) visual = gdk_visual_get_system(); |
ba0730de | 1046 | int bpp = visual->depth; |
c7abc967 | 1047 | |
ba0730de | 1048 | bitmap.SetDepth( bpp ); |
c7abc967 | 1049 | |
ba0730de RR |
1050 | if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15; |
1051 | if (bpp < 8) bpp = 8; | |
c7abc967 | 1052 | |
ba0730de RR |
1053 | #if (GTK_MINOR_VERSION > 0) |
1054 | ||
1055 | if (!HasMask() && (bpp > 8)) | |
1056 | { | |
1057 | static bool s_hasInitialized = FALSE; | |
c7abc967 | 1058 | |
ba0730de RR |
1059 | if (!s_hasInitialized) |
1060 | { | |
1061 | gdk_rgb_init(); | |
1062 | s_hasInitialized = TRUE; | |
1063 | } | |
c7abc967 | 1064 | |
ba0730de | 1065 | GdkGC *gc = gdk_gc_new( bitmap.GetPixmap() ); |
c7abc967 | 1066 | |
ba0730de RR |
1067 | gdk_draw_rgb_image( bitmap.GetPixmap(), |
1068 | gc, | |
1069 | 0, 0, | |
1070 | width, height, | |
1071 | GDK_RGB_DITHER_NONE, | |
1072 | GetData(), | |
1073 | width*3 ); | |
c7abc967 | 1074 | |
ba0730de | 1075 | gdk_gc_unref( gc ); |
c7abc967 | 1076 | |
ba0730de RR |
1077 | return bitmap; |
1078 | } | |
c7abc967 | 1079 | |
ba0730de | 1080 | #endif |
c7abc967 | 1081 | |
ba0730de | 1082 | // Create picture image |
c7abc967 | 1083 | |
99c67c77 | 1084 | GdkImage *data_image = |
b134516c | 1085 | gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), width, height ); |
c7abc967 | 1086 | |
ba0730de | 1087 | // Create mask image |
c7abc967 | 1088 | |
99c67c77 | 1089 | GdkImage *mask_image = (GdkImage*) NULL; |
c7abc967 | 1090 | |
99c67c77 RR |
1091 | if (HasMask()) |
1092 | { | |
1093 | unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); | |
c7abc967 | 1094 | |
b134516c | 1095 | mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height ); |
c7abc967 | 1096 | |
4698648f VZ |
1097 | wxMask *mask = new wxMask(); |
1098 | mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 ); | |
c7abc967 | 1099 | |
4698648f | 1100 | bitmap.SetMask( mask ); |
99c67c77 | 1101 | } |
c7abc967 | 1102 | |
99c67c77 | 1103 | // Render |
c7abc967 | 1104 | |
99c67c77 RR |
1105 | enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR }; |
1106 | byte_order b_o = RGB; | |
c7abc967 | 1107 | |
99c67c77 RR |
1108 | if (bpp >= 24) |
1109 | { | |
b134516c | 1110 | GdkVisual *visual = gdk_visual_get_system(); |
99c67c77 RR |
1111 | if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB; |
1112 | else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RGB; | |
1113 | else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG; | |
1114 | else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR; | |
1115 | else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB; | |
1116 | else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR; | |
1117 | } | |
c7abc967 | 1118 | |
99c67c77 RR |
1119 | int r_mask = GetMaskRed(); |
1120 | int g_mask = GetMaskGreen(); | |
1121 | int b_mask = GetMaskBlue(); | |
c7abc967 | 1122 | |
99c67c77 | 1123 | unsigned char* data = GetData(); |
c7abc967 | 1124 | |
99c67c77 RR |
1125 | int index = 0; |
1126 | for (int y = 0; y < height; y++) | |
1127 | { | |
1128 | for (int x = 0; x < width; x++) | |
1129 | { | |
1130 | int r = data[index]; | |
4698648f | 1131 | index++; |
99c67c77 | 1132 | int g = data[index]; |
4698648f | 1133 | index++; |
99c67c77 | 1134 | int b = data[index]; |
4698648f | 1135 | index++; |
c7abc967 | 1136 | |
4698648f VZ |
1137 | if (HasMask()) |
1138 | { | |
1139 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) | |
1140 | gdk_image_put_pixel( mask_image, x, y, 1 ); | |
1141 | else | |
1142 | gdk_image_put_pixel( mask_image, x, y, 0 ); | |
1143 | } | |
c7abc967 | 1144 | |
4698648f VZ |
1145 | if (HasMask()) |
1146 | { | |
1147 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) | |
1148 | gdk_image_put_pixel( mask_image, x, y, 1 ); | |
1149 | else | |
1150 | gdk_image_put_pixel( mask_image, x, y, 0 ); | |
1151 | } | |
c7abc967 | 1152 | |
4698648f VZ |
1153 | switch (bpp) |
1154 | { | |
dbda9e86 | 1155 | case 8: |
4698648f | 1156 | { |
f6fcbb63 | 1157 | int pixel = -1; |
4698648f VZ |
1158 | if (wxTheApp->m_colorCube) |
1159 | { | |
38274997 | 1160 | pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ]; |
4698648f | 1161 | } |
f6fcbb63 | 1162 | else |
4698648f VZ |
1163 | { |
1164 | GdkColormap *cmap = gtk_widget_get_default_colormap(); | |
f6fcbb63 RR |
1165 | GdkColor *colors = cmap->colors; |
1166 | int max = 3 * (65536); | |
c7abc967 | 1167 | |
f6fcbb63 RR |
1168 | for (int i = 0; i < cmap->size; i++) |
1169 | { | |
1170 | int rdiff = (r << 8) - colors[i].red; | |
1171 | int gdiff = (g << 8) - colors[i].green; | |
1172 | int bdiff = (b << 8) - colors[i].blue; | |
1173 | int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff); | |
1174 | if (sum < max) { pixel = i; max = sum; } | |
4698648f | 1175 | } |
99c67c77 | 1176 | } |
c7abc967 | 1177 | |
4698648f | 1178 | gdk_image_put_pixel( data_image, x, y, pixel ); |
c7abc967 | 1179 | |
4698648f VZ |
1180 | break; |
1181 | } | |
dbda9e86 | 1182 | case 15: |
4698648f VZ |
1183 | { |
1184 | guint32 pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); | |
1185 | gdk_image_put_pixel( data_image, x, y, pixel ); | |
1186 | break; | |
1187 | } | |
dbda9e86 | 1188 | case 16: |
4698648f VZ |
1189 | { |
1190 | guint32 pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); | |
1191 | gdk_image_put_pixel( data_image, x, y, pixel ); | |
1192 | break; | |
1193 | } | |
dbda9e86 JS |
1194 | case 32: |
1195 | case 24: | |
4698648f VZ |
1196 | { |
1197 | guint32 pixel = 0; | |
1198 | switch (b_o) | |
1199 | { | |
dbda9e86 JS |
1200 | case RGB: pixel = (r << 16) | (g << 8) | b; break; |
1201 | case RBG: pixel = (r << 16) | (b << 8) | g; break; | |
1202 | case BRG: pixel = (b << 16) | (r << 8) | g; break; | |
1203 | case BGR: pixel = (b << 16) | (g << 8) | r; break; | |
1204 | case GRB: pixel = (g << 16) | (r << 8) | b; break; | |
1205 | case GBR: pixel = (g << 16) | (b << 8) | r; break; | |
4698648f VZ |
1206 | } |
1207 | gdk_image_put_pixel( data_image, x, y, pixel ); | |
1208 | } | |
dbda9e86 | 1209 | default: break; |
4698648f | 1210 | } |
99c67c77 RR |
1211 | } // for |
1212 | } // for | |
c7abc967 | 1213 | |
99c67c77 | 1214 | // Blit picture |
c7abc967 | 1215 | |
99c67c77 | 1216 | GdkGC *data_gc = gdk_gc_new( bitmap.GetPixmap() ); |
c7abc967 | 1217 | |
99c67c77 | 1218 | gdk_draw_image( bitmap.GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height ); |
c7abc967 | 1219 | |
99c67c77 RR |
1220 | gdk_image_destroy( data_image ); |
1221 | gdk_gc_unref( data_gc ); | |
c7abc967 | 1222 | |
99c67c77 | 1223 | // Blit mask |
c7abc967 | 1224 | |
99c67c77 RR |
1225 | if (HasMask()) |
1226 | { | |
1227 | GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() ); | |
c7abc967 | 1228 | |
99c67c77 | 1229 | gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height ); |
c7abc967 | 1230 | |
99c67c77 RR |
1231 | gdk_image_destroy( mask_image ); |
1232 | gdk_gc_unref( mask_gc ); | |
1233 | } | |
c7abc967 | 1234 | |
99c67c77 RR |
1235 | return bitmap; |
1236 | } | |
1237 | ||
1238 | wxImage::wxImage( const wxBitmap &bitmap ) | |
1239 | { | |
50920146 | 1240 | wxCHECK_RET( bitmap.Ok(), _T("invalid bitmap") ); |
c7abc967 | 1241 | |
99c67c77 | 1242 | GdkImage *gdk_image = gdk_image_get( bitmap.GetPixmap(), |
dbda9e86 JS |
1243 | 0, 0, |
1244 | bitmap.GetWidth(), bitmap.GetHeight() ); | |
c7abc967 | 1245 | |
50920146 | 1246 | wxCHECK_RET( gdk_image, _T("couldn't create image") ); |
c7abc967 | 1247 | |
99c67c77 RR |
1248 | Create( bitmap.GetWidth(), bitmap.GetHeight() ); |
1249 | char unsigned *data = GetData(); | |
c7abc967 | 1250 | |
99c67c77 RR |
1251 | if (!data) |
1252 | { | |
1253 | gdk_image_destroy( gdk_image ); | |
50920146 | 1254 | wxFAIL_MSG( _T("couldn't create image") ); |
4698648f | 1255 | return; |
99c67c77 | 1256 | } |
c7abc967 | 1257 | |
99c67c77 RR |
1258 | GdkImage *gdk_image_mask = (GdkImage*) NULL; |
1259 | if (bitmap.GetMask()) | |
1260 | { | |
1261 | gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(), | |
dbda9e86 JS |
1262 | 0, 0, |
1263 | bitmap.GetWidth(), bitmap.GetHeight() ); | |
c7abc967 | 1264 | |
4698648f | 1265 | SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable |
99c67c77 | 1266 | } |
c7abc967 | 1267 | |
99c67c77 RR |
1268 | GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() ); |
1269 | if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent ); | |
1270 | int bpp = visual->depth; | |
1271 | if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15; | |
c7abc967 | 1272 | |
99c67c77 | 1273 | GdkColormap *cmap = gtk_widget_get_default_colormap(); |
c7abc967 | 1274 | |
99c67c77 RR |
1275 | long pos = 0; |
1276 | for (int j = 0; j < bitmap.GetHeight(); j++) | |
1277 | { | |
1278 | for (int i = 0; i < bitmap.GetWidth(); i++) | |
1279 | { | |
4cb122de RR |
1280 | wxInt32 pixel = gdk_image_get_pixel( gdk_image, i, j ); |
1281 | pixel = wxINT32_SWAP_ON_BE( pixel ); | |
99c67c77 RR |
1282 | if (bpp <= 8) |
1283 | { | |
1284 | data[pos] = cmap->colors[pixel].red >> 8; | |
1285 | data[pos+1] = cmap->colors[pixel].green >> 8; | |
1286 | data[pos+2] = cmap->colors[pixel].blue >> 8; | |
1287 | } else if (bpp == 15) | |
1288 | { | |
1289 | data[pos] = (pixel >> 7) & 0xf8; | |
1290 | data[pos+1] = (pixel >> 2) & 0xf8; | |
1291 | data[pos+2] = (pixel << 3) & 0xf8; | |
1292 | } else if (bpp == 16) | |
1293 | { | |
1294 | data[pos] = (pixel >> 8) & 0xf8; | |
1295 | data[pos+1] = (pixel >> 3) & 0xfc; | |
1296 | data[pos+2] = (pixel << 3) & 0xf8; | |
1297 | } else | |
1298 | { | |
1299 | data[pos] = (pixel >> 16) & 0xff; | |
1300 | data[pos+1] = (pixel >> 8) & 0xff; | |
1301 | data[pos+2] = pixel & 0xff; | |
1302 | } | |
c7abc967 | 1303 | |
4698648f VZ |
1304 | if (gdk_image_mask) |
1305 | { | |
1306 | int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j ); | |
1307 | if (mask_pixel == 0) | |
1308 | { | |
99c67c77 RR |
1309 | data[pos] = 16; |
1310 | data[pos+1] = 16; | |
1311 | data[pos+2] = 16; | |
dbda9e86 | 1312 | } |
4698648f | 1313 | } |
c7abc967 | 1314 | |
99c67c77 RR |
1315 | pos += 3; |
1316 | } | |
1317 | } | |
c7abc967 | 1318 | |
99c67c77 RR |
1319 | gdk_image_destroy( gdk_image ); |
1320 | if (gdk_image_mask) gdk_image_destroy( gdk_image_mask ); | |
1321 | } | |
1322 | ||
1323 | #endif | |
ee4c6942 | 1324 | |
ce4169a4 RR |
1325 | //----------------------------------------------------------------------------- |
1326 | // Motif conversion routines | |
1327 | //----------------------------------------------------------------------------- | |
1328 | ||
ee4c6942 | 1329 | #ifdef __WXMOTIF__ |
b75867a6 RR |
1330 | |
1331 | #include <Xm/Xm.h> | |
1332 | #include "wx/utils.h" | |
38274997 | 1333 | #include <math.h> |
b75867a6 | 1334 | |
ee4c6942 JS |
1335 | wxBitmap wxImage::ConvertToBitmap() const |
1336 | { | |
b75867a6 | 1337 | wxBitmap bitmap; |
c7abc967 | 1338 | |
50920146 | 1339 | wxCHECK_MSG( Ok(), bitmap, _T("invalid image") ); |
a91b47e8 | 1340 | |
b75867a6 RR |
1341 | int width = GetWidth(); |
1342 | int height = GetHeight(); | |
c7abc967 | 1343 | |
b75867a6 RR |
1344 | bitmap.SetHeight( height ); |
1345 | bitmap.SetWidth( width ); | |
c7abc967 | 1346 | |
b75867a6 RR |
1347 | Display *dpy = (Display*) wxGetDisplay(); |
1348 | Visual* vis = DefaultVisual( dpy, DefaultScreen( dpy ) ); | |
1349 | int bpp = DefaultDepth( dpy, DefaultScreen( dpy ) ); | |
c7abc967 | 1350 | |
b75867a6 | 1351 | // Create image |
c7abc967 | 1352 | |
b75867a6 | 1353 | XImage *data_image = XCreateImage( dpy, vis, bpp, ZPixmap, 0, 0, width, height, 32, 0 ); |
a91b47e8 | 1354 | data_image->data = (char*) malloc( data_image->bytes_per_line * data_image->height ); |
c7abc967 | 1355 | |
b75867a6 | 1356 | bitmap.Create( width, height, bpp ); |
a91b47e8 | 1357 | |
dbda9e86 | 1358 | /* |
b75867a6 | 1359 | // Create mask |
c7abc967 | 1360 | |
dbda9e86 | 1361 | GdkImage *mask_image = (GdkImage*) NULL; |
c7abc967 | 1362 | |
dbda9e86 JS |
1363 | if (HasMask()) |
1364 | { | |
b75867a6 | 1365 | unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); |
c7abc967 | 1366 | |
dbda9e86 | 1367 | mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height ); |
c7abc967 | 1368 | |
dbda9e86 JS |
1369 | wxMask *mask = new wxMask(); |
1370 | mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 ); | |
c7abc967 | 1371 | |
dbda9e86 JS |
1372 | bitmap.SetMask( mask ); |
1373 | } | |
1374 | */ | |
c7abc967 | 1375 | |
b75867a6 | 1376 | // Retrieve depth info |
c7abc967 | 1377 | |
b75867a6 RR |
1378 | XVisualInfo vinfo_template; |
1379 | XVisualInfo *vi; | |
c7abc967 | 1380 | |
b75867a6 RR |
1381 | vinfo_template.visual = vis; |
1382 | vinfo_template.visualid = XVisualIDFromVisual( vis ); | |
1383 | vinfo_template.depth = bpp; | |
1384 | int nitem = 0; | |
c7abc967 | 1385 | |
b75867a6 | 1386 | vi = XGetVisualInfo( dpy, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem ); |
c7abc967 VZ |
1387 | |
1388 | wxCHECK_MSG( vi, wxNullBitmap, _T("no visual") ); | |
1389 | ||
38274997 | 1390 | XFree( vi ); |
a91b47e8 | 1391 | |
b75867a6 RR |
1392 | if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15; |
1393 | if (bpp < 8) bpp = 8; | |
c7abc967 | 1394 | |
b75867a6 | 1395 | // Render |
c7abc967 | 1396 | |
b75867a6 RR |
1397 | enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR }; |
1398 | byte_order b_o = RGB; | |
c7abc967 | 1399 | |
b75867a6 RR |
1400 | if (bpp >= 24) |
1401 | { | |
1402 | if ((vi->red_mask > vi->green_mask) && (vi->green_mask > vi->blue_mask)) b_o = RGB; | |
1403 | else if ((vi->red_mask > vi->blue_mask) && (vi->blue_mask > vi->green_mask)) b_o = RGB; | |
1404 | else if ((vi->blue_mask > vi->red_mask) && (vi->red_mask > vi->green_mask)) b_o = BRG; | |
1405 | else if ((vi->blue_mask > vi->green_mask) && (vi->green_mask > vi->red_mask)) b_o = BGR; | |
1406 | else if ((vi->green_mask > vi->red_mask) && (vi->red_mask > vi->blue_mask)) b_o = GRB; | |
1407 | else if ((vi->green_mask > vi->blue_mask) && (vi->blue_mask > vi->red_mask)) b_o = GBR; | |
1408 | } | |
c7abc967 | 1409 | |
dbda9e86 | 1410 | /* |
b75867a6 RR |
1411 | int r_mask = GetMaskRed(); |
1412 | int g_mask = GetMaskGreen(); | |
1413 | int b_mask = GetMaskBlue(); | |
dbda9e86 | 1414 | */ |
c7abc967 | 1415 | |
38274997 RR |
1416 | XColor colors[256]; |
1417 | if (bpp == 8) | |
1418 | { | |
dbda9e86 | 1419 | Colormap cmap = (Colormap) wxTheApp->GetMainColormap( dpy ); |
c7abc967 | 1420 | |
38274997 | 1421 | for (int i = 0; i < 256; i++) colors[i].pixel = i; |
dbda9e86 | 1422 | XQueryColors( dpy, cmap, colors, 256 ); |
38274997 | 1423 | } |
c7abc967 | 1424 | |
b75867a6 | 1425 | unsigned char* data = GetData(); |
c7abc967 | 1426 | |
b75867a6 RR |
1427 | int index = 0; |
1428 | for (int y = 0; y < height; y++) | |
1429 | { | |
1430 | for (int x = 0; x < width; x++) | |
1431 | { | |
1432 | int r = data[index]; | |
dbda9e86 | 1433 | index++; |
b75867a6 | 1434 | int g = data[index]; |
dbda9e86 | 1435 | index++; |
b75867a6 | 1436 | int b = data[index]; |
dbda9e86 | 1437 | index++; |
c7abc967 | 1438 | |
dbda9e86 JS |
1439 | /* |
1440 | if (HasMask()) | |
1441 | { | |
1442 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) | |
1443 | gdk_image_put_pixel( mask_image, x, y, 1 ); | |
1444 | else | |
1445 | gdk_image_put_pixel( mask_image, x, y, 0 ); | |
1446 | } | |
1447 | */ | |
c7abc967 | 1448 | |
dbda9e86 JS |
1449 | switch (bpp) |
1450 | { | |
1451 | case 8: | |
1452 | { | |
b75867a6 | 1453 | int pixel = -1; |
dbda9e86 JS |
1454 | /* |
1455 | if (wxTheApp->m_colorCube) | |
1456 | { | |
1457 | pixel = wxTheApp->m_colorCube | |
c7abc967 VZ |
1458 | [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ]; |
1459 | } | |
b75867a6 | 1460 | else |
dbda9e86 JS |
1461 | { |
1462 | */ | |
1463 | int max = 3 * (65536); | |
1464 | for (int i = 0; i < 256; i++) | |
1465 | { | |
1466 | int rdiff = (r << 8) - colors[i].red; | |
1467 | int gdiff = (g << 8) - colors[i].green; | |
1468 | int bdiff = (b << 8) - colors[i].blue; | |
1469 | int sum = abs (rdiff) + abs (gdiff) + abs (bdiff); | |
1470 | if (sum < max) { pixel = i; max = sum; } | |
1471 | } | |
1472 | /* | |
1473 | } | |
1474 | */ | |
1475 | XPutPixel( data_image, x, y, pixel ); | |
1476 | break; | |
1477 | } | |
1478 | case 15: | |
1479 | { | |
1480 | int pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); | |
1481 | XPutPixel( data_image, x, y, pixel ); | |
1482 | break; | |
1483 | } | |
1484 | case 16: | |
1485 | { | |
1486 | int pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); | |
1487 | XPutPixel( data_image, x, y, pixel ); | |
1488 | break; | |
1489 | } | |
1490 | case 32: | |
1491 | case 24: | |
1492 | { | |
1493 | int pixel = 0; | |
1494 | switch (b_o) | |
1495 | { | |
1496 | case RGB: pixel = (r << 16) | (g << 8) | b; break; | |
1497 | case RBG: pixel = (r << 16) | (b << 8) | g; break; | |
1498 | case BRG: pixel = (b << 16) | (r << 8) | g; break; | |
1499 | case BGR: pixel = (b << 16) | (g << 8) | r; break; | |
1500 | case GRB: pixel = (g << 16) | (r << 8) | b; break; | |
1501 | case GBR: pixel = (g << 16) | (b << 8) | r; break; | |
1502 | } | |
1503 | XPutPixel( data_image, x, y, pixel ); | |
1504 | } | |
1505 | default: break; | |
1506 | } | |
b75867a6 RR |
1507 | } // for |
1508 | } // for | |
c7abc967 | 1509 | |
b75867a6 | 1510 | // Blit picture |
c7abc967 | 1511 | |
b75867a6 RR |
1512 | XGCValues gcvalues; |
1513 | gcvalues.foreground = BlackPixel( dpy, DefaultScreen( dpy ) ); | |
1514 | GC gc = XCreateGC( dpy, RootWindow ( dpy, DefaultScreen(dpy) ), GCForeground, &gcvalues ); | |
1515 | XPutImage( dpy, (Drawable)bitmap.GetPixmap(), gc, data_image, 0, 0, 0, 0, width, height ); | |
c7abc967 | 1516 | |
b75867a6 RR |
1517 | XDestroyImage( data_image ); |
1518 | XFreeGC( dpy, gc ); | |
c7abc967 | 1519 | |
dbda9e86 | 1520 | /* |
b75867a6 | 1521 | // Blit mask |
c7abc967 | 1522 | |
dbda9e86 JS |
1523 | if (HasMask()) |
1524 | { | |
1525 | GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() ); | |
c7abc967 | 1526 | |
b75867a6 | 1527 | gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height ); |
c7abc967 | 1528 | |
dbda9e86 JS |
1529 | gdk_image_destroy( mask_image ); |
1530 | gdk_gc_unref( mask_gc ); | |
1531 | } | |
1532 | */ | |
c7abc967 | 1533 | |
b75867a6 | 1534 | return bitmap; |
ee4c6942 JS |
1535 | } |
1536 | ||
1537 | wxImage::wxImage( const wxBitmap &bitmap ) | |
1538 | { | |
50920146 | 1539 | wxCHECK_RET( bitmap.Ok(), _T("invalid bitmap") ); |
c7abc967 | 1540 | |
38274997 RR |
1541 | Display *dpy = (Display*) wxGetDisplay(); |
1542 | Visual* vis = DefaultVisual( dpy, DefaultScreen( dpy ) ); | |
1543 | int bpp = DefaultDepth( dpy, DefaultScreen( dpy ) ); | |
c7abc967 | 1544 | |
38274997 | 1545 | XImage *ximage = XGetImage( dpy, |
dbda9e86 JS |
1546 | (Drawable)bitmap.GetPixmap(), |
1547 | 0, 0, | |
1548 | bitmap.GetWidth(), bitmap.GetHeight(), | |
1549 | AllPlanes, ZPixmap ); | |
c7abc967 | 1550 | |
50920146 | 1551 | wxCHECK_RET( ximage, _T("couldn't create image") ); |
c7abc967 | 1552 | |
38274997 RR |
1553 | Create( bitmap.GetWidth(), bitmap.GetHeight() ); |
1554 | char unsigned *data = GetData(); | |
c7abc967 | 1555 | |
38274997 RR |
1556 | if (!data) |
1557 | { | |
1558 | XDestroyImage( ximage ); | |
50920146 | 1559 | wxFAIL_MSG( _T("couldn't create image") ); |
38274997 RR |
1560 | return; |
1561 | } | |
c7abc967 | 1562 | |
dbda9e86 | 1563 | /* |
38274997 RR |
1564 | GdkImage *gdk_image_mask = (GdkImage*) NULL; |
1565 | if (bitmap.GetMask()) | |
1566 | { | |
dbda9e86 JS |
1567 | gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(), |
1568 | 0, 0, | |
1569 | bitmap.GetWidth(), bitmap.GetHeight() ); | |
c7abc967 | 1570 | |
dbda9e86 JS |
1571 | SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable |
1572 | } | |
1573 | */ | |
c7abc967 | 1574 | |
38274997 | 1575 | // Retrieve depth info |
c7abc967 | 1576 | |
38274997 RR |
1577 | XVisualInfo vinfo_template; |
1578 | XVisualInfo *vi; | |
c7abc967 | 1579 | |
38274997 RR |
1580 | vinfo_template.visual = vis; |
1581 | vinfo_template.visualid = XVisualIDFromVisual( vis ); | |
1582 | vinfo_template.depth = bpp; | |
1583 | int nitem = 0; | |
c7abc967 | 1584 | |
38274997 | 1585 | vi = XGetVisualInfo( dpy, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem ); |
c7abc967 | 1586 | |
bea74fbb | 1587 | wxCHECK_RET( vi, _T("no visual") ); |
c7abc967 | 1588 | |
38274997 | 1589 | if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15; |
c7abc967 | 1590 | |
38274997 | 1591 | XFree( vi ); |
c7abc967 | 1592 | |
38274997 RR |
1593 | XColor colors[256]; |
1594 | if (bpp == 8) | |
1595 | { | |
dbda9e86 | 1596 | Colormap cmap = (Colormap)wxTheApp->GetMainColormap( dpy ); |
c7abc967 | 1597 | |
38274997 | 1598 | for (int i = 0; i < 256; i++) colors[i].pixel = i; |
dbda9e86 | 1599 | XQueryColors( dpy, cmap, colors, 256 ); |
38274997 | 1600 | } |
c7abc967 | 1601 | |
38274997 RR |
1602 | long pos = 0; |
1603 | for (int j = 0; j < bitmap.GetHeight(); j++) | |
1604 | { | |
1605 | for (int i = 0; i < bitmap.GetWidth(); i++) | |
1606 | { | |
dbda9e86 | 1607 | int pixel = XGetPixel( ximage, i, j ); |
38274997 RR |
1608 | if (bpp <= 8) |
1609 | { | |
1610 | data[pos] = colors[pixel].red >> 8; | |
1611 | data[pos+1] = colors[pixel].green >> 8; | |
1612 | data[pos+2] = colors[pixel].blue >> 8; | |
1613 | } else if (bpp == 15) | |
1614 | { | |
1615 | data[pos] = (pixel >> 7) & 0xf8; | |
1616 | data[pos+1] = (pixel >> 2) & 0xf8; | |
1617 | data[pos+2] = (pixel << 3) & 0xf8; | |
1618 | } else if (bpp == 16) | |
1619 | { | |
1620 | data[pos] = (pixel >> 8) & 0xf8; | |
1621 | data[pos+1] = (pixel >> 3) & 0xfc; | |
1622 | data[pos+2] = (pixel << 3) & 0xf8; | |
1623 | } else | |
1624 | { | |
1625 | data[pos] = (pixel >> 16) & 0xff; | |
1626 | data[pos+1] = (pixel >> 8) & 0xff; | |
1627 | data[pos+2] = pixel & 0xff; | |
1628 | } | |
c7abc967 | 1629 | |
dbda9e86 | 1630 | /* |
38274997 RR |
1631 | if (gdk_image_mask) |
1632 | { | |
dbda9e86 JS |
1633 | int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j ); |
1634 | if (mask_pixel == 0) | |
1635 | { | |
1636 | data[pos] = 16; | |
1637 | data[pos+1] = 16; | |
1638 | data[pos+2] = 16; | |
38274997 | 1639 | } |
dbda9e86 JS |
1640 | } |
1641 | */ | |
c7abc967 | 1642 | |
38274997 RR |
1643 | pos += 3; |
1644 | } | |
1645 | } | |
c7abc967 | 1646 | |
38274997 | 1647 | XDestroyImage( ximage ); |
dbda9e86 | 1648 | /* |
38274997 | 1649 | if (gdk_image_mask) gdk_image_destroy( gdk_image_mask ); |
dbda9e86 | 1650 | */ |
ee4c6942 JS |
1651 | } |
1652 | #endif | |
a91b47e8 JS |
1653 | |
1654 | // A module to allow wxImage initialization/cleanup | |
1655 | // without calling these functions from app.cpp or from | |
1656 | // the user's application. | |
1657 | ||
1658 | class wxImageModule: public wxModule | |
1659 | { | |
1660 | DECLARE_DYNAMIC_CLASS(wxImageModule) | |
1661 | public: | |
1662 | wxImageModule() {} | |
1663 | bool OnInit() { wxImage::InitStandardHandlers(); return TRUE; }; | |
1664 | void OnExit() { wxImage::CleanUpHandlers(); }; | |
1665 | }; | |
1666 | ||
1667 | IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule) |