C++ 解码位图时获取不正确的宽度
我的源代码有一个错误,基本上导致位图图像显示得太宽。例如,它将打印宽度和高度,高度是完美的(256),宽度也应该是256,但程序说它有几十亿像素宽,每次都不同。这是源代码C++ 解码位图时获取不正确的宽度,c++,bitmap,C++,Bitmap,我的源代码有一个错误,基本上导致位图图像显示得太宽。例如,它将打印宽度和高度,高度是完美的(256),宽度也应该是256,但程序说它有几十亿像素宽,每次都不同。这是源代码 #include "glob.h" /* Image type - contains height, width, and data */ struct Image { unsigned long sizeX; unsigned long sizeY; char *data; }; typedef s
#include "glob.h"
/* Image type - contains height, width, and data */
struct Image {
unsigned long sizeX;
unsigned long sizeY;
char *data;
};
typedef struct Image Image;
int ImageLoad(char *filename, Image *image) {
FILE *file;
unsigned long size; // size of the image in bytes.
unsigned long i; // standard counter.
unsigned short int planes; // number of planes in image (must be 1)
unsigned short int bpp; // number of bits per pixel (must be 24)
char temp; // temporary color storage for bgr-rgb conversion.
// make sure the file is there.
if ((file = fopen(filename, "rb"))==NULL){
printf("bitmap Not Found : %s\n",filename);
return 0;
}
// seek through the bmp header, up to the width/height:
fseek(file, 18, SEEK_CUR);
// read the width
if ((i = fread(&image->sizeX, 4, 1, file)) != 1) {
printf("Error reading width from %s.\n", filename);
return 0;
}
printf("Width of %s: %lu\n", filename, image->sizeX);
// read the height
if ((i = fread(&image->sizeY, 4, 1, file)) != 1) {
printf("Error reading height from %s.\n", filename);
return 0;
}
printf("Height of %s: %lu\n", filename, image->sizeY);
// calculate the size (assuming 24 bits or 3 bytes per pixel).
size = image->sizeX * image->sizeY * 3;
// read the planes
if ((fread(&planes, 2, 1, file)) != 1) {
printf("Error reading planes from %s.\n", filename);
return 0;
}
if (planes != 1) {
printf("Planes from %s is not 1: %u\n", filename, planes);
return 0;
}
// read the bpp
if ((i = fread(&bpp, 2, 1, file)) != 1) {
printf("Error reading bpp from %s.\n", filename);
return 0;
}
if (bpp != 24) {
printf("Bpp from %s is not 24: %u\n", filename, bpp);
return 0;
}
// seek past the rest of the bitmap header.
fseek(file, 24, SEEK_CUR);
// read the data.
image->data = (char *) malloc(size);
if (image->data == NULL) {
printf("Error allocating memory for color-corrected image data\n");
return 0;
}
if ((i = fread(image->data, size, 1, file)) != 1) {
printf("Error reading image data from %s.\n", filename);
return 0;
}
for (i=0; i<size; i+=3) { // reverse all of the colors. (bgr -> rgb)
temp = image->data[i];
image->data[i] = image->data[i+2];
image->data[i+2] = temp;
}
// we're done.
return 0;
}
// Load Bitmaps And Convert To Textures
void glob::LoadGLTextures() {
// Load Texture
Image *image1;
// allocate space for texture
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("(image1 == NULL)\n");
exit(0);
}
ImageLoad("data/textures/NeHe.bmp", image1);
// Create Texture
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); // 2d texture (x and y size)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
// 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image,
// border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
};
#包括“glob.h”
/*图像类型-包含高度、宽度和数据*/
结构映像{
无符号长sizeX;
未签名的长码;
字符*数据;
};
typedef结构图像;
int ImageLoad(字符*文件名,图像*图像){
文件*文件;
无符号长大小;//图像的大小(字节)。
无符号长i;//标准计数器。
无符号短整数平面;//图像中的平面数(必须为1)
无符号短整数bpp;//每个像素的位数(必须为24)
char temp;//用于bgr rgb转换的临时颜色存储。
//确保文件在那里。
if((file=fopen(filename,“rb”))==NULL){
printf(“未找到位图:%s\n”,文件名);
返回0;
}
//通过bmp标题进行搜索,最大宽度/高度:
fseek(文件,18,SEEK_CUR);
//读取宽度
如果((i=fread(&image->sizeX,4,1,文件))!=1){
printf(“从%s读取宽度时出错。\n”,文件名);
返回0;
}
printf(“%s的宽度:%lu\n”,文件名,图像->sizeX);
//阅读高度
如果((i=fread(&image->sizeY,4,1,file))!=1){
printf(“从%s读取高度时出错。\n”,文件名);
返回0;
}
printf(“%s的高度:%lu\n”,文件名,图像->大小);
//计算大小(假设每像素24位或3字节)。
大小=图像->大小x*图像->大小x*3;
//读飞机
如果((fread(&平面,2,1,文件))!=1){
printf(“从%s读取平面时出错。\n”,文件名);
返回0;
}
如果(平面!=1){
printf(“来自%s的平面不是1:%u\n”,文件名,平面);
返回0;
}
//阅读bpp
如果((i=fread(&bpp,2,1,文件))!=1){
printf(“从%s读取bpp时出错。\n”,文件名);
返回0;
}
如果(bpp!=24){
printf(“来自%s的Bpp不是24:%u\n”,文件名,Bpp);
返回0;
}
//查找位图标题的其余部分。
fseek(文件,24,SEEK_CUR);
//读取数据。
图像->数据=(字符*)malloc(大小);
如果(图像->数据==NULL){
printf(“为经颜色校正的图像数据分配内存时出错\n”);
返回0;
}
如果((i=fread(图像->数据,大小,1,文件))!=1){
printf(“从%s读取图像数据时出错。\n”,文件名);
返回0;
}
对于(i=0;i rgb)
温度=图像->数据[i];
图像->数据[i]=图像->数据[i+2];
图像->数据[i+2]=温度;
}
//我们结束了。
返回0;
}
//加载位图并转换为纹理
void glob::LoadGLTextures(){
//加载纹理
图像*图像1;
//为纹理分配空间
image1=(图像*)malloc(sizeof(图像));
if(image1==NULL){
printf(“(image1==NULL)\n”);
出口(0);
}
ImageLoad(“数据/纹理/NeHe.bmp”,image1);
//创建纹理
glGenTextures(1,&纹理);
glBindTexture(GL_纹理_2D,纹理);//2D纹理(x和y大小)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//图像大于纹理时线性缩放
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);//图像小于纹理时线性缩放
//2d纹理、细节级别0(正常)、3个组件(红色、绿色、蓝色)、图像的x大小、图像的y大小、,
//边框0(正常)、rgb颜色数据、无符号字节数据,最后是数据本身。
GLTEXAGE2D(GL_纹理_2D,0,3,image1->sizeX,image1->sizeY,0,GL_RGB,GL_无符号字节,image1->数据);
};
全球h是这样的:
#ifndef GLOB_H_INCLUDED
#define GLOB_H_INCLUDED
#include <iostream>
#include <stdlib.h>
#include <stdio.h> // Header file for standard file i/o.
#include <GL/glx.h> /* this includes the necessary X headers */
#include <GL/gl.h>
//#include <GL/glut.h> // Header File For The GLUT Library
//#include <GL/glu.h> // Header File For The GLu32 Library
#include <X11/X.h> /* X11 constant (e.g. TrueColor) */
#include <X11/keysym.h>
class glob {
bool Running;
GLuint texture; //make an array when we start using more then 1
Display *dpy;
Window win;
XEvent event;
GLboolean doubleBuffer;
GLboolean needRedraw;
GLfloat xAngle, yAngle, zAngle;
float camera_x, camera_y, camera_z;
public:
glob();
int OnExecute();
public:
int init(int argc, char **argv);
void LoadGLTextures();
void OnEvent();
void redraw(void);
};
#endif // GLOB_H_INCLUDED
#如果包括全球
#定义包含的GLOB_H_
#包括
#包括
#包括//标准文件i/o的头文件。
#include/*这包括必要的X标题*/
#包括
//#包含//GLUT库的头文件
//#包含//GLu32库的头文件
#包括/*X11常数(例如TrueColor)*/
#包括
类球{
布尔跑;
GLuint texture;//当我们开始使用多于1时,创建一个数组
显示*dpy;
窗口赢;
XEvent事件;
双缓冲区;
GLD需要重新绘制;
甘格尔、杨乐、赞乐;
浮动摄影机x、摄影机y、摄影机z;
公众:
glob();
int-OnExecute();
公众:
int init(int argc,字符**argv);
void loadgl纹理();
void OnEvent();
作废重画(作废);
};
#endif//GLOB_H_包括在内
有人能帮我解决这个问题吗?很多事情可能会出问题 如果它是一个非常旧的文件,它可能有一个大小字段,每个字段只有2个字节 你的机器是小endian吗?BMP文件以小端存储 请注意,高度可能为负值(这意味着它是自顶向下的位图,而不是自下而上的位图)。如果你把一个小的负数解释为一个无符号的32位整数,你会看到数十亿的数值 此外,对实际像素数据的搜索假定它在位图标题之后立即开始。这是常见的,但不是必需的。包含实际像素数据的偏移量。(Microsoft文档称之为“位图位”或“颜色数据”。) 我建议对文件的开头做一个十六进制转储,并用手一步一步地进行,以确保所有偏移量和假设都是正确的。请随意将十六进制转储的开头粘贴到您的问题中
你在Windows上吗?您可以调用吗?因此,您在文件中查找18个字节,并希望它是以序列化的无符号长字符串形式表示的宽度。您是否将文件(例如在十六进制编辑器中)打开到v