c - 在c中水平翻转BMP图像
问题描述
我的功能是获取图像,我正在尝试向右或向左翻转它。水平翻转。我试图做类似的事情,但不知道如何前进
图像的大小是 int height 和 int width ,函数知道像素值。
这是我的代码:
void flip_hrizntal_pixels(struct Pixel **pixels, int height, int width)
{
//Stuck here don't know how flip those pixels
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
//pixels[i][j].red =
//pixels[i][j].green =
//pixels[i][j].blue =
}
}
}
这是结构数据:
struct Pixel
{
unsigned char red;
unsigned char green;
unsigned char blue;
};
struct RGB_Image
{
long height;
long width;
long size;
struct Pixel **pixels;
};
我这样调用这个函数:
struct RGB_Image image;
int status = load_image(&image);
flip_hrizntal_pixels(image.pixels, image.height, image.width);
解决方案
想象一下,您的图片被排列为像素的行和列,每个像素具有 R、G 和 B。每行将具有“宽度”数量的像素,并且会有这样的“高度”数量的行。
因此,要在水平方向上翻转,即一行中最右边的像素转到最左边,反之亦然,然后是同一行中的第二个像素,它与倒数第二个像素交换,依此类推,代码将类似于这。(PS:这只是一个快速代码,让您了解如何继续。我还没有编译/运行我的代码)
希望这可以帮助
void flip_hrizntal_pixels(struct Pixel **pixels, int height, int width)
{
Pixel tempPixel;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width/2; j++)
{
//make a temp copy of the 'j-th' Pixel
tempPixel.red = pixels[i][j].red;
tempPixel.green = pixels[i][j].green;
tempPixel.blue = pixels[i][j].blue;
//copy the corresponding last Pixel to the j-th pixel
pixels[i][j].red = pixels[i][width-j].red;
pixels[i][j].green = pixels[i][width-j].green;
pixels[i][j].blue = pixels[i][width-j].blue;
//copy the temp copy that we made earlier of j-th Pixel to the corresponding last Pixel
pixels[i][width-j].red = tempPixel.red;
pixels[i][width-j].green = tempPixel.green;
pixels[i][width-j].blue = tempPixel.blue;
}
}
}
推荐阅读
- c# - 将具有长月份名称的字符串转换为 DateTime
- spring-boot - 在 camunda/zeebe 中处理用户任务
- flutter - 无法创建新的 Flutter Provider
- asp.net-mvc - 更新了 ASP.NET Web 应用程序 (.NET Framework) MVC5 项目模板
- r - 如何识别数据集按什么变量分组
- settings - 你能举一个类符号中的 NESTED TypedDict 的例子吗?
- solver - 诗歌装置 | SolverProblemError 因为 my_project 依赖的字符串 (*) 与任何版本都不匹配,所以版本求解失败
- android - Android Studio 中的 Gradle 依赖问题
- vue.js - 带有 github 页面的 vue 3
- http - 谷歌应用脚本没有收到来自 arduino 的 GET 请求