I have a task at school to add watermark bmp image into some other bmp image. The task is called alpha blending. I have to insert watermark at specific coordinates which user will set through program parameters on start, as well as alpha value for watermark blending. I am almost succeed, but I am getting small error. Here is the code:
#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;
}
Thank you.
Aucun commentaire:
Enregistrer un commentaire