Thread: New to C++, trying to get program to run

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    5

    New to C++, trying to get program to run

    Hello everyone, I got a little help with some coding for a homework assignment where I'm supposed to use a Sobel filter for edge detection in a picture. I got the code for it, but since my background is in MATLAB, I'm having changing the code so that it will work in the C++ environment. I am using the software Dev C++ (was told it was a good resource), and I just need some help banging through this assignment.

    My biggest problem at the moment is figuring out syntax for reading files into the program because frankly, I just know how to do this in MATLAB. I'm working with PGM files and in MATLAB, you simply read the image into the program, but in C++ I have no clue how to do this. The code I got reads a BMP file into C++, but I would like to know how I would read a grayscale PGM file into C++ so that it's a matrix (the way it would be read into MATLAB basically).

    Here is the code I have:
    Code:
    #include (stdio.h)
    #include (stdlib.h)
    #include (math.h)
    #include (alloc.h)
    
    /*-------STRUCTURES---------*/
    typedef struct {int rows; int cols; unsigned char* data;} sImage;
    
    /*-------PROTOTYPES---------*/
    long getImageInfo(FILE*, long, int);
    void copyImageInfo(FILE* inputFile, FILE* outputFile);
    void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors);
    
    int main(int argc, char* argv[])
    {
       FILE			*bmpInput, *bmpOutput;
       sImage		originalImage;
       sImage		edgeImage;
       unsigned int		X, Y;
       int			I, J;
       long			sumX, sumY;
       int			nColors, SUM;
       unsigned long	vectorSize;
       unsigned long	fileSize;
       int			GX[3][3];
       int			GY[3][3];
       unsigned char 	*pChar, someChar;
       unsigned int		row, col;
    
       someChar = '0'; pChar = &someChar;
    
       /* 3x3 GX Sobel mask.  */
       GX[0][0] = -1; GX[0][1] = 0; GX[0][2] = 1;
       GX[1][0] = -2; GX[1][1] = 0; GX[1][2] = 2;
       GX[2][0] = -1; GX[2][1] = 0; GX[2][2] = 1;
    
       /* 3x3 GY Sobel mask. */
       GY[0][0] =  1; GY[0][1] =  2; GY[0][2] =  1;
       GY[1][0] =  0; GY[1][1] =  0; GY[1][2] =  0;
       GY[2][0] = -1; GY[2][1] = -2; GY[2][2] = -1;
    
       if(argc < 2) {
         printf("Usage: %s bmpInput.bmp\n", argv[0]);
         exit(0);
       };
       printf("Reading filename %s\n", argv[1]);
    
       /*-------DECLARE INPUT & OUTPUT FILES-------*/
       bmpInput = fopen(argv[1], "rb");
       bmpOutput = fopen("edgeSob.bmp", "wb");
    
       /*---SET POINTER TO BEGINNING OF FILE----*/
       fseek(bmpInput, 0L, SEEK_END);
    
       /*-------GET INPUT BMP DATA--------*/
       fileSize = getImageInfo(bmpInput, 2, 4);
       originalImage.cols = (int)getImageInfo(bmpInput, 18, 4);
       originalImage.rows = (int)getImageInfo(bmpInput, 22, 4);
       edgeImage.rows = originalImage.rows;
       edgeImage.cols = originalImage.cols;
    
       /*--------PRINT DATA TO SCREEN----------*/
       printf("Width: %d\n", originalImage.cols);
       printf("Height: %d\n", originalImage.rows);
       printf("File size: %lu\n", fileSize);
    
       nColors = (int)getImageInfo(bmpInput, 46, 4);
       printf("nColors: %d\n", nColors);
    
       /*------ALLOCATE MEMORY FOR FILES--------*/
       vectorSize = fileSize - (14+40+4*nColors);
       printf("vectorSize: %lu\n", vectorSize);
       edgeImage.data = farmalloc(vectorSize*sizeof(unsigned char));
       if(edgeImage.data == NULL) {
    	printf("Failed to malloc edgeImage.data\n");
    	exit(0);
       }
       printf("%lu bytes malloc'ed for edgeImage.data\n", vectorSize);
    
       originalImage.data = farmalloc(vectorSize*sizeof(unsigned char));
       if(originalImage.data == NULL) {
    	printf("Failed to malloc originalImage.data\n");
    	exit(0);
       }
       printf("%lu bytes malloc'ed for originalImage.datt\n", vectorSize);
    
       /*------COPY HEADER AND COLOR TABLE---------*/
       copyImageInfo(bmpInput, bmpOutput);
       copyColorTable(bmpInput, bmpOutput, nColors);
       fseek(bmpInput, (14+40+4*nColors), SEEK_SET);
       fseek(bmpOutput, (14+40+4*nColors), SEEK_SET);
    
       /* Read input.bmp and store it's raster data into originalImage.data */
       for(row=0; row<=originalImage.rows-1; row++) {
    	for(col=0; col<=originalImage.cols-1; col++) {
    	     fread(pChar, sizeof(char), 1, bmpInput);
    	     *(originalImage.data + row*originalImage.cols + col) = *pChar;
    	}
       }
    
       /*---------------------------------------------------
    		SOBEL ALGORITHM STARTS HERE
       ---------------------------------------------------*/
       for(Y=0; Y<=(originalImage.rows-1); Y++)  {
    	for(X=0; X<=(originalImage.cols-1); X++)  {
    	     sumX = 0;
    	     sumY = 0;
    
                 /* image boundaries */
    	     if(Y==0 || Y==originalImage.rows-1)
    		  SUM = 0;
    	     else if(X==0 || X==originalImage.cols-1)
    		  SUM = 0;
    
    	     /* Convolution starts here */
    	     else   {
    
    	       /*-------X GRADIENT APPROXIMATION------*/
    	       for(I=-1; I<=1; I++)  {
    		   for(J=-1; J<=1; J++)  {
    		      sumX = sumX + (int)( (*(originalImage.data + X + I + 
                                 (Y + J)*originalImage.cols)) * GX[I+1][J+1]);
    		   }
    	       }
    	       if(sumX>255)  sumX=255;
    	       if(sumX<0)    sumX=0;
    
    	       /*-------Y GRADIENT APPROXIMATION-------*/
    	       for(I=-1; I<=1; I++)  {
    		   for(J=-1; J<=1; J++)  {
    		       sumY = sumY + (int)( (*(originalImage.data + X + I + 
                                  (Y + J)*originalImage.cols)) * GY[I+1][J+1]);
    		   }
    	       }
    	       if(sumY>255)   sumY=255;
    	       if(sumY<0)     sumY=0;
    
    	       /*---GRADIENT MAGNITUDE APPROXIMATION (Myler p.218)----*/
                   SUM = abs(sumX) + abs(sumY);
                 }
    
    	     *(edgeImage.data + X + Y*originalImage.cols) = 255 - (unsigned char)(SUM);
    	     fwrite((edgeImage.data + X + Y*originalImage.cols),sizeof(char),1,bmpOutput);
    	}
       }
    
       printf("See edgeSob.bmp for results\n");
       fclose(bmpInput);
       fclose(bmpOutput);
       farfree(edgeImage.data);      /* Finished with edgeImage.data */
       farfree(originalImage.data);  /* Finished with originalImage.data */
       return 0;
    }
    
    /*----------GET IMAGE INFO SUBPROGRAM--------------*/
    long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
    {
      unsigned char			*ptrC;
      long				value = 0L;
      unsigned char			dummy;
      int				i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, offset, SEEK_SET);
    
      for(i=1; i<=numberOfChars; i++)
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        /* calculate value based on adding bytes */
        value = (long)(value + (*ptrC)*(pow(256, (i-1))));
      }
      return(value);
    
    } /* end of getImageInfo */
    
    /*-------------COPIES HEADER AND INFO HEADER----------------*/
    void copyImageInfo(FILE* inputFile, FILE* outputFile)
    {
      unsigned char		*ptrC;
      unsigned char		dummy;
      int				i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, 0L, SEEK_SET);
      fseek(outputFile, 0L, SEEK_SET);
    
      for(i=0; i<=50; i++)
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        fwrite(ptrC, sizeof(char), 1, outputFile);
      }
    
    }
    
    /*----------------COPIES COLOR TABLE-----------------------------*/
    void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors)
    {
      unsigned char		*ptrC;
      unsigned char		dummy;
      int				i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, 54L, SEEK_SET);
      fseek(outputFile, 54L, SEEK_SET);
    
      for(i=0; i<=(4*nColors); i++)  /* there are (4*nColors) bytesin color table */
      {
        fread(ptrC, sizeof(char), 1, inputFile); 
        fwrite(ptrC, sizeof(char), 1, outputFile);
      }
    
    }
    I've been having some trouble with programming lately because I never know what alerations I'm supposed to make, whether I use # or not, and just many other general problems that I asked if I would need C for the class, and I was told no.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    You can get the format of a PGM file here.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Or I have code to read a PGM file a return a buffer with its content. If you want I can post the code, although it hasn't been very tested.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 05:32 PM
  3. Replies: 3
    Last Post: 07-11-2005, 03:07 AM
  4. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM
  5. Cannot run program in background?
    By registering in forum Linux Programming
    Replies: 3
    Last Post: 06-16-2003, 05:47 AM