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