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 >> 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() ) == sal_False )
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 >> nRed[ i ];
124 0 : for ( i = 0; i < mnDstColors; i++ ) m_rRAS >> nGreen[ i ];
125 0 : for ( i = 0; i < mnDstColors; i++ ) m_rRAS >> 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 >> mnWidth >> mnHeight >> mnDepth >> mnImageDatSize >>
177 0 : mnType >> mnColorMapType >> mnColorMapSize;
178 :
179 0 : if ( mnWidth == 0 || mnHeight == 0 )
180 0 : mbStatus = sal_False;
181 :
182 0 : switch ( mnDepth )
183 : {
184 : case 24 :
185 : case 8 :
186 : case 1 :
187 0 : mnDstBitsPerPix = (sal_uInt16)mnDepth;
188 0 : break;
189 : case 32 :
190 0 : mnDstBitsPerPix = 24;
191 0 : break;
192 :
193 : default :
194 0 : mbStatus = sal_False;
195 : }
196 :
197 0 : switch ( mnType )
198 : {
199 : case RAS_TYPE_OLD :
200 : case RAS_TYPE_STANDARD :
201 : case RAS_TYPE_RGB_FORMAT :
202 : case RAS_TYPE_BYTE_ENCODED : // this type will be supported later
203 0 : break;
204 :
205 : default:
206 0 : mbStatus = sal_False;
207 : }
208 0 : return mbStatus;
209 : }
210 :
211 : //----------------------------------------------------------------------------
212 :
213 0 : sal_Bool RASReader::ImplReadBody()
214 : {
215 : sal_uLong x, y;
216 0 : sal_uInt8 nDat = 0;
217 : sal_uInt8 nRed, nGreen, nBlue;
218 0 : switch ( mnDstBitsPerPix )
219 : {
220 : case 1 :
221 0 : for ( y = 0; y < mnHeight; y++ )
222 : {
223 0 : for ( x = 0; x < mnWidth; x++ )
224 : {
225 0 : if (!(x & 7))
226 0 : nDat = ImplGetByte();
227 : mpAcc->SetPixel (
228 : y, x,
229 : sal::static_int_cast< sal_uInt8 >(
230 0 : nDat >> ( ( x & 7 ) ^ 7 )) );
231 : }
232 0 : if (!( ( x - 1 ) & 0x8 ) ) ImplGetByte(); // WORD ALIGNMENT ???
233 : }
234 0 : break;
235 :
236 : case 8 :
237 0 : for ( y = 0; y < mnHeight; y++ )
238 : {
239 0 : for ( x = 0; x < mnWidth; x++ )
240 : {
241 0 : nDat = ImplGetByte();
242 0 : mpAcc->SetPixel ( y, x, nDat );
243 : }
244 0 : if ( x & 1 ) ImplGetByte(); // WORD ALIGNMENT ???
245 : }
246 0 : break;
247 :
248 : case 24 :
249 0 : switch ( mnDepth )
250 : {
251 :
252 : case 24 :
253 0 : for ( y = 0; y < mnHeight; y++ )
254 : {
255 0 : for ( x = 0; x < mnWidth; x++ )
256 : {
257 0 : if ( mnType == RAS_TYPE_RGB_FORMAT )
258 : {
259 0 : nRed = ImplGetByte();
260 0 : nGreen = ImplGetByte();
261 0 : nBlue = ImplGetByte();
262 : }
263 : else
264 : {
265 0 : nBlue = ImplGetByte();
266 0 : nGreen = ImplGetByte();
267 0 : nRed = ImplGetByte();
268 : }
269 0 : mpAcc->SetPixel ( y, x, BitmapColor( nRed, nGreen, nBlue ) );
270 : }
271 0 : if ( x & 1 ) ImplGetByte(); // WORD ALIGNMENT ???
272 : }
273 0 : break;
274 :
275 : case 32 :
276 0 : for ( y = 0; y < mnHeight; y++ )
277 : {
278 0 : for ( x = 0; x < mnWidth; x++ )
279 : {
280 0 : nDat = ImplGetByte(); // pad byte > nil
281 0 : if ( mnType == RAS_TYPE_RGB_FORMAT )
282 : {
283 0 : nRed = ImplGetByte();
284 0 : nGreen = ImplGetByte();
285 0 : nBlue = ImplGetByte();
286 : }
287 : else
288 : {
289 0 : nBlue = ImplGetByte();
290 0 : nGreen = ImplGetByte();
291 0 : nRed = ImplGetByte();
292 : }
293 0 : mpAcc->SetPixel ( y, x, BitmapColor( nRed, nGreen, nBlue ) );
294 : }
295 : }
296 0 : break;
297 : }
298 0 : break;
299 :
300 : default:
301 0 : mbStatus = sal_False;
302 0 : break;
303 : }
304 0 : return mbStatus;
305 : }
306 :
307 : //----------------------------------------------------------------------------
308 :
309 0 : sal_uInt8 RASReader::ImplGetByte()
310 : {
311 : sal_uInt8 nRetVal;
312 0 : if ( mnType != RAS_TYPE_BYTE_ENCODED )
313 : {
314 0 : m_rRAS >> nRetVal;
315 0 : return nRetVal;
316 : }
317 : else
318 : {
319 0 : if ( mnRepCount )
320 : {
321 0 : mnRepCount--;
322 0 : return mnRepVal;
323 : }
324 : else
325 : {
326 0 : m_rRAS >> nRetVal;
327 0 : if ( nRetVal != 0x80 )
328 0 : return nRetVal;
329 0 : m_rRAS >> nRetVal;
330 0 : if ( nRetVal == 0 )
331 0 : return 0x80;
332 0 : mnRepCount = nRetVal ;
333 0 : m_rRAS >> mnRepVal;
334 0 : return mnRepVal;
335 : }
336 : }
337 : }
338 :
339 : //================== GraphicImport - die exportierte Funktion ================
340 :
341 : #ifdef DISABLE_DYNLOADING
342 : #define GraphicImport iraGraphicImport
343 : #endif
344 :
345 : extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL
346 0 : GraphicImport(SvStream & rStream, Graphic & rGraphic, FilterConfigItem*, sal_Bool)
347 : {
348 0 : RASReader aRASReader(rStream);
349 :
350 0 : return aRASReader.ReadRAS(rGraphic );
351 : }
352 :
353 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|