gpt4 book ai didi

c++ - 在 C++ 中图像 bmp 中的 Alpha 混合水印 bmp

转载 作者:搜寻专家 更新时间:2023-10-31 01:41:03 26 4
gpt4 key购买 nike

我在学校有一个任务,要将水印 bmp 图像添加到其他 bmp 图像中。该任务称为 alpha 混合。我必须在用户将在启动时通过程序参数设置的特定坐标插入水印,以及水印混合的 alpha 值。我几乎成功了,但我遇到了小错误。这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BMP_SIGNATURE_0 0x42
#define BMP_SIGNATURE_1 0x4D
#define BMP_DEPTH 24
#define BMP_HDR_SIZE 24

int isValidBmp(unsigned char *header)
{
if (header == NULL)
return -1;

if ((header[0] == BMP_SIGNATURE_0) && (header[1] == BMP_SIGNATURE_1))
{
if (header[28] != BMP_DEPTH)
return -1;
else
return 0;
}
else
return -1;
}

unsigned long getBmpWidth(unsigned char *header)
{
unsigned long width;

if (header == NULL)
return 0;

width = ((unsigned long)header[21] << 24) |
((unsigned long)header[20] << 16) |
((unsigned long)header[19] << 8) |
(unsigned long)header[18];

return width;
}

unsigned long getBmpHeight(unsigned char *header)
{
unsigned long height;

if (header == NULL)
return 0;

height = ((unsigned long)header[25] << 24) |
((unsigned long)header[24] << 16) |
((unsigned long)header[23] << 8) |
(unsigned long)header[22];

return height;
}

int main(int argc, char *argv[])
{
FILE *fIn, *fOut, *fWaterMark;
unsigned char *mIn, *mOut, *mWaterMark;
unsigned long fsize, zsize;
unsigned long fwidth, fheight, zwidth, zheight;;

fIn = fopen("D:\\Downloads\\image.bmp", "rb");
if (fIn == NULL)
{
printf("ERROR!\n\n");
return 1;
}

fseek(fIn, 0, SEEK_END);
fsize = ftell(fIn);

mIn = (unsigned char *)malloc(fsize);
fseek(fIn, 0, SEEK_SET);
fread(mIn, sizeof(unsigned char), fsize, fIn);
fclose(fIn);

if (isValidBmp(mIn) == -1)
{
printf("ERROR!\n\n");
free(mIn);
return 1;
}

fwidth = getBmpWidth(mIn);
fheight = getBmpHeight(mIn);

if ((fwidth == 0) || (fheight == 0))
{
printf("ERROR!\n\n");
free(mIn);
return 1;
}

fWaterMark = fopen("D:\\Downloads\\watermark.bmp", "rb");
if (fWaterMark == NULL)
{
free(mIn);
printf("ERROR!\n\n");
return 1;
}

fseek(fWaterMark, 0, SEEK_END);
zsize = ftell(fWaterMark);

mWaterMark = (unsigned char *)malloc(zsize);
fseek(fWaterMark, 0, SEEK_SET);
fread(mWaterMark, sizeof(unsigned char), zsize, fWaterMark);
fclose(fWaterMark);

if (isValidBmp(mWaterMark) == -1)
{
printf("ERROR!\n\n");
free(mIn);
free(mWaterMark);
return 1;
}

zwidth = getBmpWidth(mWaterMark);
zheight = getBmpHeight(mWaterMark);

if ((zwidth == 0) || (zheight == 0))
{
printf("ERROR!\n\n");
free(mIn);
free(mWaterMark);
return 1;
}

fOut = fopen("D:\\Downloads\\new_image.bmp", "wb");
if (fOut == NULL)
{
free(mIn);
free(mWaterMark);
printf("ERROR!\n\n");
return 1;
}

mOut = (unsigned char *)malloc(fsize);
fseek(fOut, 0, SEEK_SET);

double alpha = 0.5;

memcpy(mOut, mIn, fsize);
unsigned int index = BMP_HDR_SIZE;
unsigned int x = 200, y = 200;
for (unsigned int i = BMP_HDR_SIZE + x*y; i < x*y + zsize; i++)
{
unsigned char v = ((1 - alpha) * mIn[i]) + mWaterMark[index++];
mOut[i] = v;
}
fwrite(mOut, sizeof(unsigned char), fsize, fOut);

free(mIn);
free(mOut);
fclose(fOut);

return 0;
}

示例 BMP 图片:

oh look it is actually a BMP!

最佳答案

问题出在您对二维位图点数组的循环中。将循环更改为双循环。还有:

  • 您没有为像素范围使用正确的偏移量。
  • 您需要将宽度乘以 3 才能复制一个像素的所有 3 个分量颜色
  • 您应该使用四舍五入的行长度来确保覆盖所有填充。
  • 您的 y 从图片底部偏移,使用 trueY 使您从顶部偏移。
  • 为每个错误设置不同的错误文本有助于了解触发了哪个错误;我把它留作练习,以得出更有用的文本。
  • 我投了你一票,因为我认为这是一个有趣的问题;我不得不深入研究 BMP 文件的维基百科页面以得出最终答案。
  • 您只对源代码使用了 half-alpha,但要重现您在评论中分享的图片,您需要对图片和水印进行 half-alpha。

另请注意,要重现您在评论中分享的图片,x 必须为 125,y 必须为 100。总而言之,这段代码看起来可以正常工作:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>

#define BMP_SIGNATURE_0 0x42
#define BMP_SIGNATURE_1 0x4D
#define BMP_DEPTH 24
#define BMP_HDR_SIZE 24

int isValidBmp(unsigned char *header)
{
if (header == NULL)
return -1;

if ((header[0] == BMP_SIGNATURE_0) && (header[1] == BMP_SIGNATURE_1))
{
if (header[28] != BMP_DEPTH)
return -1;
else
return 0;
}
else
return -1;
}

unsigned long getBmpWidth(unsigned char *header)
{
unsigned long width;

if (header == NULL)
return 0;

width = ((unsigned long)header[21] << 24) |
((unsigned long)header[20] << 16) |
((unsigned long)header[19] << 8) |
(unsigned long)header[18];

return width;
}

unsigned long getBmpHeight(unsigned char *header)
{
unsigned long height;

if (header == NULL)
return 0;

height = ((unsigned long)header[25] << 24) |
((unsigned long)header[24] << 16) |
((unsigned long)header[23] << 8) |
(unsigned long)header[22];

return height;
}

unsigned long getPixelOffset(unsigned char *header)
{
unsigned long offset;

if (header == NULL)
return 0;

offset = ((unsigned long)header[13] << 24) |
((unsigned long)header[12] << 16) |
((unsigned long)header[11] << 8) |
(unsigned long)header[10];

return offset;
}

int main(int argc, char *argv[])
{
FILE *fIn, *fOut, *fWaterMark;
unsigned char *mIn, *mOut, *mWaterMark;
unsigned long fsize, zsize;
unsigned long fwidth, fheight, zwidth, zheight;
unsigned long foffset, frow, zoffset, zrow;

fIn = fopen("srcso.bmp", "rb");
if (fIn == NULL)
{
printf("ERROR 1!\n\n");
return 1;
}

fseek(fIn, 0, SEEK_END);
fsize = ftell(fIn);

mIn = (unsigned char *)malloc(fsize);
fseek(fIn, 0, SEEK_SET);
fread(mIn, sizeof(unsigned char), fsize, fIn);
fclose(fIn);

if (isValidBmp(mIn) == -1)
{
printf("ERROR 2!\n\n");
free(mIn);
return 1;
}

fwidth = getBmpWidth(mIn);
fheight = getBmpHeight(mIn);
foffset = getPixelOffset(mIn);
frow = (BMP_DEPTH * fwidth + 31) / 32 * 4;

if ((fwidth == 0) || (fheight == 0))
{
printf("ERROR 3!\n\n");
free(mIn);
return 1;
}

fWaterMark = fopen("wmso.bmp", "rb");
if (fWaterMark == NULL)
{
free(mIn);
printf("ERROR 4!\n\n");
return 1;
}

fseek(fWaterMark, 0, SEEK_END);
zsize = ftell(fWaterMark);

mWaterMark = (unsigned char *)malloc(zsize);
fseek(fWaterMark, 0, SEEK_SET);
fread(mWaterMark, sizeof(unsigned char), zsize, fWaterMark);
fclose(fWaterMark);

if (isValidBmp(mWaterMark) == -1)
{
printf("ERROR 5!\n\n");
free(mIn);
free(mWaterMark);
return 1;
}

zwidth = getBmpWidth(mWaterMark);
zheight = getBmpHeight(mWaterMark);
zoffset = getPixelOffset(mWaterMark);
zrow = (BMP_DEPTH * zwidth + 31) / 32 * 4;

if ((zwidth == 0) || (zheight == 0))
{
printf("ERROR 6!\n\n");
free(mIn);
free(mWaterMark);
return 1;
}

fOut = fopen("new_image.bmp", "wb");
if (fOut == NULL)
{
free(mIn);
free(mWaterMark);
printf("ERROR 7!\n\n");
return 1;
}

mOut = (unsigned char *)malloc(fsize);
fseek(fOut, 0, SEEK_SET);

double alpha = 0.5;

std::copy(mIn, mIn + fsize, mOut);
::free(mIn);
mIn = 0;
unsigned int index = BMP_HDR_SIZE;
unsigned int x = 200, y = 200;
unsigned int trueY = fheight - y - zheight;
for (unsigned int j = 0; j < zheight; ++j) {
for (unsigned int i = 0; i < zwidth*3; ++i) {
const size_t offset = foffset + (j + trueY) * frow + i + x*3;
unsigned char * const offOut = mOut + offset;
unsigned char * const offWM = mWaterMark + zoffset + j * zrow + i;
*offOut *= 1 - alpha;
*offWM *= 1 - alpha;
if ((unsigned int)*offOut + (unsigned int)*offWM < 265)
*offOut += *offWM;
else
*offOut = 255;
}
}
fwrite(mOut, sizeof(unsigned char), fsize, fOut);
fclose(fOut);

::free(mWaterMark);
::free(mOut);

return 0;
}

请注意,如果您的 alpha 小于 0.5,您会得到奇怪的颜色伪影,因为该算法可以 ping 说红色,但让蓝色和绿色保持正常,使蓝色和绿色看起来比它们应该的更亮。从技术上讲,当像素设置发生 else 情况时,它应该通过向其他两个像素添加更多像素来补偿这种影响,从而真正影响其他两个像素。

关于c++ - 在 C++ 中图像 bmp 中的 Alpha 混合水印 bmp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28865650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com