Thread: Free glut, Rastering a .raw file

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Free glut, Rastering a .raw file

    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 .........

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Falconmick View Post
    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...

    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 .........


    So you figure you'll just pop on in and get us to do your homework for you?


    Homework Policy

    (Plus, you seriously need to work on your spelling and grammar.)

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    My grammar/spelling was due to a massive headache I was having... Haha also just read over my post... I failed to mention what I wanted to ask... What I was trying to ask is can anyone tell me why when I compile and run this code that it brings up the glut screen then does nothing, I put in a line to test if it was being held up, and I think it is inside of display.

    I didn't mean to have this as a do my homework, I ment it as in can somone please tell me what I'm doing wrong/point me in the direction of a guide that would then let me be able to do the question.

    hmmm grammar/spelling might also be that I have horrible grammar/spelling.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Any one wanting the code here it is (working)
    Well... I had 2 re-do it once I completed it so I havent added text or 2 immages, although if you are having issues with drawing a .raw this should help 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];
                out_image[r][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_buf);
        puts("Success");
        /* now do the same for displaying processed image to right of original image*/
    //    glRasterPos2i(?, ?);
        // draw the other image
        glutSwapBuffers();
    }
    
    
    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(500, 500);
        glutCreateWindow("raw viewer");
        glutDisplayFunc(Display);
        glutMainLoop();
            glutIdleFunc(Display);
        //WriteCaptions();
    }
    The main problem I was having was not using
    Code:
    glutSwapBuffers();
    which was why I was asking for help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-11-2010, 12:05 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM