Read Image into Array using C++

Oct 4, 2015 at 7:15pm
Hello everyone!

I have been trying to program reading an image into an array, and to date, have achieved the following in my *.cpp file:

#include <stdafx.h>
#include <iostream>
#include <fstream>
#include <windows.h>

using namespace std;

int main()
{
ifstream image;
image.open("image.bmp",std::ios_base::binary);

if (image.is_open())
{
image.close();
cout<< "function success\n";
}
else
{
cout<< "unable to open file";
}

....

However, the next steps taken are not proving successful as I receive a run-time error (with 'n' being -1).

//get length of file:
image.seekg(0, image.end);
int n = image.tellg();
image.seekg (0, image.beg);

//allocate memory:
char* res = new char[n];

} //end of main() function.

I would really appreciate some guidance and the code in reading an image into an array. I am currently using *.bmp files.

Thanks in advance!
Oct 4, 2015 at 7:54pm
1
2
3
4
5
if (image.is_open())
{
image.close();
cout<< "function success\n";
} 
YOu are instantly closing file if it is open. Of course following operations will give you an error.
Oct 5, 2015 at 6:33pm
Thanks. Appreciate the guidance.

Is there a way of finding the height and width from the *.bmp file either through its header file or another way? I'm currently using Visual Studio 2010.

The plan is to slightly alter the code above to dynamically allocate a 1D array but using the width and height. I am posting the code intended to use but I firstly need to obtain the height and width of the image.

int width, height; // width and height of image
int offset = 0; // num of elements traversed in array
int value = 0; // image intensity value

unsigned byte* array_1D = NULL; // Pointer to unsigned byte
dynamic allocation array_1D = new unsigned byte[height*width];

if(array_1D == NULL) return; // return if memory not allocated

for(int j=0; j<height; j++) // traverse height (or rows)
{
offset = width * j; // modify offset travelled
for(int i=0; i<width; i++) // traverse width (or columns)
{
// update value at current index i.e. (offset+i)
array_1D[offset + i] = value++;
}
value = 0;
}

//delete the memory dynamically allocated
delete[] array_1D;

Thanks in advance!
Oct 5, 2015 at 8:04pm
Search on wiki BMP file format , look at the picture . Images , audios , videos, contains headers at the beggining . Depending on the format , the size of the header (in bytes) is fixed. Bitmap can be read as text file , but I suggest you to start reading binary file , since bitmap are big.
Oct 5, 2015 at 8:15pm
Bitmap can be read as text file
Never. Do. That. 

Bitmaps are binary files. Reading them as text can lead to incorrectly read data and damaged image as a result. It takes a a single sequence of 0x0D 0x0A for everything to break.
Oct 5, 2015 at 8:23pm
@MiiNiPaa sure. For educational purpose only.
Oct 5, 2015 at 8:47pm
Thanks for your input.

I recognise that the header file is 54 bytes and have learnt that the height is byte 18 and the width is byte 22.

I currently have this:

//get length of file:
image.seekg(0, image.end);
int n = image.tellg();
image.seekg (0, image.beg);

//allocate memory:
char* res = new char[n];

//read data as a block:
image.read(res, n);

Is there a way I can loop through to extract the relevant information? Is there a snippet of sample code available, also?

Thanks.
Last edited on Oct 5, 2015 at 8:47pm
Topic archived. No new replies allowed.