Update ooo320-m1
[ooovba.git] / vcl / unx / headless / svpbmp.cxx
blobf81c8703fc6adf6d7483f7d6ceb4138144c5f73f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: svpbmp.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "svpbmp.hxx"
33 #include <basegfx/vector/b2ivector.hxx>
34 #include <basegfx/range/b2irange.hxx>
35 #include <basebmp/scanlineformats.hxx>
36 #include <basebmp/color.hxx>
38 #include <vcl/salbtype.hxx>
39 #include <vcl/bitmap.hxx>
41 using namespace basebmp;
42 using namespace basegfx;
44 SvpSalBitmap::~SvpSalBitmap()
48 bool SvpSalBitmap::Create( const Size& rSize,
49 USHORT nBitCount,
50 const BitmapPalette& rPalette )
52 sal_uInt32 nFormat = SVP_DEFAULT_BITMAP_FORMAT;
53 switch( nBitCount )
55 case 1: nFormat = Format::ONE_BIT_MSB_PAL; break;
56 case 4: nFormat = Format::FOUR_BIT_MSB_PAL; break;
57 case 8: nFormat = Format::EIGHT_BIT_PAL; break;
58 #ifdef OSL_BIGENDIAN
59 case 16: nFormat = Format::SIXTEEN_BIT_MSB_TC_MASK; break;
60 #else
61 case 16: nFormat = Format::SIXTEEN_BIT_LSB_TC_MASK; break;
62 #endif
63 case 24: nFormat = Format::TWENTYFOUR_BIT_TC_MASK; break;
64 case 32: nFormat = Format::THIRTYTWO_BIT_TC_MASK; break;
66 B2IVector aSize( rSize.Width(), rSize.Height() );
67 if( aSize.getX() == 0 )
68 aSize.setX( 1 );
69 if( aSize.getY() == 0 )
70 aSize.setY( 1 );
71 if( nBitCount > 8 )
72 m_aBitmap = createBitmapDevice( aSize, false, nFormat );
73 else
75 // prepare palette
76 unsigned int nEntries = 1U << nBitCount;
77 std::vector<basebmp::Color>* pPalette =
78 new std::vector<basebmp::Color>( nEntries, basebmp::Color(COL_WHITE) );
79 unsigned int nColors = rPalette.GetEntryCount();
80 for( unsigned int i = 0; i < nColors; i++ )
82 const BitmapColor& rCol = rPalette[i];
83 (*pPalette)[i] = basebmp::Color( rCol.GetRed(), rCol.GetGreen(), rCol.GetBlue() );
85 m_aBitmap = createBitmapDevice( aSize, false, nFormat,
86 basebmp::RawMemorySharedArray(),
87 basebmp::PaletteMemorySharedVector( pPalette )
90 return true;
93 bool SvpSalBitmap::Create( const SalBitmap& rSalBmp )
95 const SvpSalBitmap& rSrc = static_cast<const SvpSalBitmap&>(rSalBmp);
96 const BitmapDeviceSharedPtr& rSrcBmp = rSrc.getBitmap();
97 if( rSrcBmp.get() )
99 B2IVector aSize = rSrcBmp->getSize();
100 m_aBitmap = cloneBitmapDevice( aSize, rSrcBmp );
101 B2IRange aRect( 0, 0, aSize.getX(), aSize.getY() );
102 m_aBitmap->drawBitmap( rSrcBmp, aRect, aRect, DrawMode_PAINT );
104 else
105 m_aBitmap.reset();
107 return true;
110 bool SvpSalBitmap::Create( const SalBitmap& /*rSalBmp*/,
111 SalGraphics* /*pGraphics*/ )
113 return false;
116 bool SvpSalBitmap::Create( const SalBitmap& /*rSalBmp*/,
117 USHORT /*nNewBitCount*/ )
119 return false;
122 bool SvpSalBitmap::Create( const ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XBitmapCanvas > /*xBitmapCanvas*/, Size& /*rSize*/, bool /*bMask*/ )
124 return false;
127 void SvpSalBitmap::Destroy()
129 m_aBitmap.reset();
132 Size SvpSalBitmap::GetSize() const
134 Size aSize;
135 if( m_aBitmap.get() )
137 B2IVector aVec( m_aBitmap->getSize() );
138 aSize = Size( aVec.getX(), aVec.getY() );
141 return aSize;
144 USHORT SvpSalBitmap::GetBitCount() const
146 USHORT nDepth = 0;
147 if( m_aBitmap.get() )
148 nDepth = getBitCountFromScanlineFormat( m_aBitmap->getScanlineFormat() );
149 return nDepth;
152 BitmapBuffer* SvpSalBitmap::AcquireBuffer( bool )
154 BitmapBuffer* pBuf = NULL;
155 if( m_aBitmap.get() )
157 pBuf = new BitmapBuffer();
158 USHORT nBitCount = 1;
159 switch( m_aBitmap->getScanlineFormat() )
161 case Format::ONE_BIT_MSB_GREY:
162 case Format::ONE_BIT_MSB_PAL:
163 nBitCount = 1;
164 pBuf->mnFormat = BMP_FORMAT_1BIT_MSB_PAL;
165 break;
166 case Format::ONE_BIT_LSB_GREY:
167 case Format::ONE_BIT_LSB_PAL:
168 nBitCount = 1;
169 pBuf->mnFormat = BMP_FORMAT_1BIT_LSB_PAL;
170 break;
171 case Format::FOUR_BIT_MSB_GREY:
172 case Format::FOUR_BIT_MSB_PAL:
173 nBitCount = 4;
174 pBuf->mnFormat = BMP_FORMAT_4BIT_MSN_PAL;
175 break;
176 case Format::FOUR_BIT_LSB_GREY:
177 case Format::FOUR_BIT_LSB_PAL:
178 nBitCount = 4;
179 pBuf->mnFormat = BMP_FORMAT_4BIT_LSN_PAL;
180 break;
181 case Format::EIGHT_BIT_PAL:
182 nBitCount = 8;
183 pBuf->mnFormat = BMP_FORMAT_8BIT_PAL;
184 break;
185 case Format::EIGHT_BIT_GREY:
186 nBitCount = 8;
187 pBuf->mnFormat = BMP_FORMAT_8BIT_PAL;
188 break;
189 case Format::SIXTEEN_BIT_LSB_TC_MASK:
190 nBitCount = 16;
191 pBuf->mnFormat = BMP_FORMAT_16BIT_TC_LSB_MASK;
192 pBuf->maColorMask = ColorMask( 0xf800, 0x07e0, 0x001f );
193 break;
194 case Format::SIXTEEN_BIT_MSB_TC_MASK:
195 nBitCount = 16;
196 pBuf->mnFormat = BMP_FORMAT_16BIT_TC_MSB_MASK;
197 pBuf->maColorMask = ColorMask( 0xf800, 0x07e0, 0x001f );
198 break;
199 case Format::TWENTYFOUR_BIT_TC_MASK:
200 nBitCount = 24;
201 pBuf->mnFormat = BMP_FORMAT_24BIT_TC_BGR;
202 break;
203 case Format::THIRTYTWO_BIT_TC_MASK:
204 nBitCount = 32;
205 pBuf->mnFormat = BMP_FORMAT_32BIT_TC_MASK;
206 #ifdef OSL_BIGENDIAN
207 pBuf->maColorMask = ColorMask( 0x0000ff, 0x00ff00, 0xff0000 );
208 #else
209 pBuf->maColorMask = ColorMask( 0xff0000, 0x00ff00, 0x0000ff );
210 #endif
211 break;
213 default:
214 // this is an error case !!!!!
215 nBitCount = 1;
216 pBuf->mnFormat = BMP_FORMAT_1BIT_MSB_PAL;
217 break;
219 if( m_aBitmap->isTopDown() )
220 pBuf->mnFormat |= BMP_FORMAT_TOP_DOWN;
222 B2IVector aSize = m_aBitmap->getSize();
223 pBuf->mnWidth = aSize.getX();
224 pBuf->mnHeight = aSize.getY();
225 pBuf->mnScanlineSize = m_aBitmap->getScanlineStride();
226 pBuf->mnBitCount = nBitCount;
227 pBuf->mpBits = (BYTE*)m_aBitmap->getBuffer().get();
228 if( nBitCount <= 8 )
230 if( m_aBitmap->getScanlineFormat() == Format::EIGHT_BIT_GREY ||
231 m_aBitmap->getScanlineFormat() == Format::FOUR_BIT_LSB_GREY ||
232 m_aBitmap->getScanlineFormat() == Format::FOUR_BIT_MSB_GREY ||
233 m_aBitmap->getScanlineFormat() == Format::ONE_BIT_LSB_GREY ||
234 m_aBitmap->getScanlineFormat() == Format::ONE_BIT_MSB_GREY
236 pBuf->maPalette = Bitmap::GetGreyPalette( 1U << nBitCount );
237 else
239 basebmp::PaletteMemorySharedVector aPalette = m_aBitmap->getPalette();
240 if( aPalette.get() )
242 unsigned int nColors = aPalette->size();
243 if( nColors > 0 )
245 pBuf->maPalette.SetEntryCount( nColors );
246 for( unsigned int i = 0; i < nColors; i++ )
248 const basebmp::Color& rCol = (*aPalette)[i];
249 pBuf->maPalette[i] = BitmapColor( rCol.getRed(), rCol.getGreen(), rCol.getBlue() );
257 return pBuf;
260 void SvpSalBitmap::ReleaseBuffer( BitmapBuffer* pBuffer, bool bReadOnly )
262 if( !bReadOnly && pBuffer->maPalette.GetEntryCount() )
264 // palette might have changed, clone device (but recycle
265 // memory)
266 USHORT nBitCount = 0;
267 switch( m_aBitmap->getScanlineFormat() )
269 case Format::ONE_BIT_MSB_GREY:
270 // FALLTHROUGH intended
271 case Format::ONE_BIT_MSB_PAL:
272 // FALLTHROUGH intended
273 case Format::ONE_BIT_LSB_GREY:
274 // FALLTHROUGH intended
275 case Format::ONE_BIT_LSB_PAL:
276 nBitCount = 1;
277 break;
279 case Format::FOUR_BIT_MSB_GREY:
280 // FALLTHROUGH intended
281 case Format::FOUR_BIT_MSB_PAL:
282 // FALLTHROUGH intended
283 case Format::FOUR_BIT_LSB_GREY:
284 // FALLTHROUGH intended
285 case Format::FOUR_BIT_LSB_PAL:
286 nBitCount = 4;
287 break;
289 case Format::EIGHT_BIT_PAL:
290 // FALLTHROUGH intended
291 case Format::EIGHT_BIT_GREY:
292 nBitCount = 8;
293 break;
295 default:
296 break;
299 if( nBitCount )
301 sal_uInt32 nEntries = 1U << nBitCount;
303 boost::shared_ptr< std::vector<basebmp::Color> > pPal(
304 new std::vector<basebmp::Color>( nEntries,
305 basebmp::Color(COL_WHITE)));
306 const sal_uInt32 nColors = std::min(
307 (sal_uInt32)pBuffer->maPalette.GetEntryCount(),
308 nEntries);
309 for( sal_uInt32 i = 0; i < nColors; i++ )
311 const BitmapColor& rCol = pBuffer->maPalette[i];
312 (*pPal)[i] = basebmp::Color( rCol.GetRed(), rCol.GetGreen(), rCol.GetBlue() );
315 m_aBitmap = basebmp::createBitmapDevice( m_aBitmap->getSize(),
316 m_aBitmap->isTopDown(),
317 m_aBitmap->getScanlineFormat(),
318 m_aBitmap->getBuffer(),
319 pPal );
323 delete pBuffer;
326 bool SvpSalBitmap::GetSystemData( BitmapSystemData& )
328 return false;