2012-09-17 20 views
0

我的源代碼出現錯誤,導致位圖圖像顯得過寬。例如它會打印寬度和高度,高度是完美的(256),寬度也應該是256,但程序說它是數十億像素寬,每次都不一樣。這裏是源代碼。解碼位圖時獲取不正確的寬度

#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是這樣的:

#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 

任何機構可以幫助我解決這個問題?

+0

所以,你正在尋找18個字節的文件,並期待這是一個串行無符號長的形式的寬度。您是否打開文件(例如在十六進制編輯器中)以驗證假設是否正確? –

+0

根據http://en.wikipedia.org/wiki/BMP_file_format有7種不同類型的DIB頭文件,並且18個字節絕對位於DIB頭文件中。您需要查看位圖標頭中的前兩個字節(偏移量0)以確定您正在處理的DIB標頭的類型。 –

回答

2

很多事情可能會出錯。

如果這是一個非常舊的文件,它可能有一個BITMAPCOREHEADER其大小字段每個只有2個字節。

你的機器是小端的嗎? BMP文件存儲小端。

請注意,高度可能是負值(這意味着它是自頂向下的位圖而不是自下而上的位圖)。如果你將一個小的負數解釋爲一個無符號的32位整數,你會看到數十億的值。

此外,您對實際像素數據的尋求假設它在位圖標題之後立即開始。這很常見,但不是必需的。 file header包含實際像素數據的偏移量。 (微軟文檔稱之爲「位圖位」或「顏色數據」。)

我建議對文件的開頭執行十六進制轉儲,並手動執行它以確保所有的偏移和假設都正確。隨意將十六進制轉儲的開頭粘貼到您的問題中。

你在Windows上嗎?你能撥打LoadImage嗎?

+0

我在Linux上,當我從另一個文件運行而不是從我的項目 –