Link to home
Start Free TrialLog in
Avatar of twobitadder
twobitadder

asked on

Bitmap program compile time error

Hi,

I've got some compile-time errors I can't understand in the following small bitmap program (takes an input bitmap and produces a modified output):

52 C:\myc\iedit.c  : initializer element is not constant
52 C:\myc\iedit.c  : [Warning] data definition has no type or storage class
54 C:\myc\iedit.c  : [Warning] data definition has no type or storage class

with some follow up errors too.  I can't understant why the initializer element needs to be constant.
I previously used the const keyword in the prototype const int doMenu(void) in iedit_menu.c  but removed it when I saw it was wrong.
I don't think the old version of the object file is being used but I could be wrong.

There are 2 source files, a menu and the implementation code  and one header file for the menu.

File 1:
-------------------------------------
Filename : iedit_menu.h
Synopsis : Header for prototypes and defines for the user menu

#define QUIT_OPTION 0
#define GRAYSCALE_OPTION 1
#define INVERT_OPTION 2

int doMenu(void);
--------------------------------

File 2:
---------------------------------
Filename : iedit_menu.c
Synopsis : Header for prototypes and defines for the user menu

#include "iedit_menu.h"

int doMenu(void)
{
  int tempChoice;
 
  for(;;)
  {
    printf("Choose an option:\r\n");
    printf("1 - Change Image to Greyscale");
    printf("2 - Invert Image Colours");
    printf("0 - Quit");
    tempChoice = getchar();
    if(tempChoice <=2 && tempChoice >0)
      return tempChoice;
    else
      printf("Invalid choice");
  }
}
-------------------------------------------------------------

File 3:
------------------------------
Filename : iedit..c
Synopsis :  Contains the main method and code for producing a new modified bitmap from an original.


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "iedit_menu.h"

#define BITMAP_HEADER_SIZE 115
#define DEBUG 0


int main(int argc, char** argv)
{
  FILE *inFile, *outFile;
  unsigned char mask;
  int i, temp, byteCounter, choice;
  int tempPart[4],signature;
  unsigned char fileType[2];
 
  mask = 0xFF;
 
    /*Check args and open source and destination files*/
  if(argc!=3)
  {
    fprintf(stderr,"Usage : iedit <source file> <destination file>");
    return EXIT_FAILURE;
  }
  inFile = fopen(argv[1],"rb");
  if(inFile == NULL)
  {
    fprintf(stderr,"iedit : Unable to open the file '%s' for reading", argv[1]);
    return EXIT_FAILURE;
  }
  outFile = fopen(argv[2],"wb");
  if(outFile == NULL)
  {
    fprintf(stderr,"iedit : Unable to open the file '%s' for reading", argv[2]);
    return EXIT_FAILURE;
  }
   
 
  fileType[0] = getc(inFile);
  fileType[1] = getc(inFile);
  fseek(inFile, 0, SEEK_SET);
  signature = ( (int)fileType[0]<<8 ) + (int)fileType[1];
  if(signature!= 0x424D)
    printf("The file does not have a recognised bitmap signature, do you wish to continue?");
    if(tolower(getChar())=='n')
      return EXIT_SUCCESS;
  }    
 
 
  choice = doMenu();
   
  byteCounter =0;  
 
    /*copy the bitmap header exactly*/
  for(i=0;i<BITMAP_HEADER_SIZE;i++)
  {
     temp = getc(inFile);
     if(temp==EOF)
     {
       fprintf(stderr,"iedit : Error, unexpected EOF found in bitmap header.\r\nSource file is likely not a bitmap or is corrupt");
       return EXIT_FAILURE;
     }
     putc(temp, outFile);    
  }
 
    /*manipulate the image data*/
  switch(choice)
  {
    case QUIT_OPTION:
      printf("Done.");
      return EXIT_SUCCESS;
      break;
    case GRAYSCALE_OPTION:
        while( (tempPart[byteCounter] = getc(inFile))!=EOF )
        {
          byteCounter++;
            /* On the 3rdbyte of a pixel, write output and reset counter*/
          if(byteCounter ==3)
          {
            byteCounter = 0;
             /*get average for grayscale intensity*/
            tempPart[0] = (tempPart[0]+tempPart[1]+tempPart[2])/3;
     
            putc(tempPart[0], outFile);
            putc(tempPart[0], outFile);
            putc(tempPart[0], outFile);
            putc(tempPart[3], outFile); /* translucency byte*/
          }
        }
        break;  
    case default :
      printf("Invalid selection");
      break;
  }
 
}
 

Thanks for any help.
Avatar of twobitadder
twobitadder

ASKER

just noticed a mistake also in the error messages for opening input and output files, they are both the same doh. (Evils of cut and paste), also once i get this sorted I'm going to modifiy the header so that it shows as greyscale and reduce the number of output bytes from 3 to 1 which is more sensible.  The real problem is the requirement for a constant initializer. I can't see why...
SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The missing bracket after if (.....) is causing the function to close before choice = doMenu(); causing unexpected errors.

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, everything's fixed except one linker error I can't figure the reason for:
 
[Linker error] undefined reference to `doMenu'

I compile it in dev-cpp which uses gcc, doMenu is declared in iedit_menu.h which is included in iedit.c and it's defined in iedit_menu.c, is this likely just an ide misconfiguration or did i make an error in the program here?

Thanks again.
You have to link iedit.o and iedit_menu.o together when building the executable.
For such a simple program, just compile and link them in the same command:

gcc -Wall -o iedit  iedit.c iedit_menu.c