这可能是一个很长的帖子,但我真的需要知道如何在24位和32位位图之间进行转换。为了这篇文章的篇幅,我删除了我的问题的PNG部分。
这里是:
我有一个类似下面的结构,它包含所有像素信息:
typedef union RGB
{
uint32_t Color;
struct
{
unsigned char B, G, R, A;
} RGBA;
} *PRGB;
std::vector<RGB> Pixels; //Holds all pixels.
除了24到32之外,所有位图写入都有效,反之亦然。我不知道我做错了什么或为什么24-32转换不起作用。我的位图读写代码如下:
Bitmap(const void* Pointer, int Width, int Height, uint32_t BitsPerPixel) //Constructor initialization here...
{
Pixels.clear();
if (Pointer == nullptr) {throw std::logic_error("Null Pointer Exception. Pointer is NULL.");}
if (Width < 1 || Height < 1) {throw std::invalid_argument("Invalid Arguments. Width and Height cannot equal 0.");}
std::memset(&Info, 0, sizeof(BITMAPINFO));
size = ((width * BitsPerPixel + 31) / 32) * 4 * height;
Info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
Info.bmiHeader.biWidth = width;
Info.bmiHeader.biHeight = height;
Info.bmiHeader.biPlanes = 1;
Info.bmiHeader.biBitCount = BitsPerPixel;
Info.bmiHeader.biCompression = BI_RGB;
Info.bmiHeader.biSizeImage = size;
bFileHeader.bfType = 0x4D42;
bFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(Info.bmiHeader);
bFileHeader.bfSize = bFileHeader.bfOffBits + size;
const unsigned char* BuffPos = static_cast<const unsigned char*>(Pointer);
height = (height < 0 ? -height : height);
Pixels.resize(width * height);
for (int I = 0; I < height; I++)
{
for (int J = 0; J < width; J++)
{
Pixels[(height - 1 - I) * width + J].RGBA.B = *(BuffPos++);
Pixels[(height - 1 - I) * width + J].RGBA.G = *(BuffPos++);
Pixels[(height - 1 - I) * width + J].RGBA.R = *(BuffPos++);
Pixels[(height - 1 - I) * width + J].RGBA.A = (Info.bmiHeader.biBitCount > 24 ? *(BuffPos++) : 0);
}
if(Info.bmiHeader.biBitCount == 24)
BuffPos += width % 4;
}
}
bool SaveBitmap(const char* FilePath)
{
std::vector<unsigned char> ImageData(size);
unsigned char* BuffPos = ImageData.data();
for (int I = 0; I < height; ++I)
{
for (int J = 0; J < width; ++J)
{
*(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.B;
*(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.G;
*(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.R;
if (Info.bmiHeader.biBitCount > 24)
*(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.A;
}
if(Info.bmiHeader.biBitCount == 24)
BuffPos += width % 4;
}
std::fstream hFile(FilePath, std::fstream::out | std::ofstream::binary);
if (!hFile.is_open()) return false;
hFile.write(reinterpret_cast<char*>(&bFileHeader), sizeof(BITMAPFILEHEADER));
hFile.write(reinterpret_cast<char*>(&Info.bmiHeader), sizeof (BITMAPINFOHEADER));
hFile.write(reinterpret_cast<char*>(&ImageData[0]), Size());
hFile.close();
return true;
}
知道这两个问题可能是什么?我想要它,如果我调用Bitmap(24BmpBuff,W,H,32);它将保存为32.如果我做Bitmap(32BmpBuff,W,H,24)它将保存为24位。我只是看不到它,所以我希望你们中的一个会。
我也尝试过制作帮助函数:
从24位转换为32位。
void T24To32(std::vector<RGB> &Input, std::vector<RGB> &Output, int Width, int Height)
{
Output.resize(Input.size());
for (int I = 0; I < Height; ++I)
{
for (int J = 0; J < Width; ++J)
{
Output[J].RGBA.B = Input[J].RGBA.B;
Output[J].RGBA.G = Input[J].RGBA.G;
Output[J].RGBA.R = Input[J].RGBA.R;
Output[J].RGBA.A = 0;
}
}
}
获取像素的unsigned char *并将它们倒置在结构中。
void Pack(int width, int height, int BPP, unsigned char* Input, std::vector<RGB> &Pixels)
{
unsigned char* BuffPos = Input;
height = (height < 0 ? -height : height);
Pixels.resize(width * height);
for (int I = 0; I < height; I++)
{
for (int J = 0; J < width; J++)
{
Pixels[(height - 1 - I) * width + J].RGBA.B = *(BuffPos++);
Pixels[(height - 1 - I) * width + J].RGBA.G = *(BuffPos++);
Pixels[(height - 1 - I) * width + J].RGBA.R = *(BuffPos++);
Pixels[(height - 1 - I) * width + J].RGBA.A = (BPP > 24 ? *(BuffPos++) : 0);
}
if(BPP == 24)
BuffPos += width % 4;
}
}
获取像素结构并将它们直立存储在unsigned char *。
中void Unpack(int width, int height, int BPP, std::vector<RGB> Pixels, unsigned char* &Output)
{
unsigned char* BuffPos = Output;
for (int I = 0; I < height; ++I)
{
for (int J = 0; J < width; ++J)
{
*(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.B;
*(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.G;
*(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.R;
if (BPP > 24)
*(BuffPos++) = Pixels[(height - 1 - I) * width + J].RGBA.A;
}
if(BPP == 24)
BuffPos += width % 4;
}
}
我使用上述所有内容。输入图像(32位):
代码:
void Bitmap32ToBitmap24(int Width, int Height)
{
Bitmap Image("C:/Images/Bitmap32.bmp");
std::vector<unsigned char> Pixels(((Width * 32 + 31) / 32) * 4 * Height); //Array large enough to hold 32 bit bmp.
unsigned char* BuffPos = Pixels.data();
Unpack(Width, Height, 32, Image.Get(), BuffPos); //Fill the array of unsigned char with image pixels being upright
Bitmap BMP(Pixels.data(), Width, Height, 24); //Convert image to 24 bit bmp and save it.
BMP.Save("C:/Images/Output/Bitmap32ToBitmap24.png");
}
输出图像(24位): 24到32导致:
答案 0 :(得分:1)
在所有代码段中
if(Info.bmiHeader.biBitCount == 24)
BuffPos += width % 4;
或
if(BPP == 24)
BuffPos += width % 4;
发生。我假设这应该为每一行添加填充值。但它不是填充,而是每行的像素数%4。
正确的添加值为(4 - ((width * 3) % 4)) % 4
。宽度* 3是该行中的字节数。 %4
计算4字节填充的多个字节数,但要填充下一个更高的值,我们需要4
- 这个值。如果不需要填充偏移,则再次为4 - &gt; %4
以避免这种情况。
计算相同值的更快方法是(-width * 3) & 3
。请参阅wiki。