merge the formfield patch from ooo-build
[ooovba.git] / goodies / source / filter.vcl / iras / iras.cxx
blob72405920b924396ceebf3f7aeee6b07635d604af
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: iras.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_goodies.hxx"
34 #include <vcl/graph.hxx>
35 #include <vcl/bmpacc.hxx>
36 #include <svtools/fltcall.hxx>
38 #define RAS_TYPE_OLD 0x00000000 // supported formats by this filter
39 #define RAS_TYPE_STANDARD 0x00000001
40 #define RAS_TYPE_BYTE_ENCODED 0x00000002
41 #define RAS_TYPE_RGB_FORMAT 0x00000003
43 #define RAS_COLOR_NO_MAP 0x00000000
44 #define RAS_COLOR_RGB_MAP 0x00000001
45 #define RAS_COLOR_RAW_MAP 0x00000002
47 #define SUNRASTER_MAGICNUMBER 0x59a66a95
49 //============================ RASReader ==================================
51 class RASReader {
53 private:
55 SvStream* mpRAS; // Die einzulesende RAS-Datei
57 BOOL mbStatus;
58 Bitmap maBmp;
59 BitmapWriteAccess* mpAcc;
60 sal_uInt32 mnWidth, mnHeight; // Bildausmass in Pixeln
61 USHORT mnDstBitsPerPix;
62 USHORT mnDstColors;
63 sal_uInt32 mnDepth, mnImageDatSize, mnType;
64 sal_uInt32 mnColorMapType, mnColorMapSize;
65 BYTE mnRepCount, mnRepVal; // RLE Decoding
66 BOOL mbPalette;
68 BOOL ImplReadBody();
69 BOOL ImplReadHeader();
70 BYTE ImplGetByte();
72 public:
73 RASReader();
74 ~RASReader();
75 BOOL ReadRAS( SvStream & rRAS, Graphic & rGraphic );
78 //=================== Methoden von RASReader ==============================
80 RASReader::RASReader() :
81 mbStatus ( TRUE ),
82 mpAcc ( NULL ),
83 mnRepCount ( 0 ),
84 mbPalette ( FALSE )
88 RASReader::~RASReader()
92 //----------------------------------------------------------------------------
94 BOOL RASReader::ReadRAS( SvStream & rRAS, Graphic & rGraphic )
96 UINT32 nMagicNumber;
98 if ( rRAS.GetError() )
99 return FALSE;
101 mpRAS = &rRAS;
102 mpRAS->SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
103 *mpRAS >> nMagicNumber;
104 if ( nMagicNumber != SUNRASTER_MAGICNUMBER )
105 return FALSE;
107 // Kopf einlesen:
109 if ( ( mbStatus = ImplReadHeader() ) == FALSE )
110 return FALSE;
112 maBmp = Bitmap( Size( mnWidth, mnHeight ), mnDstBitsPerPix );
113 if ( ( mpAcc = maBmp.AcquireWriteAccess() ) == FALSE )
114 return FALSE;
116 if ( mnDstBitsPerPix <= 8 ) // paletten bildchen
118 if ( mnColorMapType == RAS_COLOR_RAW_MAP ) // RAW Colormap wird geskipped
120 ULONG nCurPos = mpRAS->Tell();
121 mpRAS->Seek( nCurPos + mnColorMapSize );
123 else if ( mnColorMapType == RAS_COLOR_RGB_MAP ) // RGB koennen wir auslesen
125 mnDstColors = (USHORT)( mnColorMapSize / 3 );
127 if ( ( 1 << mnDstBitsPerPix ) < mnDstColors )
128 return FALSE;
130 if ( ( mnDstColors >= 2 ) && ( ( mnColorMapSize % 3 ) == 0 ) )
132 mpAcc->SetPaletteEntryCount( mnDstColors );
133 USHORT i;
134 BYTE nRed[256], nGreen[256], nBlue[256];
135 for ( i = 0; i < mnDstColors; i++ ) *mpRAS >> nRed[ i ];
136 for ( i = 0; i < mnDstColors; i++ ) *mpRAS >> nGreen[ i ];
137 for ( i = 0; i < mnDstColors; i++ ) *mpRAS >> nBlue[ i ];
138 for ( i = 0; i < mnDstColors; i++ )
140 mpAcc->SetPaletteColor( i, BitmapColor( nRed[ i ], nGreen[ i ], nBlue[ i ] ) );
142 mbPalette = TRUE;
144 else
145 return FALSE;
148 else if ( mnColorMapType != RAS_COLOR_NO_MAP ) // alles andere ist kein standard
149 return FALSE;
151 if ( !mbPalette )
153 mnDstColors = 1 << mnDstBitsPerPix;
154 mpAcc->SetPaletteEntryCount( mnDstColors );
155 for ( USHORT i = 0; i < mnDstColors; i++ )
157 ULONG nCount = 255 - ( 255 * i / ( mnDstColors - 1 ) );
158 mpAcc->SetPaletteColor( i, BitmapColor( (BYTE)nCount, (BYTE)nCount, (BYTE)nCount ) );
162 else
164 if ( mnColorMapType != RAS_COLOR_NO_MAP ) // when graphic has more then 256 colors and a color map we skip
165 { // the colormap
166 ULONG nCurPos = mpRAS->Tell();
167 mpRAS->Seek( nCurPos + mnColorMapSize );
171 // Bitmap-Daten einlesen
172 mbStatus = ImplReadBody();
174 if ( mpAcc )
176 maBmp.ReleaseAccess( mpAcc ), mpAcc = NULL;
178 if ( mbStatus )
179 rGraphic = maBmp;
181 return mbStatus;
184 //----------------------------------------------------------------------------
186 BOOL RASReader::ImplReadHeader()
188 *mpRAS >> mnWidth >> mnHeight >> mnDepth >> mnImageDatSize >>
189 mnType >> mnColorMapType >> mnColorMapSize;
191 if ( mnWidth == 0 || mnHeight == 0 )
192 mbStatus = FALSE;
194 switch ( mnDepth )
196 case 24 :
197 case 8 :
198 case 1 :
199 mnDstBitsPerPix = (USHORT)mnDepth;
200 break;
201 case 32 :
202 mnDstBitsPerPix = 24;
203 break;
205 default :
206 mbStatus = FALSE;
209 switch ( mnType )
211 case RAS_TYPE_OLD :
212 case RAS_TYPE_STANDARD :
213 case RAS_TYPE_RGB_FORMAT :
214 case RAS_TYPE_BYTE_ENCODED : // this type will be supported later
215 break;
217 default:
218 mbStatus = FALSE;
220 return mbStatus;
223 //----------------------------------------------------------------------------
225 BOOL RASReader::ImplReadBody()
227 ULONG x, y;
228 BYTE nDat = 0;
229 BYTE nRed, nGreen, nBlue;
230 switch ( mnDstBitsPerPix )
232 case 1 :
233 for ( y = 0; y < mnHeight; y++ )
235 for ( x = 0; x < mnWidth; x++ )
237 if (!(x & 7))
238 nDat = ImplGetByte();
239 mpAcc->SetPixel (
240 y, x,
241 sal::static_int_cast< BYTE >(
242 nDat >> ( ( x & 7 ) ^ 7 )) );
244 if (!( ( x - 1 ) & 0x8 ) ) ImplGetByte(); // WORD ALIGNMENT ???
246 break;
248 case 8 :
249 for ( y = 0; y < mnHeight; y++ )
251 for ( x = 0; x < mnWidth; x++ )
253 nDat = ImplGetByte();
254 mpAcc->SetPixel ( y, x, nDat );
256 if ( x & 1 ) ImplGetByte(); // WORD ALIGNMENT ???
258 break;
260 case 24 :
261 switch ( mnDepth )
264 case 24 :
265 for ( y = 0; y < mnHeight; y++ )
267 for ( x = 0; x < mnWidth; x++ )
269 if ( mnType == RAS_TYPE_RGB_FORMAT )
271 nRed = ImplGetByte();
272 nGreen = ImplGetByte();
273 nBlue = ImplGetByte();
275 else
277 nBlue = ImplGetByte();
278 nGreen = ImplGetByte();
279 nRed = ImplGetByte();
281 mpAcc->SetPixel ( y, x, BitmapColor( nRed, nGreen, nBlue ) );
283 if ( x & 1 ) ImplGetByte(); // WORD ALIGNMENT ???
285 break;
287 case 32 :
288 for ( y = 0; y < mnHeight; y++ )
290 for ( x = 0; x < mnWidth; x++ )
292 nDat = ImplGetByte(); // pad byte > nil
293 if ( mnType == RAS_TYPE_RGB_FORMAT )
295 nRed = ImplGetByte();
296 nGreen = ImplGetByte();
297 nBlue = ImplGetByte();
299 else
301 nBlue = ImplGetByte();
302 nGreen = ImplGetByte();
303 nRed = ImplGetByte();
305 mpAcc->SetPixel ( y, x, BitmapColor( nRed, nGreen, nBlue ) );
308 break;
310 break;
312 default:
313 mbStatus = FALSE;
314 break;
316 return mbStatus;
319 //----------------------------------------------------------------------------
321 BYTE RASReader::ImplGetByte()
323 BYTE nRetVal;
324 if ( mnType != RAS_TYPE_BYTE_ENCODED )
326 *mpRAS >> nRetVal;
327 return nRetVal;
329 else
331 if ( mnRepCount )
333 mnRepCount--;
334 return mnRepVal;
336 else
338 *mpRAS >> nRetVal;
339 if ( nRetVal != 0x80 )
340 return nRetVal;
341 *mpRAS >> nRetVal;
342 if ( nRetVal == 0 )
343 return 0x80;
344 mnRepCount = nRetVal ;
345 *mpRAS >> mnRepVal;
346 return mnRepVal;
351 //================== GraphicImport - die exportierte Funktion ================
353 extern "C" BOOL __LOADONCALLAPI GraphicImport(SvStream & rStream, Graphic & rGraphic, FilterConfigItem*, BOOL )
355 RASReader aRASReader;
357 return aRASReader.ReadRAS( rStream, rGraphic );
360 //================== ein bischen Muell fuer Windows ==========================
361 #ifndef GCC
362 #endif
364 #ifdef WIN
366 static HINSTANCE hDLLInst = 0; // HANDLE der DLL
368 extern "C" int CALLBACK LibMain( HINSTANCE hDLL, WORD, WORD nHeap, LPSTR )
370 #ifndef WNT
371 if ( nHeap )
372 UnlockData( 0 );
373 #endif
375 hDLLInst = hDLL;
377 return TRUE;
380 extern "C" int CALLBACK WEP( int )
382 return 1;
385 #endif