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