Skip to content
Repos

DevPlaceApp

C++ 1.2 KB34 lines Opens in site

Directory Raw Download

#pragma once
// Spec: SPEC.md UX-IMG-01. CPU bitmap: straight (non-premultiplied) ARGB8888, one uint32 per pixel.
#include <cstdint>
#include <memory>
#include <vector>

namespace dp::ui {

class Bitmap {
 public:
  Bitmap(int w, int h) : w_(w), h_(h), px_(static_cast<std::size_t>(w) * static_cast<std::size_t>(h), 0) {}

  int width() const { return w_; }
  int height() const { return h_; }
  std::uint32_t* data() { return px_.data(); }
  const std::uint32_t* data() const { return px_.data(); }
  std::size_t bytes() const { return px_.size() * 4; }
  std::uint32_t at(int x, int y) const { return px_[static_cast<std::size_t>(y) * static_cast<std::size_t>(w_) + static_cast<std::size_t>(x)]; }

  // Decode PNG/JPEG/GIF/BMP (stb_image) or SVG (nanosvg) bytes. nullptr when undecodable or over caps.
  // svgTargetPx: raster size (longest side) for vector sources.
  static std::shared_ptr<Bitmap> decode(const std::string& bytes, int svgTargetPx);
  static constexpr int kMaxDimension = 8192;
  static constexpr std::size_t kMaxBytes = 64u * 1024u * 1024u;

 private:
  int w_;
  int h_;
  std::vector<std::uint32_t> px_;
};

using BitmapPtr = std::shared_ptr<Bitmap>;

}  // namespace dp::ui
179.4ms