PDA

View Full Version : Problem running this program



joker40
11-10-2010, 06:22 PM
Hi,
I found this program online and trying to run it using bloodshed but when i run nothing happens. Can anyone try to compile and run this program please


#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <malloc.h>
#include <FreeImage.h>

//the pixel structure
typedef struct {
GLubyte r, g, b;
} pixel;

//the global structure
typedef struct {
pixel *data;
int w, h;
} glob;
glob global;

//read image
pixel *read_img(char *name, int *width, int *height) {
FIBITMAP *image;
int i,j,pnum;
RGBQUAD aPixel;
pixel *data;

if((image = FreeImage_Load(FIF_TIFF, name, 0)) == NULL) {
return NULL;
}
*width = FreeImage_GetWidth(image);
*height = FreeImage_GetHeight(image);

data = (pixel *)malloc((*height)*(*width)*sizeof(pixel *));
pnum=0;
for(i = 0 ; i < (*height) ; i++) {
for(j = 0 ; j < (*width) ; j++) {
FreeImage_GetPixelColor(image, j, i, &amp;aPixel);
data[pnum].r = (aPixel.rgbRed);
data[pnum].g = (aPixel.rgbGreen);
data[pnum++].b = (aPixel.rgbBlue);
}
}
FreeImage_Unload(image);
return data;
}//read_img

//write_img
void write_img(char *name, pixel *data, int width, int height) {
FIBITMAP *image;
RGBQUAD aPixel;
int i,j;

image = FreeImage_Allocate(width, height, 24, 0, 0, 0);
if(!image) {
perror("FreeImage_Allocate");
return;
}
for(i = 0 ; i < height ; i++) {
for(j = 0 ; j < width ; j++) {
aPixel.rgbRed = data[i*width+j].r;
aPixel.rgbGreen = data[i*width+j].g;
aPixel.rgbBlue = data[i*width+j].b;

FreeImage_SetPixelColor(image, j, i, &amp;aPixel);
}
}
if(!FreeImage_Save(FIF_TIFF, image, name, 0)) {
perror("FreeImage_Save");
}
FreeImage_Unload(image);
}//write_img


/*draw the image - it is already in the format openGL requires for glDrawPixels*/
void display_image(void)
{
glDrawPixels(global.w, global.h, GL_RGB, GL_UNSIGNED_BYTE, (GLubyte*)global.data);
glFlush();
}//display_image()

// Read the screen image back to the data buffer after drawing to it
void draw_triangle(void)
{
glDrawPixels(global.w, global.h, GL_RGB, GL_UNSIGNED_BYTE, (GLubyte*)global.data);
glBegin(GL_TRIANGLES);
glColor3f(1.0,0,0);
glVertex2i(rand()%global.w,rand()%global.h);
glColor3f(0,1.0,0);
glVertex2i(rand()%global.w,rand()%global.h);
glColor3f(0,0,1.0);
glVertex2i(rand()%global.w,rand()%global.h);
glEnd();
glFlush();
glReadPixels(0,0,global.w,global.h,GL_RGB, GL_UNSIGNED_BYTE, (GLubyte*)global.data);
}

/* A simple thresholding filter.
*/
void MyFilter(pixel* Im, int myIm_Width, int myIm_Height){
int x,y;

for (x=0; x < myIm_Width; x++)
for (y=0; y < myIm_Height; y++){
if (Im[x+y*myIm_Width].b > 128)
Im[x+y*myIm_Width].b = 255;
else
Im[x+y*myIm_Width].b = 0;

if (Im[x+y*myIm_Width].g > 128)
Im[x+y*myIm_Width].g = 255;
else
Im[x+y*myIm_Width].g = 0;

if (Im[x+y*myIm_Width].r > 128)
Im[x+y*myIm_Width].r = 255;
else
Im[x+y*myIm_Width].r = 0;
}

glutPostRedisplay(); // Tell glut that the image has been updated and needs to be redrawn
}//My_Filter


/*glut keyboard function*/
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 0x1B:
case'q':
case 'Q':
exit(0);
break;
case's':
case'S':
printf("SAVING IMAGE: backup.tif\n");
write_img("backup.tif", global.data, global.w, global.h);
break;
case 't':
case 'T':
draw_triangle();
break;
case'f':
case'F':
MyFilter(global.data,global.w,global.h);
break;
}
}//keyboard


int main(int argc, char** argv)
{
global.data = read_img("myImage.tif", &amp;global.w, &amp;global.h);
if (global.data==NULL)
{
printf("Error loading image file img.tif\n");
return 1;
}
printf("Q:quit\nF:filter\nT:triangle\nS:save\n");
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);

glutInitWindowSize(global.w,global.h);
glutCreateWindow("SIMPLE DISPLAY");
glShadeModel(GL_SMOOTH);
glutDisplayFunc(display_image);
glutKeyboardFunc(keyboard);
glMatrixMode(GL_PROJECTION);
glOrtho(0,global.w,0,global.h,0,1);

glutMainLoop();

return 0;
}

bcthund
11-10-2010, 06:47 PM
Error loading image file img.tif


Make sure you don't have that problem first off like I did...

Upon looking closer at the code, it is actually looking for "myImage.tif" and so I renamed the image I made to that and it worked just fine.

joker40
11-10-2010, 07:20 PM
I fixed that and it compiles fine but i don't understand what does it do actually.
when you run it what did the program do?

bcthund
11-11-2010, 02:26 AM
It draws the texture to a window. You can press "f" and it filters the texture to pure red, green, and blue. If you press "t" it draws a random triangle. If you press "s" it saves what is currently displayed to disk.

joker40
11-11-2010, 05:16 AM
when i run it it compiles with no error but when i run it i don't see anything.

bcthund
11-11-2010, 02:55 PM
Make sure you run the program in a console to see if it is giving you any errors, otherwise you won't get those errors.

If it doesn't give you errors in a console then you will have to do some debugging of your own to figure out why it doesn't work on you system.