1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ipbm.cxx,v $
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 ==================================
44 SvStream
* mpPBM
; // Die einzulesende PBM-Datei
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
51 BitmapWriteAccess
* mpAcc
;
52 ULONG mnWidth
, mnHeight
; // Bildausmass in Pixeln
54 ULONG mnMaxVal
; // maximaler wert in den
55 BOOL
ImplCallback( USHORT nPercent
);
57 BOOL
ImplReadHeader();
62 BOOL
ReadPBM( SvStream
& rPBM
, Graphic
& rGraphic
);
65 //=================== Methoden von PBMReader ==============================
67 PBMReader::PBMReader() :
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 );
94 BOOL
PBMReader::ReadPBM( SvStream
& rPBM
, Graphic
& rGraphic
)
98 if ( rPBM
.GetError() )
102 mpPBM
->SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN
);
106 if ( ( mbStatus
= ImplReadHeader() ) == FALSE
)
109 if ( mnWidth
== 0 || mnHeight
== 0 )
112 // 0->PBM, 1->PGM, 2->PPM
116 maBmp
= Bitmap( Size( mnWidth
, mnHeight
), 1 );
117 if ( ( mpAcc
= maBmp
.AcquireWriteAccess() ) == FALSE
)
119 mpAcc
->SetPaletteEntryCount( 2 );
120 mpAcc
->SetPaletteColor( 0, BitmapColor( 0xff, 0xff, 0xff ) );
121 mpAcc
->SetPaletteColor( 1, BitmapColor( 0x00, 0x00, 0x00 ) );
126 maBmp
= Bitmap( Size( mnWidth
, mnHeight
), 1);
127 else if ( mnMaxVal
<= 15 )
128 maBmp
= Bitmap( Size( mnWidth
, mnHeight
), 4);
130 maBmp
= Bitmap( Size( mnWidth
, mnHeight
), 8);
132 if ( ( mpAcc
= maBmp
.AcquireWriteAccess() ) == FALSE
)
134 mnCol
= (USHORT
)mnMaxVal
+ 1;
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
) );
146 maBmp
= Bitmap( Size( mnWidth
, mnHeight
), 24 );
147 if ( ( mpAcc
= maBmp
.AcquireWriteAccess() ) == FALSE
)
152 // Bitmap-Daten einlesen
153 mbStatus
= ImplReadBody();
157 maBmp
.ReleaseAccess( mpAcc
), mpAcc
= NULL
;
165 BOOL
PBMReader::ImplReadHeader()
169 BYTE nMax
, nCount
= 0;
170 BOOL bFinished
= FALSE
;
172 *mpPBM
>> nID
[ 0 ] >> nID
[ 1 ];
173 if ( nID
[ 0 ] != 'P' )
181 nMax
= 2; // number of parameters in Header
199 mnMaxVal
= mnWidth
= mnHeight
= 0;
201 while ( bFinished
== FALSE
)
203 if ( mpPBM
->GetError() )
213 else if ( ( nDat
== 0x0d ) || ( nDat
== 0x0a ) )
221 if ( ( nDat
== 0x20 ) || ( nDat
== 0x09 ) )
223 if ( ( nCount
== 0 ) && mnWidth
)
225 else if ( ( nCount
== 1 ) && mnHeight
)
227 if ( ++nCount
== nMax
)
230 else if ( ( nCount
== 2 ) && mnMaxVal
)
236 if ( ( nDat
>= '0' ) && ( nDat
<= '9' ) )
244 else if ( nCount
== 1 )
249 else if ( nCount
== 2 )
261 BOOL
PBMReader::ImplReadBody()
263 BOOL bPara
, bFinished
= FALSE
;
264 BYTE nDat
= 0, nCount
;
265 ULONG nGrey
, nRGB
[3];
268 signed char nShift
= 0;
277 while ( nHeight
!= mnHeight
)
279 if ( mpPBM
->IsEof() || mpPBM
->GetError() )
287 mpAcc
->SetPixel( nHeight
, nWidth
, nDat
>> nShift
);
288 if ( ++nWidth
== mnWidth
)
293 ImplCallback( (USHORT
)( ( 100 * nHeight
) / mnHeight
) ); // processing output in percent
300 while ( nHeight
!= mnHeight
)
302 if ( mpPBM
->IsEof() || mpPBM
->GetError() )
306 mpAcc
->SetPixel( nHeight
, nWidth
++, nDat
);
308 if ( nWidth
== mnWidth
)
312 ImplCallback( (USHORT
)( ( 100 * nHeight
) / mnHeight
) ); // processing output in percent
319 while ( nHeight
!= mnHeight
)
321 if ( mpPBM
->IsEof() || mpPBM
->GetError() )
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
)
335 ImplCallback( (USHORT
) ( ( 100 * nHeight
) / mnHeight
) ); // processing output in percent
341 else switch ( mnMode
)
345 while ( bFinished
== FALSE
)
347 if ( mpPBM
->IsEof() || mpPBM
->GetError() )
357 else if ( ( nDat
== 0x0d ) || ( nDat
== 0x0a ) )
362 if ( mbRemark
|| nDat
== 0x20 || nDat
== 0x09 )
365 if ( nDat
== '0' || nDat
== '1' )
367 mpAcc
->SetPixel( nHeight
, nWidth
, (BYTE
)nDat
-'0' );
369 if ( nWidth
== mnWidth
)
372 if ( ++nHeight
== mnHeight
)
374 ImplCallback( (USHORT
) ( ( 100 * nHeight
) / mnHeight
) ); // processing output in percent
389 while ( bFinished
== FALSE
)
394 if ( nGrey
<= mnMaxVal
)
395 nGrey
= 255 * nGrey
/ mnMaxVal
;
396 mpAcc
->SetPixel( nHeight
, nWidth
++, (BYTE
)nGrey
);
398 if ( nWidth
== mnWidth
)
401 if ( ++nHeight
== mnHeight
)
403 ImplCallback( (USHORT
) ( ( 100 * nHeight
) / mnHeight
) ); // processing output in percent
408 if ( mpPBM
->IsEof() || mpPBM
->GetError() )
423 else if ( ( nDat
== 0x0d ) || ( nDat
== 0x0a ) )
434 if ( nDat
== 0x20 || nDat
== 0x09 )
443 if ( nDat
>= '0' && nDat
<= '9' )
462 nRGB
[ 0 ] = nRGB
[ 1 ] = nRGB
[ 2 ] = 0;
464 while ( bFinished
== FALSE
)
469 mpAcc
->SetPixel( nHeight
, nWidth
++, BitmapColor( (BYTE
)nRGB
[ 0 ], (BYTE
)nRGB
[ 1 ], (BYTE
)nRGB
[ 2 ] ) );
471 nRGB
[ 0 ] = nRGB
[ 1 ] = nRGB
[ 2 ] = 0;
472 if ( nWidth
== mnWidth
)
475 if ( ++nHeight
== mnHeight
)
477 ImplCallback( (USHORT
) ( ( 100 * nHeight
) / mnHeight
) ); // processing output in percent
482 if ( mpPBM
->IsEof() || mpPBM
->GetError() )
497 else if ( ( nDat
== 0x0d ) || ( nDat
== 0x0a ) )
508 if ( nDat
== 0x20 || nDat
== 0x09 )
517 if ( nDat
>= '0' && nDat
<= '9' )
520 nRGB
[ nCount
] *= 10;
521 nRGB
[ nCount
] += nDat
-'0';
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 ==========================
547 static HINSTANCE hDLLInst
= 0; // HANDLE der DLL
549 extern "C" int CALLBACK
LibMain( HINSTANCE hDLL
, WORD
, WORD nHeap
, LPSTR
)
561 extern "C" int CALLBACK
WEP( int )