如何使用Boost :: GIL垂直翻转图像

时间:2020-05-24 19:39:24

标签: c++ boost boost-gil

我正在尝试生成图像图标。基于一些限制,我需要按{r,g,b,a,r1,g1,b1,a1,...}的顺序获取int32_t像素值的一维数组。在这种情况下,行的顺序应与OpenGL UV相同(从下到上)。为此,我使用了以下经过验证的方法:

const int image_width = image.width();
const int image_height = image.height();
if (!(image_width && image_height)) { return 1; }

int preview_width, preview_height;
if (image_width > image_height) {
    preview_width = PREVIEW_SIZE;
    preview_height = PREVIEW_SIZE * (float)image_height / (float)image_width;
}
else {
    preview_width = PREVIEW_SIZE * (float)image_width / (float)image_height;
    preview_height = PREVIEW_SIZE;
}

bg::rgba8_image_t preview_square(preview_width, preview_height);
const bg::rgba8_view_t& ps_viewer = bg::view(preview_square);
bg::resize_view(bg::const_view(image), ps_viewer, bg::bilinear_sampler());

std::vector<int32_t> arr(preview_width * preview_height);
memcpy(&arr[0], &ps_viewer[0], sizeof(int32_t) * arr.size());

基于GIL中像素的存储顺序,我需要沿y轴翻转图像。请告诉我该如何实现?

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用翻转视图进行变换:

#include <boost/gil.hpp>
#include <boost/gil/algorithm.hpp>
#include <boost/gil/dynamic_step.hpp>
#include <boost/gil/extension/io/jpeg.hpp>
#include <boost/gil/extension/io/png.hpp>

namespace bg = boost::gil;

int main() {
    bg::rgba8_image_t v;
    bg::read_and_convert_image("/tmp/VIplm.jpg", v, bg::jpeg_tag{});

    using Pixel = bg::rgba8_pixel_t;
    std::vector<Pixel> vec(v.width() * v.height());

    auto dest = bg::interleaved_view(v.width(), v.height(), vec.data(), v.width() * sizeof(Pixel));
    bg::copy_and_convert_pixels(
        bg::flipped_left_right_view(bg::const_view(v)),
        dest);

    bg::write_view("/tmp/pixels.png", dest, bg::png_tag{});
}

png在这里:

enter image description here

我没有进行尺寸调整操作。