Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / vcl / quartz / salbmp.cxx
blob2704dc428da3cd0352a2cb5efee645d7b8ad1e16
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <cstddef>
23 #include <limits>
25 #include "basebmp/scanlineformats.hxx"
26 #include "basebmp/color.hxx"
28 #include "basegfx/vector/b2ivector.hxx"
30 #include "tools/color.hxx"
32 #include "vcl/bitmap.hxx"
33 #include "vcl/salbtype.hxx"
35 #include "quartz/salbmp.h"
36 #include "quartz/utils.h"
38 #ifdef MACOSX
39 #include "osx/saldata.hxx"
40 #else
41 #include "saldatabasic.hxx"
42 #endif
44 #include "bmpfast.hxx"
46 static const unsigned long k16BitRedColorMask = 0x00007c00;
47 static const unsigned long k16BitGreenColorMask = 0x000003e0;
48 static const unsigned long k16BitBlueColorMask = 0x0000001f;
50 static const unsigned long k32BitRedColorMask = 0x00ff0000;
51 static const unsigned long k32BitGreenColorMask = 0x0000ff00;
52 static const unsigned long k32BitBlueColorMask = 0x000000ff;
54 #if defined IOS && defined DBG_UTIL
56 #include <MobileCoreServices/UTCoreTypes.h>
57 #include <ImageIO/ImageIO.h>
59 static void writeImageToFile(CGImageRef image, const char *baseName)
61 static bool bDoIt = getenv("DBG_WRITE_CGIMAGES");
62 if (!bDoIt)
63 return;
64 static int counter = 0;
65 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
66 NSString *documentsDirectory = [paths objectAtIndex:0];
67 NSString *path = [NSString stringWithFormat:@"%@/%s.%d.png", documentsDirectory, baseName, counter++];
68 CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:path];
69 CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL);
70 CGImageDestinationAddImage(destination, image, nil);
72 if (!CGImageDestinationFinalize(destination)) {
73 NSLog(@"Failed to write image to %@", path);
74 } else {
75 SAL_DEBUG("--- saved image " << baseName << " to " << [path UTF8String]);
78 CFRelease(destination);
81 #define DBG_WRITE_IMAGE(image, name) writeImageToFile(image, name)
83 #else
85 #define DBG_WRITE_IMAGE(image, name) /* empty */
87 #endif
89 static bool isValidBitCount( sal_uInt16 nBitCount )
91 return (nBitCount == 1) || (nBitCount == 4) || (nBitCount == 8) || (nBitCount == 16) || (nBitCount == 24) || (nBitCount == 32);
94 QuartzSalBitmap::QuartzSalBitmap()
95 : mxGraphicContext( NULL )
96 , mxCachedImage( NULL )
97 , mnBits(0)
98 , mnWidth(0)
99 , mnHeight(0)
100 , mnBytesPerRow(0)
104 QuartzSalBitmap::~QuartzSalBitmap()
106 Destroy();
109 bool QuartzSalBitmap::Create( CGLayerRef xLayer, int nBitmapBits,
110 int nX, int nY, int nWidth, int nHeight )
112 DBG_ASSERT( xLayer, "QuartzSalBitmap::Create() from non-layered context" );
114 // sanitize input parameters
115 if( nX < 0 )
116 nWidth += nX, nX = 0;
117 if( nY < 0 )
118 nHeight += nY, nY = 0;
119 const CGSize aLayerSize = CGLayerGetSize( xLayer );
120 CG_TRACE( "CGLayerGetSize(" << xLayer << ") = " << aLayerSize );
121 if( nWidth >= (int)aLayerSize.width - nX )
122 nWidth = (int)aLayerSize.width - nX;
123 if( nHeight >= (int)aLayerSize.height - nY )
124 nHeight = (int)aLayerSize.height - nY;
125 if( (nWidth < 0) || (nHeight < 0) )
126 nWidth = nHeight = 0;
128 // initialize properties
129 mnWidth = nWidth;
130 mnHeight = nHeight;
131 mnBits = nBitmapBits ? nBitmapBits : 32;
133 // initialize drawing context
134 CreateContext();
136 // copy layer content into the bitmap buffer
137 const CGPoint aSrcPoint = { static_cast<CGFloat>(-nX), static_cast<CGFloat>(-nY) };
138 if(mxGraphicContext) // remove warning
140 CG_TRACE( "CGContextDrawLayerAtPoint(" << mxGraphicContext << "," << aSrcPoint << "," << xLayer << ")" );
141 CGContextDrawLayerAtPoint( mxGraphicContext, aSrcPoint, xLayer );
143 return true;
146 bool QuartzSalBitmap::Create( const Size& rSize, sal_uInt16 nBits, const BitmapPalette& rBitmapPalette )
148 if( !isValidBitCount( nBits ) )
149 return false;
150 maPalette = rBitmapPalette;
151 mnBits = nBits;
152 mnWidth = rSize.Width();
153 mnHeight = rSize.Height();
154 return AllocateUserData();
157 bool QuartzSalBitmap::Create( const SalBitmap& rSalBmp )
159 return Create( rSalBmp, rSalBmp.GetBitCount() );
162 bool QuartzSalBitmap::Create( const SalBitmap& rSalBmp, SalGraphics* pGraphics )
164 return Create( rSalBmp, pGraphics ? pGraphics->GetBitCount() : rSalBmp.GetBitCount() );
167 bool QuartzSalBitmap::Create( const SalBitmap& rSalBmp, sal_uInt16 nNewBitCount )
169 const QuartzSalBitmap& rSourceBitmap = static_cast<const QuartzSalBitmap&>(rSalBmp);
171 if( isValidBitCount( nNewBitCount ) && rSourceBitmap.maUserBuffer.get() )
173 mnBits = nNewBitCount;
174 mnWidth = rSourceBitmap.mnWidth;
175 mnHeight = rSourceBitmap.mnHeight;
176 maPalette = rSourceBitmap.maPalette;
178 if( AllocateUserData() )
180 ConvertBitmapData( mnWidth, mnHeight, mnBits, mnBytesPerRow, maPalette, maUserBuffer.get(), rSourceBitmap.mnBits, rSourceBitmap.mnBytesPerRow, rSourceBitmap.maPalette, rSourceBitmap.maUserBuffer.get() );
181 return true;
184 return false;
187 bool QuartzSalBitmap::Create( const ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XBitmapCanvas > /*xBitmapCanvas*/, Size& /*rSize*/, bool /*bMask*/ )
189 return false;
192 void QuartzSalBitmap::Destroy()
194 DestroyContext();
195 maUserBuffer.reset();
198 void QuartzSalBitmap::DestroyContext()
200 if( mxCachedImage )
202 CG_TRACE( "CGImageRelease(" << mxCachedImage << ")" );
203 CGImageRelease( mxCachedImage );
204 mxCachedImage = NULL;
207 if( mxGraphicContext )
209 CG_TRACE( "CGContextRelease(" << mxGraphicContext << ")" );
210 CGContextRelease( mxGraphicContext );
211 mxGraphicContext = NULL;
212 maContextBuffer.reset();
216 bool QuartzSalBitmap::CreateContext()
218 DestroyContext();
220 // prepare graphics context
221 // convert image from user input if available
222 const bool bSkipConversion = !maUserBuffer;
223 if( bSkipConversion )
224 AllocateUserData();
226 // default to RGBA color space
227 CGColorSpaceRef aCGColorSpace = GetSalData()->mxRGBSpace;
228 CGBitmapInfo aCGBmpInfo = kCGImageAlphaNoneSkipFirst;
230 // convert data into something accepted by CGBitmapContextCreate()
231 size_t bitsPerComponent = (mnBits == 16) ? 5 : 8;
232 sal_uInt32 nContextBytesPerRow = mnBytesPerRow;
233 if( (mnBits == 16) || (mnBits == 32) )
235 // no conversion needed for truecolor
236 maContextBuffer = maUserBuffer;
238 else if( mnBits == 8 && maPalette.IsGreyPalette() )
240 // no conversion needed for grayscale
241 maContextBuffer = maUserBuffer;
242 aCGColorSpace = GetSalData()->mxGraySpace;
243 aCGBmpInfo = kCGImageAlphaNone;
244 bitsPerComponent = mnBits;
246 // TODO: is special handling for 1bit input buffers worth it?
247 else
249 // convert user data to 32 bit
250 nContextBytesPerRow = mnWidth << 2;
253 maContextBuffer.reset( new sal_uInt8[ mnHeight * nContextBytesPerRow ] );
254 #ifdef DBG_UTIL
255 for (size_t i = 0; i < mnHeight * nContextBytesPerRow; i++)
256 maContextBuffer.get()[i] = (i & 0xFF);
257 #endif
259 if( !bSkipConversion )
260 ConvertBitmapData( mnWidth, mnHeight,
261 32, nContextBytesPerRow, maPalette, maContextBuffer.get(),
262 mnBits, mnBytesPerRow, maPalette, maUserBuffer.get() );
264 catch( const std::bad_alloc& )
266 mxGraphicContext = 0;
270 if( maContextBuffer.get() )
272 mxGraphicContext = CGBitmapContextCreate( maContextBuffer.get(), mnWidth, mnHeight,
273 bitsPerComponent, nContextBytesPerRow, aCGColorSpace, aCGBmpInfo );
274 CG_TRACE( "CGBitmapContextCreate(" << mnWidth << "x" << mnHeight << "x" << bitsPerComponent << ") = " << mxGraphicContext );
277 if( !mxGraphicContext )
278 maContextBuffer.reset();
280 return mxGraphicContext != NULL;
283 bool QuartzSalBitmap::AllocateUserData()
285 Destroy();
287 if( mnWidth && mnHeight )
289 mnBytesPerRow = 0;
291 switch( mnBits )
293 case 1: mnBytesPerRow = (mnWidth + 7) >> 3; break;
294 case 4: mnBytesPerRow = (mnWidth + 1) >> 1; break;
295 case 8: mnBytesPerRow = mnWidth; break;
296 case 16: mnBytesPerRow = mnWidth << 1; break;
297 case 24: mnBytesPerRow = (mnWidth << 1) + mnWidth; break;
298 case 32: mnBytesPerRow = mnWidth << 2; break;
299 default:
300 OSL_FAIL("vcl::QuartzSalBitmap::AllocateUserData(), illegal bitcount!");
304 bool alloc = false;
305 if (mnBytesPerRow != 0
306 && mnBytesPerRow <= std::numeric_limits<std::size_t>::max() / mnHeight)
310 maUserBuffer.reset( new sal_uInt8[mnBytesPerRow * mnHeight] );
311 alloc = true;
313 catch (std::bad_alloc &) {}
315 if (!alloc)
317 SAL_WARN(
318 "vcl.quartz", "bad alloc " << mnBytesPerRow << "x" << mnHeight);
319 maUserBuffer.reset( static_cast<sal_uInt8*>(NULL) );
320 mnBytesPerRow = 0;
322 #ifdef DBG_UTIL
323 else
325 for (size_t i = 0; i < mnBytesPerRow * mnHeight; i++)
326 maUserBuffer.get()[i] = (i & 0xFF);
328 #endif
330 return maUserBuffer.get() != 0;
333 class ImplPixelFormat
335 protected:
336 sal_uInt8* pData;
337 public:
338 static ImplPixelFormat* GetFormat( sal_uInt16 nBits, const BitmapPalette& rPalette );
340 virtual void StartLine( sal_uInt8* pLine ) { pData = pLine; }
341 virtual void SkipPixel( sal_uInt32 nPixel ) = 0;
342 virtual ColorData ReadPixel() = 0;
343 virtual void WritePixel( ColorData nColor ) = 0;
344 virtual ~ImplPixelFormat() { }
347 class ImplPixelFormat32 : public ImplPixelFormat
348 // currently ARGB-format for 32bit depth
350 public:
351 virtual void SkipPixel( sal_uInt32 nPixel ) SAL_OVERRIDE
353 pData += nPixel << 2;
355 virtual ColorData ReadPixel() SAL_OVERRIDE
357 const ColorData c = RGB_COLORDATA( pData[1], pData[2], pData[3] );
358 pData += 4;
359 return c;
361 virtual void WritePixel( ColorData nColor ) SAL_OVERRIDE
363 *pData++ = 0;
364 *pData++ = COLORDATA_RED( nColor );
365 *pData++ = COLORDATA_GREEN( nColor );
366 *pData++ = COLORDATA_BLUE( nColor );
370 class ImplPixelFormat24 : public ImplPixelFormat
371 // currently BGR-format for 24bit depth
373 public:
374 virtual void SkipPixel( sal_uInt32 nPixel ) SAL_OVERRIDE
376 pData += (nPixel << 1) + nPixel;
378 virtual ColorData ReadPixel() SAL_OVERRIDE
380 const ColorData c = RGB_COLORDATA( pData[2], pData[1], pData[0] );
381 pData += 3;
382 return c;
384 virtual void WritePixel( ColorData nColor ) SAL_OVERRIDE
386 *pData++ = COLORDATA_BLUE( nColor );
387 *pData++ = COLORDATA_GREEN( nColor );
388 *pData++ = COLORDATA_RED( nColor );
392 class ImplPixelFormat16 : public ImplPixelFormat
393 // currently R5G6B5-format for 16bit depth
395 protected:
396 sal_uInt16* pData16;
397 public:
399 virtual void StartLine( sal_uInt8* pLine ) SAL_OVERRIDE
401 pData16 = (sal_uInt16*)pLine;
403 virtual void SkipPixel( sal_uInt32 nPixel ) SAL_OVERRIDE
405 pData += nPixel;
407 virtual ColorData ReadPixel() SAL_OVERRIDE
409 const ColorData c = RGB_COLORDATA( (*pData & 0x7c00) >> 7, (*pData & 0x03e0) >> 2 , (*pData & 0x001f) << 3 );
410 pData++;
411 return c;
413 virtual void WritePixel( ColorData nColor ) SAL_OVERRIDE
415 *pData++ = ((COLORDATA_RED( nColor ) & 0xf8 ) << 7 ) ||
416 ((COLORDATA_GREEN( nColor ) & 0xf8 ) << 2 ) ||
417 ((COLORDATA_BLUE( nColor ) & 0xf8 ) >> 3 );
421 class ImplPixelFormat8 : public ImplPixelFormat
423 private:
424 const BitmapPalette& mrPalette;
426 public:
427 ImplPixelFormat8( const BitmapPalette& rPalette )
428 : mrPalette( rPalette )
431 virtual void SkipPixel( sal_uInt32 nPixel ) SAL_OVERRIDE
433 pData += nPixel;
435 virtual ColorData ReadPixel() SAL_OVERRIDE
437 return mrPalette[ *pData++ ].operator Color().GetColor();
439 virtual void WritePixel( ColorData nColor ) SAL_OVERRIDE
441 const BitmapColor aColor( COLORDATA_RED( nColor ), COLORDATA_GREEN( nColor ), COLORDATA_BLUE( nColor ) );
442 *pData++ = static_cast< sal_uInt8 >( mrPalette.GetBestIndex( aColor ) );
446 class ImplPixelFormat4 : public ImplPixelFormat
448 private:
449 const BitmapPalette& mrPalette;
450 sal_uInt32 mnX;
451 sal_uInt32 mnShift;
453 public:
454 ImplPixelFormat4( const BitmapPalette& rPalette )
455 : mrPalette( rPalette )
458 virtual void SkipPixel( sal_uInt32 nPixel ) SAL_OVERRIDE
460 mnX += nPixel;
461 if( (nPixel & 1) )
462 mnShift ^= 4;
464 virtual void StartLine( sal_uInt8* pLine ) SAL_OVERRIDE
466 pData = pLine;
467 mnX = 0;
468 mnShift = 4;
470 virtual ColorData ReadPixel() SAL_OVERRIDE
472 const BitmapColor& rColor = mrPalette[( pData[mnX >> 1] >> mnShift) & 0x0f];
473 mnX++;
474 mnShift ^= 4;
475 return rColor.operator Color().GetColor();
477 virtual void WritePixel( ColorData nColor ) SAL_OVERRIDE
479 const BitmapColor aColor( COLORDATA_RED( nColor ), COLORDATA_GREEN( nColor ), COLORDATA_BLUE( nColor ) );
480 pData[mnX>>1] &= (0xf0 >> mnShift);
481 pData[mnX>>1] |= (static_cast< sal_uInt8 >( mrPalette.GetBestIndex( aColor ) ) & 0x0f);
482 mnX++;
483 mnShift ^= 4;
487 class ImplPixelFormat1 : public ImplPixelFormat
489 private:
490 const BitmapPalette& mrPalette;
491 sal_uInt32 mnX;
493 public:
494 ImplPixelFormat1( const BitmapPalette& rPalette )
495 : mrPalette( rPalette )
498 virtual void SkipPixel( sal_uInt32 nPixel ) SAL_OVERRIDE
500 mnX += nPixel;
502 virtual void StartLine( sal_uInt8* pLine ) SAL_OVERRIDE
504 pData = pLine;
505 mnX = 0;
507 virtual ColorData ReadPixel() SAL_OVERRIDE
509 const BitmapColor& rColor = mrPalette[ (pData[mnX >> 3 ] >> ( 7 - ( mnX & 7 ) )) & 1];
510 mnX++;
511 return rColor.operator Color().GetColor();
513 virtual void WritePixel( ColorData nColor ) SAL_OVERRIDE
515 const BitmapColor aColor( COLORDATA_RED( nColor ), COLORDATA_GREEN( nColor ), COLORDATA_BLUE( nColor ) );
516 if( mrPalette.GetBestIndex( aColor ) & 1 )
517 pData[ mnX >> 3 ] |= 1 << ( 7 - ( mnX & 7 ) );
518 else
519 pData[ mnX >> 3 ] &= ~( 1 << ( 7 - ( mnX & 7 ) ) );
520 mnX++;
524 ImplPixelFormat* ImplPixelFormat::GetFormat( sal_uInt16 nBits, const BitmapPalette& rPalette )
526 switch( nBits )
528 case 1: return new ImplPixelFormat1( rPalette );
529 case 4: return new ImplPixelFormat4( rPalette );
530 case 8: return new ImplPixelFormat8( rPalette );
531 case 16: return new ImplPixelFormat16;
532 case 24: return new ImplPixelFormat24;
533 case 32: return new ImplPixelFormat32;
536 return 0;
539 void QuartzSalBitmap::ConvertBitmapData( sal_uInt32 nWidth, sal_uInt32 nHeight,
540 sal_uInt16 nDestBits, sal_uInt32 nDestBytesPerRow, const BitmapPalette& rDestPalette, sal_uInt8* pDestData,
541 sal_uInt16 nSrcBits, sal_uInt32 nSrcBytesPerRow, const BitmapPalette& rSrcPalette, sal_uInt8* pSrcData )
544 if( (nDestBytesPerRow == nSrcBytesPerRow) && (nDestBits == nSrcBits) && ((nSrcBits != 8) || (rDestPalette.operator==( rSrcPalette ))) )
546 // simple case, same format, so just copy
547 memcpy( pDestData, pSrcData, nHeight * nDestBytesPerRow );
548 return;
551 // try accelerated conversion if possible
552 // TODO: are other truecolor conversions except BGR->ARGB worth it?
553 bool bConverted = false;
554 if( (nSrcBits == 24) && (nDestBits == 32) )
556 // TODO: extend bmpfast.cxx with a method that can be directly used here
557 BitmapBuffer aSrcBuf;
558 aSrcBuf.mnFormat = BMP_FORMAT_24BIT_TC_BGR;
559 aSrcBuf.mpBits = pSrcData;
560 aSrcBuf.mnBitCount = nSrcBits;
561 aSrcBuf.mnScanlineSize = nSrcBytesPerRow;
562 BitmapBuffer aDstBuf;
563 aDstBuf.mnFormat = BMP_FORMAT_32BIT_TC_ARGB;
564 aDstBuf.mpBits = pDestData;
565 aSrcBuf.mnBitCount = nDestBits;
566 aDstBuf.mnScanlineSize = nDestBytesPerRow;
568 aSrcBuf.mnWidth = aDstBuf.mnWidth = nWidth;
569 aSrcBuf.mnHeight = aDstBuf.mnHeight = nHeight;
571 SalTwoRect aTwoRects;
572 aTwoRects.mnSrcX = aTwoRects.mnDestX = 0;
573 aTwoRects.mnSrcY = aTwoRects.mnDestY = 0;
574 aTwoRects.mnSrcWidth = aTwoRects.mnDestWidth = mnWidth;
575 aTwoRects.mnSrcHeight = aTwoRects.mnDestHeight = mnHeight;
576 bConverted = ::ImplFastBitmapConversion( aDstBuf, aSrcBuf, aTwoRects );
579 if( !bConverted )
581 // TODO: this implementation is for clarety, not for speed
583 ImplPixelFormat* pD = ImplPixelFormat::GetFormat( nDestBits, rDestPalette );
584 ImplPixelFormat* pS = ImplPixelFormat::GetFormat( nSrcBits, rSrcPalette );
586 if( pD && pS )
588 sal_uInt32 nY = nHeight;
589 while( nY-- )
591 pD->StartLine( pDestData );
592 pS->StartLine( pSrcData );
594 sal_uInt32 nX = nWidth;
595 while( nX-- )
596 pD->WritePixel( pS->ReadPixel() );
598 pSrcData += nSrcBytesPerRow;
599 pDestData += nDestBytesPerRow;
602 delete pS;
603 delete pD;
607 Size QuartzSalBitmap::GetSize() const
609 return Size( mnWidth, mnHeight );
612 sal_uInt16 QuartzSalBitmap::GetBitCount() const
614 return mnBits;
617 static struct pal_entry
619 sal_uInt8 mnRed;
620 sal_uInt8 mnGreen;
621 sal_uInt8 mnBlue;
623 const aImplSalSysPalEntryAry[ 16 ] =
625 { 0, 0, 0 },
626 { 0, 0, 0x80 },
627 { 0, 0x80, 0 },
628 { 0, 0x80, 0x80 },
629 { 0x80, 0, 0 },
630 { 0x80, 0, 0x80 },
631 { 0x80, 0x80, 0 },
632 { 0x80, 0x80, 0x80 },
633 { 0xC0, 0xC0, 0xC0 },
634 { 0, 0, 0xFF },
635 { 0, 0xFF, 0 },
636 { 0, 0xFF, 0xFF },
637 { 0xFF, 0, 0 },
638 { 0xFF, 0, 0xFF },
639 { 0xFF, 0xFF, 0 },
640 { 0xFF, 0xFF, 0xFF }
643 const BitmapPalette& GetDefaultPalette( int mnBits, bool bMonochrome )
645 if( bMonochrome )
646 return Bitmap::GetGreyPalette( 1U << mnBits );
648 // at this point we should provide some kind of default palette
649 // since all other platforms do so, too.
650 static bool bDefPalInit = false;
651 static BitmapPalette aDefPalette256;
652 static BitmapPalette aDefPalette16;
653 static BitmapPalette aDefPalette2;
654 if( ! bDefPalInit )
656 bDefPalInit = true;
657 aDefPalette256.SetEntryCount( 256 );
658 aDefPalette16.SetEntryCount( 16 );
659 aDefPalette2.SetEntryCount( 2 );
661 // Standard colors
662 unsigned int i;
663 for( i = 0; i < 16; i++ )
665 aDefPalette16[i] =
666 aDefPalette256[i] = BitmapColor( aImplSalSysPalEntryAry[i].mnRed,
667 aImplSalSysPalEntryAry[i].mnGreen,
668 aImplSalSysPalEntryAry[i].mnBlue );
671 aDefPalette2[0] = BitmapColor( 0, 0, 0 );
672 aDefPalette2[1] = BitmapColor( 0xff, 0xff, 0xff );
674 // own palette (6/6/6)
675 const int DITHER_PAL_STEPS = 6;
676 const sal_uInt8 DITHER_PAL_DELTA = 51;
677 int nB, nG, nR;
678 sal_uInt8 nRed, nGreen, nBlue;
679 for( nB=0, nBlue=0; nB < DITHER_PAL_STEPS; nB++, nBlue += DITHER_PAL_DELTA )
681 for( nG=0, nGreen=0; nG < DITHER_PAL_STEPS; nG++, nGreen += DITHER_PAL_DELTA )
683 for( nR=0, nRed=0; nR < DITHER_PAL_STEPS; nR++, nRed += DITHER_PAL_DELTA )
685 aDefPalette256[ i ] = BitmapColor( nRed, nGreen, nBlue );
686 i++;
692 // now fill in appropriate palette
693 switch( mnBits )
695 case 1: return aDefPalette2;
696 case 4: return aDefPalette16;
697 case 8: return aDefPalette256;
698 default: break;
701 const static BitmapPalette aEmptyPalette;
702 return aEmptyPalette;
705 BitmapBuffer* QuartzSalBitmap::AcquireBuffer( bool /*bReadOnly*/ )
707 if( !maUserBuffer.get() )
708 // || maContextBuffer.get() && (maUserBuffer.get() != maContextBuffer.get()) )
710 // fprintf(stderr,"ASB::Acq(%dx%d,d=%d)\n",mnWidth,mnHeight,mnBits);
711 // TODO: AllocateUserData();
712 return NULL;
715 BitmapBuffer* pBuffer = new BitmapBuffer;
716 pBuffer->mnWidth = mnWidth;
717 pBuffer->mnHeight = mnHeight;
718 pBuffer->maPalette = maPalette;
719 pBuffer->mnScanlineSize = mnBytesPerRow;
720 pBuffer->mpBits = maUserBuffer.get();
721 pBuffer->mnBitCount = mnBits;
722 switch( mnBits )
724 case 1: pBuffer->mnFormat = BMP_FORMAT_1BIT_MSB_PAL; break;
725 case 4: pBuffer->mnFormat = BMP_FORMAT_4BIT_MSN_PAL; break;
726 case 8: pBuffer->mnFormat = BMP_FORMAT_8BIT_PAL; break;
727 case 16: pBuffer->mnFormat = BMP_FORMAT_16BIT_TC_MSB_MASK;
728 pBuffer->maColorMask = ColorMask( k16BitRedColorMask, k16BitGreenColorMask, k16BitBlueColorMask );
729 break;
730 case 24: pBuffer->mnFormat = BMP_FORMAT_24BIT_TC_BGR; break;
731 case 32: pBuffer->mnFormat = BMP_FORMAT_32BIT_TC_ARGB;
732 pBuffer->maColorMask = ColorMask( k32BitRedColorMask, k32BitGreenColorMask, k32BitBlueColorMask );
733 break;
735 pBuffer->mnFormat |= BMP_FORMAT_BOTTOM_UP;
737 // some BitmapBuffer users depend on a complete palette
738 if( (mnBits <= 8) && !maPalette )
739 pBuffer->maPalette = GetDefaultPalette( mnBits, true );
741 return pBuffer;
744 void QuartzSalBitmap::ReleaseBuffer( BitmapBuffer* pBuffer, bool bReadOnly )
746 // invalidate graphic context if we have different data
747 if( !bReadOnly )
749 maPalette = pBuffer->maPalette;
750 if( mxGraphicContext )
751 DestroyContext();
754 delete pBuffer;
757 CGImageRef QuartzSalBitmap::CreateCroppedImage( int nX, int nY, int nNewWidth, int nNewHeight ) const
759 if( !mxCachedImage )
761 if( !mxGraphicContext )
762 if( !const_cast<QuartzSalBitmap*>(this)->CreateContext() )
763 return NULL;
765 mxCachedImage = CGBitmapContextCreateImage( mxGraphicContext );
766 CG_TRACE( "CGBitmapContextCreateImage(" << mxGraphicContext << ") = " << mxCachedImage );
769 CGImageRef xCroppedImage = NULL;
770 // short circuit if there is nothing to crop
771 if( !nX && !nY && (mnWidth == nNewWidth) && (mnHeight == nNewHeight) )
773 xCroppedImage = mxCachedImage;
774 CG_TRACE( "CFRetain(" << xCroppedImage << ")" );
775 CFRetain( xCroppedImage );
777 else
779 nY = mnHeight - (nY + nNewHeight); // adjust for y-mirrored context
780 const CGRect aCropRect = { { static_cast<CGFloat>(nX), static_cast<CGFloat>(nY) }, { static_cast<CGFloat>(nNewWidth), static_cast<CGFloat>(nNewHeight) } };
781 xCroppedImage = CGImageCreateWithImageInRect( mxCachedImage, aCropRect );
782 CG_TRACE( "CGImageCreateWithImageInRect(" << mxCachedImage << "," << aCropRect << ") = " << xCroppedImage );
785 return xCroppedImage;
788 static void CFRTLFree(void* /*info*/, const void* data, size_t /*size*/)
790 rtl_freeMemory( const_cast<void*>(data) );
793 CGImageRef QuartzSalBitmap::CreateWithMask( const QuartzSalBitmap& rMask,
794 int nX, int nY, int nWidth, int nHeight ) const
796 CGImageRef xImage( CreateCroppedImage( nX, nY, nWidth, nHeight ) );
797 if( !xImage )
798 return NULL;
800 CGImageRef xMask = rMask.CreateCroppedImage( nX, nY, nWidth, nHeight );
801 if( !xMask )
802 return xImage;
804 // CGImageCreateWithMask() only likes masks or greyscale images => convert if needed
805 // TODO: isolate in an extra method?
806 DBG_WRITE_IMAGE(xMask, "xMask");
807 if( !CGImageIsMask(xMask) || rMask.GetBitCount() != 8)//(CGImageGetColorSpace(xMask) != GetSalData()->mxGraySpace) )
809 const CGRect xImageRect=CGRectMake( 0, 0, nWidth, nHeight );//the rect has no offset
811 // create the alpha mask image fitting our image
812 // TODO: is caching the full mask or the subimage mask worth it?
813 int nMaskBytesPerRow = ((nWidth + 3) & ~3);
814 void* pMaskMem = rtl_allocateMemory( nMaskBytesPerRow * nHeight );
815 CGContextRef xMaskContext = CGBitmapContextCreate( pMaskMem,
816 nWidth, nHeight, 8, nMaskBytesPerRow, GetSalData()->mxGraySpace, kCGImageAlphaNone );
817 CG_TRACE( "CGBitmapContextCreate(" << nWidth << "x" << nHeight << "x8," << nMaskBytesPerRow << ") = " << xMaskContext );
818 CG_TRACE( "CGContextDrawImage(" << xMaskContext << "," << xImageRect << "," << xMask << ")" );
819 CGContextDrawImage( xMaskContext, xImageRect, xMask );
820 CG_TRACE( "CFRelease(" << xMask << ")" );
821 CFRelease( xMask );
822 CGDataProviderRef xDataProvider( CGDataProviderCreateWithData( NULL,
823 pMaskMem, nHeight * nMaskBytesPerRow, &CFRTLFree ) );
824 static const CGFloat* pDecode = NULL;
825 xMask = CGImageMaskCreate( nWidth, nHeight, 8, 8, nMaskBytesPerRow, xDataProvider, pDecode, false );
826 CG_TRACE( "CGImageMaskCreate(" << nWidth << "," << nHeight << ",8,8) = " << xMask );
827 CFRelease( xDataProvider );
828 CG_TRACE( "CFRelease(" << xMaskContext << ")" );
829 CFRelease( xMaskContext );
832 if( !xMask )
833 return xImage;
835 // combine image and alpha mask
836 CGImageRef xMaskedImage = CGImageCreateWithMask( xImage, xMask );
837 CG_TRACE( "CGImageCreateWithMask(" << xImage << "," << xMask << ") = " << xMaskedImage );
838 DBG_WRITE_IMAGE(xImage, "xImage");
839 DBG_WRITE_IMAGE(xMaskedImage, "xMaskedImage");
840 CG_TRACE( "CFRelease(" << xMask << ")" );
841 CFRelease( xMask );
842 CG_TRACE( "CFRelease(" << xImage << ")" );
843 CFRelease( xImage );
844 return xMaskedImage;
847 /** creates an image from the given rectangle, replacing all black pixels with nMaskColor and make all other full transparent */
848 CGImageRef QuartzSalBitmap::CreateColorMask( int nX, int nY, int nWidth, int nHeight, SalColor nMaskColor ) const
850 CGImageRef xMask = 0;
851 if( maUserBuffer.get() && (nX + nWidth <= mnWidth) && (nY + nHeight <= mnHeight) )
853 const sal_uInt32 nDestBytesPerRow = nWidth << 2;
854 sal_uInt32* pMaskBuffer = static_cast<sal_uInt32*>( rtl_allocateMemory( nHeight * nDestBytesPerRow ) );
855 sal_uInt32* pDest = pMaskBuffer;
857 ImplPixelFormat* pSourcePixels = ImplPixelFormat::GetFormat( mnBits, maPalette );
859 if( pMaskBuffer && pSourcePixels )
861 sal_uInt32 nColor;
862 reinterpret_cast<sal_uInt8*>(&nColor)[0] = 0xff;
863 reinterpret_cast<sal_uInt8*>(&nColor)[1] = SALCOLOR_RED( nMaskColor );
864 reinterpret_cast<sal_uInt8*>(&nColor)[2] = SALCOLOR_GREEN( nMaskColor );
865 reinterpret_cast<sal_uInt8*>(&nColor)[3] = SALCOLOR_BLUE( nMaskColor );
867 sal_uInt8* pSource = maUserBuffer.get();
868 if( nY )
869 pSource += nY * mnBytesPerRow;
871 int y = nHeight;
872 while( y-- )
874 pSourcePixels->StartLine( pSource );
875 pSourcePixels->SkipPixel(nX);
876 sal_uInt32 x = nWidth;
877 while( x-- )
879 *pDest++ = ( pSourcePixels->ReadPixel() == 0 ) ? nColor : 0;
881 pSource += mnBytesPerRow;
884 CGDataProviderRef xDataProvider( CGDataProviderCreateWithData(NULL, pMaskBuffer, nHeight * nDestBytesPerRow, &CFRTLFree) );
885 xMask = CGImageCreate(nWidth, nHeight, 8, 32, nDestBytesPerRow, GetSalData()->mxRGBSpace, kCGImageAlphaPremultipliedFirst, xDataProvider, NULL, true, kCGRenderingIntentDefault);
886 CG_TRACE( "CGImageCreate(" << nWidth << "x" << nHeight << "x8) = " << xMask );
887 CFRelease(xDataProvider);
889 else
891 free(pMaskBuffer);
894 delete pSourcePixels;
896 return xMask;
899 /** QuartzSalBitmap::GetSystemData Get platform native image data from existing image
901 * @param rData struct BitmapSystemData, defined in vcl/inc/bitmap.hxx
902 * @return true if successful
904 bool QuartzSalBitmap::GetSystemData( BitmapSystemData& rData )
906 bool bRet = false;
908 if( !mxGraphicContext )
909 CreateContext();
911 if ( mxGraphicContext )
913 bRet = true;
915 if ((CGBitmapContextGetBitsPerPixel(mxGraphicContext) == 32) &&
916 (CGBitmapContextGetBitmapInfo(mxGraphicContext) & kCGBitmapByteOrderMask) != kCGBitmapByteOrder32Host) {
918 * We need to hack things because VCL does not use kCGBitmapByteOrder32Host, while Cairo requires it.
920 OSL_TRACE("QuartzSalBitmap::%s(): kCGBitmapByteOrder32Host not found => inserting it.",__func__);
922 CGImageRef xImage = CGBitmapContextCreateImage (mxGraphicContext);
923 CG_TRACE( "CGBitmapContextCreateImage(" << mxGraphicContext << ") = " << xImage );
925 // re-create the context with single change: include kCGBitmapByteOrder32Host flag.
926 CGContextRef mxGraphicContextNew = CGBitmapContextCreate( CGBitmapContextGetData(mxGraphicContext),
927 CGBitmapContextGetWidth(mxGraphicContext),
928 CGBitmapContextGetHeight(mxGraphicContext),
929 CGBitmapContextGetBitsPerComponent(mxGraphicContext),
930 CGBitmapContextGetBytesPerRow(mxGraphicContext),
931 CGBitmapContextGetColorSpace(mxGraphicContext),
932 CGBitmapContextGetBitmapInfo(mxGraphicContext) | kCGBitmapByteOrder32Host);
933 CG_TRACE( "CGBitmapContextCreate(" << CGBitmapContextGetWidth(mxGraphicContext) << "x" << CGBitmapContextGetHeight(mxGraphicContext) << "x" << CGBitmapContextGetBitsPerComponent(mxGraphicContext) << ") = " << mxGraphicContextNew );
935 CG_TRACE( "CFRelease(" << mxGraphicContext << ")" );
936 CFRelease(mxGraphicContext);
938 // Needs to be flipped
939 CG_TRACE( "CGContextSaveGState(" << mxGraphicContextNew << ")" );
940 CGContextSaveGState( mxGraphicContextNew );
941 CG_TRACE( "CGContextTranslateCTM(" << mxGraphicContextNew << ",0," << CGBitmapContextGetHeight(mxGraphicContextNew) << ")" );
942 CGContextTranslateCTM (mxGraphicContextNew, 0, CGBitmapContextGetHeight(mxGraphicContextNew));
943 CG_TRACE( "CGContextScaleCTM(" << mxGraphicContextNew << ",1,-1)" );
944 CGContextScaleCTM (mxGraphicContextNew, 1.0, -1.0);
946 CG_TRACE( "CGContextDrawImage(" << mxGraphicContextNew << "," << CGRectMake(0, 0, CGImageGetWidth(xImage), CGImageGetHeight(xImage)) << "," << xImage << ")" );
947 CGContextDrawImage(mxGraphicContextNew, CGRectMake( 0, 0, CGImageGetWidth(xImage), CGImageGetHeight(xImage)), xImage);
949 // Flip back
950 CG_TRACE( "CGContextRestoreGState(" << mxGraphicContextNew << ")" );
951 CGContextRestoreGState( mxGraphicContextNew );
953 CG_TRACE( "CGImageRelease(" << xImage << ")" );
954 CGImageRelease( xImage );
955 mxGraphicContext = mxGraphicContextNew;
958 rData.rImageContext = (void *) mxGraphicContext;
959 rData.mnWidth = mnWidth;
960 rData.mnHeight = mnHeight;
963 return bRet;
966 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */