]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/bitmap.mm
Fixed tabs (i.e. turning into spaces...)
[wxWidgets.git] / src / cocoa / bitmap.mm
CommitLineData
a24aff65 1/////////////////////////////////////////////////////////////////////////////
d135b1a5 2// Name: src/cocoa/bitmap.cpp
a24aff65 3// Purpose: wxBitmap
d135b1a5 4// Author: David Elliott
a24aff65 5// Modified by:
d135b1a5 6// Created: 2003/07/19
a24aff65 7// RCS-ID: $Id$
d135b1a5 8// Copyright: (c) 2003 David Elliott
a24aff65
DE
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
449c5673
DE
12#include "wx/wxprec.h"
13#ifndef WX_PRECOMP
14 #include "wx/log.h"
15 #include "wx/utils.h"
16 #include "wx/palette.h"
17 #include "wx/icon.h"
016b0643 18 #include "wx/colour.h"
449c5673 19#endif //WX_PRECOMP
a24aff65 20#include "wx/bitmap.h"
a24aff65 21#include "wx/image.h"
d135b1a5 22#include "wx/xpmdecod.h"
b60c6e97 23#include "wx/rawbmp.h"
a24aff65 24
d135b1a5
DE
25#include "wx/cocoa/autorelease.h"
26#include "wx/cocoa/string.h"
27
28#import <AppKit/NSBitmapImageRep.h>
29#import <AppKit/NSGraphics.h>
30
31// ========================================================================
32// wxBitmapRefData
33// ========================================================================
34class wxBitmapRefData: public wxGDIRefData
35{
36 friend class wxBitmap;
37public:
38 wxBitmapRefData();
39 wxBitmapRefData( const wxBitmapRefData& data );
40 virtual ~wxBitmapRefData();
41
42protected:
43 int m_width;
44 int m_height;
45 int m_depth;
46 bool m_ok;
47 int m_numColors;
48 wxPalette m_bitmapPalette;
49 int m_quality;
50 WX_NSBitmapImageRep m_cocoaNSBitmapImageRep;
51 wxMask *m_bitmapMask; // Optional mask
52};
53
54#define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
a24aff65
DE
55
56wxBitmapRefData::wxBitmapRefData()
57{
58 m_ok = FALSE;
59 m_width = 0;
60 m_height = 0;
61 m_depth = 0;
62 m_quality = 0;
63 m_numColors = 0;
d135b1a5 64 m_cocoaNSBitmapImageRep = nil;
a24aff65
DE
65 m_bitmapMask = NULL;
66}
67
d135b1a5
DE
68wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
69{
70 m_width = data.m_width;
71 m_height = data.m_height;
72 m_depth = data.m_depth;
73 m_ok = data.m_ok;
74 m_numColors = data.m_numColors;
75 m_bitmapPalette = data.m_bitmapPalette;
76 m_quality = data.m_quality;
ab13160e 77 m_cocoaNSBitmapImageRep = [data.m_cocoaNSBitmapImageRep copyWithZone:nil];
d135b1a5
DE
78 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
79}
80
a24aff65
DE
81wxBitmapRefData::~wxBitmapRefData()
82{
d135b1a5
DE
83 [m_cocoaNSBitmapImageRep release];
84 m_cocoaNSBitmapImageRep = NULL;
a24aff65 85
d135b1a5 86 delete m_bitmapMask;
a24aff65
DE
87 m_bitmapMask = NULL;
88}
89
d135b1a5
DE
90// ========================================================================
91// wxBitmap
92// ========================================================================
93IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
94
a24aff65
DE
95wxBitmap::wxBitmap()
96{
97 m_refData = NULL;
a24aff65
DE
98}
99
100wxBitmap::~wxBitmap()
101{
a24aff65
DE
102}
103
104wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
105{
106 m_refData = new wxBitmapRefData;
107
108 M_BITMAPDATA->m_width = the_width ;
109 M_BITMAPDATA->m_height = the_height ;
110 M_BITMAPDATA->m_depth = no_bits ;
111 M_BITMAPDATA->m_numColors = 0;
112
113 /* TODO: create the bitmap from data */
a24aff65
DE
114}
115
116wxBitmap::wxBitmap(int w, int h, int d)
117{
118 (void)Create(w, h, d);
a24aff65
DE
119}
120
121wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
122{
123 (void) Create(data, type, width, height, depth);
a24aff65
DE
124}
125
126wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
127{
128 LoadFile(filename, type);
a24aff65
DE
129}
130
d135b1a5 131wxObjectRefData *wxBitmap::CreateRefData() const
a24aff65 132{
d135b1a5 133 return new wxBitmapRefData;
a24aff65
DE
134}
135
d135b1a5 136wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
a24aff65 137{
d135b1a5 138 return new wxBitmapRefData(*(wxBitmapRefData*)data);
a24aff65
DE
139}
140
d135b1a5 141WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
a24aff65 142{
d135b1a5
DE
143 if(!M_BITMAPDATA)
144 return NULL;
145 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
a24aff65
DE
146}
147
ab13160e
DE
148void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
149{
150 if(!M_BITMAPDATA)
151 return;
152 // NOTE: No checking is done to make sure width/height agree
153 [bitmapImageRep retain];
154 [M_BITMAPDATA->m_cocoaNSBitmapImageRep release];
155 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
156}
157
a24aff65
DE
158void wxBitmap::SetWidth(int w)
159{
160 if (!M_BITMAPDATA)
161 m_refData = new wxBitmapRefData;
162
163 M_BITMAPDATA->m_width = w;
164}
165
166void wxBitmap::SetHeight(int h)
167{
168 if (!M_BITMAPDATA)
169 m_refData = new wxBitmapRefData;
170
171 M_BITMAPDATA->m_height = h;
172}
173
174void wxBitmap::SetDepth(int d)
175{
176 if (!M_BITMAPDATA)
177 m_refData = new wxBitmapRefData;
178
179 M_BITMAPDATA->m_depth = d;
180}
181
182void wxBitmap::SetQuality(int q)
183{
184 if (!M_BITMAPDATA)
185 m_refData = new wxBitmapRefData;
186
187 M_BITMAPDATA->m_quality = q;
188}
189
190void wxBitmap::SetOk(bool isOk)
191{
192 if (!M_BITMAPDATA)
193 m_refData = new wxBitmapRefData;
194
195 M_BITMAPDATA->m_ok = isOk;
196}
197
198void wxBitmap::SetPalette(const wxPalette& palette)
199{
200 if (!M_BITMAPDATA)
201 m_refData = new wxBitmapRefData;
202
203 M_BITMAPDATA->m_bitmapPalette = palette ;
204}
205
206void wxBitmap::SetMask(wxMask *mask)
207{
208 if (!M_BITMAPDATA)
209 m_refData = new wxBitmapRefData;
210
211 M_BITMAPDATA->m_bitmapMask = mask ;
212}
213
d135b1a5 214bool wxBitmap::Ok() const
a24aff65 215{
d135b1a5 216 return m_refData && M_BITMAPDATA->m_ok;
a24aff65
DE
217}
218
d135b1a5 219wxPalette* wxBitmap::GetPalette() const
a24aff65 220{
d135b1a5
DE
221 if(!m_refData)
222 return NULL;
223 return &M_BITMAPDATA->m_bitmapPalette;
a24aff65
DE
224}
225
d135b1a5 226wxMask* wxBitmap::GetMask() const
a24aff65 227{
d135b1a5
DE
228 if(!m_refData)
229 return NULL;
230 return M_BITMAPDATA->m_bitmapMask;
a24aff65
DE
231}
232
d135b1a5 233int wxBitmap::GetDepth() const
a24aff65 234{
d135b1a5
DE
235 if(!m_refData)
236 return 0;
237 return M_BITMAPDATA->m_depth;
a24aff65
DE
238}
239
d135b1a5 240int wxBitmap::GetWidth() const
a24aff65 241{
d135b1a5
DE
242 if(!m_refData)
243 return 0;
244 return M_BITMAPDATA->m_width;
a24aff65
DE
245}
246
d135b1a5 247int wxBitmap::GetHeight() const
a24aff65 248{
d135b1a5
DE
249 if(!m_refData)
250 return 0;
251 return M_BITMAPDATA->m_height;
a24aff65
DE
252}
253
d135b1a5 254bool wxBitmap::Create(int w, int h, int d)
a24aff65 255{
d135b1a5
DE
256 UnRef();
257
258 m_refData = new wxBitmapRefData;
259
260 M_BITMAPDATA->m_width = w;
261 M_BITMAPDATA->m_height = h;
262 M_BITMAPDATA->m_depth = d;
263
264 /* TODO: create new bitmap */
ab13160e
DE
265 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [[NSBitmapImageRep alloc]
266 initWithBitmapDataPlanes: NULL
267 pixelsWide: w
268 pixelsHigh: h
269 bitsPerSample: 8
270 samplesPerPixel: 3
271 hasAlpha: NO
272 isPlanar: NO
273 colorSpaceName: NSCalibratedRGBColorSpace
274 bytesPerRow: 0
275 bitsPerPixel: 0];
276
277 wxLogDebug("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p", M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
278 M_BITMAPDATA->m_ok = true;
279 M_BITMAPDATA->m_numColors = 0;
280 M_BITMAPDATA->m_quality = 0;
281 M_BITMAPDATA->m_bitmapMask = NULL;
d135b1a5
DE
282
283 return M_BITMAPDATA->m_ok;
a24aff65
DE
284}
285
d135b1a5 286bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
a24aff65 287{
d135b1a5
DE
288 wxAutoNSAutoreleasePool pool;
289 UnRef();
a24aff65 290
d135b1a5 291 m_refData = new wxBitmapRefData;
a24aff65 292
d135b1a5
DE
293 NSBitmapImageRep *imageRep = [NSBitmapImageRep
294 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
a24aff65 295
d135b1a5
DE
296 if(imageRep)
297 {
298 M_BITMAPDATA->m_width = [imageRep pixelsWide];
299 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
300 M_BITMAPDATA->m_depth = 24; // FIXME
301 M_BITMAPDATA->m_ok = true;
302 M_BITMAPDATA->m_numColors = 0;
303 M_BITMAPDATA->m_quality = 0;
304 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
305 M_BITMAPDATA->m_bitmapMask = NULL;
306 return true;
307 }
308 wxImage image;
309 if(!image.LoadFile(filename,type))
310 return false;
311 if(!image.Ok())
312 return false;
313 *this = wxBitmap(image);
314 return true;
a24aff65
DE
315}
316
d135b1a5 317bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
a24aff65 318{
d135b1a5
DE
319 UnRef();
320
321 m_refData = new wxBitmapRefData;
322
323 return false;
a24aff65
DE
324}
325
d135b1a5 326bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
a24aff65 327{
d135b1a5 328 return false;
a24aff65
DE
329}
330
d135b1a5 331bool wxBitmap::CopyFromIcon(const wxIcon& icno)
a24aff65 332{
d135b1a5 333 return false;
a24aff65
DE
334}
335
d135b1a5 336wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
a24aff65 337{
d135b1a5
DE
338 return wxNullBitmap;
339}
a24aff65 340
d135b1a5 341wxImage wxBitmap::ConvertToImage() const
a24aff65 342{
d135b1a5
DE
343 if(!M_BITMAPDATA->m_ok)
344 return wxImage(5,5)/*wxNullImage*/;
345 return wxImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
a24aff65
DE
346}
347
d135b1a5 348bool wxBitmap::CreateFromXpm(const char **xpm)
a24aff65 349{
d135b1a5
DE
350#if wxUSE_IMAGE && wxUSE_XPM
351 UnRef();
352
353 wxCHECK_MSG( xpm, false, wxT("invalid XPM data") )
354
355 wxXPMDecoder decoder;
356 wxImage img = decoder.ReadData(xpm);
357 wxCHECK_MSG( img.Ok(), false, wxT("invalid XPM data") )
358
359 *this = wxBitmap(img);
360 return true;
361#else
a24aff65 362 return false;
d135b1a5 363#endif
a24aff65
DE
364}
365
d135b1a5 366bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
a24aff65 367{
d135b1a5
DE
368 UnRef();
369
370 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
371 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
372
373 m_refData = new wxBitmapRefData();
374
375 M_BITMAPDATA->m_width = image.GetWidth();
376 M_BITMAPDATA->m_height = image.GetHeight();
377 NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]
378 initWithBitmapDataPlanes: NULL
379 pixelsWide: image.GetWidth()
380 pixelsHigh: image.GetHeight()
381 bitsPerSample: 8
382 samplesPerPixel: 3
383 hasAlpha: NO
384 isPlanar: NO
385 colorSpaceName: NSCalibratedRGBColorSpace
386 bytesPerRow: 0
387 bitsPerPixel: 0];
388
389 const int numBytes = image.GetWidth()*image.GetHeight()*3;
390 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
391 // TODO: Alpha and convert to desired depth
392 M_BITMAPDATA->m_depth = 24;
393 M_BITMAPDATA->m_ok = true;
394 M_BITMAPDATA->m_numColors = 0;
395 M_BITMAPDATA->m_quality = 0;
396 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage;
016b0643 397 M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue()));
d135b1a5 398 return true;
a24aff65
DE
399}
400
b60c6e97
DE
401void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
402{
403 if(!Ok())
404 return NULL;
405
406 NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep;
407 if(!bitmapRep)
408 return NULL;
409
410 if([bitmapRep bitsPerPixel]!=bpp)
411 {
412 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
413 return NULL;
414 }
415 data.m_width = [bitmapRep pixelsWide];
416 data.m_height = [bitmapRep pixelsHigh];
417 data.m_stride = [bitmapRep bytesPerRow];
418 return [bitmapRep bitmapData];
419
420 // NOTE: It is up to the user to make sure they used the proper
421 // pixel format class that details what is actually inside the pixels
422 // We can only check to make sure that the total number of bits per
423 // pixel are being iterated over properly
424 // NSBitmapImageRep can contain grayscale or CMYK data and
425 // wxPixelDataBase doesn't really define the color format
426}
427
428void wxBitmap::UngetRawData(wxPixelDataBase& data)
429{ // TODO
430}
431
d135b1a5
DE
432// ========================================================================
433// wxMask
434// ========================================================================
435
436IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
437
438wxMask::wxMask()
a24aff65 439{
016b0643 440 m_cocoaNSBitmapImageRep = nil;
a24aff65
DE
441}
442
d135b1a5
DE
443// Construct a mask from a bitmap and a colour indicating
444// the transparent area
445wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
a24aff65 446{
016b0643 447 m_cocoaNSBitmapImageRep = nil;
d135b1a5 448 Create(bitmap, colour);
a24aff65
DE
449}
450
d135b1a5
DE
451// Construct a mask from a bitmap and a palette index indicating
452// the transparent area
453wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
a24aff65 454{
016b0643 455 m_cocoaNSBitmapImageRep = nil;
d135b1a5
DE
456
457 Create(bitmap, paletteIndex);
a24aff65
DE
458}
459
d135b1a5
DE
460// Construct a mask from a mono bitmap (copies the bitmap).
461wxMask::wxMask(const wxBitmap& bitmap)
a24aff65 462{
016b0643 463 m_cocoaNSBitmapImageRep = nil;
d135b1a5
DE
464
465 Create(bitmap);
a24aff65
DE
466}
467
d135b1a5 468wxMask::~wxMask()
a24aff65 469{
016b0643 470 [m_cocoaNSBitmapImageRep release];
a24aff65
DE
471}
472
d135b1a5
DE
473// Create a mask from a mono bitmap (copies the bitmap).
474bool wxMask::Create(const wxBitmap& bitmap)
a24aff65 475{
d135b1a5
DE
476// TODO
477 return FALSE;
a24aff65
DE
478}
479
d135b1a5
DE
480// Create a mask from a bitmap and a palette index indicating
481// the transparent area
482bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
a24aff65 483{
d135b1a5
DE
484// TODO
485 return FALSE;
a24aff65
DE
486}
487
016b0643
DE
488template <class PixelData>
489static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData)
490{
491 wxCHECK_MSG(dstData,false,"Couldn't access mask data");
492 class PixelData::Iterator p(srcData);
493 const int nRows = srcData.GetHeight();
494 const int nCols = srcData.GetWidth();
495 // Total number of bytes per destination column
496 const int dstRowLength = (nCols+7)/8;
497 // Number of source columns that fit into a byte in the destination
498 const int width_aligned = nCols/8*8;
499 for(int y=0; y<nRows; ++y)
500 {
501 class PixelData::Iterator rowStart(p);
502 unsigned char *dstRow = dstData + y*dstRowLength;
503 for(int x=0; x<width_aligned; x+=8)
504 {
505 unsigned char *dstByte = dstRow + x/8;
506 *dstByte = 0;
507 // Take source RGB, compare it with the wxColour
508 for(int j=0; j<8; ++j, ++p)
509 {
510 *dstByte +=
511 ( p.Red()!=colour.Red()
512 || p.Green()!=colour.Green()
513 || p.Blue()!=colour.Blue()
514 ) << (7-j);
515 }
516 }
517 // Handle the remaining 0-7 pixels in the row
518 unsigned char *dstByte = dstRow + width_aligned/8;
519 *dstByte = 0;
520 for(int j=0; j<(nCols%8); ++j, ++p)
521 {
522 *dstByte +=
523 ( p.Red()!=colour.Red()
524 || p.Green()!=colour.Green()
525 || p.Blue()!=colour.Blue()
526 ) << (7-j);
527 }
528 p = rowStart;
529 p.OffsetY(srcData,1);
530 }
531 return true;
532}
533
d135b1a5
DE
534// Create a mask from a bitmap and a colour indicating
535// the transparent area
536bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
a24aff65 537{
016b0643
DE
538 wxAutoNSAutoreleasePool pool;
539 if(!bitmap.Ok())
540 return false;
541 int bmpWidth = bitmap.GetWidth();
542 int bmpHeight = bitmap.GetHeight();
543 int dstRowLength = (bmpWidth+7)/8;
544
545 // Create a bitmap image rep with 1-bit per pixel data representing
546 // the alpha channel padded such that rows end on byte boundaries
547 // Since NSBitmapImageRep doesn't have any sort of NSNullColorSpace
548 // we must have at least one channel of non-alpha data. In order to
549 // make our life easy, we use planar data which results in two
550 // separate arrays. We don't need to touch the first because it
551 // should never be used. The second is the 1-bit "alpha" data.
552 NSBitmapImageRep *maskRep = [[[NSBitmapImageRep alloc]
553 initWithBitmapDataPlanes:NULL pixelsWide:bmpWidth
554 pixelsHigh:bmpHeight bitsPerSample:1
555 samplesPerPixel:2 hasAlpha:YES isPlanar:YES
556 colorSpaceName:NSCalibratedWhiteColorSpace
557 bytesPerRow:dstRowLength bitsPerPixel:1] autorelease];
558 wxCHECK(maskRep,false);
559
560 // We need the source NSBitmapImageRep to detemine its pixel format
ce20d822 561 NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep();
016b0643
DE
562 wxCHECK_MSG(srcBitmapRep,false,"Can't create mask for an uninitialized bitmap");
563
564 // Get a pointer to the destination data
565 unsigned char *dstPlanes[5] = {NULL,NULL,NULL,NULL,NULL};
566 [maskRep getBitmapDataPlanes:dstPlanes];
567 unsigned char *dstData = dstPlanes[1];
ea3d4caf 568 // The wxImage format (which we use whenever we imported from wxImage)
016b0643
DE
569 if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
570 {
571 wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
572 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
573 false, "Unable to access raw data");
574 }
ea3d4caf
DE
575 // 32-bpp RGBx (x=throw away, no alpha)
576 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
577 {
578 typedef wxPixelFormat<unsigned char,32,0,1,2> PixelFormat;
579 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
580 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
581 false, "Unable to access raw data");
582 }
583 // 32-bpp RGBA
016b0643
DE
584 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES)
585 {
586 wxPixelData<wxBitmap,wxAlphaPixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
587 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
588 false, "Unable to access raw data");
589 }
590 else
591 { wxCHECK_MSG(false,false,"Unimplemented pixel format"); }
592
593 // maskRep was autoreleased in case we had to exit quickly
594 m_cocoaNSBitmapImageRep = [maskRep retain];
595 return true;
a24aff65
DE
596}
597