2015-04-27 33 views
-1

我是C++和編程的新手。我有以下有缺陷的代碼從BMP文件讀取並寫入另一個BMP文件。我不想使用任何外部庫。讀取和寫入BMP文件

我有一個800kb的24位bmp文件。 mybmp.bmp。將嘗試並將其上傳到保管箱。

`#include <iostream> 
#include <conio.h> 
#include <fstream> 
#include<stdio.h> 
#include<stdlib.h> 
using namespace std; 


unsigned char* editing(char* filename) 
{ 
    int i; 
int j; 
FILE* mybmpfilespointer; 
mybmpfilespointer = fopen(filename, "rb"); 
unsigned char headerinfo[54]; 
fread(headerinfo, sizeof(unsigned char), 54, mybmpfilespointer); // read the 54-byte header size_t fread (void * ptr, size_t size, size_t count, FILE * stream); 

// extract image height and width from header 
int width = *(int*)&headerinfo[18]; 
int height = *(int*)&headerinfo[22]; 

int size = 3 * width * height; 
unsigned char* imagesdata = new unsigned char[size]; // allocate 3 bytes per pixel 
fread(imagesdata, sizeof(unsigned char), size, mybmpfilespointer); // read the rest of the imagesdata at once 

// display image height and width from header 
cout << " width:" << width << endl; 
cout << " height:" << height << endl; 

ofstream arrayfile("bmpofstream.bmp"); // File Creation 

for(int a = 0; a < 53; a++) //bgr to rgb 
{ 
    arrayfile << headerinfo[a]; 
} 

for(int k=0; k<size; k++) 
{ 
    arrayfile<<imagesdata[k]<<endl; //Outputs array to file 
} 
arrayfile.close(); 

delete[] mybmpfilespointer; 
delete[] imagesdata; 
fclose(mybmpfilespointer); 
return imagesdata; 
return headerinfo; 


} 

int main() 
{ 

FILE* mybmpfilespointer = fopen("mybmp.bmp", "rb"); 
if (mybmpfilespointer) 
{ 

    editing("mybmp.bmp"); 
} 
else 
{ 
    cout << "Cant Read File"; 
} 

}` 

正如你可以看到我從mybmp.bmp這是819680bytes

和寫入bmpofstream.bmp,因爲它是閱讀。

但不知怎的,生成的文件恰好是大約2460826bytes的3倍於mybmp的大小。

我從讀取頭文件mybmp文件爲headerinfo。 和 來自的數據mybmp as imagesdata

當我寫入bmpofstream.bmp這些數組它是一個搞砸的bmp文件。

1)我猜想文件大小的增加與讀取單個像素有關,並將它們寫入3次或者其他東西,但是無法弄清楚。你爲什麼認爲這會是?

2)一旦我弄清楚如何讀寫這個文件,我想修改它。所以我現在不妨問一下:

我想修改這個圖像,以便我可以將每個像素的值增加50,所以這將最終在一個較暗的圖像。我可以直接這樣做:

for(j = 0; j < size; j++) 
    { 
    imagesdata[j]=imagesdata[j]+50; 
    } 

謝謝。

回答

0

試試這個

#include <iostream> 
#include <conio.h> 
#include <fstream> 
#include <vector> 
#include <iterator> 

void editing(char* filename) 
{ 
    std::ifstream input(filename, std::ios::binary); 

    if(!input.is_open()) 
    { 
     std::cout << "Can`t open file" << std::endl; 
     return; 
    } 

    // copies all data into buffer 
    std::vector<char> buffer((std::istreambuf_iterator<char>(input)), (std::istreambuf_iterator<char>())); 
    input.close(); 

    std::vector<char> headerinfo; 
    { 
     auto it = std::next(buffer.begin(), 54); 
     std::move(buffer.begin(), it, std::back_inserter(headerinfo)); 
     buffer.erase(buffer.begin(), it); 
    } 
    // extract image height and width from header 
    int width = *reinterpret_cast<int*>((char*)headerinfo.data() + 18); 
    int height = *reinterpret_cast<int*>((char*)headerinfo.data() + 22); 

    int size = 3 * width * height; 


    std::vector<char> imagesdata; 
    { 
     auto it = std::next(buffer.begin(), size); 
     std::move(buffer.begin(), it, std::back_inserter(imagesdata)); 
     buffer.erase(buffer.begin(), it); 
    } 
    // display image height and width from header 
    std::cout << " width:" << width << std::endl; 
    std::cout << " height:" << height << std::endl; 

    // paste your code here 
    // ... 
    std::ofstream arrayfile("bmpofstream.bmp"); // File Creation 

    std::ostream_iterator<char> output_iterator(arrayfile); 
    std::copy(headerinfo.begin(), headerinfo.end(), output_iterator); // write header to file 
    std::copy(imagesdata.begin(), imagesdata.end(), output_iterator); // write image data to file 
} 

int main(int argc, char**argv) 
{ 
    editing(argv[1]); 
} 
+0

你能解釋一下爲什麼要OP 「試試吧」?爲什麼不解釋這些差異是什麼/你改變了什麼或修正了什麼?這些代碼實際上做了什麼?這有點像,你知道,你從阿姆斯特丹的一個男人那裏得到一顆藥丸,「他說,」試試這個,夥計。「你會吞下它嗎? – usr2564301