image.cc
Go to the documentation of this file.
2 
3 #include <memory>
4 
6 #include "glog/logging.h"
7 
8 namespace cartographer {
9 namespace io {
10 namespace {
11 
12 uint32 Uint8ColorToCairo(const Uint8Color& color) {
13  return static_cast<uint32>(255) << 24 | static_cast<uint32>(color[0]) << 16 |
14  static_cast<uint32>(color[1]) << 8 | color[2];
15 }
16 
17 Uint8Color CairoToUint8Color(uint32 color) {
18  uint8 r = color >> 16;
19  uint8 g = color >> 8;
20  uint8 b = color;
21  return {{r, g, b}};
22 }
23 
24 cairo_status_t CairoWriteCallback(void* const closure,
25  const unsigned char* data,
26  const unsigned int length) {
27  if (static_cast<FileWriter*>(closure)->Write(
28  reinterpret_cast<const char*>(data), length)) {
29  return CAIRO_STATUS_SUCCESS;
30  }
31  return CAIRO_STATUS_WRITE_ERROR;
32 }
33 
34 void CheckStrideIsAsExpected(int width) {
35  const int stride = cairo_format_stride_for_width(kCairoFormat, width);
36  CHECK_EQ(stride, width * 4);
37 }
38 
39 } // namespace
40 
42  return UniqueCairoSurfacePtr(surface, cairo_surface_destroy);
43 }
44 
46  return UniqueCairoPtr(surface, cairo_destroy);
47 }
48 
49 Image::Image(int width, int height)
50  : width_(width), height_(height), pixels_(width * height, 0) {}
51 
53  : width_(cairo_image_surface_get_width(surface.get())),
54  height_(cairo_image_surface_get_height(surface.get())) {
55  CHECK_EQ(cairo_image_surface_get_format(surface.get()), kCairoFormat);
56  CheckStrideIsAsExpected(width_);
57 
58  const uint32* pixel_data =
59  reinterpret_cast<uint32*>(cairo_image_surface_get_data(surface.get()));
60  const int num_pixels = width_ * height_;
61  pixels_.reserve(num_pixels);
62  for (int i = 0; i < num_pixels; ++i) {
63  pixels_.push_back(pixel_data[i]);
64  }
65 }
66 
68  const auto old_pixels = pixels_;
69  pixels_.clear();
70  for (int x = 0; x < width_; ++x) {
71  for (int y = height_ - 1; y >= 0; --y) {
72  pixels_.push_back(old_pixels.at(y * width_ + x));
73  }
74  }
75  std::swap(width_, height_);
76 }
77 
78 void Image::WritePng(FileWriter* const file_writer) {
79  // TODO(hrapp): cairo_image_surface_create_for_data does not take ownership of
80  // the data until the surface is finalized. Once it is finalized though,
81  // cairo_surface_write_to_png fails, complaining that the surface is already
82  // finalized. This makes it pretty hard to pass back ownership of the image to
83  // the caller.
85  CHECK_EQ(cairo_surface_status(surface.get()), CAIRO_STATUS_SUCCESS);
86  CHECK_EQ(cairo_surface_write_to_png_stream(surface.get(), &CairoWriteCallback,
87  file_writer),
88  CAIRO_STATUS_SUCCESS);
89 }
90 
91 const Uint8Color Image::GetPixel(int x, int y) const {
92  return CairoToUint8Color(pixels_[y * width_ + x]);
93 }
94 
95 void Image::SetPixel(int x, int y, const Uint8Color& color) {
96  pixels_[y * width_ + x] = Uint8ColorToCairo(color);
97 }
98 
100  return MakeUniqueCairoSurfacePtr(cairo_image_surface_create_for_data(
101  reinterpret_cast<unsigned char*>(pixels_.data()), kCairoFormat, width_,
102  height_, width_ * 4 /* stride */));
103 }
104 
105 } // namespace io
106 } // namespace cartographer
UniqueCairoSurfacePtr GetCairoSurface()
Definition: image.cc:99
std::vector< uint32 > pixels_
Definition: image.h:73
UniqueCairoSurfacePtr MakeUniqueCairoSurfacePtr(cairo_surface_t *surface)
Definition: image.cc:41
uint32_t uint32
Definition: port.h:36
constexpr cairo_format_t kCairoFormat
Definition: image.h:33
const Uint8Color GetPixel(int x, int y) const
Definition: image.cc:91
void WritePng(FileWriter *const file_writer)
Definition: image.cc:78
std::unique_ptr< cairo_t, void(*)(cairo_t *)> UniqueCairoPtr
Definition: image.h:44
std::array< uint8, 3 > Uint8Color
Definition: color.h:28
Image(UniqueCairoSurfacePtr surface)
Definition: image.cc:52
UniqueCairoPtr MakeUniqueCairoPtr(cairo_t *surface)
Definition: image.cc:45
void Rotate90DegreesClockwise()
Definition: image.cc:67
void SetPixel(int x, int y, const Uint8Color &color)
Definition: image.cc:95
uint8_t uint8
Definition: port.h:34
std::unique_ptr< cairo_surface_t, void(*)(cairo_surface_t *)> UniqueCairoSurfacePtr
Definition: image.h:38


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58