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
15488b53a7cSArmin Le Grand #ifdef DBG_UTIL
15588b53a7cSArmin Le Grand // do some checks in debug mode
15688b53a7cSArmin Le Grand sal_uInt32 mnAllocSizeScanline;
15788b53a7cSArmin Le Grand sal_uInt32 mnAllocSizeScanlineAlpha;
15888b53a7cSArmin Le Grand #endif
15988b53a7cSArmin Le Grand // the temporary Scanline (and alpha) for direct scanline copy to Bitmap
16088b53a7cSArmin Le Grand sal_uInt8* mpScanline;
16188b53a7cSArmin Le Grand sal_uInt8* mpScanlineAlpha;
16288b53a7cSArmin Le Grand
163cdf0e10cSrcweir bool ReadNextChunk();
164cdf0e10cSrcweir void ReadRemainingChunks();
165cdf0e10cSrcweir void SkipRemainingChunks();
166cdf0e10cSrcweir
167cdf0e10cSrcweir void ImplSetPixel( sal_uInt32 y, sal_uInt32 x, const BitmapColor & );
168cdf0e10cSrcweir void ImplSetPixel( sal_uInt32 y, sal_uInt32 x, sal_uInt8 nPalIndex );
169cdf0e10cSrcweir void ImplSetTranspPixel( sal_uInt32 y, sal_uInt32 x, const BitmapColor &, sal_Bool bTrans );
170cdf0e10cSrcweir void ImplSetAlphaPixel( sal_uInt32 y, sal_uInt32 x, sal_uInt8 nPalIndex, sal_uInt8 nAlpha );
171cdf0e10cSrcweir void ImplSetAlphaPixel( sal_uInt32 y, sal_uInt32 x, const BitmapColor&, sal_uInt8 nAlpha );
172cdf0e10cSrcweir void ImplReadIDAT();
173cdf0e10cSrcweir bool ImplPreparePass();
174cdf0e10cSrcweir void ImplApplyFilter();
175cdf0e10cSrcweir void ImplDrawScanline( sal_uInt32 nXStart, sal_uInt32 nXAdd );
176cdf0e10cSrcweir sal_Bool ImplReadTransparent();
177cdf0e10cSrcweir void ImplGetGamma();
178cdf0e10cSrcweir void ImplGetBackground();
179cdf0e10cSrcweir sal_uInt8 ImplScaleColor();
180cdf0e10cSrcweir sal_Bool ImplReadHeader( const Size& rPreviewSizeHint );
181cdf0e10cSrcweir sal_Bool ImplReadPalette();
182cdf0e10cSrcweir void ImplGetGrayPalette( sal_uInt16 );
183cdf0e10cSrcweir sal_uInt32 ImplReadsal_uInt32();
184cdf0e10cSrcweir
185cdf0e10cSrcweir public:
186cdf0e10cSrcweir
187cdf0e10cSrcweir PNGReaderImpl( SvStream& );
188cdf0e10cSrcweir ~PNGReaderImpl();
189cdf0e10cSrcweir
190cdf0e10cSrcweir BitmapEx GetBitmapEx( const Size& rPreviewSizeHint );
191cdf0e10cSrcweir const std::vector< PNGReader::ChunkData >& GetAllChunks();
SetIgnoreGammaChunk(sal_Bool bIgnore)192cdf0e10cSrcweir void SetIgnoreGammaChunk( sal_Bool bIgnore ){ mbIgnoreGammaChunk = bIgnore; };
193cdf0e10cSrcweir };
194cdf0e10cSrcweir
195cdf0e10cSrcweir // ------------------------------------------------------------------------------
196cdf0e10cSrcweir
PNGReaderImpl(SvStream & rPNGStream)197cdf0e10cSrcweir PNGReaderImpl::PNGReaderImpl( SvStream& rPNGStream )
198cdf0e10cSrcweir : mrPNGStream( rPNGStream ),
199cdf0e10cSrcweir mpBmp ( NULL ),
200cdf0e10cSrcweir mpAcc ( NULL ),
201cdf0e10cSrcweir mpMaskBmp ( NULL ),
202cdf0e10cSrcweir mpAlphaMask ( NULL ),
203cdf0e10cSrcweir mpMaskAcc ( NULL ),
204cdf0e10cSrcweir mpZCodec ( new ZCodec( DEFAULT_IN_BUFSIZE, DEFAULT_OUT_BUFSIZE, MAX_MEM_USAGE ) ),
205cdf0e10cSrcweir mpInflateInBuf ( NULL ),
206cdf0e10cSrcweir mpScanPrior ( NULL ),
207cdf0e10cSrcweir mpTransTab ( NULL ),
208cdf0e10cSrcweir mpColorTable ( (sal_uInt8*) mpDefaultColorTable ),
2093447fe24SHerbert Dürr mnColorType( 0xFF ),
2103447fe24SHerbert Dürr mbPalette( false ),
211cdf0e10cSrcweir mbzCodecInUse ( sal_False ),
212cdf0e10cSrcweir mbStatus( sal_True),
213cdf0e10cSrcweir mbIDAT( sal_False ),
214cdf0e10cSrcweir mbGamma ( sal_False ),
215cdf0e10cSrcweir mbpHYs ( sal_False ),
21688b53a7cSArmin Le Grand mbIgnoreGammaChunk ( sal_False ),
21788b53a7cSArmin Le Grand #ifdef DBG_UTIL
21888b53a7cSArmin Le Grand mnAllocSizeScanline(0),
21988b53a7cSArmin Le Grand mnAllocSizeScanlineAlpha(0),
22088b53a7cSArmin Le Grand #endif
22188b53a7cSArmin Le Grand mpScanline(0),
22288b53a7cSArmin Le Grand mpScanlineAlpha(0)
223cdf0e10cSrcweir {
224cdf0e10cSrcweir // prepare the PNG data stream
225cdf0e10cSrcweir mnOrigStreamMode = mrPNGStream.GetNumberFormatInt();
226cdf0e10cSrcweir mrPNGStream.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
227cdf0e10cSrcweir
228cdf0e10cSrcweir // prepare the chunk reader
229cdf0e10cSrcweir maChunkSeq.reserve( 16 );
230cdf0e10cSrcweir maChunkIter = maChunkSeq.begin();
231cdf0e10cSrcweir
232cdf0e10cSrcweir // estimate PNG file size (to allow sanity checks)
233cdf0e10cSrcweir const sal_Size nStreamPos = mrPNGStream.Tell();
234cdf0e10cSrcweir mrPNGStream.Seek( STREAM_SEEK_TO_END );
235cdf0e10cSrcweir mnStreamSize = mrPNGStream.Tell();
236cdf0e10cSrcweir mrPNGStream.Seek( nStreamPos );
237cdf0e10cSrcweir
238cdf0e10cSrcweir // check the PNG header magic
239cdf0e10cSrcweir sal_uInt32 nDummy = 0;
240cdf0e10cSrcweir mrPNGStream >> nDummy;
241cdf0e10cSrcweir mbStatus = (nDummy == 0x89504e47);
242cdf0e10cSrcweir mrPNGStream >> nDummy;
243cdf0e10cSrcweir mbStatus &= (nDummy == 0x0d0a1a0a);
244cdf0e10cSrcweir
245cdf0e10cSrcweir mnPreviewShift = 0;
246cdf0e10cSrcweir mnPreviewMask = (1 << mnPreviewShift) - 1;
247cdf0e10cSrcweir }
248cdf0e10cSrcweir
249cdf0e10cSrcweir // ------------------------------------------------------------------------
250cdf0e10cSrcweir
~PNGReaderImpl()251cdf0e10cSrcweir PNGReaderImpl::~PNGReaderImpl()
252cdf0e10cSrcweir {
253cdf0e10cSrcweir mrPNGStream.SetNumberFormatInt( mnOrigStreamMode );
254cdf0e10cSrcweir
255cdf0e10cSrcweir if ( mbzCodecInUse )
256cdf0e10cSrcweir mpZCodec->EndCompression();
257cdf0e10cSrcweir
258cdf0e10cSrcweir if( mpColorTable != mpDefaultColorTable )
259cdf0e10cSrcweir delete[] mpColorTable;
260cdf0e10cSrcweir
261cdf0e10cSrcweir delete mpBmp;
262cdf0e10cSrcweir delete mpAlphaMask;
263cdf0e10cSrcweir delete mpMaskBmp;
264cdf0e10cSrcweir delete[] mpTransTab;
265cdf0e10cSrcweir delete[] mpInflateInBuf;
266cdf0e10cSrcweir delete[] mpScanPrior;
267cdf0e10cSrcweir delete mpZCodec;
26888b53a7cSArmin Le Grand
26988b53a7cSArmin Le Grand delete[] mpScanline;
27088b53a7cSArmin Le Grand delete[] mpScanlineAlpha;
271cdf0e10cSrcweir }
272cdf0e10cSrcweir
273cdf0e10cSrcweir // ------------------------------------------------------------------------
274cdf0e10cSrcweir
ReadNextChunk()275cdf0e10cSrcweir bool PNGReaderImpl::ReadNextChunk()
276cdf0e10cSrcweir {
277cdf0e10cSrcweir if( maChunkIter == maChunkSeq.end() )
278cdf0e10cSrcweir {
279cdf0e10cSrcweir // get the next chunk from the stream
280cdf0e10cSrcweir
281cdf0e10cSrcweir // unless we are at the end of the PNG stream
282cdf0e10cSrcweir if( mrPNGStream.IsEof() || (mrPNGStream.GetError() != ERRCODE_NONE) )
283cdf0e10cSrcweir return false;
284cdf0e10cSrcweir if( !maChunkSeq.empty() && (maChunkSeq.back().nType == PNGCHUNK_IEND) )
285cdf0e10cSrcweir return false;
286cdf0e10cSrcweir
287cdf0e10cSrcweir PNGReader::ChunkData aDummyChunk;
288cdf0e10cSrcweir maChunkIter = maChunkSeq.insert( maChunkSeq.end(), aDummyChunk );
289cdf0e10cSrcweir PNGReader::ChunkData& rChunkData = *maChunkIter;
290cdf0e10cSrcweir
291cdf0e10cSrcweir // read the chunk header
292cdf0e10cSrcweir mrPNGStream >> mnChunkLen >> mnChunkType;
293cdf0e10cSrcweir rChunkData.nType = mnChunkType;
294cdf0e10cSrcweir
295cdf0e10cSrcweir // #128377#/#149343# sanity check for chunk length
296cdf0e10cSrcweir if( mnChunkLen < 0 )
297cdf0e10cSrcweir return false;
298cdf0e10cSrcweir const sal_Size nStreamPos = mrPNGStream.Tell();
299cdf0e10cSrcweir if( nStreamPos + mnChunkLen >= mnStreamSize )
300cdf0e10cSrcweir return false;
301cdf0e10cSrcweir
302cdf0e10cSrcweir // calculate chunktype CRC (swap it back to original byte order)
303cdf0e10cSrcweir sal_uInt32 nChunkType = mnChunkType;;
304cdf0e10cSrcweir #if defined(__LITTLEENDIAN) || defined(OSL_LITENDIAN)
305cdf0e10cSrcweir nChunkType = SWAPLONG( nChunkType );
306cdf0e10cSrcweir #endif
307cdf0e10cSrcweir sal_uInt32 nCRC32 = rtl_crc32( 0, &nChunkType, 4 );
308cdf0e10cSrcweir
309cdf0e10cSrcweir // read the chunk data and check the CRC
310cdf0e10cSrcweir if( mnChunkLen && !mrPNGStream.IsEof() )
311cdf0e10cSrcweir {
312cdf0e10cSrcweir rChunkData.aData.resize( mnChunkLen );
313cdf0e10cSrcweir
314cdf0e10cSrcweir sal_Int32 nBytesRead = 0;
315cdf0e10cSrcweir do {
316cdf0e10cSrcweir sal_uInt8* pPtr = &rChunkData.aData[ nBytesRead ];
317cdf0e10cSrcweir nBytesRead += mrPNGStream.Read( pPtr, mnChunkLen - nBytesRead );
318cdf0e10cSrcweir } while ( ( nBytesRead < mnChunkLen ) && ( mrPNGStream.GetError() == ERRCODE_NONE ) );
319cdf0e10cSrcweir
320cdf0e10cSrcweir nCRC32 = rtl_crc32( nCRC32, &rChunkData.aData[ 0 ], mnChunkLen );
321cdf0e10cSrcweir maDataIter = rChunkData.aData.begin();
322cdf0e10cSrcweir }
323cdf0e10cSrcweir sal_uInt32 nCheck;
324cdf0e10cSrcweir mrPNGStream >> nCheck;
325cdf0e10cSrcweir if( nCRC32 != nCheck )
326cdf0e10cSrcweir return false;
327cdf0e10cSrcweir }
328cdf0e10cSrcweir else
329cdf0e10cSrcweir {
330cdf0e10cSrcweir // the next chunk was already read
331cdf0e10cSrcweir mnChunkType = (*maChunkIter).nType;
332cdf0e10cSrcweir mnChunkLen = (*maChunkIter).aData.size();
333cdf0e10cSrcweir maDataIter = (*maChunkIter).aData.begin();
334cdf0e10cSrcweir }
335cdf0e10cSrcweir
336cdf0e10cSrcweir ++maChunkIter;
337cdf0e10cSrcweir if( mnChunkType == PNGCHUNK_IEND )
338cdf0e10cSrcweir return false;
339cdf0e10cSrcweir return true;
340cdf0e10cSrcweir }
341cdf0e10cSrcweir
342cdf0e10cSrcweir // ------------------------------------------------------------------------
343cdf0e10cSrcweir
344cdf0e10cSrcweir // read the remaining chunks from mrPNGStream
ReadRemainingChunks()345cdf0e10cSrcweir void PNGReaderImpl::ReadRemainingChunks()
346cdf0e10cSrcweir {
347cdf0e10cSrcweir while( ReadNextChunk() ) ;
348cdf0e10cSrcweir }
349cdf0e10cSrcweir
350cdf0e10cSrcweir // ------------------------------------------------------------------------
351cdf0e10cSrcweir
352cdf0e10cSrcweir // move position of mrPNGStream to the end of the file
SkipRemainingChunks()353cdf0e10cSrcweir void PNGReaderImpl::SkipRemainingChunks()
354cdf0e10cSrcweir {
355cdf0e10cSrcweir // nothing to skip if the last chunk was read
356cdf0e10cSrcweir if( !maChunkSeq.empty() && (maChunkSeq.back().nType == PNGCHUNK_IEND) )
357cdf0e10cSrcweir return;
358cdf0e10cSrcweir
359cdf0e10cSrcweir // read from the stream until the IEND chunk is found
360cdf0e10cSrcweir const sal_Size nStreamPos = mrPNGStream.Tell();
361cdf0e10cSrcweir while( !mrPNGStream.IsEof() && (mrPNGStream.GetError() == ERRCODE_NONE) )
362cdf0e10cSrcweir {
363cdf0e10cSrcweir mrPNGStream >> mnChunkLen >> mnChunkType;
364cdf0e10cSrcweir if( mnChunkLen < 0 )
365cdf0e10cSrcweir break;
366cdf0e10cSrcweir if( nStreamPos + mnChunkLen >= mnStreamSize )
367cdf0e10cSrcweir break;
368cdf0e10cSrcweir mrPNGStream.SeekRel( mnChunkLen + 4 ); // skip data + CRC
369cdf0e10cSrcweir if( mnChunkType == PNGCHUNK_IEND )
370cdf0e10cSrcweir break;
371cdf0e10cSrcweir }
372cdf0e10cSrcweir }
373cdf0e10cSrcweir
374cdf0e10cSrcweir // ------------------------------------------------------------------------
375cdf0e10cSrcweir
GetAllChunks()376cdf0e10cSrcweir const std::vector< vcl::PNGReader::ChunkData >& PNGReaderImpl::GetAllChunks()
377cdf0e10cSrcweir {
378cdf0e10cSrcweir ReadRemainingChunks();
379cdf0e10cSrcweir return maChunkSeq;
380cdf0e10cSrcweir }
381cdf0e10cSrcweir
382cdf0e10cSrcweir // ------------------------------------------------------------------------
383cdf0e10cSrcweir
GetBitmapEx(const Size & rPreviewSizeHint)384cdf0e10cSrcweir BitmapEx PNGReaderImpl::GetBitmapEx( const Size& rPreviewSizeHint )
385cdf0e10cSrcweir {
386cdf0e10cSrcweir // reset to the first chunk
387cdf0e10cSrcweir maChunkIter = maChunkSeq.begin();
388cdf0e10cSrcweir
3893447fe24SHerbert Dürr // read the first chunk which must be the IHDR chunk
3903447fe24SHerbert Dürr ReadNextChunk();
3913447fe24SHerbert Dürr mbStatus = (mnChunkType == PNGCHUNK_IHDR) && ImplReadHeader( rPreviewSizeHint );
3923447fe24SHerbert Dürr
393cdf0e10cSrcweir // parse the chunks
394cdf0e10cSrcweir while( mbStatus && !mbIDAT && ReadNextChunk() )
395cdf0e10cSrcweir {
396cdf0e10cSrcweir switch( mnChunkType )
397cdf0e10cSrcweir {
398cdf0e10cSrcweir case PNGCHUNK_IHDR :
399cdf0e10cSrcweir {
4003447fe24SHerbert Dürr mbStatus = false; // only one IHDR possible
401cdf0e10cSrcweir }
402cdf0e10cSrcweir break;
403cdf0e10cSrcweir
404cdf0e10cSrcweir case PNGCHUNK_gAMA : // the gamma chunk must precede
405cdf0e10cSrcweir { // the 'IDAT' and also the 'PLTE'(if available )
406cdf0e10cSrcweir if ( !mbIgnoreGammaChunk && ( mbIDAT == sal_False ) )
407cdf0e10cSrcweir ImplGetGamma();
408cdf0e10cSrcweir }
409cdf0e10cSrcweir break;
410cdf0e10cSrcweir
411cdf0e10cSrcweir case PNGCHUNK_PLTE :
412cdf0e10cSrcweir {
413cdf0e10cSrcweir if ( !mbPalette )
414cdf0e10cSrcweir mbStatus = ImplReadPalette();
415cdf0e10cSrcweir }
416cdf0e10cSrcweir break;
417cdf0e10cSrcweir
418cdf0e10cSrcweir case PNGCHUNK_tRNS :
419cdf0e10cSrcweir {
420cdf0e10cSrcweir if ( !mbIDAT ) // the tRNS chunk must precede the IDAT
421cdf0e10cSrcweir mbStatus = ImplReadTransparent();
422cdf0e10cSrcweir }
423cdf0e10cSrcweir break;
424cdf0e10cSrcweir
425cdf0e10cSrcweir case PNGCHUNK_bKGD : // the background chunk must appear
426cdf0e10cSrcweir {
427cdf0e10cSrcweir if ( ( mbIDAT == sal_False ) && mbPalette ) // before the 'IDAT' and after the
428cdf0e10cSrcweir ImplGetBackground(); // PLTE(if available ) chunk.
429cdf0e10cSrcweir }
430cdf0e10cSrcweir break;
431cdf0e10cSrcweir
432cdf0e10cSrcweir case PNGCHUNK_IDAT :
433cdf0e10cSrcweir {
434cdf0e10cSrcweir if ( !mpInflateInBuf ) // taking care that the header has properly been read
435cdf0e10cSrcweir mbStatus = sal_False;
436cdf0e10cSrcweir else if ( !mbIDAT ) // the gfx is finished, but there may be left a zlibCRC of about 4Bytes
437cdf0e10cSrcweir ImplReadIDAT();
438cdf0e10cSrcweir }
439cdf0e10cSrcweir break;
440cdf0e10cSrcweir
441cdf0e10cSrcweir case PNGCHUNK_pHYs :
442cdf0e10cSrcweir {
443cdf0e10cSrcweir if ( !mbIDAT && mnChunkLen == 9 )
444cdf0e10cSrcweir {
445cdf0e10cSrcweir sal_uInt32 nXPixelPerMeter = ImplReadsal_uInt32();
446cdf0e10cSrcweir sal_uInt32 nYPixelPerMeter = ImplReadsal_uInt32();
447cdf0e10cSrcweir
448cdf0e10cSrcweir sal_uInt8 nUnitSpecifier = *maDataIter++;
449cdf0e10cSrcweir if( (nUnitSpecifier == 1) && nXPixelPerMeter && nXPixelPerMeter )
450cdf0e10cSrcweir {
451cdf0e10cSrcweir mbpHYs = sal_True;
452cdf0e10cSrcweir
453cdf0e10cSrcweir // convert into MAP_100TH_MM
454cdf0e10cSrcweir maPhysSize.Width() = (sal_Int32)( (100000.0 * maOrigSize.Width()) / nXPixelPerMeter );
455cdf0e10cSrcweir maPhysSize.Height() = (sal_Int32)( (100000.0 * maOrigSize.Height()) / nYPixelPerMeter );
456cdf0e10cSrcweir }
457cdf0e10cSrcweir }
458cdf0e10cSrcweir }
459cdf0e10cSrcweir break;
460cdf0e10cSrcweir
461cdf0e10cSrcweir case PNGCHUNK_IEND:
462cdf0e10cSrcweir mbStatus = mbIDAT; // there is a problem if the image is not complete yet
463cdf0e10cSrcweir break;
464cdf0e10cSrcweir }
465cdf0e10cSrcweir }
466cdf0e10cSrcweir
467cdf0e10cSrcweir // release write access of the bitmaps
468cdf0e10cSrcweir if ( mpAcc )
469cdf0e10cSrcweir mpBmp->ReleaseAccess( mpAcc ), mpAcc = NULL;
470cdf0e10cSrcweir
471cdf0e10cSrcweir if ( mpMaskAcc )
472cdf0e10cSrcweir {
473cdf0e10cSrcweir if ( mpAlphaMask )
474cdf0e10cSrcweir mpAlphaMask->ReleaseAccess( mpMaskAcc );
475cdf0e10cSrcweir else if ( mpMaskBmp )
476cdf0e10cSrcweir mpMaskBmp->ReleaseAccess( mpMaskAcc );
477cdf0e10cSrcweir
478cdf0e10cSrcweir mpMaskAcc = NULL;
479cdf0e10cSrcweir }
480cdf0e10cSrcweir
481cdf0e10cSrcweir // return the resulting BitmapEx
482cdf0e10cSrcweir BitmapEx aRet;
483cdf0e10cSrcweir
484cdf0e10cSrcweir if( !mbStatus || !mbIDAT )
485cdf0e10cSrcweir aRet.Clear();
486cdf0e10cSrcweir else
487cdf0e10cSrcweir {
488cdf0e10cSrcweir if ( mpAlphaMask )
489cdf0e10cSrcweir aRet = BitmapEx( *mpBmp, *mpAlphaMask );
490cdf0e10cSrcweir else if ( mpMaskBmp )
491cdf0e10cSrcweir aRet = BitmapEx( *mpBmp, *mpMaskBmp );
492cdf0e10cSrcweir else
493cdf0e10cSrcweir aRet = *mpBmp;
494cdf0e10cSrcweir
495cdf0e10cSrcweir if ( mbpHYs && maPhysSize.Width() && maPhysSize.Height() )
496cdf0e10cSrcweir {
497cdf0e10cSrcweir aRet.SetPrefMapMode( MAP_100TH_MM );
498cdf0e10cSrcweir aRet.SetPrefSize( maPhysSize );
499cdf0e10cSrcweir }
500cdf0e10cSrcweir
501cdf0e10cSrcweir #if 0
502cdf0e10cSrcweir // TODO: make sure nobody depends on the stream being after the IEND chunks
503cdf0e10cSrcweir // => let them do ReadChunks before
504cdf0e10cSrcweir ReadRemainingChunks();
505cdf0e10cSrcweir #endif
506cdf0e10cSrcweir }
507cdf0e10cSrcweir
508cdf0e10cSrcweir return aRet;
509cdf0e10cSrcweir }
510cdf0e10cSrcweir
511cdf0e10cSrcweir // ------------------------------------------------------------------------
512cdf0e10cSrcweir
ImplReadHeader(const Size & rPreviewSizeHint)513cdf0e10cSrcweir sal_Bool PNGReaderImpl::ImplReadHeader( const Size& rPreviewSizeHint )
514cdf0e10cSrcweir {
515cdf0e10cSrcweir if( mnChunkLen < 13 )
516cdf0e10cSrcweir return sal_False;
517cdf0e10cSrcweir
518cdf0e10cSrcweir maOrigSize.Width() = ImplReadsal_uInt32();
519cdf0e10cSrcweir maOrigSize.Height() = ImplReadsal_uInt32();
520cdf0e10cSrcweir
521cdf0e10cSrcweir if ( !maOrigSize.Width() || !maOrigSize.Height() )
522cdf0e10cSrcweir return sal_False;
523cdf0e10cSrcweir
524cdf0e10cSrcweir mnPngDepth = *(maDataIter++);
525cdf0e10cSrcweir mnColorType = *(maDataIter++);
526cdf0e10cSrcweir
527cdf0e10cSrcweir mnCompressionType = *(maDataIter++);
528cdf0e10cSrcweir if( mnCompressionType != 0 ) // unknown compression type
529cdf0e10cSrcweir return sal_False;
530cdf0e10cSrcweir
531cdf0e10cSrcweir mnFilterType = *(maDataIter++);
532cdf0e10cSrcweir if( mnFilterType != 0 ) // unknown filter type
533cdf0e10cSrcweir return sal_False;
534cdf0e10cSrcweir
535cdf0e10cSrcweir mnInterlaceType = *(maDataIter++);
536cdf0e10cSrcweir switch ( mnInterlaceType ) // filter type valid ?
537cdf0e10cSrcweir {
538cdf0e10cSrcweir case 0 : // progressive image
539cdf0e10cSrcweir mnPass = 7;
540cdf0e10cSrcweir break;
541cdf0e10cSrcweir case 1 : // Adam7-interlaced image
542cdf0e10cSrcweir mnPass = 0;
543cdf0e10cSrcweir break;
544cdf0e10cSrcweir default:
545cdf0e10cSrcweir return sal_False;
546cdf0e10cSrcweir }
547cdf0e10cSrcweir
548cdf0e10cSrcweir mbPalette = sal_True;
549cdf0e10cSrcweir mbIDAT = mbAlphaChannel = mbTransparent = sal_False;
550cdf0e10cSrcweir mbGrayScale = mbRGBTriple = sal_False;
551cdf0e10cSrcweir mnTargetDepth = mnPngDepth;
552cdf0e10cSrcweir sal_uInt64 nScansize64 = ( ( static_cast< sal_uInt64 >( maOrigSize.Width() ) * mnPngDepth ) + 7 ) >> 3;
553cdf0e10cSrcweir
554cdf0e10cSrcweir // valid color types are 0,2,3,4 & 6
555cdf0e10cSrcweir switch ( mnColorType )
556cdf0e10cSrcweir {
557cdf0e10cSrcweir case 0 : // each pixel is a grayscale
558cdf0e10cSrcweir {
559cdf0e10cSrcweir switch ( mnPngDepth )
560cdf0e10cSrcweir {
561cdf0e10cSrcweir case 2 : // 2bit target not available -> use four bits
562cdf0e10cSrcweir mnTargetDepth = 4; // we have to expand the bitmap
563cdf0e10cSrcweir mbGrayScale = sal_True;
564cdf0e10cSrcweir break;
565cdf0e10cSrcweir case 16 :
566cdf0e10cSrcweir mnTargetDepth = 8; // we have to reduce the bitmap
567cdf0e10cSrcweir // fall through
568cdf0e10cSrcweir case 1 :
569cdf0e10cSrcweir case 4 :
570cdf0e10cSrcweir case 8 :
571cdf0e10cSrcweir mbGrayScale = sal_True;
572cdf0e10cSrcweir break;
573cdf0e10cSrcweir default :
574cdf0e10cSrcweir return sal_False;
575cdf0e10cSrcweir }
576cdf0e10cSrcweir }
577cdf0e10cSrcweir break;
578cdf0e10cSrcweir
579cdf0e10cSrcweir case 2 : // each pixel is an RGB triple
580cdf0e10cSrcweir {
581cdf0e10cSrcweir mbRGBTriple = sal_True;
582cdf0e10cSrcweir nScansize64 *= 3;
583cdf0e10cSrcweir switch ( mnPngDepth )
584cdf0e10cSrcweir {
585cdf0e10cSrcweir case 16 : // we have to reduce the bitmap
586cdf0e10cSrcweir case 8 :
587cdf0e10cSrcweir mnTargetDepth = 24;
588cdf0e10cSrcweir break;
589cdf0e10cSrcweir default :
590cdf0e10cSrcweir return sal_False;
591cdf0e10cSrcweir }
592cdf0e10cSrcweir }
593cdf0e10cSrcweir break;
594cdf0e10cSrcweir
595cdf0e10cSrcweir case 3 : // each pixel is a palette index
596cdf0e10cSrcweir {
597cdf0e10cSrcweir switch ( mnPngDepth )
598cdf0e10cSrcweir {
599cdf0e10cSrcweir case 2 :
600cdf0e10cSrcweir mnTargetDepth = 4; // we have to expand the bitmap
601cdf0e10cSrcweir // fall through
602cdf0e10cSrcweir case 1 :
603cdf0e10cSrcweir case 4 :
604cdf0e10cSrcweir case 8 :
605cdf0e10cSrcweir mbPalette = sal_False;
606cdf0e10cSrcweir break;
607cdf0e10cSrcweir default :
608cdf0e10cSrcweir return sal_False;
609cdf0e10cSrcweir }
610cdf0e10cSrcweir }
611cdf0e10cSrcweir break;
612cdf0e10cSrcweir
613cdf0e10cSrcweir case 4 : // each pixel is a grayscale sample followed by an alpha sample
614cdf0e10cSrcweir {
615cdf0e10cSrcweir nScansize64 *= 2;
616cdf0e10cSrcweir mbAlphaChannel = sal_True;
617cdf0e10cSrcweir switch ( mnPngDepth )
618cdf0e10cSrcweir {
619cdf0e10cSrcweir case 16 :
620cdf0e10cSrcweir mnTargetDepth = 8; // we have to reduce the bitmap
621cdf0e10cSrcweir case 8 :
622cdf0e10cSrcweir mbGrayScale = sal_True;
623cdf0e10cSrcweir break;
624cdf0e10cSrcweir default :
625cdf0e10cSrcweir return sal_False;
626cdf0e10cSrcweir }
627cdf0e10cSrcweir }
628cdf0e10cSrcweir break;
629cdf0e10cSrcweir
630cdf0e10cSrcweir case 6 : // each pixel is an RGB triple followed by an alpha sample
631cdf0e10cSrcweir {
632cdf0e10cSrcweir mbRGBTriple = sal_True;
633cdf0e10cSrcweir nScansize64 *= 4;
634cdf0e10cSrcweir mbAlphaChannel = sal_True;
635cdf0e10cSrcweir switch (mnPngDepth )
636cdf0e10cSrcweir {
637cdf0e10cSrcweir case 16 : // we have to reduce the bitmap
638cdf0e10cSrcweir case 8 :
639cdf0e10cSrcweir mnTargetDepth = 24;
640cdf0e10cSrcweir break;
641cdf0e10cSrcweir default :
642cdf0e10cSrcweir return sal_False;
643cdf0e10cSrcweir }
644cdf0e10cSrcweir }
645cdf0e10cSrcweir break;
646cdf0e10cSrcweir
647cdf0e10cSrcweir default :
648cdf0e10cSrcweir return sal_False;
649cdf0e10cSrcweir }
650cdf0e10cSrcweir
651cdf0e10cSrcweir mnBPP = static_cast< sal_uInt32 >( nScansize64 / maOrigSize.Width() );
652cdf0e10cSrcweir if ( !mnBPP )
653cdf0e10cSrcweir mnBPP = 1;
654cdf0e10cSrcweir
655cdf0e10cSrcweir nScansize64++; // each scanline includes one filterbyte
656cdf0e10cSrcweir
657cdf0e10cSrcweir if ( nScansize64 > SAL_MAX_UINT32 )
658cdf0e10cSrcweir return sal_False;
659cdf0e10cSrcweir
660cdf0e10cSrcweir mnScansize = static_cast< sal_uInt32 >( nScansize64 );
661cdf0e10cSrcweir
662cdf0e10cSrcweir // TODO: switch between both scanlines instead of copying
663cdf0e10cSrcweir mpInflateInBuf = new (std::nothrow) sal_uInt8[ mnScansize ];
664cdf0e10cSrcweir mpScanCurrent = mpInflateInBuf;
665cdf0e10cSrcweir mpScanPrior = new (std::nothrow) sal_uInt8[ mnScansize ];
666cdf0e10cSrcweir
667cdf0e10cSrcweir if ( !mpInflateInBuf || !mpScanPrior )
668cdf0e10cSrcweir return sal_False;
669cdf0e10cSrcweir
670cdf0e10cSrcweir // calculate target size from original size and the preview hint
671cdf0e10cSrcweir if( rPreviewSizeHint.Width() || rPreviewSizeHint.Height() )
672cdf0e10cSrcweir {
673cdf0e10cSrcweir Size aPreviewSize( rPreviewSizeHint.Width(), rPreviewSizeHint.Height() );
674cdf0e10cSrcweir maTargetSize = maOrigSize;
675cdf0e10cSrcweir
676cdf0e10cSrcweir if( aPreviewSize.Width() == 0 ) {
677cdf0e10cSrcweir aPreviewSize.setWidth( ( maOrigSize.Width()*aPreviewSize.Height() )/maOrigSize.Height() );
678cdf0e10cSrcweir if( aPreviewSize.Width() <= 0 )
679cdf0e10cSrcweir aPreviewSize.setWidth( 1 );
680cdf0e10cSrcweir } else if( aPreviewSize.Height() == 0 ) {
681cdf0e10cSrcweir aPreviewSize.setHeight( ( maOrigSize.Height()*aPreviewSize.Width() )/maOrigSize.Width() );
682cdf0e10cSrcweir if( aPreviewSize.Height() <= 0 )
683cdf0e10cSrcweir aPreviewSize.setHeight( 1 );
684cdf0e10cSrcweir }
685cdf0e10cSrcweir
686cdf0e10cSrcweir if( aPreviewSize.Width() < maOrigSize.Width() && aPreviewSize.Height() < maOrigSize.Height() ) {
687cdf0e10cSrcweir OSL_TRACE("preview size %dx%d", aPreviewSize.Width(), aPreviewSize.Height() );
688cdf0e10cSrcweir
689cdf0e10cSrcweir for( int i = 1; i < 5; ++i )
690cdf0e10cSrcweir {
691cdf0e10cSrcweir if( (maTargetSize.Width() >> i) < aPreviewSize.Width() )
692cdf0e10cSrcweir break;
693cdf0e10cSrcweir if( (maTargetSize.Height() >> i) < aPreviewSize.Height() )
694cdf0e10cSrcweir break;
695cdf0e10cSrcweir mnPreviewShift = i;
696cdf0e10cSrcweir }
697cdf0e10cSrcweir mnPreviewMask = (1 << mnPreviewShift) - 1;
698cdf0e10cSrcweir }
699cdf0e10cSrcweir }
700cdf0e10cSrcweir
701cdf0e10cSrcweir maTargetSize.Width() = (maOrigSize.Width() + mnPreviewMask) >> mnPreviewShift;
702cdf0e10cSrcweir maTargetSize.Height() = (maOrigSize.Height() + mnPreviewMask) >> mnPreviewShift;
703cdf0e10cSrcweir
704cdf0e10cSrcweir mpBmp = new Bitmap( maTargetSize, mnTargetDepth );
705cdf0e10cSrcweir mpAcc = mpBmp->AcquireWriteAccess();
706cdf0e10cSrcweir if( !mpAcc )
707cdf0e10cSrcweir return sal_False;
708cdf0e10cSrcweir
709cdf0e10cSrcweir mpBmp->SetSourceSizePixel( maOrigSize );
710cdf0e10cSrcweir
711cdf0e10cSrcweir if ( mbAlphaChannel )
712cdf0e10cSrcweir {
713cdf0e10cSrcweir mpAlphaMask = new AlphaMask( maTargetSize );
714cdf0e10cSrcweir mpAlphaMask->Erase( 128 );
715cdf0e10cSrcweir mpMaskAcc = mpAlphaMask->AcquireWriteAccess();
716cdf0e10cSrcweir if( !mpMaskAcc )
717cdf0e10cSrcweir return sal_False;
718cdf0e10cSrcweir }
719cdf0e10cSrcweir
720cdf0e10cSrcweir if ( mbGrayScale )
721cdf0e10cSrcweir ImplGetGrayPalette( mnPngDepth );
722cdf0e10cSrcweir
723cdf0e10cSrcweir ImplPreparePass();
724cdf0e10cSrcweir
725cdf0e10cSrcweir return sal_True;
726cdf0e10cSrcweir }
727cdf0e10cSrcweir
728cdf0e10cSrcweir // ------------------------------------------------------------------------
729cdf0e10cSrcweir
ImplGetGrayPalette(sal_uInt16 nBitDepth)730cdf0e10cSrcweir void PNGReaderImpl::ImplGetGrayPalette( sal_uInt16 nBitDepth )
731cdf0e10cSrcweir {
732cdf0e10cSrcweir if( nBitDepth > 8 )
733cdf0e10cSrcweir nBitDepth = 8;
734cdf0e10cSrcweir
735cdf0e10cSrcweir sal_uInt16 nPaletteEntryCount = 1 << nBitDepth;
736cdf0e10cSrcweir sal_uInt32 nAdd = nBitDepth ? 256 / (nPaletteEntryCount - 1) : 0;
737cdf0e10cSrcweir
738cdf0e10cSrcweir // no bitdepth==2 available
739cdf0e10cSrcweir // but bitdepth==4 with two unused bits is close enough
740cdf0e10cSrcweir if( nBitDepth == 2 )
741cdf0e10cSrcweir nPaletteEntryCount = 16;
742cdf0e10cSrcweir
743cdf0e10cSrcweir mpAcc->SetPaletteEntryCount( nPaletteEntryCount );
744cdf0e10cSrcweir for ( sal_uInt32 i = 0, nStart = 0; nStart < 256; i++, nStart += nAdd )
745cdf0e10cSrcweir mpAcc->SetPaletteColor( (sal_uInt16)i, BitmapColor( mpColorTable[ nStart ],
746cdf0e10cSrcweir mpColorTable[ nStart ], mpColorTable[ nStart ] ) );
747cdf0e10cSrcweir }
748cdf0e10cSrcweir
749cdf0e10cSrcweir // ------------------------------------------------------------------------
750cdf0e10cSrcweir
ImplReadPalette()751cdf0e10cSrcweir sal_Bool PNGReaderImpl::ImplReadPalette()
752cdf0e10cSrcweir {
753cdf0e10cSrcweir sal_uInt16 nCount = static_cast<sal_uInt16>( mnChunkLen / 3 );
754cdf0e10cSrcweir
755cdf0e10cSrcweir if ( ( ( mnChunkLen % 3 ) == 0 ) && ( ( 0 < nCount ) && ( nCount <= 256 ) ) && mpAcc )
756cdf0e10cSrcweir {
757cdf0e10cSrcweir mbPalette = sal_True;
758cdf0e10cSrcweir mpAcc->SetPaletteEntryCount( (sal_uInt16) nCount );
759cdf0e10cSrcweir
760cdf0e10cSrcweir for ( sal_uInt16 i = 0; i < nCount; i++ )
761cdf0e10cSrcweir {
762cdf0e10cSrcweir sal_uInt8 nRed = mpColorTable[ *maDataIter++ ];
763cdf0e10cSrcweir sal_uInt8 nGreen = mpColorTable[ *maDataIter++ ];
764cdf0e10cSrcweir sal_uInt8 nBlue = mpColorTable[ *maDataIter++ ];
765cdf0e10cSrcweir mpAcc->SetPaletteColor( i, Color( nRed, nGreen, nBlue ) );
766cdf0e10cSrcweir }
767cdf0e10cSrcweir }
768cdf0e10cSrcweir else
769cdf0e10cSrcweir mbStatus = sal_False;
770cdf0e10cSrcweir
771cdf0e10cSrcweir return mbStatus;
772cdf0e10cSrcweir }
773cdf0e10cSrcweir
774cdf0e10cSrcweir // ------------------------------------------------------------------------
775cdf0e10cSrcweir
ImplReadTransparent()776cdf0e10cSrcweir sal_Bool PNGReaderImpl::ImplReadTransparent()
777cdf0e10cSrcweir {
778cdf0e10cSrcweir bool bNeedAlpha = false;
779cdf0e10cSrcweir
780cdf0e10cSrcweir if ( mpTransTab == NULL )
781cdf0e10cSrcweir {
782cdf0e10cSrcweir switch ( mnColorType )
783cdf0e10cSrcweir {
784cdf0e10cSrcweir case 0 :
785cdf0e10cSrcweir {
786cdf0e10cSrcweir if ( mnChunkLen == 2 )
787cdf0e10cSrcweir {
788cdf0e10cSrcweir mpTransTab = new sal_uInt8[ 256 ];
789cdf0e10cSrcweir rtl_fillMemory( mpTransTab, 256, 0xff );
790cdf0e10cSrcweir // color type 0 and 4 is always greyscale,
791cdf0e10cSrcweir // so the return value can be used as index
792cdf0e10cSrcweir sal_uInt8 nIndex = ImplScaleColor();
793cdf0e10cSrcweir mpTransTab[ nIndex ] = 0;
794cdf0e10cSrcweir mbTransparent = true;
795cdf0e10cSrcweir }
796cdf0e10cSrcweir }
797cdf0e10cSrcweir break;
798cdf0e10cSrcweir
799cdf0e10cSrcweir case 2 :
800cdf0e10cSrcweir {
801cdf0e10cSrcweir if ( mnChunkLen == 6 )
802cdf0e10cSrcweir {
803cdf0e10cSrcweir mnTransRed = ImplScaleColor();
804cdf0e10cSrcweir mnTransGreen = ImplScaleColor();
805cdf0e10cSrcweir mnTransBlue = ImplScaleColor();
806cdf0e10cSrcweir mbTransparent = true;
807cdf0e10cSrcweir }
808cdf0e10cSrcweir }
809cdf0e10cSrcweir break;
810cdf0e10cSrcweir
811cdf0e10cSrcweir case 3 :
812cdf0e10cSrcweir {
813cdf0e10cSrcweir if ( mnChunkLen <= 256 )
814cdf0e10cSrcweir {
815cdf0e10cSrcweir mpTransTab = new sal_uInt8 [ 256 ];
816cdf0e10cSrcweir rtl_fillMemory( mpTransTab, 256, 0xff );
817cdf0e10cSrcweir rtl_copyMemory( mpTransTab, &(*maDataIter), mnChunkLen );
818cdf0e10cSrcweir maDataIter += mnChunkLen;
819cdf0e10cSrcweir mbTransparent = true;
820cdf0e10cSrcweir // need alpha transparency if not on/off masking
821cdf0e10cSrcweir for( int i = 0; i < mnChunkLen; ++i )
822cdf0e10cSrcweir bNeedAlpha |= (mpTransTab[i]!=0x00) && (mpTransTab[i]!=0xFF);
823cdf0e10cSrcweir }
824cdf0e10cSrcweir }
825cdf0e10cSrcweir break;
826cdf0e10cSrcweir }
827cdf0e10cSrcweir }
828cdf0e10cSrcweir
829cdf0e10cSrcweir if( mbTransparent && !mbAlphaChannel && !mpMaskBmp )
830cdf0e10cSrcweir {
831cdf0e10cSrcweir if( bNeedAlpha)
832cdf0e10cSrcweir {
833cdf0e10cSrcweir mpAlphaMask = new AlphaMask( maTargetSize );
834cdf0e10cSrcweir mpMaskAcc = mpAlphaMask->AcquireWriteAccess();
835cdf0e10cSrcweir }
836cdf0e10cSrcweir else
837cdf0e10cSrcweir {
838cdf0e10cSrcweir mpMaskBmp = new Bitmap( maTargetSize, 1 );
839cdf0e10cSrcweir mpMaskAcc = mpMaskBmp->AcquireWriteAccess();
840cdf0e10cSrcweir }
841cdf0e10cSrcweir mbTransparent = (mpMaskAcc != NULL);
842cdf0e10cSrcweir if( !mbTransparent )
843cdf0e10cSrcweir return sal_False;
844cdf0e10cSrcweir mcOpaqueColor = BitmapColor( 0x00 );
845cdf0e10cSrcweir mcTranspColor = BitmapColor( 0xFF );
846cdf0e10cSrcweir mpMaskAcc->Erase( 0x00 );
847cdf0e10cSrcweir }
848cdf0e10cSrcweir
849cdf0e10cSrcweir return sal_True;
850cdf0e10cSrcweir }
851cdf0e10cSrcweir
852cdf0e10cSrcweir // ------------------------------------------------------------------------
853cdf0e10cSrcweir
ImplGetGamma()854cdf0e10cSrcweir void PNGReaderImpl::ImplGetGamma()
855cdf0e10cSrcweir {
856cdf0e10cSrcweir if( mnChunkLen < 4 )
857cdf0e10cSrcweir return;
858cdf0e10cSrcweir
859cdf0e10cSrcweir sal_uInt32 nGammaValue = ImplReadsal_uInt32();
860cdf0e10cSrcweir double fGamma = ( ( VIEWING_GAMMA / DISPLAY_GAMMA ) * ( (double)nGammaValue / 100000 ) );
861cdf0e10cSrcweir double fInvGamma = ( fGamma <= 0.0 || fGamma > 10.0 ) ? 1.0 : ( 1.0 / fGamma );
862cdf0e10cSrcweir
863cdf0e10cSrcweir if ( fInvGamma != 1.0 )
864cdf0e10cSrcweir {
865cdf0e10cSrcweir mbGamma = sal_True;
866cdf0e10cSrcweir
867cdf0e10cSrcweir if ( mpColorTable == mpDefaultColorTable )
868cdf0e10cSrcweir mpColorTable = new sal_uInt8[ 256 ];
869cdf0e10cSrcweir
870cdf0e10cSrcweir for ( sal_Int32 i = 0; i < 256; i++ )
871cdf0e10cSrcweir mpColorTable[ i ] = (sal_uInt8)(pow((double)i/255.0, fInvGamma) * 255.0 + 0.5);
872cdf0e10cSrcweir
873cdf0e10cSrcweir if ( mbGrayScale )
874cdf0e10cSrcweir ImplGetGrayPalette( mnPngDepth );
875cdf0e10cSrcweir }
876cdf0e10cSrcweir }
877cdf0e10cSrcweir
878cdf0e10cSrcweir // ------------------------------------------------------------------------
879cdf0e10cSrcweir
ImplGetBackground()880cdf0e10cSrcweir void PNGReaderImpl::ImplGetBackground()
881cdf0e10cSrcweir {
882cdf0e10cSrcweir switch ( mnColorType )
883cdf0e10cSrcweir {
884cdf0e10cSrcweir case 3 :
885cdf0e10cSrcweir {
886cdf0e10cSrcweir if ( mnChunkLen == 1 )
887cdf0e10cSrcweir {
888cdf0e10cSrcweir sal_uInt16 nCol = *maDataIter++;
889cdf0e10cSrcweir if ( nCol < mpAcc->GetPaletteEntryCount() )
890cdf0e10cSrcweir {
891cdf0e10cSrcweir mpAcc->Erase( mpAcc->GetPaletteColor( (sal_uInt8)nCol ) );
892cdf0e10cSrcweir break;
893cdf0e10cSrcweir }
894cdf0e10cSrcweir }
895cdf0e10cSrcweir }
896cdf0e10cSrcweir break;
897cdf0e10cSrcweir
898cdf0e10cSrcweir case 0 :
899cdf0e10cSrcweir case 4 :
900cdf0e10cSrcweir {
901cdf0e10cSrcweir if ( mnChunkLen == 2 )
902cdf0e10cSrcweir {
903cdf0e10cSrcweir // the color type 0 and 4 is always greyscale,
904cdf0e10cSrcweir // so the return value can be used as index
905cdf0e10cSrcweir sal_uInt8 nIndex = ImplScaleColor();
906cdf0e10cSrcweir mpAcc->Erase( mpAcc->GetPaletteColor( nIndex ) );
907cdf0e10cSrcweir }
908cdf0e10cSrcweir }
909cdf0e10cSrcweir break;
910cdf0e10cSrcweir
911cdf0e10cSrcweir case 2 :
912cdf0e10cSrcweir case 6 :
913cdf0e10cSrcweir {
914cdf0e10cSrcweir if ( mnChunkLen == 6 )
915cdf0e10cSrcweir {
916cdf0e10cSrcweir sal_uInt8 nRed = ImplScaleColor();
917cdf0e10cSrcweir sal_uInt8 nGreen = ImplScaleColor();
918cdf0e10cSrcweir sal_uInt8 nBlue = ImplScaleColor();
919cdf0e10cSrcweir mpAcc->Erase( Color( nRed, nGreen, nBlue ) );
920cdf0e10cSrcweir }
921cdf0e10cSrcweir }
922cdf0e10cSrcweir break;
923cdf0e10cSrcweir }
924cdf0e10cSrcweir }
925cdf0e10cSrcweir
926cdf0e10cSrcweir // ------------------------------------------------------------------------
927cdf0e10cSrcweir
928cdf0e10cSrcweir // for color type 0 and 4 (greyscale) the return value is always index to the color
929cdf0e10cSrcweir // 2 and 6 (RGB) the return value is always the 8 bit color component
ImplScaleColor()930cdf0e10cSrcweir sal_uInt8 PNGReaderImpl::ImplScaleColor()
931cdf0e10cSrcweir {
932cdf0e10cSrcweir sal_uInt32 nMask = ( ( 1 << mnPngDepth ) - 1 );
933cdf0e10cSrcweir sal_uInt16 nCol = ( *maDataIter++ << 8 );
934cdf0e10cSrcweir
935cdf0e10cSrcweir nCol += *maDataIter++ & (sal_uInt16)nMask;
936cdf0e10cSrcweir
937cdf0e10cSrcweir if ( mnPngDepth > 8 ) // convert 16bit graphics to 8
938cdf0e10cSrcweir nCol >>= 8;
939cdf0e10cSrcweir
940cdf0e10cSrcweir return (sal_uInt8) nCol;
941cdf0e10cSrcweir }
942cdf0e10cSrcweir
943cdf0e10cSrcweir // ------------------------------------------------------------------------
944cdf0e10cSrcweir // ImplReadIDAT reads as much image data as needed
945cdf0e10cSrcweir
ImplReadIDAT()946cdf0e10cSrcweir void PNGReaderImpl::ImplReadIDAT()
947cdf0e10cSrcweir {
948cdf0e10cSrcweir if( mnChunkLen > 0 )
949cdf0e10cSrcweir {
950cdf0e10cSrcweir if ( mbzCodecInUse == sal_False )
951cdf0e10cSrcweir {
952cdf0e10cSrcweir mbzCodecInUse = sal_True;
953cdf0e10cSrcweir mpZCodec->BeginCompression( ZCODEC_PNG_DEFAULT );
954cdf0e10cSrcweir }
955cdf0e10cSrcweir mpZCodec->SetBreak( mnChunkLen );
956cdf0e10cSrcweir SvMemoryStream aIStrm( &(*maDataIter), mnChunkLen, STREAM_READ );
957cdf0e10cSrcweir
958cdf0e10cSrcweir while ( ( mpZCodec->GetBreak() ) )
959cdf0e10cSrcweir {
960cdf0e10cSrcweir // get bytes needed to fill the current scanline
961cdf0e10cSrcweir sal_Int32 nToRead = mnScansize - (mpScanCurrent - mpInflateInBuf);
962cdf0e10cSrcweir sal_Int32 nRead = mpZCodec->ReadAsynchron( aIStrm, mpScanCurrent, nToRead );
963cdf0e10cSrcweir if ( nRead < 0 )
964cdf0e10cSrcweir {
965cdf0e10cSrcweir mbStatus = sal_False;
966cdf0e10cSrcweir break;
967cdf0e10cSrcweir }
968cdf0e10cSrcweir if ( nRead < nToRead )
969cdf0e10cSrcweir {
970cdf0e10cSrcweir mpScanCurrent += nRead; // more ZStream data in the next IDAT chunk
971cdf0e10cSrcweir break;
972cdf0e10cSrcweir }
973cdf0e10cSrcweir else // this scanline is Finished
974cdf0e10cSrcweir {
975cdf0e10cSrcweir mpScanCurrent = mpInflateInBuf;
976cdf0e10cSrcweir ImplApplyFilter();
977cdf0e10cSrcweir
978cdf0e10cSrcweir ImplDrawScanline( mnXStart, mnXAdd );
979cdf0e10cSrcweir mnYpos += mnYAdd;
980cdf0e10cSrcweir }
981cdf0e10cSrcweir
982cdf0e10cSrcweir if ( mnYpos >= (sal_uInt32)maOrigSize.Height() )
983cdf0e10cSrcweir {
984cdf0e10cSrcweir if( (mnPass < 7) && mnInterlaceType )
985cdf0e10cSrcweir if( ImplPreparePass() )
986cdf0e10cSrcweir continue;
987cdf0e10cSrcweir mbIDAT = true;
988cdf0e10cSrcweir break;
989cdf0e10cSrcweir }
990cdf0e10cSrcweir }
991cdf0e10cSrcweir }
992cdf0e10cSrcweir
993cdf0e10cSrcweir if( mbIDAT )
994cdf0e10cSrcweir {
995cdf0e10cSrcweir mpZCodec->EndCompression();
996cdf0e10cSrcweir mbzCodecInUse = sal_False;
997cdf0e10cSrcweir }
998cdf0e10cSrcweir }
999cdf0e10cSrcweir
1000cdf0e10cSrcweir // ---------------------------------------------------------------------------------------------------
1001cdf0e10cSrcweir
ImplPreparePass()1002cdf0e10cSrcweir bool PNGReaderImpl::ImplPreparePass()
1003cdf0e10cSrcweir {
1004cdf0e10cSrcweir struct InterlaceParams{ int mnXStart, mnYStart, mnXAdd, mnYAdd; };
1005cdf0e10cSrcweir static const InterlaceParams aInterlaceParams[8] =
1006cdf0e10cSrcweir {
1007cdf0e10cSrcweir // non-interlaced
1008cdf0e10cSrcweir { 0, 0, 1, 1 },
1009cdf0e10cSrcweir // Adam7-interlaced
1010cdf0e10cSrcweir { 0, 0, 8, 8 }, // pass 1
1011cdf0e10cSrcweir { 4, 0, 8, 8 }, // pass 2
1012cdf0e10cSrcweir { 0, 4, 4, 8 }, // pass 3
1013cdf0e10cSrcweir { 2, 0, 4, 4 }, // pass 4
1014cdf0e10cSrcweir { 0, 2, 2, 4 }, // pass 5
1015cdf0e10cSrcweir { 1, 0, 2, 2 }, // pass 6
1016cdf0e10cSrcweir { 0, 1, 1, 2 } // pass 7
1017cdf0e10cSrcweir };
1018cdf0e10cSrcweir
1019cdf0e10cSrcweir const InterlaceParams* pParam = &aInterlaceParams[ 0 ];
1020cdf0e10cSrcweir if( mnInterlaceType )
1021cdf0e10cSrcweir {
1022cdf0e10cSrcweir while( ++mnPass <= 7 )
1023cdf0e10cSrcweir {
1024cdf0e10cSrcweir pParam = &aInterlaceParams[ mnPass ];
1025cdf0e10cSrcweir
1026cdf0e10cSrcweir // skip this pass if the original image is too small for it
1027cdf0e10cSrcweir if( (pParam->mnXStart < maOrigSize.Width())
1028cdf0e10cSrcweir && (pParam->mnYStart < maOrigSize.Height()) )
1029cdf0e10cSrcweir break;
1030cdf0e10cSrcweir }
1031cdf0e10cSrcweir if( mnPass > 7 )
1032cdf0e10cSrcweir return false;
1033cdf0e10cSrcweir
1034cdf0e10cSrcweir // skip the last passes if possible (for scaled down target images)
1035cdf0e10cSrcweir if( mnPreviewMask & (pParam->mnXStart | pParam->mnYStart) )
1036cdf0e10cSrcweir return false;
1037cdf0e10cSrcweir }
1038cdf0e10cSrcweir
1039cdf0e10cSrcweir mnYpos = pParam->mnYStart;
1040cdf0e10cSrcweir mnXStart = pParam->mnXStart;
1041cdf0e10cSrcweir mnXAdd = pParam->mnXAdd;
1042cdf0e10cSrcweir mnYAdd = pParam->mnYAdd;
1043cdf0e10cSrcweir
1044cdf0e10cSrcweir // in Interlace mode the size of scanline is not constant
1045cdf0e10cSrcweir // so first we calculate the number of entrys
1046cdf0e10cSrcweir long nScanWidth = (maOrigSize.Width() - mnXStart + mnXAdd - 1) / mnXAdd;
1047cdf0e10cSrcweir mnScansize = nScanWidth;
1048cdf0e10cSrcweir
1049cdf0e10cSrcweir if( mbRGBTriple )
1050cdf0e10cSrcweir mnScansize = 3 * nScanWidth;
1051cdf0e10cSrcweir
1052cdf0e10cSrcweir if( mbAlphaChannel )
1053cdf0e10cSrcweir mnScansize += nScanWidth;
1054cdf0e10cSrcweir
1055cdf0e10cSrcweir // convert to width in bytes
1056cdf0e10cSrcweir mnScansize = ( mnScansize*mnPngDepth + 7 ) >> 3;
1057cdf0e10cSrcweir
1058cdf0e10cSrcweir ++mnScansize; // scan size also needs room for the filtertype byte
1059cdf0e10cSrcweir rtl_zeroMemory( mpScanPrior, mnScansize );
1060cdf0e10cSrcweir
1061cdf0e10cSrcweir return true;
1062cdf0e10cSrcweir }
1063cdf0e10cSrcweir
1064cdf0e10cSrcweir // ----------------------------------------------------------------------------
1065cdf0e10cSrcweir // ImplApplyFilter writes the complete Scanline (nY)
1066cdf0e10cSrcweir // in interlace mode the parameter nXStart and nXAdd are non-zero
1067cdf0e10cSrcweir
ImplApplyFilter()1068cdf0e10cSrcweir void PNGReaderImpl::ImplApplyFilter()
1069cdf0e10cSrcweir {
1070cdf0e10cSrcweir OSL_ASSERT( mnScansize >= mnBPP + 1 );
1071cdf0e10cSrcweir const sal_uInt8* const pScanEnd = mpInflateInBuf + mnScansize;
1072cdf0e10cSrcweir
1073cdf0e10cSrcweir sal_uInt8 nFilterType = *mpInflateInBuf; // the filter type may change each scanline
1074cdf0e10cSrcweir switch ( nFilterType )
1075cdf0e10cSrcweir {
1076cdf0e10cSrcweir default: // unknown Scanline Filter Type
1077cdf0e10cSrcweir case 0: // Filter Type "None"
1078cdf0e10cSrcweir // we let the pixels pass and display the data unfiltered
1079cdf0e10cSrcweir break;
1080cdf0e10cSrcweir
1081cdf0e10cSrcweir case 1: // Scanline Filter Type "Sub"
1082cdf0e10cSrcweir {
1083cdf0e10cSrcweir sal_uInt8* p1 = mpInflateInBuf + 1;
1084cdf0e10cSrcweir const sal_uInt8* p2 = p1;
1085cdf0e10cSrcweir p1 += mnBPP;
1086cdf0e10cSrcweir
1087cdf0e10cSrcweir // use left pixels
1088cdf0e10cSrcweir do
1089cdf0e10cSrcweir *p1 = static_cast<sal_uInt8>( *p1 + *(p2++) );
1090cdf0e10cSrcweir while( ++p1 < pScanEnd );
1091cdf0e10cSrcweir }
1092cdf0e10cSrcweir break;
1093cdf0e10cSrcweir
1094cdf0e10cSrcweir case 2: // Scanline Filter Type "Up"
1095cdf0e10cSrcweir {
1096cdf0e10cSrcweir sal_uInt8* p1 = mpInflateInBuf + 1;
1097cdf0e10cSrcweir const sal_uInt8* p2 = mpScanPrior + 1;
1098cdf0e10cSrcweir
1099cdf0e10cSrcweir // use pixels from prior line
1100cdf0e10cSrcweir while( p1 < pScanEnd )
1101cdf0e10cSrcweir {
1102cdf0e10cSrcweir *p1 = static_cast<sal_uInt8>( *p1 + *(p2++) );
1103cdf0e10cSrcweir ++p1;
1104cdf0e10cSrcweir }
1105cdf0e10cSrcweir }
1106cdf0e10cSrcweir break;
1107cdf0e10cSrcweir
1108cdf0e10cSrcweir case 3: // Scanline Filter Type "Average"
1109cdf0e10cSrcweir {
1110cdf0e10cSrcweir sal_uInt8* p1 = mpInflateInBuf + 1;
1111cdf0e10cSrcweir const sal_uInt8* p2 = mpScanPrior + 1;
1112cdf0e10cSrcweir const sal_uInt8* p3 = p1;
1113cdf0e10cSrcweir
1114cdf0e10cSrcweir // use one pixel from prior line
1115cdf0e10cSrcweir for( int n = mnBPP; --n >= 0; ++p1, ++p2)
1116cdf0e10cSrcweir *p1 = static_cast<sal_uInt8>( *p1 + (*p2 >> 1) );
1117cdf0e10cSrcweir
1118cdf0e10cSrcweir // predict by averaging the left and prior line pixels
1119cdf0e10cSrcweir while( p1 < pScanEnd )
1120cdf0e10cSrcweir {
1121cdf0e10cSrcweir *p1 = static_cast<sal_uInt8>( *p1 + ((*(p2++) + *(p3++)) >> 1) );
1122cdf0e10cSrcweir ++p1;
1123cdf0e10cSrcweir }
1124cdf0e10cSrcweir }
1125cdf0e10cSrcweir break;
1126cdf0e10cSrcweir
1127cdf0e10cSrcweir case 4: // Scanline Filter Type "PaethPredictor"
1128cdf0e10cSrcweir {
1129cdf0e10cSrcweir sal_uInt8* p1 = mpInflateInBuf + 1;
1130cdf0e10cSrcweir const sal_uInt8* p2 = mpScanPrior + 1;
1131cdf0e10cSrcweir const sal_uInt8* p3 = p1;
1132cdf0e10cSrcweir const sal_uInt8* p4 = p2;
1133cdf0e10cSrcweir
1134cdf0e10cSrcweir // use one pixel from prior line
1135cdf0e10cSrcweir for( int n = mnBPP; --n >= 0; ++p1)
1136cdf0e10cSrcweir *p1 = static_cast<sal_uInt8>( *p1 + *(p2++) );
1137cdf0e10cSrcweir
1138cdf0e10cSrcweir // predict by using the left and the prior line pixels
1139cdf0e10cSrcweir while( p1 < pScanEnd )
1140cdf0e10cSrcweir {
1141cdf0e10cSrcweir int na = *(p2++);
1142cdf0e10cSrcweir int nb = *(p3++);
1143cdf0e10cSrcweir int nc = *(p4++);
1144cdf0e10cSrcweir
1145cdf0e10cSrcweir int npa = nb - (int)nc;
1146cdf0e10cSrcweir int npb = na - (int)nc;
1147cdf0e10cSrcweir int npc = npa + npb;
1148cdf0e10cSrcweir
1149cdf0e10cSrcweir if( npa < 0 )
1150cdf0e10cSrcweir npa =-npa;
1151cdf0e10cSrcweir if( npb < 0 )
1152cdf0e10cSrcweir npb =-npb;
1153cdf0e10cSrcweir if( npc < 0 )
1154cdf0e10cSrcweir npc =-npc;
1155cdf0e10cSrcweir
1156cdf0e10cSrcweir if( npa > npb )
1157cdf0e10cSrcweir na = nb, npa = npb;
1158cdf0e10cSrcweir if( npa > npc )
1159cdf0e10cSrcweir na = nc;
1160cdf0e10cSrcweir
1161cdf0e10cSrcweir *p1 = static_cast<sal_uInt8>( *p1 + na );
1162cdf0e10cSrcweir ++p1;
1163cdf0e10cSrcweir }
1164cdf0e10cSrcweir }
1165cdf0e10cSrcweir break;
1166cdf0e10cSrcweir }
1167cdf0e10cSrcweir
1168cdf0e10cSrcweir rtl_copyMemory( mpScanPrior, mpInflateInBuf, mnScansize );
1169cdf0e10cSrcweir }
1170cdf0e10cSrcweir
1171cdf0e10cSrcweir // ---------------------------------------------------------------------------------------------------
1172cdf0e10cSrcweir // ImplDrawScanlines draws the complete Scanline (nY) into the target bitmap
1173cdf0e10cSrcweir // In interlace mode the parameter nXStart and nXAdd append to the currently used pass
1174cdf0e10cSrcweir
ImplDrawScanline(sal_uInt32 nXStart,sal_uInt32 nXAdd)1175cdf0e10cSrcweir void PNGReaderImpl::ImplDrawScanline( sal_uInt32 nXStart, sal_uInt32 nXAdd )
1176cdf0e10cSrcweir {
1177cdf0e10cSrcweir // optimization for downscaling
1178cdf0e10cSrcweir if( mnYpos & mnPreviewMask )
1179cdf0e10cSrcweir return;
1180cdf0e10cSrcweir if( nXStart & mnPreviewMask )
1181cdf0e10cSrcweir return;
1182cdf0e10cSrcweir
1183cdf0e10cSrcweir // convert nY to pixel units in the target image
1184cdf0e10cSrcweir // => TODO; also do this for nX here instead of in the ImplSet*Pixel() methods
1185cdf0e10cSrcweir const sal_uInt32 nY = mnYpos >> mnPreviewShift;
1186cdf0e10cSrcweir
1187cdf0e10cSrcweir const sal_uInt8* pTmp = mpInflateInBuf + 1;
1188cdf0e10cSrcweir if ( mpAcc->HasPalette() ) // alphachannel is not allowed by pictures including palette entries
1189cdf0e10cSrcweir {
1190cdf0e10cSrcweir switch ( mpAcc->GetBitCount() )
1191cdf0e10cSrcweir {
1192cdf0e10cSrcweir case 1 :
1193cdf0e10cSrcweir {
1194cdf0e10cSrcweir if ( mbTransparent )
1195cdf0e10cSrcweir {
1196cdf0e10cSrcweir for ( sal_Int32 nX = nXStart, nShift = 0; nX < maOrigSize.Width(); nX += nXAdd )
1197cdf0e10cSrcweir {
1198cdf0e10cSrcweir sal_uInt8 nCol;
1199cdf0e10cSrcweir nShift = (nShift - 1) & 7;
1200cdf0e10cSrcweir if ( nShift == 0 )
1201cdf0e10cSrcweir nCol = *(pTmp++);
1202cdf0e10cSrcweir else
1203cdf0e10cSrcweir nCol = static_cast<sal_uInt8>( *pTmp >> nShift );
1204cdf0e10cSrcweir nCol &= 1;
1205cdf0e10cSrcweir
1206cdf0e10cSrcweir ImplSetAlphaPixel( nY, nX, nCol, mpTransTab[ nCol ] );
1207cdf0e10cSrcweir }
1208cdf0e10cSrcweir }
1209cdf0e10cSrcweir else
1210cdf0e10cSrcweir { // BMP_FORMAT_1BIT_MSB_PAL
1211cdf0e10cSrcweir for ( sal_Int32 nX = nXStart, nShift = 0; nX < maOrigSize.Width(); nX += nXAdd )
1212cdf0e10cSrcweir {
1213cdf0e10cSrcweir nShift = (nShift - 1) & 7;
1214cdf0e10cSrcweir
1215cdf0e10cSrcweir sal_uInt8 nCol;
1216cdf0e10cSrcweir if ( nShift == 0 )
1217cdf0e10cSrcweir nCol = *(pTmp++);
1218cdf0e10cSrcweir else
1219cdf0e10cSrcweir nCol = static_cast<sal_uInt8>( *pTmp >> nShift );
1220cdf0e10cSrcweir nCol &= 1;
1221cdf0e10cSrcweir
1222cdf0e10cSrcweir ImplSetPixel( nY, nX, nCol );
1223cdf0e10cSrcweir }
1224cdf0e10cSrcweir }
1225cdf0e10cSrcweir }
1226cdf0e10cSrcweir break;
1227cdf0e10cSrcweir
1228cdf0e10cSrcweir case 4 :
1229cdf0e10cSrcweir {
1230cdf0e10cSrcweir if ( mbTransparent )
1231cdf0e10cSrcweir {
1232cdf0e10cSrcweir if ( mnPngDepth == 4 ) // check if source has a two bit pixel format
1233cdf0e10cSrcweir {
1234cdf0e10cSrcweir for ( sal_Int32 nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, ++nXIndex )
1235cdf0e10cSrcweir {
1236cdf0e10cSrcweir if( nXIndex & 1 )
1237cdf0e10cSrcweir {
1238cdf0e10cSrcweir ImplSetAlphaPixel( nY, nX, *pTmp & 0x0f, mpTransTab[ *pTmp & 0x0f ] );
1239cdf0e10cSrcweir pTmp++;
1240cdf0e10cSrcweir }
1241cdf0e10cSrcweir else
1242cdf0e10cSrcweir {
1243cdf0e10cSrcweir ImplSetAlphaPixel( nY, nX, ( *pTmp >> 4 ) & 0x0f, mpTransTab[ *pTmp >> 4 ] );
1244cdf0e10cSrcweir }
1245cdf0e10cSrcweir }
1246cdf0e10cSrcweir }
1247cdf0e10cSrcweir else // if ( mnPngDepth == 2 )
1248cdf0e10cSrcweir {
1249cdf0e10cSrcweir for ( sal_Int32 nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, nXIndex++ )
1250cdf0e10cSrcweir {
1251cdf0e10cSrcweir sal_uInt8 nCol;
1252cdf0e10cSrcweir switch( nXIndex & 3 )
1253cdf0e10cSrcweir {
1254cdf0e10cSrcweir case 0 :
1255cdf0e10cSrcweir nCol = *pTmp >> 6;
1256cdf0e10cSrcweir break;
1257cdf0e10cSrcweir
1258cdf0e10cSrcweir case 1 :
1259cdf0e10cSrcweir nCol = ( *pTmp >> 4 ) & 0x03 ;
1260cdf0e10cSrcweir break;
1261cdf0e10cSrcweir
1262cdf0e10cSrcweir case 2 :
1263cdf0e10cSrcweir nCol = ( *pTmp >> 2 ) & 0x03;
1264cdf0e10cSrcweir break;
1265cdf0e10cSrcweir
1266cdf0e10cSrcweir case 3 :
1267cdf0e10cSrcweir nCol = ( *pTmp++ ) & 0x03;
1268cdf0e10cSrcweir break;
1269cdf0e10cSrcweir
1270cdf0e10cSrcweir default: // get rid of nCol uninitialized warning
1271cdf0e10cSrcweir nCol = 0;
1272cdf0e10cSrcweir break;
1273cdf0e10cSrcweir }
1274cdf0e10cSrcweir
1275cdf0e10cSrcweir ImplSetAlphaPixel( nY, nX, nCol, mpTransTab[ nCol ] );
1276cdf0e10cSrcweir }
1277cdf0e10cSrcweir }
1278cdf0e10cSrcweir }
1279cdf0e10cSrcweir else
1280cdf0e10cSrcweir {
1281cdf0e10cSrcweir if ( mnPngDepth == 4 ) // maybe the source is a two bitmap graphic
1282cdf0e10cSrcweir { // BMP_FORMAT_4BIT_LSN_PAL
1283cdf0e10cSrcweir for ( sal_Int32 nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, nXIndex++ )
1284cdf0e10cSrcweir {
1285cdf0e10cSrcweir if( nXIndex & 1 )
1286cdf0e10cSrcweir ImplSetPixel( nY, nX, *pTmp++ & 0x0f );
1287cdf0e10cSrcweir else
1288cdf0e10cSrcweir ImplSetPixel( nY, nX, ( *pTmp >> 4 ) & 0x0f );
1289cdf0e10cSrcweir }
1290cdf0e10cSrcweir }
1291cdf0e10cSrcweir else // if ( mnPngDepth == 2 )
1292cdf0e10cSrcweir {
1293cdf0e10cSrcweir for ( sal_Int32 nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, nXIndex++ )
1294cdf0e10cSrcweir {
1295cdf0e10cSrcweir switch( nXIndex & 3 )
1296cdf0e10cSrcweir {
1297cdf0e10cSrcweir case 0 :
1298cdf0e10cSrcweir ImplSetPixel( nY, nX, *pTmp >> 6 );
1299cdf0e10cSrcweir break;
1300cdf0e10cSrcweir
1301cdf0e10cSrcweir case 1 :
1302cdf0e10cSrcweir ImplSetPixel( nY, nX, ( *pTmp >> 4 ) & 0x03 );
1303cdf0e10cSrcweir break;
1304cdf0e10cSrcweir
1305cdf0e10cSrcweir case 2 :
1306cdf0e10cSrcweir ImplSetPixel( nY, nX, ( *pTmp >> 2 ) & 0x03 );
1307cdf0e10cSrcweir break;
1308cdf0e10cSrcweir
1309cdf0e10cSrcweir case 3 :
1310cdf0e10cSrcweir ImplSetPixel( nY, nX, *pTmp++ & 0x03 );
1311cdf0e10cSrcweir break;
1312cdf0e10cSrcweir }
1313cdf0e10cSrcweir }
1314cdf0e10cSrcweir }
1315cdf0e10cSrcweir }
1316cdf0e10cSrcweir }
1317cdf0e10cSrcweir break;
1318cdf0e10cSrcweir
1319cdf0e10cSrcweir case 8 :
1320cdf0e10cSrcweir {
1321cdf0e10cSrcweir if ( mbAlphaChannel )
1322cdf0e10cSrcweir {
1323cdf0e10cSrcweir if ( mnPngDepth == 8 ) // maybe the source is a 16 bit grayscale
1324cdf0e10cSrcweir {
1325cdf0e10cSrcweir for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 2 )
1326cdf0e10cSrcweir ImplSetAlphaPixel( nY, nX, pTmp[ 0 ], pTmp[ 1 ] );
1327cdf0e10cSrcweir }
1328cdf0e10cSrcweir else
1329cdf0e10cSrcweir {
1330cdf0e10cSrcweir for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 4 )
1331cdf0e10cSrcweir ImplSetAlphaPixel( nY, nX, pTmp[ 0 ], pTmp[ 2 ] );
1332cdf0e10cSrcweir }
1333cdf0e10cSrcweir }
1334cdf0e10cSrcweir else if ( mbTransparent )
1335cdf0e10cSrcweir {
1336cdf0e10cSrcweir if ( mnPngDepth == 8 ) // maybe the source is a 16 bit grayscale
1337cdf0e10cSrcweir {
1338cdf0e10cSrcweir for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp++ )
1339cdf0e10cSrcweir ImplSetAlphaPixel( nY, nX, *pTmp, mpTransTab[ *pTmp ] );
1340cdf0e10cSrcweir }
1341cdf0e10cSrcweir else
1342cdf0e10cSrcweir {
1343cdf0e10cSrcweir for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 2 )
1344cdf0e10cSrcweir ImplSetAlphaPixel( nY, nX, *pTmp, mpTransTab[ *pTmp ] );
1345cdf0e10cSrcweir }
1346cdf0e10cSrcweir }
1347cdf0e10cSrcweir else // neither alpha nor transparency
1348cdf0e10cSrcweir {
1349cdf0e10cSrcweir if ( mnPngDepth == 8 ) // maybe the source is a 16 bit grayscale
1350cdf0e10cSrcweir {
1351cdf0e10cSrcweir if( nXAdd == 1 && mnPreviewShift == 0 ) // copy raw line data if possible
1352cdf0e10cSrcweir {
1353cdf0e10cSrcweir int nLineBytes = maOrigSize.Width();
1354cdf0e10cSrcweir mpAcc->CopyScanline( nY, pTmp, BMP_FORMAT_8BIT_PAL, nLineBytes );
1355cdf0e10cSrcweir pTmp += nLineBytes;
1356cdf0e10cSrcweir }
1357cdf0e10cSrcweir else
1358cdf0e10cSrcweir {
1359cdf0e10cSrcweir for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd )
1360cdf0e10cSrcweir ImplSetPixel( nY, nX, *pTmp++ );
1361cdf0e10cSrcweir }
1362cdf0e10cSrcweir }
1363cdf0e10cSrcweir else
1364cdf0e10cSrcweir {
1365cdf0e10cSrcweir for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 2 )
1366cdf0e10cSrcweir ImplSetPixel( nY, nX, *pTmp );
1367cdf0e10cSrcweir }
1368cdf0e10cSrcweir }
1369cdf0e10cSrcweir }
1370cdf0e10cSrcweir break;
1371cdf0e10cSrcweir
1372cdf0e10cSrcweir default :
1373cdf0e10cSrcweir mbStatus = sal_False;
1374cdf0e10cSrcweir break;
1375cdf0e10cSrcweir }
1376cdf0e10cSrcweir }
1377cdf0e10cSrcweir else // no palette => truecolor
1378cdf0e10cSrcweir {
137988b53a7cSArmin Le Grand // #122985# Added fast-lane implementations using CopyScanline with direct supported mem formats
138088b53a7cSArmin Le Grand static bool bCkeckDirectScanline(true);
138188b53a7cSArmin Le Grand
138288b53a7cSArmin Le Grand if( mbAlphaChannel )
138388b53a7cSArmin Le Grand {
138488b53a7cSArmin Le Grand // has RGB + alpha
138588b53a7cSArmin Le Grand if ( mnPngDepth == 8 ) // maybe the source has 16 bit per sample
138688b53a7cSArmin Le Grand {
138788b53a7cSArmin Le Grand // BMP_FORMAT_32BIT_TC_RGBA
138888b53a7cSArmin Le Grand // only use DirectScanline when we have no preview shifting stuff and accesses to content and alpha
138988b53a7cSArmin Le Grand const bool bDoDirectScanline(
139088b53a7cSArmin Le Grand bCkeckDirectScanline && !nXStart && 1 == nXAdd && !mnPreviewShift && mpAcc && mpMaskAcc);
139188b53a7cSArmin Le Grand const bool bCustomColorTable(mpColorTable != mpDefaultColorTable);
139288b53a7cSArmin Le Grand
139388b53a7cSArmin Le Grand if(bDoDirectScanline)
1394cdf0e10cSrcweir {
139588b53a7cSArmin Le Grand // allocate scanlines on demand, reused for next line
139688b53a7cSArmin Le Grand if(!mpScanline)
139788b53a7cSArmin Le Grand {
139888b53a7cSArmin Le Grand #ifdef DBG_UTIL
139988b53a7cSArmin Le Grand mnAllocSizeScanline = maOrigSize.Width() * 3;
140088b53a7cSArmin Le Grand #endif
140188b53a7cSArmin Le Grand mpScanline = new sal_uInt8[maOrigSize.Width() * 3];
140288b53a7cSArmin Le Grand }
140388b53a7cSArmin Le Grand
140488b53a7cSArmin Le Grand if(!mpScanlineAlpha)
140588b53a7cSArmin Le Grand {
140688b53a7cSArmin Le Grand #ifdef DBG_UTIL
140788b53a7cSArmin Le Grand mnAllocSizeScanlineAlpha = maOrigSize.Width();
140888b53a7cSArmin Le Grand #endif
140988b53a7cSArmin Le Grand mpScanlineAlpha = new sal_uInt8[maOrigSize.Width()];
141088b53a7cSArmin Le Grand }
141188b53a7cSArmin Le Grand }
141288b53a7cSArmin Le Grand
141388b53a7cSArmin Le Grand if(bDoDirectScanline)
141488b53a7cSArmin Le Grand {
141588b53a7cSArmin Le Grand OSL_ENSURE(mpScanline, "No Scanline allocated (!)");
141688b53a7cSArmin Le Grand OSL_ENSURE(mpScanlineAlpha, "No ScanlineAlpha allocated (!)");
141759c7f95bSHerbert Dürr #ifdef DBG_UTIL
141888b53a7cSArmin Le Grand OSL_ENSURE(mnAllocSizeScanline >= maOrigSize.Width() * 3, "Allocated Scanline too small (!)");
141988b53a7cSArmin Le Grand OSL_ENSURE(mnAllocSizeScanlineAlpha >= maOrigSize.Width(), "Allocated ScanlineAlpha too small (!)");
142059c7f95bSHerbert Dürr #endif
142188b53a7cSArmin Le Grand sal_uInt8* pScanline(mpScanline);
142288b53a7cSArmin Le Grand sal_uInt8* pScanlineAlpha(mpScanlineAlpha);
142388b53a7cSArmin Le Grand
1424*f037e03fSPavel Janík for(sal_Int32 nX(0); nX < maOrigSize.Width(); nX++, pTmp += 4)
142588b53a7cSArmin Le Grand {
142688b53a7cSArmin Le Grand // prepare content line as BGR by reordering when copying
142788b53a7cSArmin Le Grand // do not forget to invert alpha (source is alpha, target is opacity)
142888b53a7cSArmin Le Grand if(bCustomColorTable)
142988b53a7cSArmin Le Grand {
143088b53a7cSArmin Le Grand *pScanline++ = mpColorTable[pTmp[2]];
143188b53a7cSArmin Le Grand *pScanline++ = mpColorTable[pTmp[1]];
143288b53a7cSArmin Le Grand *pScanline++ = mpColorTable[pTmp[0]];
143388b53a7cSArmin Le Grand *pScanlineAlpha++ = ~pTmp[3];
143488b53a7cSArmin Le Grand }
143588b53a7cSArmin Le Grand else
143688b53a7cSArmin Le Grand {
143788b53a7cSArmin Le Grand *pScanline++ = pTmp[2];
143888b53a7cSArmin Le Grand *pScanline++ = pTmp[1];
143988b53a7cSArmin Le Grand *pScanline++ = pTmp[0];
144088b53a7cSArmin Le Grand *pScanlineAlpha++ = ~pTmp[3];
144188b53a7cSArmin Le Grand }
144288b53a7cSArmin Le Grand }
144388b53a7cSArmin Le Grand
144488b53a7cSArmin Le Grand // copy scanlines directly to bitmaps for content and alpha; use the formats which
144588b53a7cSArmin Le Grand // are able to copy directly to BitmapBuffer
144688b53a7cSArmin Le Grand mpAcc->CopyScanline(nY, mpScanline, BMP_FORMAT_24BIT_TC_BGR, maOrigSize.Width() * 3);
144788b53a7cSArmin Le Grand mpMaskAcc->CopyScanline(nY, mpScanlineAlpha, BMP_FORMAT_8BIT_PAL, maOrigSize.Width());
1448cdf0e10cSrcweir }
1449cdf0e10cSrcweir else
1450cdf0e10cSrcweir {
145188b53a7cSArmin Le Grand for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 4 )
1452cdf0e10cSrcweir {
145388b53a7cSArmin Le Grand if(bCustomColorTable)
145488b53a7cSArmin Le Grand {
145588b53a7cSArmin Le Grand ImplSetAlphaPixel(
145688b53a7cSArmin Le Grand nY,
145788b53a7cSArmin Le Grand nX,
145888b53a7cSArmin Le Grand BitmapColor(
145988b53a7cSArmin Le Grand mpColorTable[ pTmp[ 0 ] ],
146088b53a7cSArmin Le Grand mpColorTable[ pTmp[ 1 ] ],
146188b53a7cSArmin Le Grand mpColorTable[ pTmp[ 2 ] ]),
146288b53a7cSArmin Le Grand pTmp[ 3 ]);
146388b53a7cSArmin Le Grand }
146488b53a7cSArmin Le Grand else
146588b53a7cSArmin Le Grand {
146688b53a7cSArmin Le Grand ImplSetAlphaPixel(
146788b53a7cSArmin Le Grand nY,
146888b53a7cSArmin Le Grand nX,
146988b53a7cSArmin Le Grand BitmapColor(
147088b53a7cSArmin Le Grand pTmp[0],
147188b53a7cSArmin Le Grand pTmp[1],
147288b53a7cSArmin Le Grand pTmp[2]),
147388b53a7cSArmin Le Grand pTmp[3]);
147488b53a7cSArmin Le Grand }
1475cdf0e10cSrcweir }
1476cdf0e10cSrcweir }
147788b53a7cSArmin Le Grand }
147888b53a7cSArmin Le Grand else
147988b53a7cSArmin Le Grand {
148088b53a7cSArmin Le Grand // BMP_FORMAT_64BIT_TC_RGBA
148188b53a7cSArmin Le Grand for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 8 )
1482cdf0e10cSrcweir {
148388b53a7cSArmin Le Grand ImplSetAlphaPixel(
148488b53a7cSArmin Le Grand nY,
148588b53a7cSArmin Le Grand nX,
148688b53a7cSArmin Le Grand BitmapColor(
148788b53a7cSArmin Le Grand mpColorTable[ pTmp[ 0 ] ],
148888b53a7cSArmin Le Grand mpColorTable[ pTmp[ 2 ] ],
148988b53a7cSArmin Le Grand mpColorTable[ pTmp[ 4 ] ]),
149088b53a7cSArmin Le Grand pTmp[6]);
1491cdf0e10cSrcweir }
149288b53a7cSArmin Le Grand }
149388b53a7cSArmin Le Grand }
149488b53a7cSArmin Le Grand else if( mbTransparent ) // has RGB + transparency
149588b53a7cSArmin Le Grand {
149688b53a7cSArmin Le Grand // BMP_FORMAT_24BIT_TC_RGB
149788b53a7cSArmin Le Grand // no support currently for DirectScanline, found no real usages in current PNGs, may be added on demand
149888b53a7cSArmin Le Grand if ( mnPngDepth == 8 ) // maybe the source has 16 bit per sample
149988b53a7cSArmin Le Grand {
150088b53a7cSArmin Le Grand for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 3 )
150188b53a7cSArmin Le Grand {
150288b53a7cSArmin Le Grand sal_uInt8 nRed = pTmp[ 0 ];
150388b53a7cSArmin Le Grand sal_uInt8 nGreen = pTmp[ 1 ];
150488b53a7cSArmin Le Grand sal_uInt8 nBlue = pTmp[ 2 ];
150588b53a7cSArmin Le Grand sal_Bool bTransparent = ( ( nRed == mnTransRed )
150688b53a7cSArmin Le Grand && ( nGreen == mnTransGreen )
150788b53a7cSArmin Le Grand && ( nBlue == mnTransBlue ) );
150888b53a7cSArmin Le Grand
150988b53a7cSArmin Le Grand ImplSetTranspPixel( nY, nX, BitmapColor( mpColorTable[ nRed ],
151088b53a7cSArmin Le Grand mpColorTable[ nGreen ],
151188b53a7cSArmin Le Grand mpColorTable[ nBlue ] ), bTransparent );
151288b53a7cSArmin Le Grand }
151388b53a7cSArmin Le Grand }
151488b53a7cSArmin Le Grand else
151588b53a7cSArmin Le Grand {
151688b53a7cSArmin Le Grand // BMP_FORMAT_48BIT_TC_RGB
151788b53a7cSArmin Le Grand for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 6 )
151888b53a7cSArmin Le Grand {
151988b53a7cSArmin Le Grand sal_uInt8 nRed = pTmp[ 0 ];
152088b53a7cSArmin Le Grand sal_uInt8 nGreen = pTmp[ 2 ];
152188b53a7cSArmin Le Grand sal_uInt8 nBlue = pTmp[ 4 ];
152288b53a7cSArmin Le Grand sal_Bool bTransparent = ( ( nRed == mnTransRed )
152388b53a7cSArmin Le Grand && ( nGreen == mnTransGreen )
152488b53a7cSArmin Le Grand && ( nBlue == mnTransBlue ) );
152588b53a7cSArmin Le Grand
152688b53a7cSArmin Le Grand ImplSetTranspPixel( nY, nX, BitmapColor( mpColorTable[ nRed ],
152788b53a7cSArmin Le Grand mpColorTable[ nGreen ],
152888b53a7cSArmin Le Grand mpColorTable[ nBlue ] ), bTransparent );
152988b53a7cSArmin Le Grand }
153088b53a7cSArmin Le Grand }
153188b53a7cSArmin Le Grand }
153288b53a7cSArmin Le Grand else // has RGB but neither alpha nor transparency
153388b53a7cSArmin Le Grand {
153488b53a7cSArmin Le Grand // BMP_FORMAT_24BIT_TC_RGB
153588b53a7cSArmin Le Grand // only use DirectScanline when we have no preview shifting stuff and access to content
153688b53a7cSArmin Le Grand const bool bDoDirectScanline(
153788b53a7cSArmin Le Grand bCkeckDirectScanline && !nXStart && 1 == nXAdd && !mnPreviewShift && mpAcc);
153888b53a7cSArmin Le Grand const bool bCustomColorTable(mpColorTable != mpDefaultColorTable);
153988b53a7cSArmin Le Grand
154088b53a7cSArmin Le Grand if(bDoDirectScanline && !mpScanline)
154188b53a7cSArmin Le Grand {
154288b53a7cSArmin Le Grand // allocate scanlines on demand, reused for next line
154388b53a7cSArmin Le Grand #ifdef DBG_UTIL
154488b53a7cSArmin Le Grand mnAllocSizeScanline = maOrigSize.Width() * 3;
154588b53a7cSArmin Le Grand #endif
154688b53a7cSArmin Le Grand mpScanline = new sal_uInt8[maOrigSize.Width() * 3];
154788b53a7cSArmin Le Grand }
154888b53a7cSArmin Le Grand
154988b53a7cSArmin Le Grand if ( mnPngDepth == 8 ) // maybe the source has 16 bit per sample
155088b53a7cSArmin Le Grand {
155188b53a7cSArmin Le Grand if(bDoDirectScanline)
1552cdf0e10cSrcweir {
155388b53a7cSArmin Le Grand OSL_ENSURE(mpScanline, "No Scanline allocated (!)");
155459c7f95bSHerbert Dürr #ifdef DBG_UTIL
155588b53a7cSArmin Le Grand OSL_ENSURE(mnAllocSizeScanline >= maOrigSize.Width() * 3, "Allocated Scanline too small (!)");
155659c7f95bSHerbert Dürr #endif
155788b53a7cSArmin Le Grand sal_uInt8* pScanline(mpScanline);
155888b53a7cSArmin Le Grand
1559*f037e03fSPavel Janík for(sal_Int32 nX(0); nX < maOrigSize.Width(); nX++, pTmp += 3)
1560cdf0e10cSrcweir {
156188b53a7cSArmin Le Grand // prepare content line as BGR by reordering when copying
156288b53a7cSArmin Le Grand if(bCustomColorTable)
156388b53a7cSArmin Le Grand {
156488b53a7cSArmin Le Grand *pScanline++ = mpColorTable[pTmp[2]];
156588b53a7cSArmin Le Grand *pScanline++ = mpColorTable[pTmp[1]];
156688b53a7cSArmin Le Grand *pScanline++ = mpColorTable[pTmp[0]];
156788b53a7cSArmin Le Grand }
156888b53a7cSArmin Le Grand else
156988b53a7cSArmin Le Grand {
157088b53a7cSArmin Le Grand *pScanline++ = pTmp[2];
157188b53a7cSArmin Le Grand *pScanline++ = pTmp[1];
157288b53a7cSArmin Le Grand *pScanline++ = pTmp[0];
157388b53a7cSArmin Le Grand }
1574cdf0e10cSrcweir }
157588b53a7cSArmin Le Grand
157688b53a7cSArmin Le Grand // copy scanline directly to bitmap for content; use the format which is able to
157788b53a7cSArmin Le Grand // copy directly to BitmapBuffer
157888b53a7cSArmin Le Grand mpAcc->CopyScanline(nY, mpScanline, BMP_FORMAT_24BIT_TC_BGR, maOrigSize.Width() * 3);
157988b53a7cSArmin Le Grand }
158088b53a7cSArmin Le Grand else
158188b53a7cSArmin Le Grand {
158288b53a7cSArmin Le Grand for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 3 )
1583cdf0e10cSrcweir {
158488b53a7cSArmin Le Grand if(bCustomColorTable)
158588b53a7cSArmin Le Grand {
158688b53a7cSArmin Le Grand ImplSetPixel(
158788b53a7cSArmin Le Grand nY,
158888b53a7cSArmin Le Grand nX,
158988b53a7cSArmin Le Grand BitmapColor(
159088b53a7cSArmin Le Grand mpColorTable[ pTmp[ 0 ] ],
159188b53a7cSArmin Le Grand mpColorTable[ pTmp[ 1 ] ],
159288b53a7cSArmin Le Grand mpColorTable[ pTmp[ 2 ] ]));
159388b53a7cSArmin Le Grand }
159488b53a7cSArmin Le Grand else
159588b53a7cSArmin Le Grand {
159688b53a7cSArmin Le Grand ImplSetPixel(
159788b53a7cSArmin Le Grand nY,
159888b53a7cSArmin Le Grand nX,
159988b53a7cSArmin Le Grand BitmapColor(
160088b53a7cSArmin Le Grand pTmp[0],
160188b53a7cSArmin Le Grand pTmp[1],
160288b53a7cSArmin Le Grand pTmp[2]));
160388b53a7cSArmin Le Grand }
1604cdf0e10cSrcweir }
1605cdf0e10cSrcweir }
160688b53a7cSArmin Le Grand }
160788b53a7cSArmin Le Grand else
160888b53a7cSArmin Le Grand {
160988b53a7cSArmin Le Grand // BMP_FORMAT_48BIT_TC_RGB
161088b53a7cSArmin Le Grand // no support currently for DirectScanline, found no real usages in current PNGs, may be added on demand
161188b53a7cSArmin Le Grand for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 6 )
161288b53a7cSArmin Le Grand {
161388b53a7cSArmin Le Grand ImplSetPixel(
161488b53a7cSArmin Le Grand nY,
161588b53a7cSArmin Le Grand nX,
161688b53a7cSArmin Le Grand BitmapColor(
161788b53a7cSArmin Le Grand mpColorTable[ pTmp[ 0 ] ],
161888b53a7cSArmin Le Grand mpColorTable[ pTmp[ 2 ] ],
161988b53a7cSArmin Le Grand mpColorTable[ pTmp[ 4 ] ]));
162088b53a7cSArmin Le Grand }
162188b53a7cSArmin Le Grand }
162288b53a7cSArmin Le Grand }
162388b53a7cSArmin Le Grand }
1624cdf0e10cSrcweir }
1625cdf0e10cSrcweir
1626cdf0e10cSrcweir // ------------------------------------------------------------------------
1627cdf0e10cSrcweir
ImplSetPixel(sal_uInt32 nY,sal_uInt32 nX,const BitmapColor & rBitmapColor)1628cdf0e10cSrcweir void PNGReaderImpl::ImplSetPixel( sal_uInt32 nY, sal_uInt32 nX, const BitmapColor& rBitmapColor )
1629cdf0e10cSrcweir {
1630cdf0e10cSrcweir // TODO: get preview mode checks out of inner loop
1631cdf0e10cSrcweir if( nX & mnPreviewMask )
1632cdf0e10cSrcweir return;
1633cdf0e10cSrcweir nX >>= mnPreviewShift;
1634cdf0e10cSrcweir
1635cdf0e10cSrcweir mpAcc->SetPixel( nY, nX, rBitmapColor );
1636cdf0e10cSrcweir }
1637cdf0e10cSrcweir
1638cdf0e10cSrcweir // ------------------------------------------------------------------------
1639cdf0e10cSrcweir
ImplSetPixel(sal_uInt32 nY,sal_uInt32 nX,sal_uInt8 nPalIndex)1640cdf0e10cSrcweir void PNGReaderImpl::ImplSetPixel( sal_uInt32 nY, sal_uInt32 nX, sal_uInt8 nPalIndex )
1641cdf0e10cSrcweir {
1642cdf0e10cSrcweir // TODO: get preview mode checks out of inner loop
1643cdf0e10cSrcweir if( nX & mnPreviewMask )
1644cdf0e10cSrcweir return;
1645cdf0e10cSrcweir nX >>= mnPreviewShift;
1646cdf0e10cSrcweir
164787bc88d3SHerbert Dürr mpAcc->SetPixelIndex( nY, nX, nPalIndex );
1648cdf0e10cSrcweir }
1649cdf0e10cSrcweir
1650cdf0e10cSrcweir // ------------------------------------------------------------------------
1651cdf0e10cSrcweir
ImplSetTranspPixel(sal_uInt32 nY,sal_uInt32 nX,const BitmapColor & rBitmapColor,sal_Bool bTrans)1652cdf0e10cSrcweir void PNGReaderImpl::ImplSetTranspPixel( sal_uInt32 nY, sal_uInt32 nX, const BitmapColor& rBitmapColor, sal_Bool bTrans )
1653cdf0e10cSrcweir {
1654cdf0e10cSrcweir // TODO: get preview mode checks out of inner loop
1655cdf0e10cSrcweir if( nX & mnPreviewMask )
1656cdf0e10cSrcweir return;
1657cdf0e10cSrcweir nX >>= mnPreviewShift;
1658cdf0e10cSrcweir
1659cdf0e10cSrcweir mpAcc->SetPixel( nY, nX, rBitmapColor );
1660cdf0e10cSrcweir
1661cdf0e10cSrcweir if ( bTrans )
1662cdf0e10cSrcweir mpMaskAcc->SetPixel( nY, nX, mcTranspColor );
1663cdf0e10cSrcweir else
1664cdf0e10cSrcweir mpMaskAcc->SetPixel( nY, nX, mcOpaqueColor );
1665cdf0e10cSrcweir }
1666cdf0e10cSrcweir
1667cdf0e10cSrcweir // ------------------------------------------------------------------------
1668cdf0e10cSrcweir
ImplSetAlphaPixel(sal_uInt32 nY,sal_uInt32 nX,sal_uInt8 nPalIndex,sal_uInt8 nAlpha)1669cdf0e10cSrcweir void PNGReaderImpl::ImplSetAlphaPixel( sal_uInt32 nY, sal_uInt32 nX,
1670cdf0e10cSrcweir sal_uInt8 nPalIndex, sal_uInt8 nAlpha )
1671cdf0e10cSrcweir {
1672cdf0e10cSrcweir // TODO: get preview mode checks out of inner loop
1673cdf0e10cSrcweir if( nX & mnPreviewMask )
1674cdf0e10cSrcweir return;
1675cdf0e10cSrcweir nX >>= mnPreviewShift;
1676cdf0e10cSrcweir
167787bc88d3SHerbert Dürr mpAcc->SetPixelIndex( nY, nX, nPalIndex );
167887bc88d3SHerbert Dürr mpMaskAcc->SetPixelIndex( nY, nX, ~nAlpha );
1679cdf0e10cSrcweir }
1680cdf0e10cSrcweir
1681cdf0e10cSrcweir // ------------------------------------------------------------------------
1682cdf0e10cSrcweir
ImplSetAlphaPixel(sal_uInt32 nY,sal_uInt32 nX,const BitmapColor & rBitmapColor,sal_uInt8 nAlpha)1683cdf0e10cSrcweir void PNGReaderImpl::ImplSetAlphaPixel( sal_uInt32 nY, sal_uInt32 nX,
1684cdf0e10cSrcweir const BitmapColor& rBitmapColor, sal_uInt8 nAlpha )
1685cdf0e10cSrcweir {
1686cdf0e10cSrcweir // TODO: get preview mode checks out of inner loop
1687cdf0e10cSrcweir if( nX & mnPreviewMask )
1688cdf0e10cSrcweir return;
1689cdf0e10cSrcweir nX >>= mnPreviewShift;
1690cdf0e10cSrcweir
1691cdf0e10cSrcweir mpAcc->SetPixel( nY, nX, rBitmapColor );
169287bc88d3SHerbert Dürr mpMaskAcc->SetPixelIndex( nY, nX, ~nAlpha );
1693cdf0e10cSrcweir }
1694cdf0e10cSrcweir
1695cdf0e10cSrcweir // ------------------------------------------------------------------------
1696cdf0e10cSrcweir
ImplReadsal_uInt32()1697cdf0e10cSrcweir sal_uInt32 PNGReaderImpl::ImplReadsal_uInt32()
1698cdf0e10cSrcweir {
1699cdf0e10cSrcweir sal_uInt32 nRet;
1700cdf0e10cSrcweir nRet = *maDataIter++;
1701cdf0e10cSrcweir nRet <<= 8;
1702cdf0e10cSrcweir nRet |= *maDataIter++;
1703cdf0e10cSrcweir nRet <<= 8;
1704cdf0e10cSrcweir nRet |= *maDataIter++;
1705cdf0e10cSrcweir nRet <<= 8;
1706cdf0e10cSrcweir nRet |= *maDataIter++;
1707cdf0e10cSrcweir return nRet;
1708cdf0e10cSrcweir }
1709cdf0e10cSrcweir
1710cdf0e10cSrcweir // ------------------------------------------------------------------------
1711cdf0e10cSrcweir
1712cdf0e10cSrcweir // -------------
1713cdf0e10cSrcweir // - PNGReader -
1714cdf0e10cSrcweir // -------------
1715cdf0e10cSrcweir
PNGReader(SvStream & rIStm)1716cdf0e10cSrcweir PNGReader::PNGReader( SvStream& rIStm ) :
1717cdf0e10cSrcweir mpImpl( new ::vcl::PNGReaderImpl( rIStm ) )
1718cdf0e10cSrcweir {
1719cdf0e10cSrcweir }
1720cdf0e10cSrcweir
1721cdf0e10cSrcweir // ------------------------------------------------------------------------
1722cdf0e10cSrcweir
~PNGReader()1723cdf0e10cSrcweir PNGReader::~PNGReader()
1724cdf0e10cSrcweir {
1725cdf0e10cSrcweir delete mpImpl;
1726cdf0e10cSrcweir }
1727cdf0e10cSrcweir
1728cdf0e10cSrcweir // ------------------------------------------------------------------------
1729cdf0e10cSrcweir
Read(const Size & i_rPreviewSizeHint)1730cdf0e10cSrcweir BitmapEx PNGReader::Read( const Size& i_rPreviewSizeHint )
1731cdf0e10cSrcweir {
1732cdf0e10cSrcweir return mpImpl->GetBitmapEx( i_rPreviewSizeHint );
1733cdf0e10cSrcweir }
1734cdf0e10cSrcweir
1735cdf0e10cSrcweir // ------------------------------------------------------------------------
1736cdf0e10cSrcweir
GetChunks() const1737cdf0e10cSrcweir const std::vector< vcl::PNGReader::ChunkData >& PNGReader::GetChunks() const
1738cdf0e10cSrcweir {
1739cdf0e10cSrcweir return mpImpl->GetAllChunks();
1740cdf0e10cSrcweir }
1741cdf0e10cSrcweir
1742cdf0e10cSrcweir // ------------------------------------------------------------------------
1743cdf0e10cSrcweir
SetIgnoreGammaChunk(sal_Bool b)1744cdf0e10cSrcweir void PNGReader::SetIgnoreGammaChunk( sal_Bool b )
1745cdf0e10cSrcweir {
1746cdf0e10cSrcweir mpImpl->SetIgnoreGammaChunk( b );
1747cdf0e10cSrcweir }
1748cdf0e10cSrcweir
1749cdf0e10cSrcweir
1750cdf0e10cSrcweir } // namespace vcl
1751