473,322 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Need grayscale image class help.

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
6 3278
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
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
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
"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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tjh | last post by:
hi - i'm a bit new to PHP - i've been messing around with the GD2 image functions and was wondering: i see a lot of information about converting a color image to grayscale but i can't figure...
1
by: Sinora | last post by:
I am trying to cahnge pixel value of a grayscale image using Bitmap object . Each pixel is consists of 8 bits. Bitmap bigim(720,480,PixelFormat8bppIndexed) ( I tried 8 instead...
4
by: Phil | last post by:
k, here is my issue.. I have BLOB data in SQL that needs to be grabbed and made into a TIF file and placed on the client (could be in temp internet dir). The reason we need it in TIF format is...
8
by: RicercatoreSbadato | last post by:
I'm using bmp.Save() and the bmp is in PixelFormat.Format8bppIndexed. But when I open the image with Gimp, it tells me that the image is RGB and not grayscale.
0
by: tlemcenvisit | last post by:
Hello I translated this code (witch convert an image to grayscale) from C#.NET to C++.NET The C#.NET code is : private void GrayScale(Bitmap b) { BitmapData bmData = b.LockBits(new...
2
by: Henry Wu | last post by:
Hi I was at the search for making e.Graphics.DrawImage turn any image to Grayscale, and I found two similar solutions but different ColorMatrix values, what is the difference between the two? Is...
8
by: platinumhimani | last post by:
-How to convert any image(8,16,24,32 or 64-bit) to 8-bit grayscale -i have tried to convert a 24-bit image to grayscale using setpixel and getpixel functions, in vb.net but i am unable to save...
1
by: raghavshastri | last post by:
You are to write a C++ program to perform a statistical analysis of the blobs in an image. The image will be a grayscale image in PGM format for simplicity. Here is a sample PGM image with 10...
12
by: Speed | last post by:
Hi, Could you please tell me what is the simplest code to read a 8-bit grayscale JPEG using C++? Thanks a ton, Speed.
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.