Connecting Tech Pros Worldwide Help | Site Map

Need grayscale image class help.

QuasiChameleon
Guest
 
Posts: n/a
#1: Aug 30 '05
Hi,

I'm trying to create a grayscale image class that reads and writes
grayscale Targa format. This works well with smaller images, but
corrupts larger images and creates a "Segmentation fault (core dumped)"
error. I am at a loss as to why this works only part of the time.

I hope it's proper etiquette to post the code below (3 files).

Can someone please show me what's wrong with the following code? I
adapted this from an old matrix class of mine.

Thanks in advance!

Kevin Crosby


Bare bones code below:
g++ -c -o image.o image.cpp
g++ -o targa.exe targa.cpp image.o -lm


//targa.cpp
int main(int argc, char **argv) {
int header[18];
image gpicval, gnew;

gpicval.readTarga(argv[1], header);
gnew = gpicval;
gnew.writeTarga(argv[2], header);
}


//image.cpp
#include <iostream.h>
#include <fstream.h>
#include "image.h"

image::image(int rows, int cols, int val) {
m = rows;
n = cols;
img = alloc(m, n);
for (int i=0; i < m; i++)
for (int j=0; j < n; j++)
img[i][j] = val;
}

void image::readTarga(char* filename, int* header) {
ifstream fpin(filename, ios::binary);
unsigned char gdata_char;
if (!fpin) {
cerr << "Cannot open input file!" << endl;
exit(0);
}
for (int j = 0; j < 18; j++) {
fpin >> gdata_char;
header[j] = gdata_char;
}

if (header[16] != 8) {
cerr << "This image file does not have 8 bits per pixel, execution
halted" << endl;
exit(2);
}

freem();

m = 256*header[15] + header[14]; /* number of lines in image */
n = 256*header[13] + header[12]; /* number of pixels per line */

img = alloc(m, n);
for (int k = 0; k < m; k++)
for (int j = 0; j < n; j++) {
fpin >> gdata_char;
img[j][k] = gdata_char;
}
fpin.close();
}

void image::writeTarga(char* filename, int* header) const {
ofstream fpout(filename, ios::binary);
unsigned char gdata_char;
if (!fpout) {
cerr << "Cannot open output file!" << endl;
exit(1);
}
for (int j = 0; j < 18; j++) {
gdata_char = header[j];
fpout << gdata_char;
}
for (int k = 0; k < m; k++)
for (int j = 0; j < n; j++) {
gdata_char = img[j][k];
fpout << gdata_char;
}
fpout.close();
}

image::~image() {
freem();
}

image::image(const image& val) {
m = val.m;
n = val.n;
img = alloc(m, n);
for (int i=0; i < m; i++)
for (int j=0; j < n; j++)
img[i][j] = val(i+1, j+1);
}

int **image::alloc(int rows, int cols) {
int **temp = new int*[rows];
for (int i=0; i < rows; i++)
temp[i] = new int[cols];
return (temp);
}

void image::freem(void) {
for(int i=0; i < m; i++)
delete [] img[i];
delete [] img;
}

image& image::operator=(const image& val) {
freem();
m = val.m;
n = val.n;
img = alloc(m, n);
for (int i=0; i < m; i++)
for (int j=0; j < n; j++)
img[i][j] = val(i+1, j+1);
return (*this);
}

int &image::operator() (int row, int col) {
if ((row <= 0) || (row > m) || (col <= 0) || (col > n)) {
cerr << "Index out of range." << endl;
exit(1);
}
return img[row - 1][col - 1];
}

int image::operator() (int row, int col) const {
if ((row <= 0) || (row > m) || (col <= 0) || (col > n)) {
cerr << "Index out of range." << endl;
exit(1);
}
return img[row - 1][col - 1];
}


//image.h
#ifndef IMAGE
#define IMAGE

class image {

/* define the structure of a image */
private:
int m, n; /* quantity of rows and columns */
int **img; /* a 2D array for the elements */

/* define functions that all other functions can use */
public:
image(int row=1, int col=1, int val=0); /* default to 1x1
vacuous */
void image::readTarga(char*, int*); /* read Targa from file
*/
void image::writeTarga(char*, int*) const; /* write Targa to file
*/
~image();
image(const image &); /* copy an
existing image */
int **alloc(int, int); // allocate memory for image
void freem(void); // free memory for image
image& operator=(const image&); // equal
int &operator()(int, int); // element selection (write)
int operator()(int, int) const; // element selection (read)
};
#endif

Victor Bazarov
Guest
 
Posts: n/a
#2: Aug 30 '05

re: Need grayscale image class help.


QuasiChameleon wrote:[color=blue]
> I'm trying to create a grayscale image class that reads and writes
> grayscale Targa format. This works well with smaller images, but
> corrupts larger images and creates a "Segmentation fault (core dumped)"
> error. I am at a loss as to why this works only part of the time.
>
> I hope it's proper etiquette to post the code below (3 files).
>
> Can someone please show me what's wrong with the following code? I
> adapted this from an old matrix class of mine.
> [...][/color]

You're using formatted input (operator >>) to read _unsigned_char_ values
from a *binary* file. I think you should switch to using '.get()', IOW,
use _unformatted_ input.

V
QuasiChameleon
Guest
 
Posts: n/a
#3: Aug 30 '05

re: Need grayscale image class help.


Victor,

Thanks for the input. I have changed the formatted I/O to unformatted
I/O.

However, I still get a segmentation fault and corrupted images, so
something else is wrong with it. The image it fails on is 208 x 202
pixels.

Thanks in advance,

Kevin Crosby

Victor Bazarov
Guest
 
Posts: n/a
#4: Aug 30 '05

re: Need grayscale image class help.


QuasiChameleon wrote:[color=blue]
> Thanks for the input. I have changed the formatted I/O to unformatted
> I/O.
>
> However, I still get a segmentation fault and corrupted images, so
> something else is wrong with it. The image it fails on is 208 x 202
> pixels.[/color]

It is rather difficult to do remote debugging without even having any
data. Please learn to use the debugger, it's not that difficult and
it will be beneficial in your future ventures. The least you should do
is run it under the debugger and let the debugger catch the fault and
tell you where it occurred.

To get more information on available debuggers and GUIs for them, post
to a newsgroup for your compiler or your OS.
Cy Edmunds
Guest
 
Posts: n/a
#5: Aug 31 '05

re: Need grayscale image class help.


"QuasiChameleon" <Kevin.Crosby@bigfoot.com> wrote in message
news:1125432400.674942.62970@g14g2000cwa.googlegro ups.com...[color=blue]
> Hi,
>
> I'm trying to create a grayscale image class that reads and writes
> grayscale Targa format. This works well with smaller images, but
> corrupts larger images and creates a "Segmentation fault (core dumped)"
> error. I am at a loss as to why this works only part of the time.
>
> I hope it's proper etiquette to post the code below (3 files).
>
> Can someone please show me what's wrong with the following code? I
> adapted this from an old matrix class of mine.
>
> Thanks in advance!
>
> Kevin Crosby
>
>
> Bare bones code below:[/color]
[snip]

I never tangled with Targa but I have written interfaces for reading and
writing jpeg, tiff, etc. My advice: don't even dream of writing your own
code for reading a graphic file format. These things tend to be complicated
and writing the code is almost sure to be time consuming. I just typed
"targa library" in Google and got plenty of useful looking hits.

--
Cy
http://home.rochester.rr.com/cyhome/


Aleksey Loginov
Guest
 
Posts: n/a
#6: Aug 31 '05

re: Need grayscale image class help.



QuasiChameleon wrote:[color=blue]
> Hi,
>
> I'm trying to create a grayscale image class that reads and writes
> grayscale Targa format. This works well with smaller images, but
> corrupts larger images and creates a "Segmentation fault (core dumped)"
> error. I am at a loss as to why this works only part of the time.
>
> I hope it's proper etiquette to post the code below (3 files).
>
> Can someone please show me what's wrong with the following code? I
> adapted this from an old matrix class of mine.
>
> Thanks in advance![/color]


in addition to Victor's post, check your indexes. see below
[color=blue]
>
> Kevin Crosby
>
>
> Bare bones code below:
> g++ -c -o image.o image.cpp
> g++ -o targa.exe targa.cpp image.o -lm
>
>
> //image.cpp
> #include <iostream.h>
> #include <fstream.h>
> #include "image.h"
>
> image::image(int rows, int cols, int val) {
> m = rows;
> n = cols;
> img = alloc(m, n);
> for (int i=0; i < m; i++)
> for (int j=0; j < n; j++)
> img[i][j] = val;[/color]

img[0..m-1][0..n-1]
[color=blue]
> }
>
> void image::readTarga(char* filename, int* header) {
> ifstream fpin(filename, ios::binary);
> unsigned char gdata_char;
> if (!fpin) {
> cerr << "Cannot open input file!" << endl;
> exit(0);
> }
> for (int j = 0; j < 18; j++) {
> fpin >> gdata_char;
> header[j] = gdata_char;
> }
>
> if (header[16] != 8) {
> cerr << "This image file does not have 8 bits per pixel, execution
> halted" << endl;
> exit(2);
> }
>
> freem();
>
> m = 256*header[15] + header[14]; /* number of lines in image */
> n = 256*header[13] + header[12]; /* number of pixels per line */
>
> img = alloc(m, n);
> for (int k = 0; k < m; k++)
> for (int j = 0; j < n; j++) {
> fpin >> gdata_char;
> img[j][k] = gdata_char;[/color]

img[0..n-1][0..m-1]
[color=blue]
> }
> fpin.close();
> }
>
> void image::writeTarga(char* filename, int* header) const {
> ofstream fpout(filename, ios::binary);
> unsigned char gdata_char;
> if (!fpout) {
> cerr << "Cannot open output file!" << endl;
> exit(1);
> }
> for (int j = 0; j < 18; j++) {
> gdata_char = header[j];
> fpout << gdata_char;
> }
> for (int k = 0; k < m; k++)
> for (int j = 0; j < n; j++) {
> gdata_char = img[j][k];[/color]

img[0..n-1][0..m-1]
[color=blue]
> fpout << gdata_char;
> }
> fpout.close();
> }
>
> image::~image() {
> freem();
> }
>
> image::image(const image& val) {
> m = val.m;
> n = val.n;
> img = alloc(m, n);[/color]

img[0..m-1][0..n-1]
[color=blue]
> for (int i=0; i < m; i++)
> for (int j=0; j < n; j++)
> img[i][j] = val(i+1, j+1);[/color]

img[0..m-1][0..n-1]
[color=blue]
> [...][/color]

QuasiChameleon
Guest
 
Posts: n/a
#7: Aug 31 '05

re: Need grayscale image class help.


Thanks Aleksey! I've corrected my indices in my readTarga and
writeTarga functions and now my program works perfectly.

Thank you all for your help!

Kevin Crosby

Closed Thread