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 23 24 #ifndef _SV_BITMAPEX_HXX 25 #define _SV_BITMAPEX_HXX 26 27 #include <vcl/dllapi.h> 28 #include <vcl/bitmap.hxx> 29 #include <vcl/alpha.hxx> 30 #include <tools/color.hxx> 31 #include <basegfx/color/bcolormodifier.hxx> 32 33 // ------------------- 34 // - TransparentType - 35 // ------------------- 36 37 enum TransparentType 38 { 39 TRANSPARENT_NONE, 40 TRANSPARENT_COLOR, 41 TRANSPARENT_BITMAP 42 }; 43 44 // ------------ 45 // - BitmapEx - 46 // ------------ 47 48 class VCL_DLLPUBLIC BitmapEx 49 { 50 private: 51 friend class ImpGraphic; 52 friend bool VCL_DLLPUBLIC WriteDIBBitmapEx(const BitmapEx& rSource, SvStream& rOStm); 53 54 Bitmap aBitmap; 55 Bitmap aMask; 56 Size aBitmapSize; 57 Color aTransparentColor; 58 TransparentType eTransparent; 59 sal_Bool bAlpha; 60 61 public: 62 ImplGetBitmapImpBitmap() const63 SAL_DLLPRIVATE ImpBitmap* ImplGetBitmapImpBitmap() const { return aBitmap.ImplGetImpBitmap(); } ImplGetMaskImpBitmap() const64 SAL_DLLPRIVATE ImpBitmap* ImplGetMaskImpBitmap() const { return aMask.ImplGetImpBitmap(); } 65 66 BitmapEx(); 67 BitmapEx( const ResId& rResId ); 68 BitmapEx( const BitmapEx& rBitmapEx ); 69 BitmapEx( const BitmapEx& rBitmapEx, Point aSrc, Size aSize ); 70 BitmapEx( const Bitmap& rBmp ); 71 BitmapEx( const Bitmap& rBmp, const Bitmap& rMask ); 72 BitmapEx( const Bitmap& rBmp, const AlphaMask& rAlphaMask ); 73 BitmapEx( const Bitmap& rBmp, const Color& rTransparentColor ); 74 ~BitmapEx(); 75 76 BitmapEx& operator=( const BitmapEx& rBitmapEx ); 77 sal_Bool operator==( const BitmapEx& rBitmapEx ) const; operator !=(const BitmapEx & rBitmapEx) const78 sal_Bool operator!=( const BitmapEx& rBitmapEx ) const { return !(*this==rBitmapEx); } operator !() const79 sal_Bool operator!() const { return !aBitmap; } 80 81 sal_Bool IsEqual( const BitmapEx& rBmpEx ) const; 82 83 sal_Bool IsEmpty() const; 84 void SetEmpty(); 85 void Clear(); 86 87 void Draw( OutputDevice* pOutDev, 88 const Point& rDestPt ) const; 89 void Draw( OutputDevice* pOutDev, 90 const Point& rDestPt, const Size& rDestSize ) const; 91 void Draw( OutputDevice* pOutDev, 92 const Point& rDestPt, const Size& rDestSize, 93 const Point& rSrcPtPixel, const Size& rSrcSizePixel ) const; 94 95 sal_Bool IsTransparent() const; GetTransparentType() const96 TransparentType GetTransparentType() const { return eTransparent; } 97 98 Bitmap GetBitmap( const Color* pTransReplaceColor = NULL ) const; 99 Bitmap GetMask() const; 100 101 BitmapEx GetColorTransformedBitmapEx( BmpColorMode eColorMode ) const; 102 103 sal_Bool IsAlpha() const; 104 AlphaMask GetAlpha() const; 105 GetSizePixel() const106 const Size& GetSizePixel() const { return aBitmapSize; } 107 void SetSizePixel( const Size& rNewSize, sal_uInt32 nScaleFlag = BMP_SCALE_FASTESTINTERPOLATE ); 108 GetPrefSize() const109 const Size& GetPrefSize() const { return aBitmap.GetPrefSize(); } SetPrefSize(const Size & rPrefSize)110 void SetPrefSize( const Size& rPrefSize ) { aBitmap.SetPrefSize( rPrefSize ); } 111 GetPrefMapMode() const112 const MapMode& GetPrefMapMode() const { return aBitmap.GetPrefMapMode(); } SetPrefMapMode(const MapMode & rPrefMapMode)113 void SetPrefMapMode( const MapMode& rPrefMapMode ) { aBitmap.SetPrefMapMode( rPrefMapMode ); } 114 GetTransparentColor() const115 const Color& GetTransparentColor() const { return aTransparentColor; } SetTransparentColor(const Color & rColor)116 void SetTransparentColor( const Color& rColor ) { aTransparentColor = rColor; } 117 GetBitCount() const118 sal_uInt16 GetBitCount() const { return aBitmap.GetBitCount(); } 119 sal_uLong GetSizeBytes() const; 120 sal_uLong GetChecksum() const; 121 122 public: 123 124 /** Convert bitmap format 125 126 @param eConversion 127 The format this bitmap should be converted to. 128 129 @return sal_True, if the conversion was completed successfully. 130 */ 131 sal_Bool Convert( BmpConversion eConversion ); 132 133 /** Reduce number of colors for the bitmap 134 135 @param nNewColorCount 136 Maximal number of bitmap colors after the reduce operation 137 138 @param eReduce 139 Algorithm to use for color reduction 140 141 @return sal_True, if the color reduction operation was completed successfully. 142 */ 143 sal_Bool ReduceColors( sal_uInt16 nNewColorCount, 144 BmpReduce eReduce = BMP_REDUCE_SIMPLE ); 145 146 /** Apply a dither algorithm to the bitmap 147 148 This method dithers the bitmap inplace, i.e. a true color 149 bitmap is converted to a paletted bitmap, reducing the color 150 deviation by error diffusion. 151 152 @param nDitherFlags 153 The algorithm to be used for dithering 154 */ 155 sal_Bool Dither( sal_uLong nDitherFlags = BMP_DITHER_MATRIX ); 156 157 /** Crop the bitmap 158 159 @param rRectPixel 160 A rectangle specifying the crop amounts on all four sides of 161 the bitmap. If the upper left corner of the bitmap is assigned 162 (0,0), then this method cuts out the given rectangle from the 163 bitmap. Note that the rectangle is clipped to the bitmap's 164 dimension, i.e. negative left,top rectangle coordinates or 165 exceeding width or height is ignored. 166 167 @return sal_True, if cropping was performed successfully. If 168 nothing had to be cropped, because e.g. the crop rectangle 169 included the bitmap, sal_False is returned, too! 170 */ 171 sal_Bool Crop( const Rectangle& rRectPixel ); 172 173 /** Expand the bitmap by pixel padding 174 175 @param nDX 176 Number of pixel to pad at the right border of the bitmap 177 178 @param nDY 179 Number of scanlines to pad at the bottom border of the bitmap 180 181 @param pInitColor 182 Color to use for padded pixel 183 184 @return sal_True, if padding was performed successfully. sal_False is 185 not only returned when the operation failed, but also if 186 nothing had to be done, e.g. because nDX and nDY were zero. 187 */ 188 sal_Bool Expand( sal_uLong nDX, sal_uLong nDY, 189 const Color* pInitColor = NULL, 190 sal_Bool bExpandTransparent = sal_False ); 191 192 /** Copy a rectangular area from another bitmap 193 194 @param rRectDst 195 Destination rectangle in this bitmap. This is clipped to the 196 bitmap dimensions. 197 198 @param rRectSrc 199 Source rectangle in pBmpSrc. This is clipped to the source 200 bitmap dimensions. Note further that no scaling takes place 201 during this copy operation, i.e. only the minimum of source 202 and destination rectangle's width and height are used. 203 204 @param pBmpSrc 205 The source bitmap to copy from. If this argument is NULL, or 206 equal to the object this method is called on, copying takes 207 place within the same bitmap. 208 209 @return sal_True, if the operation completed successfully. sal_False 210 is not only returned when the operation failed, but also if 211 nothing had to be done, e.g. because one of the rectangles are 212 empty. 213 */ 214 sal_Bool CopyPixel( const Rectangle& rRectDst, 215 const Rectangle& rRectSrc, 216 const BitmapEx* pBmpExSrc = NULL ); 217 218 /** Fill the entire bitmap with the given color 219 220 @param rFillColor 221 Color value to use for filling. Set the transparency part of 222 the color to fill the mask. 223 224 @return sal_True, if the operation was completed successfully. 225 */ 226 sal_Bool Erase( const Color& rFillColor ); 227 228 /** Perform the Invert operation on every pixel 229 230 @return sal_True, if the operation was completed successfully. 231 */ 232 sal_Bool Invert(); 233 234 /** Mirror the bitmap 235 236 @param nMirrorFlags 237 About which axis (horizontal, vertical, or both) to mirror 238 239 @return sal_True, if the operation was completed successfully. 240 */ 241 sal_Bool Mirror( sal_uLong nMirrorFlags ); 242 243 /** Scale the bitmap 244 245 @param rNewSize 246 The resulting size of the scaled bitmap 247 248 @param nScaleFlag 249 The algorithm to be used for scaling 250 251 @return sal_True, if the operation was completed successfully. 252 */ 253 sal_Bool Scale( const Size& rNewSize, sal_uInt32 nScaleFlag = BMP_SCALE_FASTESTINTERPOLATE ); 254 255 /** Scale the bitmap 256 257 @param rScaleX 258 The scale factor in x direction. 259 260 @param rScaleY 261 The scale factor in y direction. 262 263 @return sal_True, if the operation was completed successfully. 264 */ 265 sal_Bool Scale( const double& rScaleX, const double& rScaleY, sal_uInt32 nScaleFlag = BMP_SCALE_FASTESTINTERPOLATE ); 266 267 /** Rotate bitmap by the specified angle 268 269 @param nAngle10 270 The rotation angle in tenth of a degree. The bitmap is always rotated around its center. 271 272 @param rFillColor 273 The color to use for filling blank areas. During rotation, the 274 bitmap is enlarged such that the whole rotation result fits 275 in. The empty spaces around that rotated original bitmap are 276 then filled with this color. 277 278 @return sal_True, if the operation was completed successfully. 279 */ 280 sal_Bool Rotate( long nAngle10, const Color& rFillColor ); 281 282 /** Replace all pixel having the search color with the specified color 283 284 @param rSearchColor 285 Color specifying which pixel should be replaced 286 287 @param rReplaceColor 288 Color to be placed in all changed pixel 289 290 @param nTol 291 Tolerance value. Specifies the maximal difference between 292 rSearchColor and the individual pixel values, such that the 293 corresponding pixel is still regarded a match. 294 295 @return sal_True, if the operation was completed successfully. 296 */ 297 sal_Bool Replace( const Color& rSearchColor, const Color& rReplaceColor, sal_uLong nTol = 0 ); 298 299 /** Replace all pixel having one the search colors with the corresponding replace color 300 301 @param pSearchColor 302 Array of colors specifying which pixel should be replaced 303 304 @param pReplaceColor 305 Array of colors to be placed in all changed pixel 306 307 @param nColorCount 308 Size of the aforementioned color arrays 309 310 @param nTol 311 Tolerance value. Specifies the maximal difference between 312 pSearchColor colors and the individual pixel values, such that 313 the corresponding pixel is still regarded a match. 314 315 @return sal_True, if the operation was completed successfully. 316 */ 317 sal_Bool Replace( const Color* pSearchColors, const Color* pReplaceColors, 318 sal_uLong nColorCount, const sal_uLong* pTols = NULL ); 319 320 /** Change various global color characteristics 321 322 @param nLuminancePercent 323 Percent of luminance change, valid range [-100,100]. Values outside this range are clipped to the valid range. 324 325 @param nContrastPercent 326 Percent of contrast change, valid range [-100,100]. Values outside this range are clipped to the valid range. 327 328 @param nChannelRPercent 329 Percent of red channel change, valid range [-100,100]. Values outside this range are clipped to the valid range. 330 331 @param nChannelGPercent 332 Percent of green channel change, valid range [-100,100]. Values outside this range are clipped to the valid range. 333 334 @param nChannelBPercent 335 Percent of blue channel change, valid range [-100,100]. Values outside this range are clipped to the valid range. 336 337 @param fGamma 338 Exponent of the gamma function applied to the bitmap. The 339 value 1.0 results in no change, the valid range is 340 (0.0,10.0]. Values outside this range are regarded as 1.0. 341 342 @param bInvert 343 If sal_True, invert the channel values with the logical 'not' operator 344 345 @return sal_True, if the operation was completed successfully. 346 */ 347 sal_Bool Adjust( short nLuminancePercent = 0, 348 short nContrastPercent = 0, 349 short nChannelRPercent = 0, 350 short nChannelGPercent = 0, 351 short nChannelBPercent = 0, 352 double fGamma = 1.0, 353 sal_Bool bInvert = sal_False ); 354 355 /** Apply specified filter to the bitmap 356 357 @param eFilter 358 The filter algorithm to apply 359 360 @param pFilterParam 361 Various parameter for the different bitmap filter algorithms 362 363 @param pProgress 364 A callback for showing the progress of the vectorization 365 366 @return sal_True, if the operation was completed successfully. 367 */ 368 sal_Bool Filter( BmpFilter eFilter, 369 const BmpFilterParam* pFilterParam = NULL, 370 const Link* pProgress = NULL ); 371 372 /** Get transparency at given position 373 374 @param nX 375 integer X-Position in Bitmap 376 377 @param nY 378 integer Y-Position in Bitmap 379 380 @return transparency value in the range of [0 .. 255] where 381 0 is not transparent, 255 is fully transparent 382 */ 383 sal_uInt8 GetTransparency(sal_Int32 nX, sal_Int32 nY) const; 384 385 /** Create transformed Bitmap 386 387 @param fWidth 388 The target width in pixels 389 390 @param fHeight 391 The target height in pixels 392 393 @param rTransformation 394 The back transformation for each pixel in (0 .. fWidth),(0 .. fHeight) to 395 local pixel coordiantes 396 397 @param bSmooth 398 Defines if pixel interpolation is to be used to create the result 399 */ 400 BitmapEx TransformBitmapEx( 401 double fWidth, 402 double fHeight, 403 const basegfx::B2DHomMatrix& rTransformation, 404 bool bSmooth = true) const; 405 406 /** Create transformed Bitmap 407 408 @param rTransformation 409 The transformation from unit coordinates to the unit range 410 411 @param rVisibleRange 412 The relative visible range in unit coordinates, relative to (0,0,1,1) which 413 defines the whole target area 414 415 @param fMaximumArea 416 A limitation for the maximum size of pixels to use for the result 417 418 @param bSmooth 419 Defines if pixel interpolation is to be used to create the result 420 421 The traget size of the result bitmap is defined by transforming the given 422 rTargetRange with the given rTransformation; the area of the result is 423 linearly scaled to not exceed the given fMaximumArea 424 425 @return The transformed bitmap 426 */ 427 BitmapEx getTransformed( 428 const basegfx::B2DHomMatrix& rTransformation, 429 const basegfx::B2DRange& rVisibleRange, 430 double fMaximumArea = 500000.0, 431 bool bSmooth = true) const; 432 433 /** Create ColorStack-modified version of this BitmapEx 434 435 @param rBColorModifierStack 436 A ColrModifierStack which defines how each pixel has to be modified 437 */ 438 BitmapEx ModifyBitmapEx(const basegfx::BColorModifierStack& rBColorModifierStack) const; 439 }; 440 441 // ------------------------------------------------------------------ 442 /** Create a blend frame as BitmapEx 443 444 @param nAlpha 445 The blend value defines how strong the frame will be blended with the 446 existing content, 255 == full coverage, 0 == no frame will be drawn 447 448 @param aColorTopLeft, aColorBottomRight, aColorTopRight, aColorBottomLeft 449 The colors defining the frame. If the version without aColorTopRight and 450 aColorBottomLeft is used, these colors are linearly interpolated from 451 aColorTopLeft and aColorBottomRight using the width and height of the area 452 453 @param rSize 454 The size of the frame in pixels 455 */ 456 457 BitmapEx VCL_DLLPUBLIC createBlendFrame( 458 const Size& rSize, 459 sal_uInt8 nAlpha, 460 Color aColorTopLeft, 461 Color aColorBottomRight); 462 463 BitmapEx VCL_DLLPUBLIC createBlendFrame( 464 const Size& rSize, 465 sal_uInt8 nAlpha, 466 Color aColorTopLeft, 467 Color aColorTopRight, 468 Color aColorBottomRight, 469 Color aColorBottomLeft); 470 471 // ------------------------------------------------------------------ 472 473 #endif // _SV_BITMAPEX_HXX 474