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