Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Bitmap program compile time error

Posted on 2004-08-23
6
Medium Priority
?
361 Views
Last Modified: 2013-12-03
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.
0
Comment
Question by:twobitadder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 5

Author Comment

by:twobitadder
ID: 11873856
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...
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 1000 total points
ID: 11873941
I think the problem is here:

if(signature!= 0x424D)   {    /* MISSING BRACKET */
    printf("The file does not have a recognised bitmap signature, do you wish to continue?");
    if(tolower(getChar())=='n')
      return EXIT_SUCCESS;
  }    
 
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11873957
The missing bracket after if (.....) is causing the function to close before choice = doMenu(); causing unexpected errors.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 23

Accepted Solution

by:
brettmjohnson earned 1000 total points
ID: 11874009
The primary problem is a missing open brace when testing the file signature:
      if(signature!= 0x424D)
      {  <----  add this brace

Using a text editor that auto-indents or matches braces, brackets, and parenthesis
would help find these trivial errors.

      case default:  
should be
      default:

getChar() should be getchar()

and iedit_menu.c should #include <stdio.h>

0
 
LVL 5

Author Comment

by:twobitadder
ID: 11879217
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.
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 11879333
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



0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question