[Newbie] I am trying to understand a reinterpret_cast could anyone help?
Hello all,
I am trying to understand a function in a computer vision class the idea is that it reads in a PGM file, however I am struggling with a single concept of it
Here is where I am confused:
ifp.read( reinterpret_cast<char >(charImage), (MN)*sizeof(unsigned char));
Here is the function:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
#include "image.h"
void readImage(char fname[], ImageType& image)
{
int i, j;
int N, M, Q;
unsigned char *charImage;
char header [100], *ptr;
ifstream ifp;
ifp.open(fname, ios::in | ios::binary);
if (!ifp) {
cout << "Can't read image: " << fname << endl;
exit(1);
}
// read header
ifp.getline(header,100,'\n');
if ( (header[0]!=80) || /* 'P' */
(header[1]!=53) ) { /* '5' */
cout << "Image " << fname << " is not PGM" << endl;
exit(1);
}
ifp.getline(header,100,'\n');
while(header[0]=='#')
ifp.getline(header,100,'\n');
M=strtol(header,&ptr,0);
N=atoi(ptr);
ifp.getline(header,100,'\n');
Q=strtol(header,&ptr,0);
charImage = (unsigned char *) new unsigned char [M*N];
ifp.read( reinterpret_cast<char *>(charImage), (M*N)*sizeof(unsigned char));
if (ifp.fail()) {
cout << "Image " << fname << " has wrong size" << endl;
exit(1);
}
ifp.close();
//
// Convert the unsigned characters to integers
//
int val;
for(i=0; i<N; i++)
for(j=0; j<M; j++) {
val = (int)charImage[i*M+j];
image.setPixelVal(i, j, val);
}
delete [] charImage;
}
Here is what I understand:
ifp.read( reinterpret_cast<char >(charImage), (MN)*sizeof(unsigned char));
ifp.read: simply takes in the remaining file and throws it into charImage at the allocated size of MNsizeof(unsigned char) however I am very confused about reinterpret_case what is its purpose why is it there?
Posted Apr 15, 2016, 1:14 AM UTC
64