0
我正在嘗試讀取BMP文件並將其保存在2維的緩衝區中,以便以直角旋轉它。但是,最終文件不正確,無法顯示。以下代碼中的問題是什麼?讀取BMP文件並旋轉
#include <fstream.h>
#include <iostream.h>
int main() {
long int width=686;
long int height=800 ;
/* open file */
FILE * bitmap_file= fopen("filename.bmp", "rb");
if(!bitmap_file) {
printf("Could not find file.\n");
fclose(bitmap_file);
return 0;
}
printf("File opened.\n");
typedef struct buffer1
{
int red;
int green;
int blue;
} buffer1;
static buffer1 buffer_bitmap1[686][800];
fseek(bitmap_file, 0, SEEK_SET);
int count=0;
for(int i = 0; i <8; ++i){
for(int j = 0; j <6; ++j){
fread(&buffer_bitmap1[j][i].red , 1, 1, bitmap_file); /*for rotate bmp exchange the width with height in matrix.*/
fread(&buffer_bitmap1[j][i].green, 1, 1, bitmap_file);
fread(&buffer_bitmap1[j][i].blue, 1, 1, bitmap_file);
count=count+8;
fseek(bitmap_file,count, SEEK_CUR); /*move pointer*/
}
}
fclose(bitmap_file);
FILE *bitmap_file1= fopen("filename.bmp", "a");
count=0;
fseek(bitmap_file, 0, SEEK_SET);
char * a ="a";
for(int i = 0; i <6; ++i){
for(int j = 0; j <8; ++j) {
fwrite(&buffer_bitmap1[i][j],1,1,bitmap_file1);
fwrite(&buffer_bitmap1[i][j].blue,1,1,bitmap_file1);
fwrite(&buffer_bitmap1[i][j].green,1,1,bitmap_file1);
count=count+8;
fseek(bitmap_file, 8, SEEK_CUR); /*move pointer*/
}
}
fclose(bitmap_file);
return 0;
}
看起來像純C語言爲什麼它被標記爲C++? – 2013-03-10 12:46:40
所以,你包含'fstream.h'和'iostream.h'(它們都不應該存在),並且根本不使用它們。然後你使用了一堆隱含的聲明函數,因爲你沒有包含'cstdio'。哎喲。除此之外,你能提供能顯示你想要的結果和你的實際結果的圖片嗎? – Zeta 2013-03-10 12:49:18
在函數作用域中定義結構體?現在,這是一個怪異的東西! – 2013-03-10 12:54:28