2013-11-02 51 views
1

我已經獲得了標題和信息標題。第一個像素讀取以下代碼即可。C將bmp像素讀入2d數組

// The BMPHEADER structure. 
typedef struct { 
byte  sigB; 
byte  sigM; 
int32_t  fileSize; 
int16_t  resv1; 
int16_t  resv2; 
int32_t  pixelOffset; 
} tBmpHeader; 

// The BMPINFOHEADER structure. 
typedef struct { 
int32_t  size; 
int32_t  width; 
int32_t  height; 
int16_t  colorPlanes; 
int16_t  bitsPerPixel; 
byte  zeros[24]; 
} tBmpInfoHeader; 

typedef uint8_t byte; 

typedef struct { 
byte blue; 
byte green; 
byte red; 
} tPixel; 

// A BMP image consists of the BMPHEADER and BMPINFOHEADER structures, and the 2D pixel array. 
typedef struct { 
tBmpHeader  header; 
tBmpInfoHeader infoHeader; 
tPixel   **pixel; 
} tBmp; 

tPixel **BmpPixelAlloc(int pWidth, int pHeight) 
{ 
    tPixel **pixels = (tPixel **)malloc (pHeight * sizeof(tPixel *)); 
    for (int row = 0; row < pHeight; ++row) 
    { 
     pixels[row] = (tPixel *)malloc(pWidth * sizeof(tPixel)); 
    } 

    return pixels; 
} 

tError BmpRead(char *pFilename, tBmp *pBmp) 
{ 
pBmp->pixel = BmpPixelAlloc(pBmp->infoHeader.width, pBmp->infoHeader.height); 

if(FileRead(file, &pBmp->pixel, sizeof(tPixel), 1)!=0) 
{ 
    errorCode = ErrorFileRead; 
} 
} 

家教我是說我說應該做到以下幾點

,但是這給了我一個分段錯誤。如果我在循環中放置一個打印件,它會在說出分段錯誤之前打印幾千次。我嘗試使用double for循環,但它甚至不能編譯。我花了幾個小時在谷歌,但無法弄清楚。

+0

爲什麼要麻煩?已經有一個易於使用且易於理解的[library](http://easybmp.sourceforge.net/)。 –

+0

家庭作業assignmnet – Covertpyro

+0

那麼,即使如此,嘗試通過該庫中的代碼。你可能會學到一些東西。 –

回答

0

沒關係我想通了。

for(int i = 0; i < pBmp->infoHeader.height; ++i) 
{ 
    for(int j = 0; j < pBmp->infoHeader.width; ++j) 
    { 
     if(FileRead(file, &pBmp->pixel[i][j], sizeof(tPixel), 1)!=0) 
     { 
      errorCode = ErrorFileRead; 
     } 
    } 
}