C ++对位图的模糊效果正在起作用,但颜色会发生变化

时间:2017-11-09 18:38:50

标签: c++ bitmap

关于我之前的问题BitMap_blur efect,我成功地使位图模糊,但问题是模糊图片的颜色已经改变:

原始照片:https://ibb.co/eFHg8G  照片模糊:https://ibb.co/mQDShb

模糊algorytm的代码与我之前的问题相同:

for (xx = 0; xx < bitmapInfoHeader.biWidth; xx++)
{
    for (yy = 0; yy <bitmapInfoHeader.biHeight; yy++)
    {
        avgB = avgG = avgR = 0;
        Counter = 0;

        for (x = xx; x < bitmapInfoHeader.biWidth && x < xx + blurSize; x++)
        {


            for (y = yy; y < bitmapInfoHeader.biHeight && y < yy + blurSize; y++)
            {
                avgB += bitmapImage[x *3 + y*bitmapInfoHeader.biWidth * 3 + 0];     //bitmapimage[x][y];
                avgG += bitmapImage[x  *3 + y*bitmapInfoHeader.biWidth * 3 + 1];
                avgR += bitmapImage[x *3 + y*bitmapInfoHeader.biWidth * 3 + 2];
                Counter++;
            }
        }

        avgB = avgB / Counter;
        avgG = avgG / Counter;
        avgR = avgR / Counter;

        bitmapImage[xx * 3 + yy*bitmapInfoHeader.biWidth * 3 + 0] = avgB;
        bitmapImage[xx * 3 + yy*bitmapInfoHeader.biWidth * 3 + 1] = avgG;
        bitmapImage[xx * 3 + yy*bitmapInfoHeader.biWidth * 3 + 2] = avgR;
    }
}

那么我在这里做错了什么?

3 个答案:

答案 0 :(得分:2)

实际上看起来每行的大小被填充为4个字节的倍数。要获得每行的正确字节偏移,您需要替换

* bitmapInfoHeader.biWidth * 3

* (bitmapInfoHeader.biWidth * 3 + padding_bytes_count)

其中

padding_bytes_count =
(
    (
        bitmapFileHeader.bfSize - bitmapFileHeader.bfOffBits
        -
        bitmapInfoHeader.biWidth * bitmapInfoHeader.biHeight * 3
    )
    /
    bitmapInfoHeader.biHeight
);

对于您的老虎图像padding_bytes_count应为2。

答案 1 :(得分:1)

在这里,我创建了一个半便携式位图读取器/写入器。适用于Windows,Linux Mint,MacOS High Sierra。我没有测试其他平台..但它应该可以工作。

它有:

  • 可移植性
  • 加载24位位图。
  • 加载32位位图。
  • 写入24位位图。
  • 写入32位位图。
  • 在24位和32位位图之间进行转换。
  • 在32位和24位位图之间进行转换。

它没有:

  • 支持Alpha透明度。 Alpha透明度具有需要在标题中设置的特殊字段和标志。我不想写它们所以它不会支持它。

只有部分看起来不太便于携带的部分才是originalRequest ..

#pragma pack

现在你所要做的就是添加你的模糊算法..我试过了,但无法找出模糊部分..我最终移植了一个算法:http://blog.ivank.net/fastest-gaussian-blur.html

#include <iostream>
#include <fstream>

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif

typedef struct
{
    uint8_t r, g, b, a;
} rgb32;


#if !defined(_WIN32) && !defined(_WIN64)
    #pragma pack(2)
    typedef struct
    {
        uint16_t bfType;
        uint32_t bfSize;
        uint16_t bfReserved1;
        uint16_t bfReserved2;
        uint32_t bfOffBits;
    } BITMAPFILEHEADER;
    #pragma pack()


    #pragma pack(2)
    typedef struct
    {
        uint32_t biSize;
        int32_t biWidth;
        int32_t biHeight;
        uint16_t biPlanes;
        uint16_t biBitCount;
        uint32_t biCompression;
        uint32_t biSizeImage;
        int16_t biXPelsPerMeter;
        int16_t biYPelsPerMeter;
        uint32_t biClrUsed;
        uint32_t biClrImportant;
    } BITMAPINFOHEADER;
    #pragma pack()
#endif

#pragma pack(2)
typedef struct
{
    BITMAPFILEHEADER bfh;
    BITMAPINFOHEADER bih;
} BMPINFO;
#pragma pack()


class bitmap
{
private:
    BMPINFO bmpInfo;
    uint8_t* pixels;

public:
    bitmap(const char* path);
    ~bitmap();

    void save(const char* path, uint16_t bit_count = 24);

    rgb32* getPixel(uint32_t x, uint32_t y) const;
    void setPixel(rgb32* pixel, uint32_t x, uint32_t y);

    uint32_t getWidth() const;
    uint32_t getHeight() const;
    uint16_t bitCount() const;
};

bitmap::bitmap(const char* path) : bmpInfo(), pixels(nullptr)
{
    std::ifstream file(path, std::ios::in | std::ios::binary);

    if (file)
    {
        file.read(reinterpret_cast<char*>(&bmpInfo.bfh), sizeof(bmpInfo.bfh));

        if (bmpInfo.bfh.bfType != 0x4d42)
        {
            throw std::runtime_error("Invalid format. Only bitmaps are supported.");
        }

        file.read(reinterpret_cast<char*>(&bmpInfo.bih), sizeof(bmpInfo.bih));

        if (bmpInfo.bih.biCompression != 0)
        {
            std::cerr<<bmpInfo.bih.biCompression<<"\n";
            throw std::runtime_error("Invalid bitmap. Only uncompressed bitmaps are supported.");
        }

        if (bmpInfo.bih.biBitCount != 24 && bmpInfo.bih.biBitCount != 32)
        {
            throw std::runtime_error("Invalid bitmap. Only 24bit and 32bit bitmaps are supported.");
        }

        file.seekg(bmpInfo.bfh.bfOffBits, std::ios::beg);

        pixels = new uint8_t[bmpInfo.bfh.bfSize - bmpInfo.bfh.bfOffBits];
        file.read(reinterpret_cast<char*>(&pixels[0]), bmpInfo.bfh.bfSize - bmpInfo.bfh.bfOffBits);


        uint8_t* temp = new uint8_t[bmpInfo.bih.biWidth * bmpInfo.bih.biHeight * sizeof(rgb32)];

        uint8_t* in = pixels;
        rgb32* out = reinterpret_cast<rgb32*>(temp);
        int padding = bmpInfo.bih.biBitCount == 24 ? ((bmpInfo.bih.biSizeImage - bmpInfo.bih.biWidth * bmpInfo.bih.biHeight * 3) / bmpInfo.bih.biHeight) : 0;

        for (int i = 0; i < bmpInfo.bih.biHeight; ++i, in += padding)
        {
            for (int j = 0; j < bmpInfo.bih.biWidth; ++j)
            {

                out->b = *(in++);
                out->g = *(in++);
                out->r = *(in++);
                out->a = bmpInfo.bih.biBitCount == 32 ? *(in++) : 0xFF;
                ++out;
            }
        }

        delete[] pixels;
        pixels = temp;
    }
}

bitmap::~bitmap()
{
    delete[] pixels;
}

void bitmap::save(const char* path, uint16_t bit_count)
{
    std::ofstream file(path, std::ios::out | std::ios::binary);

    if (file)
    {
        bmpInfo.bih.biBitCount = bit_count;
        uint32_t size = ((bmpInfo.bih.biWidth * bmpInfo.bih.biBitCount + 31) / 32) * 4 * bmpInfo.bih.biHeight;
        bmpInfo.bfh.bfSize = bmpInfo.bfh.bfOffBits + size;

        file.write(reinterpret_cast<char*>(&bmpInfo.bfh), sizeof(bmpInfo.bfh));
        file.write(reinterpret_cast<char*>(&bmpInfo.bih), sizeof(bmpInfo.bih));
        file.seekp(bmpInfo.bfh.bfOffBits, std::ios::beg);

        uint8_t* out = NULL;
        rgb32* in = reinterpret_cast<rgb32*>(pixels);
        uint8_t* temp = out = new uint8_t[bmpInfo.bih.biWidth * bmpInfo.bih.biHeight * sizeof(rgb32)];
        int padding = bmpInfo.bih.biBitCount == 24 ? ((bmpInfo.bih.biSizeImage - bmpInfo.bih.biWidth * bmpInfo.bih.biHeight * 3) / bmpInfo.bih.biHeight) : 0;

        for (int i = 0; i < bmpInfo.bih.biHeight; ++i, out += padding)
        {
            for (int j = 0; j < bmpInfo.bih.biWidth; ++j)
            {
                *(out++) = in->b;
                *(out++) = in->g;
                *(out++) = in->r;

                if (bmpInfo.bih.biBitCount == 32)
                {
                    *(out++) = in->a;
                }
                ++in;
            }
        }

        file.write(reinterpret_cast<char*>(&temp[0]), size); //bmpInfo.bfh.bfSize - bmpInfo.bfh.bfOffBits
        delete[] temp;
    }
}

rgb32* bitmap::getPixel(uint32_t x, uint32_t y) const
{
    rgb32* temp = reinterpret_cast<rgb32*>(pixels);
    return &temp[(bmpInfo.bih.biHeight - 1 - y) * bmpInfo.bih.biWidth + x];
}

void bitmap::setPixel(rgb32* pixel, uint32_t x, uint32_t y)
{
    rgb32* temp = reinterpret_cast<rgb32*>(pixels);
    memcpy(&temp[(bmpInfo.bih.biHeight - 1 - y) * bmpInfo.bih.biWidth + x], pixel, sizeof(rgb32));
};

uint32_t bitmap::getWidth() const
{
    return bmpInfo.bih.biWidth;
}

uint32_t bitmap::getHeight() const
{
    return bmpInfo.bih.biHeight;
}

uint16_t bitmap::bitCount() const
{
    return bmpInfo.bih.biBitCount;
}

void apply_blur(int x, int y, bitmap* bmp, int blurRadius)
{
    double blurValue = 0.111;
    int r = 0;
    int g = 0 ;
    int b = 0;

    for (int k = y - blurRadius; k <= blurRadius; ++k)
    {
        for (int l = x - blurRadius; l <= blurRadius; ++l)
        {
            rgb32* pixel = bmp->getPixel(l, k);
            r += blurValue * pixel->r;
            g += blurValue * pixel->g;
            b += blurValue * pixel->b;
        }
    }

    rgb32 pixel = *bmp->getPixel(x, y);

    pixel.r = r;
    pixel.g = g;
    pixel.b = b;

    bmp->setPixel(&pixel, x, y);
}

int main(int argc, const char * argv[])
{
    bitmap bmp{"/Users/brandon/Desktop/tiger.bmp"};
    bmp.save("/Users/brandon/Desktop/blurred-tiger-24.bmp");
    bmp.save("/Users/brandon/Desktop/blurred-tiger-32.bmp", 32);
    return 0;
}

结果变为: enter image description here

答案 2 :(得分:0)

由于iam仅在24位图上应用模糊效果,我添加了填充内容并修改了我的第3和第4个循环:

    for (x = xx; x < bitmapInfoHeader.biWidth && x < xx + blurSize; **x+=3**)
    {

   for (y = yy; y < bitmapInfoHeader.biHeight && y < yy + blurSize; **y+=3**)

它有效!照片左边仍然有一条细线,但我认为这是一个读/写位图问题,我可以自己处理:)

模糊的照片:https://ibb.co/iGp9Cb和另一张模糊的照片:https://ibb.co/jFXUCb

谢谢你们的回答!它有很多帮助

相关问题