1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 // MARKER(update_precomp.py): autogen include statement, do not remove 23 #include "precompiled_vcl.hxx" 24 25 #include <vcl/salbtype.hxx> 26 #include <vcl/dibtools.hxx> 27 #include <tools/zcodec.hxx> 28 #include <tools/stream.hxx> 29 #include <vcl/bitmapex.hxx> 30 #include <vcl/bmpacc.hxx> 31 #include <vcl/outdev.hxx> 32 33 ////////////////////////////////////////////////////////////////////////////// 34 // - Defines - 35 36 #define DIBCOREHEADERSIZE ( 12UL ) 37 #define DIBINFOHEADERSIZE ( sizeof(DIBInfoHeader) ) 38 #define DIBV5HEADERSIZE ( sizeof(DIBV5Header) ) 39 40 ////////////////////////////////////////////////////////////////////////////// 41 // - Compression defines 42 43 #define COMPRESS_OWN ('S'|('D'<<8UL)) 44 #define COMPRESS_NONE ( 0UL ) 45 #define RLE_8 ( 1UL ) 46 #define RLE_4 ( 2UL ) 47 #define BITFIELDS ( 3UL ) 48 #define ZCOMPRESS ( COMPRESS_OWN | 0x01000000UL ) /* == 'SD01' (binary) */ 49 50 ////////////////////////////////////////////////////////////////////////////// 51 // - DIBInfoHeader and DIBV5Header 52 53 typedef sal_Int32 FXPT2DOT30; 54 55 struct CIEXYZ 56 { 57 FXPT2DOT30 aXyzX; 58 FXPT2DOT30 aXyzY; 59 FXPT2DOT30 aXyzZ; 60 61 CIEXYZ() 62 : aXyzX(0L), 63 aXyzY(0L), 64 aXyzZ(0L) 65 {} 66 67 ~CIEXYZ() 68 {} 69 }; 70 71 struct CIEXYZTriple 72 { 73 CIEXYZ aXyzRed; 74 CIEXYZ aXyzGreen; 75 CIEXYZ aXyzBlue; 76 77 CIEXYZTriple() 78 : aXyzRed(), 79 aXyzGreen(), 80 aXyzBlue() 81 {} 82 83 ~CIEXYZTriple() 84 {} 85 }; 86 87 struct DIBInfoHeader 88 { 89 sal_uInt32 nSize; 90 sal_Int32 nWidth; 91 sal_Int32 nHeight; 92 sal_uInt16 nPlanes; 93 sal_uInt16 nBitCount; 94 sal_uInt32 nCompression; 95 sal_uInt32 nSizeImage; 96 sal_Int32 nXPelsPerMeter; 97 sal_Int32 nYPelsPerMeter; 98 sal_uInt32 nColsUsed; 99 sal_uInt32 nColsImportant; 100 101 DIBInfoHeader() 102 : nSize(0UL), 103 nWidth(0UL), 104 nHeight(0UL), 105 nPlanes(0), 106 nBitCount(0), 107 nCompression(0), 108 nSizeImage(0), 109 nXPelsPerMeter(0UL), 110 nYPelsPerMeter(0UL), 111 nColsUsed(0UL), 112 nColsImportant(0UL) 113 {} 114 115 ~DIBInfoHeader() 116 {} 117 }; 118 119 struct DIBV5Header : public DIBInfoHeader 120 { 121 sal_uInt32 nV5RedMask; 122 sal_uInt32 nV5GreenMask; 123 sal_uInt32 nV5BlueMask; 124 sal_uInt32 nV5AlphaMask; 125 sal_uInt32 nV5CSType; 126 CIEXYZTriple aV5Endpoints; 127 sal_uInt32 nV5GammaRed; 128 sal_uInt32 nV5GammaGreen; 129 sal_uInt32 nV5GammaBlue; 130 sal_uInt32 nV5Intent; 131 sal_uInt32 nV5ProfileData; 132 sal_uInt32 nV5ProfileSize; 133 sal_uInt32 nV5Reserved; 134 135 DIBV5Header() 136 : DIBInfoHeader(), 137 nV5RedMask(0UL), 138 nV5GreenMask(0UL), 139 nV5BlueMask(0UL), 140 nV5AlphaMask(0UL), 141 nV5CSType(0UL), 142 aV5Endpoints(), 143 nV5GammaRed(0UL), 144 nV5GammaGreen(0UL), 145 nV5GammaBlue(0UL), 146 nV5Intent(0UL), 147 nV5ProfileData(0UL), 148 nV5ProfileSize(0UL), 149 nV5Reserved(0UL) 150 {} 151 152 ~DIBV5Header() 153 {} 154 }; 155 156 ////////////////////////////////////////////////////////////////////////////// 157 158 namespace 159 { 160 inline sal_uInt16 discretizeBitcount( sal_uInt16 nInputCount ) 161 { 162 return ( nInputCount <= 1 ) ? 1 : 163 ( nInputCount <= 4 ) ? 4 : 164 ( nInputCount <= 8 ) ? 8 : 24; 165 } 166 167 inline bool isBitfieldCompression( sal_uLong nScanlineFormat ) 168 { 169 return (BMP_FORMAT_16BIT_TC_LSB_MASK == nScanlineFormat) || (BMP_FORMAT_32BIT_TC_MASK == nScanlineFormat); 170 } 171 } 172 173 ////////////////////////////////////////////////////////////////////////////// 174 175 bool ImplReadDIBInfoHeader(SvStream& rIStm, DIBV5Header& rHeader, bool& bTopDown) 176 { 177 // BITMAPINFOHEADER or BITMAPCOREHEADER or BITMAPV5HEADER 178 const sal_Size aStartPos(rIStm.Tell()); 179 rIStm >> rHeader.nSize; 180 181 // BITMAPCOREHEADER 182 if ( rHeader.nSize == DIBCOREHEADERSIZE ) 183 { 184 sal_Int16 nTmp16; 185 186 rIStm >> nTmp16; rHeader.nWidth = nTmp16; 187 rIStm >> nTmp16; rHeader.nHeight = nTmp16; 188 rIStm >> rHeader.nPlanes; 189 rIStm >> rHeader.nBitCount; 190 } 191 else 192 { 193 // BITMAPCOREHEADER, BITMAPV5HEADER or unknown. Read as far as possible 194 sal_Size nUsed(sizeof(rHeader.nSize)); 195 196 // read DIBInfoHeader entries 197 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nWidth; nUsed += sizeof(rHeader.nWidth); } 198 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nHeight; nUsed += sizeof(rHeader.nHeight); } 199 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nPlanes; nUsed += sizeof(rHeader.nPlanes); } 200 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nBitCount; nUsed += sizeof(rHeader.nBitCount); } 201 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nCompression; nUsed += sizeof(rHeader.nCompression); } 202 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nSizeImage; nUsed += sizeof(rHeader.nSizeImage); } 203 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nXPelsPerMeter; nUsed += sizeof(rHeader.nXPelsPerMeter); } 204 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nYPelsPerMeter; nUsed += sizeof(rHeader.nYPelsPerMeter); } 205 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nColsUsed; nUsed += sizeof(rHeader.nColsUsed); } 206 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nColsImportant; nUsed += sizeof(rHeader.nColsImportant); } 207 208 // read DIBV5HEADER members 209 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nV5RedMask; nUsed += sizeof(rHeader.nV5RedMask); } 210 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nV5GreenMask; nUsed += sizeof(rHeader.nV5GreenMask); } 211 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nV5BlueMask; nUsed += sizeof(rHeader.nV5BlueMask); } 212 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nV5AlphaMask; nUsed += sizeof(rHeader.nV5AlphaMask); } 213 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nV5CSType; nUsed += sizeof(rHeader.nV5CSType); } 214 215 // read contained CIEXYZTriple's 216 if(nUsed < rHeader.nSize) { rIStm >> rHeader.aV5Endpoints.aXyzRed.aXyzX; nUsed += sizeof(rHeader.aV5Endpoints.aXyzRed.aXyzX); } 217 if(nUsed < rHeader.nSize) { rIStm >> rHeader.aV5Endpoints.aXyzRed.aXyzY; nUsed += sizeof(rHeader.aV5Endpoints.aXyzRed.aXyzY); } 218 if(nUsed < rHeader.nSize) { rIStm >> rHeader.aV5Endpoints.aXyzRed.aXyzZ; nUsed += sizeof(rHeader.aV5Endpoints.aXyzRed.aXyzZ); } 219 if(nUsed < rHeader.nSize) { rIStm >> rHeader.aV5Endpoints.aXyzGreen.aXyzX; nUsed += sizeof(rHeader.aV5Endpoints.aXyzGreen.aXyzX); } 220 if(nUsed < rHeader.nSize) { rIStm >> rHeader.aV5Endpoints.aXyzGreen.aXyzY; nUsed += sizeof(rHeader.aV5Endpoints.aXyzGreen.aXyzY); } 221 if(nUsed < rHeader.nSize) { rIStm >> rHeader.aV5Endpoints.aXyzGreen.aXyzZ; nUsed += sizeof(rHeader.aV5Endpoints.aXyzGreen.aXyzZ); } 222 if(nUsed < rHeader.nSize) { rIStm >> rHeader.aV5Endpoints.aXyzBlue.aXyzX; nUsed += sizeof(rHeader.aV5Endpoints.aXyzBlue.aXyzX); } 223 if(nUsed < rHeader.nSize) { rIStm >> rHeader.aV5Endpoints.aXyzBlue.aXyzY; nUsed += sizeof(rHeader.aV5Endpoints.aXyzBlue.aXyzY); } 224 if(nUsed < rHeader.nSize) { rIStm >> rHeader.aV5Endpoints.aXyzBlue.aXyzZ; nUsed += sizeof(rHeader.aV5Endpoints.aXyzBlue.aXyzZ); } 225 226 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nV5GammaRed; nUsed += sizeof(rHeader.nV5GammaRed); } 227 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nV5GammaGreen; nUsed += sizeof(rHeader.nV5GammaGreen); } 228 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nV5GammaBlue; nUsed += sizeof(rHeader.nV5GammaBlue); } 229 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nV5Intent; nUsed += sizeof(rHeader.nV5Intent); } 230 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nV5ProfileData; nUsed += sizeof(rHeader.nV5ProfileData); } 231 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nV5ProfileSize; nUsed += sizeof(rHeader.nV5ProfileSize); } 232 if(nUsed < rHeader.nSize) { rIStm >> rHeader.nV5Reserved; nUsed += sizeof(rHeader.nV5Reserved); } 233 234 // seek to EndPos 235 rIStm.Seek(aStartPos + rHeader.nSize); 236 } 237 238 if ( rHeader.nHeight < 0 ) 239 { 240 bTopDown = true; 241 rHeader.nHeight *= -1; 242 } 243 else 244 { 245 bTopDown = false; 246 } 247 248 if ( rHeader.nWidth < 0 ) 249 { 250 rIStm.SetError( SVSTREAM_FILEFORMAT_ERROR ); 251 } 252 253 // #144105# protect a little against damaged files 254 if( rHeader.nSizeImage > ( 16 * static_cast< sal_uInt32 >( rHeader.nWidth * rHeader.nHeight ) ) ) 255 { 256 rHeader.nSizeImage = 0; 257 } 258 259 return( ( rHeader.nPlanes == 1 ) && ( rIStm.GetError() == 0UL ) ); 260 } 261 262 bool ImplReadDIBPalette( SvStream& rIStm, BitmapWriteAccess& rAcc, bool bQuad ) 263 { 264 const sal_uInt16 nColors = rAcc.GetPaletteEntryCount(); 265 const sal_uLong nPalSize = nColors * ( bQuad ? 4UL : 3UL ); 266 BitmapColor aPalColor; 267 268 sal_uInt8* pEntries = new sal_uInt8[ nPalSize ]; 269 rIStm.Read( pEntries, nPalSize ); 270 271 sal_uInt8* pTmpEntry = pEntries; 272 for( sal_uInt16 i = 0; i < nColors; i++ ) 273 { 274 aPalColor.SetBlue( *pTmpEntry++ ); 275 aPalColor.SetGreen( *pTmpEntry++ ); 276 aPalColor.SetRed( *pTmpEntry++ ); 277 278 if( bQuad ) 279 pTmpEntry++; 280 281 rAcc.SetPaletteColor( i, aPalColor ); 282 } 283 284 delete[] pEntries; 285 286 return( rIStm.GetError() == 0UL ); 287 } 288 289 void ImplDecodeRLE( sal_uInt8* pBuffer, DIBV5Header& rHeader, BitmapWriteAccess& rAcc, bool bRLE4 ) 290 { 291 Scanline pRLE = pBuffer; 292 long nY = rHeader.nHeight - 1L; 293 const sal_uLong nWidth = rAcc.Width(); 294 sal_uLong nCountByte; 295 sal_uLong nRunByte; 296 sal_uLong nX = 0UL; 297 sal_uInt8 cTmp; 298 bool bEndDecoding = false; 299 300 do 301 { 302 if( ( nCountByte = *pRLE++ ) == 0 ) 303 { 304 nRunByte = *pRLE++; 305 306 if( nRunByte > 2 ) 307 { 308 if( bRLE4 ) 309 { 310 nCountByte = nRunByte >> 1; 311 312 for( sal_uLong i = 0UL; i < nCountByte; i++ ) 313 { 314 cTmp = *pRLE++; 315 316 if( nX < nWidth ) 317 rAcc.SetPixelIndex( nY, nX++, cTmp >> 4 ); 318 319 if( nX < nWidth ) 320 rAcc.SetPixelIndex( nY, nX++, cTmp & 0x0f ); 321 } 322 323 if( nRunByte & 1 ) 324 { 325 if( nX < nWidth ) 326 rAcc.SetPixelIndex( nY, nX++, *pRLE >> 4 ); 327 328 pRLE++; 329 } 330 331 if( ( ( nRunByte + 1 ) >> 1 ) & 1 ) 332 pRLE++; 333 } 334 else 335 { 336 for( sal_uLong i = 0UL; i < nRunByte; i++ ) 337 { 338 if( nX < nWidth ) 339 rAcc.SetPixelIndex( nY, nX++, *pRLE ); 340 341 pRLE++; 342 } 343 344 if( nRunByte & 1 ) 345 pRLE++; 346 } 347 } 348 else if( !nRunByte ) 349 { 350 nY--; 351 nX = 0UL; 352 } 353 else if( nRunByte == 1 ) 354 bEndDecoding = true; 355 else 356 { 357 nX += *pRLE++; 358 nY -= *pRLE++; 359 } 360 } 361 else 362 { 363 cTmp = *pRLE++; 364 365 if( bRLE4 ) 366 { 367 nRunByte = nCountByte >> 1; 368 369 for( sal_uLong i = 0UL; i < nRunByte; i++ ) 370 { 371 if( nX < nWidth ) 372 rAcc.SetPixelIndex( nY, nX++, cTmp >> 4 ); 373 374 if( nX < nWidth ) 375 rAcc.SetPixelIndex( nY, nX++, cTmp & 0x0f ); 376 } 377 378 if( ( nCountByte & 1 ) && ( nX < nWidth ) ) 379 rAcc.SetPixelIndex( nY, nX++, cTmp >> 4 ); 380 } 381 else 382 { 383 for( sal_uLong i = 0UL; ( i < nCountByte ) && ( nX < nWidth ); i++ ) 384 rAcc.SetPixelIndex( nY, nX++, cTmp ); 385 } 386 } 387 } 388 while ( !bEndDecoding && ( nY >= 0L ) ); 389 } 390 391 bool ImplReadDIBBits(SvStream& rIStm, DIBV5Header& rHeader, BitmapWriteAccess& rAcc, BitmapWriteAccess* pAccAlpha, bool bTopDown, bool& rAlphaUsed) 392 { 393 const sal_Int64 nBitsPerLine (static_cast<sal_Int64>(rHeader.nWidth) * static_cast<sal_Int64>(rHeader.nBitCount)); 394 if (nBitsPerLine > SAL_MAX_UINT32) 395 return false; 396 397 const sal_uLong nAlignedWidth = AlignedWidth4Bytes(static_cast<sal_uLong>(nBitsPerLine)); 398 sal_uInt32 nRMask(( rHeader.nBitCount == 16 ) ? 0x00007c00UL : 0x00ff0000UL); 399 sal_uInt32 nGMask(( rHeader.nBitCount == 16 ) ? 0x000003e0UL : 0x0000ff00UL); 400 sal_uInt32 nBMask(( rHeader.nBitCount == 16 ) ? 0x0000001fUL : 0x000000ffUL); 401 bool bNative(false); 402 bool bTCMask(!pAccAlpha && ((16 == rHeader.nBitCount) || (32 == rHeader.nBitCount))); 403 bool bRLE((RLE_8 == rHeader.nCompression && 8 == rHeader.nBitCount) || (RLE_4 == rHeader.nCompression && 4 == rHeader.nBitCount)); 404 405 // Is native format? 406 switch(rAcc.GetScanlineFormat()) 407 { 408 case( BMP_FORMAT_1BIT_MSB_PAL ): 409 case( BMP_FORMAT_4BIT_MSN_PAL ): 410 case( BMP_FORMAT_8BIT_PAL ): 411 case( BMP_FORMAT_24BIT_TC_BGR ): 412 { 413 bNative = ( ( static_cast< bool >(rAcc.IsBottomUp()) != bTopDown ) && !bRLE && !bTCMask && ( rAcc.GetScanlineSize() == nAlignedWidth ) ); 414 break; 415 } 416 417 default: 418 { 419 break; 420 } 421 } 422 423 // Read data 424 if(bNative) 425 { 426 rIStm.Read(rAcc.GetBuffer(), rHeader.nHeight * nAlignedWidth); 427 } 428 else 429 { 430 // Read color mask 431 if(bTCMask && BITFIELDS == rHeader.nCompression) 432 { 433 rIStm.SeekRel( -12L ); 434 rIStm >> nRMask; 435 rIStm >> nGMask; 436 rIStm >> nBMask; 437 } 438 439 if(bRLE) 440 { 441 if(!rHeader.nSizeImage) 442 { 443 const sal_uLong nOldPos(rIStm.Tell()); 444 445 rIStm.Seek(STREAM_SEEK_TO_END); 446 rHeader.nSizeImage = rIStm.Tell() - nOldPos; 447 rIStm.Seek(nOldPos); 448 } 449 450 sal_uInt8* pBuffer = (sal_uInt8*)rtl_allocateMemory(rHeader.nSizeImage); 451 rIStm.Read((char*)pBuffer, rHeader.nSizeImage); 452 ImplDecodeRLE(pBuffer, rHeader, rAcc, RLE_4 == rHeader.nCompression); 453 rtl_freeMemory(pBuffer); 454 } 455 else 456 { 457 const long nWidth(rHeader.nWidth); 458 const long nHeight(rHeader.nHeight); 459 sal_uInt8* pBuf = new sal_uInt8[nAlignedWidth]; 460 461 const long nI(bTopDown ? 1 : -1); 462 long nY(bTopDown ? 0 : nHeight - 1); 463 long nCount(nHeight); 464 465 switch(rHeader.nBitCount) 466 { 467 case( 1 ): 468 { 469 sal_uInt8* pTmp; 470 sal_uInt8 cTmp; 471 472 for( ; nCount--; nY += nI ) 473 { 474 rIStm.Read( pTmp = pBuf, nAlignedWidth ); 475 cTmp = *pTmp++; 476 477 for( long nX = 0L, nShift = 8L; nX < nWidth; nX++ ) 478 { 479 if( !nShift ) 480 { 481 nShift = 8L, 482 cTmp = *pTmp++; 483 } 484 485 rAcc.SetPixelIndex( nY, nX, (cTmp >> --nShift) & 1); 486 } 487 } 488 } 489 break; 490 491 case( 4 ): 492 { 493 sal_uInt8* pTmp; 494 sal_uInt8 cTmp; 495 496 for( ; nCount--; nY += nI ) 497 { 498 rIStm.Read( pTmp = pBuf, nAlignedWidth ); 499 cTmp = *pTmp++; 500 501 for( long nX = 0L, nShift = 2L; nX < nWidth; nX++ ) 502 { 503 if( !nShift ) 504 { 505 nShift = 2UL, 506 cTmp = *pTmp++; 507 } 508 509 rAcc.SetPixelIndex( nY, nX, (cTmp >> ( --nShift << 2UL ) ) & 0x0f); 510 } 511 } 512 } 513 break; 514 515 case( 8 ): 516 { 517 sal_uInt8* pTmp; 518 519 for( ; nCount--; nY += nI ) 520 { 521 rIStm.Read( pTmp = pBuf, nAlignedWidth ); 522 523 for( long nX = 0L; nX < nWidth; nX++ ) 524 rAcc.SetPixelIndex( nY, nX, *pTmp++ ); 525 } 526 } 527 break; 528 529 case( 16 ): 530 { 531 ColorMask aMask( nRMask, nGMask, nBMask ); 532 BitmapColor aColor; 533 sal_uInt16* pTmp16; 534 535 for( ; nCount--; nY += nI ) 536 { 537 rIStm.Read( (char*)( pTmp16 = (sal_uInt16*) pBuf ), nAlignedWidth ); 538 539 for( long nX = 0L; nX < nWidth; nX++ ) 540 { 541 aMask.GetColorFor16BitLSB( aColor, (sal_uInt8*) pTmp16++ ); 542 rAcc.SetPixel( nY, nX, aColor ); 543 } 544 } 545 } 546 break; 547 548 case( 24 ): 549 { 550 BitmapColor aPixelColor; 551 sal_uInt8* pTmp; 552 553 for( ; nCount--; nY += nI ) 554 { 555 rIStm.Read( pTmp = pBuf, nAlignedWidth ); 556 557 for( long nX = 0L; nX < nWidth; nX++ ) 558 { 559 aPixelColor.SetBlue( *pTmp++ ); 560 aPixelColor.SetGreen( *pTmp++ ); 561 aPixelColor.SetRed( *pTmp++ ); 562 rAcc.SetPixel( nY, nX, aPixelColor ); 563 } 564 } 565 } 566 break; 567 568 case( 32 ): 569 { 570 ColorMask aMask(nRMask, nGMask, nBMask); 571 BitmapColor aColor; 572 sal_uInt32* pTmp32; 573 574 if(pAccAlpha) 575 { 576 sal_uInt8 aAlpha; 577 578 for( ; nCount--; nY += nI ) 579 { 580 rIStm.Read( (char*)( pTmp32 = (sal_uInt32*) pBuf ), nAlignedWidth ); 581 582 for( long nX = 0L; nX < nWidth; nX++ ) 583 { 584 aMask.GetColorAndAlphaFor32Bit( aColor, aAlpha, (sal_uInt8*) pTmp32++ ); 585 rAcc.SetPixel( nY, nX, aColor ); 586 pAccAlpha->SetPixelIndex(nY, nX, sal_uInt8(0xff) - aAlpha); 587 rAlphaUsed |= bool(0xff != aAlpha); 588 } 589 } 590 } 591 else 592 { 593 for( ; nCount--; nY += nI ) 594 { 595 rIStm.Read( (char*)( pTmp32 = (sal_uInt32*) pBuf ), nAlignedWidth ); 596 597 for( long nX = 0L; nX < nWidth; nX++ ) 598 { 599 aMask.GetColorFor32Bit( aColor, (sal_uInt8*) pTmp32++ ); 600 rAcc.SetPixel( nY, nX, aColor ); 601 } 602 } 603 } 604 } 605 } 606 607 delete[] pBuf; 608 } 609 } 610 611 return( rIStm.GetError() == 0UL ); 612 } 613 614 bool ImplReadDIBBody( SvStream& rIStm, Bitmap& rBmp, Bitmap* pBmpAlpha, sal_uLong nOffset ) 615 { 616 DIBV5Header aHeader; 617 const sal_uLong nStmPos = rIStm.Tell(); 618 bool bRet(false); 619 bool bTopDown(false); 620 621 if(ImplReadDIBInfoHeader(rIStm, aHeader, bTopDown) && aHeader.nWidth && aHeader.nHeight && aHeader.nBitCount) 622 { 623 if (aHeader.nSize > nOffset) 624 { 625 // Header size claims to extend into the image data. 626 // Looks like an error. 627 return false; 628 } 629 630 const sal_uInt16 nBitCount(discretizeBitcount(aHeader.nBitCount)); 631 const Size aSizePixel(aHeader.nWidth, aHeader.nHeight); 632 BitmapPalette aDummyPal; 633 Bitmap aNewBmp(aSizePixel, nBitCount, &aDummyPal); 634 Bitmap aNewBmpAlpha; 635 BitmapWriteAccess* pAcc = aNewBmp.AcquireWriteAccess(); 636 BitmapWriteAccess* pAccAlpha = 0; 637 bool bAlphaPossible(pBmpAlpha && aHeader.nBitCount == 32); 638 639 if(bAlphaPossible) 640 { 641 const bool bRedSet(0 != aHeader.nV5RedMask); 642 const bool bGreenSet(0 != aHeader.nV5GreenMask); 643 const bool bBlueSet(0 != aHeader.nV5BlueMask); 644 645 // some clipboard entries have alpha mask on zero to say that there is 646 // no alpha; do only use this when the other masks are set. The MS docu 647 // says that that masks are only to be set when bV5Compression is set to 648 // BI_BITFIELDS, but there seem to exist a wild variety of usages... 649 if((bRedSet || bGreenSet || bBlueSet) && (0 == aHeader.nV5AlphaMask)) 650 { 651 bAlphaPossible = false; 652 } 653 } 654 655 if(bAlphaPossible) 656 { 657 aNewBmpAlpha = Bitmap(aSizePixel, 8); 658 pAccAlpha = aNewBmpAlpha.AcquireWriteAccess(); 659 } 660 661 if(pAcc) 662 { 663 sal_uInt16 nColors(0); 664 SvStream* pIStm; 665 SvMemoryStream* pMemStm = NULL; 666 sal_uInt8* pData = NULL; 667 668 if(nBitCount <= 8) 669 { 670 if(aHeader.nColsUsed) 671 { 672 nColors = (sal_uInt16)aHeader.nColsUsed; 673 } 674 else 675 { 676 nColors = ( 1 << aHeader.nBitCount ); 677 } 678 } 679 680 if(ZCOMPRESS == aHeader.nCompression) 681 { 682 ZCodec aCodec; 683 sal_uInt32 nCodedSize(0); 684 sal_uInt32 nUncodedSize(0); 685 sal_uLong nCodedPos(0); 686 687 // read coding information 688 rIStm >> nCodedSize >> nUncodedSize >> aHeader.nCompression; 689 pData = (sal_uInt8*) rtl_allocateMemory( nUncodedSize ); 690 691 // decode buffer 692 nCodedPos = rIStm.Tell(); 693 aCodec.BeginCompression(); 694 aCodec.Read( rIStm, pData, nUncodedSize ); 695 aCodec.EndCompression(); 696 697 // skip unread bytes from coded buffer 698 rIStm.SeekRel( nCodedSize - ( rIStm.Tell() - nCodedPos ) ); 699 700 // set decoded bytes to memory stream, 701 // from which we will read the bitmap data 702 pIStm = pMemStm = new SvMemoryStream; 703 pMemStm->SetBuffer( (char*) pData, nUncodedSize, false, nUncodedSize ); 704 nOffset = 0; 705 } 706 else 707 { 708 pIStm = &rIStm; 709 } 710 711 // read palette 712 if(nColors) 713 { 714 pAcc->SetPaletteEntryCount(nColors); 715 ImplReadDIBPalette(*pIStm, *pAcc, aHeader.nSize != DIBCOREHEADERSIZE); 716 } 717 718 // read bits 719 bool bAlphaUsed(false); 720 721 if(!pIStm->GetError()) 722 { 723 if(nOffset) 724 { 725 pIStm->SeekRel(nOffset - (pIStm->Tell() - nStmPos)); 726 } 727 728 bRet = ImplReadDIBBits(*pIStm, aHeader, *pAcc, pAccAlpha, bTopDown, bAlphaUsed); 729 730 if(bRet && aHeader.nXPelsPerMeter && aHeader.nYPelsPerMeter) 731 { 732 MapMode aMapMode( 733 MAP_MM, 734 Point(), 735 Fraction(1000, aHeader.nXPelsPerMeter), 736 Fraction(1000, aHeader.nYPelsPerMeter)); 737 738 aNewBmp.SetPrefMapMode(aMapMode); 739 aNewBmp.SetPrefSize(Size(aHeader.nWidth, aHeader.nHeight)); 740 } 741 } 742 743 if( pData ) 744 { 745 rtl_freeMemory(pData); 746 } 747 748 delete pMemStm; 749 aNewBmp.ReleaseAccess(pAcc); 750 751 if(bAlphaPossible) 752 { 753 aNewBmpAlpha.ReleaseAccess(pAccAlpha); 754 755 if(!bAlphaUsed) 756 { 757 bAlphaPossible = false; 758 } 759 } 760 761 if(bRet) 762 { 763 rBmp = aNewBmp; 764 765 if(bAlphaPossible) 766 { 767 *pBmpAlpha = aNewBmpAlpha; 768 } 769 } 770 } 771 } 772 773 return bRet; 774 } 775 776 bool ImplReadDIBFileHeader( SvStream& rIStm, sal_uLong& rOffset ) 777 { 778 sal_uInt32 nTmp32; 779 sal_uInt16 nTmp16 = 0; 780 bool bRet = false; 781 782 const sal_Int64 nStreamLength (rIStm.Seek(STREAM_SEEK_TO_END)); 783 rIStm.Seek(STREAM_SEEK_TO_BEGIN); 784 785 rIStm >> nTmp16; 786 787 if ( ( 0x4D42 == nTmp16 ) || ( 0x4142 == nTmp16 ) ) 788 { 789 if ( 0x4142 == nTmp16 ) 790 { 791 rIStm.SeekRel( 12L ); 792 rIStm >> nTmp16; 793 rIStm.SeekRel( 8L ); 794 rIStm >> nTmp32; 795 rOffset = nTmp32 - 28UL; 796 bRet = ( 0x4D42 == nTmp16 ); 797 } 798 else // 0x4D42 == nTmp16, 'MB' from BITMAPFILEHEADER 799 { 800 rIStm.SeekRel( 8L ); // we are on bfSize member of BITMAPFILEHEADER, forward to bfOffBits 801 rIStm >> nTmp32; // read bfOffBits 802 rOffset = nTmp32 - 14UL; // adapt offset by sizeof(BITMAPFILEHEADER) 803 bRet = ( rIStm.GetError() == 0UL ); 804 } 805 806 if (rOffset >= nStreamLength) 807 { 808 // Offset claims that image starts past the end of the 809 // stream. Unlikely. 810 rIStm.SetError( SVSTREAM_FILEFORMAT_ERROR ); 811 bRet = false; 812 } 813 } 814 else 815 rIStm.SetError( SVSTREAM_FILEFORMAT_ERROR ); 816 817 return bRet; 818 } 819 820 bool ImplWriteDIBPalette( SvStream& rOStm, BitmapReadAccess& rAcc ) 821 { 822 const sal_uInt16 nColors = rAcc.GetPaletteEntryCount(); 823 const sal_uLong nPalSize = nColors * 4UL; 824 sal_uInt8* pEntries = new sal_uInt8[ nPalSize ]; 825 sal_uInt8* pTmpEntry = pEntries; 826 BitmapColor aPalColor; 827 828 for( sal_uInt16 i = 0; i < nColors; i++ ) 829 { 830 const BitmapColor& rPalColor = rAcc.GetPaletteColor( i ); 831 832 *pTmpEntry++ = rPalColor.GetBlue(); 833 *pTmpEntry++ = rPalColor.GetGreen(); 834 *pTmpEntry++ = rPalColor.GetRed(); 835 *pTmpEntry++ = 0; 836 } 837 838 rOStm.Write( pEntries, nPalSize ); 839 delete[] pEntries; 840 841 return( rOStm.GetError() == 0UL ); 842 } 843 844 bool ImplWriteRLE( SvStream& rOStm, BitmapReadAccess& rAcc, bool bRLE4 ) 845 { 846 const sal_uLong nWidth = rAcc.Width(); 847 const sal_uLong nHeight = rAcc.Height(); 848 sal_uLong nX; 849 sal_uLong nSaveIndex; 850 sal_uLong nCount; 851 sal_uLong nBufCount; 852 sal_uInt8* pBuf = new sal_uInt8[ ( nWidth << 1 ) + 2 ]; 853 sal_uInt8* pTmp; 854 sal_uInt8 cPix; 855 sal_uInt8 cLast; 856 bool bFound; 857 858 for ( long nY = nHeight - 1L; nY >= 0L; nY-- ) 859 { 860 pTmp = pBuf; 861 nX = nBufCount = 0UL; 862 863 while( nX < nWidth ) 864 { 865 nCount = 1L; 866 cPix = rAcc.GetPixelIndex( nY, nX++ ); 867 868 while( ( nX < nWidth ) && ( nCount < 255L ) 869 && ( cPix == rAcc.GetPixelIndex( nY, nX ) ) ) 870 { 871 nX++; 872 nCount++; 873 } 874 875 if ( nCount > 1 ) 876 { 877 *pTmp++ = (sal_uInt8) nCount; 878 *pTmp++ = ( bRLE4 ? ( ( cPix << 4 ) | cPix ) : cPix ); 879 nBufCount += 2; 880 } 881 else 882 { 883 cLast = cPix; 884 nSaveIndex = nX - 1UL; 885 bFound = false; 886 887 while( ( nX < nWidth ) && ( nCount < 256L ) 888 && ( cPix = rAcc.GetPixelIndex( nY, nX ) ) != cLast ) 889 { 890 nX++; nCount++; 891 cLast = cPix; 892 bFound = true; 893 } 894 895 if ( bFound ) 896 nX--; 897 898 if ( nCount > 3 ) 899 { 900 *pTmp++ = 0; 901 *pTmp++ = (sal_uInt8) --nCount; 902 903 if( bRLE4 ) 904 { 905 for ( sal_uLong i = 0; i < nCount; i++, pTmp++ ) 906 { 907 *pTmp = rAcc.GetPixelIndex( nY, nSaveIndex++ ) << 4; 908 909 if ( ++i < nCount ) 910 *pTmp |= rAcc.GetPixelIndex( nY, nSaveIndex++ ); 911 } 912 913 nCount = ( nCount + 1 ) >> 1; 914 } 915 else 916 { 917 for( sal_uLong i = 0UL; i < nCount; i++ ) 918 *pTmp++ = rAcc.GetPixelIndex( nY, nSaveIndex++ ); 919 } 920 921 if ( nCount & 1 ) 922 { 923 *pTmp++ = 0; 924 nBufCount += ( nCount + 3 ); 925 } 926 else 927 nBufCount += ( nCount + 2 ); 928 } 929 else 930 { 931 *pTmp++ = 1; 932 *pTmp++ = rAcc.GetPixelIndex( nY, nSaveIndex ) << (bRLE4 ? 4 : 0); 933 934 if ( nCount == 3 ) 935 { 936 *pTmp++ = 1; 937 *pTmp++ = rAcc.GetPixelIndex( nY, ++nSaveIndex ) << ( bRLE4 ? 4 : 0 ); 938 nBufCount += 4; 939 } 940 else 941 nBufCount += 2; 942 } 943 } 944 } 945 946 pBuf[ nBufCount++ ] = 0; 947 pBuf[ nBufCount++ ] = 0; 948 949 rOStm.Write( pBuf, nBufCount ); 950 } 951 952 rOStm << (sal_uInt8) 0; 953 rOStm << (sal_uInt8) 1; 954 955 delete[] pBuf; 956 957 return( rOStm.GetError() == 0UL ); 958 } 959 960 bool ImplWriteDIBBits(SvStream& rOStm, BitmapReadAccess& rAcc, BitmapReadAccess* pAccAlpha, sal_uLong nCompression, sal_uInt32& rImageSize) 961 { 962 if(!pAccAlpha && BITFIELDS == nCompression) 963 { 964 const ColorMask& rMask = rAcc.GetColorMask(); 965 SVBT32 aVal32; 966 967 UInt32ToSVBT32( rMask.GetRedMask(), aVal32 ); 968 rOStm.Write( (sal_uInt8*) aVal32, 4UL ); 969 970 UInt32ToSVBT32( rMask.GetGreenMask(), aVal32 ); 971 rOStm.Write( (sal_uInt8*) aVal32, 4UL ); 972 973 UInt32ToSVBT32( rMask.GetBlueMask(), aVal32 ); 974 rOStm.Write( (sal_uInt8*) aVal32, 4UL ); 975 976 rImageSize = rOStm.Tell(); 977 978 if( rAcc.IsBottomUp() ) 979 rOStm.Write( rAcc.GetBuffer(), rAcc.Height() * rAcc.GetScanlineSize() ); 980 else 981 { 982 for( long nY = rAcc.Height() - 1, nScanlineSize = rAcc.GetScanlineSize(); nY >= 0L; nY-- ) 983 rOStm.Write( rAcc.GetScanline( nY ), nScanlineSize ); 984 } 985 } 986 else if(!pAccAlpha && ((RLE_4 == nCompression) || (RLE_8 == nCompression))) 987 { 988 rImageSize = rOStm.Tell(); 989 ImplWriteRLE( rOStm, rAcc, RLE_4 == nCompression ); 990 } 991 else if(!nCompression) 992 { 993 // #i5xxx# Limit bitcount to 24bit, the 32 bit cases are not 994 // handled properly below (would have to set color masks, and 995 // nCompression=BITFIELDS - but color mask is not set for 996 // formats != *_TC_*). Note that this very problem might cause 997 // trouble at other places - the introduction of 32 bit RGBA 998 // bitmaps is relatively recent. 999 // #i59239# discretize bitcount for aligned width to 1,4,8,24 1000 // (other cases are not written below) 1001 const sal_uInt16 nBitCount(pAccAlpha ? 32 : discretizeBitcount(static_cast< sal_uInt16 >(rAcc.GetBitCount()))); 1002 const sal_uLong nAlignedWidth(AlignedWidth4Bytes(rAcc.Width() * nBitCount)); 1003 bool bNative(false); 1004 1005 switch(rAcc.GetScanlineFormat()) 1006 { 1007 case( BMP_FORMAT_1BIT_MSB_PAL ): 1008 case( BMP_FORMAT_4BIT_MSN_PAL ): 1009 case( BMP_FORMAT_8BIT_PAL ): 1010 case( BMP_FORMAT_24BIT_TC_BGR ): 1011 { 1012 if(!pAccAlpha && rAcc.IsBottomUp() && (rAcc.GetScanlineSize() == nAlignedWidth)) 1013 { 1014 bNative = true; 1015 } 1016 1017 break; 1018 } 1019 1020 default: 1021 { 1022 break; 1023 } 1024 } 1025 1026 rImageSize = rOStm.Tell(); 1027 1028 if(bNative) 1029 { 1030 rOStm.Write(rAcc.GetBuffer(), nAlignedWidth * rAcc.Height()); 1031 } 1032 else 1033 { 1034 const long nWidth(rAcc.Width()); 1035 const long nHeight(rAcc.Height()); 1036 sal_uInt8* pBuf = new sal_uInt8[ nAlignedWidth ]; 1037 sal_uInt8* pTmp(0); 1038 sal_uInt8 cTmp(0); 1039 1040 switch( nBitCount ) 1041 { 1042 case( 1 ): 1043 { 1044 for( long nY = nHeight - 1; nY >= 0L; nY-- ) 1045 { 1046 pTmp = pBuf; 1047 cTmp = 0; 1048 1049 for( long nX = 0L, nShift = 8L; nX < nWidth; nX++ ) 1050 { 1051 if( !nShift ) 1052 { 1053 nShift = 8L; 1054 *pTmp++ = cTmp; 1055 cTmp = 0; 1056 } 1057 1058 cTmp |= rAcc.GetPixelIndex( nY, nX ) << --nShift; 1059 } 1060 1061 *pTmp = cTmp; 1062 rOStm.Write( pBuf, nAlignedWidth ); 1063 } 1064 } 1065 break; 1066 1067 case( 4 ): 1068 { 1069 for( long nY = nHeight - 1; nY >= 0L; nY-- ) 1070 { 1071 pTmp = pBuf; 1072 cTmp = 0; 1073 1074 for( long nX = 0L, nShift = 2L; nX < nWidth; nX++ ) 1075 { 1076 if( !nShift ) 1077 { 1078 nShift = 2L; 1079 *pTmp++ = cTmp; 1080 cTmp = 0; 1081 } 1082 1083 cTmp |= rAcc.GetPixelIndex( nY, nX ) << ( --nShift << 2L ); 1084 } 1085 *pTmp = cTmp; 1086 rOStm.Write( pBuf, nAlignedWidth ); 1087 } 1088 } 1089 break; 1090 1091 case( 8 ): 1092 { 1093 for( long nY = nHeight - 1; nY >= 0L; nY-- ) 1094 { 1095 pTmp = pBuf; 1096 1097 for( long nX = 0L; nX < nWidth; nX++ ) 1098 *pTmp++ = rAcc.GetPixelIndex( nY, nX ); 1099 1100 rOStm.Write( pBuf, nAlignedWidth ); 1101 } 1102 } 1103 break; 1104 1105 // #i59239# fallback to 24 bit format, if bitcount is non-default 1106 default: 1107 // FALLTHROUGH intended 1108 case( 24 ): 1109 { 1110 BitmapColor aPixelColor; 1111 const bool bWriteAlpha(32 == nBitCount && pAccAlpha); 1112 1113 for( long nY = nHeight - 1; nY >= 0L; nY-- ) 1114 { 1115 pTmp = pBuf; 1116 1117 for( long nX = 0L; nX < nWidth; nX++ ) 1118 { 1119 // when alpha is used, this may be non-24bit main bitmap, so use GetColor 1120 // instead of GetPixel to ensure RGB value 1121 aPixelColor = rAcc.GetColor( nY, nX ); 1122 1123 *pTmp++ = aPixelColor.GetBlue(); 1124 *pTmp++ = aPixelColor.GetGreen(); 1125 *pTmp++ = aPixelColor.GetRed(); 1126 1127 if(bWriteAlpha) 1128 { 1129 if(pAccAlpha) 1130 { 1131 *pTmp++ = (sal_uInt8)0xff - (sal_uInt8)pAccAlpha->GetPixelIndex( nY, nX ); 1132 } 1133 else 1134 { 1135 *pTmp++ = (sal_uInt8)0xff; 1136 } 1137 } 1138 } 1139 1140 rOStm.Write( pBuf, nAlignedWidth ); 1141 } 1142 } 1143 break; 1144 } 1145 1146 delete[] pBuf; 1147 } 1148 } 1149 1150 rImageSize = rOStm.Tell() - rImageSize; 1151 1152 return (!rOStm.GetError()); 1153 } 1154 1155 bool ImplWriteDIBBody(const Bitmap& rBitmap, SvStream& rOStm, BitmapReadAccess& rAcc, BitmapReadAccess* pAccAlpha, bool bCompressed) 1156 { 1157 const MapMode aMapPixel(MAP_PIXEL); 1158 DIBV5Header aHeader; 1159 sal_uLong nImageSizePos(0); 1160 sal_uLong nEndPos(0); 1161 sal_uInt32 nCompression(COMPRESS_NONE); 1162 bool bRet(false); 1163 1164 aHeader.nSize = pAccAlpha ? DIBV5HEADERSIZE : DIBINFOHEADERSIZE; // size dependent on CF_DIB type to use 1165 aHeader.nWidth = rAcc.Width(); 1166 aHeader.nHeight = rAcc.Height(); 1167 aHeader.nPlanes = 1; 1168 1169 if(!pAccAlpha && isBitfieldCompression(rAcc.GetScanlineFormat())) 1170 { 1171 aHeader.nBitCount = (BMP_FORMAT_16BIT_TC_LSB_MASK == rAcc.GetScanlineFormat()) ? 16 : 32; 1172 aHeader.nSizeImage = rAcc.Height() * rAcc.GetScanlineSize(); 1173 nCompression = BITFIELDS; 1174 } 1175 else 1176 { 1177 // #i5xxx# Limit bitcount to 24bit, the 32 bit cases are 1178 // not handled properly below (would have to set color 1179 // masks, and nCompression=BITFIELDS - but color mask is 1180 // not set for formats != *_TC_*). Note that this very 1181 // problem might cause trouble at other places - the 1182 // introduction of 32 bit RGBA bitmaps is relatively 1183 // recent. 1184 // #i59239# discretize bitcount to 1,4,8,24 (other cases 1185 // are not written below) 1186 const sal_uInt16 nBitCount(pAccAlpha ? 32 : discretizeBitcount(static_cast< sal_uInt16 >(rAcc.GetBitCount()))); 1187 aHeader.nBitCount = nBitCount; 1188 aHeader.nSizeImage = rAcc.Height() * AlignedWidth4Bytes(rAcc.Width() * aHeader.nBitCount); 1189 1190 if(bCompressed) 1191 { 1192 if(4 == nBitCount) 1193 { 1194 nCompression = RLE_4; 1195 } 1196 else if(8 == nBitCount) 1197 { 1198 nCompression = RLE_8; 1199 } 1200 } 1201 } 1202 1203 if((rOStm.GetCompressMode() & COMPRESSMODE_ZBITMAP) && (rOStm.GetVersion() >= SOFFICE_FILEFORMAT_40)) 1204 { 1205 aHeader.nCompression = ZCOMPRESS; 1206 } 1207 else 1208 { 1209 aHeader.nCompression = nCompression; 1210 } 1211 1212 if(rBitmap.GetPrefSize().Width() && rBitmap.GetPrefSize().Height() && (rBitmap.GetPrefMapMode() != aMapPixel)) 1213 { 1214 // #i48108# Try to recover xpels/ypels as previously stored on 1215 // disk. The problem with just converting maPrefSize to 100th 1216 // mm and then relating that to the bitmap pixel size is that 1217 // MapMode is integer-based, and suffers from roundoffs, 1218 // especially if maPrefSize is small. Trying to circumvent 1219 // that by performing part of the math in floating point. 1220 const Size aScale100000(OutputDevice::LogicToLogic(Size(100000L, 100000L), MAP_100TH_MM, rBitmap.GetPrefMapMode())); 1221 const double fBmpWidthM((double)rBitmap.GetPrefSize().Width() / aScale100000.Width()); 1222 const double fBmpHeightM((double)rBitmap.GetPrefSize().Height() / aScale100000.Height()); 1223 1224 if(!basegfx::fTools::equalZero(fBmpWidthM) && !basegfx::fTools::equalZero(fBmpHeightM)) 1225 { 1226 aHeader.nXPelsPerMeter = basegfx::fround(rAcc.Width() / fabs(fBmpWidthM)); 1227 aHeader.nYPelsPerMeter = basegfx::fround(rAcc.Height() / fabs(fBmpHeightM)); 1228 } 1229 } 1230 1231 aHeader.nColsUsed = ((!pAccAlpha && aHeader.nBitCount <= 8) ? rAcc.GetPaletteEntryCount() : 0); 1232 aHeader.nColsImportant = 0; 1233 1234 rOStm << aHeader.nSize; 1235 rOStm << aHeader.nWidth; 1236 rOStm << aHeader.nHeight; 1237 rOStm << aHeader.nPlanes; 1238 rOStm << aHeader.nBitCount; 1239 rOStm << aHeader.nCompression; 1240 1241 nImageSizePos = rOStm.Tell(); 1242 rOStm.SeekRel( sizeof( aHeader.nSizeImage ) ); 1243 1244 rOStm << aHeader.nXPelsPerMeter; 1245 rOStm << aHeader.nYPelsPerMeter; 1246 rOStm << aHeader.nColsUsed; 1247 rOStm << aHeader.nColsImportant; 1248 1249 if(pAccAlpha) // only write DIBV5 when asked to do so 1250 { 1251 aHeader.nV5CSType = 0x57696E20; // LCS_WINDOWS_COLOR_SPACE 1252 aHeader.nV5Intent = 0x00000004; // LCS_GM_IMAGES 1253 1254 rOStm << aHeader.nV5RedMask; 1255 rOStm << aHeader.nV5GreenMask; 1256 rOStm << aHeader.nV5BlueMask; 1257 rOStm << aHeader.nV5AlphaMask; 1258 rOStm << aHeader.nV5CSType; 1259 1260 rOStm << aHeader.aV5Endpoints.aXyzRed.aXyzX; 1261 rOStm << aHeader.aV5Endpoints.aXyzRed.aXyzY; 1262 rOStm << aHeader.aV5Endpoints.aXyzRed.aXyzZ; 1263 rOStm << aHeader.aV5Endpoints.aXyzGreen.aXyzX; 1264 rOStm << aHeader.aV5Endpoints.aXyzGreen.aXyzY; 1265 rOStm << aHeader.aV5Endpoints.aXyzGreen.aXyzZ; 1266 rOStm << aHeader.aV5Endpoints.aXyzBlue.aXyzX; 1267 rOStm << aHeader.aV5Endpoints.aXyzBlue.aXyzY; 1268 rOStm << aHeader.aV5Endpoints.aXyzBlue.aXyzZ; 1269 1270 rOStm << aHeader.nV5GammaRed; 1271 rOStm << aHeader.nV5GammaGreen; 1272 rOStm << aHeader.nV5GammaBlue; 1273 rOStm << aHeader.nV5Intent; 1274 rOStm << aHeader.nV5ProfileData; 1275 rOStm << aHeader.nV5ProfileSize; 1276 rOStm << aHeader.nV5Reserved; 1277 } 1278 1279 if(ZCOMPRESS == aHeader.nCompression) 1280 { 1281 ZCodec aCodec; 1282 SvMemoryStream aMemStm(aHeader.nSizeImage + 4096, 65535); 1283 sal_uLong nCodedPos(rOStm.Tell()); 1284 sal_uLong nLastPos(0); 1285 sal_uInt32 nCodedSize(0); 1286 sal_uInt32 nUncodedSize(0); 1287 1288 // write uncoded data palette 1289 if(aHeader.nColsUsed) 1290 { 1291 ImplWriteDIBPalette(aMemStm, rAcc); 1292 } 1293 1294 // write uncoded bits 1295 bRet = ImplWriteDIBBits(aMemStm, rAcc, pAccAlpha, nCompression, aHeader.nSizeImage); 1296 1297 // get uncoded size 1298 nUncodedSize = aMemStm.Tell(); 1299 1300 // seek over compress info 1301 rOStm.SeekRel(12); 1302 1303 // write compressed data 1304 aCodec.BeginCompression(3); 1305 aCodec.Write(rOStm, (sal_uInt8*)aMemStm.GetData(), nUncodedSize); 1306 aCodec.EndCompression(); 1307 1308 // update compress info ( coded size, uncoded size, uncoded compression ) 1309 nLastPos = rOStm.Tell(); 1310 nCodedSize = nLastPos - nCodedPos - 12; 1311 rOStm.Seek(nCodedPos); 1312 rOStm << nCodedSize << nUncodedSize << nCompression; 1313 rOStm.Seek(nLastPos); 1314 1315 if(bRet) 1316 { 1317 bRet = (ERRCODE_NONE == rOStm.GetError()); 1318 } 1319 } 1320 else 1321 { 1322 if(aHeader.nColsUsed) 1323 { 1324 ImplWriteDIBPalette(rOStm, rAcc); 1325 } 1326 1327 bRet = ImplWriteDIBBits(rOStm, rAcc, pAccAlpha, aHeader.nCompression, aHeader.nSizeImage); 1328 } 1329 1330 nEndPos = rOStm.Tell(); 1331 rOStm.Seek(nImageSizePos); 1332 rOStm << aHeader.nSizeImage; 1333 rOStm.Seek(nEndPos); 1334 1335 return bRet; 1336 } 1337 1338 bool ImplWriteDIBFileHeader(SvStream& rOStm, BitmapReadAccess& rAcc, bool bUseDIBV5) 1339 { 1340 const sal_uInt32 nPalCount((rAcc.HasPalette() ? rAcc.GetPaletteEntryCount() : isBitfieldCompression(rAcc.GetScanlineFormat()) ? 3UL : 0UL)); 1341 const sal_uInt32 nOffset(14 + (bUseDIBV5 ? DIBV5HEADERSIZE : DIBINFOHEADERSIZE) + nPalCount * 4UL); 1342 1343 rOStm << (sal_uInt16)0x4D42; // 'MB' from BITMAPFILEHEADER 1344 rOStm << (sal_uInt32)(nOffset + (rAcc.Height() * rAcc.GetScanlineSize())); 1345 rOStm << (sal_uInt16)0; 1346 rOStm << (sal_uInt16)0; 1347 rOStm << nOffset; 1348 1349 return( rOStm.GetError() == 0UL ); 1350 } 1351 1352 ////////////////////////////////////////////////////////////////////////////// 1353 1354 bool ImplReadDIB( 1355 Bitmap& rTarget, Bitmap* 1356 pTargetAlpha, 1357 SvStream& rIStm, 1358 bool bFileHeader) 1359 { 1360 const sal_uInt16 nOldFormat(rIStm.GetNumberFormatInt()); 1361 const sal_uLong nOldPos(rIStm.Tell()); 1362 sal_uLong nOffset(0UL); 1363 bool bRet(false); 1364 1365 rIStm.SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN); 1366 1367 if(bFileHeader) 1368 { 1369 if(ImplReadDIBFileHeader(rIStm, nOffset)) 1370 { 1371 bRet = ImplReadDIBBody(rIStm, rTarget, nOffset >= DIBV5HEADERSIZE ? pTargetAlpha : 0, nOffset); 1372 } 1373 } 1374 else 1375 { 1376 bRet = ImplReadDIBBody(rIStm, rTarget, 0, nOffset); 1377 } 1378 1379 if(!bRet) 1380 { 1381 if(!rIStm.GetError()) 1382 { 1383 rIStm.SetError(SVSTREAM_GENERALERROR); 1384 } 1385 1386 rIStm.Seek(nOldPos); 1387 } 1388 1389 rIStm.SetNumberFormatInt(nOldFormat); 1390 1391 return bRet; 1392 } 1393 1394 bool ImplWriteDIB( 1395 const Bitmap& rSource, 1396 const Bitmap* pSourceAlpha, 1397 SvStream& rOStm, 1398 bool bCompressed, 1399 bool bFileHeader) 1400 { 1401 const Size aSizePix(rSource.GetSizePixel()); 1402 bool bRet(false); 1403 1404 if(aSizePix.Width() && aSizePix.Height()) 1405 { 1406 BitmapReadAccess* pAcc = const_cast< Bitmap& >(rSource).AcquireReadAccess(); 1407 BitmapReadAccess* pAccAlpha = 0; 1408 const sal_uInt16 nOldFormat(rOStm.GetNumberFormatInt()); 1409 const sal_uLong nOldPos(rOStm.Tell()); 1410 1411 if(pSourceAlpha) 1412 { 1413 const Size aSizePixAlpha(pSourceAlpha->GetSizePixel()); 1414 1415 if(aSizePixAlpha == aSizePix) 1416 { 1417 pAccAlpha = const_cast< Bitmap* >(pSourceAlpha)->AcquireReadAccess(); 1418 } 1419 else 1420 { 1421 OSL_ENSURE(false, "WriteDIB got an alpha channel, but it's pixel size differs from the base bitmap (!)"); 1422 } 1423 } 1424 1425 rOStm.SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN); 1426 1427 if(pAcc) 1428 { 1429 if(bFileHeader) 1430 { 1431 if(ImplWriteDIBFileHeader(rOStm, *pAcc, 0 != pSourceAlpha)) 1432 { 1433 bRet = ImplWriteDIBBody(rSource, rOStm, *pAcc, pAccAlpha, bCompressed); 1434 } 1435 } 1436 else 1437 { 1438 bRet = ImplWriteDIBBody(rSource, rOStm, *pAcc, pAccAlpha, bCompressed); 1439 } 1440 1441 const_cast< Bitmap& >(rSource).ReleaseAccess(pAcc); 1442 1443 if(pAccAlpha) 1444 { 1445 const_cast< Bitmap* >(pSourceAlpha)->ReleaseAccess(pAccAlpha); 1446 } 1447 } 1448 1449 if(!bRet) 1450 { 1451 rOStm.SetError(SVSTREAM_GENERALERROR); 1452 rOStm.Seek(nOldPos); 1453 } 1454 1455 rOStm.SetNumberFormatInt(nOldFormat); 1456 } 1457 1458 return bRet; 1459 } 1460 1461 ////////////////////////////////////////////////////////////////////////////// 1462 1463 bool ReadDIB( 1464 Bitmap& rTarget, 1465 SvStream& rIStm, 1466 bool bFileHeader) 1467 { 1468 return ImplReadDIB(rTarget, 0, rIStm, bFileHeader); 1469 } 1470 1471 bool ReadDIBBitmapEx( 1472 BitmapEx& rTarget, 1473 SvStream& rIStm) 1474 { 1475 Bitmap aBmp; 1476 bool bRetval(ImplReadDIB(aBmp, 0, rIStm, true) && !rIStm.GetError()); 1477 1478 if(bRetval) 1479 { 1480 // base bitmap was read, set as return value and try to read alpha extra-data 1481 const sal_uLong nStmPos(rIStm.Tell()); 1482 sal_uInt32 nMagic1(0); 1483 sal_uInt32 nMagic2(0); 1484 1485 rTarget = BitmapEx(aBmp); 1486 rIStm >> nMagic1 >> nMagic2; 1487 bRetval = (0x25091962 == nMagic1) && (0xACB20201 == nMagic2) && !rIStm.GetError(); 1488 1489 if(bRetval) 1490 { 1491 sal_uInt8 bTransparent(false); 1492 1493 rIStm >> bTransparent; 1494 bRetval = !rIStm.GetError(); 1495 1496 if(bRetval) 1497 { 1498 if((sal_uInt8)TRANSPARENT_BITMAP == bTransparent) 1499 { 1500 Bitmap aMask; 1501 1502 bRetval = ImplReadDIB(aMask, 0, rIStm, true); 1503 1504 if(bRetval) 1505 { 1506 if(!!aMask) 1507 { 1508 // do we have an alpha mask? 1509 if((8 == aMask.GetBitCount()) && aMask.HasGreyPalette()) 1510 { 1511 AlphaMask aAlpha; 1512 1513 // create alpha mask quickly (without greyscale conversion) 1514 aAlpha.ImplSetBitmap(aMask); 1515 rTarget = BitmapEx(aBmp, aAlpha); 1516 } 1517 else 1518 { 1519 rTarget = BitmapEx(aBmp, aMask); 1520 } 1521 } 1522 } 1523 } 1524 else if((sal_uInt8)TRANSPARENT_COLOR == bTransparent) 1525 { 1526 Color aTransparentColor; 1527 1528 rIStm >> aTransparentColor; 1529 bRetval = !rIStm.GetError(); 1530 1531 if(bRetval) 1532 { 1533 rTarget = BitmapEx(aBmp, aTransparentColor); 1534 } 1535 } 1536 } 1537 } 1538 1539 if(!bRetval) 1540 { 1541 // alpha extra data could not be read; reset, but use base bitmap as result 1542 rIStm.ResetError(); 1543 rIStm.Seek(nStmPos); 1544 bRetval = true; 1545 } 1546 } 1547 1548 return bRetval; 1549 } 1550 1551 bool ReadDIBV5( 1552 Bitmap& rTarget, 1553 Bitmap& rTargetAlpha, 1554 SvStream& rIStm) 1555 { 1556 return ImplReadDIB(rTarget, &rTargetAlpha, rIStm, true); 1557 } 1558 1559 ////////////////////////////////////////////////////////////////////////////// 1560 1561 bool WriteDIB( 1562 const Bitmap& rSource, 1563 SvStream& rOStm, 1564 bool bCompressed, 1565 bool bFileHeader) 1566 { 1567 return ImplWriteDIB(rSource, 0, rOStm, bCompressed, bFileHeader); 1568 } 1569 1570 bool WriteDIBBitmapEx( 1571 const BitmapEx& rSource, 1572 SvStream& rOStm) 1573 { 1574 if(ImplWriteDIB(rSource.GetBitmap(), 0, rOStm, true, true)) 1575 { 1576 rOStm << (sal_uInt32)0x25091962; 1577 rOStm << (sal_uInt32)0xACB20201; 1578 rOStm << (sal_uInt8)rSource.eTransparent; 1579 1580 if(TRANSPARENT_BITMAP == rSource.eTransparent) 1581 { 1582 return ImplWriteDIB(rSource.aMask, 0, rOStm, true, true); 1583 } 1584 else if(TRANSPARENT_COLOR == rSource.eTransparent) 1585 { 1586 rOStm << rSource.aTransparentColor; 1587 return true; 1588 } 1589 } 1590 1591 return false; 1592 } 1593 1594 bool WriteDIBV5( 1595 const Bitmap& rSource, 1596 const Bitmap& rSourceAlpha, 1597 SvStream& rOStm) 1598 { 1599 return ImplWriteDIB(rSource, &rSourceAlpha, rOStm, false, true); 1600 } 1601 1602 ////////////////////////////////////////////////////////////////////////////// 1603 // eof 1604