xref: /AOO41X/main/vcl/source/gdi/pngread.cxx (revision 9f62ea84a806e17e6f2bbff75724a7257a0eb5d9)
1*9f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9f62ea84SAndrew Rist  * distributed with this work for additional information
6*9f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
9*9f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*9f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*9f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9f62ea84SAndrew Rist  * software distributed under the License is distributed on an
15*9f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9f62ea84SAndrew Rist  * specific language governing permissions and limitations
18*9f62ea84SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*9f62ea84SAndrew Rist  *************************************************************/
21*9f62ea84SAndrew Rist 
22*9f62ea84SAndrew 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 ),
200cdf0e10cSrcweir 	mbzCodecInUse	( sal_False ),
201cdf0e10cSrcweir     mbStatus( sal_True),
202cdf0e10cSrcweir     mbIDAT( sal_False ),
203cdf0e10cSrcweir 	mbGamma				( sal_False ),
204cdf0e10cSrcweir 	mbpHYs				( sal_False ),
205cdf0e10cSrcweir 	mbIgnoreGammaChunk	( sal_False )
206cdf0e10cSrcweir {
207cdf0e10cSrcweir     // prepare the PNG data stream
208cdf0e10cSrcweir     mnOrigStreamMode = mrPNGStream.GetNumberFormatInt();
209cdf0e10cSrcweir     mrPNGStream.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
210cdf0e10cSrcweir 
211cdf0e10cSrcweir     // prepare the chunk reader
212cdf0e10cSrcweir     maChunkSeq.reserve( 16 );
213cdf0e10cSrcweir     maChunkIter = maChunkSeq.begin();
214cdf0e10cSrcweir 
215cdf0e10cSrcweir     // estimate PNG file size (to allow sanity checks)
216cdf0e10cSrcweir     const sal_Size nStreamPos = mrPNGStream.Tell();
217cdf0e10cSrcweir     mrPNGStream.Seek( STREAM_SEEK_TO_END );
218cdf0e10cSrcweir     mnStreamSize = mrPNGStream.Tell();
219cdf0e10cSrcweir     mrPNGStream.Seek( nStreamPos );
220cdf0e10cSrcweir 
221cdf0e10cSrcweir     // check the PNG header magic
222cdf0e10cSrcweir     sal_uInt32 nDummy = 0;
223cdf0e10cSrcweir     mrPNGStream >> nDummy;
224cdf0e10cSrcweir     mbStatus = (nDummy == 0x89504e47);
225cdf0e10cSrcweir     mrPNGStream >> nDummy;
226cdf0e10cSrcweir     mbStatus &= (nDummy == 0x0d0a1a0a);
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     mnPreviewShift = 0;
229cdf0e10cSrcweir     mnPreviewMask = (1 << mnPreviewShift) - 1;
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir // ------------------------------------------------------------------------
233cdf0e10cSrcweir 
234cdf0e10cSrcweir PNGReaderImpl::~PNGReaderImpl()
235cdf0e10cSrcweir {
236cdf0e10cSrcweir     mrPNGStream.SetNumberFormatInt( mnOrigStreamMode );
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 	if ( mbzCodecInUse )
239cdf0e10cSrcweir 		mpZCodec->EndCompression();
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 	if( mpColorTable != mpDefaultColorTable )
242cdf0e10cSrcweir 		delete[] mpColorTable;
243cdf0e10cSrcweir 
244cdf0e10cSrcweir     delete mpBmp;
245cdf0e10cSrcweir     delete mpAlphaMask;
246cdf0e10cSrcweir     delete mpMaskBmp;
247cdf0e10cSrcweir     delete[] mpTransTab;
248cdf0e10cSrcweir     delete[] mpInflateInBuf;
249cdf0e10cSrcweir     delete[] mpScanPrior;
250cdf0e10cSrcweir     delete mpZCodec;
251cdf0e10cSrcweir }
252cdf0e10cSrcweir 
253cdf0e10cSrcweir // ------------------------------------------------------------------------
254cdf0e10cSrcweir 
255cdf0e10cSrcweir bool PNGReaderImpl::ReadNextChunk()
256cdf0e10cSrcweir {
257cdf0e10cSrcweir     if( maChunkIter == maChunkSeq.end() )
258cdf0e10cSrcweir     {
259cdf0e10cSrcweir         // get the next chunk from the stream
260cdf0e10cSrcweir 
261cdf0e10cSrcweir         // unless we are at the end of the PNG stream
262cdf0e10cSrcweir         if( mrPNGStream.IsEof() || (mrPNGStream.GetError() != ERRCODE_NONE) )
263cdf0e10cSrcweir             return false;
264cdf0e10cSrcweir         if( !maChunkSeq.empty() && (maChunkSeq.back().nType == PNGCHUNK_IEND) )
265cdf0e10cSrcweir             return false;
266cdf0e10cSrcweir 
267cdf0e10cSrcweir         PNGReader::ChunkData aDummyChunk;
268cdf0e10cSrcweir         maChunkIter = maChunkSeq.insert( maChunkSeq.end(), aDummyChunk );
269cdf0e10cSrcweir         PNGReader::ChunkData& rChunkData = *maChunkIter;
270cdf0e10cSrcweir 
271cdf0e10cSrcweir         // read the chunk header
272cdf0e10cSrcweir         mrPNGStream >> mnChunkLen >> mnChunkType;
273cdf0e10cSrcweir         rChunkData.nType = mnChunkType;
274cdf0e10cSrcweir 
275cdf0e10cSrcweir         // #128377#/#149343# sanity check for chunk length
276cdf0e10cSrcweir         if( mnChunkLen < 0 )
277cdf0e10cSrcweir             return false;
278cdf0e10cSrcweir         const sal_Size nStreamPos = mrPNGStream.Tell();
279cdf0e10cSrcweir         if( nStreamPos + mnChunkLen >= mnStreamSize )
280cdf0e10cSrcweir             return false;
281cdf0e10cSrcweir 
282cdf0e10cSrcweir         // calculate chunktype CRC (swap it back to original byte order)
283cdf0e10cSrcweir         sal_uInt32 nChunkType = mnChunkType;;
284cdf0e10cSrcweir         #if defined(__LITTLEENDIAN) || defined(OSL_LITENDIAN)
285cdf0e10cSrcweir         nChunkType = SWAPLONG( nChunkType );
286cdf0e10cSrcweir         #endif
287cdf0e10cSrcweir         sal_uInt32 nCRC32 = rtl_crc32( 0, &nChunkType, 4 );
288cdf0e10cSrcweir 
289cdf0e10cSrcweir         // read the chunk data and check the CRC
290cdf0e10cSrcweir         if( mnChunkLen && !mrPNGStream.IsEof() )
291cdf0e10cSrcweir         {
292cdf0e10cSrcweir             rChunkData.aData.resize( mnChunkLen );
293cdf0e10cSrcweir 
294cdf0e10cSrcweir             sal_Int32 nBytesRead = 0;
295cdf0e10cSrcweir             do {
296cdf0e10cSrcweir                 sal_uInt8* pPtr = &rChunkData.aData[ nBytesRead ];
297cdf0e10cSrcweir                 nBytesRead += mrPNGStream.Read( pPtr, mnChunkLen - nBytesRead );
298cdf0e10cSrcweir             } while ( ( nBytesRead < mnChunkLen ) && ( mrPNGStream.GetError() == ERRCODE_NONE ) );
299cdf0e10cSrcweir 
300cdf0e10cSrcweir             nCRC32 = rtl_crc32( nCRC32, &rChunkData.aData[ 0 ], mnChunkLen );
301cdf0e10cSrcweir             maDataIter = rChunkData.aData.begin();
302cdf0e10cSrcweir         }
303cdf0e10cSrcweir         sal_uInt32 nCheck;
304cdf0e10cSrcweir         mrPNGStream >> nCheck;
305cdf0e10cSrcweir         if( nCRC32 != nCheck )
306cdf0e10cSrcweir             return false;
307cdf0e10cSrcweir     }
308cdf0e10cSrcweir     else
309cdf0e10cSrcweir     {
310cdf0e10cSrcweir         // the next chunk was already read
311cdf0e10cSrcweir         mnChunkType = (*maChunkIter).nType;
312cdf0e10cSrcweir         mnChunkLen = (*maChunkIter).aData.size();
313cdf0e10cSrcweir         maDataIter = (*maChunkIter).aData.begin();
314cdf0e10cSrcweir     }
315cdf0e10cSrcweir 
316cdf0e10cSrcweir     ++maChunkIter;
317cdf0e10cSrcweir     if( mnChunkType == PNGCHUNK_IEND )
318cdf0e10cSrcweir         return false;
319cdf0e10cSrcweir     return true;
320cdf0e10cSrcweir }
321cdf0e10cSrcweir 
322cdf0e10cSrcweir // ------------------------------------------------------------------------
323cdf0e10cSrcweir 
324cdf0e10cSrcweir // read the remaining chunks from mrPNGStream
325cdf0e10cSrcweir void PNGReaderImpl::ReadRemainingChunks()
326cdf0e10cSrcweir {
327cdf0e10cSrcweir     while( ReadNextChunk() ) ;
328cdf0e10cSrcweir }
329cdf0e10cSrcweir 
330cdf0e10cSrcweir // ------------------------------------------------------------------------
331cdf0e10cSrcweir 
332cdf0e10cSrcweir // move position of mrPNGStream to the end of the file
333cdf0e10cSrcweir void PNGReaderImpl::SkipRemainingChunks()
334cdf0e10cSrcweir {
335cdf0e10cSrcweir     // nothing to skip if the last chunk was read
336cdf0e10cSrcweir     if( !maChunkSeq.empty() && (maChunkSeq.back().nType == PNGCHUNK_IEND) )
337cdf0e10cSrcweir         return;
338cdf0e10cSrcweir 
339cdf0e10cSrcweir     // read from the stream until the IEND chunk is found
340cdf0e10cSrcweir     const sal_Size nStreamPos = mrPNGStream.Tell();
341cdf0e10cSrcweir     while( !mrPNGStream.IsEof() && (mrPNGStream.GetError() == ERRCODE_NONE) )
342cdf0e10cSrcweir     {
343cdf0e10cSrcweir         mrPNGStream >> mnChunkLen >> mnChunkType;
344cdf0e10cSrcweir         if( mnChunkLen < 0 )
345cdf0e10cSrcweir             break;
346cdf0e10cSrcweir         if( nStreamPos + mnChunkLen >= mnStreamSize )
347cdf0e10cSrcweir             break;
348cdf0e10cSrcweir         mrPNGStream.SeekRel( mnChunkLen + 4 );  // skip data + CRC
349cdf0e10cSrcweir         if( mnChunkType == PNGCHUNK_IEND )
350cdf0e10cSrcweir             break;
351cdf0e10cSrcweir     }
352cdf0e10cSrcweir }
353cdf0e10cSrcweir 
354cdf0e10cSrcweir // ------------------------------------------------------------------------
355cdf0e10cSrcweir 
356cdf0e10cSrcweir const std::vector< vcl::PNGReader::ChunkData >& PNGReaderImpl::GetAllChunks()
357cdf0e10cSrcweir {
358cdf0e10cSrcweir     ReadRemainingChunks();
359cdf0e10cSrcweir     return maChunkSeq;
360cdf0e10cSrcweir }
361cdf0e10cSrcweir 
362cdf0e10cSrcweir // ------------------------------------------------------------------------
363cdf0e10cSrcweir 
364cdf0e10cSrcweir BitmapEx PNGReaderImpl::GetBitmapEx( const Size& rPreviewSizeHint )
365cdf0e10cSrcweir {
366cdf0e10cSrcweir     // reset to the first chunk
367cdf0e10cSrcweir     maChunkIter = maChunkSeq.begin();
368cdf0e10cSrcweir 
369cdf0e10cSrcweir     // parse the chunks
370cdf0e10cSrcweir     while( mbStatus && !mbIDAT && ReadNextChunk() )
371cdf0e10cSrcweir     {
372cdf0e10cSrcweir         switch( mnChunkType )
373cdf0e10cSrcweir 		{
374cdf0e10cSrcweir 			case PNGCHUNK_IHDR :
375cdf0e10cSrcweir 			{
376cdf0e10cSrcweir 				mbStatus = ImplReadHeader( rPreviewSizeHint );
377cdf0e10cSrcweir 			}
378cdf0e10cSrcweir 			break;
379cdf0e10cSrcweir 
380cdf0e10cSrcweir 			case PNGCHUNK_gAMA :								// the gamma chunk must precede
381cdf0e10cSrcweir 			{													// the 'IDAT' and also the 'PLTE'(if available )
382cdf0e10cSrcweir 				if ( !mbIgnoreGammaChunk && ( mbIDAT == sal_False ) )
383cdf0e10cSrcweir 					ImplGetGamma();
384cdf0e10cSrcweir 			}
385cdf0e10cSrcweir 			break;
386cdf0e10cSrcweir 
387cdf0e10cSrcweir 			case PNGCHUNK_PLTE :
388cdf0e10cSrcweir 			{
389cdf0e10cSrcweir 				if ( !mbPalette )
390cdf0e10cSrcweir 					mbStatus = ImplReadPalette();
391cdf0e10cSrcweir 			}
392cdf0e10cSrcweir 			break;
393cdf0e10cSrcweir 
394cdf0e10cSrcweir 			case PNGCHUNK_tRNS :
395cdf0e10cSrcweir 			{
396cdf0e10cSrcweir 				if ( !mbIDAT )									// the tRNS chunk must precede the IDAT
397cdf0e10cSrcweir 					mbStatus = ImplReadTransparent();
398cdf0e10cSrcweir 			}
399cdf0e10cSrcweir 			break;
400cdf0e10cSrcweir 
401cdf0e10cSrcweir 			case PNGCHUNK_bKGD :								// the background chunk must appear
402cdf0e10cSrcweir 			{
403cdf0e10cSrcweir 				if ( ( mbIDAT == sal_False ) && mbPalette ) 		// before the 'IDAT' and after the
404cdf0e10cSrcweir 					ImplGetBackground();						// PLTE(if available ) chunk.
405cdf0e10cSrcweir 			}
406cdf0e10cSrcweir 			break;
407cdf0e10cSrcweir 
408cdf0e10cSrcweir 			case PNGCHUNK_IDAT :
409cdf0e10cSrcweir 			{
410cdf0e10cSrcweir 				if ( !mpInflateInBuf )	// taking care that the header has properly been read
411cdf0e10cSrcweir 					mbStatus = sal_False;
412cdf0e10cSrcweir 				else if ( !mbIDAT )		// the gfx is finished, but there may be left a zlibCRC of about 4Bytes
413cdf0e10cSrcweir 					ImplReadIDAT();
414cdf0e10cSrcweir 			}
415cdf0e10cSrcweir 			break;
416cdf0e10cSrcweir 
417cdf0e10cSrcweir 			case PNGCHUNK_pHYs :
418cdf0e10cSrcweir 			{
419cdf0e10cSrcweir 				if ( !mbIDAT && mnChunkLen == 9 )
420cdf0e10cSrcweir 				{
421cdf0e10cSrcweir                     sal_uInt32 nXPixelPerMeter = ImplReadsal_uInt32();
422cdf0e10cSrcweir                     sal_uInt32 nYPixelPerMeter = ImplReadsal_uInt32();
423cdf0e10cSrcweir 
424cdf0e10cSrcweir                     sal_uInt8 nUnitSpecifier = *maDataIter++;
425cdf0e10cSrcweir                     if( (nUnitSpecifier == 1) && nXPixelPerMeter && nXPixelPerMeter )
426cdf0e10cSrcweir                     {
427cdf0e10cSrcweir                         mbpHYs = sal_True;
428cdf0e10cSrcweir 
429cdf0e10cSrcweir                         // convert into MAP_100TH_MM
430cdf0e10cSrcweir                         maPhysSize.Width()  = (sal_Int32)( (100000.0 * maOrigSize.Width()) / nXPixelPerMeter );
431cdf0e10cSrcweir                         maPhysSize.Height() = (sal_Int32)( (100000.0 * maOrigSize.Height()) / nYPixelPerMeter );
432cdf0e10cSrcweir                     }
433cdf0e10cSrcweir 				}
434cdf0e10cSrcweir 			}
435cdf0e10cSrcweir 			break;
436cdf0e10cSrcweir 
437cdf0e10cSrcweir             case PNGCHUNK_IEND:
438cdf0e10cSrcweir                 mbStatus = mbIDAT;  // there is a problem if the image is not complete yet
439cdf0e10cSrcweir             break;
440cdf0e10cSrcweir 		}
441cdf0e10cSrcweir 	}
442cdf0e10cSrcweir 
443cdf0e10cSrcweir     // release write access of the bitmaps
444cdf0e10cSrcweir 	if ( mpAcc )
445cdf0e10cSrcweir 		mpBmp->ReleaseAccess( mpAcc ), mpAcc = NULL;
446cdf0e10cSrcweir 
447cdf0e10cSrcweir 	if ( mpMaskAcc )
448cdf0e10cSrcweir 	{
449cdf0e10cSrcweir 		if ( mpAlphaMask )
450cdf0e10cSrcweir 			mpAlphaMask->ReleaseAccess( mpMaskAcc );
451cdf0e10cSrcweir 		else if ( mpMaskBmp )
452cdf0e10cSrcweir 			mpMaskBmp->ReleaseAccess( mpMaskAcc );
453cdf0e10cSrcweir 
454cdf0e10cSrcweir 		mpMaskAcc = NULL;
455cdf0e10cSrcweir 	}
456cdf0e10cSrcweir 
457cdf0e10cSrcweir     // return the resulting BitmapEx
458cdf0e10cSrcweir     BitmapEx aRet;
459cdf0e10cSrcweir 
460cdf0e10cSrcweir     if( !mbStatus || !mbIDAT )
461cdf0e10cSrcweir         aRet.Clear();
462cdf0e10cSrcweir     else
463cdf0e10cSrcweir 	{
464cdf0e10cSrcweir 		if ( mpAlphaMask )
465cdf0e10cSrcweir 			aRet = BitmapEx( *mpBmp, *mpAlphaMask );
466cdf0e10cSrcweir 		else if ( mpMaskBmp )
467cdf0e10cSrcweir 			aRet = BitmapEx( *mpBmp, *mpMaskBmp );
468cdf0e10cSrcweir 		else
469cdf0e10cSrcweir 			aRet = *mpBmp;
470cdf0e10cSrcweir 
471cdf0e10cSrcweir 		if ( mbpHYs && maPhysSize.Width() && maPhysSize.Height() )
472cdf0e10cSrcweir 		{
473cdf0e10cSrcweir 			aRet.SetPrefMapMode( MAP_100TH_MM );
474cdf0e10cSrcweir 			aRet.SetPrefSize( maPhysSize );
475cdf0e10cSrcweir 		}
476cdf0e10cSrcweir 
477cdf0e10cSrcweir #if 0
478cdf0e10cSrcweir         // TODO: make sure nobody depends on the stream being after the IEND chunks
479cdf0e10cSrcweir         // => let them do ReadChunks before
480cdf0e10cSrcweir         ReadRemainingChunks();
481cdf0e10cSrcweir #endif
482cdf0e10cSrcweir 	}
483cdf0e10cSrcweir 
484cdf0e10cSrcweir 	return aRet;
485cdf0e10cSrcweir }
486cdf0e10cSrcweir 
487cdf0e10cSrcweir // ------------------------------------------------------------------------
488cdf0e10cSrcweir 
489cdf0e10cSrcweir sal_Bool PNGReaderImpl::ImplReadHeader( const Size& rPreviewSizeHint )
490cdf0e10cSrcweir {
491cdf0e10cSrcweir     if( mnChunkLen < 13 )
492cdf0e10cSrcweir         return sal_False;
493cdf0e10cSrcweir 
494cdf0e10cSrcweir     maOrigSize.Width()  = ImplReadsal_uInt32();
495cdf0e10cSrcweir     maOrigSize.Height() = ImplReadsal_uInt32();
496cdf0e10cSrcweir 
497cdf0e10cSrcweir     if ( !maOrigSize.Width() || !maOrigSize.Height() )
498cdf0e10cSrcweir         return sal_False;
499cdf0e10cSrcweir 
500cdf0e10cSrcweir     mnPngDepth = *(maDataIter++);
501cdf0e10cSrcweir     mnColorType = *(maDataIter++);
502cdf0e10cSrcweir 
503cdf0e10cSrcweir     mnCompressionType = *(maDataIter++);
504cdf0e10cSrcweir     if( mnCompressionType != 0 )    // unknown compression type
505cdf0e10cSrcweir         return sal_False;
506cdf0e10cSrcweir 
507cdf0e10cSrcweir     mnFilterType = *(maDataIter++);
508cdf0e10cSrcweir     if( mnFilterType != 0 )         // unknown filter type
509cdf0e10cSrcweir         return sal_False;
510cdf0e10cSrcweir 
511cdf0e10cSrcweir     mnInterlaceType = *(maDataIter++);
512cdf0e10cSrcweir     switch ( mnInterlaceType ) // filter type valid ?
513cdf0e10cSrcweir 	{
514cdf0e10cSrcweir 		case 0 :  // progressive image
515cdf0e10cSrcweir 			mnPass = 7;
516cdf0e10cSrcweir 			break;
517cdf0e10cSrcweir 		case 1 :  // Adam7-interlaced image
518cdf0e10cSrcweir 			mnPass = 0;
519cdf0e10cSrcweir 			break;
520cdf0e10cSrcweir 		default:
521cdf0e10cSrcweir 			return sal_False;
522cdf0e10cSrcweir 	}
523cdf0e10cSrcweir 
524cdf0e10cSrcweir 	mbPalette = sal_True;
525cdf0e10cSrcweir 	mbIDAT = mbAlphaChannel = mbTransparent = sal_False;
526cdf0e10cSrcweir 	mbGrayScale = mbRGBTriple = sal_False;
527cdf0e10cSrcweir 	mnTargetDepth = mnPngDepth;
528cdf0e10cSrcweir 	sal_uInt64 nScansize64 = ( ( static_cast< sal_uInt64 >( maOrigSize.Width() ) * mnPngDepth ) + 7 ) >> 3;
529cdf0e10cSrcweir 
530cdf0e10cSrcweir 	// valid color types are 0,2,3,4 & 6
531cdf0e10cSrcweir 	switch ( mnColorType )
532cdf0e10cSrcweir 	{
533cdf0e10cSrcweir 		case 0 :	// each pixel is a grayscale
534cdf0e10cSrcweir 		{
535cdf0e10cSrcweir 			switch ( mnPngDepth )
536cdf0e10cSrcweir 			{
537cdf0e10cSrcweir 				case 2 : // 2bit target not available -> use four bits
538cdf0e10cSrcweir 					mnTargetDepth = 4;	// we have to expand the bitmap
539cdf0e10cSrcweir 					mbGrayScale = sal_True;
540cdf0e10cSrcweir 					break;
541cdf0e10cSrcweir 				case 16 :
542cdf0e10cSrcweir 					mnTargetDepth = 8;	// we have to reduce the bitmap
543cdf0e10cSrcweir 					// fall through
544cdf0e10cSrcweir 				case 1 :
545cdf0e10cSrcweir 				case 4 :
546cdf0e10cSrcweir 				case 8 :
547cdf0e10cSrcweir 					mbGrayScale = sal_True;
548cdf0e10cSrcweir 					break;
549cdf0e10cSrcweir 				default :
550cdf0e10cSrcweir 					return sal_False;
551cdf0e10cSrcweir 			}
552cdf0e10cSrcweir 		}
553cdf0e10cSrcweir 		break;
554cdf0e10cSrcweir 
555cdf0e10cSrcweir 		case 2 :	// each pixel is an RGB triple
556cdf0e10cSrcweir 		{
557cdf0e10cSrcweir 			mbRGBTriple = sal_True;
558cdf0e10cSrcweir 			nScansize64 *= 3;
559cdf0e10cSrcweir 			switch ( mnPngDepth )
560cdf0e10cSrcweir 			{
561cdf0e10cSrcweir 				case 16 :			// we have to reduce the bitmap
562cdf0e10cSrcweir 				case 8 :
563cdf0e10cSrcweir 					mnTargetDepth = 24;
564cdf0e10cSrcweir 					break;
565cdf0e10cSrcweir 				default :
566cdf0e10cSrcweir 					return sal_False;
567cdf0e10cSrcweir 			}
568cdf0e10cSrcweir 		}
569cdf0e10cSrcweir 		break;
570cdf0e10cSrcweir 
571cdf0e10cSrcweir 		case 3 :	// each pixel is a palette index
572cdf0e10cSrcweir 		{
573cdf0e10cSrcweir 			switch ( mnPngDepth )
574cdf0e10cSrcweir 			{
575cdf0e10cSrcweir 				case 2 :
576cdf0e10cSrcweir 					mnTargetDepth = 4;	// we have to expand the bitmap
577cdf0e10cSrcweir 					// fall through
578cdf0e10cSrcweir 				case 1 :
579cdf0e10cSrcweir 				case 4 :
580cdf0e10cSrcweir 				case 8 :
581cdf0e10cSrcweir 					mbPalette = sal_False;
582cdf0e10cSrcweir 					break;
583cdf0e10cSrcweir 				default :
584cdf0e10cSrcweir 					return sal_False;
585cdf0e10cSrcweir 			}
586cdf0e10cSrcweir 		}
587cdf0e10cSrcweir 		break;
588cdf0e10cSrcweir 
589cdf0e10cSrcweir 		case 4 :	// each pixel is a grayscale sample followed by an alpha sample
590cdf0e10cSrcweir 		{
591cdf0e10cSrcweir 			nScansize64 *= 2;
592cdf0e10cSrcweir 			mbAlphaChannel = sal_True;
593cdf0e10cSrcweir 			switch ( mnPngDepth )
594cdf0e10cSrcweir 			{
595cdf0e10cSrcweir 				case 16 :
596cdf0e10cSrcweir 					mnTargetDepth = 8;	// we have to reduce the bitmap
597cdf0e10cSrcweir 				case 8 :
598cdf0e10cSrcweir 					mbGrayScale = sal_True;
599cdf0e10cSrcweir 					break;
600cdf0e10cSrcweir 				default :
601cdf0e10cSrcweir 					return sal_False;
602cdf0e10cSrcweir 			}
603cdf0e10cSrcweir 		}
604cdf0e10cSrcweir 		break;
605cdf0e10cSrcweir 
606cdf0e10cSrcweir 		case 6 :	// each pixel is an RGB triple followed by an alpha sample
607cdf0e10cSrcweir 		{
608cdf0e10cSrcweir 			mbRGBTriple = sal_True;
609cdf0e10cSrcweir 			nScansize64 *= 4;
610cdf0e10cSrcweir 			mbAlphaChannel = sal_True;
611cdf0e10cSrcweir 			switch (mnPngDepth )
612cdf0e10cSrcweir 			{
613cdf0e10cSrcweir 				case 16 :			// we have to reduce the bitmap
614cdf0e10cSrcweir 				case 8 :
615cdf0e10cSrcweir 					mnTargetDepth = 24;
616cdf0e10cSrcweir 					break;
617cdf0e10cSrcweir 				default :
618cdf0e10cSrcweir 					return sal_False;
619cdf0e10cSrcweir 			}
620cdf0e10cSrcweir 		}
621cdf0e10cSrcweir 		break;
622cdf0e10cSrcweir 
623cdf0e10cSrcweir 		default :
624cdf0e10cSrcweir 			return sal_False;
625cdf0e10cSrcweir 	}
626cdf0e10cSrcweir 
627cdf0e10cSrcweir     mnBPP = static_cast< sal_uInt32 >( nScansize64 / maOrigSize.Width() );
628cdf0e10cSrcweir     if ( !mnBPP )
629cdf0e10cSrcweir         mnBPP = 1;
630cdf0e10cSrcweir 
631cdf0e10cSrcweir     nScansize64++;       // each scanline includes one filterbyte
632cdf0e10cSrcweir 
633cdf0e10cSrcweir 	if ( nScansize64 > SAL_MAX_UINT32 )
634cdf0e10cSrcweir 		return sal_False;
635cdf0e10cSrcweir 
636cdf0e10cSrcweir 	mnScansize = static_cast< sal_uInt32 >( nScansize64 );
637cdf0e10cSrcweir 
638cdf0e10cSrcweir     // TODO: switch between both scanlines instead of copying
639cdf0e10cSrcweir 	mpInflateInBuf = new (std::nothrow) sal_uInt8[ mnScansize ];
640cdf0e10cSrcweir     mpScanCurrent = mpInflateInBuf;
641cdf0e10cSrcweir 	mpScanPrior = new (std::nothrow) sal_uInt8[ mnScansize ];
642cdf0e10cSrcweir 
643cdf0e10cSrcweir 	if ( !mpInflateInBuf || !mpScanPrior )
644cdf0e10cSrcweir 		return sal_False;
645cdf0e10cSrcweir 
646cdf0e10cSrcweir     // calculate target size from original size and the preview hint
647cdf0e10cSrcweir     if( rPreviewSizeHint.Width() || rPreviewSizeHint.Height() )
648cdf0e10cSrcweir     {
649cdf0e10cSrcweir 		Size aPreviewSize( rPreviewSizeHint.Width(), rPreviewSizeHint.Height() );
650cdf0e10cSrcweir         maTargetSize = maOrigSize;
651cdf0e10cSrcweir 
652cdf0e10cSrcweir 		if( aPreviewSize.Width() == 0 ) {
653cdf0e10cSrcweir 			aPreviewSize.setWidth( ( maOrigSize.Width()*aPreviewSize.Height() )/maOrigSize.Height() );
654cdf0e10cSrcweir 			if( aPreviewSize.Width() <= 0 )
655cdf0e10cSrcweir 				aPreviewSize.setWidth( 1 );
656cdf0e10cSrcweir 		} else if( aPreviewSize.Height() == 0 ) {
657cdf0e10cSrcweir 			aPreviewSize.setHeight( ( maOrigSize.Height()*aPreviewSize.Width() )/maOrigSize.Width() );
658cdf0e10cSrcweir 			if( aPreviewSize.Height() <= 0 )
659cdf0e10cSrcweir 				aPreviewSize.setHeight( 1 );
660cdf0e10cSrcweir 		}
661cdf0e10cSrcweir 
662cdf0e10cSrcweir 		if( aPreviewSize.Width() < maOrigSize.Width() && aPreviewSize.Height() < maOrigSize.Height() ) {
663cdf0e10cSrcweir 			OSL_TRACE("preview size %dx%d", aPreviewSize.Width(), aPreviewSize.Height() );
664cdf0e10cSrcweir 
665cdf0e10cSrcweir 			for( int i = 1; i < 5; ++i )
666cdf0e10cSrcweir 				{
667cdf0e10cSrcweir 					if( (maTargetSize.Width() >> i) < aPreviewSize.Width() )
668cdf0e10cSrcweir 						break;
669cdf0e10cSrcweir 					if( (maTargetSize.Height() >> i) < aPreviewSize.Height() )
670cdf0e10cSrcweir 						break;
671cdf0e10cSrcweir 					mnPreviewShift = i;
672cdf0e10cSrcweir 				}
673cdf0e10cSrcweir 			mnPreviewMask = (1 << mnPreviewShift) - 1;
674cdf0e10cSrcweir 		}
675cdf0e10cSrcweir     }
676cdf0e10cSrcweir 
677cdf0e10cSrcweir     maTargetSize.Width()  = (maOrigSize.Width() + mnPreviewMask) >> mnPreviewShift;
678cdf0e10cSrcweir     maTargetSize.Height() = (maOrigSize.Height() + mnPreviewMask) >> mnPreviewShift;
679cdf0e10cSrcweir 
680cdf0e10cSrcweir     mpBmp = new Bitmap( maTargetSize, mnTargetDepth );
681cdf0e10cSrcweir     mpAcc = mpBmp->AcquireWriteAccess();
682cdf0e10cSrcweir     if( !mpAcc )
683cdf0e10cSrcweir         return sal_False;
684cdf0e10cSrcweir 
685cdf0e10cSrcweir     mpBmp->SetSourceSizePixel( maOrigSize );
686cdf0e10cSrcweir 
687cdf0e10cSrcweir     if ( mbAlphaChannel )
688cdf0e10cSrcweir     {
689cdf0e10cSrcweir         mpAlphaMask = new AlphaMask( maTargetSize );
690cdf0e10cSrcweir         mpAlphaMask->Erase( 128 );
691cdf0e10cSrcweir         mpMaskAcc = mpAlphaMask->AcquireWriteAccess();
692cdf0e10cSrcweir         if( !mpMaskAcc )
693cdf0e10cSrcweir             return sal_False;
694cdf0e10cSrcweir     }
695cdf0e10cSrcweir 
696cdf0e10cSrcweir 	if ( mbGrayScale )
697cdf0e10cSrcweir 		ImplGetGrayPalette( mnPngDepth );
698cdf0e10cSrcweir 
699cdf0e10cSrcweir     ImplPreparePass();
700cdf0e10cSrcweir 
701cdf0e10cSrcweir 	return sal_True;
702cdf0e10cSrcweir }
703cdf0e10cSrcweir 
704cdf0e10cSrcweir // ------------------------------------------------------------------------
705cdf0e10cSrcweir 
706cdf0e10cSrcweir void PNGReaderImpl::ImplGetGrayPalette( sal_uInt16 nBitDepth )
707cdf0e10cSrcweir {
708cdf0e10cSrcweir     if( nBitDepth > 8 )
709cdf0e10cSrcweir         nBitDepth = 8;
710cdf0e10cSrcweir 
711cdf0e10cSrcweir     sal_uInt16  nPaletteEntryCount = 1 << nBitDepth;
712cdf0e10cSrcweir 	sal_uInt32  nAdd = nBitDepth ? 256 / (nPaletteEntryCount - 1) : 0;
713cdf0e10cSrcweir 
714cdf0e10cSrcweir     // no bitdepth==2 available
715cdf0e10cSrcweir     // but bitdepth==4 with two unused bits is close enough
716cdf0e10cSrcweir     if( nBitDepth == 2 )
717cdf0e10cSrcweir         nPaletteEntryCount = 16;
718cdf0e10cSrcweir 
719cdf0e10cSrcweir 	mpAcc->SetPaletteEntryCount( nPaletteEntryCount );
720cdf0e10cSrcweir 	for ( sal_uInt32 i = 0, nStart = 0; nStart < 256; i++, nStart += nAdd )
721cdf0e10cSrcweir 		mpAcc->SetPaletteColor( (sal_uInt16)i, BitmapColor( mpColorTable[ nStart ],
722cdf0e10cSrcweir 			mpColorTable[ nStart ], mpColorTable[ nStart ] ) );
723cdf0e10cSrcweir }
724cdf0e10cSrcweir 
725cdf0e10cSrcweir // ------------------------------------------------------------------------
726cdf0e10cSrcweir 
727cdf0e10cSrcweir sal_Bool PNGReaderImpl::ImplReadPalette()
728cdf0e10cSrcweir {
729cdf0e10cSrcweir 	sal_uInt16 nCount = static_cast<sal_uInt16>( mnChunkLen / 3 );
730cdf0e10cSrcweir 
731cdf0e10cSrcweir 	if ( ( ( mnChunkLen % 3 ) == 0 ) && ( ( 0 < nCount ) && ( nCount <= 256 ) ) && mpAcc )
732cdf0e10cSrcweir 	{
733cdf0e10cSrcweir 		mbPalette = sal_True;
734cdf0e10cSrcweir 		mpAcc->SetPaletteEntryCount( (sal_uInt16) nCount );
735cdf0e10cSrcweir 
736cdf0e10cSrcweir 		for ( sal_uInt16 i = 0; i < nCount; i++ )
737cdf0e10cSrcweir 		{
738cdf0e10cSrcweir 			sal_uInt8 nRed =   mpColorTable[ *maDataIter++ ];
739cdf0e10cSrcweir 			sal_uInt8 nGreen = mpColorTable[ *maDataIter++ ];
740cdf0e10cSrcweir 			sal_uInt8 nBlue =  mpColorTable[ *maDataIter++ ];
741cdf0e10cSrcweir 			mpAcc->SetPaletteColor( i, Color( nRed, nGreen, nBlue ) );
742cdf0e10cSrcweir 		}
743cdf0e10cSrcweir 	}
744cdf0e10cSrcweir 	else
745cdf0e10cSrcweir 		mbStatus = sal_False;
746cdf0e10cSrcweir 
747cdf0e10cSrcweir 	return mbStatus;
748cdf0e10cSrcweir }
749cdf0e10cSrcweir 
750cdf0e10cSrcweir // ------------------------------------------------------------------------
751cdf0e10cSrcweir 
752cdf0e10cSrcweir sal_Bool PNGReaderImpl::ImplReadTransparent()
753cdf0e10cSrcweir {
754cdf0e10cSrcweir     bool bNeedAlpha = false;
755cdf0e10cSrcweir 
756cdf0e10cSrcweir 	if ( mpTransTab == NULL )
757cdf0e10cSrcweir 	{
758cdf0e10cSrcweir 		switch ( mnColorType )
759cdf0e10cSrcweir 		{
760cdf0e10cSrcweir 			case 0 :
761cdf0e10cSrcweir 			{
762cdf0e10cSrcweir 				if ( mnChunkLen == 2 )
763cdf0e10cSrcweir 				{
764cdf0e10cSrcweir 					mpTransTab = new sal_uInt8[ 256 ];
765cdf0e10cSrcweir 					rtl_fillMemory( mpTransTab, 256, 0xff );
766cdf0e10cSrcweir                     // color type 0 and 4 is always greyscale,
767cdf0e10cSrcweir                     // so the return value can be used as index
768cdf0e10cSrcweir 					sal_uInt8 nIndex = ImplScaleColor();
769cdf0e10cSrcweir 					mpTransTab[ nIndex ] = 0;
770cdf0e10cSrcweir 					mbTransparent = true;
771cdf0e10cSrcweir 				}
772cdf0e10cSrcweir 			}
773cdf0e10cSrcweir 			break;
774cdf0e10cSrcweir 
775cdf0e10cSrcweir 			case 2 :
776cdf0e10cSrcweir 			{
777cdf0e10cSrcweir 				if ( mnChunkLen == 6 )
778cdf0e10cSrcweir 				{
779cdf0e10cSrcweir 					mnTransRed = ImplScaleColor();
780cdf0e10cSrcweir 					mnTransGreen = ImplScaleColor();
781cdf0e10cSrcweir 					mnTransBlue = ImplScaleColor();
782cdf0e10cSrcweir 					mbTransparent = true;
783cdf0e10cSrcweir 				}
784cdf0e10cSrcweir 			}
785cdf0e10cSrcweir 			break;
786cdf0e10cSrcweir 
787cdf0e10cSrcweir 			case 3 :
788cdf0e10cSrcweir 			{
789cdf0e10cSrcweir 				if ( mnChunkLen <= 256 )
790cdf0e10cSrcweir 				{
791cdf0e10cSrcweir 					mpTransTab = new sal_uInt8 [ 256 ];
792cdf0e10cSrcweir 					rtl_fillMemory( mpTransTab, 256, 0xff );
793cdf0e10cSrcweir 					rtl_copyMemory( mpTransTab, &(*maDataIter), mnChunkLen );
794cdf0e10cSrcweir 					maDataIter += mnChunkLen;
795cdf0e10cSrcweir 					mbTransparent = true;
796cdf0e10cSrcweir 					// need alpha transparency if not on/off masking
797cdf0e10cSrcweir 					for( int i = 0; i < mnChunkLen; ++i )
798cdf0e10cSrcweir 					   bNeedAlpha |= (mpTransTab[i]!=0x00) && (mpTransTab[i]!=0xFF);
799cdf0e10cSrcweir 				}
800cdf0e10cSrcweir 			}
801cdf0e10cSrcweir 			break;
802cdf0e10cSrcweir 		}
803cdf0e10cSrcweir 	}
804cdf0e10cSrcweir 
805cdf0e10cSrcweir     if( mbTransparent && !mbAlphaChannel && !mpMaskBmp )
806cdf0e10cSrcweir     {
807cdf0e10cSrcweir         if( bNeedAlpha)
808cdf0e10cSrcweir         {
809cdf0e10cSrcweir             mpAlphaMask = new AlphaMask( maTargetSize );
810cdf0e10cSrcweir             mpMaskAcc = mpAlphaMask->AcquireWriteAccess();
811cdf0e10cSrcweir         }
812cdf0e10cSrcweir         else
813cdf0e10cSrcweir         {
814cdf0e10cSrcweir             mpMaskBmp = new Bitmap( maTargetSize, 1 );
815cdf0e10cSrcweir             mpMaskAcc = mpMaskBmp->AcquireWriteAccess();
816cdf0e10cSrcweir         }
817cdf0e10cSrcweir         mbTransparent = (mpMaskAcc != NULL);
818cdf0e10cSrcweir         if( !mbTransparent )
819cdf0e10cSrcweir             return sal_False;
820cdf0e10cSrcweir         mcOpaqueColor = BitmapColor( 0x00 );
821cdf0e10cSrcweir         mcTranspColor = BitmapColor( 0xFF );
822cdf0e10cSrcweir         mpMaskAcc->Erase( 0x00 );
823cdf0e10cSrcweir     }
824cdf0e10cSrcweir 
825cdf0e10cSrcweir     return sal_True;
826cdf0e10cSrcweir }
827cdf0e10cSrcweir 
828cdf0e10cSrcweir // ------------------------------------------------------------------------
829cdf0e10cSrcweir 
830cdf0e10cSrcweir void PNGReaderImpl::ImplGetGamma()
831cdf0e10cSrcweir {
832cdf0e10cSrcweir 	if( mnChunkLen < 4 )
833cdf0e10cSrcweir 	    return;
834cdf0e10cSrcweir 
835cdf0e10cSrcweir 	sal_uInt32	nGammaValue = ImplReadsal_uInt32();
836cdf0e10cSrcweir 	double		fGamma = ( ( VIEWING_GAMMA / DISPLAY_GAMMA ) * ( (double)nGammaValue / 100000 ) );
837cdf0e10cSrcweir 	double 		fInvGamma = ( fGamma <= 0.0 || fGamma > 10.0 ) ? 1.0 : ( 1.0 / fGamma );
838cdf0e10cSrcweir 
839cdf0e10cSrcweir 	if ( fInvGamma != 1.0 )
840cdf0e10cSrcweir 	{
841cdf0e10cSrcweir 		mbGamma = sal_True;
842cdf0e10cSrcweir 
843cdf0e10cSrcweir 		if ( mpColorTable == mpDefaultColorTable )
844cdf0e10cSrcweir 			mpColorTable = new sal_uInt8[ 256 ];
845cdf0e10cSrcweir 
846cdf0e10cSrcweir 		for ( sal_Int32 i = 0; i < 256; i++ )
847cdf0e10cSrcweir 			mpColorTable[ i ] = (sal_uInt8)(pow((double)i/255.0, fInvGamma) * 255.0 + 0.5);
848cdf0e10cSrcweir 
849cdf0e10cSrcweir 		if ( mbGrayScale )
850cdf0e10cSrcweir 			ImplGetGrayPalette( mnPngDepth );
851cdf0e10cSrcweir 	}
852cdf0e10cSrcweir }
853cdf0e10cSrcweir 
854cdf0e10cSrcweir // ------------------------------------------------------------------------
855cdf0e10cSrcweir 
856cdf0e10cSrcweir void PNGReaderImpl::ImplGetBackground()
857cdf0e10cSrcweir {
858cdf0e10cSrcweir 	switch ( mnColorType )
859cdf0e10cSrcweir 	{
860cdf0e10cSrcweir 		case 3 :
861cdf0e10cSrcweir 		{
862cdf0e10cSrcweir 			if ( mnChunkLen == 1 )
863cdf0e10cSrcweir 			{
864cdf0e10cSrcweir 				sal_uInt16 nCol = *maDataIter++;
865cdf0e10cSrcweir 				if ( nCol < mpAcc->GetPaletteEntryCount() )
866cdf0e10cSrcweir 				{
867cdf0e10cSrcweir 					mpAcc->Erase( mpAcc->GetPaletteColor( (sal_uInt8)nCol ) );
868cdf0e10cSrcweir 					break;
869cdf0e10cSrcweir 				}
870cdf0e10cSrcweir 			}
871cdf0e10cSrcweir 		}
872cdf0e10cSrcweir 		break;
873cdf0e10cSrcweir 
874cdf0e10cSrcweir 		case 0 :
875cdf0e10cSrcweir 		case 4 :
876cdf0e10cSrcweir 		{
877cdf0e10cSrcweir 			if ( mnChunkLen == 2 )
878cdf0e10cSrcweir 			{
879cdf0e10cSrcweir                 // the color type 0 and 4 is always greyscale,
880cdf0e10cSrcweir                 // so the return value can be used as index
881cdf0e10cSrcweir 				sal_uInt8 nIndex = ImplScaleColor();
882cdf0e10cSrcweir 				mpAcc->Erase( mpAcc->GetPaletteColor( nIndex ) );
883cdf0e10cSrcweir 			}
884cdf0e10cSrcweir 		}
885cdf0e10cSrcweir 		break;
886cdf0e10cSrcweir 
887cdf0e10cSrcweir 		case 2 :
888cdf0e10cSrcweir 		case 6 :
889cdf0e10cSrcweir 		{
890cdf0e10cSrcweir 			if ( mnChunkLen == 6 )
891cdf0e10cSrcweir 			{
892cdf0e10cSrcweir 				sal_uInt8 nRed = ImplScaleColor();
893cdf0e10cSrcweir 				sal_uInt8 nGreen = ImplScaleColor();
894cdf0e10cSrcweir 				sal_uInt8 nBlue = ImplScaleColor();
895cdf0e10cSrcweir 				mpAcc->Erase( Color( nRed, nGreen, nBlue ) );
896cdf0e10cSrcweir 			}
897cdf0e10cSrcweir 		}
898cdf0e10cSrcweir 		break;
899cdf0e10cSrcweir 	}
900cdf0e10cSrcweir }
901cdf0e10cSrcweir 
902cdf0e10cSrcweir // ------------------------------------------------------------------------
903cdf0e10cSrcweir 
904cdf0e10cSrcweir // for color type 0 and 4 (greyscale) the return value is always index to the color
905cdf0e10cSrcweir //                2 and 6 (RGB)       the return value is always the 8 bit color component
906cdf0e10cSrcweir sal_uInt8 PNGReaderImpl::ImplScaleColor()
907cdf0e10cSrcweir {
908cdf0e10cSrcweir     sal_uInt32 nMask = ( ( 1 << mnPngDepth ) - 1 );
909cdf0e10cSrcweir 	sal_uInt16 nCol = ( *maDataIter++ << 8 );
910cdf0e10cSrcweir 
911cdf0e10cSrcweir 	nCol += *maDataIter++ & (sal_uInt16)nMask;
912cdf0e10cSrcweir 
913cdf0e10cSrcweir 	if ( mnPngDepth > 8 )	// convert 16bit graphics to 8
914cdf0e10cSrcweir 		nCol >>= 8;
915cdf0e10cSrcweir 
916cdf0e10cSrcweir 	return (sal_uInt8) nCol;
917cdf0e10cSrcweir }
918cdf0e10cSrcweir 
919cdf0e10cSrcweir // ------------------------------------------------------------------------
920cdf0e10cSrcweir // ImplReadIDAT reads as much image data as needed
921cdf0e10cSrcweir 
922cdf0e10cSrcweir void PNGReaderImpl::ImplReadIDAT()
923cdf0e10cSrcweir {
924cdf0e10cSrcweir 	if( mnChunkLen > 0 )
925cdf0e10cSrcweir 	{
926cdf0e10cSrcweir 		if ( mbzCodecInUse == sal_False )
927cdf0e10cSrcweir 		{
928cdf0e10cSrcweir 			mbzCodecInUse = sal_True;
929cdf0e10cSrcweir 			mpZCodec->BeginCompression( ZCODEC_PNG_DEFAULT );
930cdf0e10cSrcweir 		}
931cdf0e10cSrcweir 		mpZCodec->SetBreak( mnChunkLen );
932cdf0e10cSrcweir 		SvMemoryStream aIStrm( &(*maDataIter), mnChunkLen, STREAM_READ );
933cdf0e10cSrcweir 
934cdf0e10cSrcweir 		while ( ( mpZCodec->GetBreak() ) )
935cdf0e10cSrcweir 		{
936cdf0e10cSrcweir             // get bytes needed to fill the current scanline
937cdf0e10cSrcweir             sal_Int32 nToRead = mnScansize - (mpScanCurrent - mpInflateInBuf);
938cdf0e10cSrcweir             sal_Int32 nRead = mpZCodec->ReadAsynchron( aIStrm, mpScanCurrent, nToRead );
939cdf0e10cSrcweir             if ( nRead < 0 )
940cdf0e10cSrcweir 			{
941cdf0e10cSrcweir 				mbStatus = sal_False;
942cdf0e10cSrcweir 				break;
943cdf0e10cSrcweir 			}
944cdf0e10cSrcweir 			if ( nRead < nToRead )
945cdf0e10cSrcweir 			{
946cdf0e10cSrcweir 				mpScanCurrent += nRead; // more ZStream data in the next IDAT chunk
947cdf0e10cSrcweir 				break;
948cdf0e10cSrcweir 			}
949cdf0e10cSrcweir 			else  // this scanline is Finished
950cdf0e10cSrcweir 			{
951cdf0e10cSrcweir 				mpScanCurrent = mpInflateInBuf;
952cdf0e10cSrcweir                 ImplApplyFilter();
953cdf0e10cSrcweir 
954cdf0e10cSrcweir                 ImplDrawScanline( mnXStart, mnXAdd );
955cdf0e10cSrcweir                 mnYpos += mnYAdd;
956cdf0e10cSrcweir 			}
957cdf0e10cSrcweir 
958cdf0e10cSrcweir 			if ( mnYpos >= (sal_uInt32)maOrigSize.Height() )
959cdf0e10cSrcweir 			{
960cdf0e10cSrcweir                 if( (mnPass < 7) && mnInterlaceType )
961cdf0e10cSrcweir                     if( ImplPreparePass() )
962cdf0e10cSrcweir                         continue;
963cdf0e10cSrcweir                 mbIDAT = true;
964cdf0e10cSrcweir                 break;
965cdf0e10cSrcweir 			}
966cdf0e10cSrcweir 		}
967cdf0e10cSrcweir 	}
968cdf0e10cSrcweir 
969cdf0e10cSrcweir 	if( mbIDAT )
970cdf0e10cSrcweir 	{
971cdf0e10cSrcweir 		mpZCodec->EndCompression();
972cdf0e10cSrcweir 		mbzCodecInUse = sal_False;
973cdf0e10cSrcweir 	}
974cdf0e10cSrcweir }
975cdf0e10cSrcweir 
976cdf0e10cSrcweir // ---------------------------------------------------------------------------------------------------
977cdf0e10cSrcweir 
978cdf0e10cSrcweir bool PNGReaderImpl::ImplPreparePass()
979cdf0e10cSrcweir {
980cdf0e10cSrcweir     struct InterlaceParams{ int mnXStart, mnYStart, mnXAdd, mnYAdd; };
981cdf0e10cSrcweir     static const InterlaceParams aInterlaceParams[8] =
982cdf0e10cSrcweir     {
983cdf0e10cSrcweir         // non-interlaced
984cdf0e10cSrcweir         { 0, 0, 1, 1 },
985cdf0e10cSrcweir         // Adam7-interlaced
986cdf0e10cSrcweir         { 0, 0, 8, 8 },    // pass 1
987cdf0e10cSrcweir         { 4, 0, 8, 8 },    // pass 2
988cdf0e10cSrcweir         { 0, 4, 4, 8 },    // pass 3
989cdf0e10cSrcweir         { 2, 0, 4, 4 },    // pass 4
990cdf0e10cSrcweir         { 0, 2, 2, 4 },    // pass 5
991cdf0e10cSrcweir         { 1, 0, 2, 2 },    // pass 6
992cdf0e10cSrcweir         { 0, 1, 1, 2 }     // pass 7
993cdf0e10cSrcweir     };
994cdf0e10cSrcweir 
995cdf0e10cSrcweir     const InterlaceParams* pParam = &aInterlaceParams[ 0 ];
996cdf0e10cSrcweir     if( mnInterlaceType )
997cdf0e10cSrcweir     {
998cdf0e10cSrcweir         while( ++mnPass <= 7 )
999cdf0e10cSrcweir         {
1000cdf0e10cSrcweir             pParam = &aInterlaceParams[ mnPass ];
1001cdf0e10cSrcweir 
1002cdf0e10cSrcweir             // skip this pass if the original image is too small for it
1003cdf0e10cSrcweir             if( (pParam->mnXStart < maOrigSize.Width())
1004cdf0e10cSrcweir             &&  (pParam->mnYStart < maOrigSize.Height()) )
1005cdf0e10cSrcweir                 break;
1006cdf0e10cSrcweir         }
1007cdf0e10cSrcweir         if( mnPass > 7 )
1008cdf0e10cSrcweir             return false;
1009cdf0e10cSrcweir 
1010cdf0e10cSrcweir         // skip the last passes if possible (for scaled down target images)
1011cdf0e10cSrcweir         if( mnPreviewMask & (pParam->mnXStart | pParam->mnYStart) )
1012cdf0e10cSrcweir             return false;
1013cdf0e10cSrcweir     }
1014cdf0e10cSrcweir 
1015cdf0e10cSrcweir     mnYpos      = pParam->mnYStart;
1016cdf0e10cSrcweir     mnXStart    = pParam->mnXStart;
1017cdf0e10cSrcweir     mnXAdd      = pParam->mnXAdd;
1018cdf0e10cSrcweir     mnYAdd      = pParam->mnYAdd;
1019cdf0e10cSrcweir 
1020cdf0e10cSrcweir     // in Interlace mode the size of scanline is not constant
1021cdf0e10cSrcweir     // so first we calculate the number of entrys
1022cdf0e10cSrcweir     long nScanWidth = (maOrigSize.Width() - mnXStart + mnXAdd - 1) / mnXAdd;
1023cdf0e10cSrcweir     mnScansize = nScanWidth;
1024cdf0e10cSrcweir 
1025cdf0e10cSrcweir     if( mbRGBTriple )
1026cdf0e10cSrcweir         mnScansize = 3 * nScanWidth;
1027cdf0e10cSrcweir 
1028cdf0e10cSrcweir     if( mbAlphaChannel )
1029cdf0e10cSrcweir         mnScansize += nScanWidth;
1030cdf0e10cSrcweir 
1031cdf0e10cSrcweir     // convert to width in bytes
1032cdf0e10cSrcweir     mnScansize = ( mnScansize*mnPngDepth + 7 ) >> 3;
1033cdf0e10cSrcweir 
1034cdf0e10cSrcweir     ++mnScansize; // scan size also needs room for the filtertype byte
1035cdf0e10cSrcweir     rtl_zeroMemory( mpScanPrior, mnScansize );
1036cdf0e10cSrcweir 
1037cdf0e10cSrcweir     return true;
1038cdf0e10cSrcweir }
1039cdf0e10cSrcweir 
1040cdf0e10cSrcweir // ----------------------------------------------------------------------------
1041cdf0e10cSrcweir // ImplApplyFilter writes the complete Scanline (nY)
1042cdf0e10cSrcweir // in interlace mode the parameter nXStart and nXAdd are non-zero
1043cdf0e10cSrcweir 
1044cdf0e10cSrcweir void PNGReaderImpl::ImplApplyFilter()
1045cdf0e10cSrcweir {
1046cdf0e10cSrcweir 	OSL_ASSERT( mnScansize >= mnBPP + 1 );
1047cdf0e10cSrcweir     const sal_uInt8* const pScanEnd = mpInflateInBuf + mnScansize;
1048cdf0e10cSrcweir 
1049cdf0e10cSrcweir     sal_uInt8 nFilterType = *mpInflateInBuf; // the filter type may change each scanline
1050cdf0e10cSrcweir     switch ( nFilterType )
1051cdf0e10cSrcweir     {
1052cdf0e10cSrcweir         default: // unknown Scanline Filter Type
1053cdf0e10cSrcweir         case 0: // Filter Type "None"
1054cdf0e10cSrcweir             // we let the pixels pass and display the data unfiltered
1055cdf0e10cSrcweir             break;
1056cdf0e10cSrcweir 
1057cdf0e10cSrcweir         case 1: // Scanline Filter Type "Sub"
1058cdf0e10cSrcweir         {
1059cdf0e10cSrcweir             sal_uInt8* p1 = mpInflateInBuf + 1;
1060cdf0e10cSrcweir             const sal_uInt8* p2 = p1;
1061cdf0e10cSrcweir             p1 += mnBPP;
1062cdf0e10cSrcweir 
1063cdf0e10cSrcweir             // use left pixels
1064cdf0e10cSrcweir             do
1065cdf0e10cSrcweir                 *p1 = static_cast<sal_uInt8>( *p1 + *(p2++) );
1066cdf0e10cSrcweir             while( ++p1 < pScanEnd );
1067cdf0e10cSrcweir         }
1068cdf0e10cSrcweir         break;
1069cdf0e10cSrcweir 
1070cdf0e10cSrcweir         case 2: // Scanline Filter Type "Up"
1071cdf0e10cSrcweir         {
1072cdf0e10cSrcweir             sal_uInt8* p1 = mpInflateInBuf + 1;
1073cdf0e10cSrcweir             const sal_uInt8* p2 = mpScanPrior + 1;
1074cdf0e10cSrcweir 
1075cdf0e10cSrcweir             // use pixels from prior line
1076cdf0e10cSrcweir 			while( p1 < pScanEnd )
1077cdf0e10cSrcweir 			{
1078cdf0e10cSrcweir                 *p1 = static_cast<sal_uInt8>( *p1 + *(p2++) );
1079cdf0e10cSrcweir 				++p1;
1080cdf0e10cSrcweir 			}
1081cdf0e10cSrcweir         }
1082cdf0e10cSrcweir         break;
1083cdf0e10cSrcweir 
1084cdf0e10cSrcweir         case 3: // Scanline Filter Type "Average"
1085cdf0e10cSrcweir         {
1086cdf0e10cSrcweir             sal_uInt8* p1 = mpInflateInBuf + 1;
1087cdf0e10cSrcweir             const sal_uInt8* p2 = mpScanPrior + 1;
1088cdf0e10cSrcweir             const sal_uInt8* p3 = p1;
1089cdf0e10cSrcweir 
1090cdf0e10cSrcweir             // use one pixel from prior line
1091cdf0e10cSrcweir             for( int n = mnBPP; --n >= 0; ++p1, ++p2)
1092cdf0e10cSrcweir                 *p1 = static_cast<sal_uInt8>( *p1 + (*p2 >> 1) );
1093cdf0e10cSrcweir 
1094cdf0e10cSrcweir             // predict by averaging the left and prior line pixels
1095cdf0e10cSrcweir 			while( p1 < pScanEnd )
1096cdf0e10cSrcweir 			{
1097cdf0e10cSrcweir                 *p1 = static_cast<sal_uInt8>( *p1 + ((*(p2++) + *(p3++)) >> 1) );
1098cdf0e10cSrcweir 				++p1;
1099cdf0e10cSrcweir 			}
1100cdf0e10cSrcweir         }
1101cdf0e10cSrcweir         break;
1102cdf0e10cSrcweir 
1103cdf0e10cSrcweir         case 4: // Scanline Filter Type "PaethPredictor"
1104cdf0e10cSrcweir         {
1105cdf0e10cSrcweir             sal_uInt8* p1 = mpInflateInBuf + 1;
1106cdf0e10cSrcweir             const sal_uInt8* p2 = mpScanPrior + 1;
1107cdf0e10cSrcweir             const sal_uInt8* p3 = p1;
1108cdf0e10cSrcweir             const sal_uInt8* p4 = p2;
1109cdf0e10cSrcweir 
1110cdf0e10cSrcweir             // use one pixel from prior line
1111cdf0e10cSrcweir             for( int n = mnBPP; --n >= 0; ++p1)
1112cdf0e10cSrcweir                 *p1 = static_cast<sal_uInt8>( *p1 + *(p2++) );
1113cdf0e10cSrcweir 
1114cdf0e10cSrcweir             // predict by using the left and the prior line pixels
1115cdf0e10cSrcweir             while( p1 < pScanEnd )
1116cdf0e10cSrcweir             {
1117cdf0e10cSrcweir                 int na = *(p2++);
1118cdf0e10cSrcweir                 int nb = *(p3++);
1119cdf0e10cSrcweir                 int nc = *(p4++);
1120cdf0e10cSrcweir 
1121cdf0e10cSrcweir                 int npa = nb - (int)nc;
1122cdf0e10cSrcweir                 int npb = na - (int)nc;
1123cdf0e10cSrcweir                 int npc = npa + npb;
1124cdf0e10cSrcweir 
1125cdf0e10cSrcweir                 if( npa < 0 )
1126cdf0e10cSrcweir                     npa =-npa;
1127cdf0e10cSrcweir                 if( npb < 0 )
1128cdf0e10cSrcweir                     npb =-npb;
1129cdf0e10cSrcweir                 if( npc < 0 )
1130cdf0e10cSrcweir                     npc =-npc;
1131cdf0e10cSrcweir 
1132cdf0e10cSrcweir                 if( npa > npb )
1133cdf0e10cSrcweir                     na = nb, npa = npb;
1134cdf0e10cSrcweir                 if( npa > npc )
1135cdf0e10cSrcweir                     na = nc;
1136cdf0e10cSrcweir 
1137cdf0e10cSrcweir                 *p1 = static_cast<sal_uInt8>( *p1 + na );
1138cdf0e10cSrcweir 				++p1;
1139cdf0e10cSrcweir 			}
1140cdf0e10cSrcweir         }
1141cdf0e10cSrcweir         break;
1142cdf0e10cSrcweir     }
1143cdf0e10cSrcweir 
1144cdf0e10cSrcweir     rtl_copyMemory( mpScanPrior, mpInflateInBuf, mnScansize );
1145cdf0e10cSrcweir }
1146cdf0e10cSrcweir 
1147cdf0e10cSrcweir // ---------------------------------------------------------------------------------------------------
1148cdf0e10cSrcweir // ImplDrawScanlines draws the complete Scanline (nY) into the target bitmap
1149cdf0e10cSrcweir // In interlace mode the parameter nXStart and nXAdd append to the currently used pass
1150cdf0e10cSrcweir 
1151cdf0e10cSrcweir void PNGReaderImpl::ImplDrawScanline( sal_uInt32 nXStart, sal_uInt32 nXAdd )
1152cdf0e10cSrcweir {
1153cdf0e10cSrcweir     // optimization for downscaling
1154cdf0e10cSrcweir     if( mnYpos & mnPreviewMask )
1155cdf0e10cSrcweir         return;
1156cdf0e10cSrcweir     if( nXStart & mnPreviewMask )
1157cdf0e10cSrcweir         return;
1158cdf0e10cSrcweir 
1159cdf0e10cSrcweir     // convert nY to pixel units in the target image
1160cdf0e10cSrcweir     // => TODO; also do this for nX here instead of in the ImplSet*Pixel() methods
1161cdf0e10cSrcweir     const sal_uInt32 nY = mnYpos >> mnPreviewShift;
1162cdf0e10cSrcweir 
1163cdf0e10cSrcweir     const sal_uInt8* pTmp = mpInflateInBuf + 1;
1164cdf0e10cSrcweir 	if ( mpAcc->HasPalette() ) // alphachannel is not allowed by pictures including palette entries
1165cdf0e10cSrcweir 	{
1166cdf0e10cSrcweir 		switch ( mpAcc->GetBitCount() )
1167cdf0e10cSrcweir 		{
1168cdf0e10cSrcweir 			case 1 :
1169cdf0e10cSrcweir 			{
1170cdf0e10cSrcweir 				if ( mbTransparent )
1171cdf0e10cSrcweir 				{
1172cdf0e10cSrcweir                     for ( sal_Int32 nX = nXStart, nShift = 0; nX < maOrigSize.Width(); nX += nXAdd )
1173cdf0e10cSrcweir 					{
1174cdf0e10cSrcweir                         sal_uInt8 nCol;
1175cdf0e10cSrcweir                         nShift = (nShift - 1) & 7;
1176cdf0e10cSrcweir 						if ( nShift == 0 )
1177cdf0e10cSrcweir 							nCol = *(pTmp++);
1178cdf0e10cSrcweir 						else
1179cdf0e10cSrcweir 							nCol = static_cast<sal_uInt8>( *pTmp >> nShift );
1180cdf0e10cSrcweir                         nCol &= 1;
1181cdf0e10cSrcweir 
1182cdf0e10cSrcweir 						ImplSetAlphaPixel( nY, nX, nCol, mpTransTab[ nCol ] );
1183cdf0e10cSrcweir 					}
1184cdf0e10cSrcweir 				}
1185cdf0e10cSrcweir 				else
1186cdf0e10cSrcweir 				{   // BMP_FORMAT_1BIT_MSB_PAL
1187cdf0e10cSrcweir 					for ( sal_Int32 nX = nXStart, nShift = 0; nX < maOrigSize.Width(); nX += nXAdd )
1188cdf0e10cSrcweir 					{
1189cdf0e10cSrcweir                         nShift = (nShift - 1) & 7;
1190cdf0e10cSrcweir 
1191cdf0e10cSrcweir                         sal_uInt8 nCol;
1192cdf0e10cSrcweir 						if ( nShift == 0 )
1193cdf0e10cSrcweir 							nCol = *(pTmp++);
1194cdf0e10cSrcweir 						else
1195cdf0e10cSrcweir 							nCol = static_cast<sal_uInt8>( *pTmp >> nShift );
1196cdf0e10cSrcweir                         nCol &= 1;
1197cdf0e10cSrcweir 
1198cdf0e10cSrcweir                         ImplSetPixel( nY, nX, nCol );
1199cdf0e10cSrcweir 					}
1200cdf0e10cSrcweir 				}
1201cdf0e10cSrcweir 			}
1202cdf0e10cSrcweir 			break;
1203cdf0e10cSrcweir 
1204cdf0e10cSrcweir 			case 4 :
1205cdf0e10cSrcweir 			{
1206cdf0e10cSrcweir 				if ( mbTransparent )
1207cdf0e10cSrcweir 				{
1208cdf0e10cSrcweir 					if ( mnPngDepth == 4 )	// check if source has a two bit pixel format
1209cdf0e10cSrcweir 					{
1210cdf0e10cSrcweir 						for ( sal_Int32 nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, ++nXIndex )
1211cdf0e10cSrcweir 						{
1212cdf0e10cSrcweir 							if( nXIndex & 1 )
1213cdf0e10cSrcweir 							{
1214cdf0e10cSrcweir 								ImplSetAlphaPixel( nY, nX, *pTmp & 0x0f, mpTransTab[ *pTmp & 0x0f ] );
1215cdf0e10cSrcweir 								pTmp++;
1216cdf0e10cSrcweir 							}
1217cdf0e10cSrcweir 							else
1218cdf0e10cSrcweir 							{
1219cdf0e10cSrcweir 								ImplSetAlphaPixel( nY, nX, ( *pTmp >> 4 ) & 0x0f, mpTransTab[ *pTmp >> 4 ] );
1220cdf0e10cSrcweir 							}
1221cdf0e10cSrcweir 						}
1222cdf0e10cSrcweir 					}
1223cdf0e10cSrcweir 					else // if ( mnPngDepth == 2 )
1224cdf0e10cSrcweir 					{
1225cdf0e10cSrcweir 						for ( sal_Int32 nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, nXIndex++ )
1226cdf0e10cSrcweir 						{
1227cdf0e10cSrcweir                             sal_uInt8 nCol;
1228cdf0e10cSrcweir 							switch( nXIndex & 3 )
1229cdf0e10cSrcweir 							{
1230cdf0e10cSrcweir 								case 0 :
1231cdf0e10cSrcweir 									nCol = *pTmp >> 6;
1232cdf0e10cSrcweir 								break;
1233cdf0e10cSrcweir 
1234cdf0e10cSrcweir 								case 1 :
1235cdf0e10cSrcweir 									nCol = ( *pTmp >> 4 ) & 0x03 ;
1236cdf0e10cSrcweir 								break;
1237cdf0e10cSrcweir 
1238cdf0e10cSrcweir 								case 2 :
1239cdf0e10cSrcweir 									nCol = ( *pTmp >> 2 ) & 0x03;
1240cdf0e10cSrcweir 								break;
1241cdf0e10cSrcweir 
1242cdf0e10cSrcweir 								case 3 :
1243cdf0e10cSrcweir 									nCol = ( *pTmp++ ) & 0x03;
1244cdf0e10cSrcweir 								break;
1245cdf0e10cSrcweir 
1246cdf0e10cSrcweir                                 default:    // get rid of nCol uninitialized warning
1247cdf0e10cSrcweir                                     nCol = 0;
1248cdf0e10cSrcweir                                     break;
1249cdf0e10cSrcweir 							}
1250cdf0e10cSrcweir 
1251cdf0e10cSrcweir 							ImplSetAlphaPixel( nY, nX, nCol, mpTransTab[ nCol ] );
1252cdf0e10cSrcweir 						}
1253cdf0e10cSrcweir 					}
1254cdf0e10cSrcweir 				}
1255cdf0e10cSrcweir 				else
1256cdf0e10cSrcweir 				{
1257cdf0e10cSrcweir 					if ( mnPngDepth == 4 )	// maybe the source is a two bitmap graphic
1258cdf0e10cSrcweir 					{   // BMP_FORMAT_4BIT_LSN_PAL
1259cdf0e10cSrcweir 						for ( sal_Int32 nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, nXIndex++ )
1260cdf0e10cSrcweir 						{
1261cdf0e10cSrcweir 							if( nXIndex & 1 )
1262cdf0e10cSrcweir 								ImplSetPixel( nY, nX, *pTmp++ & 0x0f );
1263cdf0e10cSrcweir 							else
1264cdf0e10cSrcweir 								ImplSetPixel( nY, nX, ( *pTmp >> 4 ) & 0x0f );
1265cdf0e10cSrcweir 						}
1266cdf0e10cSrcweir 					}
1267cdf0e10cSrcweir 					else // if ( mnPngDepth == 2 )
1268cdf0e10cSrcweir 					{
1269cdf0e10cSrcweir 						for ( sal_Int32 nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, nXIndex++ )
1270cdf0e10cSrcweir 						{
1271cdf0e10cSrcweir 							switch( nXIndex & 3 )
1272cdf0e10cSrcweir 							{
1273cdf0e10cSrcweir 								case 0 :
1274cdf0e10cSrcweir 									ImplSetPixel( nY, nX, *pTmp >> 6 );
1275cdf0e10cSrcweir 								break;
1276cdf0e10cSrcweir 
1277cdf0e10cSrcweir 								case 1 :
1278cdf0e10cSrcweir 									ImplSetPixel( nY, nX, ( *pTmp >> 4 ) & 0x03 );
1279cdf0e10cSrcweir 								break;
1280cdf0e10cSrcweir 
1281cdf0e10cSrcweir 								case 2 :
1282cdf0e10cSrcweir 									ImplSetPixel( nY, nX, ( *pTmp >> 2 ) & 0x03 );
1283cdf0e10cSrcweir 								break;
1284cdf0e10cSrcweir 
1285cdf0e10cSrcweir 								case 3 :
1286cdf0e10cSrcweir 									ImplSetPixel( nY, nX, *pTmp++ & 0x03 );
1287cdf0e10cSrcweir 								break;
1288cdf0e10cSrcweir 							}
1289cdf0e10cSrcweir 						}
1290cdf0e10cSrcweir 					}
1291cdf0e10cSrcweir 				}
1292cdf0e10cSrcweir 			}
1293cdf0e10cSrcweir 			break;
1294cdf0e10cSrcweir 
1295cdf0e10cSrcweir 			case 8 :
1296cdf0e10cSrcweir 			{
1297cdf0e10cSrcweir 				if ( mbAlphaChannel )
1298cdf0e10cSrcweir 				{
1299cdf0e10cSrcweir 					if ( mnPngDepth == 8 )	// maybe the source is a 16 bit grayscale
1300cdf0e10cSrcweir 					{
1301cdf0e10cSrcweir 						for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 2 )
1302cdf0e10cSrcweir 							ImplSetAlphaPixel( nY, nX, pTmp[ 0 ], pTmp[ 1 ] );
1303cdf0e10cSrcweir 					}
1304cdf0e10cSrcweir 					else
1305cdf0e10cSrcweir 					{
1306cdf0e10cSrcweir 						for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 4 )
1307cdf0e10cSrcweir 							ImplSetAlphaPixel( nY, nX, pTmp[ 0 ], pTmp[ 2 ] );
1308cdf0e10cSrcweir 					}
1309cdf0e10cSrcweir 				}
1310cdf0e10cSrcweir 				else if ( mbTransparent )
1311cdf0e10cSrcweir 				{
1312cdf0e10cSrcweir 					if ( mnPngDepth == 8 )	// maybe the source is a 16 bit grayscale
1313cdf0e10cSrcweir 					{
1314cdf0e10cSrcweir 						for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp++ )
1315cdf0e10cSrcweir 							ImplSetAlphaPixel( nY, nX, *pTmp, mpTransTab[ *pTmp ] );
1316cdf0e10cSrcweir 					}
1317cdf0e10cSrcweir 					else
1318cdf0e10cSrcweir 					{
1319cdf0e10cSrcweir 						for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 2 )
1320cdf0e10cSrcweir 							ImplSetAlphaPixel( nY, nX, *pTmp, mpTransTab[ *pTmp ] );
1321cdf0e10cSrcweir 					}
1322cdf0e10cSrcweir 				}
1323cdf0e10cSrcweir 				else // neither alpha nor transparency
1324cdf0e10cSrcweir 				{
1325cdf0e10cSrcweir 					if ( mnPngDepth == 8 )	// maybe the source is a 16 bit grayscale
1326cdf0e10cSrcweir 					{
1327cdf0e10cSrcweir                         if( nXAdd == 1 && mnPreviewShift == 0 )  // copy raw line data if possible
1328cdf0e10cSrcweir                         {
1329cdf0e10cSrcweir                             int nLineBytes = maOrigSize.Width();
1330cdf0e10cSrcweir                             mpAcc->CopyScanline( nY, pTmp, BMP_FORMAT_8BIT_PAL, nLineBytes );
1331cdf0e10cSrcweir                             pTmp += nLineBytes;
1332cdf0e10cSrcweir                         }
1333cdf0e10cSrcweir                         else
1334cdf0e10cSrcweir                         {
1335cdf0e10cSrcweir                             for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd )
1336cdf0e10cSrcweir                                 ImplSetPixel( nY, nX, *pTmp++ );
1337cdf0e10cSrcweir                         }
1338cdf0e10cSrcweir 					}
1339cdf0e10cSrcweir 					else
1340cdf0e10cSrcweir 					{
1341cdf0e10cSrcweir 						for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 2 )
1342cdf0e10cSrcweir 							ImplSetPixel( nY, nX, *pTmp );
1343cdf0e10cSrcweir 					}
1344cdf0e10cSrcweir 				}
1345cdf0e10cSrcweir 			}
1346cdf0e10cSrcweir 			break;
1347cdf0e10cSrcweir 
1348cdf0e10cSrcweir 			default :
1349cdf0e10cSrcweir 				mbStatus = sal_False;
1350cdf0e10cSrcweir 			break;
1351cdf0e10cSrcweir 		}
1352cdf0e10cSrcweir 	}
1353cdf0e10cSrcweir 	else // no palette => truecolor
1354cdf0e10cSrcweir 	{
1355cdf0e10cSrcweir 		if( mbAlphaChannel ) // has RGB + alpha
1356cdf0e10cSrcweir 		{   // BMP_FORMAT_32BIT_TC_RGBA
1357cdf0e10cSrcweir 			if ( mnPngDepth == 8 )  // maybe the source has 16 bit per sample
1358cdf0e10cSrcweir 			{
1359cdf0e10cSrcweir                 if ( mpColorTable != mpDefaultColorTable )
1360cdf0e10cSrcweir                 {
1361cdf0e10cSrcweir                     for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 4 )
1362cdf0e10cSrcweir                        ImplSetAlphaPixel( nY, nX, BitmapColor( mpColorTable[ pTmp[ 0 ] ],
1363cdf0e10cSrcweir                                                                mpColorTable[ pTmp[ 1 ] ],
1364cdf0e10cSrcweir                                                                mpColorTable[ pTmp[ 2 ] ] ), pTmp[ 3 ] );
1365cdf0e10cSrcweir                 }
1366cdf0e10cSrcweir                 else
1367cdf0e10cSrcweir                 {
1368cdf0e10cSrcweir //                  if ( nXAdd == 1 && mnPreviewShift == 0 ) // copy raw line data if possible
1369cdf0e10cSrcweir //                  {
1370cdf0e10cSrcweir //                      int nLineBytes = 4 * maOrigSize.Width();
1371cdf0e10cSrcweir //                      mpAcc->CopyScanline( nY, pTmp, BMP_FORMAT_32BIT_TC_RGBA, nLineBytes );
1372cdf0e10cSrcweir //                      pTmp += nLineBytes;
1373cdf0e10cSrcweir //                  }
1374cdf0e10cSrcweir //                  else
1375cdf0e10cSrcweir                     {
1376cdf0e10cSrcweir                         for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 4 )
1377cdf0e10cSrcweir                             ImplSetAlphaPixel( nY, nX, BitmapColor( pTmp[0], pTmp[1], pTmp[2] ), pTmp[3] );
1378cdf0e10cSrcweir                     }
1379cdf0e10cSrcweir                 }
1380cdf0e10cSrcweir 			}
1381cdf0e10cSrcweir 			else
1382cdf0e10cSrcweir 			{   // BMP_FORMAT_64BIT_TC_RGBA
1383cdf0e10cSrcweir 				for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 8 )
1384cdf0e10cSrcweir 					ImplSetAlphaPixel( nY, nX, BitmapColor( mpColorTable[ pTmp[ 0 ] ],
1385cdf0e10cSrcweir 														mpColorTable[ pTmp[ 2 ] ],
1386cdf0e10cSrcweir 														mpColorTable[ pTmp[ 4 ] ] ), pTmp[6] );
1387cdf0e10cSrcweir 			}
1388cdf0e10cSrcweir 		}
1389cdf0e10cSrcweir 		else if( mbTransparent ) // has RGB + transparency
1390cdf0e10cSrcweir 		{   // BMP_FORMAT_24BIT_TC_RGB
1391cdf0e10cSrcweir 			if ( mnPngDepth == 8 )  // maybe the source has 16 bit per sample
1392cdf0e10cSrcweir 			{
1393cdf0e10cSrcweir 				for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 3 )
1394cdf0e10cSrcweir 				{
1395cdf0e10cSrcweir 					sal_uInt8 nRed = pTmp[ 0 ];
1396cdf0e10cSrcweir 					sal_uInt8 nGreen = pTmp[ 1 ];
1397cdf0e10cSrcweir 					sal_uInt8 nBlue = pTmp[ 2 ];
1398cdf0e10cSrcweir 					sal_Bool bTransparent = ( ( nRed == mnTransRed )
1399cdf0e10cSrcweir 							 			&& ( nGreen == mnTransGreen )
1400cdf0e10cSrcweir 										&& ( nBlue == mnTransBlue ) );
1401cdf0e10cSrcweir 
1402cdf0e10cSrcweir 					ImplSetTranspPixel( nY, nX, BitmapColor( mpColorTable[ nRed ],
1403cdf0e10cSrcweir 														mpColorTable[ nGreen ],
1404cdf0e10cSrcweir 														mpColorTable[ nBlue ] ), bTransparent );
1405cdf0e10cSrcweir 				}
1406cdf0e10cSrcweir 			}
1407cdf0e10cSrcweir 			else
1408cdf0e10cSrcweir 			{   // BMP_FORMAT_48BIT_TC_RGB
1409cdf0e10cSrcweir 				for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 6 )
1410cdf0e10cSrcweir 				{
1411cdf0e10cSrcweir 					sal_uInt8 nRed = pTmp[ 0 ];
1412cdf0e10cSrcweir 					sal_uInt8 nGreen = pTmp[ 2 ];
1413cdf0e10cSrcweir 					sal_uInt8 nBlue = pTmp[ 4 ];
1414cdf0e10cSrcweir 					sal_Bool bTransparent = ( ( nRed == mnTransRed )
1415cdf0e10cSrcweir 										&& ( nGreen == mnTransGreen )
1416cdf0e10cSrcweir 										&& ( nBlue == mnTransBlue ) );
1417cdf0e10cSrcweir 
1418cdf0e10cSrcweir 					ImplSetTranspPixel( nY, nX, BitmapColor( mpColorTable[ nRed ],
1419cdf0e10cSrcweir 														mpColorTable[ nGreen ],
1420cdf0e10cSrcweir 														mpColorTable[ nBlue ] ), bTransparent );
1421cdf0e10cSrcweir 				}
1422cdf0e10cSrcweir 			}
1423cdf0e10cSrcweir 		}
1424cdf0e10cSrcweir 		else  // has RGB but neither alpha nor transparency
1425cdf0e10cSrcweir 		{   // BMP_FORMAT_24BIT_TC_RGB
1426cdf0e10cSrcweir 			if ( mnPngDepth == 8 )   // maybe the source has 16 bit per sample
1427cdf0e10cSrcweir 			{
1428cdf0e10cSrcweir                 if ( mpColorTable != mpDefaultColorTable )
1429cdf0e10cSrcweir                 {
1430cdf0e10cSrcweir                     for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 3 )
1431cdf0e10cSrcweir                         ImplSetPixel( nY, nX, BitmapColor( mpColorTable[ pTmp[ 0 ] ],
1432cdf0e10cSrcweir                                                             mpColorTable[ pTmp[ 1 ] ],
1433cdf0e10cSrcweir                                                             mpColorTable[ pTmp[ 2 ] ] ) );
1434cdf0e10cSrcweir                 }
1435cdf0e10cSrcweir                 else
1436cdf0e10cSrcweir                 {
1437cdf0e10cSrcweir                     if( nXAdd == 1 && mnPreviewShift == 0 ) // copy raw line data if possible
1438cdf0e10cSrcweir                     {
1439cdf0e10cSrcweir                         int nLineBytes = maOrigSize.Width() * 3;
1440cdf0e10cSrcweir                         mpAcc->CopyScanline( nY, pTmp, BMP_FORMAT_24BIT_TC_RGB, nLineBytes );
1441cdf0e10cSrcweir                         pTmp += nLineBytes;
1442cdf0e10cSrcweir                     }
1443cdf0e10cSrcweir                     else
1444cdf0e10cSrcweir                     {
1445cdf0e10cSrcweir                         for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 3 )
1446cdf0e10cSrcweir                             ImplSetPixel( nY, nX, BitmapColor( pTmp[0], pTmp[1], pTmp[2] ) );
1447cdf0e10cSrcweir                     }
1448cdf0e10cSrcweir                 }
1449cdf0e10cSrcweir 			}
1450cdf0e10cSrcweir 			else
1451cdf0e10cSrcweir 			{   // BMP_FORMAT_48BIT_TC_RGB
1452cdf0e10cSrcweir 				for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 6 )
1453cdf0e10cSrcweir 					ImplSetPixel( nY, nX, BitmapColor( mpColorTable[ pTmp[ 0 ] ],
1454cdf0e10cSrcweir 														mpColorTable[ pTmp[ 2 ] ],
1455cdf0e10cSrcweir 														mpColorTable[ pTmp[ 4 ] ] ) );
1456cdf0e10cSrcweir 			}
1457cdf0e10cSrcweir 		}
1458cdf0e10cSrcweir 	}
1459cdf0e10cSrcweir }
1460cdf0e10cSrcweir 
1461cdf0e10cSrcweir // ------------------------------------------------------------------------
1462cdf0e10cSrcweir 
1463cdf0e10cSrcweir void PNGReaderImpl::ImplSetPixel( sal_uInt32 nY, sal_uInt32 nX, const BitmapColor& rBitmapColor )
1464cdf0e10cSrcweir {
1465cdf0e10cSrcweir     // TODO: get preview mode checks out of inner loop
1466cdf0e10cSrcweir     if( nX & mnPreviewMask )
1467cdf0e10cSrcweir         return;
1468cdf0e10cSrcweir     nX >>= mnPreviewShift;
1469cdf0e10cSrcweir 
1470cdf0e10cSrcweir     mpAcc->SetPixel( nY, nX, rBitmapColor );
1471cdf0e10cSrcweir }
1472cdf0e10cSrcweir 
1473cdf0e10cSrcweir // ------------------------------------------------------------------------
1474cdf0e10cSrcweir 
1475cdf0e10cSrcweir void PNGReaderImpl::ImplSetPixel( sal_uInt32 nY, sal_uInt32 nX, sal_uInt8 nPalIndex )
1476cdf0e10cSrcweir {
1477cdf0e10cSrcweir     // TODO: get preview mode checks out of inner loop
1478cdf0e10cSrcweir     if( nX & mnPreviewMask )
1479cdf0e10cSrcweir         return;
1480cdf0e10cSrcweir     nX >>= mnPreviewShift;
1481cdf0e10cSrcweir 
1482cdf0e10cSrcweir     mpAcc->SetPixel( nY, nX, nPalIndex );
1483cdf0e10cSrcweir }
1484cdf0e10cSrcweir 
1485cdf0e10cSrcweir // ------------------------------------------------------------------------
1486cdf0e10cSrcweir 
1487cdf0e10cSrcweir void PNGReaderImpl::ImplSetTranspPixel( sal_uInt32 nY, sal_uInt32 nX, const BitmapColor& rBitmapColor, sal_Bool bTrans )
1488cdf0e10cSrcweir {
1489cdf0e10cSrcweir     // TODO: get preview mode checks out of inner loop
1490cdf0e10cSrcweir     if( nX & mnPreviewMask )
1491cdf0e10cSrcweir         return;
1492cdf0e10cSrcweir     nX >>= mnPreviewShift;
1493cdf0e10cSrcweir 
1494cdf0e10cSrcweir     mpAcc->SetPixel( nY, nX, rBitmapColor );
1495cdf0e10cSrcweir 
1496cdf0e10cSrcweir     if ( bTrans )
1497cdf0e10cSrcweir         mpMaskAcc->SetPixel( nY, nX, mcTranspColor );
1498cdf0e10cSrcweir     else
1499cdf0e10cSrcweir         mpMaskAcc->SetPixel( nY, nX, mcOpaqueColor );
1500cdf0e10cSrcweir }
1501cdf0e10cSrcweir 
1502cdf0e10cSrcweir // ------------------------------------------------------------------------
1503cdf0e10cSrcweir 
1504cdf0e10cSrcweir void PNGReaderImpl::ImplSetAlphaPixel( sal_uInt32 nY, sal_uInt32 nX,
1505cdf0e10cSrcweir     sal_uInt8 nPalIndex, sal_uInt8 nAlpha )
1506cdf0e10cSrcweir {
1507cdf0e10cSrcweir     // TODO: get preview mode checks out of inner loop
1508cdf0e10cSrcweir     if( nX & mnPreviewMask )
1509cdf0e10cSrcweir         return;
1510cdf0e10cSrcweir     nX >>= mnPreviewShift;
1511cdf0e10cSrcweir 
1512cdf0e10cSrcweir     mpAcc->SetPixel( nY, nX, nPalIndex );
1513cdf0e10cSrcweir     mpMaskAcc->SetPixel( nY, nX, ~nAlpha );
1514cdf0e10cSrcweir }
1515cdf0e10cSrcweir 
1516cdf0e10cSrcweir // ------------------------------------------------------------------------
1517cdf0e10cSrcweir 
1518cdf0e10cSrcweir void PNGReaderImpl::ImplSetAlphaPixel( sal_uInt32 nY, sal_uInt32 nX,
1519cdf0e10cSrcweir     const BitmapColor& rBitmapColor, sal_uInt8 nAlpha )
1520cdf0e10cSrcweir {
1521cdf0e10cSrcweir     // TODO: get preview mode checks out of inner loop
1522cdf0e10cSrcweir     if( nX & mnPreviewMask )
1523cdf0e10cSrcweir         return;
1524cdf0e10cSrcweir     nX >>= mnPreviewShift;
1525cdf0e10cSrcweir 
1526cdf0e10cSrcweir     mpAcc->SetPixel( nY, nX, rBitmapColor );
1527cdf0e10cSrcweir     mpMaskAcc->SetPixel( nY, nX, ~nAlpha );
1528cdf0e10cSrcweir }
1529cdf0e10cSrcweir 
1530cdf0e10cSrcweir // ------------------------------------------------------------------------
1531cdf0e10cSrcweir 
1532cdf0e10cSrcweir sal_uInt32 PNGReaderImpl::ImplReadsal_uInt32()
1533cdf0e10cSrcweir {
1534cdf0e10cSrcweir 	sal_uInt32 nRet;
1535cdf0e10cSrcweir 	nRet = *maDataIter++;
1536cdf0e10cSrcweir 	nRet <<= 8;
1537cdf0e10cSrcweir 	nRet |= *maDataIter++;
1538cdf0e10cSrcweir 	nRet <<= 8;
1539cdf0e10cSrcweir 	nRet |= *maDataIter++;
1540cdf0e10cSrcweir 	nRet <<= 8;
1541cdf0e10cSrcweir 	nRet |= *maDataIter++;
1542cdf0e10cSrcweir 	return nRet;
1543cdf0e10cSrcweir }
1544cdf0e10cSrcweir 
1545cdf0e10cSrcweir // ------------------------------------------------------------------------
1546cdf0e10cSrcweir 
1547cdf0e10cSrcweir // -------------
1548cdf0e10cSrcweir // - PNGReader -
1549cdf0e10cSrcweir // -------------
1550cdf0e10cSrcweir 
1551cdf0e10cSrcweir PNGReader::PNGReader( SvStream& rIStm ) :
1552cdf0e10cSrcweir 	mpImpl( new ::vcl::PNGReaderImpl( rIStm ) )
1553cdf0e10cSrcweir {
1554cdf0e10cSrcweir }
1555cdf0e10cSrcweir 
1556cdf0e10cSrcweir // ------------------------------------------------------------------------
1557cdf0e10cSrcweir 
1558cdf0e10cSrcweir PNGReader::~PNGReader()
1559cdf0e10cSrcweir {
1560cdf0e10cSrcweir 	delete mpImpl;
1561cdf0e10cSrcweir }
1562cdf0e10cSrcweir 
1563cdf0e10cSrcweir // ------------------------------------------------------------------------
1564cdf0e10cSrcweir 
1565cdf0e10cSrcweir BitmapEx PNGReader::Read( const Size& i_rPreviewSizeHint )
1566cdf0e10cSrcweir {
1567cdf0e10cSrcweir     return mpImpl->GetBitmapEx( i_rPreviewSizeHint );
1568cdf0e10cSrcweir }
1569cdf0e10cSrcweir 
1570cdf0e10cSrcweir // ------------------------------------------------------------------------
1571cdf0e10cSrcweir 
1572cdf0e10cSrcweir const std::vector< vcl::PNGReader::ChunkData >& PNGReader::GetChunks() const
1573cdf0e10cSrcweir {
1574cdf0e10cSrcweir 	return mpImpl->GetAllChunks();
1575cdf0e10cSrcweir }
1576cdf0e10cSrcweir 
1577cdf0e10cSrcweir // ------------------------------------------------------------------------
1578cdf0e10cSrcweir 
1579cdf0e10cSrcweir void PNGReader::SetIgnoreGammaChunk( sal_Bool b )
1580cdf0e10cSrcweir {
1581cdf0e10cSrcweir 	mpImpl->SetIgnoreGammaChunk( b );
1582cdf0e10cSrcweir }
1583cdf0e10cSrcweir 
1584cdf0e10cSrcweir 
1585cdf0e10cSrcweir } // namespace vcl
1586