Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading an image 1

Status
Not open for further replies.

ferumus

Technical User
Jun 5, 2009
4
CA
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 have looked at the PGM format specification at
You mentioned you want to write a program that modifies a PGM image. Although untested, the code you have above should read in the PGM image into charImage[], which is not exactly how I would have done it, but it is a start.

Have you tried the code you supplied? If so, what was the result? The code does not supply a real definition of what "ImageType& image" is. Perhaps it is obvious to others what "ImageType& image" is, but it is not obvious to me. If you had trouble compiling the code, I would imaging "ImageType& image" would cause the compiler to choke.

What development tool are you using to write your program? C++ Builder? If you aren't using C++ Builder, is your development tool capable of showing images?

Steve.
 
Thanks for the reply Steve.

You are right. That code gave me some build problems.
So I read the image something like:

unsigned char* read_pgm(char *file_name, int *width, int *height);
void write_pgm(char *file_name, char *comment, unsigned char *image,int width,int height);

strcpy(image_in,"pic.pgm");

in=read_pgm(image_in,width,height);

This worked in storing the image in an array of values from 0 to 255.

You mentioned showing images. I am using Dev C++ which I believe is not capable of this.
So I am writing the image to a file and then opening it.
With something like:


strcpy(image_out,"outPic.pgm");
write_pgm(image_out,"pic_mod1.c",out,width,height);
// here 'out' is the array of pixels after modification.


This works for basic modification now.

The issue I have now is converting the color image to a grayscale first. Then I can use the above to modify it. If anyone knows a way I would be grateful.


Thanks

 
You will have to either stumble across some example code on the internet showing how to convert from color to gray or you will have to compare a color image and an otherwise identical grayscale image to develop your own algorithm to perform the conversion. Below is a link to an 'array' of identical images in both color and grayscale. You can compare the images and develop your own algorithm.



To compare the data I would recommend a Hex Editor. I have used XVI32 for years and I highly recommend it:



Steve.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top