Help would be much appreciated.

This is a discussion on Help would be much appreciated. within the C++ Programming forums, part of the General Programming Boards category; Alright, I'll be straight up about this. This is a homework assignment. I know those are looked down on based ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Help would be much appreciated.

    Alright, I'll be straight up about this. This is a homework assignment. I know those are looked down on based on the sticky that an admin posted. So before anyone gets ........ed off, I'm not looking for you to do the entire thing for me. I just need help.

    I'm doing an assignment that has much of the necessary code already given to me, as the professor says the code is far too advanced for the scope of our class (intro to C++) and basically I just have to change things here and there to get it to work the way I need it to.

    The assignment is all about 2D arrays-pictures, to be precise. The first task is brightness adjustment. The code given to me changes each value within the 2D array of an image to a higher value. I somehow have to change it so that the amount of change is based on a user input. Any clues as to where I should go to implement a change would be helpful. Here is the code as it is given.

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define  BUFFER_SIZE  70
    #define  TRUE          1
    #define  FALSE         0
    
    int**  img;
    int    numRows;
    int    numCols;
    int    maxVal;
    FILE*  fo1;
    
    void addtopixels(int** imgtemp, int value);
    void  writeoutpic(char* fileName, int** imgtemp);
    int** readpic(char* fileName);
    void  readHeader(FILE* imgFin);
    int   isComment(char* line);
    void  readImgID(char* line);
    void  readImgSize(char* line);
    void  readMaxVal(char* line);
    int** setImage();
    void  readBinaryData(FILE* imgFin, int** imgtemp);
    
    int main()
    {
            char fileName[BUFFER_SIZE];
            int i,j,rows,cols;
            char ci;
    
    
            printf("Enter image filename: ");
            scanf("%s", fileName);
    
            img = readpic(fileName);
    
            printf("Successfully read image file '%s'\n", fileName);
    
    
            addtopixels(img,103);
    
            printf("Enter image filename for output: ");
            scanf("%s", fileName);
    
            writeoutpic(fileName,img);
    
            free(img);
            img = NULL;
    
            return(EXIT_SUCCESS);
    }
    
    void addtopixels(int** imgtemp, int value)
    {  
            int i,j;
            
            for (i=0;i<numRows;i++)
            { for (j=0;j<numCols;j++)
                    {
                      imgtemp[i][j] += value;
                    }
            }
    }
    
    void writeoutpic(char* fileName, int** imgtemp)
    {
            int i,j;
            char ci;
            FILE* fo1;
            
            if((fo1 = fopen(fileName, "wb")) == NULL)
            {
                    printf("Unable to open out image file '%s'\n", fileName);
                    exit(EXIT_FAILURE);
            }
    
            fprintf(fo1,"P5\n");
            fprintf(fo1,"%d %d\n", numRows, numCols);
            fprintf(fo1,"255\n");
    
            for (i=0;i<numRows;i++)
            { for (j=0;j<numCols;j++)
                    {
                      ci   =  (char) (imgtemp[i][j]);
                      fprintf(fo1,"%c", ci);
                    }
            }
    }
    
    
    
    
    int** readpic(char* fileName)
    {
            FILE* imgFin;
            int** imgtemp;
    
            if((imgFin = fopen(fileName, "rb")) == NULL)
            {
                    printf("Unable to open image file '%s'\n", fileName);
                    exit(EXIT_FAILURE);
            }
    
            readHeader(imgFin);
    
    
            imgtemp  = setImage();
    
            readBinaryData(imgFin, imgtemp);
    
            fclose(imgFin);
            
            return  imgtemp;
    
    }
    
    void readHeader(FILE* imgFin)
    {
            int  haveReadImgID   = FALSE;
            int  haveReadImgSize = FALSE;
            int  haveReadMaxVal  = FALSE;
            char line[BUFFER_SIZE];
    
            while(!(haveReadImgID && haveReadImgSize && haveReadMaxVal))
            {
                    fgets(line, BUFFER_SIZE, imgFin);
    
                    if((strlen(line) == 0) || (strlen(line) == 1))
                            continue;
    
                    if(isComment(line))
                            continue;
    
                    if(!(haveReadImgID))
                    {
                            readImgID(line);
                            haveReadImgID = TRUE;
                    }
                    else if(!(haveReadImgSize))
                    {
                            readImgSize(line);
                            haveReadImgSize = TRUE;
                    }
                    else if(!(haveReadMaxVal))
                    {
                            readMaxVal(line);
                            haveReadMaxVal = TRUE;
                    }
            }
    
    }
    
    int isComment(char *line)
    {
            if(line[0] == '#')
                    return(TRUE);
    
            return(FALSE);
    }
    
    void readImgID(char* line)
    {
            if(strcmp(line, "P5\n") != 0)
            {
                    printf("Invalid image ID\n");
                    exit(EXIT_FAILURE);
            }
    }
    
    void readImgSize(char* line)
    {
            unsigned i;
    
            for(i = 0; i < strlen(line); ++i)
            {
                    if(!((isdigit((int) line[i])) || (isspace((int) line[i]))))
                    {
                            printf("Invalid image size\n");
                            exit(EXIT_FAILURE);
                    }
            }
    
            sscanf(line, "%d %d", &numRows, &numCols);
    }
    
    void readMaxVal(char* line)
    {
            unsigned i;
    
            for(i = 0; i < strlen(line); ++i)
            {
                    if(!((isdigit((int) line[i])) || (isspace((int) line[i]))))
                    {
                            printf("Invalid image max value\n");
                            exit(EXIT_FAILURE);
                    }
            }
    
            maxVal = atoi(line);
    }
    
    int** setImage()
    {
            int** imgtemp;
            unsigned i;
    
            imgtemp = (int**) calloc(numRows, sizeof(int*));
    
            for(i = 0; i < numRows; ++i)
            {
                    imgtemp[i] = (int*) calloc(numCols, sizeof(int));
            }
            return imgtemp;
    }
    
    void readBinaryData(FILE* imgFin, int** imgtemp)
    {
            unsigned  i;
            unsigned  j;
            for(i = 0; i < numRows; ++i)
            {
                    for(j = 0; j < numCols; ++j)
                    {
                                imgtemp[i][j] = 
                                fgetc(imgFin);
                    }
            }
    }
    The above code is, like i said, a contrast editor.

    On top of finding what to change so that the contrast increase/decrease (can go either way by using a + or - number), I have to embed this into an options menu (which utilizes if and else ifs), which I have already created. What, if anything, should I leave OUT of the option slot for this particular option and leave at the top by my int main? Can i simply change what I need to change and drop the whole thing into the option slot? I know that ints, floats, etc. can be declared anywhere in the program, assuming that they're declared prior to being used.

    thanks for reading, and I really appreciate help because I have a hard time tracking down a small bit of what needs to be changed if I don't really know what's going on in the code as a whole. This class has turned into a disaster.

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Making progress... I think?

    Ok, I think I'm getting closer. This is what I have so far. I think it's getting close to letting the compiler run, the only errors I get are [Linker error] undefined reference to `readpic', [Linker error] undefined reference to `addtopixels', and [Linker error] undefined reference to `writeoutpic'. What do I need to fix to make those errors go away?

    Only option that is filled with functioning code is option 1.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define  BUFFER_SIZE  70
    #define  TRUE          1
    #define  FALSE         0
    
    int**  img;
    int    numRows;
    int    numCols;
    int    maxVal;
    FILE*  fo1;
    
    void addtopixels(int** imgtemp, int value);
    void  writeoutpic(char* fileName, int** imgtemp);
    int** readpic(char* fileName);
    void  readHeader(FILE* imgFin);
    int   isComment(char* line);
    void  readImgID(char* line);
    void  readImgSize(char* line);
    void  readMaxVal(char* line);
    int** setImage();
    void  readBinaryData(FILE* imgFin, int** imgtemp);
    
    int add_bright;
    
    char fileName[BUFFER_SIZE];
            int i,j,rows,cols;
            char ci;
    
    int main(void) {
    
    int option;
    
        while (option!=4) {
        
        printf("Please Choose an Option.\n\n");
        printf("1. Brightness\n");
        printf("2. Image Subtraction\n");
        printf("3. Sobel Edge Highlighter\n");
    
    
    //Prompts user for program option.    
        printf("Enter your choice: ");
        scanf("%d", &option);
        
    
                   
                   //begin option 1 here.
    if (option == 1) {
               
    void addtopixels(int** imgtemp, int value);
    void  writeoutpic(char* fileName, int** imgtemp);
    int** readpic(char* fileName);
    
            printf("Enter image filename: ");
            scanf("%s", fileName);
     
            img = readpic(fileName);
     
            printf("Successfully read image file '%s'\n", fileName);
    
            printf("How much brighness would you like to add?");
            scanf("%d", &add_bright);
    
            addtopixels(img,add_bright);
            printf("Enter image filename for output: ");
            scanf("%s", fileName);
            writeoutpic(fileName,img);
            free(img);
            img = NULL;
            return(EXIT_SUCCESS);
    
    
                   //end option 1 here.
                   
                   
                   system("pause");
                   return 0;
                   }
                   
    else if(option==2) {
         
         
                  //begin option 2 here.
                   printf("Option 2.\n");
                   
                   //end option 2 here.
                   
                   system("pause");
                   return 0;
                   }
    else if (option==3) {
         
         //begin option 3 here.
         printf("Option 3.");
         //end option 3 here.
         
    
         system("pause");
         return 0;
         }}
    system("pause");
    
    return 0;
    }
    can anybody tell me what I need to make the errors go away?

  3. #3
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    What errors are you actually getting there? (Not that I'll actually be able to help on this one as this is beyond me, but someone else should be able to pick it up). When I compiled it in both Dev-C++ and code::blocks I received no errors.

    Also, for an intro to C++ that is somewhat advanced, although the header files are C libraries.
    Any help I give may be classified as:
    The Blind leading the Blind...
    Currently working through:
    "C++ Primer Plus"

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    You didn't get any errors? Hmm....

    I get three [Linker errors] that i mentioned earlier... and I'm using Dev-C++ too. that's weird. And you just copied and pasted directly what I have here, and it ran?

  5. #5
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    That's right, but I think I may have a problem with my compiler(s) so don't take what I've posted as meaning there are no errors.
    Any help I give may be classified as:
    The Blind leading the Blind...
    Currently working through:
    "C++ Primer Plus"

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    how could there be something wrong with a compiler? that just seems to weird.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    jitterbug, I think there is some confusion of terminology here that is tripping you up a bit. Creation of an executable is a two step process. The first is compilation of you source code into object code. Your source code contains references to functions such as printf and scanf and others but you don't write code for those do you? The actual code for those functions exist in libraries. The code for these functions is combined with your compiled object file to create a final executable in a step called linking. The linker's jobs is to basically figure out which functions you've called that you haven't defined already and to try and find those functions in the code libraries provided with your development environment's installation (or in other compiled object files that are a part of a larger overall project). The code you have provided compiles fine, that is, the compiler successfully produces an object file from the code. The linker step is what is failing, that is, it cannot resolve the above mentioned function calls because you either didn't provide the source for them (I don't see any such functions defined in the code you've provided in the above sample - prototyped yes, but defined no) or have not told the linker where to find the code in a library of some kind or in another object file.

    [edit]The 2nd sample you provided compiles but does not link as it does not contain the code for the functions mentioned (this I think may be the one BuzzBuzz said compiled fine. The 1st code sample does contain the code for those functions and so should compile and link. If you are getting linker errors with the 2nd code sample, make sure the code you are working with also contains the definitions for those functions that are causing the linker errors.[/edit]
    Last edited by hk_mp5kpdw; 04-01-2009 at 09:41 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2009, 09:54 AM
  2. Program Control, HELP appreciated.
    By Astra in forum Windows Programming
    Replies: 7
    Last Post: 01-01-2007, 06:59 AM
  3. Help Appreciated
    By cboard_member in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2005, 07:31 AM
  4. C++ Programs -- Any input appreciated
    By Kyoto Oshiro in forum Game Programming
    Replies: 0
    Last Post: 02-27-2002, 11:22 PM
  5. I need help..anything will be appreciated
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2001, 10:55 AM

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