游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2309|回复: 7

求助,,,24位位图载入错误.....

[复制链接]

2

主题

9

帖子

9

积分

新手上路

Rank: 1

积分
9
发表于 2010-5-23 12:02:00 | 显示全部楼层 |阅读模式
我写了一个24位位图载入程序...
可是对于从网上下载的大多数jpeg格式,用windows自带的画图工具转成24位bmp格式后,有的能正常显示,可是有的图片却无法正常显示...
不能正常显示的图片如下所有的不能正确显示的图片都是这样)
  显示错位了...这怎没回事呢...
位图数据抽象表示为:
程序从左到右一行一行的读图片数据.......

a  xxxxxxxxxxxxxxxx
b  xxxxxxxxxxxxxxxx
c  xxxxxxxxxxxxxxxx
d  xxxxxxxxxxxxxxxx

存放数据
图片第一行开始为 a
第二行开始为     b
第三行开始为     c
第四行开始为     d


可是按照错误的显示方式推出:图片是这样存放的,程序仍旧是从左到右
一行一行的读数据...

a  xxxxxxxxxxxxxxxx
   xxxxxxxxxxxxxxxx b
c  xxxxxxxxxxxxxxxx
   xxxxxxxxxxxxxxxx d

存放数据
图片第一行开始为 a
第二行开始为     b
第三行开始为     c
第四行开始为     d

.. 我把正确的和错误的图片bitmapinfoheader等信息都列出来看了,,,可是每个字段
没有什么不一样的...为什么呢....
而用windows画图工具和photosho制作的24位图都会出现以上错误这是怎么回事呢....


2

主题

9

帖子

9

积分

新手上路

Rank: 1

积分
9
 楼主| 发表于 2010-5-23 12:04:00 | 显示全部楼层

Re:求助,,,24位位图载入错误.....

不能上传我的错误图片...怎么回事

2

主题

9

帖子

9

积分

新手上路

Rank: 1

积分
9
 楼主| 发表于 2010-5-23 12:09:00 | 显示全部楼层

Re:求助,,,24位位图载入错误.....

图片错误描述:
  一张矩形图片:从对角线割开成为两个三角形,然后把右边的三角形一道左边,左边的移动到右边组成新的矩形.

2

主题

9

帖子

9

积分

新手上路

Rank: 1

积分
9
 楼主| 发表于 2010-5-23 12:25:00 | 显示全部楼层

Re:求助,,,24位位图载入错误.....

        //bit 24-bit
        if (bitmap->bitmapinfoheader.biBitCount==24 && screen_bpp==32)
        {
                int image_width    = bitmap->bitmapinfoheader.biWidth,
                        image_height   = bitmap->bitmapinfoheader.biHeight;
               
               
                UINT *Src          = (UINT*)ddsd.lpSurface;
                UCHAR  *buffer     = bitmap->buffer;
                int mempitch       = ddsd.lPitch>>2;
               
                for (int index_y=0;index_y<image_height;index_y++)
                {
                        for (int index_x=0;index_x<image_width;index_x++)
                        {  
                                //load the RGB data
                                UCHAR blue  = buffer[index_y*image_width*3 + index_x*3 + 0],
                                        green = buffer[index_y*image_width*3 + index_x*3 + 1],
                                        red   = buffer[index_y*image_width*3 + index_x*3 + 2];
                                //format the RGB data for screen
                                UINT color = _RGB32BIT8888(0,red,green,blue);
                               
                                Src[index_x+index_y*mempitch] = color;
                        }//end for index_x
                }//end for index_y
               
        }//end if 24-bit screen_bpp=32
    hRet = lpdds->Unlock(NULL);
       
        //return success
        return hRet;

2

主题

9

帖子

9

积分

新手上路

Rank: 1

积分
9
 楼主| 发表于 2010-5-23 12:28:00 | 显示全部楼层

楼上是把位图数据读入Directdraw显示表面的代码,本楼的是

        if(bitmap->bitmapinfoheader.biBitCount==24)
                _lseek(file_handle,-(int)(Size_Image_data),SEEK_END);


        if (bitmap->bitmapinfoheader.biBitCount == 8  ||
                bitmap->bitmapinfoheader.biBitCount == 16 ||
                bitmap->bitmapinfoheader.biBitCount == 24 )
        {
                //delete the last image if there was one
                if(bitmap->buffer)
                        free(bitmap->buffer);
               
                //allocate the memory for the buffer to store image
                if(!(bitmap->buffer=(UCHAR*)malloc(Size_Image_data)))
                {
                        //close the file
                        _close(file_handle);
                       
                        //return error
                        return DDraw_ReturnError("DDraw_LoadBitMapFile","malloc");
                       
                }//end if malloc
               
                //now read it in
                _lread(file_handle,bitmap->buffer,Size_Image_data);
               
        }//end read image
        else  // the bitmap bibitcount not supply
        {
                return DDraw_ReturnError("DDraw_LoadBitMapFile","_read(bitmap->buffer)");
               
        }// end else read image
       
       
        // all info read ,close the file
        _close(file_handle);
       
        //test and flip the bitmap
          
        if (bitmap->bitmapinfoheader.biHeight > 0)
        {
                DDraw_FlipBitMap(bitmap);
               
        }//end biHeight > 0
       
        //return success
        return 1;


1

主题

266

帖子

280

积分

中级会员

Rank: 3Rank: 3

积分
280
发表于 2010-5-25 00:57:00 | 显示全部楼层

Re:求助,,,24位位图载入错误.....

bitmapinfoheader.biHeight的值有可能是负值,你考虑到了么

2

主题

9

帖子

9

积分

新手上路

Rank: 1

积分
9
 楼主| 发表于 2010-5-25 14:02:00 | 显示全部楼层

Re:求助,,,24位位图载入错误.....

恩这个,遇到过..我考虑到了,,,,
正的
在载入图片的时候..就调用函数把它翻转过来了...
负的
就什么都不管

11

主题

1238

帖子

1782

积分

金牌会员

Rank: 6Rank: 6

积分
1782
发表于 2010-5-25 14:12:00 | 显示全部楼层

Re:求助,,,24位位图载入错误.....

位图的每一行都以一定字节对齐的
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-9 14:35

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表