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 <vcl/graph.hxx>
22 : #include <vcl/bmpacc.hxx>
23 :
24 : class FilterConfigItem;
25 :
26 : #define RAS_TYPE_OLD 0x00000000 // supported formats by this filter
27 : #define RAS_TYPE_STANDARD 0x00000001
28 : #define RAS_TYPE_BYTE_ENCODED 0x00000002
29 : #define RAS_TYPE_RGB_FORMAT 0x00000003
30 :
31 : #define RAS_COLOR_NO_MAP 0x00000000
32 : #define RAS_COLOR_RGB_MAP 0x00000001
33 : #define RAS_COLOR_RAW_MAP 0x00000002
34 :
35 : #define SUNRASTER_MAGICNUMBER 0x59a66a95
36 :
37 : //============================ RASReader ==================================
38 :
39 : class RASReader {
40 :
41 : private:
42 :
43 : SvStream& m_rRAS; // Die einzulesende RAS-Datei
44 :
45 : sal_Bool mbStatus;
46 : Bitmap maBmp;
47 : BitmapWriteAccess* mpAcc;
48 : sal_uInt32 mnWidth, mnHeight; // Bildausmass in Pixeln
49 : sal_uInt16 mnDstBitsPerPix;
50 : sal_uInt16 mnDstColors;
51 : sal_uInt32 mnDepth, mnImageDatSize, mnType;
52 : sal_uInt32 mnColorMapType, mnColorMapSize;
53 : sal_uInt8 mnRepCount, mnRepVal; // RLE Decoding
54 : sal_Bool mbPalette;
55 :
56 : sal_Bool ImplReadBody();
57 : sal_Bool ImplReadHeader();
58 : sal_uInt8 ImplGetByte();
59 :
60 : public:
61 : RASReader(SvStream &rRAS);
62 : ~RASReader();
63 : sal_Bool ReadRAS(Graphic & rGraphic);
64 : };
65 :
66 : //=================== Methoden von RASReader ==============================
67 :
68 0 : RASReader::RASReader(SvStream &rRAS)
69 : : m_rRAS(rRAS)
70 : , mbStatus(sal_True)
71 : , mpAcc(NULL)
72 : , mnRepCount(0)
73 0 : , mbPalette(sal_False)
74 : {
75 0 : }
76 :
77 0 : RASReader::~RASReader()
78 : {
79 0 : }
80 :
81 :
82 :
83 0 : sal_Bool RASReader::ReadRAS(Graphic & rGraphic)
84 : {
85 : sal_uInt32 nMagicNumber;
86 :
87 0 : if ( m_rRAS.GetError() )
88 0 : return sal_False;
89 :
90 0 : m_rRAS.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
91 0 : m_rRAS.ReadUInt32( nMagicNumber );
92 0 : if ( nMagicNumber != SUNRASTER_MAGICNUMBER )
93 0 : return sal_False;
94 :
95 : // Kopf einlesen:
96 :
97 0 : if ( ( mbStatus = ImplReadHeader() ) == sal_False )
98 0 : return sal_False;
99 :
100 0 : maBmp = Bitmap( Size( mnWidth, mnHeight ), mnDstBitsPerPix );
101 0 : if ( ( mpAcc = maBmp.AcquireWriteAccess() ) == 0 )
102 0 : return sal_False;
103 :
104 0 : if ( mnDstBitsPerPix <= 8 ) // paletten bildchen
105 : {
106 0 : if ( mnColorMapType == RAS_COLOR_RAW_MAP ) // RAW Colormap wird geskipped
107 : {
108 0 : sal_uLong nCurPos = m_rRAS.Tell();
109 0 : m_rRAS.Seek( nCurPos + mnColorMapSize );
110 : }
111 0 : else if ( mnColorMapType == RAS_COLOR_RGB_MAP ) // RGB koennen wir auslesen
112 : {
113 0 : mnDstColors = (sal_uInt16)( mnColorMapSize / 3 );
114 :
115 0 : if ( ( 1 << mnDstBitsPerPix ) < mnDstColors )
116 0 : return sal_False;
117 :
118 0 : if ( ( mnDstColors >= 2 ) && ( ( mnColorMapSize % 3 ) == 0 ) )
119 : {
120 0 : mpAcc->SetPaletteEntryCount( mnDstColors );
121 : sal_uInt16 i;
122 : sal_uInt8 nRed[256], nGreen[256], nBlue[256];
123 0 : for ( i = 0; i < mnDstColors; i++ ) m_rRAS.ReadUChar( nRed[ i ] );
124 0 : for ( i = 0; i < mnDstColors; i++ ) m_rRAS.ReadUChar( nGreen[ i ] );
125 0 : for ( i = 0; i < mnDstColors; i++ ) m_rRAS.ReadUChar( nBlue[ i ] );
126 0 : for ( i = 0; i < mnDstColors; i++ )
127 : {
128 0 : mpAcc->SetPaletteColor( i, BitmapColor( nRed[ i ], nGreen[ i ], nBlue[ i ] ) );
129 : }
130 0 : mbPalette = sal_True;
131 : }
132 : else
133 0 : return sal_False;
134 :
135 : }
136 0 : else if ( mnColorMapType != RAS_COLOR_NO_MAP ) // alles andere ist kein standard
137 0 : return sal_False;
138 :
139 0 : if ( !mbPalette )
140 : {
141 0 : mnDstColors = 1 << mnDstBitsPerPix;
142 0 : mpAcc->SetPaletteEntryCount( mnDstColors );
143 0 : for ( sal_uInt16 i = 0; i < mnDstColors; i++ )
144 : {
145 0 : sal_uLong nCount = 255 - ( 255 * i / ( mnDstColors - 1 ) );
146 0 : mpAcc->SetPaletteColor( i, BitmapColor( (sal_uInt8)nCount, (sal_uInt8)nCount, (sal_uInt8)nCount ) );
147 : }
148 : }
149 : }
150 : else
151 : {
152 0 : if ( mnColorMapType != RAS_COLOR_NO_MAP ) // when graphic has more then 256 colors and a color map we skip
153 : { // the colormap
154 0 : sal_uLong nCurPos = m_rRAS.Tell();
155 0 : m_rRAS.Seek( nCurPos + mnColorMapSize );
156 : }
157 : }
158 :
159 : // Bitmap-Daten einlesen
160 0 : mbStatus = ImplReadBody();
161 :
162 0 : if ( mpAcc )
163 : {
164 0 : maBmp.ReleaseAccess( mpAcc ), mpAcc = NULL;
165 : }
166 0 : if ( mbStatus )
167 0 : rGraphic = maBmp;
168 :
169 0 : return mbStatus;
170 : }
171 :
172 :
173 :
174 0 : sal_Bool RASReader::ImplReadHeader()
175 : {
176 0 : m_rRAS.ReadUInt32( mnWidth ).ReadUInt32( mnHeight ).ReadUInt32( mnDepth ).ReadUInt32( mnImageDatSize ). ReadUInt32( mnType ).ReadUInt32( mnColorMapType ).ReadUInt32( mnColorMapSize );
177 :
178 0 : if ( mnWidth == 0 || mnHeight == 0 )
179 0 : mbStatus = sal_False;
180 :
181 0 : switch ( mnDepth )
182 : {
183 : case 24 :
184 : case 8 :
185 : case 1 :
186 0 : mnDstBitsPerPix = (sal_uInt16)mnDepth;
187 0 : break;
188 : case 32 :
189 0 : mnDstBitsPerPix = 24;
190 0 : break;
191 :
192 : default :
193 0 : mbStatus = sal_False;
194 : }
195 :
196 0 : switch ( mnType )
197 : {
198 : case RAS_TYPE_OLD :
199 : case RAS_TYPE_STANDARD :
200 : case RAS_TYPE_RGB_FORMAT :
201 : case RAS_TYPE_BYTE_ENCODED : // this type will be supported later
202 0 : break;
203 :
204 : default:
205 0 : mbStatus = sal_False;
206 : }
207 0 : return mbStatus;
208 : }
209 :
210 :
211 :
212 0 : sal_Bool RASReader::ImplReadBody()
213 : {
214 : sal_uLong x, y;
215 0 : sal_uInt8 nDat = 0;
216 : sal_uInt8 nRed, nGreen, nBlue;
217 0 : switch ( mnDstBitsPerPix )
218 : {
219 : case 1 :
220 0 : for ( y = 0; y < mnHeight; y++ )
221 : {
222 0 : for ( x = 0; x < mnWidth; x++ )
223 : {
224 0 : if (!(x & 7))
225 0 : nDat = ImplGetByte();
226 : mpAcc->SetPixelIndex( y, x,
227 : sal::static_int_cast< sal_uInt8 >(
228 0 : nDat >> ( ( x & 7 ) ^ 7 )) );
229 : }
230 0 : if (!( ( x - 1 ) & 0x8 ) ) ImplGetByte(); // WORD ALIGNMENT ???
231 : }
232 0 : break;
233 :
234 : case 8 :
235 0 : for ( y = 0; y < mnHeight; y++ )
236 : {
237 0 : for ( x = 0; x < mnWidth; x++ )
238 : {
239 0 : nDat = ImplGetByte();
240 0 : mpAcc->SetPixelIndex( y, x, nDat );
241 : }
242 0 : if ( x & 1 ) ImplGetByte(); // WORD ALIGNMENT ???
243 : }
244 0 : break;
245 :
246 : case 24 :
247 0 : switch ( mnDepth )
248 : {
249 :
250 : case 24 :
251 0 : for ( y = 0; y < mnHeight; y++ )
252 : {
253 0 : for ( x = 0; x < mnWidth; x++ )
254 : {
255 0 : if ( mnType == RAS_TYPE_RGB_FORMAT )
256 : {
257 0 : nRed = ImplGetByte();
258 0 : nGreen = ImplGetByte();
259 0 : nBlue = ImplGetByte();
260 : }
261 : else
262 : {
263 0 : nBlue = ImplGetByte();
264 0 : nGreen = ImplGetByte();
265 0 : nRed = ImplGetByte();
266 : }
267 0 : mpAcc->SetPixel ( y, x, BitmapColor( nRed, nGreen, nBlue ) );
268 : }
269 0 : if ( x & 1 ) ImplGetByte(); // WORD ALIGNMENT ???
270 : }
271 0 : break;
272 :
273 : case 32 :
274 0 : for ( y = 0; y < mnHeight; y++ )
275 : {
276 0 : for ( x = 0; x < mnWidth; x++ )
277 : {
278 0 : nDat = ImplGetByte(); // pad byte > nil
279 0 : if ( mnType == RAS_TYPE_RGB_FORMAT )
280 : {
281 0 : nRed = ImplGetByte();
282 0 : nGreen = ImplGetByte();
283 0 : nBlue = ImplGetByte();
284 : }
285 : else
286 : {
287 0 : nBlue = ImplGetByte();
288 0 : nGreen = ImplGetByte();
289 0 : nRed = ImplGetByte();
290 : }
291 0 : mpAcc->SetPixel ( y, x, BitmapColor( nRed, nGreen, nBlue ) );
292 : }
293 : }
294 0 : break;
295 : }
296 0 : break;
297 :
298 : default:
299 0 : mbStatus = sal_False;
300 0 : break;
301 : }
302 0 : return mbStatus;
303 : }
304 :
305 :
306 :
307 0 : sal_uInt8 RASReader::ImplGetByte()
308 : {
309 : sal_uInt8 nRetVal;
310 0 : if ( mnType != RAS_TYPE_BYTE_ENCODED )
311 : {
312 0 : m_rRAS.ReadUChar( nRetVal );
313 0 : return nRetVal;
314 : }
315 : else
316 : {
317 0 : if ( mnRepCount )
318 : {
319 0 : mnRepCount--;
320 0 : return mnRepVal;
321 : }
322 : else
323 : {
324 0 : m_rRAS.ReadUChar( nRetVal );
325 0 : if ( nRetVal != 0x80 )
326 0 : return nRetVal;
327 0 : m_rRAS.ReadUChar( nRetVal );
328 0 : if ( nRetVal == 0 )
329 0 : return 0x80;
330 0 : mnRepCount = nRetVal ;
331 0 : m_rRAS.ReadUChar( mnRepVal );
332 0 : return mnRepVal;
333 : }
334 : }
335 : }
336 :
337 : //================== GraphicImport - die exportierte Funktion ================
338 :
339 : // this needs to be kept in sync with
340 : // ImpFilterLibCacheEntry::GetImportFunction() from
341 : // vcl/source/filter/graphicfilter.cxx
342 : #if defined(DISABLE_DYNLOADING)
343 : #define GraphicImport iraGraphicImport
344 : #endif
345 :
346 : extern "C" SAL_DLLPUBLIC_EXPORT bool SAL_CALL
347 0 : GraphicImport( SvStream & rStream, Graphic & rGraphic, FilterConfigItem* )
348 : {
349 0 : RASReader aRASReader(rStream);
350 :
351 0 : return aRASReader.ReadRAS(rGraphic );
352 : }
353 :
354 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|