如何使用 Boost::GIL 垂直翻转图像
How to flip image vertically using Boost::GIL
我正在尝试生成图像图标。基于一些限制,我需要得到一个int32_t像素值的一维数组,顺序为{r, g, b, a, r1, g1, b1, a1, ...}。在这种情况下,行的顺序应该与 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轴翻转图像。请告诉我如何实现这个?
您可以使用翻转视图进行变换:
#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 在这里:
我省略了调整大小操作。
我正在尝试生成图像图标。基于一些限制,我需要得到一个int32_t像素值的一维数组,顺序为{r, g, b, a, r1, g1, b1, a1, ...}。在这种情况下,行的顺序应该与 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轴翻转图像。请告诉我如何实现这个?
您可以使用翻转视图进行变换:
#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 在这里:
我省略了调整大小操作。