Bitmap.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Checked.h>
  7. #include <AK/LexicalPath.h>
  8. #include <AK/Memory.h>
  9. #include <AK/MemoryStream.h>
  10. #include <AK/Optional.h>
  11. #include <AK/ScopeGuard.h>
  12. #include <AK/String.h>
  13. #include <LibGfx/BMPLoader.h>
  14. #include <LibGfx/Bitmap.h>
  15. #include <LibGfx/DDSLoader.h>
  16. #include <LibGfx/GIFLoader.h>
  17. #include <LibGfx/ICOLoader.h>
  18. #include <LibGfx/JPGLoader.h>
  19. #include <LibGfx/PBMLoader.h>
  20. #include <LibGfx/PGMLoader.h>
  21. #include <LibGfx/PNGLoader.h>
  22. #include <LibGfx/PPMLoader.h>
  23. #include <LibGfx/ShareableBitmap.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <sys/mman.h>
  27. namespace Gfx {
  28. struct BackingStore {
  29. void* data { nullptr };
  30. size_t pitch { 0 };
  31. size_t size_in_bytes { 0 };
  32. };
  33. size_t Bitmap::minimum_pitch(size_t physical_width, BitmapFormat format)
  34. {
  35. size_t element_size;
  36. switch (determine_storage_format(format)) {
  37. case StorageFormat::Indexed8:
  38. element_size = 1;
  39. break;
  40. case StorageFormat::BGRx8888:
  41. case StorageFormat::BGRA8888:
  42. case StorageFormat::RGBA8888:
  43. element_size = 4;
  44. break;
  45. default:
  46. VERIFY_NOT_REACHED();
  47. }
  48. return physical_width * element_size;
  49. }
  50. static bool size_would_overflow(BitmapFormat format, const IntSize& size, int scale_factor)
  51. {
  52. if (size.width() < 0 || size.height() < 0)
  53. return true;
  54. // This check is a bit arbitrary, but should protect us from most shenanigans:
  55. if (size.width() >= 32768 || size.height() >= 32768 || scale_factor < 1 || scale_factor > 4)
  56. return true;
  57. // In contrast, this check is absolutely necessary:
  58. size_t pitch = Bitmap::minimum_pitch(size.width() * scale_factor, format);
  59. return Checked<size_t>::multiplication_would_overflow(pitch, size.height() * scale_factor);
  60. }
  61. RefPtr<Bitmap> Bitmap::create(BitmapFormat format, const IntSize& size, int scale_factor)
  62. {
  63. auto backing_store = Bitmap::allocate_backing_store(format, size, scale_factor, Purgeable::No);
  64. if (!backing_store.has_value())
  65. return nullptr;
  66. return adopt_ref(*new Bitmap(format, size, scale_factor, Purgeable::No, backing_store.value()));
  67. }
  68. RefPtr<Bitmap> Bitmap::create_purgeable(BitmapFormat format, const IntSize& size, int scale_factor)
  69. {
  70. auto backing_store = Bitmap::allocate_backing_store(format, size, scale_factor, Purgeable::Yes);
  71. if (!backing_store.has_value())
  72. return nullptr;
  73. return adopt_ref(*new Bitmap(format, size, scale_factor, Purgeable::Yes, backing_store.value()));
  74. }
  75. RefPtr<Bitmap> Bitmap::create_shareable(BitmapFormat format, const IntSize& size, int scale_factor)
  76. {
  77. if (size_would_overflow(format, size, scale_factor))
  78. return nullptr;
  79. const auto pitch = minimum_pitch(size.width() * scale_factor, format);
  80. const auto data_size = size_in_bytes(pitch, size.height() * scale_factor);
  81. auto buffer = Core::AnonymousBuffer::create_with_size(round_up_to_power_of_two(data_size, PAGE_SIZE));
  82. if (!buffer.is_valid())
  83. return nullptr;
  84. return Bitmap::create_with_anonymous_buffer(format, buffer, size, scale_factor, {});
  85. }
  86. Bitmap::Bitmap(BitmapFormat format, const IntSize& size, int scale_factor, Purgeable purgeable, const BackingStore& backing_store)
  87. : m_size(size)
  88. , m_scale(scale_factor)
  89. , m_data(backing_store.data)
  90. , m_pitch(backing_store.pitch)
  91. , m_format(format)
  92. , m_purgeable(purgeable == Purgeable::Yes)
  93. {
  94. VERIFY(!m_size.is_empty());
  95. VERIFY(!size_would_overflow(format, size, scale_factor));
  96. VERIFY(m_data);
  97. VERIFY(backing_store.size_in_bytes == size_in_bytes());
  98. allocate_palette_from_format(format, {});
  99. m_needs_munmap = true;
  100. }
  101. RefPtr<Bitmap> Bitmap::create_wrapper(BitmapFormat format, const IntSize& size, int scale_factor, size_t pitch, void* data)
  102. {
  103. if (size_would_overflow(format, size, scale_factor))
  104. return nullptr;
  105. return adopt_ref(*new Bitmap(format, size, scale_factor, pitch, data));
  106. }
  107. RefPtr<Bitmap> Bitmap::load_from_file(String const& path, int scale_factor)
  108. {
  109. if (scale_factor > 1 && path.starts_with("/res/")) {
  110. LexicalPath lexical_path { path };
  111. StringBuilder highdpi_icon_path;
  112. highdpi_icon_path.append(lexical_path.dirname());
  113. highdpi_icon_path.append('/');
  114. highdpi_icon_path.append(lexical_path.title());
  115. highdpi_icon_path.appendff("-{}x.", scale_factor);
  116. highdpi_icon_path.append(lexical_path.extension());
  117. RefPtr<Bitmap> bmp;
  118. #define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \
  119. if (path.ends_with(Ext, CaseSensitivity::CaseInsensitive)) \
  120. bmp = load_##Name(highdpi_icon_path.to_string());
  121. ENUMERATE_IMAGE_FORMATS
  122. #undef __ENUMERATE_IMAGE_FORMAT
  123. if (bmp) {
  124. VERIFY(bmp->width() % scale_factor == 0);
  125. VERIFY(bmp->height() % scale_factor == 0);
  126. bmp->m_size.set_width(bmp->width() / scale_factor);
  127. bmp->m_size.set_height(bmp->height() / scale_factor);
  128. bmp->m_scale = scale_factor;
  129. return bmp;
  130. }
  131. }
  132. #define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \
  133. if (path.ends_with(Ext, CaseSensitivity::CaseInsensitive)) \
  134. return load_##Name(path);
  135. ENUMERATE_IMAGE_FORMATS
  136. #undef __ENUMERATE_IMAGE_FORMAT
  137. return nullptr;
  138. }
  139. Bitmap::Bitmap(BitmapFormat format, const IntSize& size, int scale_factor, size_t pitch, void* data)
  140. : m_size(size)
  141. , m_scale(scale_factor)
  142. , m_data(data)
  143. , m_pitch(pitch)
  144. , m_format(format)
  145. {
  146. VERIFY(pitch >= minimum_pitch(size.width() * scale_factor, format));
  147. VERIFY(!size_would_overflow(format, size, scale_factor));
  148. // FIXME: assert that `data` is actually long enough!
  149. allocate_palette_from_format(format, {});
  150. }
  151. static bool check_size(const IntSize& size, int scale_factor, BitmapFormat format, unsigned actual_size)
  152. {
  153. // FIXME: Code duplication of size_in_bytes() and m_pitch
  154. unsigned expected_size_min = Bitmap::minimum_pitch(size.width() * scale_factor, format) * size.height() * scale_factor;
  155. unsigned expected_size_max = round_up_to_power_of_two(expected_size_min, PAGE_SIZE);
  156. if (expected_size_min > actual_size || actual_size > expected_size_max) {
  157. // Getting here is most likely an error.
  158. dbgln("Constructing a shared bitmap for format {} and size {} @ {}x, which demands {} bytes, which rounds up to at most {}.",
  159. static_cast<int>(format),
  160. size,
  161. scale_factor,
  162. expected_size_min,
  163. expected_size_max);
  164. dbgln("However, we were given {} bytes, which is outside this range?! Refusing cowardly.", actual_size);
  165. return false;
  166. }
  167. return true;
  168. }
  169. RefPtr<Bitmap> Bitmap::create_with_anonymous_buffer(BitmapFormat format, Core::AnonymousBuffer buffer, const IntSize& size, int scale_factor, const Vector<RGBA32>& palette)
  170. {
  171. if (size_would_overflow(format, size, scale_factor))
  172. return nullptr;
  173. return adopt_ref(*new Bitmap(format, buffer, size, scale_factor, palette));
  174. }
  175. /// Read a bitmap as described by:
  176. /// - actual size
  177. /// - width
  178. /// - height
  179. /// - scale_factor
  180. /// - format
  181. /// - palette count
  182. /// - palette data (= palette count * BGRA8888)
  183. /// - image data (= actual size * u8)
  184. RefPtr<Bitmap> Bitmap::create_from_serialized_byte_buffer(ByteBuffer&& buffer)
  185. {
  186. InputMemoryStream stream { buffer };
  187. unsigned actual_size;
  188. unsigned width;
  189. unsigned height;
  190. unsigned scale_factor;
  191. BitmapFormat format;
  192. unsigned palette_size;
  193. Vector<RGBA32> palette;
  194. auto read = [&]<typename T>(T& value) {
  195. if (stream.read({ &value, sizeof(T) }) != sizeof(T))
  196. return false;
  197. return true;
  198. };
  199. if (!read(actual_size) || !read(width) || !read(height) || !read(scale_factor) || !read(format) || !read(palette_size))
  200. return nullptr;
  201. if (format > BitmapFormat::BGRA8888 || format < BitmapFormat::Indexed1)
  202. return nullptr;
  203. if (!check_size({ width, height }, scale_factor, format, actual_size))
  204. return {};
  205. palette.ensure_capacity(palette_size);
  206. for (size_t i = 0; i < palette_size; ++i) {
  207. if (!read(palette[i]))
  208. return {};
  209. }
  210. if (stream.remaining() < actual_size)
  211. return {};
  212. auto data = stream.bytes().slice(stream.offset(), actual_size);
  213. auto bitmap = Bitmap::create(format, { width, height }, scale_factor);
  214. if (!bitmap)
  215. return {};
  216. bitmap->m_palette = new RGBA32[palette_size];
  217. memcpy(bitmap->m_palette, palette.data(), palette_size * sizeof(RGBA32));
  218. data.copy_to({ bitmap->scanline(0), bitmap->size_in_bytes() });
  219. return bitmap;
  220. }
  221. ByteBuffer Bitmap::serialize_to_byte_buffer() const
  222. {
  223. auto buffer = ByteBuffer::create_uninitialized(5 * sizeof(unsigned) + sizeof(BitmapFormat) + sizeof(RGBA32) * palette_size(m_format) + size_in_bytes());
  224. OutputMemoryStream stream { buffer };
  225. auto write = [&]<typename T>(T value) {
  226. if (stream.write({ &value, sizeof(T) }) != sizeof(T))
  227. return false;
  228. return true;
  229. };
  230. auto palette = palette_to_vector();
  231. if (!write(size_in_bytes()) || !write((unsigned)size().width()) || !write((unsigned)size().height()) || !write((unsigned)scale()) || !write(m_format) || !write((unsigned)palette.size()))
  232. return {};
  233. for (auto& p : palette) {
  234. if (!write(p))
  235. return {};
  236. }
  237. auto size = size_in_bytes();
  238. VERIFY(stream.remaining() == size);
  239. if (stream.write({ scanline(0), size }) != size)
  240. return {};
  241. return buffer;
  242. }
  243. Bitmap::Bitmap(BitmapFormat format, Core::AnonymousBuffer buffer, const IntSize& size, int scale_factor, const Vector<RGBA32>& palette)
  244. : m_size(size)
  245. , m_scale(scale_factor)
  246. , m_data(buffer.data<void>())
  247. , m_pitch(minimum_pitch(size.width() * scale_factor, format))
  248. , m_format(format)
  249. , m_purgeable(true)
  250. , m_buffer(buffer)
  251. {
  252. VERIFY(!is_indexed() || !palette.is_empty());
  253. VERIFY(!size_would_overflow(format, size, scale_factor));
  254. if (is_indexed(m_format))
  255. allocate_palette_from_format(m_format, palette);
  256. }
  257. RefPtr<Gfx::Bitmap> Bitmap::clone() const
  258. {
  259. RefPtr<Gfx::Bitmap> new_bitmap {};
  260. if (m_purgeable) {
  261. new_bitmap = Bitmap::create_purgeable(format(), size(), scale());
  262. } else {
  263. new_bitmap = Bitmap::create(format(), size(), scale());
  264. }
  265. if (!new_bitmap) {
  266. return nullptr;
  267. }
  268. VERIFY(size_in_bytes() == new_bitmap->size_in_bytes());
  269. memcpy(new_bitmap->scanline(0), scanline(0), size_in_bytes());
  270. return new_bitmap;
  271. }
  272. RefPtr<Gfx::Bitmap> Bitmap::rotated(Gfx::RotationDirection rotation_direction) const
  273. {
  274. auto new_bitmap = Gfx::Bitmap::create(this->format(), { height(), width() }, scale());
  275. if (!new_bitmap)
  276. return nullptr;
  277. auto w = this->physical_width();
  278. auto h = this->physical_height();
  279. for (int i = 0; i < w; i++) {
  280. for (int j = 0; j < h; j++) {
  281. Color color;
  282. if (rotation_direction == Gfx::RotationDirection::CounterClockwise)
  283. color = this->get_pixel(w - i - 1, j);
  284. else
  285. color = this->get_pixel(i, h - j - 1);
  286. new_bitmap->set_pixel(j, i, color);
  287. }
  288. }
  289. return new_bitmap;
  290. }
  291. RefPtr<Gfx::Bitmap> Bitmap::flipped(Gfx::Orientation orientation) const
  292. {
  293. auto new_bitmap = Gfx::Bitmap::create(this->format(), { width(), height() }, scale());
  294. if (!new_bitmap)
  295. return nullptr;
  296. auto w = this->physical_width();
  297. auto h = this->physical_height();
  298. for (int i = 0; i < w; i++) {
  299. for (int j = 0; j < h; j++) {
  300. Color color = this->get_pixel(i, j);
  301. if (orientation == Orientation::Vertical)
  302. new_bitmap->set_pixel(i, h - j - 1, color);
  303. else
  304. new_bitmap->set_pixel(w - i - 1, j, color);
  305. }
  306. }
  307. return new_bitmap;
  308. }
  309. RefPtr<Gfx::Bitmap> Bitmap::scaled(int sx, int sy) const
  310. {
  311. VERIFY(sx >= 0 && sy >= 0);
  312. if (sx == 1 && sy == 1)
  313. return this;
  314. auto new_bitmap = Gfx::Bitmap::create(format(), { width() * sx, height() * sy }, scale());
  315. if (!new_bitmap)
  316. return nullptr;
  317. auto old_width = physical_width();
  318. auto old_height = physical_height();
  319. for (int y = 0; y < old_height; y++) {
  320. for (int x = 0; x < old_width; x++) {
  321. auto color = get_pixel(x, y);
  322. auto base_x = x * sx;
  323. auto base_y = y * sy;
  324. for (int new_y = base_y; new_y < base_y + sy; new_y++) {
  325. for (int new_x = base_x; new_x < base_x + sx; new_x++) {
  326. new_bitmap->set_pixel(new_x, new_y, color);
  327. }
  328. }
  329. }
  330. }
  331. return new_bitmap;
  332. }
  333. // http://fourier.eng.hmc.edu/e161/lectures/resize/node3.html
  334. RefPtr<Gfx::Bitmap> Bitmap::scaled(float sx, float sy) const
  335. {
  336. VERIFY(sx >= 0.0f && sy >= 0.0f);
  337. if (floorf(sx) == sx && floorf(sy) == sy)
  338. return scaled(static_cast<int>(sx), static_cast<int>(sy));
  339. int scaled_width = (int)ceilf(sx * (float)width());
  340. int scaled_height = (int)ceilf(sy * (float)height());
  341. auto new_bitmap = Gfx::Bitmap::create(format(), { scaled_width, scaled_height }, scale());
  342. if (!new_bitmap)
  343. return nullptr;
  344. auto old_width = physical_width();
  345. auto old_height = physical_height();
  346. auto new_width = new_bitmap->physical_width();
  347. auto new_height = new_bitmap->physical_height();
  348. // The interpolation goes out of bounds on the bottom- and right-most edges.
  349. // We handle those in two specialized loops not only to make them faster, but
  350. // also to avoid four branch checks for every pixel.
  351. for (int y = 0; y < new_height - 1; y++) {
  352. for (int x = 0; x < new_width - 1; x++) {
  353. auto p = static_cast<float>(x) * static_cast<float>(old_width - 1) / static_cast<float>(new_width - 1);
  354. auto q = static_cast<float>(y) * static_cast<float>(old_height - 1) / static_cast<float>(new_height - 1);
  355. int i = floor(p);
  356. int j = floor(q);
  357. float u = p - static_cast<float>(i);
  358. float v = q - static_cast<float>(j);
  359. auto a = get_pixel(i, j);
  360. auto b = get_pixel(i + 1, j);
  361. auto c = get_pixel(i, j + 1);
  362. auto d = get_pixel(i + 1, j + 1);
  363. auto e = a.interpolate(b, u);
  364. auto f = c.interpolate(d, u);
  365. auto color = e.interpolate(f, v);
  366. new_bitmap->set_pixel(x, y, color);
  367. }
  368. }
  369. // Bottom strip (excluding last pixel)
  370. auto old_bottom_y = old_height - 1;
  371. auto new_bottom_y = new_height - 1;
  372. for (int x = 0; x < new_width - 1; x++) {
  373. auto p = static_cast<float>(x) * static_cast<float>(old_width - 1) / static_cast<float>(new_width - 1);
  374. int i = floor(p);
  375. float u = p - static_cast<float>(i);
  376. auto a = get_pixel(i, old_bottom_y);
  377. auto b = get_pixel(i + 1, old_bottom_y);
  378. auto color = a.interpolate(b, u);
  379. new_bitmap->set_pixel(x, new_bottom_y, color);
  380. }
  381. // Right strip (excluding last pixel)
  382. auto old_right_x = old_width - 1;
  383. auto new_right_x = new_width - 1;
  384. for (int y = 0; y < new_height - 1; y++) {
  385. auto q = static_cast<float>(y) * static_cast<float>(old_height - 1) / static_cast<float>(new_height - 1);
  386. int j = floor(q);
  387. float v = q - static_cast<float>(j);
  388. auto c = get_pixel(old_right_x, j);
  389. auto d = get_pixel(old_right_x, j + 1);
  390. auto color = c.interpolate(d, v);
  391. new_bitmap->set_pixel(new_right_x, y, color);
  392. }
  393. // Bottom-right pixel
  394. new_bitmap->set_pixel(new_width - 1, new_height - 1, get_pixel(physical_width() - 1, physical_height() - 1));
  395. return new_bitmap;
  396. }
  397. RefPtr<Gfx::Bitmap> Bitmap::cropped(Gfx::IntRect crop) const
  398. {
  399. auto new_bitmap = Gfx::Bitmap::create(format(), { crop.width(), crop.height() }, 1);
  400. if (!new_bitmap)
  401. return nullptr;
  402. for (int y = 0; y < crop.height(); ++y) {
  403. for (int x = 0; x < crop.width(); ++x) {
  404. int global_x = x + crop.left();
  405. int global_y = y + crop.top();
  406. if (global_x >= physical_width() || global_y >= physical_height() || global_x < 0 || global_y < 0) {
  407. new_bitmap->set_pixel(x, y, Gfx::Color::Black);
  408. } else {
  409. new_bitmap->set_pixel(x, y, get_pixel(global_x, global_y));
  410. }
  411. }
  412. }
  413. return new_bitmap;
  414. }
  415. RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_anonymous_buffer() const
  416. {
  417. if (m_buffer.is_valid())
  418. return *this;
  419. auto buffer = Core::AnonymousBuffer::create_with_size(round_up_to_power_of_two(size_in_bytes(), PAGE_SIZE));
  420. if (!buffer.is_valid())
  421. return nullptr;
  422. auto bitmap = Bitmap::create_with_anonymous_buffer(m_format, buffer, size(), scale(), palette_to_vector());
  423. if (!bitmap)
  424. return nullptr;
  425. memcpy(bitmap->scanline(0), scanline(0), size_in_bytes());
  426. return bitmap;
  427. }
  428. Bitmap::~Bitmap()
  429. {
  430. if (m_needs_munmap) {
  431. int rc = munmap(m_data, size_in_bytes());
  432. VERIFY(rc == 0);
  433. }
  434. m_data = nullptr;
  435. delete[] m_palette;
  436. }
  437. void Bitmap::set_mmap_name([[maybe_unused]] String const& name)
  438. {
  439. VERIFY(m_needs_munmap);
  440. #ifdef __serenity__
  441. ::set_mmap_name(m_data, size_in_bytes(), name.characters());
  442. #endif
  443. }
  444. void Bitmap::fill(Color color)
  445. {
  446. VERIFY(!is_indexed(m_format));
  447. for (int y = 0; y < physical_height(); ++y) {
  448. auto* scanline = this->scanline(y);
  449. fast_u32_fill(scanline, color.value(), physical_width());
  450. }
  451. }
  452. void Bitmap::set_volatile()
  453. {
  454. VERIFY(m_purgeable);
  455. if (m_volatile)
  456. return;
  457. #ifdef __serenity__
  458. int rc = madvise(m_data, size_in_bytes(), MADV_SET_VOLATILE);
  459. if (rc < 0) {
  460. perror("madvise(MADV_SET_VOLATILE)");
  461. VERIFY_NOT_REACHED();
  462. }
  463. #endif
  464. m_volatile = true;
  465. }
  466. [[nodiscard]] bool Bitmap::set_nonvolatile()
  467. {
  468. VERIFY(m_purgeable);
  469. if (!m_volatile)
  470. return true;
  471. #ifdef __serenity__
  472. int rc = madvise(m_data, size_in_bytes(), MADV_SET_NONVOLATILE);
  473. if (rc < 0) {
  474. perror("madvise(MADV_SET_NONVOLATILE)");
  475. VERIFY_NOT_REACHED();
  476. }
  477. #else
  478. int rc = 0;
  479. #endif
  480. m_volatile = false;
  481. return rc == 0;
  482. }
  483. ShareableBitmap Bitmap::to_shareable_bitmap() const
  484. {
  485. auto bitmap = to_bitmap_backed_by_anonymous_buffer();
  486. if (!bitmap)
  487. return {};
  488. return ShareableBitmap(*bitmap);
  489. }
  490. Optional<BackingStore> Bitmap::allocate_backing_store(BitmapFormat format, const IntSize& size, int scale_factor, [[maybe_unused]] Purgeable purgeable)
  491. {
  492. if (size_would_overflow(format, size, scale_factor))
  493. return {};
  494. const auto pitch = minimum_pitch(size.width() * scale_factor, format);
  495. const auto data_size_in_bytes = size_in_bytes(pitch, size.height() * scale_factor);
  496. int map_flags = MAP_ANONYMOUS | MAP_PRIVATE;
  497. if (purgeable == Purgeable::Yes)
  498. map_flags |= MAP_NORESERVE;
  499. #ifdef __serenity__
  500. void* data = mmap_with_name(nullptr, data_size_in_bytes, PROT_READ | PROT_WRITE, map_flags, 0, 0, String::formatted("GraphicsBitmap [{}]", size).characters());
  501. #else
  502. void* data = mmap(nullptr, data_size_in_bytes, PROT_READ | PROT_WRITE, map_flags, 0, 0);
  503. #endif
  504. if (data == MAP_FAILED) {
  505. perror("mmap");
  506. return {};
  507. }
  508. return { { data, pitch, data_size_in_bytes } };
  509. }
  510. void Bitmap::allocate_palette_from_format(BitmapFormat format, const Vector<RGBA32>& source_palette)
  511. {
  512. size_t size = palette_size(format);
  513. if (size == 0)
  514. return;
  515. m_palette = new RGBA32[size];
  516. if (!source_palette.is_empty()) {
  517. VERIFY(source_palette.size() == size);
  518. memcpy(m_palette, source_palette.data(), size * sizeof(RGBA32));
  519. }
  520. }
  521. Vector<RGBA32> Bitmap::palette_to_vector() const
  522. {
  523. Vector<RGBA32> vector;
  524. auto size = palette_size(m_format);
  525. vector.ensure_capacity(size);
  526. for (size_t i = 0; i < size; ++i)
  527. vector.unchecked_append(palette_color(i).value());
  528. return vector;
  529. }
  530. }