please, any pointers to why this code is not working...?

This is a discussion on please, any pointers to why this code is not working...? within the C Programming forums, part of the General Programming Boards category; Code: #include (stdio.h) #include (stdlib.h) #include (math.h) #include (alloc.h) /*-------STRUCTURES---------*/ typedef struct {int rows; int cols; unsigned char* data;} sImage; ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    1

    please, any pointers to why this code is not working...?

    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			SUM;
       int			nColors;
       unsigned long	vectorSize;
       unsigned long	fileSize;
       int			MASK[5][5];
       unsigned char 	*pChar, someChar;
       unsigned int		row, col;
    
       someChar = '0'; pChar = &someChar;
    
       /* 5x5 Laplace mask.  Ref: Myler Handbook p. 135 */
       MASK[0][0] = -1; MASK[0][1] = -1; MASK[0][2] = -1; MASK[0][3] = -1; MASK[0][4] = -1;
       MASK[1][0] = -1; MASK[1][1] = -1; MASK[1][2] = -1; MASK[1][3] = -1; MASK[1][4] = -1;
       MASK[2][0] = -1; MASK[2][1] = -1; MASK[2][2] = 24; MASK[2][3] = -1; MASK[2][4] = -1;
       MASK[3][0] = -1; MASK[3][1] = -1; MASK[3][2] = -1; MASK[3][3] = -1; MASK[3][4] = -1;
       MASK[4][0] = -1; MASK[4][1] = -1; MASK[4][2] = -1; MASK[4][3] = -1; MASK[4][4] = -1;
    
       if(argc < 2) {
         printf("Usage: %s bmpInput.bmp\n", argv[0]);
         exit(0);
       };
       printf("Reading filename %s\n", argv[1]);
    
       /* open files for reading and writing to */
       bmpInput = fopen(argv[1], "rb");
       bmpOutput = fopen("edgeLap.bmp", "wb");
    
       /* start pointer at beginning of file */
       fseek(bmpInput, 0L, SEEK_END);
    
       /* retrieve and print filesize and number of cols and rows */
       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;
    
       printf("Width: %d\n", originalImage.cols);
       printf("Height: %d\n", originalImage.rows);
       printf("File size: %lu\n", fileSize);
    
       /* retrieve and print Number of colors */
       nColors = (int)getImageInfo(bmpInput, 46, 4);
       printf("nColors: %d\n", nColors);
    
       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);
    
       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;
    	}
       }
    
       for(Y=0; Y<=(originalImage.rows-1); Y++)  {
    	for(X=0; X<=(originalImage.cols-1); X++)  {
    	     SUM = 0;
    
    	  /* image boundaries */
    	  if(Y==0 || Y==1 || Y==originalImage.rows-2 || Y==originalImage.rows-1)
    		  SUM = 0;
    	  else if(X==0 || X==1 || X==originalImage.cols-2 || X==originalImage.cols-1)
    		  SUM = 0;
    
    	  /* Convolution starts here */
    	  else   {
    	     for(I=-2; I<=2; I++)  {
    		 for(J=-2; J<=2; J++)  {
    		    SUM = SUM + (int)( (*(originalImage.data + X + I + 
                              (Y + J)*originalImage.cols)) * MASK[I+2][J+2]);
    
    		 }
    	     }
    	  }
    	     if(SUM>255)  SUM=255;
    	     if(SUM<0)    SUM=0;
    
    	     *(edgeImage.data + X + Y*originalImage.cols) = 255 - (unsigned char)(SUM);
     	     fwrite((edgeImage.data + X + Y*originalImage.cols),sizeof(char),1,bmpOutput);
    	}
       }
    
       printf("See edgeLap.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 used Borland's Turbo C++ compiler to compile it. But it doesn't work.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Did you look at the error messages you got?

    For a start, you use < and > for header files: #include <stdio.h>, not #include (stdio.h)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > for(i=0; i<=(4*nColors); i++) /* there are (4*nColors) bytesin color table */
    Well this goes round the loop one more times than you expect

    > fread(ptrC, sizeof(char), 1, inputFile);
    You may as well just write
    fread(&dummy, sizeof(char), 1, inputFile);
    and save yourself some variables, assignments etc.

    In addition, check the return results.

    > for(i=0; i<=50; i++)
    Again, this is 51 iterations, not 50

    > originalImage.data = farmalloc(vectorSize*sizeof(unsigned char));
    Try using a new compiler for the modern age, rather than that stone-age fossil limited to 64K at a time (just how much memory do you have in your real machine?)

    > value = (long)(value + (*ptrC)*(pow(256, (i-1))));
    Ewww, using pow() to calculate powers of 2

    value = ( value << 8 ) | *ptrC;
    Is how you shuffle bytes into a longer word.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Plus sizeof(unsigned char) is always 1.

    Did you write that code? (And you mean Turbo C, right?)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2009, 09:37 PM
  2. Replies: 3
    Last Post: 02-24-2009, 08:49 PM
  3. my code is not working
    By turbot in forum C Programming
    Replies: 3
    Last Post: 01-28-2009, 05:23 AM
  4. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  5. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 08:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21