00001 /* 00002 * Copyright 2017 The Cartographer Authors 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef CARTOGRAPHER_IO_IMAGE_H_ 00018 #define CARTOGRAPHER_IO_IMAGE_H_ 00019 00020 #include <cstdint> 00021 #include <vector> 00022 00023 #include "cairo/cairo.h" 00024 #include "cartographer/common/port.h" 00025 #include "cartographer/io/color.h" 00026 #include "cartographer/io/file_writer.h" 00027 #include "cartographer/io/points_batch.h" 00028 00029 namespace cartographer { 00030 namespace io { 00031 00032 // The only cairo image format we use for Cartographer. 00033 constexpr cairo_format_t kCairoFormat = CAIRO_FORMAT_ARGB32; 00034 00035 // std::unique_ptr for Cairo surfaces. The surface is destroyed when the 00036 // std::unique_ptr is reset or destroyed. 00037 using UniqueCairoSurfacePtr = 00038 std::unique_ptr<cairo_surface_t, void (*)(cairo_surface_t*)>; 00039 00040 // Takes ownership. 00041 UniqueCairoSurfacePtr MakeUniqueCairoSurfacePtr(cairo_surface_t* surface); 00042 00043 // std::unique_ptr for Cairo contexts. 00044 using UniqueCairoPtr = std::unique_ptr<cairo_t, void (*)(cairo_t*)>; 00045 00046 // Takes ownership. 00047 UniqueCairoPtr MakeUniqueCairoPtr(cairo_t* surface); 00048 00049 class Image { 00050 public: 00051 explicit Image(UniqueCairoSurfacePtr surface); 00052 Image(int width, int height); 00053 00054 const Uint8Color GetPixel(int x, int y) const; 00055 void SetPixel(int x, int y, const Uint8Color& color); 00056 void WritePng(FileWriter* const file_writer); 00057 00058 // Rotates the image in place. 00059 void Rotate90DegreesClockwise(); 00060 00061 // Returns a pointer to a cairo surface that contains the current pixel data. 00062 // The 'Image' object must therefore outlive the returned surface object. It 00063 // is undefined behavior to call any of the mutating functions while a pointer 00064 // to this surface is alive. 00065 UniqueCairoSurfacePtr GetCairoSurface(); 00066 00067 int width() const { return width_; } 00068 int height() const { return height_; } 00069 00070 private: 00071 int width_; 00072 int height_; 00073 std::vector<uint32> pixels_; 00074 }; 00075 00076 } // namespace io 00077 } // namespace cartographer 00078 00079 #endif // CARTOGRAPHER_IO_IMAGE_H_