Bitmap.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 T>
  29. Bitmap<T> map(std::function<T(Pixel)> f) const {
  30. Bitmap<T> b{ width, height };
  31. #pragma omp parallel for
  32. for (long i = 0; i < width * height; i++) {
  33. b.pixels[i] = f(pixels[i]);
  34. }
  35. return b;
  36. }
  37. Pixel& get(long x, long y)
  38. {
  39. return pixels[x + y * width];
  40. }
  41. const Pixel& get(long x, long y) const
  42. {
  43. return pixels[x + y * width];
  44. }
  45. void print(void)
  46. {
  47. for (size_t i = 0; i < width * height; i++) {
  48. printf("%03d ", int(pixels[i].r));
  49. }
  50. }
  51. };
  52. #endif // BITMAP_H_