Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / headless / svpbmp.cxx
blob901526ea7fea96aa8ceae51bace257ec7766bb87
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 #ifndef IOS
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,
41 sal_uInt16 nBitCount,
42 const BitmapPalette& rPalette )
44 SAL_INFO( "vcl.headless", "SvpSalBitmap::Create(" << rSize.Width() << "," << rSize.Height() << "," << nBitCount << ")" );
46 SvpSalInstance* pInst = SvpSalInstance::s_pDefaultInstance;
47 assert( pInst );
48 basebmp::Format nFormat = pInst->getFormatForBitCount( nBitCount );
50 B2IVector aSize( rSize.Width(), rSize.Height() );
51 if( aSize.getX() == 0 )
52 aSize.setX( 1 );
53 if( aSize.getY() == 0 )
54 aSize.setY( 1 );
55 sal_Int32 nStride = getBitmapDeviceStrideForWidth(nFormat, aSize.getX());
56 if( nBitCount > 8 )
57 m_aBitmap = createBitmapDevice( aSize, false, nFormat, nStride );
58 else
60 // prepare palette
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 )
75 return true;
78 bool SvpSalBitmap::Create( const SalBitmap& rSalBmp )
80 const SvpSalBitmap& rSrc = static_cast<const SvpSalBitmap&>(rSalBmp);
81 const BitmapDeviceSharedPtr& rSrcBmp = rSrc.getBitmap();
82 if( rSrcBmp.get() )
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 );
89 else
90 m_aBitmap.reset();
92 return true;
95 bool SvpSalBitmap::Create( const SalBitmap& /*rSalBmp*/,
96 SalGraphics* /*pGraphics*/ )
98 return false;
101 bool SvpSalBitmap::Create( const SalBitmap& /*rSalBmp*/,
102 sal_uInt16 /*nNewBitCount*/ )
104 return false;
107 bool SvpSalBitmap::Create( const ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XBitmapCanvas >& /*xBitmapCanvas*/, Size& /*rSize*/, bool /*bMask*/ )
109 return false;
112 void SvpSalBitmap::Destroy()
114 m_aBitmap.reset();
117 Size SvpSalBitmap::GetSize() const
119 Size aSize;
120 if( m_aBitmap.get() )
122 B2IVector aVec( m_aBitmap->getSize() );
123 aSize = Size( aVec.getX(), aVec.getY() );
126 return aSize;
129 sal_uInt16 SvpSalBitmap::GetBitCount() const
131 sal_uInt16 nDepth = 0;
132 if( m_aBitmap.get() )
133 nDepth = getBitCountFromScanlineFormat( m_aBitmap->getScanlineFormat() );
134 return nDepth;
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:
148 nBitCount = 1;
149 pBuf->mnFormat = BMP_FORMAT_1BIT_MSB_PAL;
150 break;
151 case FORMAT_ONE_BIT_LSB_GREY:
152 case FORMAT_ONE_BIT_LSB_PAL:
153 nBitCount = 1;
154 pBuf->mnFormat = BMP_FORMAT_1BIT_LSB_PAL;
155 break;
156 case FORMAT_FOUR_BIT_MSB_GREY:
157 case FORMAT_FOUR_BIT_MSB_PAL:
158 nBitCount = 4;
159 pBuf->mnFormat = BMP_FORMAT_4BIT_MSN_PAL;
160 break;
161 case FORMAT_FOUR_BIT_LSB_GREY:
162 case FORMAT_FOUR_BIT_LSB_PAL:
163 nBitCount = 4;
164 pBuf->mnFormat = BMP_FORMAT_4BIT_LSN_PAL;
165 break;
166 case FORMAT_EIGHT_BIT_PAL:
167 nBitCount = 8;
168 pBuf->mnFormat = BMP_FORMAT_8BIT_PAL;
169 break;
170 case FORMAT_EIGHT_BIT_GREY:
171 nBitCount = 8;
172 pBuf->mnFormat = BMP_FORMAT_8BIT_PAL;
173 break;
174 case FORMAT_SIXTEEN_BIT_LSB_TC_MASK:
175 nBitCount = 16;
176 pBuf->mnFormat = BMP_FORMAT_16BIT_TC_LSB_MASK;
177 pBuf->maColorMask = ColorMask( 0xf800, 0x07e0, 0x001f );
178 break;
179 case FORMAT_SIXTEEN_BIT_MSB_TC_MASK:
180 nBitCount = 16;
181 pBuf->mnFormat = BMP_FORMAT_16BIT_TC_MSB_MASK;
182 pBuf->maColorMask = ColorMask( 0xf800, 0x07e0, 0x001f );
183 break;
184 case FORMAT_TWENTYFOUR_BIT_TC_MASK:
185 nBitCount = 24;
186 pBuf->mnFormat = BMP_FORMAT_24BIT_TC_BGR;
187 break;
188 case FORMAT_THIRTYTWO_BIT_TC_MASK_BGRX:
189 nBitCount = 32;
190 pBuf->mnFormat = BMP_FORMAT_32BIT_TC_MASK;
191 #ifdef OSL_BIGENDIAN
192 pBuf->maColorMask = ColorMask( 0x0000ff00, 0x00ff0000, 0xff000000 );
193 #else
194 pBuf->maColorMask = ColorMask( 0x00ff0000, 0x0000ff00, 0x000000ff );
195 #endif
196 break;
197 case FORMAT_THIRTYTWO_BIT_TC_MASK_BGRA:
198 nBitCount = 32;
199 pBuf->mnFormat = BMP_FORMAT_32BIT_TC_MASK;
200 #ifdef OSL_BIGENDIAN
201 pBuf->maColorMask = ColorMask( 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff );
202 #else
203 pBuf->maColorMask = ColorMask( 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 );
204 #endif
205 break;
206 case FORMAT_THIRTYTWO_BIT_TC_MASK_ARGB:
207 nBitCount = 32;
208 pBuf->mnFormat = BMP_FORMAT_32BIT_TC_MASK;
209 #ifdef OSL_BIGENDIAN
210 pBuf->maColorMask = ColorMask( 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 );
211 #else
212 pBuf->maColorMask = ColorMask( 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff );
213 #endif
214 break;
215 case FORMAT_THIRTYTWO_BIT_TC_MASK_ABGR:
216 nBitCount = 32;
217 pBuf->mnFormat = BMP_FORMAT_32BIT_TC_MASK;
218 #ifdef OSL_BIGENDIAN
219 pBuf->maColorMask = ColorMask( 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 );
220 #else
221 pBuf->maColorMask = ColorMask( 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff );
222 #endif
223 break;
224 case FORMAT_THIRTYTWO_BIT_TC_MASK_RGBA:
225 nBitCount = 32;
226 pBuf->mnFormat = BMP_FORMAT_32BIT_TC_MASK;
227 #ifdef OSL_BIGENDIAN
228 pBuf->maColorMask = ColorMask( 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff );
229 #else
230 pBuf->maColorMask = ColorMask( 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 );
231 #endif
232 break;
234 default:
235 // this is an error case !!!!!
236 nBitCount = 1;
237 pBuf->mnFormat = BMP_FORMAT_1BIT_MSB_PAL;
238 break;
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();
249 if( nBitCount <= 8 )
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 );
258 else
260 basebmp::PaletteMemorySharedVector aPalette = m_aBitmap->getPalette();
261 if( aPalette.get() )
263 unsigned int nColors = aPalette->size();
264 if( nColors > 0 )
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() );
278 return pBuf;
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
286 // memory)
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:
297 nBitCount = 1;
298 break;
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:
307 nBitCount = 4;
308 break;
310 case FORMAT_EIGHT_BIT_PAL:
311 // FALLTHROUGH intended
312 case FORMAT_EIGHT_BIT_GREY:
313 nBitCount = 8;
314 break;
316 default:
317 break;
320 if( nBitCount )
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(),
329 nEntries);
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(),
341 pPal );
345 delete pBuffer;
348 bool SvpSalBitmap::GetSystemData( BitmapSystemData& )
350 return false;
353 bool SvpSalBitmap::Crop( const Rectangle& /*rRectPixel*/ )
355 return false;
358 bool SvpSalBitmap::Erase( const ::Color& /*rFillColor*/ )
360 return false;
363 bool SvpSalBitmap::Scale( const double& /*rScaleX*/, const double& /*rScaleY*/, BmpScaleFlag /*nScaleFlag*/ )
365 return false;
368 bool SvpSalBitmap::Replace( const ::Color& /*rSearchColor*/, const ::Color& /*rReplaceColor*/, sal_uLong /*nTol*/ )
370 return false;
373 sal_uInt32 SvpSalBitmap::getBitCountFromScanlineFormat( basebmp::Format nFormat )
375 sal_uInt32 nBitCount = 1;
376 switch( nFormat )
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:
382 nBitCount = 1;
383 break;
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:
388 nBitCount = 4;
389 break;
390 case FORMAT_EIGHT_BIT_PAL:
391 case FORMAT_EIGHT_BIT_GREY:
392 nBitCount = 8;
393 break;
394 case FORMAT_SIXTEEN_BIT_LSB_TC_MASK:
395 case FORMAT_SIXTEEN_BIT_MSB_TC_MASK:
396 nBitCount = 16;
397 break;
398 case FORMAT_TWENTYFOUR_BIT_TC_MASK:
399 nBitCount = 24;
400 break;
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:
406 nBitCount = 32;
407 break;
408 default:
409 OSL_FAIL( "unsupported basebmp format" );
410 break;
412 return nBitCount;
415 #endif
417 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */