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_uLong nAlignedWidth = AlignedWidth4Bytes(rHeader.nWidth * rHeader.nBitCount); 394 sal_uInt32 nRMask(( rHeader.nBitCount == 16 ) ? 0x00007c00UL : 0x00ff0000UL); 395 sal_uInt32 nGMask(( rHeader.nBitCount == 16 ) ? 0x000003e0UL : 0x0000ff00UL); 396 sal_uInt32 nBMask(( rHeader.nBitCount == 16 ) ? 0x0000001fUL : 0x000000ffUL); 397 bool bNative(false); 398 bool bTCMask(!pAccAlpha && ((16 == rHeader.nBitCount) || (32 == rHeader.nBitCount))); 399 bool bRLE((RLE_8 == rHeader.nCompression && 8 == rHeader.nBitCount) || (RLE_4 == rHeader.nCompression && 4 == rHeader.nBitCount)); 400 401 // Is native format? 402 switch(rAcc.GetScanlineFormat()) 403 { 404 case( BMP_FORMAT_1BIT_MSB_PAL ): 405 case( BMP_FORMAT_4BIT_MSN_PAL ): 406 case( BMP_FORMAT_8BIT_PAL ): 407 case( BMP_FORMAT_24BIT_TC_BGR ): 408 { 409 bNative = ( ( static_cast< bool >(rAcc.IsBottomUp()) != bTopDown ) && !bRLE && !bTCMask && ( rAcc.GetScanlineSize() == nAlignedWidth ) ); 410 break; 411 } 412 413 default: 414 { 415 break; 416 } 417 } 418 419 // Read data 420 if(bNative) 421 { 422 rIStm.Read(rAcc.GetBuffer(), rHeader.nHeight * nAlignedWidth); 423 } 424 else 425 { 426 // Read color mask 427 if(bTCMask && BITFIELDS == rHeader.nCompression) 428 { 429 rIStm.SeekRel( -12L ); 430 rIStm >> nRMask; 431 rIStm >> nGMask; 432 rIStm >> nBMask; 433 } 434 435 if(bRLE) 436 { 437 if(!rHeader.nSizeImage) 438 { 439 const sal_uLong nOldPos(rIStm.Tell()); 440 441 rIStm.Seek(STREAM_SEEK_TO_END); 442 rHeader.nSizeImage = rIStm.Tell() - nOldPos; 443 rIStm.Seek(nOldPos); 444 } 445 446 sal_uInt8* pBuffer = (sal_uInt8*)rtl_allocateMemory(rHeader.nSizeImage); 447 rIStm.Read((char*)pBuffer, rHeader.nSizeImage); 448 ImplDecodeRLE(pBuffer, rHeader, rAcc, RLE_4 == rHeader.nCompression); 449 rtl_freeMemory(pBuffer); 450 } 451 else 452 { 453 const long nWidth(rHeader.nWidth); 454 const long nHeight(rHeader.nHeight); 455 sal_uInt8* pBuf = new sal_uInt8[nAlignedWidth]; 456 457 const long nI(bTopDown ? 1 : -1); 458 long nY(bTopDown ? 0 : nHeight - 1); 459 long nCount(nHeight); 460 461 switch(rHeader.nBitCount) 462 { 463 case( 1 ): 464 { 465 sal_uInt8* pTmp; 466 sal_uInt8 cTmp; 467 468 for( ; nCount--; nY += nI ) 469 { 470 rIStm.Read( pTmp = pBuf, nAlignedWidth ); 471 cTmp = *pTmp++; 472 473 for( long nX = 0L, nShift = 8L; nX < nWidth; nX++ ) 474 { 475 if( !nShift ) 476 { 477 nShift = 8L, 478 cTmp = *pTmp++; 479 } 480 481 rAcc.SetPixelIndex( nY, nX, (cTmp >> --nShift) & 1); 482 } 483 } 484 } 485 break; 486 487 case( 4 ): 488 { 489 sal_uInt8* pTmp; 490 sal_uInt8 cTmp; 491 492 for( ; nCount--; nY += nI ) 493 { 494 rIStm.Read( pTmp = pBuf, nAlignedWidth ); 495 cTmp = *pTmp++; 496 497 for( long nX = 0L, nShift = 2L; nX < nWidth; nX++ ) 498 { 499 if( !nShift ) 500 { 501 nShift = 2UL, 502 cTmp = *pTmp++; 503 } 504 505 rAcc.SetPixelIndex( nY, nX, (cTmp >> ( --nShift << 2UL ) ) & 0x0f); 506 } 507 } 508 } 509 break; 510 511 case( 8 ): 512 { 513 sal_uInt8* pTmp; 514 515 for( ; nCount--; nY += nI ) 516 { 517 rIStm.Read( pTmp = pBuf, nAlignedWidth ); 518 519 for( long nX = 0L; nX < nWidth; nX++ ) 520 rAcc.SetPixelIndex( nY, nX, *pTmp++ ); 521 } 522 } 523 break; 524 525 case( 16 ): 526 { 527 ColorMask aMask( nRMask, nGMask, nBMask ); 528 BitmapColor aColor; 529 sal_uInt16* pTmp16; 530 531 for( ; nCount--; nY += nI ) 532 { 533 rIStm.Read( (char*)( pTmp16 = (sal_uInt16*) pBuf ), nAlignedWidth ); 534 535 for( long nX = 0L; nX < nWidth; nX++ ) 536 { 537 aMask.GetColorFor16BitLSB( aColor, (sal_uInt8*) pTmp16++ ); 538 rAcc.SetPixel( nY, nX, aColor ); 539 } 540 } 541 } 542 break; 543 544 case( 24 ): 545 { 546 BitmapColor aPixelColor; 547 sal_uInt8* pTmp; 548 549 for( ; nCount--; nY += nI ) 550 { 551 rIStm.Read( pTmp = pBuf, nAlignedWidth ); 552 553 for( long nX = 0L; nX < nWidth; nX++ ) 554 { 555 aPixelColor.SetBlue( *pTmp++ ); 556 aPixelColor.SetGreen( *pTmp++ ); 557 aPixelColor.SetRed( *pTmp++ ); 558 rAcc.SetPixel( nY, nX, aPixelColor ); 559 } 560 } 561 } 562 break; 563 564 case( 32 ): 565 { 566 ColorMask aMask(nRMask, nGMask, nBMask); 567 BitmapColor aColor; 568 sal_uInt32* pTmp32; 569 570 if(pAccAlpha) 571 { 572 sal_uInt8 aAlpha; 573 574 for( ; nCount--; nY += nI ) 575 { 576 rIStm.Read( (char*)( pTmp32 = (sal_uInt32*) pBuf ), nAlignedWidth ); 577 578 for( long nX = 0L; nX < nWidth; nX++ ) 579 { 580 aMask.GetColorAndAlphaFor32Bit( aColor, aAlpha, (sal_uInt8*) pTmp32++ ); 581 rAcc.SetPixel( nY, nX, aColor ); 582 pAccAlpha->SetPixelIndex(nY, nX, sal_uInt8(0xff) - aAlpha); 583 rAlphaUsed |= bool(0xff != aAlpha); 584 } 585 } 586 } 587 else 588 { 589 for( ; nCount--; nY += nI ) 590 { 591 rIStm.Read( (char*)( pTmp32 = (sal_uInt32*) pBuf ), nAlignedWidth ); 592 593 for( long nX = 0L; nX < nWidth; nX++ ) 594 { 595 aMask.GetColorFor32Bit( aColor, (sal_uInt8*) pTmp32++ ); 596 rAcc.SetPixel( nY, nX, aColor ); 597 } 598 } 599 } 600 } 601 } 602 603 delete[] pBuf; 604 } 605 } 606 607 return( rIStm.GetError() == 0UL ); 608 } 609 610 bool ImplReadDIBBody( SvStream& rIStm, Bitmap& rBmp, Bitmap* pBmpAlpha, sal_uLong nOffset ) 611 { 612 DIBV5Header aHeader; 613 const sal_uLong nStmPos = rIStm.Tell(); 614 bool bRet(false); 615 bool bTopDown(false); 616 617 if(ImplReadDIBInfoHeader(rIStm, aHeader, bTopDown) && aHeader.nWidth && aHeader.nHeight && aHeader.nBitCount) 618 { 619 const sal_uInt16 nBitCount(discretizeBitcount(aHeader.nBitCount)); 620 const Size aSizePixel(aHeader.nWidth, aHeader.nHeight); 621 BitmapPalette aDummyPal; 622 Bitmap aNewBmp(aSizePixel, nBitCount, &aDummyPal); 623 Bitmap aNewBmpAlpha; 624 BitmapWriteAccess* pAcc = aNewBmp.AcquireWriteAccess(); 625 BitmapWriteAccess* pAccAlpha = 0; 626 bool bAlphaPossible(pBmpAlpha && aHeader.nBitCount == 32); 627 628 if(bAlphaPossible) 629 { 630 const bool bRedSet(0 != aHeader.nV5RedMask); 631 const bool bGreenSet(0 != aHeader.nV5GreenMask); 632 const bool bBlueSet(0 != aHeader.nV5BlueMask); 633 634 // some clipboard entries have alpha mask on zero to say that there is 635 // no alpha; do only use this when the other masks are set. The MS docu 636 // says that that masks are only to be set when bV5Compression is set to 637 // BI_BITFIELDS, but there seem to exist a wild variety of usages... 638 if((bRedSet || bGreenSet || bBlueSet) && (0 == aHeader.nV5AlphaMask)) 639 { 640 bAlphaPossible = false; 641 } 642 } 643 644 if(bAlphaPossible) 645 { 646 aNewBmpAlpha = Bitmap(aSizePixel, 8); 647 pAccAlpha = aNewBmpAlpha.AcquireWriteAccess(); 648 } 649 650 if(pAcc) 651 { 652 sal_uInt16 nColors(0); 653 SvStream* pIStm; 654 SvMemoryStream* pMemStm = NULL; 655 sal_uInt8* pData = NULL; 656 657 if(nBitCount <= 8) 658 { 659 if(aHeader.nColsUsed) 660 { 661 nColors = (sal_uInt16)aHeader.nColsUsed; 662 } 663 else 664 { 665 nColors = ( 1 << aHeader.nBitCount ); 666 } 667 } 668 669 if(ZCOMPRESS == aHeader.nCompression) 670 { 671 ZCodec aCodec; 672 sal_uInt32 nCodedSize(0); 673 sal_uInt32 nUncodedSize(0); 674 sal_uLong nCodedPos(0); 675 676 // read coding information 677 rIStm >> nCodedSize >> nUncodedSize >> aHeader.nCompression; 678 pData = (sal_uInt8*) rtl_allocateMemory( nUncodedSize ); 679 680 // decode buffer 681 nCodedPos = rIStm.Tell(); 682 aCodec.BeginCompression(); 683 aCodec.Read( rIStm, pData, nUncodedSize ); 684 aCodec.EndCompression(); 685 686 // skip unread bytes from coded buffer 687 rIStm.SeekRel( nCodedSize - ( rIStm.Tell() - nCodedPos ) ); 688 689 // set decoded bytes to memory stream, 690 // from which we will read the bitmap data 691 pIStm = pMemStm = new SvMemoryStream; 692 pMemStm->SetBuffer( (char*) pData, nUncodedSize, false, nUncodedSize ); 693 nOffset = 0; 694 } 695 else 696 { 697 pIStm = &rIStm; 698 } 699 700 // read palette 701 if(nColors) 702 { 703 pAcc->SetPaletteEntryCount(nColors); 704 ImplReadDIBPalette(*pIStm, *pAcc, aHeader.nSize != DIBCOREHEADERSIZE); 705 } 706 707 // read bits 708 bool bAlphaUsed(false); 709 710 if(!pIStm->GetError()) 711 { 712 if(nOffset) 713 { 714 pIStm->SeekRel(nOffset - (pIStm->Tell() - nStmPos)); 715 } 716 717 bRet = ImplReadDIBBits(*pIStm, aHeader, *pAcc, pAccAlpha, bTopDown, bAlphaUsed); 718 719 if(bRet && aHeader.nXPelsPerMeter && aHeader.nYPelsPerMeter) 720 { 721 MapMode aMapMode( 722 MAP_MM, 723 Point(), 724 Fraction(1000, aHeader.nXPelsPerMeter), 725 Fraction(1000, aHeader.nYPelsPerMeter)); 726 727 aNewBmp.SetPrefMapMode(aMapMode); 728 aNewBmp.SetPrefSize(Size(aHeader.nWidth, aHeader.nHeight)); 729 } 730 } 731 732 if( pData ) 733 { 734 rtl_freeMemory(pData); 735 } 736 737 delete pMemStm; 738 aNewBmp.ReleaseAccess(pAcc); 739 740 if(bAlphaPossible) 741 { 742 aNewBmpAlpha.ReleaseAccess(pAccAlpha); 743 744 if(!bAlphaUsed) 745 { 746 bAlphaPossible = false; 747 } 748 } 749 750 if(bRet) 751 { 752 rBmp = aNewBmp; 753 754 if(bAlphaPossible) 755 { 756 *pBmpAlpha = aNewBmpAlpha; 757 } 758 } 759 } 760 } 761 762 return bRet; 763 } 764 765 bool ImplReadDIBFileHeader( SvStream& rIStm, sal_uLong& rOffset ) 766 { 767 sal_uInt32 nTmp32; 768 sal_uInt16 nTmp16 = 0; 769 bool bRet = false; 770 771 rIStm >> nTmp16; 772 773 if ( ( 0x4D42 == nTmp16 ) || ( 0x4142 == nTmp16 ) ) 774 { 775 if ( 0x4142 == nTmp16 ) 776 { 777 rIStm.SeekRel( 12L ); 778 rIStm >> nTmp16; 779 rIStm.SeekRel( 8L ); 780 rIStm >> nTmp32; 781 rOffset = nTmp32 - 28UL; 782 bRet = ( 0x4D42 == nTmp16 ); 783 } 784 else // 0x4D42 == nTmp16, 'MB' from BITMAPFILEHEADER 785 { 786 rIStm.SeekRel( 8L ); // we are on bfSize member of BITMAPFILEHEADER, forward to bfOffBits 787 rIStm >> nTmp32; // read bfOffBits 788 rOffset = nTmp32 - 14UL; // adapt offset by sizeof(BITMAPFILEHEADER) 789 bRet = ( rIStm.GetError() == 0UL ); 790 } 791 } 792 else 793 rIStm.SetError( SVSTREAM_FILEFORMAT_ERROR ); 794 795 return bRet; 796 } 797 798 bool ImplWriteDIBPalette( SvStream& rOStm, BitmapReadAccess& rAcc ) 799 { 800 const sal_uInt16 nColors = rAcc.GetPaletteEntryCount(); 801 const sal_uLong nPalSize = nColors * 4UL; 802 sal_uInt8* pEntries = new sal_uInt8[ nPalSize ]; 803 sal_uInt8* pTmpEntry = pEntries; 804 BitmapColor aPalColor; 805 806 for( sal_uInt16 i = 0; i < nColors; i++ ) 807 { 808 const BitmapColor& rPalColor = rAcc.GetPaletteColor( i ); 809 810 *pTmpEntry++ = rPalColor.GetBlue(); 811 *pTmpEntry++ = rPalColor.GetGreen(); 812 *pTmpEntry++ = rPalColor.GetRed(); 813 *pTmpEntry++ = 0; 814 } 815 816 rOStm.Write( pEntries, nPalSize ); 817 delete[] pEntries; 818 819 return( rOStm.GetError() == 0UL ); 820 } 821 822 bool ImplWriteRLE( SvStream& rOStm, BitmapReadAccess& rAcc, bool bRLE4 ) 823 { 824 const sal_uLong nWidth = rAcc.Width(); 825 const sal_uLong nHeight = rAcc.Height(); 826 sal_uLong nX; 827 sal_uLong nSaveIndex; 828 sal_uLong nCount; 829 sal_uLong nBufCount; 830 sal_uInt8* pBuf = new sal_uInt8[ ( nWidth << 1 ) + 2 ]; 831 sal_uInt8* pTmp; 832 sal_uInt8 cPix; 833 sal_uInt8 cLast; 834 bool bFound; 835 836 for ( long nY = nHeight - 1L; nY >= 0L; nY-- ) 837 { 838 pTmp = pBuf; 839 nX = nBufCount = 0UL; 840 841 while( nX < nWidth ) 842 { 843 nCount = 1L; 844 cPix = rAcc.GetPixelIndex( nY, nX++ ); 845 846 while( ( nX < nWidth ) && ( nCount < 255L ) 847 && ( cPix == rAcc.GetPixelIndex( nY, nX ) ) ) 848 { 849 nX++; 850 nCount++; 851 } 852 853 if ( nCount > 1 ) 854 { 855 *pTmp++ = (sal_uInt8) nCount; 856 *pTmp++ = ( bRLE4 ? ( ( cPix << 4 ) | cPix ) : cPix ); 857 nBufCount += 2; 858 } 859 else 860 { 861 cLast = cPix; 862 nSaveIndex = nX - 1UL; 863 bFound = false; 864 865 while( ( nX < nWidth ) && ( nCount < 256L ) 866 && ( cPix = rAcc.GetPixelIndex( nY, nX ) ) != cLast ) 867 { 868 nX++; nCount++; 869 cLast = cPix; 870 bFound = true; 871 } 872 873 if ( bFound ) 874 nX--; 875 876 if ( nCount > 3 ) 877 { 878 *pTmp++ = 0; 879 *pTmp++ = (sal_uInt8) --nCount; 880 881 if( bRLE4 ) 882 { 883 for ( sal_uLong i = 0; i < nCount; i++, pTmp++ ) 884 { 885 *pTmp = rAcc.GetPixelIndex( nY, nSaveIndex++ ) << 4; 886 887 if ( ++i < nCount ) 888 *pTmp |= rAcc.GetPixelIndex( nY, nSaveIndex++ ); 889 } 890 891 nCount = ( nCount + 1 ) >> 1; 892 } 893 else 894 { 895 for( sal_uLong i = 0UL; i < nCount; i++ ) 896 *pTmp++ = rAcc.GetPixelIndex( nY, nSaveIndex++ ); 897 } 898 899 if ( nCount & 1 ) 900 { 901 *pTmp++ = 0; 902 nBufCount += ( nCount + 3 ); 903 } 904 else 905 nBufCount += ( nCount + 2 ); 906 } 907 else 908 { 909 *pTmp++ = 1; 910 *pTmp++ = rAcc.GetPixelIndex( nY, nSaveIndex ) << (bRLE4 ? 4 : 0); 911 912 if ( nCount == 3 ) 913 { 914 *pTmp++ = 1; 915 *pTmp++ = rAcc.GetPixelIndex( nY, ++nSaveIndex ) << ( bRLE4 ? 4 : 0 ); 916 nBufCount += 4; 917 } 918 else 919 nBufCount += 2; 920 } 921 } 922 } 923 924 pBuf[ nBufCount++ ] = 0; 925 pBuf[ nBufCount++ ] = 0; 926 927 rOStm.Write( pBuf, nBufCount ); 928 } 929 930 rOStm << (sal_uInt8) 0; 931 rOStm << (sal_uInt8) 1; 932 933 delete[] pBuf; 934 935 return( rOStm.GetError() == 0UL ); 936 } 937 938 bool ImplWriteDIBBits(SvStream& rOStm, BitmapReadAccess& rAcc, BitmapReadAccess* pAccAlpha, sal_uLong nCompression, sal_uInt32& rImageSize) 939 { 940 if(!pAccAlpha && BITFIELDS == nCompression) 941 { 942 const ColorMask& rMask = rAcc.GetColorMask(); 943 SVBT32 aVal32; 944 945 UInt32ToSVBT32( rMask.GetRedMask(), aVal32 ); 946 rOStm.Write( (sal_uInt8*) aVal32, 4UL ); 947 948 UInt32ToSVBT32( rMask.GetGreenMask(), aVal32 ); 949 rOStm.Write( (sal_uInt8*) aVal32, 4UL ); 950 951 UInt32ToSVBT32( rMask.GetBlueMask(), aVal32 ); 952 rOStm.Write( (sal_uInt8*) aVal32, 4UL ); 953 954 rImageSize = rOStm.Tell(); 955 956 if( rAcc.IsBottomUp() ) 957 rOStm.Write( rAcc.GetBuffer(), rAcc.Height() * rAcc.GetScanlineSize() ); 958 else 959 { 960 for( long nY = rAcc.Height() - 1, nScanlineSize = rAcc.GetScanlineSize(); nY >= 0L; nY-- ) 961 rOStm.Write( rAcc.GetScanline( nY ), nScanlineSize ); 962 } 963 } 964 else if(!pAccAlpha && ((RLE_4 == nCompression) || (RLE_8 == nCompression))) 965 { 966 rImageSize = rOStm.Tell(); 967 ImplWriteRLE( rOStm, rAcc, RLE_4 == nCompression ); 968 } 969 else if(!nCompression) 970 { 971 // #i5xxx# Limit bitcount to 24bit, the 32 bit cases are not 972 // handled properly below (would have to set color masks, and 973 // nCompression=BITFIELDS - but color mask is not set for 974 // formats != *_TC_*). Note that this very problem might cause 975 // trouble at other places - the introduction of 32 bit RGBA 976 // bitmaps is relatively recent. 977 // #i59239# discretize bitcount for aligned width to 1,4,8,24 978 // (other cases are not written below) 979 const sal_uInt16 nBitCount(pAccAlpha ? 32 : discretizeBitcount(static_cast< sal_uInt16 >(rAcc.GetBitCount()))); 980 const sal_uLong nAlignedWidth(AlignedWidth4Bytes(rAcc.Width() * nBitCount)); 981 bool bNative(false); 982 983 switch(rAcc.GetScanlineFormat()) 984 { 985 case( BMP_FORMAT_1BIT_MSB_PAL ): 986 case( BMP_FORMAT_4BIT_MSN_PAL ): 987 case( BMP_FORMAT_8BIT_PAL ): 988 case( BMP_FORMAT_24BIT_TC_BGR ): 989 { 990 if(!pAccAlpha && rAcc.IsBottomUp() && (rAcc.GetScanlineSize() == nAlignedWidth)) 991 { 992 bNative = true; 993 } 994 995 break; 996 } 997 998 default: 999 { 1000 break; 1001 } 1002 } 1003 1004 rImageSize = rOStm.Tell(); 1005 1006 if(bNative) 1007 { 1008 rOStm.Write(rAcc.GetBuffer(), nAlignedWidth * rAcc.Height()); 1009 } 1010 else 1011 { 1012 const long nWidth(rAcc.Width()); 1013 const long nHeight(rAcc.Height()); 1014 sal_uInt8* pBuf = new sal_uInt8[ nAlignedWidth ]; 1015 sal_uInt8* pTmp(0); 1016 sal_uInt8 cTmp(0); 1017 1018 switch( nBitCount ) 1019 { 1020 case( 1 ): 1021 { 1022 for( long nY = nHeight - 1; nY >= 0L; nY-- ) 1023 { 1024 pTmp = pBuf; 1025 cTmp = 0; 1026 1027 for( long nX = 0L, nShift = 8L; nX < nWidth; nX++ ) 1028 { 1029 if( !nShift ) 1030 { 1031 nShift = 8L; 1032 *pTmp++ = cTmp; 1033 cTmp = 0; 1034 } 1035 1036 cTmp |= rAcc.GetPixelIndex( nY, nX ) << --nShift; 1037 } 1038 1039 *pTmp = cTmp; 1040 rOStm.Write( pBuf, nAlignedWidth ); 1041 } 1042 } 1043 break; 1044 1045 case( 4 ): 1046 { 1047 for( long nY = nHeight - 1; nY >= 0L; nY-- ) 1048 { 1049 pTmp = pBuf; 1050 cTmp = 0; 1051 1052 for( long nX = 0L, nShift = 2L; nX < nWidth; nX++ ) 1053 { 1054 if( !nShift ) 1055 { 1056 nShift = 2L; 1057 *pTmp++ = cTmp; 1058 cTmp = 0; 1059 } 1060 1061 cTmp |= rAcc.GetPixelIndex( nY, nX ) << ( --nShift << 2L ); 1062 } 1063 *pTmp = cTmp; 1064 rOStm.Write( pBuf, nAlignedWidth ); 1065 } 1066 } 1067 break; 1068 1069 case( 8 ): 1070 { 1071 for( long nY = nHeight - 1; nY >= 0L; nY-- ) 1072 { 1073 pTmp = pBuf; 1074 1075 for( long nX = 0L; nX < nWidth; nX++ ) 1076 *pTmp++ = rAcc.GetPixelIndex( nY, nX ); 1077 1078 rOStm.Write( pBuf, nAlignedWidth ); 1079 } 1080 } 1081 break; 1082 1083 // #i59239# fallback to 24 bit format, if bitcount is non-default 1084 default: 1085 // FALLTHROUGH intended 1086 case( 24 ): 1087 { 1088 BitmapColor aPixelColor; 1089 const bool bWriteAlpha(32 == nBitCount && pAccAlpha); 1090 1091 for( long nY = nHeight - 1; nY >= 0L; nY-- ) 1092 { 1093 pTmp = pBuf; 1094 1095 for( long nX = 0L; nX < nWidth; nX++ ) 1096 { 1097 // when alpha is used, this may be non-24bit main bitmap, so use GetColor 1098 // instead of GetPixel to ensure RGB value 1099 aPixelColor = rAcc.GetColor( nY, nX ); 1100 1101 *pTmp++ = aPixelColor.GetBlue(); 1102 *pTmp++ = aPixelColor.GetGreen(); 1103 *pTmp++ = aPixelColor.GetRed(); 1104 1105 if(bWriteAlpha) 1106 { 1107 if(pAccAlpha) 1108 { 1109 *pTmp++ = (sal_uInt8)0xff - (sal_uInt8)pAccAlpha->GetPixelIndex( nY, nX ); 1110 } 1111 else 1112 { 1113 *pTmp++ = (sal_uInt8)0xff; 1114 } 1115 } 1116 } 1117 1118 rOStm.Write( pBuf, nAlignedWidth ); 1119 } 1120 } 1121 break; 1122 } 1123 1124 delete[] pBuf; 1125 } 1126 } 1127 1128 rImageSize = rOStm.Tell() - rImageSize; 1129 1130 return (!rOStm.GetError()); 1131 } 1132 1133 bool ImplWriteDIBBody(const Bitmap& rBitmap, SvStream& rOStm, BitmapReadAccess& rAcc, BitmapReadAccess* pAccAlpha, bool bCompressed) 1134 { 1135 const MapMode aMapPixel(MAP_PIXEL); 1136 DIBV5Header aHeader; 1137 sal_uLong nImageSizePos(0); 1138 sal_uLong nEndPos(0); 1139 sal_uInt32 nCompression(COMPRESS_NONE); 1140 bool bRet(false); 1141 1142 aHeader.nSize = pAccAlpha ? DIBV5HEADERSIZE : DIBINFOHEADERSIZE; // size dependent on CF_DIB type to use 1143 aHeader.nWidth = rAcc.Width(); 1144 aHeader.nHeight = rAcc.Height(); 1145 aHeader.nPlanes = 1; 1146 1147 if(!pAccAlpha && isBitfieldCompression(rAcc.GetScanlineFormat())) 1148 { 1149 aHeader.nBitCount = (BMP_FORMAT_16BIT_TC_LSB_MASK == rAcc.GetScanlineFormat()) ? 16 : 32; 1150 aHeader.nSizeImage = rAcc.Height() * rAcc.GetScanlineSize(); 1151 nCompression = BITFIELDS; 1152 } 1153 else 1154 { 1155 // #i5xxx# Limit bitcount to 24bit, the 32 bit cases are 1156 // not handled properly below (would have to set color 1157 // masks, and nCompression=BITFIELDS - but color mask is 1158 // not set for formats != *_TC_*). Note that this very 1159 // problem might cause trouble at other places - the 1160 // introduction of 32 bit RGBA bitmaps is relatively 1161 // recent. 1162 // #i59239# discretize bitcount to 1,4,8,24 (other cases 1163 // are not written below) 1164 const sal_uInt16 nBitCount(pAccAlpha ? 32 : discretizeBitcount(static_cast< sal_uInt16 >(rAcc.GetBitCount()))); 1165 aHeader.nBitCount = nBitCount; 1166 aHeader.nSizeImage = rAcc.Height() * AlignedWidth4Bytes(rAcc.Width() * aHeader.nBitCount); 1167 1168 if(bCompressed) 1169 { 1170 if(4 == nBitCount) 1171 { 1172 nCompression = RLE_4; 1173 } 1174 else if(8 == nBitCount) 1175 { 1176 nCompression = RLE_8; 1177 } 1178 } 1179 } 1180 1181 if((rOStm.GetCompressMode() & COMPRESSMODE_ZBITMAP) && (rOStm.GetVersion() >= SOFFICE_FILEFORMAT_40)) 1182 { 1183 aHeader.nCompression = ZCOMPRESS; 1184 } 1185 else 1186 { 1187 aHeader.nCompression = nCompression; 1188 } 1189 1190 if(rBitmap.GetPrefSize().Width() && rBitmap.GetPrefSize().Height() && (rBitmap.GetPrefMapMode() != aMapPixel)) 1191 { 1192 // #i48108# Try to recover xpels/ypels as previously stored on 1193 // disk. The problem with just converting maPrefSize to 100th 1194 // mm and then relating that to the bitmap pixel size is that 1195 // MapMode is integer-based, and suffers from roundoffs, 1196 // especially if maPrefSize is small. Trying to circumvent 1197 // that by performing part of the math in floating point. 1198 const Size aScale100000(OutputDevice::LogicToLogic(Size(100000L, 100000L), MAP_100TH_MM, rBitmap.GetPrefMapMode())); 1199 const double fBmpWidthM((double)rBitmap.GetPrefSize().Width() / aScale100000.Width()); 1200 const double fBmpHeightM((double)rBitmap.GetPrefSize().Height() / aScale100000.Height()); 1201 1202 if(!basegfx::fTools::equalZero(fBmpWidthM) && !basegfx::fTools::equalZero(fBmpHeightM)) 1203 { 1204 aHeader.nXPelsPerMeter = basegfx::fround(rAcc.Width() / fabs(fBmpWidthM)); 1205 aHeader.nYPelsPerMeter = basegfx::fround(rAcc.Height() / fabs(fBmpHeightM)); 1206 } 1207 } 1208 1209 aHeader.nColsUsed = ((!pAccAlpha && aHeader.nBitCount <= 8) ? rAcc.GetPaletteEntryCount() : 0); 1210 aHeader.nColsImportant = 0; 1211 1212 rOStm << aHeader.nSize; 1213 rOStm << aHeader.nWidth; 1214 rOStm << aHeader.nHeight; 1215 rOStm << aHeader.nPlanes; 1216 rOStm << aHeader.nBitCount; 1217 rOStm << aHeader.nCompression; 1218 1219 nImageSizePos = rOStm.Tell(); 1220 rOStm.SeekRel( sizeof( aHeader.nSizeImage ) ); 1221 1222 rOStm << aHeader.nXPelsPerMeter; 1223 rOStm << aHeader.nYPelsPerMeter; 1224 rOStm << aHeader.nColsUsed; 1225 rOStm << aHeader.nColsImportant; 1226 1227 if(pAccAlpha) // only write DIBV5 when asked to do so 1228 { 1229 aHeader.nV5CSType = 0x57696E20; // LCS_WINDOWS_COLOR_SPACE 1230 aHeader.nV5Intent = 0x00000004; // LCS_GM_IMAGES 1231 1232 rOStm << aHeader.nV5RedMask; 1233 rOStm << aHeader.nV5GreenMask; 1234 rOStm << aHeader.nV5BlueMask; 1235 rOStm << aHeader.nV5AlphaMask; 1236 rOStm << aHeader.nV5CSType; 1237 1238 rOStm << aHeader.aV5Endpoints.aXyzRed.aXyzX; 1239 rOStm << aHeader.aV5Endpoints.aXyzRed.aXyzY; 1240 rOStm << aHeader.aV5Endpoints.aXyzRed.aXyzZ; 1241 rOStm << aHeader.aV5Endpoints.aXyzGreen.aXyzX; 1242 rOStm << aHeader.aV5Endpoints.aXyzGreen.aXyzY; 1243 rOStm << aHeader.aV5Endpoints.aXyzGreen.aXyzZ; 1244 rOStm << aHeader.aV5Endpoints.aXyzBlue.aXyzX; 1245 rOStm << aHeader.aV5Endpoints.aXyzBlue.aXyzY; 1246 rOStm << aHeader.aV5Endpoints.aXyzBlue.aXyzZ; 1247 1248 rOStm << aHeader.nV5GammaRed; 1249 rOStm << aHeader.nV5GammaGreen; 1250 rOStm << aHeader.nV5GammaBlue; 1251 rOStm << aHeader.nV5Intent; 1252 rOStm << aHeader.nV5ProfileData; 1253 rOStm << aHeader.nV5ProfileSize; 1254 rOStm << aHeader.nV5Reserved; 1255 } 1256 1257 if(ZCOMPRESS == aHeader.nCompression) 1258 { 1259 ZCodec aCodec; 1260 SvMemoryStream aMemStm(aHeader.nSizeImage + 4096, 65535); 1261 sal_uLong nCodedPos(rOStm.Tell()); 1262 sal_uLong nLastPos(0); 1263 sal_uInt32 nCodedSize(0); 1264 sal_uInt32 nUncodedSize(0); 1265 1266 // write uncoded data palette 1267 if(aHeader.nColsUsed) 1268 { 1269 ImplWriteDIBPalette(aMemStm, rAcc); 1270 } 1271 1272 // write uncoded bits 1273 bRet = ImplWriteDIBBits(aMemStm, rAcc, pAccAlpha, nCompression, aHeader.nSizeImage); 1274 1275 // get uncoded size 1276 nUncodedSize = aMemStm.Tell(); 1277 1278 // seek over compress info 1279 rOStm.SeekRel(12); 1280 1281 // write compressed data 1282 aCodec.BeginCompression(3); 1283 aCodec.Write(rOStm, (sal_uInt8*)aMemStm.GetData(), nUncodedSize); 1284 aCodec.EndCompression(); 1285 1286 // update compress info ( coded size, uncoded size, uncoded compression ) 1287 nLastPos = rOStm.Tell(); 1288 nCodedSize = nLastPos - nCodedPos - 12; 1289 rOStm.Seek(nCodedPos); 1290 rOStm << nCodedSize << nUncodedSize << nCompression; 1291 rOStm.Seek(nLastPos); 1292 1293 if(bRet) 1294 { 1295 bRet = (ERRCODE_NONE == rOStm.GetError()); 1296 } 1297 } 1298 else 1299 { 1300 if(aHeader.nColsUsed) 1301 { 1302 ImplWriteDIBPalette(rOStm, rAcc); 1303 } 1304 1305 bRet = ImplWriteDIBBits(rOStm, rAcc, pAccAlpha, aHeader.nCompression, aHeader.nSizeImage); 1306 } 1307 1308 nEndPos = rOStm.Tell(); 1309 rOStm.Seek(nImageSizePos); 1310 rOStm << aHeader.nSizeImage; 1311 rOStm.Seek(nEndPos); 1312 1313 return bRet; 1314 } 1315 1316 bool ImplWriteDIBFileHeader(SvStream& rOStm, BitmapReadAccess& rAcc, bool bUseDIBV5) 1317 { 1318 const sal_uInt32 nPalCount((rAcc.HasPalette() ? rAcc.GetPaletteEntryCount() : isBitfieldCompression(rAcc.GetScanlineFormat()) ? 3UL : 0UL)); 1319 const sal_uInt32 nOffset(14 + (bUseDIBV5 ? DIBV5HEADERSIZE : DIBINFOHEADERSIZE) + nPalCount * 4UL); 1320 1321 rOStm << (sal_uInt16)0x4D42; // 'MB' from BITMAPFILEHEADER 1322 rOStm << (sal_uInt32)(nOffset + (rAcc.Height() * rAcc.GetScanlineSize())); 1323 rOStm << (sal_uInt16)0; 1324 rOStm << (sal_uInt16)0; 1325 rOStm << nOffset; 1326 1327 return( rOStm.GetError() == 0UL ); 1328 } 1329 1330 ////////////////////////////////////////////////////////////////////////////// 1331 1332 bool ImplReadDIB( 1333 Bitmap& rTarget, Bitmap* 1334 pTargetAlpha, 1335 SvStream& rIStm, 1336 bool bFileHeader) 1337 { 1338 const sal_uInt16 nOldFormat(rIStm.GetNumberFormatInt()); 1339 const sal_uLong nOldPos(rIStm.Tell()); 1340 sal_uLong nOffset(0UL); 1341 bool bRet(false); 1342 1343 rIStm.SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN); 1344 1345 if(bFileHeader) 1346 { 1347 if(ImplReadDIBFileHeader(rIStm, nOffset)) 1348 { 1349 bRet = ImplReadDIBBody(rIStm, rTarget, nOffset >= DIBV5HEADERSIZE ? pTargetAlpha : 0, nOffset); 1350 } 1351 } 1352 else 1353 { 1354 bRet = ImplReadDIBBody(rIStm, rTarget, 0, nOffset); 1355 } 1356 1357 if(!bRet) 1358 { 1359 if(!rIStm.GetError()) 1360 { 1361 rIStm.SetError(SVSTREAM_GENERALERROR); 1362 } 1363 1364 rIStm.Seek(nOldPos); 1365 } 1366 1367 rIStm.SetNumberFormatInt(nOldFormat); 1368 1369 return bRet; 1370 } 1371 1372 bool ImplWriteDIB( 1373 const Bitmap& rSource, 1374 const Bitmap* pSourceAlpha, 1375 SvStream& rOStm, 1376 bool bCompressed, 1377 bool bFileHeader) 1378 { 1379 const Size aSizePix(rSource.GetSizePixel()); 1380 bool bRet(false); 1381 1382 if(aSizePix.Width() && aSizePix.Height()) 1383 { 1384 BitmapReadAccess* pAcc = const_cast< Bitmap& >(rSource).AcquireReadAccess(); 1385 BitmapReadAccess* pAccAlpha = 0; 1386 const sal_uInt16 nOldFormat(rOStm.GetNumberFormatInt()); 1387 const sal_uLong nOldPos(rOStm.Tell()); 1388 1389 if(pSourceAlpha) 1390 { 1391 const Size aSizePixAlpha(pSourceAlpha->GetSizePixel()); 1392 1393 if(aSizePixAlpha == aSizePix) 1394 { 1395 pAccAlpha = const_cast< Bitmap* >(pSourceAlpha)->AcquireReadAccess(); 1396 } 1397 else 1398 { 1399 OSL_ENSURE(false, "WriteDIB got an alpha channel, but it's pixel size differs from the base bitmap (!)"); 1400 } 1401 } 1402 1403 rOStm.SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN); 1404 1405 if(pAcc) 1406 { 1407 if(bFileHeader) 1408 { 1409 if(ImplWriteDIBFileHeader(rOStm, *pAcc, 0 != pSourceAlpha)) 1410 { 1411 bRet = ImplWriteDIBBody(rSource, rOStm, *pAcc, pAccAlpha, bCompressed); 1412 } 1413 } 1414 else 1415 { 1416 bRet = ImplWriteDIBBody(rSource, rOStm, *pAcc, pAccAlpha, bCompressed); 1417 } 1418 1419 const_cast< Bitmap& >(rSource).ReleaseAccess(pAcc); 1420 1421 if(pAccAlpha) 1422 { 1423 const_cast< Bitmap* >(pSourceAlpha)->ReleaseAccess(pAccAlpha); 1424 } 1425 } 1426 1427 if(!bRet) 1428 { 1429 rOStm.SetError(SVSTREAM_GENERALERROR); 1430 rOStm.Seek(nOldPos); 1431 } 1432 1433 rOStm.SetNumberFormatInt(nOldFormat); 1434 } 1435 1436 return bRet; 1437 } 1438 1439 ////////////////////////////////////////////////////////////////////////////// 1440 1441 bool ReadDIB( 1442 Bitmap& rTarget, 1443 SvStream& rIStm, 1444 bool bFileHeader) 1445 { 1446 return ImplReadDIB(rTarget, 0, rIStm, bFileHeader); 1447 } 1448 1449 bool ReadDIBBitmapEx( 1450 BitmapEx& rTarget, 1451 SvStream& rIStm) 1452 { 1453 Bitmap aBmp; 1454 bool bRetval(ImplReadDIB(aBmp, 0, rIStm, true) && !rIStm.GetError()); 1455 1456 if(bRetval) 1457 { 1458 // base bitmap was read, set as return value and try to read alpha extra-data 1459 const sal_uLong nStmPos(rIStm.Tell()); 1460 sal_uInt32 nMagic1(0); 1461 sal_uInt32 nMagic2(0); 1462 1463 rTarget = BitmapEx(aBmp); 1464 rIStm >> nMagic1 >> nMagic2; 1465 bRetval = (0x25091962 == nMagic1) && (0xACB20201 == nMagic2) && !rIStm.GetError(); 1466 1467 if(bRetval) 1468 { 1469 sal_uInt8 bTransparent(false); 1470 1471 rIStm >> bTransparent; 1472 bRetval = !rIStm.GetError(); 1473 1474 if(bRetval) 1475 { 1476 if((sal_uInt8)TRANSPARENT_BITMAP == bTransparent) 1477 { 1478 Bitmap aMask; 1479 1480 bRetval = ImplReadDIB(aMask, 0, rIStm, true); 1481 1482 if(bRetval) 1483 { 1484 if(!!aMask) 1485 { 1486 // do we have an alpha mask? 1487 if((8 == aMask.GetBitCount()) && aMask.HasGreyPalette()) 1488 { 1489 AlphaMask aAlpha; 1490 1491 // create alpha mask quickly (without greyscale conversion) 1492 aAlpha.ImplSetBitmap(aMask); 1493 rTarget = BitmapEx(aBmp, aAlpha); 1494 } 1495 else 1496 { 1497 rTarget = BitmapEx(aBmp, aMask); 1498 } 1499 } 1500 } 1501 } 1502 else if((sal_uInt8)TRANSPARENT_COLOR == bTransparent) 1503 { 1504 Color aTransparentColor; 1505 1506 rIStm >> aTransparentColor; 1507 bRetval = !rIStm.GetError(); 1508 1509 if(bRetval) 1510 { 1511 rTarget = BitmapEx(aBmp, aTransparentColor); 1512 } 1513 } 1514 } 1515 } 1516 1517 if(!bRetval) 1518 { 1519 // alpha extra data could not be read; reset, but use base bitmap as result 1520 rIStm.ResetError(); 1521 rIStm.Seek(nStmPos); 1522 bRetval = true; 1523 } 1524 } 1525 1526 return bRetval; 1527 } 1528 1529 bool ReadDIBV5( 1530 Bitmap& rTarget, 1531 Bitmap& rTargetAlpha, 1532 SvStream& rIStm) 1533 { 1534 return ImplReadDIB(rTarget, &rTargetAlpha, rIStm, true); 1535 } 1536 1537 ////////////////////////////////////////////////////////////////////////////// 1538 1539 bool WriteDIB( 1540 const Bitmap& rSource, 1541 SvStream& rOStm, 1542 bool bCompressed, 1543 bool bFileHeader) 1544 { 1545 return ImplWriteDIB(rSource, 0, rOStm, bCompressed, bFileHeader); 1546 } 1547 1548 bool WriteDIBBitmapEx( 1549 const BitmapEx& rSource, 1550 SvStream& rOStm) 1551 { 1552 if(ImplWriteDIB(rSource.GetBitmap(), 0, rOStm, true, true)) 1553 { 1554 rOStm << (sal_uInt32)0x25091962; 1555 rOStm << (sal_uInt32)0xACB20201; 1556 rOStm << (sal_uInt8)rSource.eTransparent; 1557 1558 if(TRANSPARENT_BITMAP == rSource.eTransparent) 1559 { 1560 return ImplWriteDIB(rSource.aMask, 0, rOStm, true, true); 1561 } 1562 else if(TRANSPARENT_COLOR == rSource.eTransparent) 1563 { 1564 rOStm << rSource.aTransparentColor; 1565 return true; 1566 } 1567 } 1568 1569 return false; 1570 } 1571 1572 bool WriteDIBV5( 1573 const Bitmap& rSource, 1574 const Bitmap& rSourceAlpha, 1575 SvStream& rOStm) 1576 { 1577 return ImplWriteDIB(rSource, &rSourceAlpha, rOStm, false, true); 1578 } 1579 1580 ////////////////////////////////////////////////////////////////////////////// 1581 // eof 1582