1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include "headless/svpbmp.hxx"
23 #include "headless/svpinst.hxx"
25 #include <basegfx/vector/b2ivector.hxx>
26 #include <basegfx/range/b2ibox.hxx>
27 #include <basebmp/scanlineformats.hxx>
28 #include <basebmp/color.hxx>
30 #include <vcl/salbtype.hxx>
31 #include <vcl/bitmap.hxx>
33 using namespace basebmp
;
34 using namespace basegfx
;
36 SvpSalBitmap::~SvpSalBitmap()
40 bool SvpSalBitmap::Create( const Size
& rSize
,
42 const BitmapPalette
& rPalette
)
44 SAL_INFO( "vcl.headless", "SvpSalBitmap::Create(" << rSize
.Width() << "," << rSize
.Height() << "," << nBitCount
<< ")" );
46 SvpSalInstance
* pInst
= SvpSalInstance::s_pDefaultInstance
;
48 basebmp::Format nFormat
= pInst
->getFormatForBitCount( nBitCount
);
50 B2IVector
aSize( rSize
.Width(), rSize
.Height() );
51 if( aSize
.getX() == 0 )
53 if( aSize
.getY() == 0 )
55 sal_Int32 nStride
= getBitmapDeviceStrideForWidth(nFormat
, aSize
.getX());
57 m_aBitmap
= createBitmapDevice( aSize
, false, nFormat
, nStride
);
61 unsigned int nEntries
= 1U << nBitCount
;
62 std::vector
<basebmp::Color
>* pPalette
=
63 new std::vector
<basebmp::Color
>( nEntries
, basebmp::Color(COL_WHITE
) );
64 unsigned int nColors
= rPalette
.GetEntryCount();
65 for( unsigned int i
= 0; i
< nColors
; i
++ )
67 const BitmapColor
& rCol
= rPalette
[i
];
68 (*pPalette
)[i
] = basebmp::Color( rCol
.GetRed(), rCol
.GetGreen(), rCol
.GetBlue() );
70 m_aBitmap
= createBitmapDevice( aSize
, false, nFormat
, nStride
,
71 basebmp::RawMemorySharedArray(),
72 basebmp::PaletteMemorySharedVector( pPalette
)
78 bool SvpSalBitmap::Create( const SalBitmap
& rSalBmp
)
80 const SvpSalBitmap
& rSrc
= static_cast<const SvpSalBitmap
&>(rSalBmp
);
81 const BitmapDeviceSharedPtr
& rSrcBmp
= rSrc
.getBitmap();
84 B2IVector aSize
= rSrcBmp
->getSize();
85 m_aBitmap
= cloneBitmapDevice( aSize
, rSrcBmp
);
86 B2IBox
aRect( 0, 0, aSize
.getX(), aSize
.getY() );
87 m_aBitmap
->drawBitmap( rSrcBmp
, aRect
, aRect
, DrawMode_PAINT
);
95 bool SvpSalBitmap::Create( const SalBitmap
& /*rSalBmp*/,
96 SalGraphics
* /*pGraphics*/ )
101 bool SvpSalBitmap::Create( const SalBitmap
& /*rSalBmp*/,
102 sal_uInt16
/*nNewBitCount*/ )
107 bool SvpSalBitmap::Create( const ::com::sun::star::uno::Reference
< ::com::sun::star::rendering::XBitmapCanvas
>& /*xBitmapCanvas*/, Size
& /*rSize*/, bool /*bMask*/ )
112 void SvpSalBitmap::Destroy()
117 Size
SvpSalBitmap::GetSize() const
120 if( m_aBitmap
.get() )
122 B2IVector
aVec( m_aBitmap
->getSize() );
123 aSize
= Size( aVec
.getX(), aVec
.getY() );
129 sal_uInt16
SvpSalBitmap::GetBitCount() const
131 sal_uInt16 nDepth
= 0;
132 if( m_aBitmap
.get() )
133 nDepth
= getBitCountFromScanlineFormat( m_aBitmap
->getScanlineFormat() );
137 BitmapBuffer
* SvpSalBitmap::AcquireBuffer( BitmapAccessMode
)
139 BitmapBuffer
* pBuf
= NULL
;
140 if( m_aBitmap
.get() )
142 pBuf
= new BitmapBuffer();
143 sal_uInt16 nBitCount
= 1;
144 switch( m_aBitmap
->getScanlineFormat() )
146 case FORMAT_ONE_BIT_MSB_GREY
:
147 case FORMAT_ONE_BIT_MSB_PAL
:
149 pBuf
->mnFormat
= BMP_FORMAT_1BIT_MSB_PAL
;
151 case FORMAT_ONE_BIT_LSB_GREY
:
152 case FORMAT_ONE_BIT_LSB_PAL
:
154 pBuf
->mnFormat
= BMP_FORMAT_1BIT_LSB_PAL
;
156 case FORMAT_FOUR_BIT_MSB_GREY
:
157 case FORMAT_FOUR_BIT_MSB_PAL
:
159 pBuf
->mnFormat
= BMP_FORMAT_4BIT_MSN_PAL
;
161 case FORMAT_FOUR_BIT_LSB_GREY
:
162 case FORMAT_FOUR_BIT_LSB_PAL
:
164 pBuf
->mnFormat
= BMP_FORMAT_4BIT_LSN_PAL
;
166 case FORMAT_EIGHT_BIT_PAL
:
168 pBuf
->mnFormat
= BMP_FORMAT_8BIT_PAL
;
170 case FORMAT_EIGHT_BIT_GREY
:
172 pBuf
->mnFormat
= BMP_FORMAT_8BIT_PAL
;
174 case FORMAT_SIXTEEN_BIT_LSB_TC_MASK
:
176 pBuf
->mnFormat
= BMP_FORMAT_16BIT_TC_LSB_MASK
;
177 pBuf
->maColorMask
= ColorMask( 0xf800, 0x07e0, 0x001f );
179 case FORMAT_SIXTEEN_BIT_MSB_TC_MASK
:
181 pBuf
->mnFormat
= BMP_FORMAT_16BIT_TC_MSB_MASK
;
182 pBuf
->maColorMask
= ColorMask( 0xf800, 0x07e0, 0x001f );
184 case FORMAT_TWENTYFOUR_BIT_TC_MASK
:
186 pBuf
->mnFormat
= BMP_FORMAT_24BIT_TC_BGR
;
188 case FORMAT_THIRTYTWO_BIT_TC_MASK_BGRX
:
190 pBuf
->mnFormat
= BMP_FORMAT_32BIT_TC_MASK
;
192 pBuf
->maColorMask
= ColorMask( 0x0000ff00, 0x00ff0000, 0xff000000 );
194 pBuf
->maColorMask
= ColorMask( 0x00ff0000, 0x0000ff00, 0x000000ff );
197 case FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA
:
199 pBuf
->mnFormat
= BMP_FORMAT_32BIT_TC_MASK
;
201 pBuf
->maColorMask
= ColorMask( 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff );
203 pBuf
->maColorMask
= ColorMask( 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 );
206 case FORMAT_THIRTYTWO_BIT_TC_MASK_ARGB
:
208 pBuf
->mnFormat
= BMP_FORMAT_32BIT_TC_MASK
;
210 pBuf
->maColorMask
= ColorMask( 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 );
212 pBuf
->maColorMask
= ColorMask( 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff );
215 case FORMAT_THIRTYTWO_BIT_TC_MASK_ABGR
:
217 pBuf
->mnFormat
= BMP_FORMAT_32BIT_TC_MASK
;
219 pBuf
->maColorMask
= ColorMask( 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 );
221 pBuf
->maColorMask
= ColorMask( 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff );
224 case FORMAT_THIRTYTWO_BIT_TC_MASK_RGBA
:
226 pBuf
->mnFormat
= BMP_FORMAT_32BIT_TC_MASK
;
228 pBuf
->maColorMask
= ColorMask( 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff );
230 pBuf
->maColorMask
= ColorMask( 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 );
235 // this is an error case !!!!!
237 pBuf
->mnFormat
= BMP_FORMAT_1BIT_MSB_PAL
;
240 if( m_aBitmap
->isTopDown() )
241 pBuf
->mnFormat
|= BMP_FORMAT_TOP_DOWN
;
243 B2IVector aSize
= m_aBitmap
->getSize();
244 pBuf
->mnWidth
= aSize
.getX();
245 pBuf
->mnHeight
= aSize
.getY();
246 pBuf
->mnScanlineSize
= m_aBitmap
->getScanlineStride();
247 pBuf
->mnBitCount
= nBitCount
;
248 pBuf
->mpBits
= (sal_uInt8
*)m_aBitmap
->getBuffer().get();
251 if( m_aBitmap
->getScanlineFormat() == FORMAT_EIGHT_BIT_GREY
||
252 m_aBitmap
->getScanlineFormat() == FORMAT_FOUR_BIT_LSB_GREY
||
253 m_aBitmap
->getScanlineFormat() == FORMAT_FOUR_BIT_MSB_GREY
||
254 m_aBitmap
->getScanlineFormat() == FORMAT_ONE_BIT_LSB_GREY
||
255 m_aBitmap
->getScanlineFormat() == FORMAT_ONE_BIT_MSB_GREY
257 pBuf
->maPalette
= Bitmap::GetGreyPalette( 1U << nBitCount
);
260 basebmp::PaletteMemorySharedVector aPalette
= m_aBitmap
->getPalette();
263 unsigned int nColors
= aPalette
->size();
266 pBuf
->maPalette
.SetEntryCount( nColors
);
267 for( unsigned int i
= 0; i
< nColors
; i
++ )
269 const basebmp::Color
& rCol
= (*aPalette
)[i
];
270 pBuf
->maPalette
[i
] = BitmapColor( rCol
.getRed(), rCol
.getGreen(), rCol
.getBlue() );
281 void SvpSalBitmap::ReleaseBuffer( BitmapBuffer
* pBuffer
, BitmapAccessMode nMode
)
283 if( nMode
== BITMAP_WRITE_ACCESS
&& pBuffer
->maPalette
.GetEntryCount() )
285 // palette might have changed, clone device (but recycle
287 sal_uInt16 nBitCount
= 0;
288 switch( m_aBitmap
->getScanlineFormat() )
290 case FORMAT_ONE_BIT_MSB_GREY
:
291 // FALLTHROUGH intended
292 case FORMAT_ONE_BIT_MSB_PAL
:
293 // FALLTHROUGH intended
294 case FORMAT_ONE_BIT_LSB_GREY
:
295 // FALLTHROUGH intended
296 case FORMAT_ONE_BIT_LSB_PAL
:
300 case FORMAT_FOUR_BIT_MSB_GREY
:
301 // FALLTHROUGH intended
302 case FORMAT_FOUR_BIT_MSB_PAL
:
303 // FALLTHROUGH intended
304 case FORMAT_FOUR_BIT_LSB_GREY
:
305 // FALLTHROUGH intended
306 case FORMAT_FOUR_BIT_LSB_PAL
:
310 case FORMAT_EIGHT_BIT_PAL
:
311 // FALLTHROUGH intended
312 case FORMAT_EIGHT_BIT_GREY
:
322 sal_uInt32 nEntries
= 1U << nBitCount
;
324 boost::shared_ptr
< std::vector
<basebmp::Color
> > pPal(
325 new std::vector
<basebmp::Color
>( nEntries
,
326 basebmp::Color(COL_WHITE
)));
327 const sal_uInt32 nColors
= std::min(
328 (sal_uInt32
)pBuffer
->maPalette
.GetEntryCount(),
330 for( sal_uInt32 i
= 0; i
< nColors
; i
++ )
332 const BitmapColor
& rCol
= pBuffer
->maPalette
[i
];
333 (*pPal
)[i
] = basebmp::Color( rCol
.GetRed(), rCol
.GetGreen(), rCol
.GetBlue() );
336 m_aBitmap
= basebmp::createBitmapDevice( m_aBitmap
->getSize(),
337 m_aBitmap
->isTopDown(),
338 m_aBitmap
->getScanlineFormat(),
339 m_aBitmap
->getScanlineStride(),
340 m_aBitmap
->getBuffer(),
348 bool SvpSalBitmap::GetSystemData( BitmapSystemData
& )
353 bool SvpSalBitmap::Crop( const Rectangle
& /*rRectPixel*/ )
358 bool SvpSalBitmap::Erase( const ::Color
& /*rFillColor*/ )
363 bool SvpSalBitmap::Scale( const double& /*rScaleX*/, const double& /*rScaleY*/, BmpScaleFlag
/*nScaleFlag*/ )
368 bool SvpSalBitmap::Replace( const ::Color
& /*rSearchColor*/, const ::Color
& /*rReplaceColor*/, sal_uLong
/*nTol*/ )
373 sal_uInt32
SvpSalBitmap::getBitCountFromScanlineFormat( basebmp::Format nFormat
)
375 sal_uInt32 nBitCount
= 1;
378 case FORMAT_ONE_BIT_MSB_GREY
:
379 case FORMAT_ONE_BIT_LSB_GREY
:
380 case FORMAT_ONE_BIT_MSB_PAL
:
381 case FORMAT_ONE_BIT_LSB_PAL
:
384 case FORMAT_FOUR_BIT_MSB_GREY
:
385 case FORMAT_FOUR_BIT_LSB_GREY
:
386 case FORMAT_FOUR_BIT_MSB_PAL
:
387 case FORMAT_FOUR_BIT_LSB_PAL
:
390 case FORMAT_EIGHT_BIT_PAL
:
391 case FORMAT_EIGHT_BIT_GREY
:
394 case FORMAT_SIXTEEN_BIT_LSB_TC_MASK
:
395 case FORMAT_SIXTEEN_BIT_MSB_TC_MASK
:
398 case FORMAT_TWENTYFOUR_BIT_TC_MASK
:
401 case FORMAT_THIRTYTWO_BIT_TC_MASK_BGRX
:
402 case FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA
:
403 case FORMAT_THIRTYTWO_BIT_TC_MASK_ARGB
:
404 case FORMAT_THIRTYTWO_BIT_TC_MASK_ABGR
:
405 case FORMAT_THIRTYTWO_BIT_TC_MASK_RGBA
:
409 OSL_FAIL( "unsupported basebmp format" );
417 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */