image.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CARTOGRAPHER_IO_IMAGE_H_
18 #define CARTOGRAPHER_IO_IMAGE_H_
19 
20 #include <cstdint>
21 #include <vector>
22 
23 #include "cairo/cairo.h"
25 #include "cartographer/io/color.h"
28 
29 namespace cartographer {
30 namespace io {
31 
32 // The only cairo image format we use for Cartographer.
33 constexpr cairo_format_t kCairoFormat = CAIRO_FORMAT_ARGB32;
34 
35 // std::unique_ptr for Cairo surfaces. The surface is destroyed when the
36 // std::unique_ptr is reset or destroyed.
38  std::unique_ptr<cairo_surface_t, void (*)(cairo_surface_t*)>;
39 
40 // Takes ownership.
41 UniqueCairoSurfacePtr MakeUniqueCairoSurfacePtr(cairo_surface_t* surface);
42 
43 // std::unique_ptr for Cairo contexts.
44 using UniqueCairoPtr = std::unique_ptr<cairo_t, void (*)(cairo_t*)>;
45 
46 // Takes ownership.
47 UniqueCairoPtr MakeUniqueCairoPtr(cairo_t* surface);
48 
49 class Image {
50  public:
51  explicit Image(UniqueCairoSurfacePtr surface);
52  Image(int width, int height);
53 
54  const Uint8Color GetPixel(int x, int y) const;
55  void SetPixel(int x, int y, const Uint8Color& color);
56  void WritePng(FileWriter* const file_writer);
57 
58  // Rotates the image in place.
60 
61  // Returns a pointer to a cairo surface that contains the current pixel data.
62  // The 'Image' object must therefore outlive the returned surface object. It
63  // is undefined behavior to call any of the mutating functions while a pointer
64  // to this surface is alive.
66 
67  int width() const { return width_; }
68  int height() const { return height_; }
69 
70  private:
71  int width_;
72  int height_;
73  std::vector<uint32> pixels_;
74 };
75 
76 } // namespace io
77 } // namespace cartographer
78 
79 #endif // CARTOGRAPHER_IO_IMAGE_H_
UniqueCairoSurfacePtr GetCairoSurface()
Definition: image.cc:99
int width() const
Definition: image.h:67
std::vector< uint32 > pixels_
Definition: image.h:73
UniqueCairoSurfacePtr MakeUniqueCairoSurfacePtr(cairo_surface_t *surface)
Definition: image.cc:41
int height() const
Definition: image.h:68
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
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