6 #include "glog/logging.h" 13 return static_cast<uint32>(255) << 24 | static_cast<uint32>(color[0]) << 16 |
14 static_cast<uint32>(color[1]) << 8 | color[2];
18 uint8 r = color >> 16;
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;
31 return CAIRO_STATUS_WRITE_ERROR;
34 void CheckStrideIsAsExpected(
int width) {
35 const int stride = cairo_format_stride_for_width(
kCairoFormat, width);
36 CHECK_EQ(stride, width * 4);
50 : width_(width), height_(height), pixels_(width * height, 0) {}
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_);
59 reinterpret_cast<uint32*
>(cairo_image_surface_get_data(surface.get()));
62 for (
int i = 0; i < num_pixels; ++i) {
63 pixels_.push_back(pixel_data[i]);
68 const auto old_pixels =
pixels_;
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));
85 CHECK_EQ(cairo_surface_status(surface.get()), CAIRO_STATUS_SUCCESS);
86 CHECK_EQ(cairo_surface_write_to_png_stream(surface.get(), &CairoWriteCallback,
88 CAIRO_STATUS_SUCCESS);
UniqueCairoSurfacePtr GetCairoSurface()
std::vector< uint32 > pixels_
UniqueCairoSurfacePtr MakeUniqueCairoSurfacePtr(cairo_surface_t *surface)
constexpr cairo_format_t kCairoFormat
const Uint8Color GetPixel(int x, int y) const
void WritePng(FileWriter *const file_writer)
std::unique_ptr< cairo_t, void(*)(cairo_t *)> UniqueCairoPtr
std::array< uint8, 3 > Uint8Color
Image(UniqueCairoSurfacePtr surface)
UniqueCairoPtr MakeUniqueCairoPtr(cairo_t *surface)
void Rotate90DegreesClockwise()
void SetPixel(int x, int y, const Uint8Color &color)
std::unique_ptr< cairo_surface_t, void(*)(cairo_surface_t *)> UniqueCairoSurfacePtr