Hello all,
I am new to much C++ syntax. I am an avid programmer in other languages. I am trying to develope an application that takes an image and modifies it automatically. But for some reason I can't find an efficient way to read a .pgm image (converting it to an array) and then modifying some of the pixels and then just displaying it.
I have this starting code I found:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include "image.h"
int 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);
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;
return (1);
}
Any advice would be much appreciated.
Thanks,
fer
I am new to much C++ syntax. I am an avid programmer in other languages. I am trying to develope an application that takes an image and modifies it automatically. But for some reason I can't find an efficient way to read a .pgm image (converting it to an array) and then modifying some of the pixels and then just displaying it.
I have this starting code I found:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include "image.h"
int 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);
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;
return (1);
}
Any advice would be much appreciated.
Thanks,
fer