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/pngwrite.hxx> 28cdf0e10cSrcweir 29cdf0e10cSrcweir #include <cmath> 30cdf0e10cSrcweir #include <limits> 31cdf0e10cSrcweir #include <rtl/crc.h> 32cdf0e10cSrcweir #include <rtl/memory.h> 33cdf0e10cSrcweir #include <rtl/alloc.h> 34cdf0e10cSrcweir #include <tools/zcodec.hxx> 35cdf0e10cSrcweir #include <tools/stream.hxx> 36cdf0e10cSrcweir #include <vcl/bmpacc.hxx> 37cdf0e10cSrcweir #include <vcl/svapp.hxx> 38cdf0e10cSrcweir #include <vcl/alpha.hxx> 39cdf0e10cSrcweir #include <osl/endian.h> 40cdf0e10cSrcweir 41cdf0e10cSrcweir // ----------- 42cdf0e10cSrcweir // - Defines - 43cdf0e10cSrcweir // ----------- 44cdf0e10cSrcweir 45cdf0e10cSrcweir #define PNG_DEF_COMPRESSION 6 46cdf0e10cSrcweir 47cdf0e10cSrcweir #define PNGCHUNK_IHDR 0x49484452 48cdf0e10cSrcweir #define PNGCHUNK_PLTE 0x504c5445 49cdf0e10cSrcweir #define PNGCHUNK_IDAT 0x49444154 50cdf0e10cSrcweir #define PNGCHUNK_IEND 0x49454e44 51cdf0e10cSrcweir #define PNGCHUNK_bKGD 0x624b4744 52cdf0e10cSrcweir #define PNGCHUNK_cHRM 0x6348524d 53cdf0e10cSrcweir #define PNGCHUNK_gAMA 0x67414d41 54cdf0e10cSrcweir #define PNGCHUNK_hIST 0x68495354 55cdf0e10cSrcweir #define PNGCHUNK_pHYs 0x70485973 56cdf0e10cSrcweir #define PNGCHUNK_sBIT 0x73425420 57cdf0e10cSrcweir #define PNGCHUNK_tIME 0x74494d45 58cdf0e10cSrcweir #define PNGCHUNK_tEXt 0x74455874 59cdf0e10cSrcweir #define PNGCHUNK_tRNS 0x74524e53 60cdf0e10cSrcweir #define PNGCHUNK_zTXt 0x7a545874 61cdf0e10cSrcweir 62cdf0e10cSrcweir namespace vcl 63cdf0e10cSrcweir { 64cdf0e10cSrcweir // ----------------- 65cdf0e10cSrcweir // - PNGWriterImplImpl - 66cdf0e10cSrcweir // ----------------- 67cdf0e10cSrcweir 68cdf0e10cSrcweir class PNGWriterImpl 69cdf0e10cSrcweir { 70cdf0e10cSrcweir public: 71cdf0e10cSrcweir 72cdf0e10cSrcweir PNGWriterImpl( const BitmapEx& BmpEx, 73cdf0e10cSrcweir const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData = NULL ); 74cdf0e10cSrcweir ~PNGWriterImpl(); 75cdf0e10cSrcweir 76cdf0e10cSrcweir sal_Bool Write( SvStream& rOStm ); 77cdf0e10cSrcweir 78cdf0e10cSrcweir std::vector< vcl::PNGWriter::ChunkData >& GetChunks(); 79cdf0e10cSrcweir 80cdf0e10cSrcweir private: 81cdf0e10cSrcweir 82cdf0e10cSrcweir std::vector< vcl::PNGWriter::ChunkData > maChunkSeq; 83cdf0e10cSrcweir 84cdf0e10cSrcweir sal_Int32 mnCompLevel; 85cdf0e10cSrcweir sal_Int32 mnInterlaced; 86cdf0e10cSrcweir sal_uInt32 mnMaxChunkSize; 87cdf0e10cSrcweir sal_Bool mbStatus; 88cdf0e10cSrcweir 89cdf0e10cSrcweir BitmapReadAccess* mpAccess; 90cdf0e10cSrcweir BitmapReadAccess* mpMaskAccess; 91cdf0e10cSrcweir ZCodec* mpZCodec; 92cdf0e10cSrcweir 93cdf0e10cSrcweir sal_uInt8* mpDeflateInBuf; // as big as the size of a scanline + alphachannel + 1 94cdf0e10cSrcweir sal_uInt8* mpPreviousScan; // as big as mpDeflateInBuf 95cdf0e10cSrcweir sal_uInt8* mpCurrentScan; 96cdf0e10cSrcweir sal_uLong mnDeflateInSize; 97cdf0e10cSrcweir 98cdf0e10cSrcweir sal_uLong mnWidth, mnHeight; 99cdf0e10cSrcweir sal_uInt8 mnBitsPerPixel; 100cdf0e10cSrcweir sal_uInt8 mnFilterType; // 0 oder 4; 101cdf0e10cSrcweir sal_uLong mnBBP; // bytes per pixel ( needed for filtering ) 102cdf0e10cSrcweir sal_Bool mbTrueAlpha; 103cdf0e10cSrcweir sal_uLong mnCRC; 104cdf0e10cSrcweir long mnChunkDatSize; 105cdf0e10cSrcweir sal_uLong mnLastPercent; 106cdf0e10cSrcweir 107cdf0e10cSrcweir void ImplWritepHYs( const BitmapEx& rBitmapEx ); 108cdf0e10cSrcweir void ImplWriteIDAT(); 109cdf0e10cSrcweir sal_uLong ImplGetFilter( sal_uLong nY, sal_uLong nXStart=0, sal_uLong nXAdd=1 ); 110cdf0e10cSrcweir void ImplClearFirstScanline(); 111cdf0e10cSrcweir void ImplWriteTransparent(); 112cdf0e10cSrcweir sal_Bool ImplWriteHeader(); 113cdf0e10cSrcweir void ImplWritePalette(); 114cdf0e10cSrcweir void ImplOpenChunk( sal_uLong nChunkType ); 115cdf0e10cSrcweir void ImplWriteChunk( sal_uInt8 nNumb ); 116cdf0e10cSrcweir void ImplWriteChunk( sal_uInt32 nNumb ); 117cdf0e10cSrcweir void ImplWriteChunk( unsigned char* pSource, sal_uInt32 nDatSize ); 118cdf0e10cSrcweir void ImplCloseChunk( void ); 119cdf0e10cSrcweir }; 120cdf0e10cSrcweir 121cdf0e10cSrcweir // ------------------------------------------------------------------------ 122cdf0e10cSrcweir 123cdf0e10cSrcweir PNGWriterImpl::PNGWriterImpl( const BitmapEx& rBmpEx, 124cdf0e10cSrcweir const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData ) : 125cdf0e10cSrcweir mnCompLevel ( PNG_DEF_COMPRESSION ), 126cdf0e10cSrcweir mbStatus ( sal_True ), 127cdf0e10cSrcweir mpAccess ( NULL ), 128cdf0e10cSrcweir mpMaskAccess ( NULL ), 129cdf0e10cSrcweir mpZCodec ( new ZCodec( DEFAULT_IN_BUFSIZE, DEFAULT_OUT_BUFSIZE, MAX_MEM_USAGE ) ), 130cdf0e10cSrcweir mnCRC(0UL), 131cdf0e10cSrcweir mnLastPercent ( 0UL ) 132cdf0e10cSrcweir { 133cdf0e10cSrcweir if ( !rBmpEx.IsEmpty() ) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir Bitmap aBmp( rBmpEx.GetBitmap() ); 136cdf0e10cSrcweir 137cdf0e10cSrcweir mnInterlaced = 0; // ( aBmp.GetSizePixel().Width() > 128 ) || ( aBmp.GetSizePixel().Height() > 128 ) ? 1 : 0; #i67236# 138cdf0e10cSrcweir 139cdf0e10cSrcweir // #i67234# defaulting max chunk size to 256kb when using interlace mode 140cdf0e10cSrcweir mnMaxChunkSize = mnInterlaced == 0 ? std::numeric_limits< sal_uInt32 >::max() : 0x40000; 141cdf0e10cSrcweir 142cdf0e10cSrcweir if ( pFilterData ) 143cdf0e10cSrcweir { 144cdf0e10cSrcweir sal_Int32 i = 0; 145cdf0e10cSrcweir for ( i = 0; i < pFilterData->getLength(); i++ ) 146cdf0e10cSrcweir { 147cdf0e10cSrcweir if ( (*pFilterData)[ i ].Name.equalsAscii( "Compression" ) ) 148cdf0e10cSrcweir (*pFilterData)[ i ].Value >>= mnCompLevel; 149cdf0e10cSrcweir else if ( (*pFilterData)[ i ].Name.equalsAscii( "Interlaced" ) ) 150cdf0e10cSrcweir (*pFilterData)[ i ].Value >>= mnInterlaced; 151cdf0e10cSrcweir else if ( (*pFilterData)[ i ].Name.equalsAscii( "MaxChunkSize" ) ) 152cdf0e10cSrcweir { 153cdf0e10cSrcweir sal_Int32 nVal = 0; 154cdf0e10cSrcweir if ( (*pFilterData)[ i ].Value >>= nVal ) 155cdf0e10cSrcweir mnMaxChunkSize = (sal_uInt32)nVal; 156cdf0e10cSrcweir } 157cdf0e10cSrcweir } 158cdf0e10cSrcweir } 159cdf0e10cSrcweir mnBitsPerPixel = (sal_uInt8)aBmp.GetBitCount(); 160cdf0e10cSrcweir 161cdf0e10cSrcweir if( rBmpEx.IsTransparent() ) 162cdf0e10cSrcweir { 163cdf0e10cSrcweir if ( mnBitsPerPixel <= 8 && rBmpEx.IsAlpha() ) 164cdf0e10cSrcweir { 165cdf0e10cSrcweir aBmp.Convert( BMP_CONVERSION_24BIT ); 166cdf0e10cSrcweir mnBitsPerPixel = 24; 167cdf0e10cSrcweir } 168cdf0e10cSrcweir 169cdf0e10cSrcweir if ( mnBitsPerPixel <= 8 ) // transparent palette 170cdf0e10cSrcweir { 171cdf0e10cSrcweir aBmp.Convert( BMP_CONVERSION_8BIT_TRANS ); 172cdf0e10cSrcweir aBmp.Replace( rBmpEx.GetMask(), BMP_COL_TRANS ); 173cdf0e10cSrcweir mnBitsPerPixel = 8; 174cdf0e10cSrcweir mpAccess = aBmp.AcquireReadAccess(); 175cdf0e10cSrcweir if ( mpAccess ) 176cdf0e10cSrcweir { 177cdf0e10cSrcweir if ( ImplWriteHeader() ) 178cdf0e10cSrcweir { 179cdf0e10cSrcweir ImplWritepHYs( rBmpEx ); 180cdf0e10cSrcweir ImplWritePalette(); 181cdf0e10cSrcweir ImplWriteTransparent(); 182cdf0e10cSrcweir ImplWriteIDAT(); 183cdf0e10cSrcweir } 184cdf0e10cSrcweir aBmp.ReleaseAccess( mpAccess ); 185cdf0e10cSrcweir } 186cdf0e10cSrcweir else 187cdf0e10cSrcweir mbStatus = sal_False; 188cdf0e10cSrcweir } 189cdf0e10cSrcweir else 190cdf0e10cSrcweir { 191cdf0e10cSrcweir mpAccess = aBmp.AcquireReadAccess(); // sal_True RGB with alphachannel 192cdf0e10cSrcweir if( mpAccess ) 193cdf0e10cSrcweir { 194cdf0e10cSrcweir if ( ( mbTrueAlpha = rBmpEx.IsAlpha() ) != sal_False ) 195cdf0e10cSrcweir { 196cdf0e10cSrcweir AlphaMask aMask( rBmpEx.GetAlpha() ); 197cdf0e10cSrcweir mpMaskAccess = aMask.AcquireReadAccess(); 198cdf0e10cSrcweir if ( mpMaskAccess ) 199cdf0e10cSrcweir { 200cdf0e10cSrcweir if ( ImplWriteHeader() ) 201cdf0e10cSrcweir { 202cdf0e10cSrcweir ImplWritepHYs( rBmpEx ); 203cdf0e10cSrcweir ImplWriteIDAT(); 204cdf0e10cSrcweir } 205cdf0e10cSrcweir aMask.ReleaseAccess( mpMaskAccess ); 206cdf0e10cSrcweir } 207cdf0e10cSrcweir else 208cdf0e10cSrcweir mbStatus = sal_False; 209cdf0e10cSrcweir } 210cdf0e10cSrcweir else 211cdf0e10cSrcweir { 212cdf0e10cSrcweir Bitmap aMask( rBmpEx.GetMask() ); 213cdf0e10cSrcweir mpMaskAccess = aMask.AcquireReadAccess(); 214cdf0e10cSrcweir if( mpMaskAccess ) 215cdf0e10cSrcweir { 216cdf0e10cSrcweir if ( ImplWriteHeader() ) 217cdf0e10cSrcweir { 218cdf0e10cSrcweir ImplWritepHYs( rBmpEx ); 219cdf0e10cSrcweir ImplWriteIDAT(); 220cdf0e10cSrcweir } 221cdf0e10cSrcweir aMask.ReleaseAccess( mpMaskAccess ); 222cdf0e10cSrcweir } 223cdf0e10cSrcweir else 224cdf0e10cSrcweir mbStatus = sal_False; 225cdf0e10cSrcweir } 226cdf0e10cSrcweir aBmp.ReleaseAccess( mpAccess ); 227cdf0e10cSrcweir } 228cdf0e10cSrcweir else 229cdf0e10cSrcweir mbStatus = sal_False; 230cdf0e10cSrcweir } 231cdf0e10cSrcweir } 232cdf0e10cSrcweir else 233cdf0e10cSrcweir { 234cdf0e10cSrcweir mpAccess = aBmp.AcquireReadAccess(); // palette + RGB without alphachannel 235cdf0e10cSrcweir if( mpAccess ) 236cdf0e10cSrcweir { 237cdf0e10cSrcweir if ( ImplWriteHeader() ) 238cdf0e10cSrcweir { 239cdf0e10cSrcweir ImplWritepHYs( rBmpEx ); 240cdf0e10cSrcweir if( mpAccess->HasPalette() ) 241cdf0e10cSrcweir ImplWritePalette(); 242cdf0e10cSrcweir 243cdf0e10cSrcweir ImplWriteIDAT(); 244cdf0e10cSrcweir } 245cdf0e10cSrcweir aBmp.ReleaseAccess( mpAccess ); 246cdf0e10cSrcweir } 247cdf0e10cSrcweir else 248cdf0e10cSrcweir mbStatus = sal_False; 249cdf0e10cSrcweir } 250cdf0e10cSrcweir if ( mbStatus ) 251cdf0e10cSrcweir { 252cdf0e10cSrcweir ImplOpenChunk( PNGCHUNK_IEND ); // create an IEND chunk 253cdf0e10cSrcweir ImplCloseChunk(); 254cdf0e10cSrcweir } 255cdf0e10cSrcweir } 256cdf0e10cSrcweir } 257cdf0e10cSrcweir 258cdf0e10cSrcweir // ------------------------------------------------------------------------ 259cdf0e10cSrcweir 260cdf0e10cSrcweir PNGWriterImpl::~PNGWriterImpl() 261cdf0e10cSrcweir { 262cdf0e10cSrcweir delete mpZCodec; 263cdf0e10cSrcweir } 264cdf0e10cSrcweir 265cdf0e10cSrcweir // ------------------------------------------------------------------------ 266cdf0e10cSrcweir 267cdf0e10cSrcweir sal_Bool PNGWriterImpl::Write( SvStream& rOStm ) 268cdf0e10cSrcweir { 269cdf0e10cSrcweir /* png signature is always an array of 8 bytes */ 270cdf0e10cSrcweir sal_uInt16 nOldMode = rOStm.GetNumberFormatInt(); 271cdf0e10cSrcweir rOStm.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN ); 272cdf0e10cSrcweir rOStm << static_cast<sal_uInt32>(0x89504e47); 273cdf0e10cSrcweir rOStm << static_cast<sal_uInt32>(0x0d0a1a0a); 274cdf0e10cSrcweir 275cdf0e10cSrcweir std::vector< vcl::PNGWriter::ChunkData >::iterator aBeg( maChunkSeq.begin() ); 276cdf0e10cSrcweir std::vector< vcl::PNGWriter::ChunkData >::iterator aEnd( maChunkSeq.end() ); 277cdf0e10cSrcweir while( aBeg != aEnd ) 278cdf0e10cSrcweir { 279cdf0e10cSrcweir sal_uInt32 nType = aBeg->nType; 280cdf0e10cSrcweir #if defined(__LITTLEENDIAN) || defined(OSL_LITENDIAN) 281cdf0e10cSrcweir nType = SWAPLONG( nType ); 282cdf0e10cSrcweir #endif 283cdf0e10cSrcweir sal_uInt32 nCRC = rtl_crc32( 0, &nType, 4 ); 284cdf0e10cSrcweir sal_uInt32 nDataSize = aBeg->aData.size(); 285cdf0e10cSrcweir if ( nDataSize ) 286cdf0e10cSrcweir nCRC = rtl_crc32( nCRC, &aBeg->aData[ 0 ], nDataSize ); 287cdf0e10cSrcweir rOStm << nDataSize 288cdf0e10cSrcweir << aBeg->nType; 289cdf0e10cSrcweir if ( nDataSize ) 290cdf0e10cSrcweir rOStm.Write( &aBeg->aData[ 0 ], nDataSize ); 291cdf0e10cSrcweir rOStm << nCRC; 292cdf0e10cSrcweir aBeg++; 293cdf0e10cSrcweir } 294cdf0e10cSrcweir rOStm.SetNumberFormatInt( nOldMode ); 295cdf0e10cSrcweir return mbStatus; 296cdf0e10cSrcweir } 297cdf0e10cSrcweir 298cdf0e10cSrcweir // ------------------------------------------------------------------------ 299cdf0e10cSrcweir 300cdf0e10cSrcweir std::vector< vcl::PNGWriter::ChunkData >& PNGWriterImpl::GetChunks() 301cdf0e10cSrcweir { 302cdf0e10cSrcweir return maChunkSeq; 303cdf0e10cSrcweir } 304cdf0e10cSrcweir 305cdf0e10cSrcweir // ------------------------------------------------------------------------ 306cdf0e10cSrcweir 307cdf0e10cSrcweir sal_Bool PNGWriterImpl::ImplWriteHeader() 308cdf0e10cSrcweir { 309cdf0e10cSrcweir ImplOpenChunk(PNGCHUNK_IHDR); 310cdf0e10cSrcweir ImplWriteChunk( sal_uInt32( mnWidth = mpAccess->Width() ) ); 311cdf0e10cSrcweir ImplWriteChunk( sal_uInt32( mnHeight = mpAccess->Height() ) ); 312cdf0e10cSrcweir 313cdf0e10cSrcweir if ( mnWidth && mnHeight && mnBitsPerPixel && mbStatus ) 314cdf0e10cSrcweir { 315cdf0e10cSrcweir sal_uInt8 nBitDepth = mnBitsPerPixel; 316cdf0e10cSrcweir if ( mnBitsPerPixel <= 8 ) 317cdf0e10cSrcweir mnFilterType = 0; 318cdf0e10cSrcweir else 319cdf0e10cSrcweir mnFilterType = 4; 320cdf0e10cSrcweir 321cdf0e10cSrcweir sal_uInt8 nColorType = 2; // colortype: 322cdf0e10cSrcweir // bit 0 -> palette is used 323cdf0e10cSrcweir if ( mpAccess->HasPalette() ) // bit 1 -> color is used 324cdf0e10cSrcweir nColorType |= 1; // bit 2 -> alpha channel is used 325cdf0e10cSrcweir else 326cdf0e10cSrcweir nBitDepth /= 3; 327cdf0e10cSrcweir 328cdf0e10cSrcweir if ( mpMaskAccess ) 329cdf0e10cSrcweir nColorType |= 4; 330cdf0e10cSrcweir 331cdf0e10cSrcweir ImplWriteChunk( nBitDepth ); 332cdf0e10cSrcweir ImplWriteChunk( nColorType ); // colortype 333cdf0e10cSrcweir ImplWriteChunk((sal_uInt8) 0 ); // compression type 334cdf0e10cSrcweir ImplWriteChunk((sal_uInt8) 0 ); // filter type - is not supported in this version 335cdf0e10cSrcweir ImplWriteChunk((sal_uInt8) mnInterlaced ); // interlace type 336cdf0e10cSrcweir ImplCloseChunk(); 337cdf0e10cSrcweir } 338cdf0e10cSrcweir else 339cdf0e10cSrcweir mbStatus = sal_False; 340cdf0e10cSrcweir return mbStatus; 341cdf0e10cSrcweir } 342cdf0e10cSrcweir 343cdf0e10cSrcweir // ------------------------------------------------------------------------ 344cdf0e10cSrcweir 345cdf0e10cSrcweir void PNGWriterImpl::ImplWritePalette() 346cdf0e10cSrcweir { 347cdf0e10cSrcweir const sal_uLong nCount = mpAccess->GetPaletteEntryCount(); 348cdf0e10cSrcweir sal_uInt8* pTempBuf = new sal_uInt8[ nCount*3 ]; 349cdf0e10cSrcweir sal_uInt8* pTmp = pTempBuf; 350cdf0e10cSrcweir 351cdf0e10cSrcweir ImplOpenChunk( PNGCHUNK_PLTE ); 352cdf0e10cSrcweir 353cdf0e10cSrcweir for ( sal_uInt16 i = 0; i < nCount; i++ ) 354cdf0e10cSrcweir { 355cdf0e10cSrcweir const BitmapColor& rColor = mpAccess->GetPaletteColor( i ); 356cdf0e10cSrcweir *pTmp++ = rColor.GetRed(); 357cdf0e10cSrcweir *pTmp++ = rColor.GetGreen(); 358cdf0e10cSrcweir *pTmp++ = rColor.GetBlue(); 359cdf0e10cSrcweir } 360cdf0e10cSrcweir ImplWriteChunk( pTempBuf, nCount*3 ); 361cdf0e10cSrcweir ImplCloseChunk(); 362cdf0e10cSrcweir delete[] pTempBuf; 363cdf0e10cSrcweir } 364cdf0e10cSrcweir 365cdf0e10cSrcweir // ------------------------------------------------------------------------ 366cdf0e10cSrcweir 367cdf0e10cSrcweir void PNGWriterImpl::ImplWriteTransparent () 368cdf0e10cSrcweir { 369cdf0e10cSrcweir const sal_uLong nTransIndex = mpAccess->GetBestMatchingColor( BMP_COL_TRANS ); 370cdf0e10cSrcweir 371cdf0e10cSrcweir ImplOpenChunk( PNGCHUNK_tRNS ); 372cdf0e10cSrcweir 373cdf0e10cSrcweir for ( sal_uLong n = 0UL; n <= nTransIndex; n++ ) 374cdf0e10cSrcweir ImplWriteChunk( ( nTransIndex == n ) ? (sal_uInt8) 0x0 : (sal_uInt8) 0xff ); 375cdf0e10cSrcweir 376cdf0e10cSrcweir ImplCloseChunk(); 377cdf0e10cSrcweir } 378cdf0e10cSrcweir 379cdf0e10cSrcweir // ------------------------------------------------------------------------ 380cdf0e10cSrcweir 381cdf0e10cSrcweir void PNGWriterImpl::ImplWritepHYs( const BitmapEx& rBmpEx ) 382cdf0e10cSrcweir { 383cdf0e10cSrcweir if ( rBmpEx.GetPrefMapMode() == MAP_100TH_MM ) 384cdf0e10cSrcweir { 385cdf0e10cSrcweir Size aPrefSize( rBmpEx.GetPrefSize() ); 386cdf0e10cSrcweir if ( aPrefSize.Width() && aPrefSize.Height() ) 387cdf0e10cSrcweir { 388cdf0e10cSrcweir ImplOpenChunk( PNGCHUNK_pHYs ); 389cdf0e10cSrcweir sal_uInt8 nMapUnit = 1; 390cdf0e10cSrcweir sal_uInt32 nPrefSizeX = (sal_uInt32)( (double)100000.0 / ( (double)aPrefSize.Width() / mnWidth ) + 0.5 ); 391cdf0e10cSrcweir sal_uInt32 nPrefSizeY = (sal_uInt32)( (double)100000.0 / ( (double)aPrefSize.Height() / mnHeight ) + 0.5 ); 392cdf0e10cSrcweir ImplWriteChunk( nPrefSizeX ); 393cdf0e10cSrcweir ImplWriteChunk( nPrefSizeY ); 394cdf0e10cSrcweir ImplWriteChunk( nMapUnit ); 395cdf0e10cSrcweir ImplCloseChunk(); 396cdf0e10cSrcweir } 397cdf0e10cSrcweir } 398cdf0e10cSrcweir } 399cdf0e10cSrcweir 400cdf0e10cSrcweir // ------------------------------------------------------------------------ 401cdf0e10cSrcweir 402cdf0e10cSrcweir void PNGWriterImpl::ImplWriteIDAT () 403cdf0e10cSrcweir { 404cdf0e10cSrcweir mnDeflateInSize = mnBitsPerPixel; 405cdf0e10cSrcweir 406cdf0e10cSrcweir if( mpMaskAccess ) 407cdf0e10cSrcweir mnDeflateInSize += 8; 408cdf0e10cSrcweir 409cdf0e10cSrcweir mnBBP = ( mnDeflateInSize + 7 ) >> 3; 410cdf0e10cSrcweir 411cdf0e10cSrcweir mnDeflateInSize = mnBBP * mnWidth + 1; 412cdf0e10cSrcweir 413cdf0e10cSrcweir mpDeflateInBuf = new sal_uInt8[ mnDeflateInSize ]; 414cdf0e10cSrcweir 415cdf0e10cSrcweir if ( mnFilterType ) // using filter type 4 we need memory for the scanline 3 times 416cdf0e10cSrcweir { 417cdf0e10cSrcweir mpPreviousScan = new sal_uInt8[ mnDeflateInSize ]; 418cdf0e10cSrcweir mpCurrentScan = new sal_uInt8[ mnDeflateInSize ]; 419cdf0e10cSrcweir ImplClearFirstScanline(); 420cdf0e10cSrcweir } 421cdf0e10cSrcweir mpZCodec->BeginCompression( ZCODEC_PNG_DEFAULT + mnCompLevel ); 422cdf0e10cSrcweir mpZCodec->SetCRC( mnCRC ); 423cdf0e10cSrcweir SvMemoryStream aOStm; 424cdf0e10cSrcweir if ( mnInterlaced == 0 ) 425cdf0e10cSrcweir { 426cdf0e10cSrcweir for ( sal_uLong nY = 0; nY < mnHeight; nY++ ) 427cdf0e10cSrcweir mpZCodec->Write( aOStm, mpDeflateInBuf, ImplGetFilter( nY ) ); 428cdf0e10cSrcweir } 429cdf0e10cSrcweir else 430cdf0e10cSrcweir { 431cdf0e10cSrcweir // interlace mode 432cdf0e10cSrcweir sal_uLong nY; 433cdf0e10cSrcweir for ( nY = 0; nY < mnHeight; nY+=8 ) // pass 1 434cdf0e10cSrcweir mpZCodec->Write( aOStm, mpDeflateInBuf, ImplGetFilter ( nY, 0, 8 ) ); 435cdf0e10cSrcweir ImplClearFirstScanline(); 436cdf0e10cSrcweir 437cdf0e10cSrcweir for ( nY = 0; nY < mnHeight; nY+=8 ) // pass 2 438cdf0e10cSrcweir mpZCodec->Write( aOStm, mpDeflateInBuf, ImplGetFilter ( nY, 4, 8 ) ); 439cdf0e10cSrcweir ImplClearFirstScanline(); 440cdf0e10cSrcweir 441cdf0e10cSrcweir if ( mnHeight >= 5 ) // pass 3 442cdf0e10cSrcweir { 443cdf0e10cSrcweir for ( nY = 4; nY < mnHeight; nY+=8 ) 444cdf0e10cSrcweir mpZCodec->Write( aOStm, mpDeflateInBuf, ImplGetFilter ( nY, 0, 4 ) ); 445cdf0e10cSrcweir ImplClearFirstScanline(); 446cdf0e10cSrcweir } 447cdf0e10cSrcweir 448cdf0e10cSrcweir for ( nY = 0; nY < mnHeight; nY+=4 ) // pass 4 449cdf0e10cSrcweir mpZCodec->Write( aOStm, mpDeflateInBuf, ImplGetFilter ( nY, 2, 4 ) ); 450cdf0e10cSrcweir ImplClearFirstScanline(); 451cdf0e10cSrcweir 452cdf0e10cSrcweir if ( mnHeight >= 3 ) // pass 5 453cdf0e10cSrcweir { 454cdf0e10cSrcweir for ( nY = 2; nY < mnHeight; nY+=4 ) 455cdf0e10cSrcweir mpZCodec->Write( aOStm, mpDeflateInBuf, ImplGetFilter ( nY, 0, 2 ) ); 456cdf0e10cSrcweir ImplClearFirstScanline(); 457cdf0e10cSrcweir } 458cdf0e10cSrcweir 459cdf0e10cSrcweir for ( nY = 0; nY < mnHeight; nY+=2 ) // pass 6 460cdf0e10cSrcweir mpZCodec->Write( aOStm, mpDeflateInBuf, ImplGetFilter ( nY, 1, 2 ) ); 461cdf0e10cSrcweir ImplClearFirstScanline(); 462cdf0e10cSrcweir 463cdf0e10cSrcweir if ( mnHeight >= 2 ) // pass 7 464cdf0e10cSrcweir { 465cdf0e10cSrcweir for ( nY = 1; nY < mnHeight; nY+=2 ) 466cdf0e10cSrcweir mpZCodec->Write( aOStm, mpDeflateInBuf, ImplGetFilter ( nY, 0, 1 ) ); 467cdf0e10cSrcweir } 468cdf0e10cSrcweir } 469cdf0e10cSrcweir mpZCodec->EndCompression(); 470cdf0e10cSrcweir mnCRC = mpZCodec->GetCRC(); 471cdf0e10cSrcweir 472cdf0e10cSrcweir if ( mnFilterType ) // using filter type 4 we need memory for the scanline 3 times 473cdf0e10cSrcweir { 474cdf0e10cSrcweir delete[] mpCurrentScan; 475cdf0e10cSrcweir delete[] mpPreviousScan; 476cdf0e10cSrcweir } 477cdf0e10cSrcweir delete[] mpDeflateInBuf; 478cdf0e10cSrcweir 479cdf0e10cSrcweir sal_uInt32 nIDATSize = aOStm.Tell(); 480cdf0e10cSrcweir sal_uInt32 nBytes, nBytesToWrite = nIDATSize; 481cdf0e10cSrcweir while( nBytesToWrite ) 482cdf0e10cSrcweir { 483cdf0e10cSrcweir nBytes = nBytesToWrite <= mnMaxChunkSize ? nBytesToWrite : mnMaxChunkSize; 484cdf0e10cSrcweir ImplOpenChunk( PNGCHUNK_IDAT ); 485cdf0e10cSrcweir ImplWriteChunk( (unsigned char*)aOStm.GetData() + ( nIDATSize - nBytesToWrite ), nBytes ); 486cdf0e10cSrcweir ImplCloseChunk(); 487cdf0e10cSrcweir nBytesToWrite -= nBytes; 488cdf0e10cSrcweir } 489cdf0e10cSrcweir } 490cdf0e10cSrcweir 491cdf0e10cSrcweir // --------------------------------------------------------------------------------------------------- 492cdf0e10cSrcweir // ImplGetFilter writes the complete Scanline (nY) - in interlace mode the parameter nXStart and nXAdd 493cdf0e10cSrcweir // appends to the currently used pass 494cdf0e10cSrcweir // the complete size of scanline will be returned - in interlace mode zero is possible! 495cdf0e10cSrcweir 496cdf0e10cSrcweir sal_uLong PNGWriterImpl::ImplGetFilter ( sal_uLong nY, sal_uLong nXStart, sal_uLong nXAdd ) 497cdf0e10cSrcweir { 498cdf0e10cSrcweir sal_uInt8* pDest; 499cdf0e10cSrcweir 500cdf0e10cSrcweir if ( mnFilterType ) 501cdf0e10cSrcweir pDest = mpCurrentScan; 502cdf0e10cSrcweir else 503cdf0e10cSrcweir pDest = mpDeflateInBuf; 504cdf0e10cSrcweir 505cdf0e10cSrcweir if ( nXStart < mnWidth ) 506cdf0e10cSrcweir { 507cdf0e10cSrcweir *pDest++ = mnFilterType; // in this version the filter type is either 0 or 4 508cdf0e10cSrcweir 509cdf0e10cSrcweir if ( mpAccess->HasPalette() ) // alphachannel is not allowed by pictures including palette entries 510cdf0e10cSrcweir { 511cdf0e10cSrcweir switch ( mnBitsPerPixel ) 512cdf0e10cSrcweir { 513cdf0e10cSrcweir case( 1 ): 514cdf0e10cSrcweir { 515cdf0e10cSrcweir sal_uLong nX, nXIndex; 516cdf0e10cSrcweir for ( nX = nXStart, nXIndex = 0; nX < mnWidth; nX+=nXAdd, nXIndex++ ) 517cdf0e10cSrcweir { 518cdf0e10cSrcweir sal_uLong nShift = ( nXIndex & 7 ) ^ 7; 519cdf0e10cSrcweir if ( nShift == 7) 520cdf0e10cSrcweir *pDest = (sal_uInt8)(mpAccess->GetPixel( nY, nX ) << nShift); 521cdf0e10cSrcweir else if ( nShift == 0 ) 522cdf0e10cSrcweir *pDest++ |= (sal_uInt8) mpAccess->GetPixel( nY, nX ) << nShift; 523cdf0e10cSrcweir else 524cdf0e10cSrcweir *pDest |= (sal_uInt8) mpAccess->GetPixel( nY, nX ) << nShift; 525cdf0e10cSrcweir } 526cdf0e10cSrcweir if ( ( nXIndex & 7 ) != 0 ) pDest++; // byte is not completely used, so the 527cdf0e10cSrcweir } // bufferpointer is to correct 528cdf0e10cSrcweir break; 529cdf0e10cSrcweir 530cdf0e10cSrcweir case( 4 ): 531cdf0e10cSrcweir { 532cdf0e10cSrcweir sal_uLong nX, nXIndex; 533cdf0e10cSrcweir for ( nX = nXStart, nXIndex = 0; nX < mnWidth; nX+= nXAdd, nXIndex++ ) 534cdf0e10cSrcweir { 535cdf0e10cSrcweir if( nXIndex & 1 ) 536cdf0e10cSrcweir *pDest++ |= (sal_uInt8) mpAccess->GetPixel( nY, nX ); 537cdf0e10cSrcweir else 538cdf0e10cSrcweir *pDest = (sal_uInt8) mpAccess->GetPixel( nY, nX ) << 4; 539cdf0e10cSrcweir } 540cdf0e10cSrcweir if ( nXIndex & 1 ) pDest++; 541cdf0e10cSrcweir } 542cdf0e10cSrcweir break; 543cdf0e10cSrcweir 544cdf0e10cSrcweir case( 8 ): 545cdf0e10cSrcweir { 546cdf0e10cSrcweir for ( sal_uLong nX = nXStart; nX < mnWidth; nX+=nXAdd ) 547cdf0e10cSrcweir *pDest++ = mpAccess->GetPixel( nY, nX ); 548cdf0e10cSrcweir } 549cdf0e10cSrcweir break; 550cdf0e10cSrcweir 551cdf0e10cSrcweir default : 552cdf0e10cSrcweir mbStatus = sal_False; 553cdf0e10cSrcweir break; 554cdf0e10cSrcweir } 555cdf0e10cSrcweir } 556cdf0e10cSrcweir else 557cdf0e10cSrcweir { 558cdf0e10cSrcweir if ( mpMaskAccess ) // mpMaskAccess != NULL -> alphachannel is to create 559cdf0e10cSrcweir { 560cdf0e10cSrcweir if ( mbTrueAlpha ) 561cdf0e10cSrcweir { 562cdf0e10cSrcweir for ( sal_uLong nX = nXStart; nX < mnWidth; nX += nXAdd ) 563cdf0e10cSrcweir { 564cdf0e10cSrcweir const BitmapColor& rColor = mpAccess->GetPixel( nY, nX ); 565cdf0e10cSrcweir *pDest++ = rColor.GetRed(); 566cdf0e10cSrcweir *pDest++ = rColor.GetGreen(); 567cdf0e10cSrcweir *pDest++ = rColor.GetBlue(); 568cdf0e10cSrcweir *pDest++ = 255 - mpMaskAccess->GetPixel( nY, nX ); 569cdf0e10cSrcweir } 570cdf0e10cSrcweir } 571cdf0e10cSrcweir else 572cdf0e10cSrcweir { 573cdf0e10cSrcweir const BitmapColor aTrans( mpMaskAccess->GetBestMatchingColor( Color( COL_WHITE ) ) ); 574cdf0e10cSrcweir 575cdf0e10cSrcweir for ( sal_uLong nX = nXStart; nX < mnWidth; nX+=nXAdd ) 576cdf0e10cSrcweir { 577cdf0e10cSrcweir const BitmapColor& rColor = mpAccess->GetPixel( nY, nX ); 578cdf0e10cSrcweir *pDest++ = rColor.GetRed(); 579cdf0e10cSrcweir *pDest++ = rColor.GetGreen(); 580cdf0e10cSrcweir *pDest++ = rColor.GetBlue(); 581cdf0e10cSrcweir 582cdf0e10cSrcweir if( mpMaskAccess->GetPixel( nY, nX ) == aTrans ) 583cdf0e10cSrcweir *pDest++ = 0; 584cdf0e10cSrcweir else 585cdf0e10cSrcweir *pDest++ = 0xff; 586cdf0e10cSrcweir } 587cdf0e10cSrcweir } 588cdf0e10cSrcweir } 589cdf0e10cSrcweir else 590cdf0e10cSrcweir { 591cdf0e10cSrcweir for ( sal_uLong nX = nXStart; nX < mnWidth; nX+=nXAdd ) 592cdf0e10cSrcweir { 593cdf0e10cSrcweir const BitmapColor& rColor = mpAccess->GetPixel( nY, nX ); 594cdf0e10cSrcweir *pDest++ = rColor.GetRed(); 595cdf0e10cSrcweir *pDest++ = rColor.GetGreen(); 596cdf0e10cSrcweir *pDest++ = rColor.GetBlue(); 597cdf0e10cSrcweir } 598cdf0e10cSrcweir } 599cdf0e10cSrcweir } 600cdf0e10cSrcweir } 601cdf0e10cSrcweir // filter type4 ( PAETH ) will be used only for 24bit graphics 602cdf0e10cSrcweir if ( mnFilterType ) 603cdf0e10cSrcweir { 604cdf0e10cSrcweir mnDeflateInSize = pDest - mpCurrentScan; 605cdf0e10cSrcweir pDest = mpDeflateInBuf; 606cdf0e10cSrcweir *pDest++ = 4; // filter type 607cdf0e10cSrcweir 608cdf0e10cSrcweir sal_uLong na, nb, nc; 609cdf0e10cSrcweir long np, npa, npb, npc; 610cdf0e10cSrcweir 611cdf0e10cSrcweir sal_uInt8* p1 = mpCurrentScan + 1; // Current Pixel 612cdf0e10cSrcweir sal_uInt8* p2 = p1 - mnBBP; // left pixel 613cdf0e10cSrcweir sal_uInt8* p3 = mpPreviousScan; // upper pixel 614cdf0e10cSrcweir sal_uInt8* p4 = p3 - mnBBP; // upperleft Pixel; 615cdf0e10cSrcweir 616cdf0e10cSrcweir while ( pDest < mpDeflateInBuf + mnDeflateInSize ) 617cdf0e10cSrcweir { 618cdf0e10cSrcweir nb = *p3++; 619cdf0e10cSrcweir if ( p2 >= mpCurrentScan + 1 ) 620cdf0e10cSrcweir { 621cdf0e10cSrcweir na = *p2; 622cdf0e10cSrcweir nc = *p4; 623cdf0e10cSrcweir } 624cdf0e10cSrcweir else 625cdf0e10cSrcweir na = nc = 0; 626cdf0e10cSrcweir 627cdf0e10cSrcweir np = na + nb; 628cdf0e10cSrcweir np -= nc; 629cdf0e10cSrcweir npa = np - na; 630cdf0e10cSrcweir npb = np - nb; 631cdf0e10cSrcweir npc = np - nc; 632cdf0e10cSrcweir if ( npa < 0 ) 633cdf0e10cSrcweir npa =-npa; 634cdf0e10cSrcweir if ( npb < 0 ) 635cdf0e10cSrcweir npb =-npb; 636cdf0e10cSrcweir if ( npc < 0 ) 637cdf0e10cSrcweir npc =-npc; 638cdf0e10cSrcweir if ( ( npa <= npb ) && ( npa <= npc ) ) *pDest++ = *p1++ - (sal_uInt8)na; 639cdf0e10cSrcweir else if ( npb <= npc ) *pDest++ = *p1++ - (sal_uInt8)nb; 640cdf0e10cSrcweir else *pDest++ = *p1++ - (sal_uInt8)nc; 641cdf0e10cSrcweir p4++; 642cdf0e10cSrcweir p2++; 643cdf0e10cSrcweir } 644cdf0e10cSrcweir for ( long i = 0; i < (long)( mnDeflateInSize - 1 ); i++ ) 645cdf0e10cSrcweir mpPreviousScan[ i ] = mpCurrentScan[ i + 1 ]; 646cdf0e10cSrcweir } 647cdf0e10cSrcweir else 648cdf0e10cSrcweir mnDeflateInSize = pDest - mpDeflateInBuf; 649cdf0e10cSrcweir return ( mnDeflateInSize ); 650cdf0e10cSrcweir } 651cdf0e10cSrcweir 652cdf0e10cSrcweir // ------------------------------------------------------------------------ 653cdf0e10cSrcweir 654cdf0e10cSrcweir void PNGWriterImpl::ImplClearFirstScanline() 655cdf0e10cSrcweir { 656cdf0e10cSrcweir if ( mnFilterType ) 657cdf0e10cSrcweir rtl_zeroMemory( mpPreviousScan, mnDeflateInSize ); 658cdf0e10cSrcweir } 659cdf0e10cSrcweir 660cdf0e10cSrcweir // ------------------------------------------------------------------------ 661cdf0e10cSrcweir 662cdf0e10cSrcweir void PNGWriterImpl::ImplOpenChunk ( sal_uLong nChunkType ) 663cdf0e10cSrcweir { 664cdf0e10cSrcweir maChunkSeq.resize( maChunkSeq.size() + 1 ); 665cdf0e10cSrcweir maChunkSeq.back().nType = nChunkType; 666cdf0e10cSrcweir } 667cdf0e10cSrcweir 668cdf0e10cSrcweir // ------------------------------------------------------------------------ 669cdf0e10cSrcweir 670cdf0e10cSrcweir void PNGWriterImpl::ImplWriteChunk ( sal_uInt8 nSource ) 671cdf0e10cSrcweir { 672cdf0e10cSrcweir maChunkSeq.back().aData.push_back( nSource ); 673cdf0e10cSrcweir } 674cdf0e10cSrcweir 675cdf0e10cSrcweir void PNGWriterImpl::ImplWriteChunk ( sal_uInt32 nSource ) 676cdf0e10cSrcweir { 677cdf0e10cSrcweir vcl::PNGWriter::ChunkData& rChunkData = maChunkSeq.back(); 678cdf0e10cSrcweir rChunkData.aData.push_back( (sal_uInt8)( nSource >> 24 ) ); 679cdf0e10cSrcweir rChunkData.aData.push_back( (sal_uInt8)( nSource >> 16 ) ); 680cdf0e10cSrcweir rChunkData.aData.push_back( (sal_uInt8)( nSource >> 8 ) ); 681cdf0e10cSrcweir rChunkData.aData.push_back( (sal_uInt8)( nSource ) ); 682cdf0e10cSrcweir } 683cdf0e10cSrcweir 684cdf0e10cSrcweir void PNGWriterImpl::ImplWriteChunk ( unsigned char* pSource, sal_uInt32 nDatSize ) 685cdf0e10cSrcweir { 686cdf0e10cSrcweir if ( nDatSize ) 687cdf0e10cSrcweir { 688cdf0e10cSrcweir vcl::PNGWriter::ChunkData& rChunkData = maChunkSeq.back(); 689cdf0e10cSrcweir sal_uInt32 nSize = rChunkData.aData.size(); 690cdf0e10cSrcweir rChunkData.aData.resize( nSize + nDatSize ); 691cdf0e10cSrcweir rtl_copyMemory( &rChunkData.aData[ nSize ], pSource, nDatSize ); 692cdf0e10cSrcweir } 693cdf0e10cSrcweir } 694cdf0e10cSrcweir 695cdf0e10cSrcweir // ------------------------------------------------------------------------ 696cdf0e10cSrcweir // nothing to do 697cdf0e10cSrcweir void PNGWriterImpl::ImplCloseChunk ( void ) 698cdf0e10cSrcweir { 699cdf0e10cSrcweir } 700cdf0e10cSrcweir 701cdf0e10cSrcweir // ------------- 702cdf0e10cSrcweir // - PNGWriter - 703cdf0e10cSrcweir // ------------- 704cdf0e10cSrcweir 705cdf0e10cSrcweir PNGWriter::PNGWriter( const BitmapEx& rBmpEx, 706cdf0e10cSrcweir const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData ) : 707cdf0e10cSrcweir mpImpl( new ::vcl::PNGWriterImpl( rBmpEx, pFilterData ) ) 708cdf0e10cSrcweir { 709cdf0e10cSrcweir } 710cdf0e10cSrcweir 711cdf0e10cSrcweir // ------------------------------------------------------------------------ 712cdf0e10cSrcweir 713cdf0e10cSrcweir PNGWriter::~PNGWriter() 714cdf0e10cSrcweir { 715cdf0e10cSrcweir delete mpImpl; 716cdf0e10cSrcweir } 717cdf0e10cSrcweir 718cdf0e10cSrcweir // ------------------------------------------------------------------------ 719cdf0e10cSrcweir 720cdf0e10cSrcweir sal_Bool PNGWriter::Write( SvStream& rIStm ) 721cdf0e10cSrcweir { 722cdf0e10cSrcweir return mpImpl->Write( rIStm ); 723cdf0e10cSrcweir } 724cdf0e10cSrcweir 725cdf0e10cSrcweir // ------------------------------------------------------------------------ 726cdf0e10cSrcweir 727cdf0e10cSrcweir std::vector< vcl::PNGWriter::ChunkData >& PNGWriter::GetChunks() 728cdf0e10cSrcweir { 729cdf0e10cSrcweir return mpImpl->GetChunks(); 730cdf0e10cSrcweir } 731cdf0e10cSrcweir 732cdf0e10cSrcweir } // namespace vcl 733cdf0e10cSrcweir 734