1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: iras.cxx,v $
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 ==================================
55 SvStream
* mpRAS
; // Die einzulesende RAS-Datei
59 BitmapWriteAccess
* mpAcc
;
60 sal_uInt32 mnWidth
, mnHeight
; // Bildausmass in Pixeln
61 USHORT mnDstBitsPerPix
;
63 sal_uInt32 mnDepth
, mnImageDatSize
, mnType
;
64 sal_uInt32 mnColorMapType
, mnColorMapSize
;
65 BYTE mnRepCount
, mnRepVal
; // RLE Decoding
69 BOOL
ImplReadHeader();
75 BOOL
ReadRAS( SvStream
& rRAS
, Graphic
& rGraphic
);
78 //=================== Methoden von RASReader ==============================
80 RASReader::RASReader() :
88 RASReader::~RASReader()
92 //----------------------------------------------------------------------------
94 BOOL
RASReader::ReadRAS( SvStream
& rRAS
, Graphic
& rGraphic
)
98 if ( rRAS
.GetError() )
102 mpRAS
->SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN
);
103 *mpRAS
>> nMagicNumber
;
104 if ( nMagicNumber
!= SUNRASTER_MAGICNUMBER
)
109 if ( ( mbStatus
= ImplReadHeader() ) == FALSE
)
112 maBmp
= Bitmap( Size( mnWidth
, mnHeight
), mnDstBitsPerPix
);
113 if ( ( mpAcc
= maBmp
.AcquireWriteAccess() ) == 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
)
130 if ( ( mnDstColors
>= 2 ) && ( ( mnColorMapSize
% 3 ) == 0 ) )
132 mpAcc
->SetPaletteEntryCount( mnDstColors
);
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
] ) );
148 else if ( mnColorMapType
!= RAS_COLOR_NO_MAP
) // alles andere ist kein standard
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
) );
164 if ( mnColorMapType
!= RAS_COLOR_NO_MAP
) // when graphic has more then 256 colors and a color map we skip
166 ULONG nCurPos
= mpRAS
->Tell();
167 mpRAS
->Seek( nCurPos
+ mnColorMapSize
);
171 // Bitmap-Daten einlesen
172 mbStatus
= ImplReadBody();
176 maBmp
.ReleaseAccess( mpAcc
), mpAcc
= NULL
;
184 //----------------------------------------------------------------------------
186 BOOL
RASReader::ImplReadHeader()
188 *mpRAS
>> mnWidth
>> mnHeight
>> mnDepth
>> mnImageDatSize
>>
189 mnType
>> mnColorMapType
>> mnColorMapSize
;
191 if ( mnWidth
== 0 || mnHeight
== 0 )
199 mnDstBitsPerPix
= (USHORT
)mnDepth
;
202 mnDstBitsPerPix
= 24;
212 case RAS_TYPE_STANDARD
:
213 case RAS_TYPE_RGB_FORMAT
:
214 case RAS_TYPE_BYTE_ENCODED
: // this type will be supported later
223 //----------------------------------------------------------------------------
225 BOOL
RASReader::ImplReadBody()
229 BYTE nRed
, nGreen
, nBlue
;
230 switch ( mnDstBitsPerPix
)
233 for ( y
= 0; y
< mnHeight
; y
++ )
235 for ( x
= 0; x
< mnWidth
; x
++ )
238 nDat
= ImplGetByte();
241 sal::static_int_cast
< BYTE
>(
242 nDat
>> ( ( x
& 7 ) ^ 7 )) );
244 if (!( ( x
- 1 ) & 0x8 ) ) ImplGetByte(); // WORD ALIGNMENT ???
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 ???
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();
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 ???
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();
301 nBlue
= ImplGetByte();
302 nGreen
= ImplGetByte();
303 nRed
= ImplGetByte();
305 mpAcc
->SetPixel ( y
, x
, BitmapColor( nRed
, nGreen
, nBlue
) );
319 //----------------------------------------------------------------------------
321 BYTE
RASReader::ImplGetByte()
324 if ( mnType
!= RAS_TYPE_BYTE_ENCODED
)
339 if ( nRetVal
!= 0x80 )
344 mnRepCount
= nRetVal
;
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 ==========================
366 static HINSTANCE hDLLInst
= 0; // HANDLE der DLL
368 extern "C" int CALLBACK
LibMain( HINSTANCE hDLL
, WORD
, WORD nHeap
, LPSTR
)
380 extern "C" int CALLBACK
WEP( int )