Bitmap.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #pragma once
  2. #ifndef BITMAP_H_
  3. #define BITMAP_H_
  4. #include "Color.h"
  5. #include <memory>
  6. #include <string>
  7. #include <functional>
  8. template<typename Pixel>
  9. struct Bitmap
  10. {
  11. long width, height;
  12. std::unique_ptr<Pixel[]> pixels;
  13. public:
  14. Bitmap(void) :
  15. width{ 0 },
  16. height{ 0 },
  17. pixels{ 0 }
  18. {
  19. }
  20. Bitmap(long width, long height) :
  21. width{ width }, height{ height },
  22. pixels{ std::make_unique<Pixel[]>(width * height) }
  23. {
  24. }
  25. Bitmap(Bitmap&&) = default;
  26. Bitmap& operator = (Bitmap&&) = default;
  27. ~Bitmap() = default;
  28. template<typename P = Pixel>
  29. auto createPng(const std::string& path) const -> typename std::enable_if<std::is_same<P, RGBColor>::value>::type;
  30. template<typename T>
  31. Bitmap<T> map(std::function<T(Pixel)> f) const {
  32. Bitmap<T> b{ width, height };
  33. for (::size_t i = 0; i < width * height; i++) {
  34. b.pixels[i] = f(pixels[i]);
  35. }
  36. return b;
  37. }
  38. Pixel& get(long x, long y)
  39. {
  40. return pixels[x + y * width];
  41. }
  42. const Pixel& get(long x, long y) const
  43. {
  44. return pixels[x + y * width];
  45. }
  46. void print(void)
  47. {
  48. for (size_t i = 0; i < width * height; i++) {
  49. printf("%03d ", int(pixels[i].r));
  50. }
  51. }
  52. };
  53. #endif // BITMAP_H_