2010-05-07 04:47:10 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2009-2010, Pier Luigi Fiorini. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
|
|
|
|
*/
|
|
|
|
|
2010-05-10 16:54:28 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <Path.h>
|
|
|
|
|
2015-06-24 11:23:04 -05:00
|
|
|
#include <IconUtils.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
#include "BitmapUtils.h"
|
|
|
|
|
|
|
|
#define BEOS_ICON_ATTRIBUTE "BEOS:ICON"
|
|
|
|
#define BEOS_MINI_ICON_ATTRIBUTE "BEOS:M:STD_ICON"
|
|
|
|
#define BEOS_LARGE_ICON_ATTRIBUTE "BEOS:L:STD_ICON"
|
|
|
|
|
|
|
|
|
|
|
|
BBitmap*
|
|
|
|
ReadNodeIcon(const char* name, icon_size size, bool followSymlink)
|
|
|
|
{
|
|
|
|
BEntry entry(name, followSymlink);
|
2013-01-04 07:52:14 -06:00
|
|
|
entry_ref ref;
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
entry.GetRef(&ref);
|
|
|
|
|
|
|
|
BNode node(BPath(&ref).Path());
|
|
|
|
|
2010-05-20 13:11:32 -05:00
|
|
|
BBitmap* ret = new BBitmap(BRect(0, 0, (float)size - 1, (float)size - 1), B_RGBA32);
|
2010-05-07 04:47:10 -05:00
|
|
|
if (BIconUtils::GetIcon(&node, BEOS_ICON_ATTRIBUTE, BEOS_MINI_ICON_ATTRIBUTE,
|
|
|
|
BEOS_LARGE_ICON_ATTRIBUTE, size, ret) != B_OK) {
|
2013-01-04 07:52:14 -06:00
|
|
|
delete ret;
|
|
|
|
ret = NULL;
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BBitmap* IconFromResources(BResources* res, int32 num, icon_size size)
|
|
|
|
{
|
|
|
|
if (!res)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
size_t nbytes = 0;
|
|
|
|
type_code type = B_VECTOR_ICON_TYPE;
|
|
|
|
color_space cspace = B_RGBA32;
|
|
|
|
|
|
|
|
// Try to find a vector icon
|
|
|
|
const void* data = res->LoadResource(type, num, &nbytes);
|
|
|
|
if (data == NULL) {
|
|
|
|
// Determine resource type from icon size
|
|
|
|
switch (size) {
|
|
|
|
case B_MINI_ICON:
|
|
|
|
type = B_MINI_ICON_TYPE;
|
|
|
|
break;
|
|
|
|
case B_LARGE_ICON:
|
|
|
|
type = B_LARGE_ICON_TYPE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get bitmap icon
|
|
|
|
data = res->LoadResource(type, num, &nbytes);
|
|
|
|
if (data == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cspace = B_CMAP8;
|
|
|
|
}
|
|
|
|
|
2010-05-19 17:40:34 -05:00
|
|
|
BBitmap* icon = new BBitmap(BRect(0, 0, (float)size - 1, (float)size - 1),
|
|
|
|
cspace);
|
2010-05-07 04:47:10 -05:00
|
|
|
if (icon->InitCheck() != B_OK)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case B_VECTOR_ICON_TYPE:
|
|
|
|
if (BIconUtils::GetVectorIcon((const uint8*)data, nbytes, icon) != B_OK) {
|
|
|
|
delete icon;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
icon->SetBits(data, size * size, 0, cspace);
|
|
|
|
}
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
}
|
2010-05-08 11:21:36 -05:00
|
|
|
|
|
|
|
|
|
|
|
BBitmap*
|
2012-10-19 10:51:39 -05:00
|
|
|
RescaleBitmap(const BBitmap* src, float width, float height)
|
2010-05-08 11:21:36 -05:00
|
|
|
{
|
|
|
|
width--; height--;
|
|
|
|
|
|
|
|
if (!src || !src->IsValid())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
BRect srcSize = src->Bounds();
|
|
|
|
|
|
|
|
if (height < 0) {
|
|
|
|
float srcProp = srcSize.Height() / srcSize.Width();
|
2012-10-19 10:51:39 -05:00
|
|
|
height = width * (float)ceil(srcProp);
|
2010-05-08 11:21:36 -05:00
|
|
|
}
|
|
|
|
|
2010-05-19 17:40:34 -05:00
|
|
|
BBitmap* res = new BBitmap(BRect(0, 0, (float)width, (float)height),
|
|
|
|
src->ColorSpace());
|
2010-05-08 11:21:36 -05:00
|
|
|
|
2012-10-19 10:51:39 -05:00
|
|
|
int32 dx = (int32)((srcSize.Width() + 1) / (float)(width + 1));
|
|
|
|
int32 dy = (int32)((srcSize.Height() + 1) / (float)(height + 1));
|
2010-05-20 13:11:32 -05:00
|
|
|
uint8 bpp = (uint8)(src->BytesPerRow() / ceil(srcSize.Width()));
|
2010-05-08 11:21:36 -05:00
|
|
|
|
2012-10-19 10:51:39 -05:00
|
|
|
int32 srcYOff = src->BytesPerRow();
|
|
|
|
int32 dstYOff = res->BytesPerRow();
|
2010-05-08 11:21:36 -05:00
|
|
|
|
|
|
|
void* dstData = res->Bits();
|
|
|
|
void* srcData = src->Bits();
|
|
|
|
|
|
|
|
for (int32 y = 0; y <= height; y++) {
|
2010-05-19 17:40:34 -05:00
|
|
|
void* dstRow = (void*)((uint32)dstData + (uint32)(y * dstYOff));
|
2010-05-20 13:11:32 -05:00
|
|
|
void* srcRow = (void*)((uint32)srcData + ((uint32)(y * dy)
|
|
|
|
* srcYOff));
|
2010-05-08 11:21:36 -05:00
|
|
|
|
|
|
|
for (int32 x = 0; x <= width; x++)
|
2010-05-20 13:11:32 -05:00
|
|
|
memcpy((void*)((uint32)dstRow + (x * bpp)), (void*)((uint32)srcRow
|
2013-01-04 07:52:14 -06:00
|
|
|
+ ((uint32)(x * dx) * bpp)), bpp);
|
2010-05-08 11:21:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|