HaikuDepot: SharedBitmap: Added loading from buffers.
Refactored loading from a buffer when loading from resources and re-used that for instantiating a SharedBitmap from a BMallocIO.
This commit is contained in:
@@ -26,6 +26,8 @@ SharedBitmap::SharedBitmap(BBitmap* bitmap)
|
|||||||
:
|
:
|
||||||
BReferenceable(),
|
BReferenceable(),
|
||||||
fResourceID(-1),
|
fResourceID(-1),
|
||||||
|
fBuffer(NULL),
|
||||||
|
fSize(0),
|
||||||
fMimeType()
|
fMimeType()
|
||||||
{
|
{
|
||||||
fBitmap[0] = bitmap;
|
fBitmap[0] = bitmap;
|
||||||
@@ -38,6 +40,8 @@ SharedBitmap::SharedBitmap(int32 resourceID)
|
|||||||
:
|
:
|
||||||
BReferenceable(),
|
BReferenceable(),
|
||||||
fResourceID(resourceID),
|
fResourceID(resourceID),
|
||||||
|
fBuffer(NULL),
|
||||||
|
fSize(0),
|
||||||
fMimeType()
|
fMimeType()
|
||||||
{
|
{
|
||||||
fBitmap[0] = NULL;
|
fBitmap[0] = NULL;
|
||||||
@@ -50,6 +54,8 @@ SharedBitmap::SharedBitmap(const char* mimeType)
|
|||||||
:
|
:
|
||||||
BReferenceable(),
|
BReferenceable(),
|
||||||
fResourceID(-1),
|
fResourceID(-1),
|
||||||
|
fBuffer(NULL),
|
||||||
|
fSize(0),
|
||||||
fMimeType(mimeType)
|
fMimeType(mimeType)
|
||||||
{
|
{
|
||||||
fBitmap[0] = NULL;
|
fBitmap[0] = NULL;
|
||||||
@@ -58,18 +64,36 @@ SharedBitmap::SharedBitmap(const char* mimeType)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SharedBitmap::SharedBitmap(const BMallocIO& data)
|
||||||
|
:
|
||||||
|
BReferenceable(),
|
||||||
|
fResourceID(-1),
|
||||||
|
fBuffer(new(std::nothrow) uint8[data.BufferLength()]),
|
||||||
|
fSize(data.BufferLength()),
|
||||||
|
fMimeType()
|
||||||
|
{
|
||||||
|
if (fBuffer != NULL)
|
||||||
|
memcpy(fBuffer, data.Buffer(), fSize);
|
||||||
|
|
||||||
|
fBitmap[0] = NULL;
|
||||||
|
fBitmap[1] = NULL;
|
||||||
|
fBitmap[2] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
SharedBitmap::~SharedBitmap()
|
SharedBitmap::~SharedBitmap()
|
||||||
{
|
{
|
||||||
delete fBitmap[0];
|
delete fBitmap[0];
|
||||||
delete fBitmap[1];
|
delete fBitmap[1];
|
||||||
delete fBitmap[2];
|
delete fBitmap[2];
|
||||||
|
delete[] fBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const BBitmap*
|
const BBitmap*
|
||||||
SharedBitmap::Bitmap(Size which)
|
SharedBitmap::Bitmap(Size which)
|
||||||
{
|
{
|
||||||
if (fResourceID == -1 && fMimeType.Length() == 0)
|
if (fResourceID == -1 && fMimeType.Length() == 0 && fBuffer == NULL)
|
||||||
return fBitmap[0];
|
return fBitmap[0];
|
||||||
|
|
||||||
int32 index = 0;
|
int32 index = 0;
|
||||||
@@ -93,6 +117,8 @@ SharedBitmap::Bitmap(Size which)
|
|||||||
if (fBitmap[index] == NULL) {
|
if (fBitmap[index] == NULL) {
|
||||||
if (fResourceID >= 0)
|
if (fResourceID >= 0)
|
||||||
fBitmap[index] = _CreateBitmapFromResource(size);
|
fBitmap[index] = _CreateBitmapFromResource(size);
|
||||||
|
else if (fBuffer != NULL)
|
||||||
|
fBitmap[index] = _CreateBitmapFromBuffer(size);
|
||||||
else if (fMimeType.Length() > 0)
|
else if (fMimeType.Length() > 0)
|
||||||
fBitmap[index] = _CreateBitmapFromMimeType(size);
|
fBitmap[index] = _CreateBitmapFromMimeType(size);
|
||||||
}
|
}
|
||||||
@@ -112,48 +138,29 @@ SharedBitmap::_CreateBitmapFromResource(int32 size) const
|
|||||||
size_t dataSize;
|
size_t dataSize;
|
||||||
const void* data = resources.LoadResource(B_VECTOR_ICON_TYPE, fResourceID,
|
const void* data = resources.LoadResource(B_VECTOR_ICON_TYPE, fResourceID,
|
||||||
&dataSize);
|
&dataSize);
|
||||||
if (data != NULL) {
|
if (data != NULL)
|
||||||
BBitmap* bitmap = new BBitmap(BRect(0, 0, size - 1, size - 1), 0,
|
return _LoadIconFromBuffer(data, dataSize, size);
|
||||||
B_RGBA32);
|
|
||||||
status = bitmap->InitCheck();
|
|
||||||
if (status == B_OK) {
|
|
||||||
status = BIconUtils::GetVectorIcon(
|
|
||||||
reinterpret_cast<const uint8*>(data), dataSize, bitmap);
|
|
||||||
};
|
|
||||||
|
|
||||||
if (status != B_OK) {
|
|
||||||
delete bitmap;
|
|
||||||
bitmap = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bitmap;
|
|
||||||
}
|
|
||||||
|
|
||||||
data = resources.LoadResource(B_MESSAGE_TYPE, fResourceID, &dataSize);
|
data = resources.LoadResource(B_MESSAGE_TYPE, fResourceID, &dataSize);
|
||||||
if (data != NULL) {
|
if (data != NULL)
|
||||||
BMemoryIO stream(data, dataSize);
|
return _LoadBitmapFromBuffer(data, dataSize);
|
||||||
|
|
||||||
// Try to read as an archived bitmap.
|
|
||||||
BMessage archive;
|
|
||||||
status = archive.Unflatten(&stream);
|
|
||||||
if (status != B_OK)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
BBitmap* bitmap = new BBitmap(&archive);
|
|
||||||
|
|
||||||
status = bitmap->InitCheck();
|
|
||||||
if (status != B_OK) {
|
|
||||||
delete bitmap;
|
|
||||||
bitmap = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bitmap;
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BBitmap*
|
||||||
|
SharedBitmap::_CreateBitmapFromBuffer(int32 size) const
|
||||||
|
{
|
||||||
|
BBitmap* bitmap = _LoadIconFromBuffer(fBuffer, fSize, size);
|
||||||
|
|
||||||
|
if (bitmap == NULL)
|
||||||
|
bitmap = _LoadBitmapFromBuffer(fBuffer, fSize);
|
||||||
|
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BBitmap*
|
BBitmap*
|
||||||
SharedBitmap::_CreateBitmapFromMimeType(int32 size) const
|
SharedBitmap::_CreateBitmapFromMimeType(int32 size) const
|
||||||
{
|
{
|
||||||
@@ -176,6 +183,50 @@ SharedBitmap::_CreateBitmapFromMimeType(int32 size) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BBitmap*
|
||||||
|
SharedBitmap::_LoadBitmapFromBuffer(const void* buffer, size_t size) const
|
||||||
|
{
|
||||||
|
BMemoryIO stream(buffer, size);
|
||||||
|
|
||||||
|
// Try to read as an archived bitmap.
|
||||||
|
BMessage archive;
|
||||||
|
status_t status = archive.Unflatten(&stream);
|
||||||
|
if (status != B_OK)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
BBitmap* bitmap = new BBitmap(&archive);
|
||||||
|
|
||||||
|
status = bitmap->InitCheck();
|
||||||
|
if (status != B_OK) {
|
||||||
|
delete bitmap;
|
||||||
|
bitmap = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BBitmap*
|
||||||
|
SharedBitmap::_LoadIconFromBuffer(const void* data, size_t dataSize,
|
||||||
|
int32 size) const
|
||||||
|
{
|
||||||
|
BBitmap* bitmap = new BBitmap(BRect(0, 0, size - 1, size - 1), 0,
|
||||||
|
B_RGBA32);
|
||||||
|
status_t status = bitmap->InitCheck();
|
||||||
|
if (status == B_OK) {
|
||||||
|
status = BIconUtils::GetVectorIcon(
|
||||||
|
reinterpret_cast<const uint8*>(data), dataSize, bitmap);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (status != B_OK) {
|
||||||
|
delete bitmap;
|
||||||
|
bitmap = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// #pragma mark - UserInfo
|
// #pragma mark - UserInfo
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
|
|
||||||
class BBitmap;
|
class BBitmap;
|
||||||
|
class BMallocIO;
|
||||||
|
|
||||||
|
|
||||||
class SharedBitmap : public BReferenceable {
|
class SharedBitmap : public BReferenceable {
|
||||||
@@ -30,16 +31,25 @@ public:
|
|||||||
SharedBitmap(BBitmap* bitmap);
|
SharedBitmap(BBitmap* bitmap);
|
||||||
SharedBitmap(int32 resourceID);
|
SharedBitmap(int32 resourceID);
|
||||||
SharedBitmap(const char* mimeType);
|
SharedBitmap(const char* mimeType);
|
||||||
|
SharedBitmap(const BMallocIO& data);
|
||||||
~SharedBitmap();
|
~SharedBitmap();
|
||||||
|
|
||||||
const BBitmap* Bitmap(Size which);
|
const BBitmap* Bitmap(Size which);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BBitmap* _CreateBitmapFromResource(int32 size) const;
|
BBitmap* _CreateBitmapFromResource(int32 size) const;
|
||||||
|
BBitmap* _CreateBitmapFromBuffer(int32 size) const;
|
||||||
BBitmap* _CreateBitmapFromMimeType(int32 size) const;
|
BBitmap* _CreateBitmapFromMimeType(int32 size) const;
|
||||||
|
|
||||||
|
BBitmap* _LoadBitmapFromBuffer(const void* buffer,
|
||||||
|
size_t dataSize) const;
|
||||||
|
BBitmap* _LoadIconFromBuffer(const void* buffer,
|
||||||
|
size_t dataSize, int32 size) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int32 fResourceID;
|
int32 fResourceID;
|
||||||
|
uint8* fBuffer;
|
||||||
|
size_t fSize;
|
||||||
BString fMimeType;
|
BString fMimeType;
|
||||||
BBitmap* fBitmap[3];
|
BBitmap* fBitmap[3];
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user