xref: /haiku/src/servers/app/drawing/MallocBuffer.cpp (revision 7749d0bb0c358a3279b1b9cc76d8376e900130a5)
1 // MallocBuffer.h
2 
3 #include <malloc.h>
4 
5 #include "MallocBuffer.h"
6 
7 // TODO: maybe this class could be more flexible by taking
8 // a color_space argument in the constructor
9 // the hardcoded width * 4 (because that's how it's used now anyways)
10 // could be avoided, but I'm in a hurry... :-)
11 
12 // constructor
13 MallocBuffer::MallocBuffer(uint32 width,
14 						   uint32 height)
15 	: fBuffer(NULL),
16 	  fWidth(width),
17 	  fHeight(height)
18 {
19 	if (fWidth > 0 && fHeight > 0) {
20 		fBuffer = malloc((fWidth * 4) * fHeight);
21 	}
22 }
23 
24 // destructor
25 MallocBuffer::~MallocBuffer()
26 {
27 	if (fBuffer)
28 		free(fBuffer);
29 }
30 
31 // InitCheck
32 status_t
33 MallocBuffer::InitCheck() const
34 {
35 	return fBuffer ? B_OK : B_NO_MEMORY;
36 }
37 
38 // ColorSpace
39 color_space
40 MallocBuffer::ColorSpace() const
41 {
42 	return B_RGBA32;
43 }
44 
45 // Bits
46 void*
47 MallocBuffer::Bits() const
48 {
49 	if (InitCheck() >= B_OK)
50 		return fBuffer;
51 	return NULL;
52 }
53 
54 // BytesPerRow
55 uint32
56 MallocBuffer::BytesPerRow() const
57 {
58 	if (InitCheck() >= B_OK)
59 		return fWidth * 4;
60 	return 0;
61 }
62 
63 // Width
64 uint32
65 MallocBuffer::Width() const
66 {
67 	if (InitCheck() >= B_OK)
68 		return fWidth;
69 	return 0;
70 }
71 
72 // Height
73 uint32
74 MallocBuffer::Height() const
75 {
76 	if (InitCheck() >= B_OK)
77 		return fHeight;
78 	return 0;
79 }
80 
81