1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
int rows,cols, maxV;
char header [100], *ptr;
std::ifstream im;
//open image in binary format
im.open(name.c_str(), std::ios::in | std::ios::binary);
if (!im)
{
std::cout << "Can't read image!" << std::endl;
exit(1);
}
//read the header of the image
im.getline(header, 3,'\n');
//test if header is P6
if ((header[0] != 80) || (header[1] != 54))
{
std::cout << "Image" << name << "is not .ppm format" << std::endl;
}
//get next line for height and width
im.getline(header,100,'\n');
//dont read the comments
while (header[0] == '#')
im.getline(header,100,'\n');
//number of columns, rows
cols = strtol(header, &ptr, 0);
rows = strtol(header, &ptr, 0);
maxV = strtol(header, &ptr, 0);
const int rows1=rows;
const int cols1=cols;
Component * tbuffer;
const_cast<Component*> (tbuffer);
tbuffer = new Component[rows1*cols1 * 3];
im.read((char *)tbuffer, cols*rows * 3);
std::cout << tbuffer[3000000] << std::endl;
im.close();
|