Compilation fix
[wxWidgets.git] / src / cocoa / bitmap.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/bitmap.cpp
3 // Purpose:     wxBitmap
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/07/19
7 // RCS-ID:      $Id$
8 // Copyright:   (c) 2003 David Elliott
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
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     #include "wx/colour.h"
19 #endif //WX_PRECOMP
20 #include "wx/bitmap.h"
21 #include "wx/image.h"
22 #include "wx/xpmdecod.h"
23 #include "wx/rawbmp.h"
24
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 // ========================================================================
34 class wxBitmapRefData: public wxGDIRefData
35 {
36     friend class wxBitmap;
37 public:
38     wxBitmapRefData();
39     wxBitmapRefData( const wxBitmapRefData& data );
40     virtual ~wxBitmapRefData();
41
42 protected:
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)
55
56 wxBitmapRefData::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;
64     m_cocoaNSBitmapImageRep = nil;
65     m_bitmapMask = NULL;
66 }
67
68 wxBitmapRefData::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;
77     m_cocoaNSBitmapImageRep = [data.m_cocoaNSBitmapImageRep copyWithZone:nil];
78     m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
79 }
80
81 wxBitmapRefData::~wxBitmapRefData()
82 {
83     [m_cocoaNSBitmapImageRep release];
84     m_cocoaNSBitmapImageRep = NULL;
85
86     delete m_bitmapMask;
87     m_bitmapMask = NULL;
88 }
89
90 // ========================================================================
91 // wxBitmap
92 // ========================================================================
93 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
94
95 wxBitmap::wxBitmap()
96 {
97     m_refData = NULL;
98 }
99
100 wxBitmap::~wxBitmap()
101 {
102 }
103
104 wxBitmap::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 */
114 }
115
116 wxBitmap::wxBitmap(int w, int h, int d)
117 {
118     (void)Create(w, h, d);
119 }
120
121 wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
122 {
123     (void) Create(data, type, width, height, depth);
124 }
125
126 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
127 {
128     LoadFile(filename, type);
129 }
130
131 wxObjectRefData *wxBitmap::CreateRefData() const
132 {
133     return new wxBitmapRefData;
134 }
135
136 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
137 {
138     return new wxBitmapRefData(*(wxBitmapRefData*)data);
139 }
140
141 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
142 {
143     if(!M_BITMAPDATA)
144         return NULL;
145     return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
146 }
147
148 void 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
158 void wxBitmap::SetWidth(int w)
159 {
160     if (!M_BITMAPDATA)
161         m_refData = new wxBitmapRefData;
162
163     M_BITMAPDATA->m_width = w;
164 }
165
166 void wxBitmap::SetHeight(int h)
167 {
168     if (!M_BITMAPDATA)
169         m_refData = new wxBitmapRefData;
170
171     M_BITMAPDATA->m_height = h;
172 }
173
174 void wxBitmap::SetDepth(int d)
175 {
176     if (!M_BITMAPDATA)
177         m_refData = new wxBitmapRefData;
178
179     M_BITMAPDATA->m_depth = d;
180 }
181
182 void wxBitmap::SetQuality(int q)
183 {
184     if (!M_BITMAPDATA)
185         m_refData = new wxBitmapRefData;
186
187     M_BITMAPDATA->m_quality = q;
188 }
189
190 void wxBitmap::SetOk(bool isOk)
191 {
192     if (!M_BITMAPDATA)
193         m_refData = new wxBitmapRefData;
194
195     M_BITMAPDATA->m_ok = isOk;
196 }
197
198 void wxBitmap::SetPalette(const wxPalette& palette)
199 {
200     if (!M_BITMAPDATA)
201         m_refData = new wxBitmapRefData;
202
203     M_BITMAPDATA->m_bitmapPalette = palette ;
204 }
205
206 void wxBitmap::SetMask(wxMask *mask)
207 {
208     if (!M_BITMAPDATA)
209         m_refData = new wxBitmapRefData;
210
211     M_BITMAPDATA->m_bitmapMask = mask ;
212 }
213
214 bool wxBitmap::Ok() const
215 {
216     return m_refData && M_BITMAPDATA->m_ok;
217 }
218
219 wxPalette* wxBitmap::GetPalette() const
220 {
221     if(!m_refData)
222         return NULL;
223     return &M_BITMAPDATA->m_bitmapPalette;
224 }
225
226 wxMask* wxBitmap::GetMask() const
227 {
228     if(!m_refData)
229         return NULL;
230     return M_BITMAPDATA->m_bitmapMask;
231 }
232
233 int wxBitmap::GetDepth() const
234 {
235     if(!m_refData)
236         return 0;
237     return M_BITMAPDATA->m_depth;
238 }
239
240 int wxBitmap::GetWidth() const
241 {
242     if(!m_refData)
243         return 0;
244     return M_BITMAPDATA->m_width;
245 }
246
247 int wxBitmap::GetHeight() const
248 {
249     if(!m_refData)
250         return 0;
251     return M_BITMAPDATA->m_height;
252 }
253
254 bool wxBitmap::Create(int w, int h, int d)
255 {
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 */
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;
282
283     return M_BITMAPDATA->m_ok;
284 }
285
286 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
287 {
288     wxAutoNSAutoreleasePool pool;
289     UnRef();
290
291     m_refData = new wxBitmapRefData;
292
293     NSBitmapImageRep *imageRep = [NSBitmapImageRep
294         imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
295
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;
315 }
316
317 bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
318 {
319     UnRef();
320
321     m_refData = new wxBitmapRefData;
322
323     return false;
324 }
325
326 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
327 {
328     return false;
329 }
330
331 bool wxBitmap::CopyFromIcon(const wxIcon& icno)
332 {
333     return false;
334 }
335
336 wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
337 {
338     return wxNullBitmap;
339 }
340
341 wxImage wxBitmap::ConvertToImage() const
342 {
343     if(!M_BITMAPDATA->m_ok)
344         return wxImage(5,5)/*wxNullImage*/;
345     return wxImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
346 }
347
348 bool wxBitmap::CreateFromXpm(const char **xpm)
349 {
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
362     return false;
363 #endif
364 }
365
366 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
367 {
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;
397     M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue()));
398     return true;
399 }
400
401 void *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
428 void wxBitmap::UngetRawData(wxPixelDataBase& data)
429 {   // TODO
430 }
431
432 // ========================================================================
433 // wxMask
434 // ========================================================================
435
436 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
437
438 wxMask::wxMask()
439 {
440     m_cocoaNSBitmapImageRep = nil;
441 }
442
443 // Construct a mask from a bitmap and a colour indicating
444 // the transparent area
445 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
446 {
447     m_cocoaNSBitmapImageRep = nil;
448     Create(bitmap, colour);
449 }
450
451 // Construct a mask from a bitmap and a palette index indicating
452 // the transparent area
453 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
454 {
455     m_cocoaNSBitmapImageRep = nil;
456
457     Create(bitmap, paletteIndex);
458 }
459
460 // Construct a mask from a mono bitmap (copies the bitmap).
461 wxMask::wxMask(const wxBitmap& bitmap)
462 {
463     m_cocoaNSBitmapImageRep = nil;
464
465     Create(bitmap);
466 }
467
468 wxMask::~wxMask()
469 {
470     [m_cocoaNSBitmapImageRep release];
471 }
472
473 // Create a mask from a mono bitmap (copies the bitmap).
474 bool wxMask::Create(const wxBitmap& bitmap)
475 {
476 // TODO
477     return FALSE;
478 }
479
480 // Create a mask from a bitmap and a palette index indicating
481 // the transparent area
482 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
483 {
484 // TODO
485     return FALSE;
486 }
487
488 template <class PixelData>
489 static 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
534 // Create a mask from a bitmap and a colour indicating
535 // the transparent area
536 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
537 {
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
561     NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep();
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];
568     if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
569     {
570         wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
571         wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
572             false, "Unable to access raw data");
573     }
574     else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES)
575     {
576         wxPixelData<wxBitmap,wxAlphaPixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
577         wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
578             false, "Unable to access raw data");
579     }
580     else
581     {   wxCHECK_MSG(false,false,"Unimplemented pixel format"); }
582
583     // maskRep was autoreleased in case we had to exit quickly
584     m_cocoaNSBitmapImageRep = [maskRep retain];
585     return true;
586 }
587