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