Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <unistd.h>
31 : : #include <cstdio>
32 : : #include <cstring>
33 : :
34 : : #include <bmp.hxx>
35 : :
36 : : #include <X11_selection.hxx>
37 : : #include <unx/x11/xlimits.hxx>
38 : : #include <sal/macros.h>
39 : :
40 : : using namespace x11;
41 : : using namespace com::sun::star::uno;
42 : : using namespace com::sun::star::script;
43 : : using namespace com::sun::star::awt;
44 : :
45 : : /*
46 : : * helper functions
47 : : */
48 : :
49 : 0 : inline void writeLE( sal_uInt16 nNumber, sal_uInt8* pBuffer )
50 : : {
51 : 0 : pBuffer[ 0 ] = (nNumber & 0xff);
52 : 0 : pBuffer[ 1 ] = ((nNumber>>8)&0xff);
53 : 0 : }
54 : :
55 : 0 : inline void writeLE( sal_uInt32 nNumber, sal_uInt8* pBuffer )
56 : : {
57 : 0 : pBuffer[ 0 ] = (nNumber & 0xff);
58 : 0 : pBuffer[ 1 ] = ((nNumber>>8)&0xff);
59 : 0 : pBuffer[ 2 ] = ((nNumber>>16)&0xff);
60 : 0 : pBuffer[ 3 ] = ((nNumber>>24)&0xff);
61 : 0 : }
62 : :
63 : 0 : inline sal_uInt16 readLE16( const sal_uInt8* pBuffer )
64 : : {
65 : 0 : return (((sal_uInt16)pBuffer[1]) << 8 ) | pBuffer[0];
66 : : }
67 : :
68 : 0 : inline sal_uInt16 readLE32( const sal_uInt8* pBuffer )
69 : : {
70 : : return
71 : : (((sal_uInt32)pBuffer[3]) << 24 ) |
72 : : (((sal_uInt32)pBuffer[2]) << 16 ) |
73 : 0 : (((sal_uInt32)pBuffer[1]) << 8 ) |
74 : 0 : pBuffer[0];
75 : : }
76 : :
77 : :
78 : : /*
79 : : * BmpTransporter
80 : : */
81 : :
82 : 0 : BmpTransporter::BmpTransporter( const Sequence<sal_Int8>& rBmp ) :
83 : 0 : m_aBM( rBmp )
84 : : {
85 : 0 : const sal_uInt8* pData = (const sal_uInt8*)rBmp.getConstArray();
86 : :
87 : 0 : if( pData[0] == 'B' || pData[1] == 'M' )
88 : : {
89 : 0 : pData = pData+14;
90 : 0 : m_aSize.Width = readLE32( pData+4 );
91 : 0 : m_aSize.Height = readLE32( pData+8 );
92 : : }
93 : : else
94 : 0 : m_aSize.Width = m_aSize.Height = 0;
95 : 0 : }
96 : :
97 : 0 : BmpTransporter::~BmpTransporter()
98 : : {
99 : 0 : }
100 : :
101 : 0 : com::sun::star::awt::Size SAL_CALL BmpTransporter::getSize() throw()
102 : : {
103 : 0 : return m_aSize;
104 : : }
105 : :
106 : 0 : Sequence< sal_Int8 > SAL_CALL BmpTransporter::getDIB() throw()
107 : : {
108 : 0 : return m_aBM;
109 : : }
110 : :
111 : 0 : Sequence< sal_Int8 > SAL_CALL BmpTransporter::getMaskDIB() throw()
112 : : {
113 : 0 : return Sequence< sal_Int8 >();
114 : : }
115 : :
116 : : /*
117 : : * scanline helpers
118 : : */
119 : :
120 : 0 : inline void X11_writeScanlinePixel( unsigned long nColor, sal_uInt8* pScanline, int depth, int x )
121 : : {
122 : 0 : switch( depth )
123 : : {
124 : : case 1:
125 : 0 : pScanline[ x/8 ] &= ~(1 << (x&7));
126 : 0 : pScanline[ x/8 ] |= ((nColor & 1) << (x&7));
127 : 0 : break;
128 : : case 4:
129 : 0 : pScanline[ x/2 ] &= ((x&1) ? 0x0f : 0xf0);
130 : 0 : pScanline[ x/2 ] |= ((x&1) ? (nColor & 0x0f) : ((nColor & 0x0f) << 4));
131 : 0 : break;
132 : : default:
133 : : case 8:
134 : 0 : pScanline[ x ] = (nColor & 0xff);
135 : 0 : break;
136 : : }
137 : 0 : }
138 : :
139 : 0 : static sal_uInt8* X11_getPaletteBmpFromImage(
140 : : Display* pDisplay,
141 : : XImage* pImage,
142 : : Colormap aColormap,
143 : : sal_Int32& rOutSize
144 : : )
145 : : {
146 : 0 : sal_uInt32 nColors = 0;
147 : :
148 : 0 : rOutSize = 0;
149 : :
150 : 0 : sal_uInt8* pBuffer = 0;
151 : : sal_uInt32 nHeaderSize, nScanlineSize;
152 : : sal_uInt16 nBitCount;
153 : : // determine header and scanline size
154 : 0 : switch( pImage->depth )
155 : : {
156 : : case 1:
157 : 0 : nHeaderSize = 64;
158 : 0 : nScanlineSize = (pImage->width+31)/32;
159 : 0 : nBitCount = 1;
160 : 0 : break;
161 : : case 4:
162 : 0 : nHeaderSize = 72;
163 : 0 : nScanlineSize = (pImage->width+1)/2;
164 : 0 : nBitCount = 4;
165 : 0 : break;
166 : : default:
167 : : case 8:
168 : 0 : nHeaderSize = 1084;
169 : 0 : nScanlineSize = pImage->width;
170 : 0 : nBitCount = 8;
171 : 0 : break;
172 : : }
173 : : // adjust scan lines to begin on %4 boundaries
174 : 0 : if( nScanlineSize & 3 )
175 : : {
176 : 0 : nScanlineSize &= 0xfffffffc;
177 : 0 : nScanlineSize += 4;
178 : : }
179 : :
180 : : // allocate buffer to hold header and scanlines, initialize to zero
181 : 0 : rOutSize = nHeaderSize + nScanlineSize*pImage->height;
182 : 0 : pBuffer = (sal_uInt8*)rtl_allocateZeroMemory( rOutSize );
183 : 0 : for( int y = 0; y < pImage->height; y++ )
184 : : {
185 : 0 : sal_uInt8* pScanline = pBuffer + nHeaderSize + (pImage->height-1-y)*nScanlineSize;
186 : 0 : for( int x = 0; x < pImage->width; x++ )
187 : : {
188 : 0 : unsigned long nPixel = XGetPixel( pImage, x, y );
189 : 0 : if( nPixel >= nColors )
190 : 0 : nColors = nPixel+1;
191 : 0 : X11_writeScanlinePixel( nPixel, pScanline, pImage->depth, x );
192 : : }
193 : : }
194 : :
195 : : // fill in header fields
196 : 0 : pBuffer[ 0 ] = 'B';
197 : 0 : pBuffer[ 1 ] = 'M';
198 : :
199 : 0 : writeLE( nHeaderSize, pBuffer+10 );
200 : 0 : writeLE( (sal_uInt32)40, pBuffer+14 );
201 : 0 : writeLE( (sal_uInt32)pImage->width, pBuffer+18 );
202 : 0 : writeLE( (sal_uInt32)pImage->height, pBuffer+22 );
203 : 0 : writeLE( (sal_uInt16)1, pBuffer+26 );
204 : 0 : writeLE( nBitCount, pBuffer+28 );
205 : 0 : writeLE( (sal_uInt32)(DisplayWidth(pDisplay,DefaultScreen(pDisplay))*1000/DisplayWidthMM(pDisplay,DefaultScreen(pDisplay))), pBuffer+38);
206 : 0 : writeLE( (sal_uInt32)(DisplayHeight(pDisplay,DefaultScreen(pDisplay))*1000/DisplayHeightMM(pDisplay,DefaultScreen(pDisplay))), pBuffer+42);
207 : 0 : writeLE( nColors, pBuffer+46 );
208 : 0 : writeLE( nColors, pBuffer+50 );
209 : :
210 : : XColor aColors[256];
211 : 0 : if( nColors > (1U << nBitCount) ) // paranoia
212 : 0 : nColors = (1U << nBitCount);
213 : 0 : for( unsigned long nPixel = 0; nPixel < nColors; nPixel++ )
214 : : {
215 : 0 : aColors[nPixel].flags = DoRed | DoGreen | DoBlue;
216 : 0 : aColors[nPixel].pixel = nPixel;
217 : : }
218 : 0 : XQueryColors( pDisplay, aColormap, aColors, nColors );
219 : 0 : for( sal_uInt32 i = 0; i < nColors; i++ )
220 : : {
221 : 0 : pBuffer[ 54 + i*4 ] = (sal_uInt8)(aColors[i].blue >> 8);
222 : 0 : pBuffer[ 55 + i*4 ] = (sal_uInt8)(aColors[i].green >> 8);
223 : 0 : pBuffer[ 56 + i*4 ] = (sal_uInt8)(aColors[i].red >> 8);
224 : : }
225 : :
226 : : // done
227 : :
228 : 0 : return pBuffer;
229 : : }
230 : :
231 : 0 : inline unsigned long doRightShift( unsigned long nValue, int nShift )
232 : : {
233 : 0 : return (nShift > 0) ? (nValue >> nShift) : (nValue << (-nShift));
234 : : }
235 : :
236 : 0 : inline unsigned long doLeftShift( unsigned long nValue, int nShift )
237 : : {
238 : 0 : return (nShift > 0) ? (nValue << nShift) : (nValue >> (-nShift));
239 : : }
240 : :
241 : 0 : static void getShift( unsigned long nMask, int& rShift, int& rSigBits, int& rShift2 )
242 : : {
243 : 0 : unsigned long nUseMask = nMask;
244 : 0 : rShift = 0;
245 : 0 : while( nMask & 0xffffff00 )
246 : : {
247 : 0 : rShift++;
248 : 0 : nMask >>= 1;
249 : : }
250 : 0 : if( rShift == 0 )
251 : 0 : while( ! (nMask & 0x00000080) )
252 : : {
253 : 0 : rShift--;
254 : 0 : nMask <<= 1;
255 : : }
256 : :
257 : 0 : int nRotate = sizeof(unsigned long)*8 - rShift;
258 : 0 : rSigBits = 0;
259 : 0 : nMask = doRightShift( nUseMask, rShift) ;
260 : 0 : while( nRotate-- )
261 : : {
262 : 0 : if( nMask & 1 )
263 : 0 : rSigBits++;
264 : 0 : nMask >>= 1;
265 : : }
266 : :
267 : 0 : rShift2 = 0;
268 : 0 : if( rSigBits < 8 )
269 : 0 : rShift2 = 8-rSigBits;
270 : 0 : }
271 : :
272 : 0 : static sal_uInt8* X11_getTCBmpFromImage(
273 : : Display* pDisplay,
274 : : XImage* pImage,
275 : : sal_Int32& rOutSize,
276 : : int nScreenNo
277 : : )
278 : : {
279 : : // get masks from visual info (guesswork)
280 : : XVisualInfo aVInfo;
281 : 0 : if( ! XMatchVisualInfo( pDisplay, nScreenNo, pImage->depth, TrueColor, &aVInfo ) )
282 : 0 : return NULL;
283 : :
284 : 0 : rOutSize = 0;
285 : :
286 : 0 : sal_uInt8* pBuffer = 0;
287 : 0 : sal_uInt32 nHeaderSize = 60;
288 : 0 : sal_uInt32 nScanlineSize = pImage->width*3;
289 : :
290 : : // adjust scan lines to begin on %4 boundaries
291 : 0 : if( nScanlineSize & 3 )
292 : : {
293 : 0 : nScanlineSize &= 0xfffffffc;
294 : 0 : nScanlineSize += 4;
295 : : }
296 : 0 : int nRedShift, nRedSig, nRedShift2 = 0;
297 : 0 : getShift( aVInfo.red_mask, nRedShift, nRedSig, nRedShift2 );
298 : 0 : int nGreenShift, nGreenSig, nGreenShift2 = 0;
299 : 0 : getShift( aVInfo.green_mask, nGreenShift, nGreenSig, nGreenShift2 );
300 : 0 : int nBlueShift, nBlueSig, nBlueShift2 = 0;
301 : 0 : getShift( aVInfo.blue_mask, nBlueShift, nBlueSig, nBlueShift2 );
302 : :
303 : : // allocate buffer to hold header and scanlines, initialize to zero
304 : 0 : rOutSize = nHeaderSize + nScanlineSize*pImage->height;
305 : 0 : pBuffer = (sal_uInt8*)rtl_allocateZeroMemory( rOutSize );
306 : 0 : for( int y = 0; y < pImage->height; y++ )
307 : : {
308 : 0 : sal_uInt8* pScanline = pBuffer + nHeaderSize + (pImage->height-1-y)*nScanlineSize;
309 : 0 : for( int x = 0; x < pImage->width; x++ )
310 : : {
311 : 0 : unsigned long nPixel = XGetPixel( pImage, x, y );
312 : :
313 : 0 : sal_uInt8 nValue = (sal_uInt8)doRightShift( nPixel&aVInfo.blue_mask, nBlueShift);
314 : 0 : if( nBlueShift2 )
315 : 0 : nValue |= (nValue >> nBlueShift2 );
316 : 0 : *pScanline++ = nValue;
317 : :
318 : 0 : nValue = (sal_uInt8)doRightShift( nPixel&aVInfo.green_mask, nGreenShift);
319 : 0 : if( nGreenShift2 )
320 : 0 : nValue |= (nValue >> nGreenShift2 );
321 : 0 : *pScanline++ = nValue;
322 : :
323 : 0 : nValue = (sal_uInt8)doRightShift( nPixel&aVInfo.red_mask, nRedShift);
324 : 0 : if( nRedShift2 )
325 : 0 : nValue |= (nValue >> nRedShift2 );
326 : 0 : *pScanline++ = nValue;
327 : : }
328 : : }
329 : :
330 : : // fill in header fields
331 : 0 : pBuffer[ 0 ] = 'B';
332 : 0 : pBuffer[ 1 ] = 'M';
333 : :
334 : 0 : writeLE( nHeaderSize, pBuffer+10 );
335 : 0 : writeLE( (sal_uInt32)40, pBuffer+14 );
336 : 0 : writeLE( (sal_uInt32)pImage->width, pBuffer+18 );
337 : 0 : writeLE( (sal_uInt32)pImage->height, pBuffer+22 );
338 : 0 : writeLE( (sal_uInt16)1, pBuffer+26 );
339 : 0 : writeLE( (sal_uInt16)24, pBuffer+28 );
340 : 0 : writeLE( (sal_uInt32)(DisplayWidth(pDisplay,DefaultScreen(pDisplay))*1000/DisplayWidthMM(pDisplay,DefaultScreen(pDisplay))), pBuffer+38);
341 : 0 : writeLE( (sal_uInt32)(DisplayHeight(pDisplay,DefaultScreen(pDisplay))*1000/DisplayHeightMM(pDisplay,DefaultScreen(pDisplay))), pBuffer+42);
342 : :
343 : : // done
344 : :
345 : 0 : return pBuffer;
346 : : }
347 : :
348 : 0 : sal_uInt8* x11::X11_getBmpFromPixmap(
349 : : Display* pDisplay,
350 : : Drawable aDrawable,
351 : : Colormap aColormap,
352 : : sal_Int32& rOutSize
353 : : )
354 : : {
355 : : // get geometry of drawable
356 : : XLIB_Window aRoot;
357 : : int x,y;
358 : : unsigned int w, h, bw, d;
359 : 0 : XGetGeometry( pDisplay, aDrawable, &aRoot, &x, &y, &w, &h, &bw, &d );
360 : :
361 : : // find which screen we are on
362 : 0 : int nScreenNo = ScreenCount( pDisplay );
363 : 0 : while( nScreenNo-- )
364 : : {
365 : 0 : if( RootWindow( pDisplay, nScreenNo ) == aRoot )
366 : 0 : break;
367 : : }
368 : 0 : if( nScreenNo < 0 )
369 : 0 : return NULL;
370 : :
371 : 0 : if( aColormap == None )
372 : 0 : aColormap = DefaultColormap( pDisplay, nScreenNo );
373 : :
374 : : // get the image
375 : 0 : XImage* pImage = XGetImage( pDisplay, aDrawable, 0, 0, w, h, AllPlanes, ZPixmap );
376 : 0 : if( ! pImage )
377 : 0 : return NULL;
378 : :
379 : : sal_uInt8* pBmp = d <= 8 ?
380 : : X11_getPaletteBmpFromImage( pDisplay, pImage, aColormap, rOutSize ) :
381 : 0 : X11_getTCBmpFromImage( pDisplay, pImage, rOutSize, nScreenNo );
382 : 0 : XDestroyImage( pImage );
383 : :
384 : 0 : return pBmp;
385 : : }
386 : :
387 : 0 : void x11::X11_freeBmp( sal_uInt8* pBmp )
388 : : {
389 : 0 : rtl_freeMemory( pBmp );
390 : 0 : }
391 : :
392 : : /*
393 : : * PixmapHolder
394 : : */
395 : :
396 : 0 : PixmapHolder::PixmapHolder( Display* pDisplay ) :
397 : : m_pDisplay( pDisplay ),
398 : : m_aColormap( None ),
399 : : m_aPixmap( None ),
400 : 0 : m_aBitmap( None )
401 : : {
402 : : /* try to get a 24 bit true color visual, if that fails,
403 : : * revert to default visual
404 : : */
405 : 0 : if( ! XMatchVisualInfo( m_pDisplay, DefaultScreen( m_pDisplay ), 24, TrueColor, &m_aInfo ) )
406 : : {
407 : : #if OSL_DEBUG_LEVEL > 1
408 : : fprintf( stderr, "PixmapHolder reverting to default visual\n" );
409 : : #endif
410 : 0 : Visual* pVisual = DefaultVisual( m_pDisplay, DefaultScreen( m_pDisplay ) );
411 : 0 : m_aInfo.screen = DefaultScreen( m_pDisplay );
412 : 0 : m_aInfo.visual = pVisual;
413 : 0 : m_aInfo.visualid = pVisual->visualid;
414 : 0 : m_aInfo.c_class = pVisual->c_class;
415 : 0 : m_aInfo.red_mask = pVisual->red_mask;
416 : 0 : m_aInfo.green_mask = pVisual->green_mask;
417 : 0 : m_aInfo.blue_mask = pVisual->blue_mask;
418 : 0 : m_aInfo.depth = DefaultDepth( m_pDisplay, m_aInfo.screen );
419 : : }
420 : 0 : m_aColormap = DefaultColormap( m_pDisplay, m_aInfo.screen );
421 : : #if OSL_DEBUG_LEVEL > 1
422 : : static const char* pClasses[] =
423 : : { "StaticGray", "GrayScale", "StaticColor", "PseudoColor", "TrueColor", "DirectColor" };
424 : : fprintf( stderr, "PixmapHolder visual: id = 0x%lx, class = %s (%d), depth=%d; color map = 0x%lx\n",
425 : : m_aInfo.visualid,
426 : : (m_aInfo.c_class >= 0 && unsigned(m_aInfo.c_class) < SAL_N_ELEMENTS(pClasses)) ? pClasses[m_aInfo.c_class] : "<unknown>",
427 : : m_aInfo.c_class,
428 : : m_aInfo.depth,
429 : : m_aColormap );
430 : : #endif
431 : 0 : if( m_aInfo.c_class == TrueColor )
432 : : {
433 : : int nRedSig, nGreenSig, nBlueSig;
434 : 0 : m_nRedShift = m_nRedShift2 = 0;
435 : 0 : getShift( m_aInfo.red_mask, m_nRedShift, nRedSig, m_nRedShift2 );
436 : 0 : m_nGreenShift = m_nGreenShift2 = 0;
437 : 0 : getShift( m_aInfo.green_mask, m_nGreenShift, nGreenSig, m_nGreenShift2 );
438 : 0 : m_nBlueShift = m_nBlueShift2 = 0;
439 : 0 : getShift( m_aInfo.blue_mask, m_nBlueShift, nBlueSig, m_nBlueShift2 );
440 : :
441 : 0 : m_nBlueShift2Mask = m_nBlueShift2 ? ~((unsigned long)((1<<m_nBlueShift2)-1)) : ~0L;
442 : 0 : m_nGreenShift2Mask = m_nGreenShift2 ? ~((unsigned long)((1<<m_nGreenShift2)-1)) : ~0L;
443 : 0 : m_nRedShift2Mask = m_nRedShift2 ? ~((unsigned long)((1<<m_nRedShift2)-1)) : ~0L;
444 : : }
445 : 0 : }
446 : :
447 : 0 : PixmapHolder::~PixmapHolder()
448 : : {
449 : 0 : if( m_aPixmap != None )
450 : 0 : XFreePixmap( m_pDisplay, m_aPixmap );
451 : 0 : if( m_aBitmap != None )
452 : 0 : XFreePixmap( m_pDisplay, m_aBitmap );
453 : 0 : }
454 : :
455 : 0 : unsigned long PixmapHolder::getTCPixel( sal_uInt8 r, sal_uInt8 g, sal_uInt8 b ) const
456 : : {
457 : 0 : unsigned long nPixel = 0;
458 : 0 : unsigned long nValue = (unsigned long)b;
459 : 0 : nValue &= m_nBlueShift2Mask;
460 : 0 : nPixel |= doLeftShift( nValue, m_nBlueShift );
461 : :
462 : 0 : nValue = (unsigned long)g;
463 : 0 : nValue &= m_nGreenShift2Mask;
464 : 0 : nPixel |= doLeftShift( nValue, m_nGreenShift );
465 : :
466 : 0 : nValue = (unsigned long)r;
467 : 0 : nValue &= m_nRedShift2Mask;
468 : 0 : nPixel |= doLeftShift( nValue, m_nRedShift );
469 : :
470 : 0 : return nPixel;
471 : : }
472 : :
473 : 0 : void PixmapHolder::setBitmapDataPalette( const sal_uInt8* pData, XImage* pImage )
474 : : {
475 : : // setup palette
476 : : XColor aPalette[256];
477 : :
478 : 0 : sal_uInt32 nColors = readLE32( pData+32 );
479 : 0 : sal_uInt32 nWidth = readLE32( pData+4 );
480 : 0 : sal_uInt32 nHeight = readLE32( pData+8 );
481 : 0 : sal_uInt16 nDepth = readLE16( pData+14 );
482 : :
483 : 0 : for( sal_uInt16 i = 0 ; i < nColors; i++ )
484 : : {
485 : 0 : if( m_aInfo.c_class != TrueColor )
486 : : {
487 : 0 : aPalette[i].red = ((unsigned short)pData[42 + i*4]) << 8 | ((unsigned short)pData[42 + i*4]);
488 : 0 : aPalette[i].green = ((unsigned short)pData[41 + i*4]) << 8 | ((unsigned short)pData[41 + i*4]);
489 : 0 : aPalette[i].blue = ((unsigned short)pData[40 + i*4]) << 8 | ((unsigned short)pData[40 + i*4]);
490 : 0 : XAllocColor( m_pDisplay, m_aColormap, aPalette+i );
491 : : }
492 : : else
493 : 0 : aPalette[i].pixel = getTCPixel( pData[42+i*4], pData[41+i*4], pData[40+i*4] );
494 : : }
495 : 0 : const sal_uInt8* pBMData = pData + readLE32( pData ) + 4*nColors;
496 : :
497 : 0 : sal_uInt32 nScanlineSize = 0;
498 : 0 : switch( nDepth )
499 : : {
500 : : case 1:
501 : 0 : nScanlineSize = (nWidth+31)/32;
502 : 0 : break;
503 : : case 4:
504 : 0 : nScanlineSize = (nWidth+1)/2;
505 : 0 : break;
506 : : case 8:
507 : 0 : nScanlineSize = nWidth;
508 : 0 : break;
509 : : }
510 : : // adjust scan lines to begin on %4 boundaries
511 : 0 : if( nScanlineSize & 3 )
512 : : {
513 : 0 : nScanlineSize &= 0xfffffffc;
514 : 0 : nScanlineSize += 4;
515 : : }
516 : :
517 : : // allocate buffer to hold header and scanlines, initialize to zero
518 : 0 : for( unsigned int y = 0; y < nHeight; y++ )
519 : : {
520 : 0 : const sal_uInt8* pScanline = pBMData + (nHeight-1-y)*nScanlineSize;
521 : 0 : for( unsigned int x = 0; x < nWidth; x++ )
522 : : {
523 : 0 : int nCol = 0;
524 : 0 : switch( nDepth )
525 : : {
526 : 0 : case 1: nCol = (pScanline[ x/8 ] & (0x80 >> (x&7))) != 0 ? 0 : 1; break;
527 : : case 4:
528 : 0 : if( x & 1 )
529 : 0 : nCol = (int)(pScanline[ x/2 ] >> 4);
530 : : else
531 : 0 : nCol = (int)(pScanline[ x/2 ] & 0x0f);
532 : 0 : break;
533 : 0 : case 8: nCol = (int)pScanline[x];
534 : : }
535 : 0 : XPutPixel( pImage, x, y, aPalette[nCol].pixel );
536 : : }
537 : : }
538 : 0 : }
539 : :
540 : 0 : void PixmapHolder::setBitmapDataTCDither( const sal_uInt8* pData, XImage* pImage )
541 : : {
542 : : XColor aPalette[216];
543 : :
544 : 0 : int nNonAllocs = 0;
545 : :
546 : 0 : for( int r = 0; r < 6; r++ )
547 : : {
548 : 0 : for( int g = 0; g < 6; g++ )
549 : : {
550 : 0 : for( int b = 0; b < 6; b++ )
551 : : {
552 : 0 : int i = r*36+g*6+b;
553 : 0 : aPalette[i].red = r == 5 ? 0xffff : r*10922;
554 : 0 : aPalette[i].green = g == 5 ? 0xffff : g*10922;
555 : 0 : aPalette[i].blue = b == 5 ? 0xffff : b*10922;
556 : 0 : aPalette[i].pixel = 0;
557 : 0 : if( ! XAllocColor( m_pDisplay, m_aColormap, aPalette+i ) )
558 : 0 : nNonAllocs++;
559 : : }
560 : : }
561 : : }
562 : :
563 : 0 : if( nNonAllocs )
564 : : {
565 : : XColor aRealPalette[256];
566 : 0 : int nColors = 1 << m_aInfo.depth;
567 : : int i;
568 : 0 : for( i = 0; i < nColors; i++ )
569 : 0 : aRealPalette[i].pixel = (unsigned long)i;
570 : 0 : XQueryColors( m_pDisplay, m_aColormap, aRealPalette, nColors );
571 : 0 : for( i = 0; i < nColors; i++ )
572 : : {
573 : : sal_uInt8 nIndex =
574 : : 36*(sal_uInt8)(aRealPalette[i].red/10923) +
575 : : 6*(sal_uInt8)(aRealPalette[i].green/10923) +
576 : 0 : (sal_uInt8)(aRealPalette[i].blue/10923);
577 : 0 : if( aPalette[nIndex].pixel == 0 )
578 : 0 : aPalette[nIndex] = aRealPalette[i];
579 : : }
580 : : }
581 : :
582 : 0 : sal_uInt32 nWidth = readLE32( pData+4 );
583 : 0 : sal_uInt32 nHeight = readLE32( pData+8 );
584 : :
585 : 0 : const sal_uInt8* pBMData = pData + readLE32( pData );
586 : 0 : sal_uInt32 nScanlineSize = nWidth*3;
587 : : // adjust scan lines to begin on %4 boundaries
588 : 0 : if( nScanlineSize & 3 )
589 : : {
590 : 0 : nScanlineSize &= 0xfffffffc;
591 : 0 : nScanlineSize += 4;
592 : : }
593 : :
594 : 0 : for( int y = 0; y < (int)nHeight; y++ )
595 : : {
596 : 0 : const sal_uInt8* pScanline = pBMData + (nHeight-1-(sal_uInt32)y)*nScanlineSize;
597 : 0 : for( int x = 0; x < (int)nWidth; x++ )
598 : : {
599 : 0 : sal_uInt8 b = pScanline[3*x];
600 : 0 : sal_uInt8 g = pScanline[3*x+1];
601 : 0 : sal_uInt8 r = pScanline[3*x+2];
602 : 0 : sal_uInt8 i = 36*(r/43) + 6*(g/43) + (b/43);
603 : :
604 : 0 : XPutPixel( pImage, x, y, aPalette[ i ].pixel );
605 : : }
606 : : }
607 : 0 : }
608 : :
609 : 0 : void PixmapHolder::setBitmapDataTC( const sal_uInt8* pData, XImage* pImage )
610 : : {
611 : 0 : sal_uInt32 nWidth = readLE32( pData+4 );
612 : 0 : sal_uInt32 nHeight = readLE32( pData+8 );
613 : :
614 : 0 : const sal_uInt8* pBMData = pData + readLE32( pData );
615 : 0 : sal_uInt32 nScanlineSize = nWidth*3;
616 : : // adjust scan lines to begin on %4 boundaries
617 : 0 : if( nScanlineSize & 3 )
618 : : {
619 : 0 : nScanlineSize &= 0xfffffffc;
620 : 0 : nScanlineSize += 4;
621 : : }
622 : :
623 : 0 : for( int y = 0; y < (int)nHeight; y++ )
624 : : {
625 : 0 : const sal_uInt8* pScanline = pBMData + (nHeight-1-(sal_uInt32)y)*nScanlineSize;
626 : 0 : for( int x = 0; x < (int)nWidth; x++ )
627 : : {
628 : 0 : unsigned long nPixel = getTCPixel( pScanline[3*x+2], pScanline[3*x+1], pScanline[3*x] );
629 : 0 : XPutPixel( pImage, x, y, nPixel );
630 : : }
631 : : }
632 : 0 : }
633 : :
634 : 0 : bool PixmapHolder::needsConversion( const sal_uInt8* pData )
635 : : {
636 : 0 : if( pData[0] != 'B' || pData[1] != 'M' )
637 : 0 : return true;
638 : :
639 : 0 : pData = pData+14;
640 : 0 : sal_uInt32 nDepth = readLE32( pData+14 );
641 : 0 : if( nDepth == 24 )
642 : : {
643 : 0 : if( m_aInfo.c_class != TrueColor )
644 : 0 : return true;
645 : : }
646 : 0 : else if( nDepth != (sal_uInt32)m_aInfo.depth )
647 : : {
648 : 0 : if( m_aInfo.c_class != TrueColor )
649 : 0 : return true;
650 : : }
651 : :
652 : 0 : return false;
653 : : }
654 : :
655 : 0 : Pixmap PixmapHolder::setBitmapData( const sal_uInt8* pData )
656 : : {
657 : 0 : if( pData[0] != 'B' || pData[1] != 'M' )
658 : 0 : return None;
659 : :
660 : 0 : pData = pData+14;
661 : :
662 : : // reject compressed data
663 : 0 : if( readLE32( pData + 16 ) != 0 )
664 : 0 : return None;
665 : :
666 : 0 : sal_uInt32 nWidth = readLE32( pData+4 );
667 : 0 : sal_uInt32 nHeight = readLE32( pData+8 );
668 : :
669 : 0 : if( m_aPixmap != None )
670 : 0 : XFreePixmap( m_pDisplay, m_aPixmap ), m_aPixmap = None;
671 : 0 : if( m_aBitmap != None )
672 : 0 : XFreePixmap( m_pDisplay, m_aBitmap ), m_aBitmap = None;
673 : :
674 : : m_aPixmap = limitXCreatePixmap( m_pDisplay,
675 : 0 : RootWindow( m_pDisplay, m_aInfo.screen ),
676 : 0 : nWidth, nHeight, m_aInfo.depth );
677 : :
678 : 0 : if( m_aPixmap != None )
679 : : {
680 : : XImage aImage;
681 : 0 : aImage.width = (int)nWidth;
682 : 0 : aImage.height = (int)nHeight;
683 : 0 : aImage.xoffset = 0;
684 : 0 : aImage.format = ZPixmap;
685 : 0 : aImage.data = NULL;
686 : 0 : aImage.byte_order = ImageByteOrder( m_pDisplay );
687 : 0 : aImage.bitmap_unit = BitmapUnit( m_pDisplay );
688 : 0 : aImage.bitmap_bit_order = BitmapBitOrder( m_pDisplay );
689 : 0 : aImage.bitmap_pad = BitmapPad( m_pDisplay );
690 : 0 : aImage.depth = m_aInfo.depth;
691 : 0 : aImage.red_mask = m_aInfo.red_mask;
692 : 0 : aImage.green_mask = m_aInfo.green_mask;
693 : 0 : aImage.blue_mask = m_aInfo.blue_mask;
694 : 0 : aImage.bytes_per_line = 0; // filled in by XInitImage
695 : 0 : if( m_aInfo.depth <= 8 )
696 : 0 : aImage.bits_per_pixel = m_aInfo.depth;
697 : : else
698 : 0 : aImage.bits_per_pixel = 8*((m_aInfo.depth+7)/8);
699 : 0 : aImage.obdata = NULL;
700 : :
701 : 0 : XInitImage( &aImage );
702 : 0 : aImage.data = (char*)rtl_allocateMemory( nHeight*aImage.bytes_per_line );
703 : :
704 : 0 : if( readLE32( pData+14 ) == 24 )
705 : : {
706 : 0 : if( m_aInfo.c_class == TrueColor )
707 : 0 : setBitmapDataTC( pData, &aImage );
708 : : else
709 : 0 : setBitmapDataTCDither( pData, &aImage );
710 : : }
711 : : else
712 : 0 : setBitmapDataPalette( pData, &aImage );
713 : :
714 : : // put the image
715 : : XPutImage( m_pDisplay,
716 : : m_aPixmap,
717 : 0 : DefaultGC( m_pDisplay, m_aInfo.screen ),
718 : : &aImage,
719 : : 0, 0,
720 : : 0, 0,
721 : 0 : nWidth, nHeight );
722 : :
723 : : // clean up
724 : 0 : rtl_freeMemory( aImage.data );
725 : :
726 : : // prepare bitmap (mask)
727 : : m_aBitmap = limitXCreatePixmap( m_pDisplay,
728 : 0 : RootWindow( m_pDisplay, m_aInfo.screen ),
729 : 0 : nWidth, nHeight, 1 );
730 : : XGCValues aVal;
731 : 0 : aVal.function = GXcopy;
732 : 0 : aVal.foreground = 0xffffffff;
733 : 0 : GC aGC = XCreateGC( m_pDisplay, m_aBitmap, GCFunction | GCForeground, &aVal );
734 : 0 : XFillRectangle( m_pDisplay, m_aBitmap, aGC, 0, 0, nWidth, nHeight );
735 : 0 : XFreeGC( m_pDisplay, aGC );
736 : : }
737 : :
738 : 0 : return m_aPixmap;
739 : : }
740 : :
741 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|