Thread: A nice C Programming question

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1
    This is nice C Programming question.

    Code:
    you are going to implement an image manipulation sowtware. 
    The software you will implement will be able to read and write windows BMP color image files. 
    The required operations that you need to implement is shown in the following sample menu of the program:
    
    WELCOME TO PICTURE APPLICATION
    PLEASE SELECT FROM FOLLOWING:
    1-	LOAD BMP FILE
    2-	STORE BMP FILE
    3-	INSERT NAME WATERMARK
    4-	ROTATE 90 DEGREE CLOCKWISE
    5-	FLIP HORIZONTAL
    6-	FLIP VERTICAL
    7-	DARKEN 10%
    8-	 
    9-	 
    0-	EXIT
    
    First two operations are used to load bitmap file to memory and store file to disk, both with asking filename within program directory. 
    Third operation, inserts your name to the bottom of files by changing relevant pixels. 
    Corresponding logo pixel matrix would be constructed as follows (use your name etc.):
     
    http://img253.imageshack.us/img253/7...e002ux8.th.jpg
    After running the code, BMP file should contain your logo at its bottom right corner. 
    Rotation of 90 degree should rotate the image to its right. 
    Flipping both horizontal and vertical are also necessary. You may start by writing flipping function. 
    Darkening 10% requires each RGB component will be multiplied by 0.9 and written back. 
    This will reduce brightness of the image. You may add extra features for menus 8 and 9 if you like, however it’s not obligatory. 
    NOTE: You are not going to display the image on screen. No windows is necessary. Use Paint or photoshop to view files.
    
    The BITMAP image file can be of “any” height and width, and the information is stored within the file itself. 
    You may use 640x480 images to start with. Use attached sample to learn how to read BMP files.
    
    	
    Your program should include all necessary error checking. 
    	
    /* A SAMPLE PROGRAM DEMONSTRATING HOW TO READ A BITMAP FILE */
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    
    /* Structures */
    typedef struct
    {
    int rows; int cols; unsigned char* data;
    } sImage;
    
    /* Functions */
    long getImageInfo(FILE*, long, int);
    
    int main(int argc, char * argv[])
    {
    
     FILE        *bmpInput;
     char		 signature[2];	   /* Signature of the Image File BM = BMP */
     int         nRows, nCols;        /* Row and Column size of the Image */
     int         xpixpeRm, ypixpeRm;  /* Pixel/m */
     long        nColors;             /* BMP number of colors */
     long        fileSize;            /* BMP file size */
     long        vectorSize;         /* BMP's raster data in number of bytes */
     int		 nBits;	              /* # of BIts per Pixel */
     int		 rasterOffset;        /* Beginning of the Raster Data */
     int         i;
    
     if(argc<2) {
       printf("Usage: %s bmpInput.bmp\n", argv[0]);
       exit(0);
     }
     printf("Reading filename %s\n", argv[1]);
    
     if ((bmpInput = fopen(argv[1], "rb")) == NULL)
     {
    	 printf("Can not read BMP file");
    	 exit(0);
     } /* Read BMP input file */
    
     fseek(bmpInput, 0L, SEEK_SET);   /* File pointer at byte #0 */
    
     /* Signature of the File BM = BMP at byte # 0*/
     for(i=0; i<2; i++)
     {
    	signature[i] = (char)getImageInfo(bmpInput, i, 1);
     }
     if ((signature[0] == 'B') && (signature[1] == 'M')) printf("It is verified that the Image is in Bitmap format\n");
     else
     {
    	 printf("The image is not a bitmap format, quitting....\n");
    	 exit(0);
     }
    
     /* Read BMP bits/pixel at byte #28 */
     nBits = (int)getImageInfo(bmpInput, 28, 2);
     printf("The Image is \t%d-bit\n", nBits);
    
     /* Position of First Raster Data at byte # 10*/
     rasterOffset = (int)getImageInfo(bmpInput, 10, 4);
     printf("The beginning of the Raster Data \nis at \t\t%d byte\n", rasterOffset);
    
     /* Read BMP file size at byte # 2 */
     fileSize = getImageInfo(bmpInput, 2, 4);
     printf("File size is \t%ld byte\n", fileSize);
    
      /* Read BMP width at byte #18 */
     nCols = (int)getImageInfo(bmpInput, 18, 4);
     printf("Width: \t\t%d\n", nCols);
    
     /* Read BMP height at byte #22 */
     nRows = (int)getImageInfo(bmpInput, 22, 4);
     printf("Height: \t%d\n", nRows);
    
     /* # of Pixels in a meter in x direction at byte # 38 */
     xpixpeRm = (int)getImageInfo(bmpInput, 38, 4);
     printf("Image has \t%d pixels per m in x-dir.\n", xpixpeRm);
    
     /* # of Pixels in a meter in y direction at byte # 42 */
     ypixpeRm = getImageInfo(bmpInput, 42, 4);
     printf("Image has \t%d pixels per m in y-dir.\n", ypixpeRm);
    
     /* Read number of colors at byte #46 */
     nColors = pow(2L,nBits);
     printf("There are \t%ld number of Colors \n", nColors);
    
     vectorSize = (long)((long)nCols*(long)nRows);
     printf("Vector Size is \t%ld\n", vectorSize);
    
     fclose (bmpInput);
    
     } /* end of main */
    
    
     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);
    
         value = (long)(value + (*ptrC)*(pow(256, (i-1))));
      }
    
      return(value);
    
     } /* end of getImageInfo */
    
    /******************* END *********************/
    Last edited by westfalen; 01-18-2009 at 06:44 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I see no question. I do see a homework assignment.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    When i run the .exe of this sample always appears: "usage: bmpImput.bmp"

    What i do wrong?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by marcus View Post
    When i run the .exe of this sample always appears: "usage: bmpImput.bmp"

    What i do wrong?
    I'm gonna go out on a limb here and claim an input file is required on the command-line, perhaps a bitmap file...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    it only appears "press a key to exit" or something similar, i can't claim an input file

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by marcus View Post
    it only appears "press a key to exit" or something similar, i can't claim an input file
    ....
    Quote Originally Posted by mk27!!!
    on the command-line, perhaps
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    1) you don't provide any INPUT. You have to supply a bitmap image and it's location in the directory.
    2) You are supposed to ADD code to accomplish the MENU requirements listed above.
    3) You need to CREATE a MENU so items can be selected from the above list.
    4) I think you have been skipping class and have no idea what you are doing --- would that be a fair assumption?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's another clue and a decent place for you to start investigating C:
    Code:
    int main(int argc, char * argv[])
    [...]
    if(argc<2)
    You need a grip on:
    "if" statements
    integers
    main()
    function parameters
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 06:24 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM