C++ Alpha混合水印bmp在图像中bmp在c++;
我在学校的任务是将水印bmp图像添加到其他bmp图像中。该任务称为alpha混合。我必须在特定坐标处插入水印,用户将在开始时通过程序参数设置该坐标,以及水印混合的alpha值。我几乎成功了,但我犯了一个小错误。代码如下:C++ Alpha混合水印bmp在图像中bmp在c++;,c++,bmp,alphablending,C++,Bmp,Alphablending,我在学校的任务是将水印bmp图像添加到其他bmp图像中。该任务称为alpha混合。我必须在特定坐标处插入水印,用户将在开始时通过程序参数设置该坐标,以及水印混合的alpha值。我几乎成功了,但我犯了一个小错误。代码如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #define BMP_SIGNATURE_0 0x42 #define BMP_SIGNATURE_1
#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\u签名\u 0 0x42
#定义BMP\u签名\u 1 0x4D
#定义BMP_深度24
#定义BMP\U HDR\U大小24
int isValidBmp(无符号字符*头)
{
if(头==NULL)
返回-1;
if((头[0]==BMP\u签名\u 0)和&(头[1]==BMP\u签名\u 1))
{
如果(标题[28]!=BMP\U深度)
返回-1;
其他的
返回0;
}
其他的
返回-1;
}
无符号长getBmpWidth(无符号字符*头)
{
无符号长宽;
if(头==NULL)
返回0;
宽度=((无符号长)头[21]问题在于位图点的二维数组上的循环。请将循环更改为双循环。另外:
- 您没有为像素范围使用正确的偏移
- 您需要将宽度乘以3来复制像素的所有3种组件颜色
- 您应该使用舍入的行长度来确保覆盖所有填充
- y从图片底部偏移,使用trueY从顶部偏移
- 每个错误都有一个不同的错误文本,以知道触发了哪个错误,这很有帮助;我把它作为一个练习,以产生更有用的文本
- 我投票支持你,因为我认为这是一个有趣的问题;我不得不深入维基百科页面寻找BMP文件,以得出最终答案
- 您在源代码上只使用了半个alpha,但要复制您在评论中共享的图像,您需要对图像和水印都使用半个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;
}
#包括
#包括
#包括
#包括
#定义BMP\u签名\u 0 0x42
#定义BMP\u签名\u 1 0x4D
#定义BMP_深度24
#定义BMP\U HDR\U大小24
int isValidBmp(无符号字符*头)
{
if(头==NULL)
返回-1;
if((头[0]==BMP\u签名\u 0)和&(头[1]==BMP\u签名\u 1))
{
如果(标题[28]!=BMP\U深度)
返回-1;
其他的
返回0;
}
其他的
返回-1;
}
无符号长getBmpWidth(无符号字符*头)
{
无符号长宽;
if(头==NULL)
返回0;
宽度=((无符号长)头(21))你想告诉我们你所得到的小错误还是秘密?为什么你在使用Maloc时把这个标记为C++?为什么不“MIN =新的无符号字符[fStay]?”和“Dele[[M]”而不是Field]?嗨,Blastfurnace,这里是“小错误”。:我仍然得到一个错误的结果。这是我的图像:这是水印:这是@TimeHorse:建议的解决方案的结果,而正确的结果应该是:请帮助!我忘记BMP是否存储列的行(x行,在y列上分层)或者行的列。我将检查您的结果,并在修改时通知您,但我确信这只是行与列的问题。好消息。我会等待。谢谢。顺便说一句,我没有注意到,尽管这并不能修复它,因为mOut已经包含mIn的副本,一旦它被复制,我们就不需要mIn。所以我将更改代码上图。我不得不移动很多雪,但我越来越近了。结果表明,你也没有使用正确的像素偏移。但它仍然不太正确。我还在调查。