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 :
21 : #include "main.hxx"
22 :
23 :
24 :
25 0 : CGMBitmap::CGMBitmap( CGM& rCGM ) :
26 : mpCGM ( &rCGM ),
27 0 : pCGMBitmapDescriptor ( new CGMBitmapDescriptor )
28 : {
29 0 : ImplGetBitmap( *pCGMBitmapDescriptor );
30 0 : };
31 :
32 :
33 :
34 0 : CGMBitmap::~CGMBitmap()
35 : {
36 0 : delete pCGMBitmapDescriptor;
37 0 : }
38 :
39 :
40 :
41 0 : void CGMBitmap::ImplGetBitmap( CGMBitmapDescriptor& rDesc )
42 : {
43 0 : rDesc.mbStatus = true;
44 :
45 0 : if ( ImplGetDimensions( rDesc ) && rDesc.mpBuf )
46 : {
47 0 : if ( ( rDesc.mpBitmap = new Bitmap( Size( rDesc.mnX, rDesc.mnY ), (sal_uInt16)rDesc.mnDstBitsPerPixel ) ) != NULL )
48 : {
49 0 : if ( ( rDesc.mpAcc = rDesc.mpBitmap->AcquireWriteAccess() ) != NULL )
50 : {
51 :
52 : // the picture may either be read from left to right or right to left, from top to bottom ...
53 :
54 0 : long nxCount = rDesc.mnX + 1; // +1 because we are using prefix decreasing
55 0 : long nyCount = rDesc.mnY + 1;
56 : long nx, ny, nxC;
57 :
58 0 : switch ( rDesc.mnDstBitsPerPixel )
59 : {
60 : case 1 :
61 : {
62 0 : if ( rDesc.mnLocalColorPrecision == 1 )
63 0 : ImplSetCurrentPalette( rDesc );
64 : else
65 : {
66 0 : rDesc.mpAcc->SetPaletteEntryCount( 2 );
67 0 : rDesc.mpAcc->SetPaletteColor( 0, BMCOL( mpCGM->pElement->nBackGroundColor ) );
68 : rDesc.mpAcc->SetPaletteColor( 1,
69 0 : ( mpCGM->pElement->nAspectSourceFlags & ASF_FILLINTERIORSTYLE )
70 0 : ? BMCOL( mpCGM->pElement->pFillBundle->GetColor() )
71 0 : : BMCOL( mpCGM->pElement->aFillBundle.GetColor() ) ) ;
72 : }
73 0 : for ( ny = 0; --nyCount ; ny++, rDesc.mpBuf += rDesc.mnScanSize )
74 : {
75 0 : nxC = nxCount;
76 0 : for ( nx = 0; --nxC; nx++ )
77 : { // this is not fast, but a one bit/pixel format is rarely used
78 0 : rDesc.mpAcc->SetPixelIndex( ny, nx, static_cast<sal_uInt8>( (*( rDesc.mpBuf + (nx >> 3)) >> ((nx & 7)^7))) & 1 );
79 : }
80 : }
81 : }
82 0 : break;
83 :
84 : case 2 :
85 : {
86 0 : ImplSetCurrentPalette( rDesc );
87 0 : for ( ny = 0; --nyCount; ny++, rDesc.mpBuf += rDesc.mnScanSize )
88 : {
89 0 : nxC = nxCount;
90 0 : for ( nx = 0; --nxC; nx++ )
91 : { // this is not fast, but a two bits/pixel format is rarely used
92 0 : rDesc.mpAcc->SetPixelIndex( ny, nx, static_cast<sal_uInt8>( (*(rDesc.mpBuf + (nx >> 2)) >> (((nx & 3)^3) << 1))) & 3 );
93 : }
94 : }
95 : }
96 0 : break;
97 :
98 : case 4 :
99 : {
100 0 : ImplSetCurrentPalette( rDesc );
101 0 : for ( ny = 0; --nyCount; ny++, rDesc.mpBuf += rDesc.mnScanSize )
102 : {
103 0 : nxC = nxCount;
104 : sal_Int8 nDat;
105 0 : sal_uInt8* pTemp = rDesc.mpBuf;
106 0 : for ( nx = 0; --nxC; nx++ )
107 : {
108 0 : nDat = *pTemp++;
109 0 : rDesc.mpAcc->SetPixelIndex( ny, nx, static_cast<sal_uInt8>(nDat >> 4) );
110 0 : if ( --nxC )
111 : {
112 0 : nx ++;
113 0 : rDesc.mpAcc->SetPixelIndex( ny, nx, static_cast<sal_uInt8>(nDat & 15) );
114 : }
115 : else
116 0 : break;
117 : }
118 : }
119 : }
120 0 : break;
121 :
122 : case 8 :
123 : {
124 0 : ImplSetCurrentPalette( rDesc );
125 0 : for ( ny = 0; --nyCount; ny++, rDesc.mpBuf += rDesc.mnScanSize )
126 : {
127 0 : sal_uInt8* pTemp = rDesc.mpBuf;
128 0 : nxC = nxCount;
129 0 : for ( nx = 0; --nxC; nx++ )
130 : {
131 0 : rDesc.mpAcc->SetPixelIndex( ny, nx, *(pTemp++) );
132 : }
133 : }
134 : }
135 0 : break;
136 :
137 : case 24 :
138 : {
139 : {
140 0 : BitmapColor aBitmapColor;
141 0 : for ( ny = 0; --nyCount; ny++, rDesc.mpBuf += rDesc.mnScanSize )
142 : {
143 0 : sal_uInt8* pTemp = rDesc.mpBuf;
144 0 : nxC = nxCount;
145 0 : for ( nx = 0; --nxC; nx++ )
146 : {
147 0 : aBitmapColor.SetRed( *pTemp++ );
148 0 : aBitmapColor.SetGreen( *pTemp++ );
149 0 : aBitmapColor.SetBlue( *pTemp++ );
150 0 : rDesc.mpAcc->SetPixel( ny, nx, aBitmapColor );
151 : }
152 0 : }
153 : }
154 : }
155 0 : break;
156 : }
157 0 : double nX = rDesc.mnR.X - rDesc.mnQ.X;
158 0 : double nY = rDesc.mnR.Y - rDesc.mnQ.Y;
159 :
160 0 : rDesc.mndy = sqrt( nX * nX + nY * nY );
161 :
162 0 : nX = rDesc.mnR.X - rDesc.mnP.X;
163 0 : nY = rDesc.mnR.Y - rDesc.mnP.Y;
164 :
165 0 : rDesc.mndx = sqrt( nX * nX + nY * nY );
166 :
167 0 : nX = rDesc.mnR.X - rDesc.mnP.X;
168 0 : nY = rDesc.mnR.Y - rDesc.mnP.Y;
169 :
170 0 : rDesc.mnOrientation = acos( nX / sqrt( nX * nX + nY * nY ) ) * 57.29577951308;
171 0 : if ( nY > 0 )
172 0 : rDesc.mnOrientation = 360 - rDesc.mnOrientation;
173 :
174 0 : nX = rDesc.mnQ.X - rDesc.mnR.X;
175 0 : nY = rDesc.mnQ.Y - rDesc.mnR.Y;
176 :
177 0 : double fAngle = 0.01745329251994 * ( 360 - rDesc.mnOrientation );
178 0 : double fSin = sin(fAngle);
179 0 : double fCos = cos(fAngle);
180 0 : nX = fCos * nX + fSin * nY;
181 0 : nY = -( fSin * nX - fCos * nY );
182 :
183 0 : fAngle = acos( nX / sqrt( nX * nX + nY * nY ) ) * 57.29577951308;
184 0 : if ( nY > 0 )
185 0 : fAngle = 360 - fAngle;
186 :
187 0 : if ( fAngle > 180 ) // is the picture build upwards or downwards ?
188 : {
189 0 : rDesc.mnOrigin = rDesc.mnP;
190 : }
191 : else
192 : {
193 0 : rDesc.mbVMirror = true;
194 0 : rDesc.mnOrigin = rDesc.mnP;
195 0 : rDesc.mnOrigin.X += rDesc.mnQ.X - rDesc.mnR.X;
196 0 : rDesc.mnOrigin.Y += rDesc.mnQ.Y - rDesc.mnR.Y;
197 : }
198 : }
199 : else
200 0 : rDesc.mbStatus = false;
201 : }
202 : else
203 0 : rDesc.mbStatus = false;
204 : }
205 : else
206 0 : rDesc.mbStatus = false;
207 :
208 0 : if ( rDesc.mpAcc )
209 : {
210 0 : rDesc.mpBitmap->ReleaseAccess( rDesc.mpAcc );
211 0 : rDesc.mpAcc = NULL;
212 : }
213 0 : if ( rDesc.mbStatus == false )
214 : {
215 0 : if ( rDesc.mpBitmap )
216 : {
217 0 : delete rDesc.mpBitmap;
218 0 : rDesc.mpBitmap = NULL;
219 : }
220 : }
221 0 : }
222 :
223 :
224 :
225 0 : void CGMBitmap::ImplSetCurrentPalette( CGMBitmapDescriptor& rDesc )
226 : {
227 : sal_uInt16 nColors = sal::static_int_cast< sal_uInt16 >(
228 0 : 1 << rDesc.mnDstBitsPerPixel);
229 0 : rDesc.mpAcc->SetPaletteEntryCount( nColors );
230 0 : for ( sal_uInt16 i = 0; i < nColors; i++ )
231 : {
232 0 : rDesc.mpAcc->SetPaletteColor( i, BMCOL( mpCGM->pElement->aLatestColorTable[ i ] ) );
233 : }
234 0 : }
235 :
236 :
237 :
238 0 : bool CGMBitmap::ImplGetDimensions( CGMBitmapDescriptor& rDesc )
239 : {
240 0 : mpCGM->ImplGetPoint( rDesc.mnP ); // parallelogram p < - > r
241 0 : mpCGM->ImplGetPoint( rDesc.mnQ ); // |
242 0 : mpCGM->ImplGetPoint( rDesc.mnR ); // q
243 0 : sal_uInt32 nPrecision = mpCGM->pElement->nIntegerPrecision;
244 0 : rDesc.mnX = mpCGM->ImplGetUI( nPrecision );
245 0 : rDesc.mnY = mpCGM->ImplGetUI( nPrecision );
246 0 : rDesc.mnLocalColorPrecision = mpCGM->ImplGetI( nPrecision );
247 0 : rDesc.mnScanSize = 0;
248 0 : switch( rDesc.mnLocalColorPrecision )
249 : {
250 : case long(0x80000001) : // monochrome ( bit = 0->backgroundcolor )
251 : case 0 : // bit = 1->fillcolor
252 0 : rDesc.mnDstBitsPerPixel = 1;
253 0 : break;
254 : case 1 : // 2 color indexed ( monochrome )
255 : case -1 :
256 0 : rDesc.mnDstBitsPerPixel = 1;
257 0 : break;
258 : case 2 : // 4 color indexed
259 : case -2 :
260 0 : rDesc.mnDstBitsPerPixel = 2;
261 0 : break;
262 : case 4 : // 16 color indexed
263 : case -4 :
264 0 : rDesc.mnDstBitsPerPixel = 4;
265 0 : break;
266 : case 8 : // 256 color indexed
267 : case -8 :
268 0 : rDesc.mnDstBitsPerPixel = 8;
269 0 : rDesc.mnScanSize = rDesc.mnX;
270 0 : break;
271 : case 16 : // NS
272 : case -16 :
273 0 : rDesc.mbStatus = false;
274 0 : break;
275 : case 24 : // 24 bit directColor ( 8 bits each component )
276 : case -24 :
277 0 : rDesc.mnDstBitsPerPixel = 24;
278 0 : break;
279 : case 32 : // NS
280 : case -32 :
281 0 : rDesc.mbStatus = false;
282 0 : break;
283 :
284 : }
285 : // mnCompressionMode == 0 : CCOMP_RUNLENGTH
286 : // == 1 : CCOMP_PACKED ( no compression. each row starts on a 4 byte boundary )
287 0 : if ( ( rDesc.mnCompressionMode = mpCGM->ImplGetUI16() ) != 1 )
288 0 : rDesc.mbStatus = false;
289 :
290 0 : if ( !( rDesc.mnX || rDesc.mnY ) )
291 0 : rDesc.mbStatus = false;
292 :
293 0 : sal_uInt32 nHeaderSize = 2 + 3 * nPrecision + 3 * mpCGM->ImplGetPointSize();
294 0 : rDesc.mnScanSize = ( ( rDesc.mnX * rDesc.mnDstBitsPerPixel + 7 ) >> 3 );
295 :
296 : sal_uInt32 nScanSize;
297 0 : nScanSize = rDesc.mnScanSize;
298 0 : if ( ( nScanSize * rDesc.mnY + nHeaderSize ) != mpCGM->mnElementSize ) // try a scansize without dw alignment
299 : {
300 0 : nScanSize = ( rDesc.mnScanSize + 1 ) & ~1;
301 0 : if ( ( nScanSize * rDesc.mnY + nHeaderSize ) != mpCGM->mnElementSize ) // then we'll try word alignment
302 : {
303 0 : nScanSize = ( rDesc.mnScanSize + 3 ) & ~3;
304 0 : if ( ( nScanSize * rDesc.mnY + nHeaderSize ) != mpCGM->mnElementSize ) // and last we'll try dword alignment
305 : {
306 0 : nScanSize = ( rDesc.mnScanSize + 1 ) & ~1; // and LAST BUT NOT LEAST we'll try word alignment without aligning the last line
307 0 : if ( ( nScanSize * ( rDesc.mnY - 1 ) + rDesc.mnScanSize + nHeaderSize ) != mpCGM->mnElementSize )
308 : {
309 0 : nScanSize = ( rDesc.mnScanSize + 3 ) & ~3;
310 0 : if ( ( nScanSize * ( rDesc.mnY - 1 ) + rDesc.mnScanSize + nHeaderSize ) != mpCGM->mnElementSize )
311 : {
312 0 : mpCGM->mnParaSize = 0; // this format is corrupt
313 0 : rDesc.mbStatus = false;
314 : }
315 : }
316 : }
317 : }
318 : }
319 0 : rDesc.mnScanSize = nScanSize;
320 0 : if ( rDesc.mbStatus )
321 : {
322 0 : rDesc.mpBuf = mpCGM->mpSource + mpCGM->mnParaSize; // mpBuf now points to the first scanline
323 0 : mpCGM->mnParaSize += rDesc.mnScanSize * rDesc.mnY;
324 : }
325 0 : return rDesc.mbStatus;
326 : }
327 :
328 :
329 :
330 0 : void CGMBitmap::ImplInsert( CGMBitmapDescriptor& rSource, CGMBitmapDescriptor& rDest )
331 : {
332 0 : if ( ( rSource.mnR.Y == rDest.mnQ.Y ) && ( rSource.mnR.X == rDest.mnQ.X ) )
333 : { // Insert on Bottom
334 0 : if ( mpCGM->mnVDCYmul == -1 )
335 0 : rDest.mnOrigin = rSource.mnOrigin; // new origin
336 0 : rDest.mpBitmap->Expand( 0, rSource.mnY );
337 : rDest.mpBitmap->CopyPixel( Rectangle( Point( 0, rDest.mnY ), Size( rSource.mnX, rSource.mnY ) ),
338 0 : Rectangle( Point( 0, 0 ), Size( rSource.mnX, rSource.mnY ) ), rSource.mpBitmap );
339 0 : FloatPoint aFloatPoint;
340 0 : aFloatPoint.X = rSource.mnQ.X - rSource.mnR.X;
341 0 : aFloatPoint.Y = rSource.mnQ.Y - rSource.mnR.Y;
342 0 : rDest.mnQ.X += aFloatPoint.X;
343 0 : rDest.mnQ.Y += aFloatPoint.Y;
344 0 : rDest.mnP = rSource.mnP;
345 0 : rDest.mnR = rSource.mnR;
346 : }
347 : else
348 : { // Insert on Top
349 0 : if ( mpCGM->mnVDCYmul == 1 )
350 0 : rDest.mnOrigin = rSource.mnOrigin; // new origin
351 0 : rDest.mpBitmap->Expand( 0, rSource.mnY );
352 : rDest.mpBitmap->CopyPixel( Rectangle( Point( 0, rDest.mnY ), Size( rSource.mnX, rSource.mnY ) ),
353 0 : Rectangle( Point( 0, 0 ), Size( rSource.mnX, rSource.mnY ) ), rSource.mpBitmap );
354 0 : rDest.mnP = rSource.mnP;
355 0 : rDest.mnR = rSource.mnR;
356 : }
357 0 : rDest.mnY += rSource.mnY;
358 0 : rDest.mndy += rSource.mndy;
359 0 : };
360 :
361 :
362 :
363 0 : CGMBitmap* CGMBitmap::GetNext()
364 : {
365 0 : if ( pCGMBitmapDescriptor->mpBitmap && pCGMBitmapDescriptor->mbStatus )
366 : {
367 0 : CGMBitmap* pCGMTempBitmap = new CGMBitmap( *mpCGM );
368 0 : if ( pCGMTempBitmap )
369 : {
370 0 : if ( ( (long)pCGMTempBitmap->pCGMBitmapDescriptor->mnOrientation == (long)pCGMBitmapDescriptor->mnOrientation ) &&
371 0 : ( ( ( pCGMTempBitmap->pCGMBitmapDescriptor->mnR.X == pCGMBitmapDescriptor->mnQ.X ) &&
372 0 : ( pCGMTempBitmap->pCGMBitmapDescriptor->mnR.Y == pCGMBitmapDescriptor->mnQ.Y ) ) ||
373 0 : ( ( pCGMTempBitmap->pCGMBitmapDescriptor->mnQ.X == pCGMBitmapDescriptor->mnR.X ) &&
374 0 : ( pCGMTempBitmap->pCGMBitmapDescriptor->mnQ.Y == pCGMBitmapDescriptor->mnR.Y ) ) ) )
375 : {
376 0 : ImplInsert( *(pCGMTempBitmap->pCGMBitmapDescriptor), *(pCGMBitmapDescriptor) );
377 0 : delete pCGMTempBitmap;
378 0 : return NULL;
379 : }
380 : else // we'll replace the pointers and return the old one
381 : {
382 0 : CGMBitmapDescriptor* pTempBD = pCGMBitmapDescriptor;
383 0 : pCGMBitmapDescriptor = pCGMTempBitmap->pCGMBitmapDescriptor;
384 0 : pCGMTempBitmap->pCGMBitmapDescriptor = pTempBD;
385 0 : return pCGMTempBitmap;
386 : }
387 : }
388 0 : return NULL;
389 : }
390 : else
391 0 : return NULL;
392 12 : }
393 :
394 :
395 :
396 :
397 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|