BitmapUtils.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2009-2010, Pier Luigi Fiorini. All rights reserved.
  3. * Distributed under the terms of the MIT License.
  4. *
  5. * Authors:
  6. * Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
  7. */
  8. #include <string.h>
  9. #include <Path.h>
  10. #include <IconUtils.h>
  11. #include "BitmapUtils.h"
  12. #define BEOS_ICON_ATTRIBUTE "BEOS:ICON"
  13. #define BEOS_MINI_ICON_ATTRIBUTE "BEOS:M:STD_ICON"
  14. #define BEOS_LARGE_ICON_ATTRIBUTE "BEOS:L:STD_ICON"
  15. BBitmap*
  16. ReadNodeIcon(const char* name, icon_size size, bool followSymlink)
  17. {
  18. BEntry entry(name, followSymlink);
  19. entry_ref ref;
  20. entry.GetRef(&ref);
  21. BNode node(BPath(&ref).Path());
  22. BBitmap* ret = new BBitmap(BRect(0, 0, (float)size - 1, (float)size - 1), B_RGBA32);
  23. if (BIconUtils::GetIcon(&node, BEOS_ICON_ATTRIBUTE, BEOS_MINI_ICON_ATTRIBUTE,
  24. BEOS_LARGE_ICON_ATTRIBUTE, size, ret) != B_OK) {
  25. delete ret;
  26. ret = NULL;
  27. }
  28. return ret;
  29. }
  30. BBitmap* IconFromResources(BResources* res, int32 num, icon_size size)
  31. {
  32. if (!res)
  33. return NULL;
  34. size_t nbytes = 0;
  35. type_code type = B_VECTOR_ICON_TYPE;
  36. color_space cspace = B_RGBA32;
  37. // Try to find a vector icon
  38. const void* data = res->LoadResource(type, num, &nbytes);
  39. if (data == NULL) {
  40. // Determine resource type from icon size
  41. switch (size) {
  42. case B_MINI_ICON:
  43. type = B_MINI_ICON_TYPE;
  44. break;
  45. case B_LARGE_ICON:
  46. type = B_LARGE_ICON_TYPE;
  47. break;
  48. default:
  49. return NULL;
  50. }
  51. // Get bitmap icon
  52. data = res->LoadResource(type, num, &nbytes);
  53. if (data == NULL)
  54. return NULL;
  55. cspace = B_CMAP8;
  56. }
  57. BBitmap* icon = new BBitmap(BRect(0, 0, (float)size - 1, (float)size - 1),
  58. cspace);
  59. if (icon->InitCheck() != B_OK)
  60. return NULL;
  61. switch (type) {
  62. case B_VECTOR_ICON_TYPE:
  63. if (BIconUtils::GetVectorIcon((const uint8*)data, nbytes, icon) != B_OK) {
  64. delete icon;
  65. return NULL;
  66. }
  67. break;
  68. default:
  69. icon->SetBits(data, size * size, 0, cspace);
  70. }
  71. return icon;
  72. }
  73. BBitmap*
  74. RescaleBitmap(const BBitmap* src, float width, float height)
  75. {
  76. width--; height--;
  77. if (!src || !src->IsValid())
  78. return NULL;
  79. BRect srcSize = src->Bounds();
  80. if (height < 0) {
  81. float srcProp = srcSize.Height() / srcSize.Width();
  82. height = width * (float)ceil(srcProp);
  83. }
  84. BBitmap* res = new BBitmap(BRect(0, 0, (float)width, (float)height),
  85. src->ColorSpace());
  86. int32 dx = (int32)((srcSize.Width() + 1) / (float)(width + 1));
  87. int32 dy = (int32)((srcSize.Height() + 1) / (float)(height + 1));
  88. uint8 bpp = (uint8)(src->BytesPerRow() / ceil(srcSize.Width()));
  89. int32 srcYOff = src->BytesPerRow();
  90. int32 dstYOff = res->BytesPerRow();
  91. void* dstData = res->Bits();
  92. void* srcData = src->Bits();
  93. for (int32 y = 0; y <= height; y++) {
  94. void* dstRow = (void*)((uintptr_t)dstData + (uint32)(y * dstYOff));
  95. void* srcRow = (void*)((uintptr_t)srcData + ((uint32)(y * dy)
  96. * srcYOff));
  97. for (int32 x = 0; x <= width; x++)
  98. memcpy((void*)((uintptr_t)dstRow + (x * bpp)), (void*)((uintptr_t)srcRow
  99. + ((uint32)(x * dx) * bpp)), bpp);
  100. }
  101. return res;
  102. }