1/* 2 * Copyright 2011, Haiku inc. 3 * Distributed under the terms of the MIT Licence. 4 * 5 * Documentation by: 6 * Axel Dörfler <axeld@pinc-software.de> 7 * John Scipione <jscipione@gmail.com> 8 * Corresponds to: 9 * /trunk/headers/os/interface/Bitmap.h rev 42274 10 * /trunk/src/kits/interface/Bitmap.cpp rev 42274 11 */ 12 13/*! 14 \file Bitmap.h 15 \brief Defines the BBitmap class and global operators and functions for 16 handling bitmaps. 17*/ 18 19 20/*! 21 \class BBitmap 22 \ingroup interface 23 \ingroup libbe 24 \brief Access and manipulate digital images commonly known as bitmaps. 25 26 A BBitmap is a rectangular map of pixel data. The BBitmap class allows you 27 to create a bitmap by specifying its pixel data and has operations for 28 altering and accessing the properties of bitmaps. 29 30 To create a BBitmap object use one of the constructor methods below. You 31 can determine if initialization was successful by calling the InitCheck() 32 method. You can determine if a BBitmap object is valid at any time by 33 calling the IsValid() method. 34 35 An example of creating a new 32x32 pixel BBitmap object and assigning the 36 icon of the current application looks like this: 37 \code 38BBitmap iconBitmap = new BBitmap(BRect(0, 0, 31, 31), B_RGBA32)); 39appFileInfo.GetIcon(iconBitmap, B_LARGE_ICON); 40 \endcode 41 42 You can access the properties of a bitmap by calling the Bounds(), 43 Flags(), ColorSpace(), Area(), Bits(), BitsLength(), BytesPerRow(), 44 and GetOverlayRestrictions() methods. 45 46 To directly set the pixel data of a bitmap call the Bits() or SetBits() 47 methods or you can use the ImportBits() method to copy the bits from an 48 existing bitmap. 49 50 You can also draw into a bitmap by attaching a child BView to the bitmap. 51 To add and remove child BView's to a bitmap call the AddChild() and 52 RemoveChild() methods respectively. You can access the child views of a 53 bitmap by calling the CountChildren(), ChildAt(), and FindView() methods. 54 55 For off-screen bitmaps it is important to lock the bitmap before drawing 56 the pixels and then unlock the bitmap when you are done to prevent 57 flickering. To lock and unlock a bitmap call the LockBits() and UnLockBits() 58 methods respectively. To lock and unlock the off-screen window that a 59 bitmap resides in you should call the Lock() and UnLock() methods. To 60 determine is a bitmap is currently locked you can call the IsLocked() 61 method. 62*/ 63 64 65/*! 66 \fn BBitmap::BBitmap(BRect bounds, uint32 flags, color_space colorSpace, 67 int32 bytesPerRow, screen_id screenID) 68 \brief Creates and initializes a BBitmap object. 69 70 \param bounds The bitmap dimensions. 71 \param flags Creation flags. 72 \param colorSpace The bitmap's color space. 73 \param bytesPerRow The number of bytes per row the bitmap should use. 74 \c B_ANY_BYTES_PER_ROW to let the constructor choose an appropriate 75 value. 76 \param screenID ??? 77*/ 78 79 80/*! 81 \fn BBitmap::BBitmap(BRect bounds, color_space colorSpace, 82 bool acceptsViews, bool needsContiguous) 83 \brief Creates and initializes a BBitmap object. 84 85 \param bounds The bitmap dimensions. 86 \param colorSpace The bitmap's color space. 87 \param acceptsViews \c true, if the bitmap shall accept BViews, i.e. if 88 it shall be possible to attach BView to the bitmap and draw into 89 it. 90 \param needsContiguous If \c true a physically contiguous chunk of memory 91 will be allocated. 92*/ 93 94 95/*! 96 \fn BBitmap::BBitmap(const BBitmap* source, bool acceptsViews, 97 bool needsContiguous) 98 \brief Creates a BBitmap object as a clone of another bitmap. 99 100 \param source The source bitmap. 101 \param acceptsViews \c true, if the bitmap shall accept BViews, i.e. if 102 it shall be possible to attach BView to the bitmap and draw into 103 it. 104 \param needsContiguous If \c true a physically contiguous chunk of memory 105 will be allocated. 106*/ 107 108 109/*! 110 \fn BBitmap::BBitmap(const BBitmap& source, uint32 flags) 111 \brief Creates a BBitmap object as a clone of another bitmap. 112 113 \param source The source bitmap. 114 \param flags Creation flags. 115*/ 116 117 118/*! 119 \fn BBitmap::BBitmap(const BBitmap& source) 120 \brief Creates a BBitmap object as a clone of another bitmap. 121 122 \param source The source bitmap. 123*/ 124 125 126/*! 127 \fn BBitmap::~BBitmap() 128 \brief Destructor Method 129 130 Frees all resources associated with this object. 131*/ 132 133 134/*! 135 \name Archiving 136*/ 137 138 139//! @{ 140 141 142/*! 143 \fn BBitmap::BBitmap(BMessage* data) 144 \brief Unarchives a bitmap from a BMessage. 145 146 \param data The archive. 147*/ 148 149 150/*! 151 \fn BArchivable* BBitmap::Instantiate(BMessage* data) 152 \brief Instantiates a BBitmap from an archive. 153 154 \param data The archive. 155 \return A bitmap reconstructed from the archive or \c NULL, if an error 156 occurred. 157*/ 158 159 160/*! 161 \fn status_t BBitmap::Archive(BMessage* data, bool deep) const 162 \brief Archives the BBitmap object. 163 164 \param data The archive. 165 \param deep if \c true, child object will be archived as well. 166 \return \c B_OK, if everything went fine, an error code otherwise. 167*/ 168 169 170//! @} 171 172 173/*! 174 \fn status_t BBitmap::InitCheck() const 175 \brief Gets the status of the constructor. 176 177 \returns B_OK if initialization succeeded, otherwise returns an 178 error status. 179*/ 180 181 182/*! 183 \fn bool BBitmap::IsValid() const 184 \brief Determines whether or not the BBitmap object is valid. 185 186 \return \c true, if the object is properly initialized, \c false otherwise. 187*/ 188 189 190/*! 191 \name Locking 192*/ 193 194 195//! @{ 196 197 198/*! 199 \fn status_t BBitmap::LockBits(uint32* state) 200 \brief Locks the bitmap bits so that they cannot be relocated. 201 202 This is currently only used for overlay bitmaps; whenever you 203 need to access their Bits() you must lock them first. 204 On resolution change overlay bitmaps can be relocated in memory; 205 using this call prevents you from accessing an invalid pointer 206 and clobbering memory that doesn't belong you. 207 208 \param state Unused 209 \returns \c B_OK on success or an error status code. 210*/ 211 212 213/*! 214 \fn void BBitmap::UnlockBits() 215 \brief Unlocks the bitmap's buffer. 216 217 Counterpart to BBitmap::LockBits(). 218*/ 219 220 221/*! 222 \fn bool BBitmap::Lock() 223 \brief Locks the off-screen window that belongs to the bitmap. 224 225 The bitmap must accept views, if locking should work. 226 227 \returns \c true, if the lock was acquired successfully. 228*/ 229 230 231/*! 232 \fn void BBitmap::Unlock() 233 \brief Unlocks the off-screen window that belongs to the bitmap. 234 235 The bitmap must accept views, if locking should work. 236*/ 237 238 239/*! 240 \fn bool BBitmap::IsLocked() const 241 \brief Determines whether or not the bitmap's off-screen window is locked. 242 243 The bitmap must accept views, if locking should work. 244 245 \return \c true, if the caller owns a lock , \c false otherwise. 246*/ 247 248 249//! @} 250 251 252/*! 253 \name Accessors 254*/ 255 256 257//! @{ 258 259 260/*! 261 \fn area_id BBitmap::Area() const 262 \brief Gets the ID of the area the bitmap data reside in. 263 264 \return The ID of the area the bitmap data reside in. 265*/ 266 267 268/*! 269 \fn void* BBitmap::Bits() const 270 \brief Gets the pointer to the bitmap data. 271 272 \return The pointer to the bitmap data. 273*/ 274 275 276/*! 277 \fn int32 BBitmap::BitsLength() const 278 \brief Gets the length of the bitmap data. 279 280 \return The length of the bitmap data as an int32. 281*/ 282 283 284/*! 285 \fn int32 BBitmap::BytesPerRow() const 286 \brief Gets the number of bytes used to store a row of bitmap data. 287 288 \return The number of bytes used to store a row of bitmap data. 289*/ 290 291 292/*! 293 \fn color_space BBitmap::ColorSpace() const 294 \brief Gets the bitmap's color space. 295 296 \return The bitmap's color space. 297*/ 298 299 300/*! 301 \fn BRect BBitmap::Bounds() const 302 \brief Gets a BRect the size of the bitmap's dimensions. 303 304 \return A BRect the size of the bitmap's dimensions. 305*/ 306 307 308/*! 309 \fn uint32 BBitmap::Flags() const 310 \brief Accesses the bitmap's creation flags. 311 312 This method informs about which flags have been used to create the 313 bitmap. It would for example tell you wether this is an overlay 314 bitmap. If bitmap creation succeeded, all flags are fulfilled. 315 316 \return The bitmap's creation flags. 317*/ 318 319 320/*! 321 \fn status_t BBitmap::GetOverlayRestrictions(overlay_restrictions* 322 restrictions) const 323 \brief Gets the overlay_restrictions structure for this bitmap. 324 325 \note This function is not part of the BeOS R5 API. 326 327 \param restrictions The overlay restrictions flag 328 329 \retval B_OK The overlay restriction structure was found. 330 \retval B_BAD_TYPE The overlay restriction structure for the bitmap could 331 not be found. 332*/ 333 334 335//! @} 336 337 338/*! 339 \name Setters 340*/ 341 342 343//! @{ 344 345 346/*! 347 \fn void BBitmap::SetBits(const void* data, int32 length, int32 offset, 348 color_space colorSpace) 349 \brief Assigns data to the bitmap. 350 351 Data are directly written into the bitmap's data buffer, being converted 352 beforehand, if necessary. Some conversions do not work intuitively: 353 - \c B_RGB32: The source buffer is supposed to contain \c B_RGB24_BIG 354 data without padding at the end of the rows. 355 - \c B_RGB32: The source buffer is supposed to contain \c B_CMAP8 356 data without padding at the end of the rows. 357 - other color spaces: The source buffer is supposed to contain data 358 according to the specified color space being padded to int32 row-wise. 359 360 The currently supported source/target color spaces are 361 <code>B_RGB{32,24,16,15}[_BIG]</code>, \c B_CMAP8 and 362 <code>B_GRAY{8,1}</code>. 363 364 \note Since this methods is a bit strange to use, Haiku has introduced 365 the ImportBits() method which is the recommended replacement. 366 367 \param data The data to be copied. 368 \param length The length in bytes of the data to be copied. 369 \param offset The offset (in bytes) relative to beginning of the bitmap 370 data specifying the position at which the source data shall be 371 written. 372 \param colorSpace Color space of the source data. 373*/ 374 375 376/*! 377 \fn status_t BBitmap::ImportBits(const void* data, int32 length, int32 bpr, 378 int32 offset, color_space colorSpace) 379 \brief Assigns data to the bitmap. 380 381 Data are directly written into the bitmap's data buffer, being converted 382 beforehand, if necessary. Unlike for SetBits(), the meaning of 383 \a colorSpace is exactly the expected one here, i.e. the source buffer 384 is supposed to contain data of that color space. \a bpr specifies how 385 many bytes the source contains per row. \c B_ANY_BYTES_PER_ROW can be 386 supplied, if standard padding to int32 is used. 387 388 The currently supported source/target color spaces are 389 <code>B_RGB{32,24,16,15}[_BIG]</code>, \c B_CMAP8 and 390 <code>B_GRAY{8,1}</code>. 391 392 \note This function is not part of the BeOS R5 API. 393 394 \param data The data to be copied. 395 \param length The length in bytes of the data to be copied. 396 \param bpr The number of bytes per row in the source data. 397 \param offset The offset (in bytes) relative to beginning of the bitmap 398 data specifying the position at which the source data shall be 399 written. 400 \param colorSpace Color space of the source data. 401 402 \retval B_OK The bits were imported into the bitmap. 403 \retval B_BAD_VALUE \c NULL \a data, invalid \a bpr or \a offset, or 404 unsupported \a colorSpace. 405*/ 406 407 408/*! 409 \fn status_t BBitmap::ImportBits(const void* data, int32 length, 410 int32 bpr, color_space colorSpace, BPoint from, BPoint to, 411 int32 width, int32 height) 412 \brief Assigns data to the bitmap. 413 414 Allows for a BPoint offset in the source and in the bitmap. The region 415 of the source at \a from extending \a width and \a height is assigned 416 (and converted if necessary) to the bitmap at \a to. 417 418 The currently supported source/target color spaces are 419 <code>B_RGB{32,24,16,15}[_BIG]</code>, \c B_CMAP8 and 420 <code>B_GRAY{8,1}</code>. 421 422 \note This function is not part of the BeOS R5 API. 423 424 \param data The data to be copied. 425 \param length The length in bytes of the data to be copied. 426 \param bpr The number of bytes per row in the source data. 427 \param colorSpace Color space of the source data. 428 \param from The offset in the source where reading should begin. 429 \param to The offset in the bitmap where the source should be written. 430 \param width The width (in pixels) to be imported. 431 \param height The height (in pixels) to be imported. 432 433 \retval B_OK The bits were imported into the bitmap. 434 \retval B_BAD_VALUE: \c NULL \a data, invalid \a bpr, unsupported 435 \a colorSpace or invalid \a width or \a height. 436*/ 437 438 439/*! 440 \fn status_t BBitmap::ImportBits(const BBitmap* bitmap) 441 \brief Assigns another bitmap's data to this bitmap. 442 443 The supplied bitmap must have the exactly same dimensions as this bitmap. 444 Its data is converted to the color space of this bitmap. 445 446 The currently supported source/target color spaces are 447 <code>B_RGB{32,24,16,15}[_BIG]</code>, \c B_CMAP8 and 448 <code>B_GRAY{8,1}</code>. 449 450 \note This function is not part of the BeOS R5 API. 451 452 \param bitmap The source bitmap. 453 454 \retval B_OK The bits were imported into the bitmap. 455 \retval B_BAD_VALUE \c NULL \a bitmap, or \a bitmap has other dimensions, 456 or the conversion from or to one of the color spaces is not supported. 457*/ 458 459 460/*! 461 \fn status_t BBitmap::ImportBits(const BBitmap* bitmap, BPoint from, 462 BPoint to,int32 width, int32 height) 463 \brief Assigns data to the bitmap. 464 465 Allows for a BPoint offset in the source and in the bitmap. The region 466 of the source at \a from extending \a width and \a height is assigned 467 (and converted if necessary) to the bitmap at \a to. The source bitmap is 468 clipped to the bitmap and they don't need to have the same dimensions. 469 470 The currently supported source/target color spaces are 471 <code>B_RGB{32,24,16,15}[_BIG]</code>, \c B_CMAP8 and 472 <code>B_GRAY{8,1}</code>. 473 474 \note This function is not part of the BeOS R5 API. 475 476 \param bitmap The source bitmap. 477 \param from The offset in the source where reading should begin. 478 \param to The offset in the bitmap where the source should be written. 479 \param width The width (in pixels) to be imported. 480 \param height The height (in pixels) to be imported. 481 482 \retval B_OK The bits were imported into the bitmap. 483 \retval B_BAD_VALUE \c NULL \a bitmap, the conversion from or to one of 484 the color spaces is not supported, or invalid \a width or \a height. 485*/ 486 487 488//! @} 489 490 491/*! 492 \name Child View Methods 493*/ 494 495 496//! @{ 497 498 499/*! 500 \fn void BBitmap::AddChild(BView* view) 501 \brief Adds a BView to the bitmap's view hierarchy. 502 503 The bitmap must accept views and the supplied view must not be child of 504 another parent. 505 506 \param view The view to be added. 507*/ 508 509 510/*! 511 \fn bool BBitmap::RemoveChild(BView* view) 512 \brief Removes a BView from the bitmap's view hierarchy. 513 514 \param view The view to be removed. 515*/ 516 517 518/*! 519 \fn int32 BBitmap::CountChildren() const 520 \brief Gets the number of BViews currently belonging to the bitmap. 521 522 \returns The number of BViews currently belonging to the bitmap. 523*/ 524 525 526/*! 527 \fn BView* BBitmap::ChildAt(int32 index) const 528 \brief Gets the BView at a certain index in the bitmap's list of views. 529 530 \param index The index of the BView to be returned. 531 \returns The BView at index \a index or \c NULL if the index is out of 532 range. 533*/ 534 535 536/*! 537 \fn BView* BBitmap::FindView(const char* viewName) const 538 \brief Accesses a bitmap's child BView with a the name \a viewName. 539 540 \param viewName The name of the BView to be returned. 541 \returns The BView with the name \a name or \c NULL if the bitmap doesn't 542 know a view with that name. 543*/ 544 545 546/*! 547 \fn BView* BBitmap::FindView(BPoint point) const 548 \brief Accesses a bitmap's BView at a certain location. 549 550 \param point The location. 551 \returns The BView with located at \a point or \c NULL if the bitmap 552 doesn't know a view at this location. 553*/ 554 555 556//! @} 557