Update ooo320-m1
[ooovba.git] / goodies / source / filter.vcl / ipbm / ipbm.cxx
blob8c55d25b854f75e6a0cc762570b87186f48913df
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ipbm.cxx,v $
10 * $Revision: 1.9 $
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 //============================ PBMReader ==================================
40 class PBMReader {
42 private:
44 SvStream* mpPBM; // Die einzulesende PBM-Datei
46 BOOL mbStatus;
47 BOOL mbRemark; // FALSE wenn sich stream in einem Kommentar befindet
48 BOOL mbRaw; // RAW/ASCII MODE
49 ULONG mnMode; // 0->PBM, 1->PGM, 2->PPM
50 Bitmap maBmp;
51 BitmapWriteAccess* mpAcc;
52 ULONG mnWidth, mnHeight; // Bildausmass in Pixeln
53 ULONG mnCol;
54 ULONG mnMaxVal; // maximaler wert in den
55 BOOL ImplCallback( USHORT nPercent );
56 BOOL ImplReadBody();
57 BOOL ImplReadHeader();
59 public:
60 PBMReader();
61 ~PBMReader();
62 BOOL ReadPBM( SvStream & rPBM, Graphic & rGraphic );
65 //=================== Methoden von PBMReader ==============================
67 PBMReader::PBMReader() :
68 mbStatus ( TRUE ),
69 mbRemark ( FALSE ),
70 mbRaw ( TRUE ),
71 mpAcc ( NULL )
75 PBMReader::~PBMReader()
79 BOOL PBMReader::ImplCallback( USHORT /*nPercent*/ )
82 if ( pCallback != NULL )
84 if ( ( (*pCallback)( pCallerData, nPercent ) ) == TRUE )
86 mpPBM->SetError( SVSTREAM_FILEFORMAT_ERROR );
87 return TRUE;
91 return FALSE;
94 BOOL PBMReader::ReadPBM( SvStream & rPBM, Graphic & rGraphic )
96 USHORT i;
98 if ( rPBM.GetError() )
99 return FALSE;
101 mpPBM = &rPBM;
102 mpPBM->SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN );
104 // Kopf einlesen:
106 if ( ( mbStatus = ImplReadHeader() ) == FALSE )
107 return FALSE;
109 if ( mnWidth == 0 || mnHeight == 0 )
110 return FALSE;
112 // 0->PBM, 1->PGM, 2->PPM
113 switch ( mnMode )
115 case 0 :
116 maBmp = Bitmap( Size( mnWidth, mnHeight ), 1 );
117 if ( ( mpAcc = maBmp.AcquireWriteAccess() ) == FALSE )
118 return FALSE;
119 mpAcc->SetPaletteEntryCount( 2 );
120 mpAcc->SetPaletteColor( 0, BitmapColor( 0xff, 0xff, 0xff ) );
121 mpAcc->SetPaletteColor( 1, BitmapColor( 0x00, 0x00, 0x00 ) );
122 break;
124 case 1 :
125 if ( mnMaxVal <= 1 )
126 maBmp = Bitmap( Size( mnWidth, mnHeight ), 1);
127 else if ( mnMaxVal <= 15 )
128 maBmp = Bitmap( Size( mnWidth, mnHeight ), 4);
129 else
130 maBmp = Bitmap( Size( mnWidth, mnHeight ), 8);
132 if ( ( mpAcc = maBmp.AcquireWriteAccess() ) == FALSE )
133 return FALSE;
134 mnCol = (USHORT)mnMaxVal + 1;
135 if ( mnCol > 256 )
136 mnCol = 256;
138 mpAcc->SetPaletteEntryCount( 256 );
139 for ( i = 0; i < mnCol; i++ )
141 ULONG nCount = 255 * i / mnCol;
142 mpAcc->SetPaletteColor( i, BitmapColor( (BYTE)nCount, (BYTE)nCount, (BYTE)nCount ) );
144 break;
145 case 2 :
146 maBmp = Bitmap( Size( mnWidth, mnHeight ), 24 );
147 if ( ( mpAcc = maBmp.AcquireWriteAccess() ) == FALSE )
148 return FALSE;
149 break;
152 // Bitmap-Daten einlesen
153 mbStatus = ImplReadBody();
155 if ( mpAcc )
157 maBmp.ReleaseAccess( mpAcc ), mpAcc = NULL;
159 if ( mbStatus )
160 rGraphic = maBmp;
162 return mbStatus;
165 BOOL PBMReader::ImplReadHeader()
167 BYTE nID[ 2 ];
168 BYTE nDat;
169 BYTE nMax, nCount = 0;
170 BOOL bFinished = FALSE;
172 *mpPBM >> nID[ 0 ] >> nID[ 1 ];
173 if ( nID[ 0 ] != 'P' )
174 return FALSE;
175 switch ( nID[ 1 ] )
177 case '1' :
178 mbRaw = FALSE;
179 case '4' :
180 mnMode = 0;
181 nMax = 2; // number of parameters in Header
182 break;
183 case '2' :
184 mbRaw = FALSE;
185 case '5' :
186 mnMode = 1;
187 nMax = 3;
188 break;
189 case '3' :
190 mbRaw = FALSE;
191 case '6' :
192 mnMode = 2;
193 nMax = 3;
194 break;
195 default:
196 return FALSE;
199 mnMaxVal = mnWidth = mnHeight = 0;
201 while ( bFinished == FALSE )
203 if ( mpPBM->GetError() )
204 return FALSE;
206 *mpPBM >> nDat;
208 if ( nDat == '#' )
210 mbRemark = TRUE;
211 continue;
213 else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
215 mbRemark = FALSE;
216 nDat = 0x20;
218 if ( mbRemark )
219 continue;
221 if ( ( nDat == 0x20 ) || ( nDat == 0x09 ) )
223 if ( ( nCount == 0 ) && mnWidth )
224 nCount++;
225 else if ( ( nCount == 1 ) && mnHeight )
227 if ( ++nCount == nMax )
228 bFinished = TRUE;
230 else if ( ( nCount == 2 ) && mnMaxVal )
232 bFinished = TRUE;
234 continue;
236 if ( ( nDat >= '0' ) && ( nDat <= '9' ) )
238 nDat -= '0';
239 if ( nCount == 0 )
241 mnWidth *= 10;
242 mnWidth += nDat;
244 else if ( nCount == 1 )
246 mnHeight *= 10;
247 mnHeight += nDat;
249 else if ( nCount == 2 )
251 mnMaxVal *= 10;
252 mnMaxVal += nDat;
255 else
256 return FALSE;
258 return mbStatus;
261 BOOL PBMReader::ImplReadBody()
263 BOOL bPara, bFinished = FALSE;
264 BYTE nDat = 0, nCount;
265 ULONG nGrey, nRGB[3];
266 ULONG nWidth = 0;
267 ULONG nHeight = 0;
268 signed char nShift = 0;
270 if ( mbRaw )
272 switch ( mnMode )
275 // PBM
276 case 0 :
277 while ( nHeight != mnHeight )
279 if ( mpPBM->IsEof() || mpPBM->GetError() )
280 return FALSE;
282 if ( --nShift < 0 )
284 *mpPBM >> nDat;
285 nShift = 7;
287 mpAcc->SetPixel( nHeight, nWidth, nDat >> nShift );
288 if ( ++nWidth == mnWidth )
290 nShift = 0;
291 nWidth = 0;
292 nHeight++;
293 ImplCallback( (USHORT)( ( 100 * nHeight ) / mnHeight ) ); // processing output in percent
296 break;
298 // PGM
299 case 1 :
300 while ( nHeight != mnHeight )
302 if ( mpPBM->IsEof() || mpPBM->GetError() )
303 return FALSE;
305 *mpPBM >> nDat;
306 mpAcc->SetPixel( nHeight, nWidth++, nDat);
308 if ( nWidth == mnWidth )
310 nWidth = 0;
311 nHeight++;
312 ImplCallback( (USHORT)( ( 100 * nHeight ) / mnHeight ) ); // processing output in percent
315 break;
317 // PPM
318 case 2 :
319 while ( nHeight != mnHeight )
321 if ( mpPBM->IsEof() || mpPBM->GetError() )
322 return FALSE;
324 BYTE nR, nG, nB;
325 ULONG nRed, nGreen, nBlue;
326 *mpPBM >> nR >> nG >> nB;
327 nRed = 255 * nR / mnMaxVal;
328 nGreen = 255 * nG / mnMaxVal;
329 nBlue = 255 * nB / mnMaxVal;
330 mpAcc->SetPixel( nHeight, nWidth++, BitmapColor( (BYTE)nRed, (BYTE)nGreen, (BYTE)nBlue ) );
331 if ( nWidth == mnWidth )
333 nWidth = 0;
334 nHeight++;
335 ImplCallback( (USHORT) ( ( 100 * nHeight ) / mnHeight ) ); // processing output in percent
338 break;
341 else switch ( mnMode )
343 // PBM
344 case 0 :
345 while ( bFinished == FALSE )
347 if ( mpPBM->IsEof() || mpPBM->GetError() )
348 return FALSE;
350 *mpPBM >> nDat;
352 if ( nDat == '#' )
354 mbRemark = TRUE;
355 continue;
357 else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
359 mbRemark = FALSE;
360 continue;
362 if ( mbRemark || nDat == 0x20 || nDat == 0x09 )
363 continue;
365 if ( nDat == '0' || nDat == '1' )
367 mpAcc->SetPixel( nHeight, nWidth, (BYTE)nDat-'0' );
368 nWidth++;
369 if ( nWidth == mnWidth )
371 nWidth = 0;
372 if ( ++nHeight == mnHeight )
373 bFinished = TRUE;
374 ImplCallback( (USHORT) ( ( 100 * nHeight ) / mnHeight ) ); // processing output in percent
377 else
378 return FALSE;
380 break;
382 // PGM
383 case 1 :
385 bPara = FALSE;
386 nCount = 0;
387 nGrey = 0;
389 while ( bFinished == FALSE )
391 if ( nCount )
393 nCount--;
394 if ( nGrey <= mnMaxVal )
395 nGrey = 255 * nGrey / mnMaxVal;
396 mpAcc->SetPixel( nHeight, nWidth++, (BYTE)nGrey );
397 nGrey = 0;
398 if ( nWidth == mnWidth )
400 nWidth = 0;
401 if ( ++nHeight == mnHeight )
402 bFinished = TRUE;
403 ImplCallback( (USHORT) ( ( 100 * nHeight ) / mnHeight ) ); // processing output in percent
405 continue;
408 if ( mpPBM->IsEof() || mpPBM->GetError() )
409 return FALSE;
411 *mpPBM >> nDat;
413 if ( nDat == '#' )
415 mbRemark = TRUE;
416 if ( bPara )
418 bPara = FALSE;
419 nCount++;
421 continue;
423 else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
425 mbRemark = FALSE;
426 if ( bPara )
428 bPara = FALSE;
429 nCount++;
431 continue;
434 if ( nDat == 0x20 || nDat == 0x09 )
436 if ( bPara )
438 bPara = FALSE;
439 nCount++;
441 continue;
443 if ( nDat >= '0' && nDat <= '9' )
445 bPara = TRUE;
446 nGrey *= 10;
447 nGrey += nDat-'0';
448 continue;
450 else
451 return FALSE;
453 break;
457 // PPM
458 case 2 :
460 bPara = FALSE;
461 nCount = 0;
462 nRGB[ 0 ] = nRGB[ 1 ] = nRGB[ 2 ] = 0;
464 while ( bFinished == FALSE )
466 if ( nCount == 3 )
468 nCount = 0;
469 mpAcc->SetPixel( nHeight, nWidth++, BitmapColor( (BYTE)nRGB[ 0 ], (BYTE)nRGB[ 1 ], (BYTE)nRGB[ 2 ] ) );
470 nCount = 0;
471 nRGB[ 0 ] = nRGB[ 1 ] = nRGB[ 2 ] = 0;
472 if ( nWidth == mnWidth )
474 nWidth = 0;
475 if ( ++nHeight == mnHeight )
476 bFinished = TRUE;
477 ImplCallback( (USHORT) ( ( 100 * nHeight ) / mnHeight ) ); // processing output in percent
479 continue;
482 if ( mpPBM->IsEof() || mpPBM->GetError() )
483 return FALSE;
485 *mpPBM >> nDat;
487 if ( nDat == '#' )
489 mbRemark = TRUE;
490 if ( bPara )
492 bPara = FALSE;
493 nCount++;
495 continue;
497 else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
499 mbRemark = FALSE;
500 if ( bPara )
502 bPara = FALSE;
503 nCount++;
505 continue;
508 if ( nDat == 0x20 || nDat == 0x09 )
510 if ( bPara )
512 bPara = FALSE;
513 nCount++;
515 continue;
517 if ( nDat >= '0' && nDat <= '9' )
519 bPara = TRUE;
520 nRGB[ nCount ] *= 10;
521 nRGB[ nCount ] += nDat-'0';
522 continue;
524 else
525 return FALSE;
527 break;
529 return mbStatus;
532 //================== GraphicImport - die exportierte Funktion ================
534 extern "C" BOOL __LOADONCALLAPI GraphicImport(SvStream & rStream, Graphic & rGraphic, FilterConfigItem*, BOOL )
536 PBMReader aPBMReader;
538 return aPBMReader.ReadPBM( rStream, rGraphic );
541 //================== ein bischen Muell fuer Windows ==========================
542 #ifndef GCC
543 #endif
545 #ifdef WIN
547 static HINSTANCE hDLLInst = 0; // HANDLE der DLL
549 extern "C" int CALLBACK LibMain( HINSTANCE hDLL, WORD, WORD nHeap, LPSTR )
551 #ifndef WNT
552 if ( nHeap )
553 UnlockData( 0 );
554 #endif
556 hDLLInst = hDLL;
558 return TRUE;
561 extern "C" int CALLBACK WEP( int )
563 return 1;
566 #endif