写完BMP文件后,图像被颠倒翻转 [英] After writing BMP file, image is flipped upside down

查看:2232
本文介绍了写完BMP文件后,图像被颠倒翻转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码:

f = fopen( _stringhelper.STR("%s.bmp", filename), "wb" );
if( !f ) {
    _core.Error( ERC_ASSET, "ncImageLoader::CreateImage - Couldn't create %s image.\n", filename );
    return false;
}

int w = width;
int h = height;
int i;

int filesize = 54 + 3 * w * h;

byte bmpfileheader[14] = {
    'B', 'M',
    0, 0, 0, 0,
    0, 0,
    0, 0,
    54, 0, 0, 0 };

byte bmpinfoheader[40] = { 40, 0, 0, 0,
    0, 0, 0, 0,
    0, 0, 0, 0,
    1, 0,
    24, 0};

byte bmppad[3] = { 0, 0, 0 };

bmpfileheader[2] = (byte)( filesize );
bmpfileheader[3] = (byte)( filesize >> 8 );
bmpfileheader[4] = (byte)( filesize >> 16 );
bmpfileheader[5] = (byte)( filesize >> 24 );

bmpinfoheader[4] = (byte)( w );
bmpinfoheader[5] = (byte)( w >> 8);
bmpinfoheader[6] = (byte)( w >> 16  );
bmpinfoheader[7] = (byte)( w >> 24);
bmpinfoheader[8] = (byte)( h );
bmpinfoheader[9] = (byte)( h >> 8 );
bmpinfoheader[10] = (byte)( h >> 16 );
bmpinfoheader[11] = (byte)( h >> 24 );

fwrite( bmpfileheader, 1, 14, f );
fwrite( bmpinfoheader, 1, 40, f );

for( i = 0; i < h; i++ ) {
    fwrite( data + ( w * (h - i - 1) * 3 ), 3, w, f );
    fwrite( bmppad, 1, ( 4 - ( w * 3 ) % 4 ) % 4, f );
}

fclose(f);

我正在使用 glReadPixels()来捕获显示数据。如果我去保存这个文件的文件夹并打开它 - 它很好,调色板很好,但是它是颠倒翻转的。我试图将for loop写入向后,但仍然没有......我没有得到它。

I am using glReadPixels() to capture the display data. If I go to the folder where I saved this file and open it - it's fine, color palette is good, but it is flipped upside down. I've tried to write the for loop "backwards" but still nothing.. I don't get it.

可能是什么问题?

推荐答案

位图颠倒存储,详情请见:

Bitmaps are stored "upside-down", see more about that here:

为什么颠簸存放?

您可以将高度设置为负值,以使其正确显示。 (-height)

You could set the height as a negative value to have it displayed correctly. (-height)

来自 MSDN网站,BITMAPINFOHEADER

From MSDN site, BITMAPINFOHEADER:


如果 biHeight 为负数,则位图是一个自上而下的DIB,左上角有
来源。

If biHeight is negative, the bitmap is a top-down DIB with the origin at the upper left corner.

这篇关于写完BMP文件后,图像被颠倒翻转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆