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