1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26
27 #include <boost/bind.hpp>
28
29 #include "basebmp/scanlineformats.hxx"
30 #include "basebmp/color.hxx"
31
32 #include "basegfx/vector/b2ivector.hxx"
33
34 #include "tools/color.hxx"
35
36 #include "vcl/bitmap.hxx" // for BitmapSystemData
37 #include "vcl/salbtype.hxx"
38
39 #include "aqua/salbmp.h"
40 #include "aqua/salinst.h"
41
42 #include "bmpfast.hxx"
43
44 // =======================================================================
45
isValidBitCount(sal_uInt16 nBitCount)46 static bool isValidBitCount( sal_uInt16 nBitCount )
47 {
48 return (nBitCount == 1) || (nBitCount == 4) || (nBitCount == 8) || (nBitCount == 16) || (nBitCount == 24) || (nBitCount == 32);
49 }
50
51 // =======================================================================
52
AquaSalBitmap()53 AquaSalBitmap::AquaSalBitmap()
54 : mxGraphicContext( NULL )
55 , mxCachedImage( NULL )
56 , mnBits(0)
57 , mnWidth(0)
58 , mnHeight(0)
59 , mnBytesPerRow(0)
60 {
61 }
62
63 // ------------------------------------------------------------------
64
~AquaSalBitmap()65 AquaSalBitmap::~AquaSalBitmap()
66 {
67 Destroy();
68 }
69
70 // ------------------------------------------------------------------
71
Create(const ScaledLayer & rLayer,int nBitmapBits,int nX,int nY,int nWidth,int nHeight,bool)72 bool AquaSalBitmap::Create( const ScaledLayer& rLayer, int nBitmapBits,
73 int nX, int nY, int nWidth, int nHeight, bool /*bMirrorVert*/ )
74 {
75 CGLayerRef xLayer = rLayer.get();
76 DBG_ASSERT( xLayer, "AquaSalBitmap::Create() from non-layered context" );
77
78 // sanitize input parameters
79 if( nX < 0 )
80 nWidth += nX, nX = 0;
81 if( nY < 0 )
82 nHeight += nY, nY = 0;
83 const CGSize aLayerSize = rLayer.getPointSize();
84 if( nWidth >= (int)aLayerSize.width - nX )
85 nWidth = (int)aLayerSize.width - nX;
86 if( nHeight >= (int)aLayerSize.height - nY )
87 nHeight = (int)aLayerSize.height - nY;
88 if( (nWidth < 0) || (nHeight < 0) )
89 nWidth = nHeight = 0;
90
91 // initialize properties
92 mnWidth = nWidth;
93 mnHeight = nHeight;
94 mnBits = nBitmapBits ? nBitmapBits : 32;
95
96 // initialize drawing context
97 CreateContext();
98
99 // copy layer content into the bitmap buffer
100 const CGPoint aSrcPoint = CGPointMake( -nX, -nY);
101 ::CGContextDrawLayerInRect( mxGraphicContext, CGRectMake( aSrcPoint.x, aSrcPoint.y, aLayerSize.width, aLayerSize.height ), xLayer );
102 return true;
103 }
104
105 // ------------------------------------------------------------------
106
Create(const Size & rSize,sal_uInt16 nBits,const BitmapPalette & rBitmapPalette)107 bool AquaSalBitmap::Create( const Size& rSize, sal_uInt16 nBits, const BitmapPalette& rBitmapPalette )
108 {
109 if( !isValidBitCount( nBits ) )
110 return false;
111 maPalette = rBitmapPalette;
112 mnBits = nBits;
113 mnWidth = rSize.Width();
114 mnHeight = rSize.Height();
115 return AllocateUserData();
116 }
117
118 // ------------------------------------------------------------------
119
Create(const SalBitmap & rSalBmp)120 bool AquaSalBitmap::Create( const SalBitmap& rSalBmp )
121 {
122 return Create( rSalBmp, rSalBmp.GetBitCount() );
123 }
124
125 // ------------------------------------------------------------------
126
Create(const SalBitmap & rSalBmp,SalGraphics * pGraphics)127 bool AquaSalBitmap::Create( const SalBitmap& rSalBmp, SalGraphics* pGraphics )
128 {
129 return Create( rSalBmp, pGraphics ? pGraphics->GetBitCount() : rSalBmp.GetBitCount() );
130 }
131
132 // ------------------------------------------------------------------
133
Create(const SalBitmap & rSalBmp,sal_uInt16 nNewBitCount)134 bool AquaSalBitmap::Create( const SalBitmap& rSalBmp, sal_uInt16 nNewBitCount )
135 {
136 const AquaSalBitmap& rSourceBitmap = static_cast<const AquaSalBitmap&>(rSalBmp);
137
138 if( isValidBitCount( nNewBitCount ) && rSourceBitmap.maUserBuffer.get() )
139 {
140 mnBits = nNewBitCount;
141 mnWidth = rSourceBitmap.mnWidth;
142 mnHeight = rSourceBitmap.mnHeight;
143 maPalette = rSourceBitmap.maPalette;
144
145 if( AllocateUserData() )
146 {
147 ConvertBitmapData( mnWidth, mnHeight, mnBits, mnBytesPerRow, maPalette, maUserBuffer.get(), rSourceBitmap.mnBits, rSourceBitmap.mnBytesPerRow, rSourceBitmap.maPalette, rSourceBitmap.maUserBuffer.get() );
148 return true;
149 }
150 }
151 return false;
152 }
153
154 // ------------------------------------------------------------------
155
Destroy()156 void AquaSalBitmap::Destroy()
157 {
158 DestroyContext();
159 maUserBuffer.reset();
160 }
161
162 // ------------------------------------------------------------------
163
DestroyContext()164 void AquaSalBitmap::DestroyContext()
165 {
166 CGImageRelease( mxCachedImage );
167 mxCachedImage = NULL;
168
169 if( mxGraphicContext )
170 {
171 CGContextRelease( mxGraphicContext );
172 mxGraphicContext = NULL;
173 maContextBuffer.reset();
174 }
175 }
176
177 // ------------------------------------------------------------------
178
CreateContext()179 bool AquaSalBitmap::CreateContext()
180 {
181 DestroyContext();
182
183 // prepare graphics context
184 // convert image from user input if available
185 const bool bSkipConversion = !maUserBuffer;
186 if( bSkipConversion )
187 AllocateUserData();
188
189 // default to RGBA color space
190 CGColorSpaceRef aCGColorSpace = GetSalData()->mxRGBSpace;
191 CGBitmapInfo aCGBmpInfo = kCGImageAlphaNoneSkipFirst;
192
193 // convert data into something accepted by CGBitmapContextCreate()
194 size_t bitsPerComponent = (mnBits == 16) ? 5 : 8;
195 sal_uInt32 nContextBytesPerRow = mnBytesPerRow;
196 if( (mnBits == 16) || (mnBits == 32) )
197 {
198 // no conversion needed for truecolor
199 maContextBuffer = maUserBuffer;
200 }
201 else if( (mnBits == 8) && maPalette.IsGreyPalette() )
202 {
203 // no conversion needed for grayscale
204 maContextBuffer = maUserBuffer;
205 aCGColorSpace = GetSalData()->mxGraySpace;
206 aCGBmpInfo = kCGImageAlphaNone;
207 bitsPerComponent = mnBits;
208 }
209 // TODO: is special handling for 1bit input buffers worth it?
210 else
211 {
212 // convert user data to 32 bit
213 nContextBytesPerRow = mnWidth << 2;
214 try
215 {
216 maContextBuffer.reset( new sal_uInt8[ mnHeight * nContextBytesPerRow ] );
217
218 if( !bSkipConversion )
219 ConvertBitmapData( mnWidth, mnHeight,
220 32, nContextBytesPerRow, maPalette, maContextBuffer.get(),
221 mnBits, mnBytesPerRow, maPalette, maUserBuffer.get() );
222 }
223 catch( std::bad_alloc )
224 {
225 mxGraphicContext = 0;
226 }
227 }
228
229 if( maContextBuffer.get() )
230 {
231 mxGraphicContext = ::CGBitmapContextCreate( maContextBuffer.get(), mnWidth, mnHeight,
232 bitsPerComponent, nContextBytesPerRow, aCGColorSpace, aCGBmpInfo );
233 }
234
235 if( !mxGraphicContext )
236 maContextBuffer.reset();
237
238 return mxGraphicContext != NULL;
239 }
240
241 // ------------------------------------------------------------------
242
AllocateUserData()243 bool AquaSalBitmap::AllocateUserData()
244 {
245 Destroy();
246
247 if( mnWidth && mnHeight )
248 {
249 mnBytesPerRow = 0;
250
251 switch( mnBits )
252 {
253 case 1: mnBytesPerRow = (mnWidth + 7) >> 3; break;
254 case 4: mnBytesPerRow = (mnWidth + 1) >> 1; break;
255 case 8: mnBytesPerRow = mnWidth; break;
256 case 16: mnBytesPerRow = mnWidth << 1; break;
257 case 24: mnBytesPerRow = (mnWidth << 1) + mnWidth; break;
258 case 32: mnBytesPerRow = mnWidth << 2; break;
259 default:
260 DBG_ERROR("vcl::AquaSalBitmap::AllocateUserData(), illegal bitcount!");
261 }
262 }
263
264 try
265 {
266 if( mnBytesPerRow )
267 maUserBuffer.reset( new sal_uInt8[mnBytesPerRow * mnHeight] );
268 }
269 catch( const std::bad_alloc& )
270 {
271 DBG_ERROR( "vcl::AquaSalBitmap::AllocateUserData: bad alloc" );
272 maUserBuffer.reset();
273 mnBytesPerRow = 0;
274 }
275
276 return maUserBuffer.get() != 0;
277 }
278
279 // ------------------------------------------------------------------
280
281 class ImplPixelFormat
282 {
283 protected:
284 sal_uInt8* pData;
285 public:
286 static ImplPixelFormat* GetFormat( sal_uInt16 nBits, const BitmapPalette& rPalette );
287
StartLine(sal_uInt8 * pLine)288 virtual void StartLine( sal_uInt8* pLine ) { pData = pLine; }
289 virtual void SkipPixel( sal_uInt32 nPixel ) = 0;
290 virtual ColorData ReadPixel() = 0;
291 virtual void WritePixel( ColorData nColor ) = 0;
292 };
293
294 class ImplPixelFormat32 : public ImplPixelFormat
295 // currently ARGB-format for 32bit depth
296 {
297 public:
SkipPixel(sal_uInt32 nPixel)298 virtual void SkipPixel( sal_uInt32 nPixel )
299 {
300 pData += nPixel << 2;
301 }
ReadPixel()302 virtual ColorData ReadPixel()
303 {
304 const ColorData c = RGB_COLORDATA( pData[1], pData[2], pData[3] );
305 pData += 4;
306 return c;
307 }
WritePixel(ColorData nColor)308 virtual void WritePixel( ColorData nColor )
309 {
310 *pData++ = 0;
311 *pData++ = COLORDATA_RED( nColor );
312 *pData++ = COLORDATA_GREEN( nColor );
313 *pData++ = COLORDATA_BLUE( nColor );
314 }
315 };
316
317 class ImplPixelFormat24 : public ImplPixelFormat
318 // currently BGR-format for 24bit depth
319 {
320 public:
SkipPixel(sal_uInt32 nPixel)321 virtual void SkipPixel( sal_uInt32 nPixel )
322 {
323 pData += (nPixel << 1) + nPixel;
324 }
ReadPixel()325 virtual ColorData ReadPixel()
326 {
327 const ColorData c = RGB_COLORDATA( pData[2], pData[1], pData[0] );
328 pData += 3;
329 return c;
330 }
WritePixel(ColorData nColor)331 virtual void WritePixel( ColorData nColor )
332 {
333 *pData++ = COLORDATA_BLUE( nColor );
334 *pData++ = COLORDATA_GREEN( nColor );
335 *pData++ = COLORDATA_RED( nColor );
336 }
337 };
338
339 class ImplPixelFormat16 : public ImplPixelFormat
340 // currently R5G6B5-format for 16bit depth
341 {
342 protected:
343 sal_uInt16* pData16;
344 public:
345
StartLine(sal_uInt8 * pLine)346 virtual void StartLine( sal_uInt8* pLine )
347 {
348 pData16 = (sal_uInt16*)pLine;
349 }
SkipPixel(sal_uInt32 nPixel)350 virtual void SkipPixel( sal_uInt32 nPixel )
351 {
352 pData += nPixel;
353 }
ReadPixel()354 virtual ColorData ReadPixel()
355 {
356 const ColorData c = RGB_COLORDATA( (*pData & 0x7c00) >> 7, (*pData & 0x03e0) >> 2 , (*pData & 0x001f) << 3 );
357 pData++;
358 return c;
359 }
WritePixel(ColorData nColor)360 virtual void WritePixel( ColorData nColor )
361 {
362 *pData++ = ((COLORDATA_RED( nColor ) & 0xf8 ) << 7 ) ||
363 ((COLORDATA_GREEN( nColor ) & 0xf8 ) << 2 ) ||
364 ((COLORDATA_BLUE( nColor ) & 0xf8 ) >> 3 );
365 }
366 };
367
368 class ImplPixelFormat8 : public ImplPixelFormat
369 {
370 private:
371 const BitmapPalette& mrPalette;
372
373 public:
ImplPixelFormat8(const BitmapPalette & rPalette)374 ImplPixelFormat8( const BitmapPalette& rPalette )
375 : mrPalette( rPalette )
376 {
377 }
SkipPixel(sal_uInt32 nPixel)378 virtual void SkipPixel( sal_uInt32 nPixel )
379 {
380 pData += nPixel;
381 }
ReadPixel()382 virtual ColorData ReadPixel()
383 {
384 return mrPalette[ *pData++ ].operator Color().GetColor();
385 }
WritePixel(ColorData nColor)386 virtual void WritePixel( ColorData nColor )
387 {
388 const BitmapColor aColor( COLORDATA_RED( nColor ), COLORDATA_GREEN( nColor ), COLORDATA_BLUE( nColor ) );
389 *pData++ = static_cast< sal_uInt8 >( mrPalette.GetBestIndex( aColor ) );
390 }
391 };
392
393 class ImplPixelFormat4 : public ImplPixelFormat
394 {
395 private:
396 const BitmapPalette& mrPalette;
397 sal_uInt32 mnX;
398 sal_uInt32 mnShift;
399
400 public:
ImplPixelFormat4(const BitmapPalette & rPalette)401 ImplPixelFormat4( const BitmapPalette& rPalette )
402 : mrPalette( rPalette )
403 {
404 }
SkipPixel(sal_uInt32 nPixel)405 virtual void SkipPixel( sal_uInt32 nPixel )
406 {
407 mnX += nPixel;
408 if( (nPixel & 1) )
409 mnShift ^= 4;
410 }
StartLine(sal_uInt8 * pLine)411 virtual void StartLine( sal_uInt8* pLine )
412 {
413 pData = pLine;
414 mnX = 0;
415 mnShift = 4;
416 }
ReadPixel()417 virtual ColorData ReadPixel()
418 {
419 const BitmapColor& rColor = mrPalette[( pData[mnX >> 1] >> mnShift) & 0x0f];
420 mnX++;
421 mnShift ^= 4;
422 return rColor.operator Color().GetColor();
423 }
WritePixel(ColorData nColor)424 virtual void WritePixel( ColorData nColor )
425 {
426 const BitmapColor aColor( COLORDATA_RED( nColor ), COLORDATA_GREEN( nColor ), COLORDATA_BLUE( nColor ) );
427 pData[mnX>>1] &= (0xf0 >> mnShift);
428 pData[mnX>>1] |= (static_cast< sal_uInt8 >( mrPalette.GetBestIndex( aColor ) ) & 0x0f);
429 mnX++;
430 mnShift ^= 4;
431 }
432 };
433
434 class ImplPixelFormat1 : public ImplPixelFormat
435 {
436 private:
437 const BitmapPalette& mrPalette;
438 sal_uInt32 mnX;
439
440 public:
ImplPixelFormat1(const BitmapPalette & rPalette)441 ImplPixelFormat1( const BitmapPalette& rPalette )
442 : mrPalette( rPalette )
443 {
444 }
SkipPixel(sal_uInt32 nPixel)445 virtual void SkipPixel( sal_uInt32 nPixel )
446 {
447 mnX += nPixel;
448 }
StartLine(sal_uInt8 * pLine)449 virtual void StartLine( sal_uInt8* pLine )
450 {
451 pData = pLine;
452 mnX = 0;
453 }
ReadPixel()454 virtual ColorData ReadPixel()
455 {
456 const BitmapColor& rColor = mrPalette[ (pData[mnX >> 3 ] >> ( 7 - ( mnX & 7 ) )) & 1];
457 mnX++;
458 return rColor.operator Color().GetColor();
459 }
WritePixel(ColorData nColor)460 virtual void WritePixel( ColorData nColor )
461 {
462 const BitmapColor aColor( COLORDATA_RED( nColor ), COLORDATA_GREEN( nColor ), COLORDATA_BLUE( nColor ) );
463 if( mrPalette.GetBestIndex( aColor ) & 1 )
464 pData[ mnX >> 3 ] |= 1 << ( 7 - ( mnX & 7 ) );
465 else
466 pData[ mnX >> 3 ] &= ~( 1 << ( 7 - ( mnX & 7 ) ) );
467 mnX++;
468 }
469 };
470
GetFormat(sal_uInt16 nBits,const BitmapPalette & rPalette)471 ImplPixelFormat* ImplPixelFormat::GetFormat( sal_uInt16 nBits, const BitmapPalette& rPalette )
472 {
473 switch( nBits )
474 {
475 case 1: return new ImplPixelFormat1( rPalette );
476 case 4: return new ImplPixelFormat4( rPalette );
477 case 8: return new ImplPixelFormat8( rPalette );
478 case 16: return new ImplPixelFormat16;
479 case 24: return new ImplPixelFormat24;
480 case 32: return new ImplPixelFormat32;
481 }
482
483 return 0;
484 }
485
ConvertBitmapData(sal_uInt32 nWidth,sal_uInt32 nHeight,sal_uInt16 nDestBits,sal_uInt32 nDestBytesPerRow,const BitmapPalette & rDestPalette,sal_uInt8 * pDestData,sal_uInt16 nSrcBits,sal_uInt32 nSrcBytesPerRow,const BitmapPalette & rSrcPalette,sal_uInt8 * pSrcData)486 void AquaSalBitmap::ConvertBitmapData( sal_uInt32 nWidth, sal_uInt32 nHeight,
487 sal_uInt16 nDestBits, sal_uInt32 nDestBytesPerRow, const BitmapPalette& rDestPalette, sal_uInt8* pDestData,
488 sal_uInt16 nSrcBits, sal_uInt32 nSrcBytesPerRow, const BitmapPalette& rSrcPalette, sal_uInt8* pSrcData )
489
490 {
491 if( (nDestBytesPerRow == nSrcBytesPerRow) && (nDestBits == nSrcBits) && ((nSrcBits != 8) || (rDestPalette.operator==( rSrcPalette ))) )
492 {
493 // simple case, same format, so just copy
494 memcpy( pDestData, pSrcData, nHeight * nDestBytesPerRow );
495 return;
496 }
497
498 // try accelerated conversion if possible
499 // TODO: are other truecolor conversions except BGR->ARGB worth it?
500 bool bConverted = false;
501 if( (nSrcBits == 24) && (nDestBits == 32) )
502 {
503 // TODO: extend bmpfast.cxx with a method that can be directly used here
504 BitmapBuffer aSrcBuf;
505 aSrcBuf.mnFormat = BMP_FORMAT_24BIT_TC_BGR;
506 aSrcBuf.mpBits = pSrcData;
507 aSrcBuf.mnBitCount = nSrcBits;
508 aSrcBuf.mnScanlineSize = nSrcBytesPerRow;
509 BitmapBuffer aDstBuf;
510 aDstBuf.mnFormat = BMP_FORMAT_32BIT_TC_ARGB;
511 aDstBuf.mpBits = pDestData;
512 aSrcBuf.mnBitCount = nDestBits;
513 aDstBuf.mnScanlineSize = nDestBytesPerRow;
514
515 aSrcBuf.mnWidth = aDstBuf.mnWidth = nWidth;
516 aSrcBuf.mnHeight = aDstBuf.mnHeight = nHeight;
517
518 SalTwoRect aTwoRects;
519 aTwoRects.mnSrcX = aTwoRects.mnDestX = 0;
520 aTwoRects.mnSrcY = aTwoRects.mnDestY = 0;
521 aTwoRects.mnSrcWidth = aTwoRects.mnDestWidth = mnWidth;
522 aTwoRects.mnSrcHeight = aTwoRects.mnDestHeight = mnHeight;
523 bConverted = ::ImplFastBitmapConversion( aDstBuf, aSrcBuf, aTwoRects );
524 }
525
526 if( !bConverted )
527 {
528 // TODO: this implementation is for clarety, not for speed
529
530 ImplPixelFormat* pD = ImplPixelFormat::GetFormat( nDestBits, rDestPalette );
531 ImplPixelFormat* pS = ImplPixelFormat::GetFormat( nSrcBits, rSrcPalette );
532
533 if( pD && pS )
534 {
535 sal_uInt32 nY = nHeight;
536 while( nY-- )
537 {
538 pD->StartLine( pDestData );
539 pS->StartLine( pSrcData );
540
541 sal_uInt32 nX = nWidth;
542 while( nX-- )
543 pD->WritePixel( pS->ReadPixel() );
544
545 pSrcData += nSrcBytesPerRow;
546 pDestData += nDestBytesPerRow;
547 }
548 }
549 delete pS;
550 delete pD;
551 }
552 }
553
554 // ------------------------------------------------------------------
555
GetSize() const556 Size AquaSalBitmap::GetSize() const
557 {
558 return Size( mnWidth, mnHeight );
559 }
560
561 // ------------------------------------------------------------------
562
GetBitCount() const563 sal_uInt16 AquaSalBitmap::GetBitCount() const
564 {
565 return mnBits;
566 }
567
568 // ------------------------------------------------------------------
569
570 static struct pal_entry
571 {
572 sal_uInt8 mnRed;
573 sal_uInt8 mnGreen;
574 sal_uInt8 mnBlue;
575 }
576 const aImplSalSysPalEntryAry[ 16 ] =
577 {
578 { 0, 0, 0 },
579 { 0, 0, 0x80 },
580 { 0, 0x80, 0 },
581 { 0, 0x80, 0x80 },
582 { 0x80, 0, 0 },
583 { 0x80, 0, 0x80 },
584 { 0x80, 0x80, 0 },
585 { 0x80, 0x80, 0x80 },
586 { 0xC0, 0xC0, 0xC0 },
587 { 0, 0, 0xFF },
588 { 0, 0xFF, 0 },
589 { 0, 0xFF, 0xFF },
590 { 0xFF, 0, 0 },
591 { 0xFF, 0, 0xFF },
592 { 0xFF, 0xFF, 0 },
593 { 0xFF, 0xFF, 0xFF }
594 };
595
GetDefaultPalette(int mnBits,bool bMonochrome)596 const BitmapPalette& GetDefaultPalette( int mnBits, bool bMonochrome )
597 {
598 if( bMonochrome )
599 return Bitmap::GetGreyPalette( 1U << mnBits );
600
601 // at this point we should provide some kind of default palette
602 // since all other platforms do so, too.
603 static bool bDefPalInit = false;
604 static BitmapPalette aDefPalette256;
605 static BitmapPalette aDefPalette16;
606 static BitmapPalette aDefPalette2;
607 if( ! bDefPalInit )
608 {
609 bDefPalInit = true;
610 aDefPalette256.SetEntryCount( 256 );
611 aDefPalette16.SetEntryCount( 16 );
612 aDefPalette2.SetEntryCount( 2 );
613
614 // Standard colors
615 unsigned int i;
616 for( i = 0; i < 16; i++ )
617 {
618 aDefPalette16[i] =
619 aDefPalette256[i] = BitmapColor( aImplSalSysPalEntryAry[i].mnRed,
620 aImplSalSysPalEntryAry[i].mnGreen,
621 aImplSalSysPalEntryAry[i].mnBlue );
622 }
623
624 aDefPalette2[0] = BitmapColor( 0, 0, 0 );
625 aDefPalette2[1] = BitmapColor( 0xff, 0xff, 0xff );
626
627 // own palette (6/6/6)
628 const int DITHER_PAL_STEPS = 6;
629 const sal_uInt8 DITHER_PAL_DELTA = 51;
630 int nB, nG, nR;
631 sal_uInt8 nRed, nGreen, nBlue;
632 for( nB=0, nBlue=0; nB < DITHER_PAL_STEPS; nB++, nBlue += DITHER_PAL_DELTA )
633 {
634 for( nG=0, nGreen=0; nG < DITHER_PAL_STEPS; nG++, nGreen += DITHER_PAL_DELTA )
635 {
636 for( nR=0, nRed=0; nR < DITHER_PAL_STEPS; nR++, nRed += DITHER_PAL_DELTA )
637 {
638 aDefPalette256[ i ] = BitmapColor( nRed, nGreen, nBlue );
639 i++;
640 }
641 }
642 }
643 }
644
645 // now fill in appropriate palette
646 switch( mnBits )
647 {
648 case 1: return aDefPalette2;
649 case 4: return aDefPalette16;
650 case 8: return aDefPalette256;
651 default: break;
652 }
653
654 const static BitmapPalette aEmptyPalette;
655 return aEmptyPalette;
656 }
657
AcquireBuffer(bool)658 BitmapBuffer* AquaSalBitmap::AcquireBuffer( bool /*bReadOnly*/ )
659 {
660 if( !maUserBuffer.get() )
661 // || maContextBuffer.get() && (maUserBuffer.get() != maContextBuffer.get()) )
662 {
663 fprintf(stderr,"ASB::Acq(%dx%d,d=%d)\n",mnWidth,mnHeight,mnBits);
664 // TODO: AllocateUserData();
665 return NULL;
666 }
667
668 BitmapBuffer* pBuffer = new BitmapBuffer;
669 pBuffer->mnWidth = mnWidth;
670 pBuffer->mnHeight = mnHeight;
671 pBuffer->maPalette = maPalette;
672 pBuffer->mnScanlineSize = mnBytesPerRow;
673 pBuffer->mpBits = maUserBuffer.get();
674 pBuffer->mnBitCount = mnBits;
675 switch( mnBits )
676 {
677 case 1: pBuffer->mnFormat = BMP_FORMAT_1BIT_MSB_PAL; break;
678 case 4: pBuffer->mnFormat = BMP_FORMAT_4BIT_MSN_PAL; break;
679 case 8: pBuffer->mnFormat = BMP_FORMAT_8BIT_PAL; break;
680 case 16: pBuffer->mnFormat = BMP_FORMAT_16BIT_TC_MSB_MASK;
681 pBuffer->maColorMask = ColorMask( k16BitRedColorMask, k16BitGreenColorMask, k16BitBlueColorMask );
682 break;
683 case 24: pBuffer->mnFormat = BMP_FORMAT_24BIT_TC_BGR; break;
684 case 32: pBuffer->mnFormat = BMP_FORMAT_32BIT_TC_ARGB;
685 pBuffer->maColorMask = ColorMask( k32BitRedColorMask, k32BitGreenColorMask, k32BitBlueColorMask );
686 break;
687 }
688 pBuffer->mnFormat |= BMP_FORMAT_BOTTOM_UP;
689
690 // some BitmapBuffer users depend on a complete palette
691 if( (mnBits <= 8) && !maPalette )
692 pBuffer->maPalette = GetDefaultPalette( mnBits, true );
693
694 return pBuffer;
695 }
696
697 // ------------------------------------------------------------------
698
ReleaseBuffer(BitmapBuffer * pBuffer,bool bReadOnly)699 void AquaSalBitmap::ReleaseBuffer( BitmapBuffer* pBuffer, bool bReadOnly )
700 {
701 // invalidate graphic context if we have different data
702 if( !bReadOnly )
703 {
704 maPalette = pBuffer->maPalette;
705 if( mxGraphicContext )
706 DestroyContext();
707 }
708
709 delete pBuffer;
710 }
711
712 // ------------------------------------------------------------------
713
CreateCroppedImage(int nX,int nY,int nNewWidth,int nNewHeight) const714 CGImageRef AquaSalBitmap::CreateCroppedImage( int nX, int nY, int nNewWidth, int nNewHeight ) const
715 {
716 if( !mxCachedImage )
717 {
718 if( !mxGraphicContext )
719 if( !const_cast<AquaSalBitmap*>(this)->CreateContext() )
720 return NULL;
721
722 mxCachedImage = CGBitmapContextCreateImage( mxGraphicContext );
723 }
724
725 CGImageRef xCroppedImage = NULL;
726 // short circuit if there is nothing to crop
727 if( !nX && !nY && (mnWidth == nNewWidth) && (mnHeight == nNewHeight) )
728 {
729 xCroppedImage = mxCachedImage;
730 CFRetain( xCroppedImage );
731 }
732 else
733 {
734 nY = mnHeight - (nY + nNewHeight); // adjust for y-mirrored context
735 const CGRect aCropRect = CGRectMake( nX, nY, nNewWidth, nNewHeight);
736 xCroppedImage = CGImageCreateWithImageInRect( mxCachedImage, aCropRect );
737 }
738
739 return xCroppedImage;
740 }
741
742 // ------------------------------------------------------------------
743
CFRTLFree(void *,const void * data,size_t)744 static void CFRTLFree(void* /*info*/, const void* data, size_t /*size*/)
745 {
746 rtl_freeMemory( const_cast<void*>(data) );
747 }
748
CreateWithMask(const AquaSalBitmap & rMask,int nX,int nY,int nWidth,int nHeight) const749 CGImageRef AquaSalBitmap::CreateWithMask( const AquaSalBitmap& rMask,
750 int nX, int nY, int nWidth, int nHeight ) const
751 {
752 CGImageRef xImage( CreateCroppedImage( nX, nY, nWidth, nHeight ) );
753 if( !xImage )
754 return NULL;
755
756 CGImageRef xMask = rMask.CreateCroppedImage( nX, nY, nWidth, nHeight );
757 if( !xMask )
758 return xImage;
759
760 // CGImageCreateWithMask() only likes masks or greyscale images => convert if needed
761 // TODO: isolate in an extra method?
762 if( !CGImageIsMask(xMask) || (CGImageGetColorSpace(xMask) != GetSalData()->mxGraySpace) )
763 {
764 const CGRect xImageRect=CGRectMake( 0, 0, nWidth, nHeight );//the rect has no offset
765
766 // create the alpha mask image fitting our image
767 // TODO: is caching the full mask or the subimage mask worth it?
768 int nMaskBytesPerRow = ((nWidth + 3) & ~3);
769 void* pMaskMem = rtl_allocateMemory( nMaskBytesPerRow * nHeight );
770 CGContextRef xMaskContext = CGBitmapContextCreate( pMaskMem,
771 nWidth, nHeight, 8, nMaskBytesPerRow, GetSalData()->mxGraySpace, kCGImageAlphaNone );
772 CGContextDrawImage( xMaskContext, xImageRect, xMask );
773 CFRelease( xMask );
774 CGDataProviderRef xDataProvider( CGDataProviderCreateWithData( NULL,
775 pMaskMem, nHeight * nMaskBytesPerRow, &CFRTLFree ) );
776 static const CGFloat* pDecode = NULL;
777 xMask = CGImageMaskCreate( nWidth, nHeight, 8, 8, nMaskBytesPerRow, xDataProvider, pDecode, false );
778 CFRelease( xDataProvider );
779 CFRelease( xMaskContext );
780 }
781
782 if( !xMask )
783 return xImage;
784
785 // combine image and alpha mask
786 CGImageRef xMaskedImage = CGImageCreateWithMask( xImage, xMask );
787 CFRelease( xMask );
788 CFRelease( xImage );
789 return xMaskedImage;
790 }
791
792 // ------------------------------------------------------------------
793
794 /** creates an image from the given rectangle, replacing all black pixels with nMaskColor and make all other full transparent */
CreateColorMask(int nX,int nY,int nWidth,int nHeight,SalColor nMaskColor) const795 CGImageRef AquaSalBitmap::CreateColorMask( int nX, int nY, int nWidth, int nHeight, SalColor nMaskColor ) const
796 {
797 CGImageRef xMask = 0;
798 if( maUserBuffer.get() && (nX + nWidth <= mnWidth) && (nY + nHeight <= mnHeight) )
799 {
800 const sal_uInt32 nDestBytesPerRow = nWidth << 2;
801 sal_uInt32* pMaskBuffer = static_cast<sal_uInt32*>( rtl_allocateMemory( nHeight * nDestBytesPerRow ) );
802 sal_uInt32* pDest = pMaskBuffer;
803
804 ImplPixelFormat* pSourcePixels = ImplPixelFormat::GetFormat( mnBits, maPalette );
805
806 if( pMaskBuffer && pSourcePixels )
807 {
808 sal_uInt32 nColor;
809 reinterpret_cast<sal_uInt8*>(&nColor)[0] = 0xff;
810 reinterpret_cast<sal_uInt8*>(&nColor)[1] = SALCOLOR_RED( nMaskColor );
811 reinterpret_cast<sal_uInt8*>(&nColor)[2] = SALCOLOR_GREEN( nMaskColor );
812 reinterpret_cast<sal_uInt8*>(&nColor)[3] = SALCOLOR_BLUE( nMaskColor );
813
814 sal_uInt8* pSource = maUserBuffer.get();
815 if( nY )
816 pSource += nY * mnBytesPerRow;
817
818 int y = nHeight;
819 while( y-- )
820 {
821 pSourcePixels->StartLine( pSource );
822 pSourcePixels->SkipPixel(nX);
823 sal_uInt32 x = nWidth;
824 while( x-- )
825 {
826 *pDest++ = ( pSourcePixels->ReadPixel() == 0 ) ? nColor : 0;
827 }
828 pSource += mnBytesPerRow;
829 }
830
831 CGDataProviderRef xDataProvider( CGDataProviderCreateWithData(NULL, pMaskBuffer, nHeight * nDestBytesPerRow, &CFRTLFree) );
832 xMask = CGImageCreate(nWidth, nHeight, 8, 32, nDestBytesPerRow, GetSalData()->mxRGBSpace, kCGImageAlphaPremultipliedFirst, xDataProvider, NULL, true, kCGRenderingIntentDefault);
833 CFRelease(xDataProvider);
834 }
835 else
836 {
837 free(pMaskBuffer);
838 }
839
840 delete pSourcePixels;
841 }
842 return xMask;
843 }
844
845 // =======================================================================
846
847 /** AquaSalBitmap::GetSystemData Get platform native image data from existing image
848 *
849 * @param rData struct BitmapSystemData, defined in vcl/inc/bitmap.hxx
850 * @return true if successful
851 **/
GetSystemData(BitmapSystemData & rData)852 bool AquaSalBitmap::GetSystemData( BitmapSystemData& rData )
853 {
854 bool bRet = false;
855
856 if( !mxGraphicContext )
857 CreateContext();
858
859 if ( mxGraphicContext )
860 {
861 bRet = true;
862
863 #ifdef CAIRO
864 if ((CGBitmapContextGetBitsPerPixel(mxGraphicContext) == 32) &&
865 (CGBitmapContextGetBitmapInfo(mxGraphicContext) & kCGBitmapByteOrderMask) != kCGBitmapByteOrder32Host) {
866 /**
867 * We need to hack things because VCL does not use kCGBitmapByteOrder32Host, while Cairo requires it.
868 */
869 OSL_TRACE("AquaSalBitmap::%s(): kCGBitmapByteOrder32Host not found => inserting it.",__func__);
870
871 CGImageRef xImage = CGBitmapContextCreateImage (mxGraphicContext);
872
873 // re-create the context with single change: include kCGBitmapByteOrder32Host flag.
874 CGContextRef mxGraphicContextNew = CGBitmapContextCreate( CGBitmapContextGetData(mxGraphicContext),
875 CGBitmapContextGetWidth(mxGraphicContext),
876 CGBitmapContextGetHeight(mxGraphicContext),
877 CGBitmapContextGetBitsPerComponent(mxGraphicContext),
878 CGBitmapContextGetBytesPerRow(mxGraphicContext),
879 CGBitmapContextGetColorSpace(mxGraphicContext),
880 CGBitmapContextGetBitmapInfo(mxGraphicContext) | kCGBitmapByteOrder32Host);
881 CFRelease(mxGraphicContext);
882
883 // Needs to be flipped
884 CGContextSaveGState( mxGraphicContextNew );
885 CGContextTranslateCTM (mxGraphicContextNew, 0, CGBitmapContextGetHeight(mxGraphicContextNew));
886 CGContextScaleCTM (mxGraphicContextNew, 1.0, -1.0);
887
888 CGContextDrawImage(mxGraphicContextNew, CGRectMake( 0, 0, CGImageGetWidth(xImage), CGImageGetHeight(xImage)), xImage);
889
890 // Flip back
891 CGContextRestoreGState( mxGraphicContextNew );
892
893 CGImageRelease( xImage );
894 mxGraphicContext = mxGraphicContextNew;
895 }
896 #endif
897
898 rData.rImageContext = (void *) mxGraphicContext;
899 rData.mnWidth = mnWidth;
900 rData.mnHeight = mnHeight;
901 }
902
903 return bRet;
904 }
905