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 : #include <sal/config.h>
21 :
22 : #include <vcl/FilterConfigItem.hxx>
23 : #include <vcl/graph.hxx>
24 : #include <vcl/bmpacc.hxx>
25 :
26 : //============================ PBMReader ==================================
27 :
28 : class PBMReader {
29 :
30 : private:
31 :
32 : SvStream& mrPBM; // the PBM file to read
33 :
34 : bool mbStatus;
35 : bool mbRemark; // sal_False if the stream is in a comment
36 : bool mbRaw; // RAW/ASCII MODE
37 : sal_uLong mnMode; // 0->PBM, 1->PGM, 2->PPM
38 : Bitmap maBmp;
39 : BitmapWriteAccess* mpAcc;
40 : sal_uLong mnWidth, mnHeight; // dimensions in pixel
41 : sal_uLong mnCol;
42 : sal_uLong mnMaxVal; // max value in the <missing comment>
43 : bool ImplReadBody();
44 : bool ImplReadHeader();
45 :
46 : public:
47 : PBMReader(SvStream & rPBM);
48 : ~PBMReader();
49 : bool ReadPBM(Graphic & rGraphic );
50 : };
51 :
52 : //=================== Methods of PBMReader ==============================
53 :
54 28 : PBMReader::PBMReader(SvStream & rPBM)
55 : : mrPBM(rPBM)
56 : , mbStatus(true)
57 : , mbRemark(false)
58 : , mbRaw(true)
59 : , mnMode(0)
60 : , mpAcc(NULL)
61 : , mnWidth(0)
62 : , mnHeight(0)
63 : , mnCol(0)
64 28 : , mnMaxVal(0)
65 : {
66 28 : }
67 :
68 28 : PBMReader::~PBMReader()
69 : {
70 28 : }
71 :
72 28 : bool PBMReader::ReadPBM(Graphic & rGraphic )
73 : {
74 : sal_uInt16 i;
75 :
76 28 : if ( mrPBM.GetError() )
77 0 : return false;
78 :
79 28 : mrPBM.SetEndian( SvStreamEndian::LITTLE );
80 :
81 : // read header:
82 :
83 28 : if ( !( mbStatus = ImplReadHeader() ) )
84 1 : return false;
85 :
86 27 : if ( ( mnMaxVal == 0 ) || ( mnWidth == 0 ) || ( mnHeight == 0 ) )
87 0 : return false;
88 :
89 : // 0->PBM, 1->PGM, 2->PPM
90 27 : switch ( mnMode )
91 : {
92 : case 0 :
93 0 : maBmp = Bitmap( Size( mnWidth, mnHeight ), 1 );
94 0 : if ( ( mpAcc = maBmp.AcquireWriteAccess() ) == 0 )
95 0 : return false;
96 0 : mpAcc->SetPaletteEntryCount( 2 );
97 0 : mpAcc->SetPaletteColor( 0, BitmapColor( 0xff, 0xff, 0xff ) );
98 0 : mpAcc->SetPaletteColor( 1, BitmapColor( 0x00, 0x00, 0x00 ) );
99 0 : break;
100 :
101 : case 1 :
102 0 : if ( mnMaxVal <= 1 )
103 0 : maBmp = Bitmap( Size( mnWidth, mnHeight ), 1);
104 0 : else if ( mnMaxVal <= 15 )
105 0 : maBmp = Bitmap( Size( mnWidth, mnHeight ), 4);
106 : else
107 0 : maBmp = Bitmap( Size( mnWidth, mnHeight ), 8);
108 :
109 0 : if ( ( mpAcc = maBmp.AcquireWriteAccess() ) == 0 )
110 0 : return false;
111 0 : mnCol = (sal_uInt16)mnMaxVal + 1;
112 0 : if ( mnCol > 256 )
113 0 : mnCol = 256;
114 :
115 0 : mpAcc->SetPaletteEntryCount( 256 );
116 0 : for ( i = 0; i < mnCol; i++ )
117 : {
118 0 : sal_uLong nCount = 255 * i / mnCol;
119 0 : mpAcc->SetPaletteColor( i, BitmapColor( (sal_uInt8)nCount, (sal_uInt8)nCount, (sal_uInt8)nCount ) );
120 : }
121 0 : break;
122 : case 2 :
123 27 : maBmp = Bitmap( Size( mnWidth, mnHeight ), 24 );
124 27 : if ( ( mpAcc = maBmp.AcquireWriteAccess() ) == 0 )
125 0 : return false;
126 27 : break;
127 : }
128 :
129 : // read bitmap data
130 27 : mbStatus = ImplReadBody();
131 :
132 27 : if ( mpAcc )
133 : {
134 27 : Bitmap::ReleaseAccess( mpAcc ), mpAcc = NULL;
135 : }
136 27 : if ( mbStatus )
137 27 : rGraphic = maBmp;
138 :
139 27 : return mbStatus;
140 : }
141 :
142 28 : bool PBMReader::ImplReadHeader()
143 : {
144 : sal_uInt8 nID[ 2 ];
145 : sal_uInt8 nDat;
146 28 : sal_uInt8 nMax, nCount = 0;
147 28 : bool bFinished = false;
148 :
149 28 : mrPBM.ReadUChar( nID[ 0 ] ).ReadUChar( nID[ 1 ] );
150 28 : if ( nID[ 0 ] != 'P' )
151 0 : return false;
152 28 : mnMaxVal = mnWidth = mnHeight = 0;
153 28 : switch ( nID[ 1 ] )
154 : {
155 : case '1' :
156 0 : mbRaw = false;
157 : //fall-through
158 : case '4' :
159 0 : mnMode = 0;
160 0 : nMax = 2; // number of parameters in Header
161 0 : mnMaxVal = 1;
162 0 : break;
163 : case '2' :
164 0 : mbRaw = false;
165 : //fall-through
166 : case '5' :
167 0 : mnMode = 1;
168 0 : nMax = 3;
169 0 : break;
170 : case '3' :
171 1 : mbRaw = false;
172 : //fall-through
173 : case '6' :
174 28 : mnMode = 2;
175 28 : nMax = 3;
176 28 : break;
177 : default:
178 0 : return false;
179 : }
180 345 : while ( !bFinished )
181 : {
182 290 : if ( mrPBM.GetError() )
183 0 : return false;
184 :
185 290 : mrPBM.ReadUChar( nDat );
186 :
187 290 : if ( nDat == '#' )
188 : {
189 0 : mbRemark = true;
190 0 : continue;
191 : }
192 290 : else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
193 : {
194 56 : mbRemark = false;
195 56 : nDat = 0x20;
196 : }
197 290 : if ( mbRemark )
198 0 : continue;
199 :
200 290 : if ( ( nDat == 0x20 ) || ( nDat == 0x09 ) )
201 : {
202 108 : if ( ( nCount == 0 ) && mnWidth )
203 27 : nCount++;
204 81 : else if ( ( nCount == 1 ) && mnHeight )
205 : {
206 54 : if ( ++nCount == nMax )
207 0 : bFinished = true;
208 : }
209 54 : else if ( ( nCount == 2 ) && mnMaxVal )
210 : {
211 27 : bFinished = true;
212 : }
213 108 : continue;
214 : }
215 182 : if ( ( nDat >= '0' ) && ( nDat <= '9' ) )
216 : {
217 181 : nDat -= '0';
218 362 : if ( nCount == 0 )
219 : {
220 50 : mnWidth *= 10;
221 50 : mnWidth += nDat;
222 : }
223 131 : else if ( nCount == 1 )
224 : {
225 50 : mnHeight *= 10;
226 50 : mnHeight += nDat;
227 : }
228 81 : else if ( nCount == 2 )
229 : {
230 81 : mnMaxVal *= 10;
231 81 : mnMaxVal += nDat;
232 : }
233 : }
234 : else
235 1 : return false;
236 : }
237 27 : return mbStatus;
238 : }
239 :
240 27 : bool PBMReader::ImplReadBody()
241 : {
242 27 : bool bPara, bFinished = false;
243 27 : sal_uInt8 nDat = 0, nCount;
244 : sal_uLong nGrey, nRGB[3];
245 27 : sal_uLong nWidth = 0;
246 27 : sal_uLong nHeight = 0;
247 :
248 27 : if ( mbRaw )
249 : {
250 27 : signed char nShift = 0;
251 27 : switch ( mnMode )
252 : {
253 :
254 : // PBM
255 : case 0 :
256 0 : while ( nHeight != mnHeight )
257 : {
258 0 : if ( mrPBM.IsEof() || mrPBM.GetError() )
259 0 : return false;
260 :
261 0 : if ( --nShift < 0 )
262 : {
263 0 : mrPBM.ReadUChar( nDat );
264 0 : nShift = 7;
265 : }
266 0 : mpAcc->SetPixelIndex( nHeight, nWidth, nDat >> nShift );
267 0 : if ( ++nWidth == mnWidth )
268 : {
269 0 : nShift = 0;
270 0 : nWidth = 0;
271 0 : nHeight++;
272 : }
273 : }
274 0 : break;
275 :
276 : // PGM
277 : case 1 :
278 0 : while ( nHeight != mnHeight )
279 : {
280 0 : if ( mrPBM.IsEof() || mrPBM.GetError() )
281 0 : return false;
282 :
283 0 : mrPBM.ReadUChar( nDat );
284 0 : mpAcc->SetPixelIndex( nHeight, nWidth++, nDat);
285 :
286 0 : if ( nWidth == mnWidth )
287 : {
288 0 : nWidth = 0;
289 0 : nHeight++;
290 : }
291 : }
292 0 : break;
293 :
294 : // PPM
295 : case 2 :
296 3069370 : while ( nHeight != mnHeight )
297 : {
298 3069316 : if ( mrPBM.IsEof() || mrPBM.GetError() )
299 0 : return false;
300 :
301 : sal_uInt8 nR, nG, nB;
302 : sal_uLong nRed, nGreen, nBlue;
303 3069316 : mrPBM.ReadUChar( nR ).ReadUChar( nG ).ReadUChar( nB );
304 3069316 : nRed = 255 * nR / mnMaxVal;
305 3069316 : nGreen = 255 * nG / mnMaxVal;
306 3069316 : nBlue = 255 * nB / mnMaxVal;
307 3069316 : mpAcc->SetPixel( nHeight, nWidth++, BitmapColor( (sal_uInt8)nRed, (sal_uInt8)nGreen, (sal_uInt8)nBlue ) );
308 3069316 : if ( nWidth == mnWidth )
309 : {
310 5976 : nWidth = 0;
311 5976 : nHeight++;
312 : }
313 : }
314 27 : break;
315 : }
316 : }
317 0 : else switch ( mnMode )
318 : {
319 : // PBM
320 : case 0 :
321 0 : while ( !bFinished )
322 : {
323 0 : if ( mrPBM.IsEof() || mrPBM.GetError() )
324 0 : return false;
325 :
326 0 : mrPBM.ReadUChar( nDat );
327 :
328 0 : if ( nDat == '#' )
329 : {
330 0 : mbRemark = true;
331 0 : continue;
332 : }
333 0 : else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
334 : {
335 0 : mbRemark = false;
336 0 : continue;
337 : }
338 0 : if ( mbRemark || nDat == 0x20 || nDat == 0x09 )
339 0 : continue;
340 :
341 0 : if ( nDat == '0' || nDat == '1' )
342 : {
343 0 : mpAcc->SetPixelIndex( nHeight, nWidth, static_cast<sal_uInt8>(nDat - '0') );
344 0 : nWidth++;
345 0 : if ( nWidth == mnWidth )
346 : {
347 0 : nWidth = 0;
348 0 : if ( ++nHeight == mnHeight )
349 0 : bFinished = true;
350 : }
351 : }
352 : else
353 0 : return false;
354 : }
355 0 : break;
356 :
357 : // PGM
358 : case 1 :
359 :
360 0 : bPara = false;
361 0 : nCount = 0;
362 0 : nGrey = 0;
363 :
364 0 : while ( !bFinished )
365 : {
366 0 : if ( nCount )
367 : {
368 0 : nCount--;
369 0 : if ( nGrey <= mnMaxVal )
370 0 : nGrey = 255 * nGrey / mnMaxVal;
371 0 : mpAcc->SetPixelIndex( nHeight, nWidth++, static_cast<sal_uInt8>(nGrey) );
372 0 : nGrey = 0;
373 0 : if ( nWidth == mnWidth )
374 : {
375 0 : nWidth = 0;
376 0 : if ( ++nHeight == mnHeight )
377 0 : bFinished = true;
378 : }
379 0 : continue;
380 : }
381 :
382 0 : if ( mrPBM.IsEof() || mrPBM.GetError() )
383 0 : return false;
384 :
385 0 : mrPBM.ReadUChar( nDat );
386 :
387 0 : if ( nDat == '#' )
388 : {
389 0 : mbRemark = true;
390 0 : if ( bPara )
391 : {
392 0 : bPara = false;
393 0 : nCount++;
394 : }
395 0 : continue;
396 : }
397 0 : else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
398 : {
399 0 : mbRemark = false;
400 0 : if ( bPara )
401 : {
402 0 : bPara = false;
403 0 : nCount++;
404 : }
405 0 : continue;
406 : }
407 :
408 0 : if ( nDat == 0x20 || nDat == 0x09 )
409 : {
410 0 : if ( bPara )
411 : {
412 0 : bPara = false;
413 0 : nCount++;
414 : }
415 0 : continue;
416 : }
417 0 : if ( nDat >= '0' && nDat <= '9' )
418 : {
419 0 : bPara = true;
420 0 : nGrey *= 10;
421 0 : nGrey += nDat-'0';
422 0 : continue;
423 : }
424 : else
425 0 : return false;
426 : }
427 0 : break;
428 :
429 :
430 :
431 : // PPM
432 : case 2 :
433 :
434 0 : bPara = false;
435 0 : nCount = 0;
436 0 : nRGB[ 0 ] = nRGB[ 1 ] = nRGB[ 2 ] = 0;
437 :
438 0 : while ( !bFinished )
439 : {
440 0 : if ( nCount == 3 )
441 : {
442 0 : nCount = 0;
443 0 : mpAcc->SetPixel( nHeight, nWidth++, BitmapColor( static_cast< sal_uInt8 >( ( nRGB[ 0 ] * 255 ) / mnMaxVal ),
444 0 : static_cast< sal_uInt8 >( ( nRGB[ 1 ] * 255 ) / mnMaxVal ),
445 0 : static_cast< sal_uInt8 >( ( nRGB[ 2 ] * 255 ) / mnMaxVal ) ) );
446 0 : nRGB[ 0 ] = nRGB[ 1 ] = nRGB[ 2 ] = 0;
447 0 : if ( nWidth == mnWidth )
448 : {
449 0 : nWidth = 0;
450 0 : if ( ++nHeight == mnHeight )
451 0 : bFinished = true;
452 : }
453 0 : continue;
454 : }
455 :
456 0 : if ( mrPBM.IsEof() || mrPBM.GetError() )
457 0 : return false;
458 :
459 0 : mrPBM.ReadUChar( nDat );
460 :
461 0 : if ( nDat == '#' )
462 : {
463 0 : mbRemark = true;
464 0 : if ( bPara )
465 : {
466 0 : bPara = false;
467 0 : nCount++;
468 : }
469 0 : continue;
470 : }
471 0 : else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
472 : {
473 0 : mbRemark = false;
474 0 : if ( bPara )
475 : {
476 0 : bPara = false;
477 0 : nCount++;
478 : }
479 0 : continue;
480 : }
481 :
482 0 : if ( nDat == 0x20 || nDat == 0x09 )
483 : {
484 0 : if ( bPara )
485 : {
486 0 : bPara = false;
487 0 : nCount++;
488 : }
489 0 : continue;
490 : }
491 0 : if ( nDat >= '0' && nDat <= '9' )
492 : {
493 0 : bPara = true;
494 0 : nRGB[ nCount ] *= 10;
495 0 : nRGB[ nCount ] += nDat-'0';
496 0 : continue;
497 : }
498 : else
499 0 : return false;
500 : }
501 0 : break;
502 : }
503 27 : return mbStatus;
504 : }
505 :
506 : //================== GraphicImport - the exported function ================
507 :
508 : // this needs to be kept in sync with
509 : // ImpFilterLibCacheEntry::GetImportFunction() from
510 : // vcl/source/filter/graphicfilter.cxx
511 : #if defined(DISABLE_DYNLOADING)
512 : #define GraphicImport ipbGraphicImport
513 : #endif
514 :
515 : extern "C" SAL_DLLPUBLIC_EXPORT bool SAL_CALL
516 28 : GraphicImport( SvStream & rStream, Graphic & rGraphic, FilterConfigItem* )
517 : {
518 28 : PBMReader aPBMReader(rStream);
519 :
520 28 : return aPBMReader.ReadPBM(rGraphic );
521 : }
522 :
523 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|