By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,617 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,617 IT Pros & Developers. It's quick & easy.

Need grayscale image class help.

P: n/a
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

Aug 30 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
QuasiChameleon wrote:
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.
[...]


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
Aug 30 '05 #2

P: n/a
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

Aug 30 '05 #3

P: n/a
QuasiChameleon wrote:
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.


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.
Aug 30 '05 #4

P: n/a
"QuasiChameleon" <Ke**********@bigfoot.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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:

[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/
Aug 31 '05 #5

P: n/a

QuasiChameleon wrote:
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!

in addition to Victor's post, check your indexes. see below

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;
img[0..m-1][0..n-1]
}

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;
img[0..n-1][0..m-1]
}
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];
img[0..n-1][0..m-1]
fpout << gdata_char;
}
fpout.close();
}

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

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


Aug 31 '05 #6

P: n/a
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

Aug 31 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.