Ok... Its a uni lab... The give us some COMPLETELY useless code, don't explain in the slightest how to use it other than basic theory, not implimentation in our lecture, and I REALY REALY don't want 2 spend all afternoon swaring at my PC like I did yesterday... Can any1 please plase please please please help me... I need to make a program that reads in a .raw immage and prints it to the screen then makes a coppie of that immage and prints that...
well this is the actual question
Write a C program using OpenGL that takes an input image and displays two images side by side. The left image is the original image and the second image is a copy of the original image. This means two different arrays are used. The original image will have a caption at the bottom of the image “original image” and the right image’s caption is “copy”. Note that the copy must be an actual copy and not the same image displayed in two separate locations. Example input images are provided (image.raw and link.raw). This image has 8 bits per pixel. The size is 320 columns and 200 rows. You can create your own images using Paint Shop/Photoshop/gimp or some other tool. If you change the image width and rows, your program will have to reflect this. Save in raw format. You may want to start off with code from the lecture notes.
The code from the lecture notes:
Code:
#define MAXROW 200
#define MAXCOL 320 /* handles 320x200 pixel images */
typedef unsigned char pixel; /* one byte (0 .. 255) for each
pixel */
typedef char name[15];
name image_file_name;
/* image buffers used for display */
pixel image_buf[MAXROW*MAXCOL],
out_image_buf[MAXROW*MAXCOL];
pixel image[MAXROW][MAXCOL],
out_image[MAXROW][MAXCOL] ; /* image arrays */
pixel p;
name inf_name, outf_name; /* strings to store file names */
FILE *inf_handle, *outf_handle; /*file handles created at file
open */
int charin;
int r,c;
void InputImage(void)
}
printf("Enter the name of the input file : ");
gets(inf_name);
/* try to open file for reading (r) in binary (b) mode */
if ((inf_handle = fopen(inf_name, "rb")) == NULL) /* open failed */
}
puts("*** Can't open input file - please check file name typed!\n ");
charin = getchar();
exit(1); /* terminate execution */
{
/* reaching this line of code means file opened successfully,
now read file contents into image array */
for ( r = 0; r < MAXROW; r++ )
for ( c = 0; c < MAXCOL; c++) {
if((charin=fgetc(inf_handle))==EOF) /* if read failed */
}
printf("***File reading failed! \n");
charin = getchar();
exit(1); /* terminate execution */
{
image[r][c] = charin;
{
/* reaching this line of code means all of file read successfully */
printf("\nImage input completed!\n");
fclose(inf_handle); /* close input file */
} /* end InputImage */
void Display(void) // fix it to get it to work.
}
int offset;
/* flip image - 1st row becomes last - before calling glDrawPixels
to display original image – why?*/
offset = 0;
for ( r = MAXROW-1; r >= 0; r-- ) {
for ( c = 0; c < MAXCOL; c++) {
image_buf[MAXCOL*offset + c] = image[r][c];
{
offset++;
{
/* flip image - 1st row becomes last - before calling glDrawPixels
to display processed image*/
offset = 0;
for ( r = MAXROW-1; r >= 0; r-- ) {
for ( c = 0; c < MAXCOL; c++) {
out_image_buf[MAXCOL*offset + c] =
out_image[r][c];
{
offset++;
{
glClear(GL_COLOR_BUFFER_BIT);
/* switch matrix mode to 'projection' */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* set up an orthographic projection in 2D with a 760x280 viewing
window */
gluOrtho2D(0.0,760.0, 0.0,280.0);
/* switch matrix mode back to 'model view' */
glMatrixMode(GL_MODELVIEW);
WriteCaptions();
/* set raster position for displaying image in graphics image buffer*/
glRasterPos2i(40, 40); // (x,y)
/* load graphics image buffer with image from your own image buffer */
glDrawPixels(MAXCOL, MAXROW, GL_LUMINANCE,
GL_UNSIGNED_BYTE, image);
27 ICT215 Topic 4
/* now do the same for displaying processed image to right of original
image*/
glRasterPos2i(?, ?);
// draw the other image
{
void WriteCaptions(void)
}
int i;
char caption1[ ] = "INPUT_IMAGE"; // 11 chars + null char
char caption2[ ] = "OUTPUT_IMAGE"; // 12 chars + null char
glColor3f(0.0, 0.0, 0.0); // this is black – so background has to be white.
glRasterPos2i(154, 20);
for (i=0; i< sizeof(caption1) ; i++)
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, caption1[i]);
// write the other caption
And heres my 1/2 working code... as in Ive got rid of most of the annoing stupid problems the ..............s decided 2 put in the code because ........ing with a program which your students have never done with so they are better debuggers is a great way to make them want to kill you.
Code:
#define MAXROW 200
#define MAXCOL 320 /* handles 320x200 pixel images */
/*
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
*/
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
void WriteCaptions(void);
typedef unsigned char pixel; /* one byte (0 .. 255) for each pixel */
typedef char name[15];
name image_file_name; /* image buffers used for display */
pixel image_buf[MAXROW*MAXCOL], out_image_buf[MAXROW*MAXCOL];
pixel image[MAXROW][MAXCOL], out_image[MAXROW][MAXCOL] ; /* image arrays */
pixel p;
name inf_name, outf_name; /* strings to store file names */
FILE *inf_handle, *outf_handle; /*file handles created at file open */
int charin;
int r,c;
void InputImage(void)
{
printf("Enter the name of the input file : ");
gets(inf_name);
/* try to open file for reading (r) in binary (b) mode */
if ((inf_handle = fopen(inf_name, "rb")) == NULL) /* open failed */
{
puts("*** Can't open input file - please check file name typed!\n ");
charin = getchar();
exit(1); /* terminate execution */
}
/* reaching this line of code means file opened successfully,
now read file contents into image array */
for ( r = 0; r < MAXROW; r++ )
for ( c = 0; c < MAXCOL; c++) {
if((charin=fgetc(inf_handle))==EOF) /* if read failed */
{
printf("***File reading failed! \n");
charin = getchar();
exit(1); /* terminate execution */
}
image[r][c] = charin;
}
/* reaching this line of code means all of file read successfully */
printf("\nImage input completed!\n");
fclose(inf_handle); /* close input file */
} /* end InputImage */
void Display(void) // fix it to get it to work.
{
int offset;
/* flip image - 1st row becomes last - before calling glDrawPixels
to display original image – why?*/
offset = 0;
for ( r = MAXROW-1; r >= 0; r-- ) {
for ( c = 0; c < MAXCOL; c++) {
image_buf[MAXCOL*offset + c] = image[r][c];
}
offset++;
}
/* flip image - 1st row becomes last - before calling glDrawPixels
to display processed image*/
offset = 0;
for ( r = MAXROW-1; r >= 0; r-- ) {
for ( c = 0; c < MAXCOL; c++) {
out_image_buf[MAXCOL*offset + c] = out_image[r][c];
}
offset++;
}
glClear(GL_COLOR_BUFFER_BIT);
/* switch matrix mode to 'projection' */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* set up an orthographic projection in 2D with a 760x280 viewing window */
gluOrtho2D(0.0,760.0, 0.0,280.0);
/* switch matrix mode back to 'model view' */
glMatrixMode(GL_MODELVIEW);
WriteCaptions();
/* set raster position for displaying image in graphics image buffer*/
glRasterPos2i(40, 40); // (x,y)
/* load graphics image buffer with image from your own image buffer */
glDrawPixels(MAXCOL, MAXROW, GL_LUMINANCE, GL_UNSIGNED_BYTE, image);
puts("Success");
/* now do the same for displaying processed image to right of original image*/
// glRasterPos2i(?, ?);
// draw the other image
}
void WriteCaptions(void)
{
int i;
char caption1[] = "INPUT_IMAGE"; // 11 chars + null char
char caption2[] = "OUTPUT_IMAGE"; // 12 chars + null char
glColor3f(0.0, 0.0, 0.0); // this is black – so background has to be white.
glRasterPos2i(154, 20);
for (i=0; i< sizeof(caption1) ; i++)
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, caption1[i]);
// write the other caption
}
void main(int argc, char **argv)
{
InputImage();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1000, 1000);
glutCreateWindow("raw viewer");
glutDisplayFunc(Display);
glutMainLoop();
glutIdleFunc(Display);
//WriteCaptions();
}
Its just really hard to find the problems because If I attempt to use a debugger I get a Segmentation fault... Posibly because glut can't debug... I don't know, all I know is that its a pain in the ass, I would be ok usualy, but I can't debugg which ........s me over because I have no idea why its not drawing.. Theres a posibility that the program is getting hung up in its first run of Display, but I am at my witts end and can't take any more of this .........