]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/bitmap.mm
wxCocoa: Added wxNativePixelFormat and defined wxPIXEL_FORMAT_ALPHA
[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"
18#endif //WX_PRECOMP
a24aff65 19#include "wx/bitmap.h"
a24aff65 20#include "wx/image.h"
d135b1a5 21#include "wx/xpmdecod.h"
a24aff65 22
d135b1a5
DE
23#include "wx/cocoa/autorelease.h"
24#include "wx/cocoa/string.h"
25
26#import <AppKit/NSBitmapImageRep.h>
27#import <AppKit/NSGraphics.h>
28
29// ========================================================================
30// wxBitmapRefData
31// ========================================================================
32class wxBitmapRefData: public wxGDIRefData
33{
34 friend class wxBitmap;
35public:
36 wxBitmapRefData();
37 wxBitmapRefData( const wxBitmapRefData& data );
38 virtual ~wxBitmapRefData();
39
40protected:
41 int m_width;
42 int m_height;
43 int m_depth;
44 bool m_ok;
45 int m_numColors;
46 wxPalette m_bitmapPalette;
47 int m_quality;
48 WX_NSBitmapImageRep m_cocoaNSBitmapImageRep;
49 wxMask *m_bitmapMask; // Optional mask
50};
51
52#define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
a24aff65
DE
53
54wxBitmapRefData::wxBitmapRefData()
55{
56 m_ok = FALSE;
57 m_width = 0;
58 m_height = 0;
59 m_depth = 0;
60 m_quality = 0;
61 m_numColors = 0;
d135b1a5 62 m_cocoaNSBitmapImageRep = nil;
a24aff65
DE
63 m_bitmapMask = NULL;
64}
65
d135b1a5
DE
66wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
67{
68 m_width = data.m_width;
69 m_height = data.m_height;
70 m_depth = data.m_depth;
71 m_ok = data.m_ok;
72 m_numColors = data.m_numColors;
73 m_bitmapPalette = data.m_bitmapPalette;
74 m_quality = data.m_quality;
ab13160e 75 m_cocoaNSBitmapImageRep = [data.m_cocoaNSBitmapImageRep copyWithZone:nil];
d135b1a5
DE
76 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
77}
78
a24aff65
DE
79wxBitmapRefData::~wxBitmapRefData()
80{
d135b1a5
DE
81 [m_cocoaNSBitmapImageRep release];
82 m_cocoaNSBitmapImageRep = NULL;
a24aff65 83
d135b1a5 84 delete m_bitmapMask;
a24aff65
DE
85 m_bitmapMask = NULL;
86}
87
d135b1a5
DE
88// ========================================================================
89// wxBitmap
90// ========================================================================
91IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
92
a24aff65
DE
93wxBitmap::wxBitmap()
94{
95 m_refData = NULL;
a24aff65
DE
96}
97
98wxBitmap::~wxBitmap()
99{
a24aff65
DE
100}
101
102wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
103{
104 m_refData = new wxBitmapRefData;
105
106 M_BITMAPDATA->m_width = the_width ;
107 M_BITMAPDATA->m_height = the_height ;
108 M_BITMAPDATA->m_depth = no_bits ;
109 M_BITMAPDATA->m_numColors = 0;
110
111 /* TODO: create the bitmap from data */
a24aff65
DE
112}
113
114wxBitmap::wxBitmap(int w, int h, int d)
115{
116 (void)Create(w, h, d);
a24aff65
DE
117}
118
119wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
120{
121 (void) Create(data, type, width, height, depth);
a24aff65
DE
122}
123
124wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
125{
126 LoadFile(filename, type);
a24aff65
DE
127}
128
d135b1a5 129wxObjectRefData *wxBitmap::CreateRefData() const
a24aff65 130{
d135b1a5 131 return new wxBitmapRefData;
a24aff65
DE
132}
133
d135b1a5 134wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
a24aff65 135{
d135b1a5 136 return new wxBitmapRefData(*(wxBitmapRefData*)data);
a24aff65
DE
137}
138
d135b1a5 139WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
a24aff65 140{
d135b1a5
DE
141 if(!M_BITMAPDATA)
142 return NULL;
143 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
a24aff65
DE
144}
145
ab13160e
DE
146void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
147{
148 if(!M_BITMAPDATA)
149 return;
150 // NOTE: No checking is done to make sure width/height agree
151 [bitmapImageRep retain];
152 [M_BITMAPDATA->m_cocoaNSBitmapImageRep release];
153 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
154}
155
a24aff65
DE
156void wxBitmap::SetWidth(int w)
157{
158 if (!M_BITMAPDATA)
159 m_refData = new wxBitmapRefData;
160
161 M_BITMAPDATA->m_width = w;
162}
163
164void wxBitmap::SetHeight(int h)
165{
166 if (!M_BITMAPDATA)
167 m_refData = new wxBitmapRefData;
168
169 M_BITMAPDATA->m_height = h;
170}
171
172void wxBitmap::SetDepth(int d)
173{
174 if (!M_BITMAPDATA)
175 m_refData = new wxBitmapRefData;
176
177 M_BITMAPDATA->m_depth = d;
178}
179
180void wxBitmap::SetQuality(int q)
181{
182 if (!M_BITMAPDATA)
183 m_refData = new wxBitmapRefData;
184
185 M_BITMAPDATA->m_quality = q;
186}
187
188void wxBitmap::SetOk(bool isOk)
189{
190 if (!M_BITMAPDATA)
191 m_refData = new wxBitmapRefData;
192
193 M_BITMAPDATA->m_ok = isOk;
194}
195
196void wxBitmap::SetPalette(const wxPalette& palette)
197{
198 if (!M_BITMAPDATA)
199 m_refData = new wxBitmapRefData;
200
201 M_BITMAPDATA->m_bitmapPalette = palette ;
202}
203
204void wxBitmap::SetMask(wxMask *mask)
205{
206 if (!M_BITMAPDATA)
207 m_refData = new wxBitmapRefData;
208
209 M_BITMAPDATA->m_bitmapMask = mask ;
210}
211
d135b1a5 212bool wxBitmap::Ok() const
a24aff65 213{
d135b1a5 214 return m_refData && M_BITMAPDATA->m_ok;
a24aff65
DE
215}
216
d135b1a5 217wxPalette* wxBitmap::GetPalette() const
a24aff65 218{
d135b1a5
DE
219 if(!m_refData)
220 return NULL;
221 return &M_BITMAPDATA->m_bitmapPalette;
a24aff65
DE
222}
223
d135b1a5 224wxMask* wxBitmap::GetMask() const
a24aff65 225{
d135b1a5
DE
226 if(!m_refData)
227 return NULL;
228 return M_BITMAPDATA->m_bitmapMask;
a24aff65
DE
229}
230
d135b1a5 231int wxBitmap::GetDepth() const
a24aff65 232{
d135b1a5
DE
233 if(!m_refData)
234 return 0;
235 return M_BITMAPDATA->m_depth;
a24aff65
DE
236}
237
d135b1a5 238int wxBitmap::GetWidth() const
a24aff65 239{
d135b1a5
DE
240 if(!m_refData)
241 return 0;
242 return M_BITMAPDATA->m_width;
a24aff65
DE
243}
244
d135b1a5 245int wxBitmap::GetHeight() const
a24aff65 246{
d135b1a5
DE
247 if(!m_refData)
248 return 0;
249 return M_BITMAPDATA->m_height;
a24aff65
DE
250}
251
d135b1a5 252bool wxBitmap::Create(int w, int h, int d)
a24aff65 253{
d135b1a5
DE
254 UnRef();
255
256 m_refData = new wxBitmapRefData;
257
258 M_BITMAPDATA->m_width = w;
259 M_BITMAPDATA->m_height = h;
260 M_BITMAPDATA->m_depth = d;
261
262 /* TODO: create new bitmap */
ab13160e
DE
263 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [[NSBitmapImageRep alloc]
264 initWithBitmapDataPlanes: NULL
265 pixelsWide: w
266 pixelsHigh: h
267 bitsPerSample: 8
268 samplesPerPixel: 3
269 hasAlpha: NO
270 isPlanar: NO
271 colorSpaceName: NSCalibratedRGBColorSpace
272 bytesPerRow: 0
273 bitsPerPixel: 0];
274
275 wxLogDebug("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p", M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
276 M_BITMAPDATA->m_ok = true;
277 M_BITMAPDATA->m_numColors = 0;
278 M_BITMAPDATA->m_quality = 0;
279 M_BITMAPDATA->m_bitmapMask = NULL;
d135b1a5
DE
280
281 return M_BITMAPDATA->m_ok;
a24aff65
DE
282}
283
d135b1a5 284bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
a24aff65 285{
d135b1a5
DE
286 wxAutoNSAutoreleasePool pool;
287 UnRef();
a24aff65 288
d135b1a5 289 m_refData = new wxBitmapRefData;
a24aff65 290
d135b1a5
DE
291 NSBitmapImageRep *imageRep = [NSBitmapImageRep
292 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
a24aff65 293
d135b1a5
DE
294 if(imageRep)
295 {
296 M_BITMAPDATA->m_width = [imageRep pixelsWide];
297 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
298 M_BITMAPDATA->m_depth = 24; // FIXME
299 M_BITMAPDATA->m_ok = true;
300 M_BITMAPDATA->m_numColors = 0;
301 M_BITMAPDATA->m_quality = 0;
302 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
303 M_BITMAPDATA->m_bitmapMask = NULL;
304 return true;
305 }
306 wxImage image;
307 if(!image.LoadFile(filename,type))
308 return false;
309 if(!image.Ok())
310 return false;
311 *this = wxBitmap(image);
312 return true;
a24aff65
DE
313}
314
d135b1a5 315bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
a24aff65 316{
d135b1a5
DE
317 UnRef();
318
319 m_refData = new wxBitmapRefData;
320
321 return false;
a24aff65
DE
322}
323
d135b1a5 324bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
a24aff65 325{
d135b1a5 326 return false;
a24aff65
DE
327}
328
d135b1a5 329bool wxBitmap::CopyFromIcon(const wxIcon& icno)
a24aff65 330{
d135b1a5 331 return false;
a24aff65
DE
332}
333
d135b1a5 334wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
a24aff65 335{
d135b1a5
DE
336 return wxNullBitmap;
337}
a24aff65 338
d135b1a5 339wxImage wxBitmap::ConvertToImage() const
a24aff65 340{
d135b1a5
DE
341 if(!M_BITMAPDATA->m_ok)
342 return wxImage(5,5)/*wxNullImage*/;
343 return wxImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
a24aff65
DE
344}
345
d135b1a5 346bool wxBitmap::CreateFromXpm(const char **xpm)
a24aff65 347{
d135b1a5
DE
348#if wxUSE_IMAGE && wxUSE_XPM
349 UnRef();
350
351 wxCHECK_MSG( xpm, false, wxT("invalid XPM data") )
352
353 wxXPMDecoder decoder;
354 wxImage img = decoder.ReadData(xpm);
355 wxCHECK_MSG( img.Ok(), false, wxT("invalid XPM data") )
356
357 *this = wxBitmap(img);
358 return true;
359#else
a24aff65 360 return false;
d135b1a5 361#endif
a24aff65
DE
362}
363
d135b1a5 364bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
a24aff65 365{
d135b1a5
DE
366 UnRef();
367
368 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
369 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
370
371 m_refData = new wxBitmapRefData();
372
373 M_BITMAPDATA->m_width = image.GetWidth();
374 M_BITMAPDATA->m_height = image.GetHeight();
375 NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]
376 initWithBitmapDataPlanes: NULL
377 pixelsWide: image.GetWidth()
378 pixelsHigh: image.GetHeight()
379 bitsPerSample: 8
380 samplesPerPixel: 3
381 hasAlpha: NO
382 isPlanar: NO
383 colorSpaceName: NSCalibratedRGBColorSpace
384 bytesPerRow: 0
385 bitsPerPixel: 0];
386
387 const int numBytes = image.GetWidth()*image.GetHeight()*3;
388 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
389 // TODO: Alpha and convert to desired depth
390 M_BITMAPDATA->m_depth = 24;
391 M_BITMAPDATA->m_ok = true;
392 M_BITMAPDATA->m_numColors = 0;
393 M_BITMAPDATA->m_quality = 0;
394 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage;
395 M_BITMAPDATA->m_bitmapMask = NULL;
396 return true;
a24aff65
DE
397}
398
d135b1a5
DE
399// ========================================================================
400// wxMask
401// ========================================================================
402
403IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
404
405wxMask::wxMask()
a24aff65 406{
d135b1a5
DE
407/* TODO
408 m_maskBitmap = 0;
409*/
a24aff65
DE
410}
411
d135b1a5
DE
412// Construct a mask from a bitmap and a colour indicating
413// the transparent area
414wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
a24aff65 415{
d135b1a5
DE
416/* TODO
417 m_maskBitmap = 0;
418*/
419 Create(bitmap, colour);
a24aff65
DE
420}
421
d135b1a5
DE
422// Construct a mask from a bitmap and a palette index indicating
423// the transparent area
424wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
a24aff65 425{
d135b1a5
DE
426/* TODO
427 m_maskBitmap = 0;
428*/
429
430 Create(bitmap, paletteIndex);
a24aff65
DE
431}
432
d135b1a5
DE
433// Construct a mask from a mono bitmap (copies the bitmap).
434wxMask::wxMask(const wxBitmap& bitmap)
a24aff65 435{
d135b1a5
DE
436/* TODO
437 m_maskBitmap = 0;
438*/
439
440 Create(bitmap);
a24aff65
DE
441}
442
d135b1a5 443wxMask::~wxMask()
a24aff65 444{
d135b1a5 445// TODO: delete mask bitmap
a24aff65
DE
446}
447
d135b1a5
DE
448// Create a mask from a mono bitmap (copies the bitmap).
449bool wxMask::Create(const wxBitmap& bitmap)
a24aff65 450{
d135b1a5
DE
451// TODO
452 return FALSE;
a24aff65
DE
453}
454
d135b1a5
DE
455// Create a mask from a bitmap and a palette index indicating
456// the transparent area
457bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
a24aff65 458{
d135b1a5
DE
459// TODO
460 return FALSE;
a24aff65
DE
461}
462
d135b1a5
DE
463// Create a mask from a bitmap and a colour indicating
464// the transparent area
465bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
a24aff65 466{
d135b1a5
DE
467// TODO
468 return FALSE;
a24aff65
DE
469}
470