alpha混合水印bmp在c ++中的图像bmp中

时间:2015-03-04 21:46:23

标签: 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         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!

1 个答案:

答案 0 :(得分:3)

问题出在你的二维位图点阵列上。将循环更改为双循环。也:

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

另请注意,要重现您在评论中共享的图像,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小于.5,你会得到奇怪的颜色伪影,因为算法可能会说红色但是保持蓝色和绿色正常使蓝色和绿色看起来比它们应该更亮。从技术上讲,当像素设置发生其他情况时,它应该通过向它们添加更多来影响其他两个像素,以弥补这种影响。