42 std::unique_ptr<vcl::bitmap::RawBitmap> mpRawBmp;
43 std::vector<Color> mvPalette;
48 bool ImplReadHeader();
52 bool ReadPBM(
Graphic & rGraphic );
72bool PBMReader::ReadPBM(
Graphic & rGraphic )
74 if ( mrPBM.GetError() )
77 mrPBM.SetEndian( SvStreamEndian::LITTLE );
81 mbStatus = ImplReadHeader();
85 if ( ( mnMaxVal == 0 ) || ( mnWidth <= 0 ) || ( mnHeight <= 0 ) )
88 sal_uInt32 nPixelsRequired;
89 if (o3tl::checked_multiply<sal_uInt32>(mnWidth, mnHeight, nPixelsRequired))
91 const auto nRemainingSize = mrPBM.remainingSize();
98 if (nRemainingSize < nPixelsRequired / 8)
102 mvPalette.resize( 2 );
103 mvPalette[0] =
Color( 0xff, 0xff, 0xff );
104 mvPalette[1] =
Color( 0x00, 0x00, 0x00 );
108 if (nRemainingSize < nPixelsRequired)
112 mnCol =
static_cast<sal_uInt16
>(mnMaxVal) + 1;
116 mvPalette.resize( 256 );
117 for ( sal_uInt16 i = 0;
i <
mnCol;
i++ )
124 if (nRemainingSize / 3 < nPixelsRequired)
132 mbStatus = ImplReadBody();
140bool PBMReader::ImplReadHeader()
145 bool bFinished =
false;
147 mrPBM.ReadUChar( nID[ 0 ] ).ReadUChar( nID[ 1 ] );
148 if (!mrPBM.good() || nID[0] !=
'P')
180 mrPBM.ReadUChar( nDat );
190 else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
198 if ( ( nDat == 0x20 ) || ( nDat == 0x09 ) )
200 if ( ( nCount == 0 ) && mnWidth )
202 else if ( ( nCount == 1 ) && mnHeight )
204 if ( ++nCount == nMax )
207 else if ( ( nCount == 2 ) && mnMaxVal )
213 if ( ( nDat >=
'0' ) && ( nDat <=
'9' ) )
218 if (mnWidth > SAL_MAX_INT32 / 10)
223 if (nDat > SAL_MAX_INT32 - mnWidth)
229 else if ( nCount == 1 )
231 if (mnHeight > SAL_MAX_INT32 / 10)
236 if (nDat > SAL_MAX_INT32 - mnHeight)
242 else if ( nCount == 2 )
244 if (mnMaxVal > std::numeric_limits<sal_uInt64>::max() / 10)
249 if (nDat > std::numeric_limits<sal_uInt64>::max() - mnMaxVal)
262bool PBMReader::ImplReadBody()
265 sal_uInt64 nGrey, nRGB[3];
266 sal_Int32 nWidth = 0;
267 sal_Int32 nHeight = 0;
271 signed char nShift = 0;
277 while ( nHeight != mnHeight )
284 mrPBM.ReadUChar( nDat );
287 mpRawBmp->SetPixel( nHeight, nWidth, mvPalette[(nDat >> nShift) & 0x01] );
288 if ( ++nWidth == mnWidth )
299 while ( nHeight != mnHeight )
304 mrPBM.ReadUChar( nDat );
305 mpRawBmp->SetPixel( nHeight, nWidth++, mvPalette[nDat]);
307 if ( nWidth == mnWidth )
317 while ( nHeight != mnHeight )
324 mrPBM.ReadUChar( nR ).ReadUChar( nG ).ReadUChar( nB );
325 nRed = 255 * nR / mnMaxVal;
326 nGreen = 255 * nG / mnMaxVal;
327 nBlue = 255 * nB / mnMaxVal;
328 mpRawBmp->SetPixel( nHeight, nWidth++,
Color( nRed, nGreen, nBlue ) );
329 if ( nWidth == mnWidth )
341 bool bFinished =
false;
352 mrPBM.ReadUChar( nDat );
359 else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
364 if ( mbRemark || nDat == 0x20 || nDat == 0x09 )
367 if ( nDat ==
'0' || nDat ==
'1' )
369 mpRawBmp->SetPixel( nHeight, nWidth, mvPalette[
static_cast<sal_uInt8>(nDat -
'0')] );
371 if ( nWidth == mnWidth )
374 if ( ++nHeight == mnHeight )
395 if ( nGrey <= mnMaxVal )
396 nGrey = 255 * nGrey / mnMaxVal;
397 mpRawBmp->SetPixel( nHeight, nWidth++, mvPalette[
static_cast<sal_uInt8>(nGrey)] );
399 if ( nWidth == mnWidth )
402 if ( ++nHeight == mnHeight )
411 mrPBM.ReadUChar( nDat );
423 else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
434 if ( nDat == 0x20 || nDat == 0x09 )
443 if ( nDat >=
'0' && nDat <=
'9' )
461 nRGB[ 0 ] = nRGB[ 1 ] = nRGB[ 2 ] = 0;
468 mpRawBmp->SetPixel( nHeight, nWidth++,
Color(
static_cast< sal_uInt8 >( ( nRGB[ 0 ] * 255 ) / mnMaxVal ),
469 static_cast< sal_uInt8 >( ( nRGB[ 1 ] * 255 ) / mnMaxVal ),
470 static_cast< sal_uInt8 >( ( nRGB[ 2 ] * 255 ) / mnMaxVal ) ) );
471 nRGB[ 0 ] = nRGB[ 1 ] = nRGB[ 2 ] = 0;
472 if ( nWidth == mnWidth )
475 if ( ++nHeight == mnHeight )
484 mrPBM.ReadUChar( nDat );
496 else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
507 if ( nDat == 0x20 || nDat == 0x09 )
516 if ( nDat >=
'0' && nDat <=
'9' )
520 nRGB[
nCount ] += nDat-
'0';
536 PBMReader aPBMReader(rStream);
538 return aPBMReader.ReadPBM(rGraphic );
Intended to be used to feed into CreateFromData to create a BitmapEx.
bool ImportPbmGraphic(SvStream &rStream, Graphic &rGraphic)
BitmapEx CreateFromData(sal_uInt8 const *pData, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int32 nStride, sal_Int8 nBitCount, bool bReversColors, bool bReverseAlpha)
Copy block of image data into the bitmap.