Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "headless/svpbmp.hxx"
21 :
22 : #include <basegfx/vector/b2ivector.hxx>
23 : #include <basegfx/range/b2ibox.hxx>
24 : #include <basebmp/scanlineformats.hxx>
25 : #include <basebmp/color.hxx>
26 :
27 : #include <vcl/salbtype.hxx>
28 : #include <vcl/bitmap.hxx>
29 :
30 : using namespace basebmp;
31 : using namespace basegfx;
32 :
33 867296 : SvpSalBitmap::~SvpSalBitmap()
34 : {
35 867296 : }
36 :
37 86453 : bool SvpSalBitmap::Create( const Size& rSize,
38 : sal_uInt16 nBitCount,
39 : const BitmapPalette& rPalette )
40 : {
41 86453 : sal_uInt32 nFormat = SVP_DEFAULT_BITMAP_FORMAT;
42 : SAL_INFO( "vcl.headless", "SvpSalBitmap::Create(" << rSize.Width() << "," << rSize.Height() << "," << nBitCount << ")" );
43 86453 : switch( nBitCount )
44 : {
45 714 : case 1: nFormat = Format::ONE_BIT_MSB_PAL; break;
46 165 : case 4: nFormat = Format::FOUR_BIT_MSB_PAL; break;
47 51152 : case 8: nFormat = Format::EIGHT_BIT_PAL; break;
48 : #ifdef OSL_BIGENDIAN
49 : case 16: nFormat = Format::SIXTEEN_BIT_MSB_TC_MASK; break;
50 : #else
51 0 : case 16: nFormat = Format::SIXTEEN_BIT_LSB_TC_MASK; break;
52 : #endif
53 34422 : case 24: nFormat = Format::TWENTYFOUR_BIT_TC_MASK; break;
54 : #if defined(ANDROID) || defined(IOS)
55 : case 32: nFormat = Format::THIRTYTWO_BIT_TC_MASK_RGBA; break;
56 : #else
57 0 : case 32: nFormat = Format::THIRTYTWO_BIT_TC_MASK; break;
58 : #endif
59 : }
60 86453 : B2IVector aSize( rSize.Width(), rSize.Height() );
61 86453 : if( aSize.getX() == 0 )
62 0 : aSize.setX( 1 );
63 86453 : if( aSize.getY() == 0 )
64 0 : aSize.setY( 1 );
65 86453 : if( nBitCount > 8 )
66 34422 : m_aBitmap = createBitmapDevice( aSize, false, nFormat );
67 : else
68 : {
69 : // prepare palette
70 52031 : unsigned int nEntries = 1U << nBitCount;
71 : std::vector<basebmp::Color>* pPalette =
72 52031 : new std::vector<basebmp::Color>( nEntries, basebmp::Color(COL_WHITE) );
73 52031 : unsigned int nColors = rPalette.GetEntryCount();
74 13030282 : for( unsigned int i = 0; i < nColors; i++ )
75 : {
76 12978251 : const BitmapColor& rCol = rPalette[i];
77 12978251 : (*pPalette)[i] = basebmp::Color( rCol.GetRed(), rCol.GetGreen(), rCol.GetBlue() );
78 : }
79 104062 : m_aBitmap = createBitmapDevice( aSize, false, nFormat,
80 : basebmp::RawMemorySharedArray(),
81 : basebmp::PaletteMemorySharedVector( pPalette )
82 52031 : );
83 : }
84 86453 : return true;
85 : }
86 :
87 108786 : bool SvpSalBitmap::Create( const SalBitmap& rSalBmp )
88 : {
89 108786 : const SvpSalBitmap& rSrc = static_cast<const SvpSalBitmap&>(rSalBmp);
90 108786 : const BitmapDeviceSharedPtr& rSrcBmp = rSrc.getBitmap();
91 108786 : if( rSrcBmp.get() )
92 : {
93 108786 : B2IVector aSize = rSrcBmp->getSize();
94 108786 : m_aBitmap = cloneBitmapDevice( aSize, rSrcBmp );
95 108786 : B2IBox aRect( 0, 0, aSize.getX(), aSize.getY() );
96 108786 : m_aBitmap->drawBitmap( rSrcBmp, aRect, aRect, DrawMode_PAINT );
97 : }
98 : else
99 0 : m_aBitmap.reset();
100 :
101 108786 : return true;
102 : }
103 :
104 1 : bool SvpSalBitmap::Create( const SalBitmap& /*rSalBmp*/,
105 : SalGraphics* /*pGraphics*/ )
106 : {
107 1 : return false;
108 : }
109 :
110 4 : bool SvpSalBitmap::Create( const SalBitmap& /*rSalBmp*/,
111 : sal_uInt16 /*nNewBitCount*/ )
112 : {
113 4 : return false;
114 : }
115 :
116 0 : bool SvpSalBitmap::Create( const ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XBitmapCanvas > /*xBitmapCanvas*/, Size& /*rSize*/, bool /*bMask*/ )
117 : {
118 0 : return false;
119 : }
120 :
121 0 : void SvpSalBitmap::Destroy()
122 : {
123 0 : m_aBitmap.reset();
124 0 : }
125 :
126 789647 : Size SvpSalBitmap::GetSize() const
127 : {
128 789647 : Size aSize;
129 789647 : if( m_aBitmap.get() )
130 : {
131 789647 : B2IVector aVec( m_aBitmap->getSize() );
132 789647 : aSize = Size( aVec.getX(), aVec.getY() );
133 : }
134 :
135 789647 : return aSize;
136 : }
137 :
138 258185 : sal_uInt16 SvpSalBitmap::GetBitCount() const
139 : {
140 258185 : sal_uInt16 nDepth = 0;
141 258185 : if( m_aBitmap.get() )
142 258181 : nDepth = getBitCountFromScanlineFormat( m_aBitmap->getScanlineFormat() );
143 258185 : return nDepth;
144 : }
145 :
146 581741 : BitmapBuffer* SvpSalBitmap::AcquireBuffer( bool )
147 : {
148 581741 : BitmapBuffer* pBuf = NULL;
149 581741 : if( m_aBitmap.get() )
150 : {
151 581737 : pBuf = new BitmapBuffer();
152 581737 : sal_uInt16 nBitCount = 1;
153 581737 : switch( m_aBitmap->getScanlineFormat() )
154 : {
155 : case Format::ONE_BIT_MSB_GREY:
156 : case Format::ONE_BIT_MSB_PAL:
157 3786 : nBitCount = 1;
158 3786 : pBuf->mnFormat = BMP_FORMAT_1BIT_MSB_PAL;
159 3786 : break;
160 : case Format::ONE_BIT_LSB_GREY:
161 : case Format::ONE_BIT_LSB_PAL:
162 0 : nBitCount = 1;
163 0 : pBuf->mnFormat = BMP_FORMAT_1BIT_LSB_PAL;
164 0 : break;
165 : case Format::FOUR_BIT_MSB_GREY:
166 : case Format::FOUR_BIT_MSB_PAL:
167 304 : nBitCount = 4;
168 304 : pBuf->mnFormat = BMP_FORMAT_4BIT_MSN_PAL;
169 304 : break;
170 : case Format::FOUR_BIT_LSB_GREY:
171 : case Format::FOUR_BIT_LSB_PAL:
172 0 : nBitCount = 4;
173 0 : pBuf->mnFormat = BMP_FORMAT_4BIT_LSN_PAL;
174 0 : break;
175 : case Format::EIGHT_BIT_PAL:
176 329074 : nBitCount = 8;
177 329074 : pBuf->mnFormat = BMP_FORMAT_8BIT_PAL;
178 329074 : break;
179 : case Format::EIGHT_BIT_GREY:
180 0 : nBitCount = 8;
181 0 : pBuf->mnFormat = BMP_FORMAT_8BIT_PAL;
182 0 : break;
183 : case Format::SIXTEEN_BIT_LSB_TC_MASK:
184 0 : nBitCount = 16;
185 0 : pBuf->mnFormat = BMP_FORMAT_16BIT_TC_LSB_MASK;
186 0 : pBuf->maColorMask = ColorMask( 0xf800, 0x07e0, 0x001f );
187 0 : break;
188 : case Format::SIXTEEN_BIT_MSB_TC_MASK:
189 0 : nBitCount = 16;
190 0 : pBuf->mnFormat = BMP_FORMAT_16BIT_TC_MSB_MASK;
191 0 : pBuf->maColorMask = ColorMask( 0xf800, 0x07e0, 0x001f );
192 0 : break;
193 : case Format::TWENTYFOUR_BIT_TC_MASK:
194 248573 : nBitCount = 24;
195 248573 : pBuf->mnFormat = BMP_FORMAT_24BIT_TC_BGR;
196 248573 : break;
197 : case Format::THIRTYTWO_BIT_TC_MASK_BGRA:
198 0 : nBitCount = 32;
199 0 : pBuf->mnFormat = BMP_FORMAT_32BIT_TC_MASK;
200 : #ifdef OSL_BIGENDIAN
201 : pBuf->maColorMask = ColorMask( 0x0000ff00, 0x00ff0000, 0xff000000 );
202 : #else
203 0 : pBuf->maColorMask = ColorMask( 0x00ff0000, 0x0000ff00, 0x000000ff );
204 : #endif
205 0 : break;
206 : case Format::THIRTYTWO_BIT_TC_MASK_ARGB:
207 0 : nBitCount = 32;
208 0 : pBuf->mnFormat = BMP_FORMAT_32BIT_TC_MASK;
209 : #ifdef OSL_BIGENDIAN
210 : pBuf->maColorMask = ColorMask( 0x00ff0000, 0x0000ff00, 0x000000ff );
211 : #else
212 0 : pBuf->maColorMask = ColorMask( 0x0000ff00, 0x00ff0000, 0xff000000 );
213 : #endif
214 0 : break;
215 : case Format::THIRTYTWO_BIT_TC_MASK_ABGR:
216 0 : nBitCount = 32;
217 0 : pBuf->mnFormat = BMP_FORMAT_32BIT_TC_MASK;
218 : #ifdef OSL_BIGENDIAN
219 : pBuf->maColorMask = ColorMask( 0x000000ff, 0x0000ff00, 0x00ff0000 );
220 : #else
221 0 : pBuf->maColorMask = ColorMask( 0xff000000, 0x00ff0000, 0x0000ff00 );
222 : #endif
223 0 : break;
224 : case Format::THIRTYTWO_BIT_TC_MASK_RGBA:
225 0 : nBitCount = 32;
226 0 : pBuf->mnFormat = BMP_FORMAT_32BIT_TC_MASK;
227 : #ifdef OSL_BIGENDIAN
228 : pBuf->maColorMask = ColorMask( 0xff000000, 0x00ff0000, 0x0000ff00 );
229 : #else
230 0 : pBuf->maColorMask = ColorMask( 0x000000ff, 0x0000ff00, 0x00ff0000 );
231 : #endif
232 0 : break;
233 :
234 : default:
235 : // this is an error case !!!!!
236 0 : nBitCount = 1;
237 0 : pBuf->mnFormat = BMP_FORMAT_1BIT_MSB_PAL;
238 0 : break;
239 : }
240 581737 : if( m_aBitmap->isTopDown() )
241 0 : pBuf->mnFormat |= BMP_FORMAT_TOP_DOWN;
242 :
243 581737 : B2IVector aSize = m_aBitmap->getSize();
244 581737 : pBuf->mnWidth = aSize.getX();
245 581737 : pBuf->mnHeight = aSize.getY();
246 581737 : pBuf->mnScanlineSize = m_aBitmap->getScanlineStride();
247 581737 : pBuf->mnBitCount = nBitCount;
248 581737 : pBuf->mpBits = (sal_uInt8*)m_aBitmap->getBuffer().get();
249 581737 : if( nBitCount <= 8 )
250 : {
251 999492 : if( m_aBitmap->getScanlineFormat() == Format::EIGHT_BIT_GREY ||
252 666328 : m_aBitmap->getScanlineFormat() == Format::FOUR_BIT_LSB_GREY ||
253 666328 : m_aBitmap->getScanlineFormat() == Format::FOUR_BIT_MSB_GREY ||
254 999492 : m_aBitmap->getScanlineFormat() == Format::ONE_BIT_LSB_GREY ||
255 333164 : m_aBitmap->getScanlineFormat() == Format::ONE_BIT_MSB_GREY
256 : )
257 0 : pBuf->maPalette = Bitmap::GetGreyPalette( 1U << nBitCount );
258 : else
259 : {
260 333164 : basebmp::PaletteMemorySharedVector aPalette = m_aBitmap->getPalette();
261 333164 : if( aPalette.get() )
262 : {
263 333164 : unsigned int nColors = aPalette->size();
264 333164 : if( nColors > 0 )
265 : {
266 333164 : pBuf->maPalette.SetEntryCount( nColors );
267 84588544 : for( unsigned int i = 0; i < nColors; i++ )
268 : {
269 84255380 : const basebmp::Color& rCol = (*aPalette)[i];
270 84255380 : pBuf->maPalette[i] = BitmapColor( rCol.getRed(), rCol.getGreen(), rCol.getBlue() );
271 : }
272 : }
273 333164 : }
274 : }
275 581737 : }
276 : }
277 :
278 581741 : return pBuf;
279 : }
280 :
281 581737 : void SvpSalBitmap::ReleaseBuffer( BitmapBuffer* pBuffer, bool bReadOnly )
282 : {
283 581737 : if( !bReadOnly && pBuffer->maPalette.GetEntryCount() )
284 : {
285 : // palette might have changed, clone device (but recycle
286 : // memory)
287 64055 : sal_uInt16 nBitCount = 0;
288 64055 : switch( m_aBitmap->getScanlineFormat() )
289 : {
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 694 : nBitCount = 1;
298 694 : break;
299 :
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 175 : nBitCount = 4;
308 175 : break;
309 :
310 : case Format::EIGHT_BIT_PAL:
311 : // FALLTHROUGH intended
312 : case Format::EIGHT_BIT_GREY:
313 63186 : nBitCount = 8;
314 63186 : break;
315 :
316 : default:
317 0 : break;
318 : }
319 :
320 64055 : if( nBitCount )
321 : {
322 64055 : sal_uInt32 nEntries = 1U << nBitCount;
323 :
324 : boost::shared_ptr< std::vector<basebmp::Color> > pPal(
325 : new std::vector<basebmp::Color>( nEntries,
326 64055 : basebmp::Color(COL_WHITE)));
327 : const sal_uInt32 nColors = std::min(
328 64055 : (sal_uInt32)pBuffer->maPalette.GetEntryCount(),
329 64055 : nEntries);
330 16135559 : for( sal_uInt32 i = 0; i < nColors; i++ )
331 : {
332 16071504 : const BitmapColor& rCol = pBuffer->maPalette[i];
333 16071504 : (*pPal)[i] = basebmp::Color( rCol.GetRed(), rCol.GetGreen(), rCol.GetBlue() );
334 : }
335 :
336 192165 : m_aBitmap = basebmp::createBitmapDevice( m_aBitmap->getSize(),
337 64055 : m_aBitmap->isTopDown(),
338 : m_aBitmap->getScanlineFormat(),
339 : m_aBitmap->getBuffer(),
340 128110 : pPal );
341 : }
342 : }
343 :
344 581737 : delete pBuffer;
345 581737 : }
346 :
347 0 : bool SvpSalBitmap::GetSystemData( BitmapSystemData& )
348 : {
349 0 : return false;
350 : }
351 :
352 387559 : sal_uInt32 SvpSalBitmap::getBitCountFromScanlineFormat( sal_Int32 nFormat )
353 : {
354 387559 : sal_uInt32 nBitCount = 1;
355 387559 : switch( nFormat )
356 : {
357 : case Format::ONE_BIT_MSB_GREY:
358 : case Format::ONE_BIT_LSB_GREY:
359 : case Format::ONE_BIT_MSB_PAL:
360 : case Format::ONE_BIT_LSB_PAL:
361 4732 : nBitCount = 1;
362 4732 : break;
363 : case Format::FOUR_BIT_MSB_GREY:
364 : case Format::FOUR_BIT_LSB_GREY:
365 : case Format::FOUR_BIT_MSB_PAL:
366 : case Format::FOUR_BIT_LSB_PAL:
367 327 : nBitCount = 4;
368 327 : break;
369 : case Format::EIGHT_BIT_PAL:
370 : case Format::EIGHT_BIT_GREY:
371 184108 : nBitCount = 8;
372 184108 : break;
373 : case Format::SIXTEEN_BIT_LSB_TC_MASK:
374 : case Format::SIXTEEN_BIT_MSB_TC_MASK:
375 0 : nBitCount = 16;
376 0 : break;
377 : case Format::TWENTYFOUR_BIT_TC_MASK:
378 198392 : nBitCount = 24;
379 198392 : break;
380 : case Format::THIRTYTWO_BIT_TC_MASK_BGRA:
381 : case Format::THIRTYTWO_BIT_TC_MASK_ARGB:
382 : case Format::THIRTYTWO_BIT_TC_MASK_ABGR:
383 : case Format::THIRTYTWO_BIT_TC_MASK_RGBA:
384 0 : nBitCount = 32;
385 0 : break;
386 : default:
387 : OSL_FAIL( "unsupported basebmp format" );
388 0 : break;
389 : }
390 387559 : return nBitCount;
391 453 : }
392 :
393 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|