hallo
ich hab mich vom beispiel 6 von nehe.gamedev.net bedient.
leider gibt es probleme beim lesen aus einer bmp datei:
Code:
/* Image type - contains height, width, and data */
struct Image {
unsigned long sizeX;
unsigned long sizeY;
char *data;
};
typedef struct Image Image;
// quick and dirty bitmap loader...for 24 bit bitmaps with 1 plane only.
// See http://www.dcs.ed.ac.uk/~mxr/gfx/2d/BMP.txt for more info.
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("File 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
fseek(file,22,SEEK_SET);
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");
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 1;
}
ich erhlte bei compilieren mittels "gcc lesseon6.c -lGL -lGLU -lglut" keine fehler oder warnungen, allerdings bei der ausführung des programms:
Code:
./a.out
Width of Data/lesson6/NeHe.bmp: 256
Height of Data/lesson6/NeHe.bmp: 46909632807168
Error allocating memory for color-corrected image data
der erste wert stimmt, der zweite definitiv nicht !
ich hab übrigens deises tutorial schonmal gemacht und ich kann mich daran erinnern, dass er ohne probleme auszuführen ging. die bmp ist übrigens auch in ordnung, ich hab sie mit einer anderen getestet, was zum gleichen ergebnis führt.
hier der download link:
http://nehe.gamedev.net/data/lessons...esson06.tar.gz
ich nutze linux gentoo mit kernel 2.6.15
ich hab auch mich erkundigt, wie die bmp header aufgebaut ist und ich bin der meinung, dass der source an sich richtig ist !
dennoch kann ich mir die fehler die auftreten nicht erklären.
bitte probiert den source aus, sofern ihr könnt (ich mein wegen linux)
mich würde zugern interessieren warum das nicht funktioniert .
Lesezeichen