LibreOffice Module filter (master) 1
bitmap.cxx
Go to the documentation of this file.
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
21#include <sal/log.hxx>
23#include <vcl/BitmapTools.hxx>
24#include <memory>
25
26#include "bitmap.hxx"
27#include "elements.hxx"
28
29namespace {
30
31Color BMCOL(sal_uInt32 _col) {
32 return Color( static_cast<sal_Int8>(_col >> 16 ), static_cast<sal_Int8>( _col >> 8 ), static_cast<sal_Int8>(_col) );
33}
34
35}
36
38 : mpCGM(&rCGM)
39 , pCGMBitmapDescriptor(new CGMBitmapDescriptor)
40{
42};
43
45{
46}
47
48namespace
49{
50 bool isLegalBitsPerPixel(sal_uInt32 nBitsPerPixel)
51 {
52 switch (nBitsPerPixel)
53 {
54 case 1:
55 case 2:
56 case 4:
57 case 8:
58 case 24:
59 return true;
60 default:
61 break;
62 }
63 return false;
64 }
65}
66
68{
69 rDesc.mbStatus = true;
70
71 if (!ImplGetDimensions(rDesc) || !rDesc.mpBuf)
72 return;
73
74 if (!isLegalBitsPerPixel(rDesc.mnDstBitsPerPixel))
75 {
76 rDesc.mbStatus = false;
77 return;
78 }
79
80 try
81 {
82 if (rDesc.mnScanSize)
83 {
84 vcl::bitmap::RawBitmap aBitmap( Size( rDesc.mnX, rDesc.mnY ), 24 );
85
86 // the picture may either be read from left to right or right to left, from top to bottom ...
87
88 tools::Long nxCount = rDesc.mnX + 1; // +1 because we are using prefix decreasing
89 tools::Long nyCount = rDesc.mnY + 1;
90 tools::Long nx, ny, nxC;
91
92 switch ( rDesc.mnDstBitsPerPixel ) {
93 case 1 : {
94 std::vector<Color> palette(2);
95 if ( rDesc.mnLocalColorPrecision == 1 )
96 palette = ImplGeneratePalette( rDesc );
97 else {
98 palette[0] = BMCOL( mpCGM->pElement->nBackGroundColor );
99 palette[1] = ( mpCGM->pElement->nAspectSourceFlags & ASF_FILLINTERIORSTYLE )
100 ? BMCOL( mpCGM->pElement->pFillBundle->GetColor() )
101 : BMCOL( mpCGM->pElement->aFillBundle.GetColor() );
102 };
103 for (ny = 0; rDesc.mbStatus && --nyCount; ny++, rDesc.mpBuf += rDesc.mnScanSize) {
104 nxC = nxCount;
105 for ( nx = 0; --nxC; nx++ ) {
106 // this is not fast, but a one bit/pixel format is rarely used
107 const sal_uInt8* pPos = rDesc.mpBuf + (nx >> 3);
108 if (pPos >= rDesc.mpEndBuf)
109 {
110 SAL_WARN("filter.icgm", "buffer is too small");
111 rDesc.mbStatus = false;
112 break;
113 }
114 sal_uInt8 colorIndex = static_cast<sal_uInt8>((*pPos >> ((nx & 7)^7))) & 1;
115 aBitmap.SetPixel(ny, nx, palette[colorIndex]);
116 }
117 }
118 }
119 break;
120
121 case 2 : {
122 auto palette = ImplGeneratePalette( rDesc );
123 for (ny = 0; rDesc.mbStatus && --nyCount; ny++, rDesc.mpBuf += rDesc.mnScanSize) {
124 nxC = nxCount;
125 for ( nx = 0; --nxC; nx++ ) {
126 // this is not fast, but a two bits/pixel format is rarely used
127 const sal_uInt8* pPos = rDesc.mpBuf + (nx >> 2);
128 if (pPos >= rDesc.mpEndBuf)
129 {
130 SAL_WARN("filter.icgm", "buffer is too small");
131 rDesc.mbStatus = false;
132 break;
133 }
134 aBitmap.SetPixel(ny, nx, palette[static_cast<sal_uInt8>( (*pPos >> (((nx & 3)^3) << 1))) & 3]);
135 }
136 }
137 }
138 break;
139
140 case 4 : {
141 auto palette = ImplGeneratePalette( rDesc );
142 for (ny = 0; rDesc.mbStatus && --nyCount; ny++, rDesc.mpBuf += rDesc.mnScanSize) {
143 nxC = nxCount;
144 sal_uInt8* pTemp = rDesc.mpBuf;
145 for ( nx = 0; --nxC; nx++ ) {
146
147 if (pTemp >= rDesc.mpEndBuf)
148 {
149 SAL_WARN("filter.icgm", "buffer is too small");
150 rDesc.mbStatus = false;
151 break;
152 }
153
154 sal_uInt8 nDat = *pTemp++;
155
156 aBitmap.SetPixel(ny, nx, palette[static_cast<sal_uInt8>(nDat >> 4)]);
157 if ( --nxC ) {
158 ++nx;
159 aBitmap.SetPixel(ny, nx, palette[static_cast<sal_uInt8>(nDat & 15)]);
160 } else
161 break;
162 }
163 }
164 }
165 break;
166
167 case 8 : {
168 auto palette = ImplGeneratePalette( rDesc );
169 for (ny = 0; rDesc.mbStatus && --nyCount; ny++, rDesc.mpBuf += rDesc.mnScanSize) {
170 sal_uInt8* pTemp = rDesc.mpBuf;
171 nxC = nxCount;
172 for ( nx = 0; --nxC; nx++ ) {
173
174 if (pTemp >= rDesc.mpEndBuf)
175 {
176 SAL_WARN("filter.icgm", "buffer is too small");
177 rDesc.mbStatus = false;
178 break;
179 }
180
181 aBitmap.SetPixel(ny, nx, palette[*(pTemp++)]);
182 }
183 }
184 }
185 break;
186
187 case 24 : {
188 Color aBitmapColor;
189 for (ny = 0; rDesc.mbStatus && --nyCount; ny++, rDesc.mpBuf += rDesc.mnScanSize) {
190 sal_uInt8* pTemp = rDesc.mpBuf;
191 nxC = nxCount;
192 for ( nx = 0; --nxC; nx++ ) {
193
194 if (pTemp + 2 >= rDesc.mpEndBuf)
195 {
196 SAL_WARN("filter.icgm", "buffer is too small");
197 rDesc.mbStatus = false;
198 break;
199 }
200
201 aBitmapColor.SetRed( *pTemp++ );
202 aBitmapColor.SetGreen( *pTemp++ );
203 aBitmapColor.SetBlue( *pTemp++ );
204 aBitmap.SetPixel(ny, nx, aBitmapColor);
205 }
206 }
207 }
208 break;
209 }
210
211 if ( rDesc.mbStatus )
212 rDesc.mxBitmap = vcl::bitmap::CreateFromData(std::move(aBitmap));
213 }
214
215 double nX = rDesc.mnR.X - rDesc.mnQ.X;
216 double nY = rDesc.mnR.Y - rDesc.mnQ.Y;
217
218 rDesc.mndy = std::hypot(nX, nY);
219
220 nX = rDesc.mnR.X - rDesc.mnP.X;
221 nY = rDesc.mnR.Y - rDesc.mnP.Y;
222
223 rDesc.mndx = std::hypot(nX, nY);
224
225 nX = rDesc.mnR.X - rDesc.mnP.X;
226 nY = rDesc.mnR.Y - rDesc.mnP.Y;
227
228 double fSqrt = std::hypot(nX, nY);
229 rDesc.mnOrientation = fSqrt != 0.0 ? basegfx::rad2deg(acos(nX / fSqrt)) : 0.0;
230 if ( nY > 0 )
231 rDesc.mnOrientation = 360 - rDesc.mnOrientation;
232
233 nX = rDesc.mnQ.X - rDesc.mnR.X;
234 nY = rDesc.mnQ.Y - rDesc.mnR.Y;
235
236 double fAngle = basegfx::deg2rad( 360 - rDesc.mnOrientation );
237 double fSin = sin(fAngle);
238 double fCos = cos(fAngle);
239 nX = fCos * nX + fSin * nY;
240 nY = -( fSin * nX - fCos * nY );
241
242 fSqrt = std::hypot(nX, nY);
243 fAngle = fSqrt != 0.0 ? basegfx::rad2deg(acos(nX / fSqrt)) : 0.0;
244 if ( nY > 0 )
245 fAngle = 360 - fAngle;
246
247 if ( fAngle > 180 ) { // is the picture build upwards or downwards ?
248 rDesc.mnOrigin = rDesc.mnP;
249 } else {
250 rDesc.mbVMirror = true;
251 rDesc.mnOrigin = rDesc.mnP;
252 rDesc.mnOrigin.X += rDesc.mnQ.X - rDesc.mnR.X;
253 rDesc.mnOrigin.Y += rDesc.mnQ.Y - rDesc.mnR.Y;
254 }
255 }
256 catch (const std::bad_alloc&)
257 {
258 rDesc.mbStatus = false;
259 }
260}
261
262std::vector<Color> CGMBitmap::ImplGeneratePalette( CGMBitmapDescriptor const & rDesc )
263{
264 sal_uInt16 nColors = sal::static_int_cast< sal_uInt16 >(
265 1 << rDesc.mnDstBitsPerPixel);
266 std::vector<Color> palette( nColors );
267 for ( sal_uInt16 i = 0; i < nColors; i++ )
268 {
269 palette[i] = BMCOL( mpCGM->pElement->aLatestColorTable[ i ] );
270 }
271 return palette;
272}
273
274
276{
277 mpCGM->ImplGetPoint( rDesc.mnP ); // parallelogram p < - > r
278 mpCGM->ImplGetPoint( rDesc.mnQ ); // |
279 mpCGM->ImplGetPoint( rDesc.mnR ); // q
280 sal_uInt32 nPrecision = mpCGM->pElement->nIntegerPrecision;
281 rDesc.mnX = mpCGM->ImplGetUI( nPrecision );
282 rDesc.mnY = mpCGM->ImplGetUI( nPrecision );
283 rDesc.mnLocalColorPrecision = mpCGM->ImplGetI( nPrecision );
284 rDesc.mnScanSize = 0;
285 switch( rDesc.mnLocalColorPrecision )
286 {
287 case tools::Long(0x80000001) : // monochrome ( bit = 0->backgroundcolor )
288 case 0 : // bit = 1->fillcolor
289 rDesc.mnDstBitsPerPixel = 1;
290 break;
291 case 1 : // 2 color indexed ( monochrome )
292 case -1 :
293 rDesc.mnDstBitsPerPixel = 1;
294 break;
295 case 2 : // 4 color indexed
296 case -2 :
297 rDesc.mnDstBitsPerPixel = 2;
298 break;
299 case 4 : // 16 color indexed
300 case -4 :
301 rDesc.mnDstBitsPerPixel = 4;
302 break;
303 case 8 : // 256 color indexed
304 case -8 :
305 rDesc.mnDstBitsPerPixel = 8;
306 rDesc.mnScanSize = rDesc.mnX;
307 break;
308 case 16 : // NS
309 case -16 :
310 rDesc.mbStatus = false;
311 break;
312 case 24 : // 24 bit directColor ( 8 bits each component )
313 case -24 :
314 rDesc.mnDstBitsPerPixel = 24;
315 break;
316 case 32 : // NS
317 case -32 :
318 rDesc.mbStatus = false;
319 break;
320
321 }
322 // mnCompressionMode == 0 : CCOMP_RUNLENGTH
323 // == 1 : CCOMP_PACKED ( no compression. each row starts on a 4 byte boundary )
324 if ( ( rDesc.mnCompressionMode = mpCGM->ImplGetUI16() ) != 1 )
325 rDesc.mbStatus = false;
326
327 if ( !( rDesc.mnX || rDesc.mnY ) )
328 rDesc.mbStatus = false;
329
330 sal_uInt32 nHeaderSize = 2 + 3 * nPrecision + 3 * mpCGM->ImplGetPointSize();
331
332 sal_uInt32 nWidthBits;
333 if (o3tl::checked_multiply(rDesc.mnX, rDesc.mnDstBitsPerPixel, nWidthBits))
334 {
335 rDesc.mbStatus = false;
336 return false;
337 }
338
339 rDesc.mnScanSize = (nWidthBits + 7) >> 3;
340
341 sal_uInt32 nScanSize;
342 nScanSize = rDesc.mnScanSize;
343 if ( ( nScanSize * rDesc.mnY + nHeaderSize ) != mpCGM->mnElementSize ) // try a scansize without dw alignment
344 {
345 nScanSize = ( rDesc.mnScanSize + 1 ) & ~1;
346 if ( ( nScanSize * rDesc.mnY + nHeaderSize ) != mpCGM->mnElementSize ) // then we'll try word alignment
347 {
348 nScanSize = ( rDesc.mnScanSize + 3 ) & ~3;
349 if ( ( nScanSize * rDesc.mnY + nHeaderSize ) != mpCGM->mnElementSize ) // and last we'll try dword alignment
350 {
351 nScanSize = ( rDesc.mnScanSize + 1 ) & ~1; // and LAST BUT NOT LEAST we'll try word alignment without aligning the last line
352 if ( ( nScanSize * ( rDesc.mnY - 1 ) + rDesc.mnScanSize + nHeaderSize ) != mpCGM->mnElementSize )
353 {
354 nScanSize = ( rDesc.mnScanSize + 3 ) & ~3;
355 if ( ( nScanSize * ( rDesc.mnY - 1 ) + rDesc.mnScanSize + nHeaderSize ) != mpCGM->mnElementSize )
356 {
357 mpCGM->mnParaSize = 0; // this format is corrupt
358 rDesc.mbStatus = false;
359 }
360 }
361 }
362 }
363 }
364 rDesc.mnScanSize = nScanSize;
365 if ( rDesc.mbStatus )
366 {
367 rDesc.mpBuf = mpCGM->mpSource + mpCGM->mnParaSize; // mpBuf now points to the first scanline
369 mpCGM->mnParaSize += rDesc.mnScanSize * rDesc.mnY;
370 }
371 return rDesc.mbStatus;
372}
373
374
376{
378 static const bool bFuzzing = utl::ConfigManager::IsFuzzing();
379 if (bFuzzing)
380 {
381 if (rDest.mxBitmap.GetSizePixel().Height() + rSource.mnY > SAL_MAX_UINT16)
382 {
383 SAL_WARN("filter.icgm", "bitmap would expand too much");
384 rDest.mbStatus = false;
385 return;
386 }
387 if (mpCGM->mnBitmapInserts > 1024)
388 {
389 SAL_WARN("filter.icgm", "too many inserts");
390 rDest.mbStatus = false;
391 return;
392 }
393 }
394 rDest.mxBitmap.Expand( 0, rSource.mnY );
395 rDest.mxBitmap.CopyPixel( tools::Rectangle( Point( 0, rDest.mnY ), Size( rSource.mnX, rSource.mnY ) ),
396 tools::Rectangle( Point( 0, 0 ), Size( rSource.mnX, rSource.mnY ) ), &rSource.mxBitmap );
397
398 if ( ( rSource.mnR.Y == rDest.mnQ.Y ) && ( rSource.mnR.X == rDest.mnQ.X ) )
399 { // Insert on Bottom
400 if ( mpCGM->mnVDCYmul == -1 )
401 rDest.mnOrigin = rSource.mnOrigin; // new origin
402 FloatPoint aFloatPoint;
403 aFloatPoint.X = rSource.mnQ.X - rSource.mnR.X;
404 aFloatPoint.Y = rSource.mnQ.Y - rSource.mnR.Y;
405 rDest.mnQ.X += aFloatPoint.X;
406 rDest.mnQ.Y += aFloatPoint.Y;
407 rDest.mnP = rSource.mnP;
408 rDest.mnR = rSource.mnR;
409 }
410 else
411 { // Insert on Top
412 if ( mpCGM->mnVDCYmul == 1 )
413 rDest.mnOrigin = rSource.mnOrigin; // new origin
414 rDest.mnP = rSource.mnP;
415 rDest.mnR = rSource.mnR;
416 }
417 rDest.mnY += rSource.mnY;
418 rDest.mndy += rSource.mndy;
419};
420
421std::unique_ptr<CGMBitmap> CGMBitmap::GetNext()
422{
423 std::unique_ptr<CGMBitmap> xCGMTempBitmap;
424 if (!pCGMBitmapDescriptor->mxBitmap.IsEmpty() && pCGMBitmapDescriptor->mbStatus)
425 {
426 xCGMTempBitmap.reset(new CGMBitmap(*mpCGM));
427 if ( ( static_cast<tools::Long>(xCGMTempBitmap->pCGMBitmapDescriptor->mnOrientation) == static_cast<tools::Long>(pCGMBitmapDescriptor->mnOrientation) ) &&
428 ( ( ( xCGMTempBitmap->pCGMBitmapDescriptor->mnR.X == pCGMBitmapDescriptor->mnQ.X ) &&
429 ( xCGMTempBitmap->pCGMBitmapDescriptor->mnR.Y == pCGMBitmapDescriptor->mnQ.Y ) ) ||
430 ( ( xCGMTempBitmap->pCGMBitmapDescriptor->mnQ.X == pCGMBitmapDescriptor->mnR.X ) &&
431 ( xCGMTempBitmap->pCGMBitmapDescriptor->mnQ.Y == pCGMBitmapDescriptor->mnR.Y ) ) ) )
432 {
433 ImplInsert( *(xCGMTempBitmap->pCGMBitmapDescriptor), *pCGMBitmapDescriptor );
434 xCGMTempBitmap.reset();
435 return xCGMTempBitmap;
436 }
437
438 pCGMBitmapDescriptor.swap(xCGMTempBitmap->pCGMBitmapDescriptor);
439 }
440 return xCGMTempBitmap;
441}
442
443/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
double ny
double nx
#define ASF_FILLINTERIORSTYLE
Definition: cgmtypes.hxx:69
void Expand(sal_Int32 nDX, sal_Int32 nDY, bool bExpandTransparent=false)
bool CopyPixel(const tools::Rectangle &rRectDst, const tools::Rectangle &rRectSrc, const BitmapEx *pBmpExSrc)
const Size & GetSizePixel() const
sal_uInt8 * mpBuf
Definition: bitmap.hxx:31
BitmapEx mxBitmap
Definition: bitmap.hxx:33
sal_uInt32 mnScanSize
Definition: bitmap.hxx:37
FloatPoint mnOrigin
Definition: bitmap.hxx:40
sal_uInt32 mnCompressionMode
Definition: bitmap.hxx:46
sal_uInt32 mnDstBitsPerPixel
Definition: bitmap.hxx:36
sal_uInt32 mnY
Definition: bitmap.hxx:44
FloatPoint mnR
Definition: bitmap.hxx:38
sal_uInt32 mnX
Definition: bitmap.hxx:44
FloatPoint mnQ
Definition: bitmap.hxx:38
double mnOrientation
Definition: bitmap.hxx:42
FloatPoint mnP
Definition: bitmap.hxx:38
sal_uInt8 * mpEndBuf
Definition: bitmap.hxx:32
tools::Long mnLocalColorPrecision
Definition: bitmap.hxx:45
std::unique_ptr< CGMBitmap > GetNext()
Definition: bitmap.cxx:421
std::vector< Color > ImplGeneratePalette(CGMBitmapDescriptor const &)
Definition: bitmap.cxx:262
void ImplGetBitmap(CGMBitmapDescriptor &)
Definition: bitmap.cxx:67
CGMBitmap(CGM &rCGM)
Definition: bitmap.cxx:37
void ImplInsert(CGMBitmapDescriptor const &rSource, CGMBitmapDescriptor &rDest)
Definition: bitmap.cxx:375
std::unique_ptr< CGMBitmapDescriptor > pCGMBitmapDescriptor
Definition: bitmap.hxx:69
CGM * mpCGM
Definition: bitmap.hxx:67
bool ImplGetDimensions(CGMBitmapDescriptor &)
Definition: bitmap.cxx:275
~CGMBitmap()
Definition: bitmap.cxx:44
Definition: cgm.hxx:39
int mnBitmapInserts
Definition: cgm.hxx:64
sal_Int32 ImplGetI(sal_uInt32 nPrecision)
Definition: cgm.cxx:96
sal_uInt32 ImplGetUI16()
Definition: cgm.cxx:82
sal_uInt8 * mpSource
Definition: cgm.hxx:75
void ImplGetPoint(FloatPoint &rFloatPoint, bool bMap=false)
Definition: cgm.cxx:274
sal_uInt8 * mpEndValidSource
Definition: cgm.hxx:77
sal_uInt32 ImplGetUI(sal_uInt32 nPrecision)
Definition: cgm.cxx:128
sal_uInt32 mnElementSize
Definition: cgm.hxx:87
std::unique_ptr< CGMElements > pElement
Definition: cgm.hxx:69
sal_uInt32 ImplGetPointSize()
Definition: cgm.cxx:246
double mnVDCYmul
Definition: cgm.hxx:48
sal_uInt32 mnParaSize
Definition: cgm.hxx:78
void SetGreen(sal_uInt8 nGreen)
void SetRed(sal_uInt8 nRed)
void SetBlue(sal_uInt8 nBlue)
constexpr tools::Long Height() const
static bool IsFuzzing()
void SetPixel(tools::Long nY, tools::Long nX, Color nColor)
#define SAL_WARN(area, stream)
constexpr double rad2deg(double v)
constexpr double deg2rad(double v)
int i
Definition: gentoken.py:48
std::enable_if< std::is_signed< T >::value, bool >::type checked_multiply(T a, T b, T &result)
long Long
BitmapEx CreateFromData(sal_uInt8 const *pData, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int32 nStride, sal_Int8 nBitCount, bool bReversColors, bool bReverseAlpha)
double Y
Definition: cgmtypes.hxx:27
double X
Definition: cgmtypes.hxx:26
unsigned char sal_uInt8
#define SAL_MAX_UINT16
signed char sal_Int8