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