LibreOffice Module vcl (master)  1
pngread.cxx
Go to the documentation of this file.
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 #include <sal/log.hxx>
22 #include <o3tl/safeint.hxx>
23 #include <osl/diagnose.h>
24 
25 #include <cassert>
26 #include <memory>
27 #include <unotools/configmgr.hxx>
28 #include <vcl/pngread.hxx>
29 
30 #include <cmath>
31 #include <rtl/crc.h>
32 #include <tools/zcodec.hxx>
33 #include <tools/stream.hxx>
34 #include <vcl/alpha.hxx>
35 #include <osl/endian.h>
36 #include <bitmapwriteaccess.hxx>
37 
38 namespace vcl
39 {
40 
41 #define PNGCHUNK_IHDR 0x49484452
42 #define PNGCHUNK_PLTE 0x504c5445
43 #define PNGCHUNK_IDAT 0x49444154
44 #define PNGCHUNK_IEND 0x49454e44
45 #define PNGCHUNK_bKGD 0x624b4744
46 #define PNGCHUNK_gAMA 0x67414d41
47 #define PNGCHUNK_pHYs 0x70485973
48 #define PNGCHUNK_tRNS 0x74524e53
49 
50 #define VIEWING_GAMMA 2.35
51 #define DISPLAY_GAMMA 1.0
52 
53 
54 static const sal_uInt8 mpDefaultColorTable[ 256 ] =
55 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
56  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
57  0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
58  0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
59  0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
60  0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
61  0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
62  0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
63  0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
64  0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
65  0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
66  0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
67  0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
68  0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
69  0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
70  0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
71 };
72 
74 {
75 private:
78 
79  std::vector<vcl::PNGReader::ChunkData> maChunkSeq;
80  std::vector<vcl::PNGReader::ChunkData>::iterator maChunkIter;
81  std::vector<sal_uInt8>::iterator maDataIter;
82 
83  std::unique_ptr<Bitmap> mpBmp;
85  std::unique_ptr<Bitmap> mpMaskBmp;
87  std::unique_ptr<AlphaMask> mpAlphaMask;
90 
92  std::unique_ptr<sal_uInt8[]>
93  mpInflateInBuf; // as big as the size of a scanline + alphachannel + 1
94  std::unique_ptr<sal_uInt8[]>
95  mpScanPrior; // pointer to the latest scanline
96  std::unique_ptr<sal_uInt8[]>
97  mpTransTab; // for transparency in images with palette colortype
98  sal_uInt8* mpScanCurrent; // pointer into the current scanline
100  std::size_t mnStreamSize; // estimate of PNG file size
101  sal_uInt32 mnChunkType; // Type of current PNG chunk
102  sal_Int32 mnChunkLen; // Length of current PNG chunk
103  Size maOrigSize; // pixel size of the full image
104  Size maTargetSize; // pixel size of the result image
105  Size maPhysSize; // preferred size in MapUnit::Map100thMM units
106  sal_uInt32 mnBPP; // number of bytes per pixel
107  sal_uInt32 mnScansize; // max size of scanline
108  sal_uInt32 mnYpos; // latest y position in full image
109  int mnPass; // if interlaced the latest pass ( 1..7 ) else 7
110  sal_uInt32 mnXStart; // the starting X for the current pass
111  sal_uInt32 mnXAdd; // the increment for input images X coords for the current pass
112  sal_uInt32 mnYAdd; // the increment for input images Y coords for the current pass
113  int mnPreviewShift; // shift to convert orig image coords into preview image coords
114  int mnPreviewMask; // == ((1 << mnPreviewShift) - 1)
115  sal_uInt16 mnTargetDepth; // pixel depth of target bitmap
119  sal_uInt8 mnPngDepth; // pixel depth of PNG data
124  const BitmapColor mcTranspColor; // transparency mask's transparency "color"
125  const BitmapColor mcOpaqueColor; // transparency mask's opaque "color"
126  bool mbTransparent : 1; // graphic includes a tRNS Chunk or an alpha Channel
127  bool mbAlphaChannel : 1; // is true for ColorType 4 and 6
128  bool mbRGBTriple : 1;
129  bool mbPalette : 1; // false if we need a Palette
130  bool mbGrayScale : 1;
131  bool mbzCodecInUse : 1;
132  bool mbStatus : 1;
133  bool mbIDATStarted : 1; // true if IDAT seen
134  bool mbIDATComplete : 1; // true if finished with enough IDAT chunks
135  bool mbpHYs : 1; // true if physical size of pixel available
137  bool mbIgnoreCRC : 1; // skip checking CRCs while fuzzing
138 
139 #if OSL_DEBUG_LEVEL > 0
140  // do some checks in debug mode
143 #endif
144  // the temporary Scanline (and alpha) for direct scanline copy to Bitmap
145  std::unique_ptr<sal_uInt8[]>
147  std::unique_ptr<sal_uInt8[]>
149 
150  bool ReadNextChunk();
151 
152  void ImplSetPixel( sal_uInt32 y, sal_uInt32 x, const BitmapColor & );
153  void ImplSetPixel( sal_uInt32 y, sal_uInt32 x, sal_uInt8 nPalIndex );
154  void ImplSetTranspPixel( sal_uInt32 y, sal_uInt32 x, const BitmapColor &, bool bTrans );
155  void ImplSetAlphaPixel( sal_uInt32 y, sal_uInt32 x, sal_uInt8 nPalIndex, sal_uInt8 nAlpha );
156  void ImplSetAlphaPixel( sal_uInt32 y, sal_uInt32 x, const BitmapColor&, sal_uInt8 nAlpha );
157  void ImplReadIDAT();
158  bool ImplPreparePass();
159  void ImplApplyFilter();
160  void ImplDrawScanline( sal_uInt32 nXStart, sal_uInt32 nXAdd );
161  bool ImplReadTransparent();
162  void ImplGetGamma();
163  void ImplGetBackground();
165  bool ImplReadHeader();
166  bool ImplReadPalette();
167  void ImplGetGrayPalette( sal_uInt16 );
168  sal_uInt32 ImplReadsal_uInt32();
169 
170 public:
171 
172  explicit PNGReaderImpl( SvStream& );
173  ~PNGReaderImpl();
174 
176  const std::vector<vcl::PNGReader::ChunkData>& GetAllChunks();
177  void SetIgnoreGammaChunk( bool bIgnore ){ mbIgnoreGammaChunk = bIgnore; };
178 };
179 
181 : mrPNGStream( rPNGStream ),
182  mpMaskAcc ( nullptr ),
183  mpScanCurrent ( nullptr ),
184  mpColorTable ( const_cast<sal_uInt8*>(mpDefaultColorTable) ),
185  mnChunkType ( 0 ),
186  mnChunkLen ( 0 ),
187  mnBPP ( 0 ),
188  mnScansize ( 0 ),
189  mnYpos ( 0 ),
190  mnPass ( 0 ),
191  mnXStart ( 0 ),
192  mnXAdd ( 0 ),
193  mnYAdd ( 0 ),
194  mnTargetDepth ( 0 ),
195  mnTransRed ( 0 ),
196  mnTransGreen ( 0 ),
197  mnTransBlue ( 0 ),
198  mnPngDepth ( 0 ),
199  mnColorType ( 0 ),
200  mnCompressionType( 0 ),
201  mnFilterType ( 0 ),
202  mnInterlaceType ( 0 ),
203  mcTranspColor ( BitmapColor( 0xFF )),
204  mcOpaqueColor ( BitmapColor( 0x00 )),
205  mbTransparent( false ),
206  mbAlphaChannel( false ),
207  mbRGBTriple( false ),
208  mbPalette( false ),
209  mbGrayScale( false ),
210  mbzCodecInUse ( false ),
211  mbStatus( true ),
212  mbIDATStarted( false ),
213  mbIDATComplete( false ),
214  mbpHYs ( false ),
215  mbIgnoreGammaChunk ( false ),
216  mbIgnoreCRC( utl::ConfigManager::IsFuzzing() )
217 #if OSL_DEBUG_LEVEL > 0
218  ,mnAllocSizeScanline(0),
219  mnAllocSizeScanlineAlpha(0)
220 #endif
221 {
222  // prepare the PNG data stream
224  mrPNGStream.SetEndian( SvStreamEndian::BIG );
225 
226  // prepare the chunk reader
227  maChunkSeq.reserve( 16 );
228  maChunkIter = maChunkSeq.begin();
229 
230  // estimate PNG file size (to allow sanity checks)
232 
233  // check the PNG header magic
234  sal_uInt32 nDummy = 0;
235  mrPNGStream.ReadUInt32( nDummy );
236  mbStatus = (nDummy == 0x89504e47);
237  mrPNGStream.ReadUInt32( nDummy );
238  mbStatus = (nDummy == 0x0d0a1a0a) && mbStatus;
239 
240  mnPreviewShift = 0;
241  mnPreviewMask = (1 << mnPreviewShift) - 1;
242 }
243 
245 {
247 
248  if ( mbzCodecInUse )
250 
251  if( mpColorTable != mpDefaultColorTable )
252  delete[] mpColorTable;
253 }
254 
256 {
257  if( maChunkIter == maChunkSeq.end() )
258  {
259  // get the next chunk from the stream
260 
261  // unless we are at the end of the PNG stream
262  if (!mrPNGStream.good())
263  return false;
264  if( !maChunkSeq.empty() && (maChunkSeq.back().nType == PNGCHUNK_IEND) )
265  return false;
266 
267  PNGReader::ChunkData aDummyChunk;
268  maChunkIter = maChunkSeq.insert( maChunkSeq.end(), aDummyChunk );
269  PNGReader::ChunkData& rChunkData = *maChunkIter;
270 
271  // read the chunk header
272  mnChunkLen = 0;
273  mnChunkType = 0;
275  rChunkData.nType = mnChunkType;
276 
277  // fdo#61847 truncate over-long, trailing chunks
278  const std::size_t nStreamPos = mrPNGStream.Tell();
279  if( mnChunkLen < 0 || nStreamPos + mnChunkLen >= mnStreamSize )
280  mnChunkLen = mnStreamSize - nStreamPos;
281 
282  // calculate chunktype CRC (swap it back to original byte order)
283  sal_uInt32 nChunkType = mnChunkType;
284  #if defined(__LITTLEENDIAN) || defined(OSL_LITENDIAN)
285  nChunkType = OSL_SWAPDWORD( nChunkType );
286  #endif
287  sal_uInt32 nCRC32 = rtl_crc32( 0, &nChunkType, 4 );
288 
289  // read the chunk data and check the CRC
290  if( mnChunkLen && !mrPNGStream.eof() )
291  {
292  rChunkData.aData.resize( mnChunkLen );
293 
294  sal_Int32 nBytesRead = 0;
295  do
296  {
297  sal_uInt8& rPtr = rChunkData.aData[nBytesRead];
298  nBytesRead += mrPNGStream.ReadBytes(&rPtr, mnChunkLen - nBytesRead);
299  } while (nBytesRead < mnChunkLen && mrPNGStream.good());
300 
301  nCRC32 = rtl_crc32( nCRC32, rChunkData.aData.data(), mnChunkLen );
302  maDataIter = rChunkData.aData.begin();
303  }
304  sal_uInt32 nCheck(0);
305  mrPNGStream.ReadUInt32( nCheck );
306  if (!mbIgnoreCRC && nCRC32 != nCheck)
307  return false;
308  }
309  else
310  {
311  // the next chunk was already read
312  mnChunkType = (*maChunkIter).nType;
313  mnChunkLen = (*maChunkIter).aData.size();
314  maDataIter = (*maChunkIter).aData.begin();
315  }
316 
317  ++maChunkIter;
318  return mnChunkType != PNGCHUNK_IEND;
319 }
320 
321 const std::vector< vcl::PNGReader::ChunkData >& PNGReaderImpl::GetAllChunks()
322 {
323  // read the remaining chunks from mrPNGStream
324  while( ReadNextChunk() ) ;
325  return maChunkSeq;
326 }
327 
329 {
330  // reset to the first chunk
331  maChunkIter = maChunkSeq.begin();
332 
333  // first chunk must be IDHR
334  if( mbStatus && ReadNextChunk() )
335  {
336  if (mnChunkType == PNGCHUNK_IHDR)
338  else
339  mbStatus = false;
340  }
341 
342  // parse the remaining chunks
343  while (mbStatus && !mbIDATComplete && ReadNextChunk())
344  {
345  switch( mnChunkType )
346  {
347  case PNGCHUNK_IHDR :
348  {
349  mbStatus = false; //IHDR should only appear as the first chunk
350  }
351  break;
352 
353  case PNGCHUNK_gAMA : // the gamma chunk must precede
354  { // the 'IDAT' and also the 'PLTE'(if available )
356  ImplGetGamma();
357  }
358  break;
359 
360  case PNGCHUNK_PLTE :
361  {
362  if (!mbPalette && !mbIDATStarted)
364  }
365  break;
366 
367  case PNGCHUNK_tRNS :
368  {
369  if (!mbIDATComplete) // the tRNS chunk must precede the IDAT
371  }
372  break;
373 
374  case PNGCHUNK_bKGD : // the background chunk must appear
375  {
376  if (!mbIDATComplete && mbPalette) // before the 'IDAT' and after the
377  ImplGetBackground(); // PLTE(if available ) chunk.
378  }
379  break;
380 
381  case PNGCHUNK_IDAT :
382  {
383  if ( !mpInflateInBuf ) // taking care that the header has properly been read
384  mbStatus = false;
385  else if (!mbIDATComplete) // the gfx is finished, but there may be left a zlibCRC of about 4Bytes
386  ImplReadIDAT();
387  }
388  break;
389 
390  case PNGCHUNK_pHYs :
391  {
392  if (!mbIDATComplete && mnChunkLen == 9)
393  {
394  sal_uInt32 nXPixelPerMeter = ImplReadsal_uInt32();
395  sal_uInt32 nYPixelPerMeter = ImplReadsal_uInt32();
396 
397  sal_uInt8 nUnitSpecifier = *maDataIter++;
398  if( (nUnitSpecifier == 1) && nXPixelPerMeter && nYPixelPerMeter )
399  {
400  mbpHYs = true;
401 
402  // convert into MapUnit::Map100thMM
403  maPhysSize.setWidth( static_cast<sal_Int32>( (100000.0 * maOrigSize.Width()) / nXPixelPerMeter ) );
404  maPhysSize.setHeight( static_cast<sal_Int32>( (100000.0 * maOrigSize.Height()) / nYPixelPerMeter ) );
405  }
406  }
407  }
408  break;
409 
410  case PNGCHUNK_IEND:
411  mbStatus = mbIDATComplete; // there is a problem if the image is not complete yet
412  break;
413  }
414  }
415 
416  // release write access of the bitmaps
417  mxAcc.reset();
418  mxMaskAcc.reset();
419  mxAlphaAcc.reset();
420  mpMaskAcc = nullptr;
421 
422  // return the resulting BitmapEx
423  BitmapEx aRet;
424 
425  if (!mbStatus || !mbIDATComplete)
426  aRet.Clear();
427  else
428  {
429  if ( mpAlphaMask )
430  aRet = BitmapEx( *mpBmp, *mpAlphaMask );
431  else if ( mpMaskBmp )
432  aRet = BitmapEx( *mpBmp, *mpMaskBmp );
433  else
434  aRet = *mpBmp;
435 
436  if ( mbpHYs && maPhysSize.Width() && maPhysSize.Height() )
437  {
438  aRet.SetPrefMapMode(MapMode(MapUnit::Map100thMM));
439  aRet.SetPrefSize( maPhysSize );
440  }
441  }
442  return aRet;
443 }
444 
446 {
447  if( mnChunkLen < 13 )
448  return false;
449 
452 
453  if (maOrigSize.IsEmpty())
454  return false;
455 
456  mnPngDepth = *(maDataIter++);
457  mnColorType = *(maDataIter++);
458 
460  if( mnCompressionType != 0 ) // unknown compression type
461  return false;
462 
463  mnFilterType = *(maDataIter++);
464  if( mnFilterType != 0 ) // unknown filter type
465  return false;
466 
468  switch ( mnInterlaceType ) // filter type valid ?
469  {
470  case 0 : // progressive image
471  mnPass = 7;
472  break;
473  case 1 : // Adam7-interlaced image
474  mnPass = 0;
475  break;
476  default:
477  return false;
478  }
479 
480  mbPalette = true;
482  mbGrayScale = mbRGBTriple = false;
484  sal_uInt64 nScansize64 = ( ( static_cast< sal_uInt64 >( maOrigSize.Width() ) * mnPngDepth ) + 7 ) >> 3;
485 
486  // valid color types are 0,2,3,4 & 6
487  switch ( mnColorType )
488  {
489  case 0 : // each pixel is a grayscale
490  {
491  switch ( mnPngDepth )
492  {
493  case 2 : // 2bit target not available -> use four bits
494  mnTargetDepth = 4; // we have to expand the bitmap
495  mbGrayScale = true;
496  break;
497  case 16 :
498  mnTargetDepth = 8; // we have to reduce the bitmap
499  [[fallthrough]];
500  case 1 :
501  case 4 :
502  case 8 :
503  mbGrayScale = true;
504  break;
505  default :
506  return false;
507  }
508  }
509  break;
510 
511  case 2 : // each pixel is an RGB triple
512  {
513  mbRGBTriple = true;
514  nScansize64 *= 3;
515  switch ( mnPngDepth )
516  {
517  case 16 : // we have to reduce the bitmap
518  case 8 :
519  mnTargetDepth = 24;
520  break;
521  default :
522  return false;
523  }
524  }
525  break;
526 
527  case 3 : // each pixel is a palette index
528  {
529  switch ( mnPngDepth )
530  {
531  case 2 :
532  mnTargetDepth = 4; // we have to expand the bitmap
533  mbPalette = false;
534  break;
535  case 1 :
536  case 4 :
537  case 8 :
538  mbPalette = false;
539  break;
540  default :
541  return false;
542  }
543  }
544  break;
545 
546  case 4 : // each pixel is a grayscale sample followed by an alpha sample
547  {
548  nScansize64 *= 2;
549  mbAlphaChannel = true;
550  switch ( mnPngDepth )
551  {
552  case 16 :
553  mnTargetDepth = 8; // we have to reduce the bitmap
554  [[fallthrough]];
555  case 8 :
556  mbGrayScale = true;
557  break;
558  default :
559  return false;
560  }
561  }
562  break;
563 
564  case 6 : // each pixel is an RGB triple followed by an alpha sample
565  {
566  mbRGBTriple = true;
567  nScansize64 *= 4;
568  mbAlphaChannel = true;
569  switch (mnPngDepth )
570  {
571  case 16 : // we have to reduce the bitmap
572  case 8 :
573  mnTargetDepth = 24;
574  break;
575  default :
576  return false;
577  }
578  }
579  break;
580 
581  default :
582  return false;
583  }
584 
585  mnBPP = static_cast< sal_uInt32 >( nScansize64 / maOrigSize.Width() );
586  if ( !mnBPP )
587  mnBPP = 1;
588 
589  nScansize64++; // each scanline includes one filterbyte
590 
591  if ( nScansize64 > SAL_MAX_UINT32 )
592  return false;
593 
594  // assume max theoretical compression of 1:1032
595  sal_uInt64 nMinSizeRequired = (nScansize64 * maOrigSize.Height()) / 1032;
596  if (nMinSizeRequired > mnStreamSize)
597  {
598  SAL_WARN("vcl.gdi", "overlarge png dimensions: " <<
599  maOrigSize.Width() << " x " << maOrigSize.Height() << " depth: " << static_cast<int>(mnPngDepth) <<
600  " couldn't be supplied by file length " << mnStreamSize << " at least " << nMinSizeRequired << " needed ");
601  return false;
602  }
603 
604  mnScansize = static_cast< sal_uInt32 >( nScansize64 );
605 
608 
609  //round bits up to nearest multiple of 8 and divide by 8 to get num of bytes per pixel
610  int nBytesPerPixel = ((mnTargetDepth + 7) & ~7)/8;
611 
612  //stupidly big, forget about it
613  if (maTargetSize.Width() >= SAL_MAX_INT32 / nBytesPerPixel / maTargetSize.Height())
614  {
615  SAL_WARN( "vcl.gdi", "overlarge png dimensions: " <<
616  maTargetSize.Width() << " x " << maTargetSize.Height() << " depth: " << mnTargetDepth);
617  return false;
618  }
619 
620  // TODO: switch between both scanlines instead of copying
621  mpInflateInBuf.reset( new (std::nothrow) sal_uInt8[ mnScansize ] );
623  mpScanPrior.reset( new (std::nothrow) sal_uInt8[ mnScansize ] );
624 
625  if ( !mpInflateInBuf || !mpScanPrior )
626  return false;
627 
628  mpBmp = std::make_unique<Bitmap>( maTargetSize, mnTargetDepth );
630  if (!mxAcc)
631  return false;
632 
633  if ( mbAlphaChannel )
634  {
635  mpAlphaMask = std::make_unique<AlphaMask>( maTargetSize );
636  mpAlphaMask->Erase( 128 );
639  if (!mpMaskAcc)
640  return false;
641  }
642 
643  if ( mbGrayScale )
645 
646  ImplPreparePass();
647 
648  return true;
649 }
650 
651 void PNGReaderImpl::ImplGetGrayPalette( sal_uInt16 nBitDepth )
652 {
653  if( nBitDepth > 8 )
654  nBitDepth = 8;
655 
656  sal_uInt16 nPaletteEntryCount = 1 << nBitDepth;
657  sal_uInt32 nAdd = nBitDepth ? 256 / (nPaletteEntryCount - 1) : 0;
658 
659  // no bitdepth==2 available
660  // but bitdepth==4 with two unused bits is close enough
661  if( nBitDepth == 2 )
662  nPaletteEntryCount = 16;
663 
664  mxAcc->SetPaletteEntryCount( nPaletteEntryCount );
665  for ( sal_uInt32 i = 0, nStart = 0; nStart < 256; i++, nStart += nAdd )
666  mxAcc->SetPaletteColor( static_cast<sal_uInt16>(i), BitmapColor( mpColorTable[ nStart ],
667  mpColorTable[ nStart ], mpColorTable[ nStart ] ) );
668 }
669 
671 {
672  sal_uInt16 nCount = static_cast<sal_uInt16>( mnChunkLen / 3 );
673 
674  if ( ( ( mnChunkLen % 3 ) == 0 ) && ( ( 0 < nCount ) && ( nCount <= 256 ) ) && mxAcc )
675  {
676  mbPalette = true;
677  mxAcc->SetPaletteEntryCount( nCount );
678 
679  for ( sal_uInt16 i = 0; i < nCount; i++ )
680  {
681  sal_uInt8 nRed = mpColorTable[ *maDataIter++ ];
682  sal_uInt8 nGreen = mpColorTable[ *maDataIter++ ];
683  sal_uInt8 nBlue = mpColorTable[ *maDataIter++ ];
684  mxAcc->SetPaletteColor( i, Color( nRed, nGreen, nBlue ) );
685  }
686  }
687  else
688  mbStatus = false;
689 
690  return mbStatus;
691 }
692 
694 {
695  bool bNeedAlpha = false;
696 
697  if ( mpTransTab == nullptr )
698  {
699  switch ( mnColorType )
700  {
701  case 0 :
702  {
703  if ( mnChunkLen == 2 )
704  {
705  mpTransTab.reset( new sal_uInt8[ 256 ] );
706  memset( mpTransTab.get(), 0xff, 256);
707  // color type 0 and 4 is always greyscale,
708  // so the return value can be used as index
710  mpTransTab[ nIndex ] = 0;
711  mbTransparent = true;
712  }
713  }
714  break;
715 
716  case 2 :
717  {
718  if ( mnChunkLen == 6 )
719  {
723  mbTransparent = true;
724  }
725  }
726  break;
727 
728  case 3 :
729  {
730  if ( mnChunkLen <= 256 )
731  {
732  mbTransparent = true;
733  mpTransTab.reset( new sal_uInt8 [ 256 ] );
734  memset( mpTransTab.get(), 0xff, 256 );
735  if (mnChunkLen > 0)
736  {
737  memcpy( mpTransTab.get(), &(*maDataIter), mnChunkLen );
739  // need alpha transparency if not on/off masking
740  for( int i = 0; i < mnChunkLen; ++i )
741  bNeedAlpha |= (mpTransTab[i]!=0x00) && (mpTransTab[i]!=0xFF);
742  }
743  }
744  }
745  break;
746  }
747  }
748 
750  {
751  if( bNeedAlpha)
752  {
753  mpAlphaMask = std::make_unique<AlphaMask>( maTargetSize );
755  mpMaskAcc = mxAlphaAcc.get();
756  }
757  else
758  {
759  mpMaskBmp = std::make_unique<Bitmap>( maTargetSize, 1 );
761  mpMaskAcc = mxMaskAcc.get();
762  }
763  mbTransparent = (mpMaskAcc != nullptr);
764  if( !mbTransparent )
765  return false;
766  mpMaskAcc->Erase( Color(0,0,0) );
767  }
768 
769  return true;
770 }
771 
773 {
774  if( mnChunkLen < 4 )
775  return;
776 
777  sal_uInt32 nGammaValue = ImplReadsal_uInt32();
778  double fGamma = ( VIEWING_GAMMA / DISPLAY_GAMMA ) * ( static_cast<double>(nGammaValue) / 100000 );
779  double fInvGamma = ( fGamma <= 0.0 || fGamma > 10.0 ) ? 1.0 : ( 1.0 / fGamma );
780 
781  if ( fInvGamma != 1.0 )
782  {
783  if ( mpColorTable == mpDefaultColorTable )
784  mpColorTable = new sal_uInt8[ 256 ];
785 
786  for ( sal_Int32 i = 0; i < 256; i++ )
787  mpColorTable[ i ] = static_cast<sal_uInt8>(pow(static_cast<double>(i)/255.0, fInvGamma) * 255.0 + 0.5);
788 
789  if ( mbGrayScale )
791  }
792 }
793 
795 {
796  switch (mnColorType)
797  {
798  case 3:
799  {
800  if (mnChunkLen == 1)
801  {
802  sal_uInt16 nCol = *maDataIter++;
803 
804  if (nCol < mxAcc->GetPaletteEntryCount())
805  {
806  mxAcc->Erase(mxAcc->GetPaletteColor(static_cast<sal_uInt8>(nCol)));
807  break;
808  }
809  }
810  }
811  break;
812 
813  case 0:
814  case 4:
815  {
816  if (mnChunkLen == 2)
817  {
818  // the color type 0 and 4 is always greyscale,
819  // so the return value can be used as index
821  }
822  }
823  break;
824 
825  case 2:
826  case 6:
827  {
828  if (mnChunkLen == 6)
829  {
830  sal_uInt8 nRed = ImplScaleColor();
831  sal_uInt8 nGreen = ImplScaleColor();
832  sal_uInt8 nBlue = ImplScaleColor();
833  // ofz#18653 slow and uninteresting
835  return;
836  mxAcc->Erase(Color(nRed, nGreen, nBlue));
837  }
838  }
839  break;
840  }
841 }
842 
843 // for color type 0 and 4 (greyscale) the return value is always index to the color
844 // 2 and 6 (RGB) the return value is always the 8 bit color component
846 {
847  sal_uInt32 nMask = ( 1 << mnPngDepth ) - 1;
848  sal_uInt16 nCol = ( *maDataIter++ << 8 );
849 
850  nCol += *maDataIter++ & static_cast<sal_uInt16>(nMask);
851 
852  if ( mnPngDepth > 8 ) // convert 16bit graphics to 8
853  nCol >>= 8;
854 
855  return static_cast<sal_uInt8>(nCol);
856 }
857 
858 // ImplReadIDAT reads as much image data as needed
859 
861 {
862  //when fuzzing with a max len set, max decompress to 250 times that limit
863  static size_t nMaxAllowedDecompression = [](const char* pEnv) { size_t nRet = pEnv ? std::atoi(pEnv) : 0; return nRet * 250; }(std::getenv("FUZZ_MAX_INPUT_LEN"));
864  size_t nTotalDataRead = 0;
865 
866  if( mnChunkLen > 0 )
867  {
868  mbIDATStarted = true;
869 
870  if ( !mbzCodecInUse )
871  {
872  mbzCodecInUse = true;
874  }
876  SvMemoryStream aIStrm( &(*maDataIter), mnChunkLen, StreamMode::READ );
877 
878  while ( mpZCodec.GetBreak() )
879  {
880  // get bytes needed to fill the current scanline
881  sal_Int32 nToRead = mnScansize - (mpScanCurrent - mpInflateInBuf.get());
882  sal_Int32 nRead = mpZCodec.ReadAsynchron( aIStrm, mpScanCurrent, nToRead );
883  if ( nRead < 0 )
884  {
885  mbStatus = false;
886  break;
887  }
888  nTotalDataRead += nRead;
889  if (nMaxAllowedDecompression && nTotalDataRead > nMaxAllowedDecompression)
890  {
891  mbStatus = false;
892  break;
893  }
894  if ( nRead < nToRead )
895  {
896  mpScanCurrent += nRead; // more ZStream data in the next IDAT chunk
897  break;
898  }
899  else // this scanline is Finished
900  {
902  ImplApplyFilter();
903 
905  mnYpos += mnYAdd;
906  }
907 
909  {
910  if( (mnPass < 7) && mnInterlaceType )
911  if( ImplPreparePass() )
912  continue;
913  mbIDATComplete = true;
914  break;
915  }
916  }
917  }
918 
919  if (mbIDATComplete)
920  {
922  mbzCodecInUse = false;
923  }
924 }
925 
927 {
928  struct InterlaceParams{ int mnXStart, mnYStart, mnXAdd, mnYAdd; };
929  static const InterlaceParams aInterlaceParams[8] =
930  {
931  // non-interlaced
932  { 0, 0, 1, 1 },
933  // Adam7-interlaced
934  { 0, 0, 8, 8 }, // pass 1
935  { 4, 0, 8, 8 }, // pass 2
936  { 0, 4, 4, 8 }, // pass 3
937  { 2, 0, 4, 4 }, // pass 4
938  { 0, 2, 2, 4 }, // pass 5
939  { 1, 0, 2, 2 }, // pass 6
940  { 0, 1, 1, 2 } // pass 7
941  };
942 
943  const InterlaceParams* pParam = &aInterlaceParams[ 0 ];
944  if( mnInterlaceType )
945  {
946  while( ++mnPass <= 7 )
947  {
948  pParam = &aInterlaceParams[ mnPass ];
949 
950  // skip this pass if the original image is too small for it
951  if( (pParam->mnXStart < maOrigSize.Width())
952  && (pParam->mnYStart < maOrigSize.Height()) )
953  break;
954  }
955  if( mnPass > 7 )
956  return false;
957 
958  // skip the last passes if possible (for scaled down target images)
959  if( mnPreviewMask & (pParam->mnXStart | pParam->mnYStart) )
960  return false;
961  }
962 
963  mnYpos = pParam->mnYStart;
964  mnXStart = pParam->mnXStart;
965  mnXAdd = pParam->mnXAdd;
966  mnYAdd = pParam->mnYAdd;
967 
968  // in Interlace mode the size of scanline is not constant
969  // so first we calculate the number of entries
970  long nScanWidth = (maOrigSize.Width() - mnXStart + mnXAdd - 1) / mnXAdd;
971  mnScansize = nScanWidth;
972 
973  if( mbRGBTriple )
974  mnScansize = 3 * nScanWidth;
975 
976  if( mbAlphaChannel )
977  mnScansize += nScanWidth;
978 
979  // convert to width in bytes
980  mnScansize = ( mnScansize*mnPngDepth + 7 ) >> 3;
981 
982  ++mnScansize; // scan size also needs room for the filtertype byte
983  memset( mpScanPrior.get(), 0, mnScansize );
984 
985  return true;
986 }
987 
988 // ImplApplyFilter writes the complete Scanline (nY)
989 // in interlace mode the parameter nXStart and nXAdd are non-zero
990 
992 {
993  OSL_ASSERT( mnScansize >= mnBPP + 1 );
994  const sal_uInt8* const pScanEnd = mpInflateInBuf.get() + mnScansize;
995 
996  sal_uInt8 nFilterType = mpInflateInBuf[0]; // the filter type may change each scanline
997  switch ( nFilterType )
998  {
999  default: // unknown Scanline Filter Type
1000  case 0: // Filter Type "None"
1001  // we let the pixels pass and display the data unfiltered
1002  break;
1003 
1004  case 1: // Scanline Filter Type "Sub"
1005  {
1006  sal_uInt8* p1 = mpInflateInBuf.get() + 1;
1007  const sal_uInt8* p2 = p1;
1008  p1 += mnBPP;
1009 
1010  // use left pixels
1011  while (p1 < pScanEnd)
1012  {
1013  *p1 = static_cast<sal_uInt8>( *p1 + *(p2++) );
1014  ++p1;
1015  }
1016  }
1017  break;
1018 
1019  case 2: // Scanline Filter Type "Up"
1020  {
1021  sal_uInt8* p1 = mpInflateInBuf.get() + 1;
1022  const sal_uInt8* p2 = mpScanPrior.get() + 1;
1023 
1024  // use pixels from prior line
1025  while( p1 < pScanEnd )
1026  {
1027  *p1 = static_cast<sal_uInt8>( *p1 + *(p2++) );
1028  ++p1;
1029  }
1030  }
1031  break;
1032 
1033  case 3: // Scanline Filter Type "Average"
1034  {
1035  sal_uInt8* p1 = mpInflateInBuf.get() + 1;
1036  const sal_uInt8* p2 = mpScanPrior.get() + 1;
1037  const sal_uInt8* p3 = p1;
1038 
1039  // use one pixel from prior line
1040  for( int n = mnBPP; --n >= 0; ++p1, ++p2)
1041  *p1 = static_cast<sal_uInt8>( *p1 + (*p2 >> 1) );
1042 
1043  // predict by averaging the left and prior line pixels
1044  while( p1 < pScanEnd )
1045  {
1046  *p1 = static_cast<sal_uInt8>( *p1 + ((*(p2++) + *(p3++)) >> 1) );
1047  ++p1;
1048  }
1049  }
1050  break;
1051 
1052  case 4: // Scanline Filter Type "PathPredictor"
1053  {
1054  sal_uInt8* p1 = mpInflateInBuf.get() + 1;
1055  const sal_uInt8* p2 = mpScanPrior.get() + 1;
1056  const sal_uInt8* p3 = p1;
1057  const sal_uInt8* p4 = p2;
1058 
1059  // use one pixel from prior line
1060  for( int n = mnBPP; --n >= 0; ++p1)
1061  *p1 = static_cast<sal_uInt8>( *p1 + *(p2++) );
1062 
1063  // predict by using the left and the prior line pixels
1064  while( p1 < pScanEnd )
1065  {
1066  int na = *(p2++);
1067  int nb = *(p3++);
1068  int nc = *(p4++);
1069 
1070  int npa = nb - nc;
1071  int npb = na - nc;
1072  int npc = npa + npb;
1073 
1074  if( npa < 0 )
1075  npa =-npa;
1076  if( npb < 0 )
1077  npb =-npb;
1078  if( npc < 0 )
1079  npc =-npc;
1080 
1081  if( npa > npb )
1082  {
1083  na = nb;
1084  npa = npb;
1085  }
1086  if( npa > npc )
1087  na = nc;
1088 
1089  *p1 = static_cast<sal_uInt8>( *p1 + na );
1090  ++p1;
1091  }
1092  }
1093  break;
1094  }
1095 
1096  memcpy( mpScanPrior.get(), mpInflateInBuf.get(), mnScansize );
1097 }
1098 
1099 namespace
1100 {
1101  sal_uInt8 SanitizePaletteIndex(sal_uInt8 nIndex, sal_uInt16 nPaletteEntryCount)
1102  {
1103  if (nIndex >= nPaletteEntryCount)
1104  {
1105  auto nSanitizedIndex = nIndex % nPaletteEntryCount;
1106  SAL_WARN_IF(nIndex != nSanitizedIndex, "vcl", "invalid colormap index: "
1107  << static_cast<unsigned int>(nIndex) << ", colormap len is: "
1108  << nPaletteEntryCount);
1109  nIndex = nSanitizedIndex;
1110  }
1111  return nIndex;
1112  }
1113 
1114  void SanitizePaletteIndexes(sal_uInt8* pEntries, int nLen, const BitmapScopedWriteAccess& rAcc)
1115  {
1116  sal_uInt16 nPaletteEntryCount = rAcc->GetPaletteEntryCount();
1117  for (int nX = 0; nX < nLen; ++nX)
1118  {
1119  if (pEntries[nX] >= nPaletteEntryCount)
1120  {
1121  SAL_WARN("vcl.gdi", "invalid colormap index: "
1122  << static_cast<unsigned int>(pEntries[nX]) << ", colormap len is: "
1123  << nPaletteEntryCount);
1124  pEntries[nX] = pEntries[nX] % nPaletteEntryCount;
1125  }
1126  }
1127  }
1128 }
1129 
1130 // ImplDrawScanlines draws the complete Scanline (nY) into the target bitmap
1131 // In interlace mode the parameter nXStart and nXAdd append to the currently used pass
1132 
1133 void PNGReaderImpl::ImplDrawScanline( sal_uInt32 nXStart, sal_uInt32 nXAdd )
1134 {
1135  // optimization for downscaling
1136  if( mnYpos & mnPreviewMask )
1137  return;
1138  if( nXStart & mnPreviewMask )
1139  return;
1140 
1141  // convert nY to pixel units in the target image
1142  // => TODO; also do this for nX here instead of in the ImplSet*Pixel() methods
1143  const sal_uInt32 nY = mnYpos >> mnPreviewShift;
1144 
1145  sal_uInt8* pTmp = mpInflateInBuf.get() + 1;
1146  if ( mxAcc->HasPalette() ) // alphachannel is not allowed by pictures including palette entries
1147  {
1148  switch ( mxAcc->GetBitCount() )
1149  {
1150  case 1 :
1151  {
1152  if ( mbTransparent )
1153  {
1154  for ( long nX = nXStart, nShift = 0; nX < maOrigSize.Width(); nX += nXAdd )
1155  {
1156  sal_uInt8 nCol;
1157  nShift = (nShift - 1) & 7;
1158  if ( nShift == 0 )
1159  nCol = *(pTmp++);
1160  else
1161  nCol = static_cast<sal_uInt8>( *pTmp >> nShift );
1162  nCol &= 1;
1163 
1164  ImplSetAlphaPixel( nY, nX, nCol, mpTransTab[ nCol ] );
1165  }
1166  }
1167  else
1168  { // ScanlineFormat::N1BitMsbPal
1169  for ( long nX = nXStart, nShift = 0; nX < maOrigSize.Width(); nX += nXAdd )
1170  {
1171  nShift = (nShift - 1) & 7;
1172 
1173  sal_uInt8 nCol;
1174  if ( nShift == 0 )
1175  nCol = *(pTmp++);
1176  else
1177  nCol = static_cast<sal_uInt8>( *pTmp >> nShift );
1178  nCol &= 1;
1179 
1180  ImplSetPixel( nY, nX, nCol );
1181  }
1182  }
1183  }
1184  break;
1185 
1186  case 4 :
1187  {
1188  if ( mbTransparent )
1189  {
1190  if ( mnPngDepth == 4 ) // check if source has a two bit pixel format
1191  {
1192  for ( long nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, ++nXIndex )
1193  {
1194  if( nXIndex & 1 )
1195  {
1196  ImplSetAlphaPixel( nY, nX, *pTmp & 0x0f, mpTransTab[ *pTmp & 0x0f ] );
1197  pTmp++;
1198  }
1199  else
1200  {
1201  ImplSetAlphaPixel( nY, nX, ( *pTmp >> 4 ) & 0x0f, mpTransTab[ *pTmp >> 4 ] );
1202  }
1203  }
1204  }
1205  else // if ( mnPngDepth == 2 )
1206  {
1207  for ( long nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, nXIndex++ )
1208  {
1209  sal_uInt8 nCol;
1210  switch( nXIndex & 3 )
1211  {
1212  case 0 :
1213  nCol = *pTmp >> 6;
1214  break;
1215 
1216  case 1 :
1217  nCol = ( *pTmp >> 4 ) & 0x03 ;
1218  break;
1219 
1220  case 2 :
1221  nCol = ( *pTmp >> 2 ) & 0x03;
1222  break;
1223 
1224  case 3 :
1225  nCol = ( *pTmp++ ) & 0x03;
1226  break;
1227 
1228  default: // get rid of nCol uninitialized warning
1229  nCol = 0;
1230  break;
1231  }
1232 
1233  ImplSetAlphaPixel( nY, nX, nCol, mpTransTab[ nCol ] );
1234  }
1235  }
1236  }
1237  else
1238  {
1239  if ( mnPngDepth == 4 ) // maybe the source is a two bitmap graphic
1240  { // ScanlineFormat::N4BitLsnPal
1241  for ( long nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, nXIndex++ )
1242  {
1243  if( nXIndex & 1 )
1244  ImplSetPixel( nY, nX, *pTmp++ & 0x0f );
1245  else
1246  ImplSetPixel( nY, nX, ( *pTmp >> 4 ) & 0x0f );
1247  }
1248  }
1249  else // if ( mnPngDepth == 2 )
1250  {
1251  for ( long nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, nXIndex++ )
1252  {
1253  switch( nXIndex & 3 )
1254  {
1255  case 0 :
1256  ImplSetPixel( nY, nX, *pTmp >> 6 );
1257  break;
1258 
1259  case 1 :
1260  ImplSetPixel( nY, nX, ( *pTmp >> 4 ) & 0x03 );
1261  break;
1262 
1263  case 2 :
1264  ImplSetPixel( nY, nX, ( *pTmp >> 2 ) & 0x03 );
1265  break;
1266 
1267  case 3 :
1268  ImplSetPixel( nY, nX, *pTmp++ & 0x03 );
1269  break;
1270  }
1271  }
1272  }
1273  }
1274  }
1275  break;
1276 
1277  case 8 :
1278  {
1279  if ( mbAlphaChannel )
1280  {
1281  if ( mnPngDepth == 8 ) // maybe the source is a 16 bit grayscale
1282  {
1283  for ( long nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 2 )
1284  ImplSetAlphaPixel( nY, nX, pTmp[ 0 ], pTmp[ 1 ] );
1285  }
1286  else
1287  {
1288  assert(mnPngDepth == 16);
1289  for ( long nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 4 )
1290  ImplSetAlphaPixel( nY, nX, pTmp[ 0 ], pTmp[ 2 ] );
1291  }
1292  }
1293  else if ( mbTransparent )
1294  {
1295  if ( mnPngDepth == 8 ) // maybe the source is a 16 bit grayscale
1296  {
1297  for ( long nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp++ )
1298  ImplSetAlphaPixel( nY, nX, *pTmp, mpTransTab[ *pTmp ] );
1299  }
1300  else if (mnPngDepth == 1 )
1301  {
1302  for ( long nX = nXStart, nShift = 0; nX < maOrigSize.Width(); nX += nXAdd )
1303  {
1304  nShift = (nShift - 1) & 7;
1305 
1306  sal_uInt8 nCol;
1307  if ( nShift == 0 )
1308  nCol = *(pTmp++);
1309  else
1310  nCol = static_cast<sal_uInt8>( *pTmp >> nShift );
1311  nCol &= 1;
1312 
1313  ImplSetAlphaPixel( nY, nX, nCol, mpTransTab[ nCol ] );
1314  }
1315  }
1316  else
1317  {
1318  for ( long nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 2 )
1319  ImplSetAlphaPixel( nY, nX, *pTmp, mpTransTab[ *pTmp ] );
1320  }
1321  }
1322  else // neither alpha nor transparency
1323  {
1324  if ( mnPngDepth == 8 ) // maybe the source is a 16 bit grayscale or 1 bit indexed
1325  {
1326  if( nXAdd == 1 && mnPreviewShift == 0 ) // copy raw line data if possible
1327  {
1328  int nLineBytes = maOrigSize.Width();
1329  if (mbPalette)
1330  SanitizePaletteIndexes(pTmp, nLineBytes, mxAcc);
1331  mxAcc->CopyScanline( nY, pTmp, ScanlineFormat::N8BitPal, nLineBytes );
1332  }
1333  else
1334  {
1335  for ( long nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd )
1336  ImplSetPixel( nY, nX, *pTmp++ );
1337  }
1338  }
1339  else if (mnPngDepth == 1 )
1340  {
1341  for ( long nX = nXStart, nShift = 0; nX < maOrigSize.Width(); nX += nXAdd )
1342  {
1343  nShift = (nShift - 1) & 7;
1344 
1345  sal_uInt8 nCol;
1346  if ( nShift == 0 )
1347  nCol = *(pTmp++);
1348  else
1349  nCol = static_cast<sal_uInt8>( *pTmp >> nShift );
1350  nCol &= 1;
1351 
1352  ImplSetPixel( nY, nX, nCol );
1353  }
1354  }
1355  else
1356  {
1357  for ( long nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 2 )
1358  ImplSetPixel( nY, nX, *pTmp );
1359  }
1360  }
1361  }
1362  break;
1363 
1364  default :
1365  mbStatus = false;
1366  break;
1367  }
1368  }
1369  else // no palette => truecolor
1370  {
1371  if( mbAlphaChannel )
1372  {
1373  // has RGB + alpha
1374  if ( mnPngDepth == 8 ) // maybe the source has 16 bit per sample
1375  {
1376  // ScanlineFormat::N32BitTcRgba
1377  // only use DirectScanline when we have no preview shifting stuff and accesses to content and alpha
1378  const bool bDoDirectScanline(
1379  !nXStart && 1 == nXAdd && !mnPreviewShift && mpMaskAcc);
1380  const bool bCustomColorTable(mpColorTable != mpDefaultColorTable);
1381 
1382  if(bDoDirectScanline)
1383  {
1384  // allocate scanlines on demand, reused for next line
1385  if(!mpScanline)
1386  {
1387 #if OSL_DEBUG_LEVEL > 0
1389 #endif
1390  mpScanline.reset( new sal_uInt8[maOrigSize.Width() * 3] );
1391  }
1392 
1393  if(!mpScanlineAlpha)
1394  {
1395 #if OSL_DEBUG_LEVEL > 0
1397 #endif
1398  mpScanlineAlpha.reset( new sal_uInt8[maOrigSize.Width()] );
1399  }
1400  }
1401 
1402  if(bDoDirectScanline)
1403  {
1404  OSL_ENSURE(mpScanline, "No Scanline allocated (!)");
1405  OSL_ENSURE(mpScanlineAlpha, "No ScanlineAlpha allocated (!)");
1406 #if OSL_DEBUG_LEVEL > 0
1407  OSL_ENSURE(mnAllocSizeScanline >= maOrigSize.Width() * 3, "Allocated Scanline too small (!)");
1408  OSL_ENSURE(mnAllocSizeScanlineAlpha >= maOrigSize.Width(), "Allocated ScanlineAlpha too small (!)");
1409 #endif
1410  sal_uInt8* pScanline(mpScanline.get());
1411  sal_uInt8* pScanlineAlpha(mpScanlineAlpha.get());
1412 
1413  for (long nX(0); nX < maOrigSize.Width(); nX++, pTmp += 4)
1414  {
1415  // prepare content line as BGR by reordering when copying
1416  // do not forget to invert alpha (source is alpha, target is opacity)
1417  if(bCustomColorTable)
1418  {
1419  *pScanline++ = mpColorTable[pTmp[2]];
1420  *pScanline++ = mpColorTable[pTmp[1]];
1421  *pScanline++ = mpColorTable[pTmp[0]];
1422  *pScanlineAlpha++ = ~pTmp[3];
1423  }
1424  else
1425  {
1426  *pScanline++ = pTmp[2];
1427  *pScanline++ = pTmp[1];
1428  *pScanline++ = pTmp[0];
1429  *pScanlineAlpha++ = ~pTmp[3];
1430  }
1431  }
1432 
1433  // copy scanlines directly to bitmaps for content and alpha; use the formats which
1434  // are able to copy directly to BitmapBuffer
1437  }
1438  else
1439  {
1440  for ( long nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 4 )
1441  {
1442  if(bCustomColorTable)
1443  {
1445  nY,
1446  nX,
1447  BitmapColor(
1448  mpColorTable[ pTmp[ 0 ] ],
1449  mpColorTable[ pTmp[ 1 ] ],
1450  mpColorTable[ pTmp[ 2 ] ]),
1451  pTmp[ 3 ]);
1452  }
1453  else
1454  {
1456  nY,
1457  nX,
1458  BitmapColor(
1459  pTmp[0],
1460  pTmp[1],
1461  pTmp[2]),
1462  pTmp[3]);
1463  }
1464  }
1465  }
1466  }
1467  else
1468  {
1469  // BMP_FORMAT_64BIT_TC_RGBA
1470  for ( long nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 8 )
1471  {
1473  nY,
1474  nX,
1475  BitmapColor(
1476  mpColorTable[ pTmp[ 0 ] ],
1477  mpColorTable[ pTmp[ 2 ] ],
1478  mpColorTable[ pTmp[ 4 ] ]),
1479  pTmp[6]);
1480  }
1481  }
1482  }
1483  else if( mbTransparent ) // has RGB + transparency
1484  {
1485  // ScanlineFormat::N24BitTcRgb
1486  // no support currently for DirectScanline, found no real usages in current PNGs, may be added on demand
1487  if ( mnPngDepth == 8 ) // maybe the source has 16 bit per sample
1488  {
1489  for ( long nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 3 )
1490  {
1491  sal_uInt8 nRed = pTmp[ 0 ];
1492  sal_uInt8 nGreen = pTmp[ 1 ];
1493  sal_uInt8 nBlue = pTmp[ 2 ];
1494  bool bTransparent = ( ( nRed == mnTransRed )
1495  && ( nGreen == mnTransGreen )
1496  && ( nBlue == mnTransBlue ) );
1497 
1498  ImplSetTranspPixel( nY, nX, BitmapColor( mpColorTable[ nRed ],
1499  mpColorTable[ nGreen ],
1500  mpColorTable[ nBlue ] ), bTransparent );
1501  }
1502  }
1503  else
1504  {
1505  // BMP_FORMAT_48BIT_TC_RGB
1506  for ( long nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 6 )
1507  {
1508  sal_uInt8 nRed = pTmp[ 0 ];
1509  sal_uInt8 nGreen = pTmp[ 2 ];
1510  sal_uInt8 nBlue = pTmp[ 4 ];
1511  bool bTransparent = ( ( nRed == mnTransRed )
1512  && ( nGreen == mnTransGreen )
1513  && ( nBlue == mnTransBlue ) );
1514 
1515  ImplSetTranspPixel( nY, nX, BitmapColor( mpColorTable[ nRed ],
1516  mpColorTable[ nGreen ],
1517  mpColorTable[ nBlue ] ), bTransparent );
1518  }
1519  }
1520  }
1521  else // has RGB but neither alpha nor transparency
1522  {
1523  // ScanlineFormat::N24BitTcRgb
1524  // only use DirectScanline when we have no preview shifting stuff and access to content
1525  const bool bDoDirectScanline(
1526  !nXStart && 1 == nXAdd && !mnPreviewShift);
1527  const bool bCustomColorTable(mpColorTable != mpDefaultColorTable);
1528 
1529  if(bDoDirectScanline && !mpScanline)
1530  {
1531  // allocate scanlines on demand, reused for next line
1532 #if OSL_DEBUG_LEVEL > 0
1534 #endif
1535  mpScanline.reset( new sal_uInt8[maOrigSize.Width() * 3] );
1536  }
1537 
1538  if ( mnPngDepth == 8 ) // maybe the source has 16 bit per sample
1539  {
1540  if(bDoDirectScanline)
1541  {
1542  OSL_ENSURE(mpScanline, "No Scanline allocated (!)");
1543 #if OSL_DEBUG_LEVEL > 0
1544  OSL_ENSURE(mnAllocSizeScanline >= maOrigSize.Width() * 3, "Allocated Scanline too small (!)");
1545 #endif
1546  sal_uInt8* pScanline(mpScanline.get());
1547 
1548  for (long nX(0); nX < maOrigSize.Width(); nX++, pTmp += 3)
1549  {
1550  // prepare content line as BGR by reordering when copying
1551  if(bCustomColorTable)
1552  {
1553  *pScanline++ = mpColorTable[pTmp[2]];
1554  *pScanline++ = mpColorTable[pTmp[1]];
1555  *pScanline++ = mpColorTable[pTmp[0]];
1556  }
1557  else
1558  {
1559  *pScanline++ = pTmp[2];
1560  *pScanline++ = pTmp[1];
1561  *pScanline++ = pTmp[0];
1562  }
1563  }
1564 
1565  // copy scanline directly to bitmap for content; use the format which is able to
1566  // copy directly to BitmapBuffer
1568  }
1569  else
1570  {
1571  for ( long nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 3 )
1572  {
1573  if(bCustomColorTable)
1574  {
1575  ImplSetPixel(
1576  nY,
1577  nX,
1578  BitmapColor(
1579  mpColorTable[ pTmp[ 0 ] ],
1580  mpColorTable[ pTmp[ 1 ] ],
1581  mpColorTable[ pTmp[ 2 ] ]));
1582  }
1583  else
1584  {
1585  ImplSetPixel(
1586  nY,
1587  nX,
1588  BitmapColor(
1589  pTmp[0],
1590  pTmp[1],
1591  pTmp[2]));
1592  }
1593  }
1594  }
1595  }
1596  else
1597  {
1598  // BMP_FORMAT_48BIT_TC_RGB
1599  // no support currently for DirectScanline, found no real usages in current PNGs, may be added on demand
1600  for ( long nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 6 )
1601  {
1602  ImplSetPixel(
1603  nY,
1604  nX,
1605  BitmapColor(
1606  mpColorTable[ pTmp[ 0 ] ],
1607  mpColorTable[ pTmp[ 2 ] ],
1608  mpColorTable[ pTmp[ 4 ] ]));
1609  }
1610  }
1611  }
1612  }
1613 }
1614 
1615 void PNGReaderImpl::ImplSetPixel( sal_uInt32 nY, sal_uInt32 nX, const BitmapColor& rBitmapColor )
1616 {
1617  // TODO: get preview mode checks out of inner loop
1618  if( nX & mnPreviewMask )
1619  return;
1620  nX >>= mnPreviewShift;
1621 
1622  mxAcc->SetPixel( nY, nX, rBitmapColor );
1623 }
1624 
1625 void PNGReaderImpl::ImplSetPixel( sal_uInt32 nY, sal_uInt32 nX, sal_uInt8 nPalIndex )
1626 {
1627  // TODO: get preview mode checks out of inner loop
1628  if( nX & mnPreviewMask )
1629  return;
1630  nX >>= mnPreviewShift;
1631 
1632  mxAcc->SetPixelIndex(nY, nX, SanitizePaletteIndex(nPalIndex, mxAcc->GetPaletteEntryCount()));
1633 }
1634 
1635 void PNGReaderImpl::ImplSetTranspPixel( sal_uInt32 nY, sal_uInt32 nX, const BitmapColor& rBitmapColor, bool bTrans )
1636 {
1637  // TODO: get preview mode checks out of inner loop
1638  if( nX & mnPreviewMask )
1639  return;
1640  nX >>= mnPreviewShift;
1641 
1642  mxAcc->SetPixel( nY, nX, rBitmapColor );
1643 
1644  if ( bTrans )
1645  mpMaskAcc->SetPixel( nY, nX, mcTranspColor );
1646  else
1647  mpMaskAcc->SetPixel( nY, nX, mcOpaqueColor );
1648 }
1649 
1650 void PNGReaderImpl::ImplSetAlphaPixel( sal_uInt32 nY, sal_uInt32 nX,
1651  sal_uInt8 nPalIndex, sal_uInt8 nAlpha )
1652 {
1653  // TODO: get preview mode checks out of inner loop
1654  if( nX & mnPreviewMask )
1655  return;
1656  nX >>= mnPreviewShift;
1657 
1658  mxAcc->SetPixelIndex(nY, nX, SanitizePaletteIndex(nPalIndex, mxAcc->GetPaletteEntryCount()));
1659  mpMaskAcc->SetPixel(nY, nX, BitmapColor(~nAlpha));
1660 }
1661 
1662 void PNGReaderImpl::ImplSetAlphaPixel( sal_uInt32 nY, sal_uInt32 nX,
1663  const BitmapColor& rBitmapColor, sal_uInt8 nAlpha )
1664 {
1665  // TODO: get preview mode checks out of inner loop
1666  if( nX & mnPreviewMask )
1667  return;
1668  nX >>= mnPreviewShift;
1669 
1670  mxAcc->SetPixel( nY, nX, rBitmapColor );
1671  if (!mpMaskAcc)
1672  return;
1673  mpMaskAcc->SetPixel(nY, nX, BitmapColor(~nAlpha));
1674 }
1675 
1677 {
1678  sal_uInt32 nRet;
1679  nRet = *maDataIter++;
1680  nRet <<= 8;
1681  nRet |= *maDataIter++;
1682  nRet <<= 8;
1683  nRet |= *maDataIter++;
1684  nRet <<= 8;
1685  nRet |= *maDataIter++;
1686  return nRet;
1687 }
1688 
1690  mpImpl(new vcl::PNGReaderImpl(rIStream))
1691 {
1692 }
1693 
1695 {
1696 }
1697 
1699 {
1700  return mpImpl->GetBitmapEx();
1701 }
1702 
1703 const std::vector< vcl::PNGReader::ChunkData >& PNGReader::GetChunks() const
1704 {
1705  return mpImpl->GetAllChunks();
1706 }
1707 
1708 void PNGReader::SetIgnoreGammaChunk(bool bIgnoreGammaChunk)
1709 {
1710  mpImpl->SetIgnoreGammaChunk(bIgnoreGammaChunk);
1711 }
1712 
1713 } // namespace vcl
1714 
1715 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 mnTargetDepth
Definition: pngread.cxx:115
long Width() const
BitmapScopedWriteAccess mxMaskAcc
Definition: pngread.cxx:86
void ImplSetPixel(sal_uInt32 y, sal_uInt32 x, const BitmapColor &)
Definition: pngread.cxx:1615
void SetBreak(size_t)
sal_uInt32 mnXStart
Definition: pngread.cxx:110
void ImplGetBackground()
Definition: pngread.cxx:794
sal_Int32 nIndex
sal_uInt8 mnColorType
Definition: pngread.cxx:120
long Height() const
sal_uInt8 mnTransRed
Definition: pngread.cxx:116
std::unique_ptr< Bitmap > mpBmp
Definition: pngread.cxx:83
virtual sal_uInt64 TellEnd()
void SetPrefMapMode(const MapMode &rPrefMapMode)
Definition: bitmapex.hxx:90
void SetPixel(long nY, long nX, const BitmapColor &rBitmapColor)
sal_Int64 n
vcl::ScopedBitmapAccess< BitmapWriteAccess, Bitmap,&Bitmap::AcquireWriteAccess > BitmapScopedWriteAccess
sal_uInt16 GetBitCount() const
sal_uInt8 * mpScanCurrent
Definition: pngread.cxx:98
bool ImplReadHeader()
Definition: pngread.cxx:445
bool ImplReadPalette()
Definition: pngread.cxx:670
void ImplSetTranspPixel(sal_uInt32 y, sal_uInt32 x, const BitmapColor &, bool bTrans)
Definition: pngread.cxx:1635
sal_uInt32 mnChunkType
Definition: pngread.cxx:101
SvStream & mrPNGStream
Definition: pngread.cxx:76
std::unique_ptr< sal_uInt8[]> mpInflateInBuf
Definition: pngread.cxx:93
std::unique_ptr< sal_uInt8[]> mpScanPrior
Definition: pngread.cxx:95
void SetIgnoreGammaChunk(bool bIgnore)
Definition: pngread.cxx:177
bool ImplPreparePass()
Definition: pngread.cxx:926
sal_uInt8 mnTransGreen
Definition: pngread.cxx:117
sal_Int32 mnAllocSizeScanlineAlpha
Definition: pngread.cxx:142
#define VIEWING_GAMMA
Definition: pngread.cxx:50
sal_uInt32 mnXAdd
Definition: pngread.cxx:111
sal_uInt8 * mpColorTable
Definition: pngread.cxx:99
sal_uInt32 ImplReadsal_uInt32()
Definition: pngread.cxx:1676
#define SAL_MAX_UINT32
sal_uInt8 mnTransBlue
Definition: pngread.cxx:118
std::unique_ptr< Bitmap > mpMaskBmp
Definition: pngread.cxx:85
long EndCompression()
bool eof() const
static bool IsFuzzing()
int nCount
const std::vector< vcl::PNGReader::ChunkData > & GetAllChunks()
Definition: pngread.cxx:321
sal_uInt32 mnYpos
Definition: pngread.cxx:108
sal_uInt32 mnBPP
Definition: pngread.cxx:106
if(nullptr==pCandidateA||nullptr==pCandidateB)
void Clear()
Definition: bitmapex.cxx:217
const std::vector< ChunkData > & GetChunks() const
Definition: pngread.cxx:1703
size_t GetBreak() const
AlphaScopedWriteAccess mxAlphaAcc
Definition: pngread.cxx:88
#define PNGCHUNK_IHDR
Definition: pngread.cxx:41
void ImplSetAlphaPixel(sal_uInt32 y, sal_uInt32 x, sal_uInt8 nPalIndex, sal_uInt8 nAlpha)
Definition: pngread.cxx:1650
SvStream & ReadUInt32(sal_uInt32 &rUInt32)
#define PNGCHUNK_PLTE
Definition: pngread.cxx:42
std::unique_ptr< sal_uInt8[]> mpTransTab
Definition: pngread.cxx:97
static const sal_uInt8 mpDefaultColorTable[256]
Definition: pngread.cxx:54
sal_Int32 mnAllocSizeScanline
Definition: pngread.cxx:141
#define SAL_MAX_INT32
bool IsEmpty() const
int i
bool HasPalette() const
void Erase(const Color &rColor)
Definition: bmpacc3.cxx:68
std::unique_ptr< AlphaMask > mpAlphaMask
Definition: pngread.cxx:87
void ImplGetGrayPalette(sal_uInt16)
Definition: pngread.cxx:651
PNGReaderImpl(SvStream &)
Definition: pngread.cxx:180
std::vector< sal_uInt8 >::iterator maDataIter
Definition: pngread.cxx:81
sal_uInt8 mnInterlaceType
Definition: pngread.cxx:123
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
void BeginCompression(int nCompressLevel=ZCODEC_DEFAULT_COMPRESSION, bool gzLib=false)
std::size_t mnStreamSize
Definition: pngread.cxx:100
void ImplDrawScanline(sal_uInt32 nXStart, sal_uInt32 nXAdd)
Definition: pngread.cxx:1133
const BitmapColor mcOpaqueColor
Definition: pngread.cxx:125
bool ImplReadTransparent()
Definition: pngread.cxx:693
std::unique_ptr< sal_uInt8[]> mpScanlineAlpha
Definition: pngread.cxx:148
sal_uInt8 mnCompressionType
Definition: pngread.cxx:121
std::vector< sal_uInt8 > aData
Definition: pngread.hxx:52
#define PNGCHUNK_bKGD
Definition: pngread.cxx:45
#define DISPLAY_GAMMA
Definition: pngread.cxx:51
std::vector< vcl::PNGReader::ChunkData >::iterator maChunkIter
Definition: pngread.cxx:80
sal_uInt8 mnFilterType
Definition: pngread.cxx:122
SvStreamEndian mnOrigStreamMode
Definition: pngread.cxx:77
BitmapScopedWriteAccess mxAcc
Definition: pngread.cxx:84
sal_uInt16 GetPaletteEntryCount() const
SvStream & ReadInt32(sal_Int32 &rInt32)
bool ReadNextChunk()
Definition: pngread.cxx:255
std::size_t ReadBytes(void *pData, std::size_t nSize)
#define PNGCHUNK_tRNS
Definition: pngread.cxx:48
BitmapEx GetBitmapEx()
Definition: pngread.cxx:328
SvStreamEndian GetEndian() const
sal_uInt32 mnScansize
Definition: pngread.cxx:107
#define ZCODEC_NO_COMPRESSION
#define PNGCHUNK_IDAT
Definition: pngread.cxx:43
#define SAL_WARN_IF(condition, area, stream)
unsigned char sal_uInt8
long ReadAsynchron(SvStream &rIStm, sal_uInt8 *pData, sal_uInt32 nSize)
#define PNGCHUNK_IEND
Definition: pngread.cxx:44
void SetEndian(SvStreamEndian SvStreamEndian)
void SetPrefSize(const Size &rPrefSize)
Definition: bitmapex.hxx:87
void SetIgnoreGammaChunk(bool bIgnoreGammaChunk)
Definition: pngread.cxx:1708
std::unique_ptr< PNGReaderImpl > mpImpl
Definition: pngread.hxx:35
sal_uInt64 Tell() const
BitmapWriteAccess * mpMaskAcc
Definition: pngread.cxx:89
#define PNGCHUNK_pHYs
Definition: pngread.cxx:47
bool good() const
std::unique_ptr< sal_uInt8[]> mpScanline
Definition: pngread.cxx:146
vcl::ScopedBitmapAccess< BitmapWriteAccess, AlphaMask,&AlphaMask::AcquireAlphaWriteAccess > AlphaScopedWriteAccess
const BitmapColor mcTranspColor
Definition: pngread.cxx:124
#define SAL_WARN(area, stream)
void CopyScanline(long nY, const BitmapReadAccess &rReadAcc)
Definition: bmpacc.cxx:363
SvStreamEndian
const BitmapColor & GetPaletteColor(sal_uInt16 nColor) const
sal_uInt8 mnPngDepth
Definition: pngread.cxx:119
sal_uInt32 mnYAdd
Definition: pngread.cxx:112
void ImplApplyFilter()
Definition: pngread.cxx:991
sal_Int32 mnChunkLen
Definition: pngread.cxx:102
void setWidth(long nWidth)
#define PNGCHUNK_gAMA
Definition: pngread.cxx:46
void SetPaletteEntryCount(sal_uInt16 nCount)
void SetPaletteColor(sal_uInt16 nColor, const BitmapColor &rBitmapColor)
void SetPixelIndex(long nY, long nX, sal_uInt8 cIndex)
sal_uInt8 ImplScaleColor()
Definition: pngread.cxx:845
PNGReader(SvStream &rStream)
Definition: pngread.cxx:1689
std::vector< vcl::PNGReader::ChunkData > maChunkSeq
Definition: pngread.cxx:79
BitmapEx Read()
Definition: pngread.cxx:1698
void setHeight(long nHeight)