GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Apr-2009, 17:13
jitterbug jitterbug is offline
New Member
 
Join Date: Apr 2009
Posts: 2
jitterbug is on a distinguished road

Help with 2D arrays.


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.

CPP / C++ / C 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 based on option # input), 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.
Last edited by LuciWiz : 01-Apr-2009 at 17:24. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 01-Apr-2009, 17:32
jitterbug jitterbug is offline
New Member
 
Join Date: Apr 2009
Posts: 2
jitterbug is on a distinguished road

Re: Help with 2D arrays.


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.


CPP / C++ / C 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;
}
  #3  
Old 02-Apr-2009, 08:16
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Help with 2D arrays.


CPP / C++ / C Code:
               
//begin option 1 here.
if (option == 1) {

/* REMOVE THESE FROM THE MIDDLE OF YOUR FUNCTION.
   THEY BELONG AT THE TOP OF THE FILE.
*/        
void addtopixels(int** imgtemp, int value);
void  writeoutpic(char* fileName, int** imgtemp);
int** readpic(char* fileName);
/**********************************/

printf("Enter image filename: ");
First off, function prototypes don't go in the middle of a function. You already have them defined at the top of the file so just remove them here.

As far as your linker errors, it looks like the linker can't find the function implementations. You removed them from the file you were given from your first post. Put them back and then recompile.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
Arrays as function arguments - return arrays from functions pisuke C Programming Language 2 25-Jul-2007 12:03
Dynamic vs Static Arrays WaltP Miscellaneous Programming Forum 5 16-Feb-2006 16:54
I am reviewing Arrays and need help converting some strings to arrays jenmaz C Programming Language 22 23-Nov-2004 00:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:56.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.