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 13MallocBuffer::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 25MallocBuffer::~MallocBuffer() 26{ 27 if (fBuffer) 28 free(fBuffer); 29} 30 31// InitCheck 32status_t 33MallocBuffer::InitCheck() const 34{ 35 return fBuffer ? B_OK : B_NO_MEMORY; 36} 37 38// ColorSpace 39color_space 40MallocBuffer::ColorSpace() const 41{ 42 return B_RGBA32; 43} 44 45// Bits 46void* 47MallocBuffer::Bits() const 48{ 49 if (InitCheck() >= B_OK) 50 return fBuffer; 51 return NULL; 52} 53 54// BytesPerRow 55uint32 56MallocBuffer::BytesPerRow() const 57{ 58 if (InitCheck() >= B_OK) 59 return fWidth * 4; 60 return 0; 61} 62 63// Width 64uint32 65MallocBuffer::Width() const 66{ 67 if (InitCheck() >= B_OK) 68 return fWidth; 69 return 0; 70} 71 72// Height 73uint32 74MallocBuffer::Height() const 75{ 76 if (InitCheck() >= B_OK) 77 return fHeight; 78 return 0; 79} 80 81