Im getting an error and i cant figure out why. please help.

This is a discussion on Im getting an error and i cant figure out why. please help. within the C Programming forums, part of the General Programming Boards category; hey im getting an error in this program and i just cant get it to compile. its a program to ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    66

    Im getting an error and i cant figure out why. please help.

    hey im getting an error in this program and i just cant get it to compile. its a program to modify images like edge detection and adding brightness and overlaying two images. im getting an error when i call a function that i call in different parts of the program that works fine but on the new part i just added i get an error.
    the new function i added adds a disc of brightness to the image of a radius that the user inputs.
    please let me know if you can help.





    Code:
        
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
    
    #define  BUFFER_SIZE  70
    #define  TRUE          1
    #define  FALSE         0
    
    int    sel;
    int    val = 500;
    int**  img;
    int**  img2;
    int**  imgout;
    int**  temppicx, temppicy;
    int**  img,sobelout;
    int    numRows;
    int    numCols;
    int    maxVal;
    FILE*  fo1;
    
    void   sel1();
    void   sel2();
    void   sel3();
    void   sel4();
    void   add_disc(int** imgtemp, int val);
    void   addtopixels(int** imgtemp, int value);
    void   subtractfrompixels(int** img, int** img2, int** imgout);
    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);
    void   sobelfunc(int** pic, int** edges, int** tempx, int** tempy);
    
    
    
    int main() {
        
        char fileName[BUFFER_SIZE];
        int i,j,rows,cols;
        char ci;
        int value1;
        int num = 0; 
        
            printf("Please choose one of the following options:\n\n");
            printf("\t1 - Modify Image Brightness\n");
            printf("\t2 - Subtract Images\n");
            printf("\t3 - Sobel Edge Highlighter\n");
            printf("\t4 - Add Brightness Disk\n\n");
            printf("\t5 - EXIT\n\n");
            printf("\tOption: ");
            scanf("%d", &sel);                            
            // Modify Brightness - calls function 'sel1'
            if (sel == 1)             
                sel1();                
            // Subtract Images - calls function 'sel2'
            if (sel == 2) 
                sel2();
            // Sobel Edge Highlighter - calls function 'sel'3
            if (sel == 3)
                sel3();
            // Brightness disk
            if (sel == 4) 
                sel4();
            // exit
            if (sel == 5)
                return(0);
                
        
    }
    
    // Option 1 
    void sel1() {
        
        char* fileName;
        
            printf("\nEnter image filename: ");    
            scanf("%s", fileName);              
            img = readpic(fileName);            
            printf("Successfully read image file '%s'\n\n", fileName);
            printf("Enter value to brighten image by: ");
            scanf("%d", &val);        
            // Adds user inputted value of brightness to the entire image    
            addtopixels(img,val);            
            writeoutpic("out1.pgm",img);            
            free(img);            
            img = NULL;    
            system("pause");                            
    }
    
    // Option 2   
    void sel2() {
        
        char*  fileName;
        int**  img;
        int**  img2;
        int**  imgout;    
        
            printf("\nEnter first image filename: ");
            scanf("%s", fileName);              
            img = readpic(fileName);
            printf("Successfully read image file '%s'\n\n", fileName);       
            printf("Enter image to be subtracted filename: ");
            scanf("%s", fileName);              
            img2 = readpic(fileName);
            printf("Successfully read image file '%s'\n\n", fileName);                                       
            // Subtracts image 2 from image 1 
            subtractfrompixels(img,img2,imgout);                                    
            system("pause");                               
    }
    
    // Option 3
    void sel3() {
        
        char*  fileName;
        int**  temppicx;
        int**  temppicy;
        int**  img;
        int**  sobelout;
        
            printf("\nEnter image filename: ");
            scanf("%s", fileName);              
            img = readpic(fileName);            
            printf("Successfully read image file '%s'\n\n", fileName);        
            sobelout= setImage();
            temppicx= setImage();
            temppicy= setImage();
            // function that highlights edges in an image
            sobelfunc(img,sobelout,temppicx,temppicy);
            writeoutpic("out1.pgm",sobelout);            
            free(img);            
            img = NULL;   
            system("pause");                            
    }
    
    void sel4() {
        int     i=0;
        int     j=0;
        char*   fileName;
        int**   img;
        
            printf("\nEnter image filename: ");
            scanf("%s", fileName);              
            img = readpic(fileName);            
            printf("Successfully read image file '%s'\n\n", fileName);                                         
            printf("numCols = %d, numRows = %d\n", numCols, numRows);
            
        
            add_disc(img, val);
            
            writeoutpic("out1.pgm",imgout);            
            free(img);            
            img = NULL; 
            system("pause");
          
        
    }
    
    
    void add_disc(int** imgtemp, int val) {
        
        int i,j;
        int k;
        int** img;
          
            printf("Please enter a radius: \n");
            scanf("%d", &val);
            printf("Enter value to brighten image by: ");              
            scanf("%d", k);
            img = setimage();
            for(i = 0; i < numRows; i++) {
                for(j = 0; j < numCols; i++) {
                    if (val^2 >= ((i-(numRows/2))^2+(j-(numCols/2))^2)) {
                        img[i][j] += k;
                    }
                }
            }    
    }
    
    void addtopixels(int** imgtemp, int value) {
          
        int i,j;
            
            for (i=0;i<numRows;i++) {
                for (j=0;j<numCols;j++) {
                    imgtemp[i][j] += val;
                }
            }
    }
    
    void subtractfrompixels(int** img, int** img2, int** imgout) {
            
        int i,j;    
               
            // Allocates memory to store new image
            imgout = setImage();
            for (i=0;i<numRows;i++) {
                for (j=0;j<numCols;j++) {
                    // Subtracts image 2 from image 1
                    imgout[i][j] = abs(img[i][j] - img2[i][j]);
                }
            }
            writeoutpic("out1.pgm",imgout);            
            free(img);
            img  = NULL;            
            img2 = NULL;
    }        
    
    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);
                }
            }
    }
    
    void sobelfunc(int** pic, int** edges, int** tempx, int** tempy) {
    
        int maskx[3][3] = {{-1,0,1},{-2,0,2},{-1,0,1}};
        int masky[3][3] = {{1,2,1},{0,0,0},{-1,-2,-1}};
        int maxival;      
        int i,j,p,q,mr,sum1,sum2;
        double threshold;
            
            mr = 1;
            for (i=mr;i<numRows-mr;i++) {
                for (j=mr;j<numCols-mr;j++) {          
                    sum1 = 0;
                    sum2 = 0;
                    for (p=-mr;p<=mr;p++) {             
                        for (q=-mr;q<=mr;q++) {                
                            sum1 += pic[i+p][j+q] * maskx[p+mr][q+mr];
                            sum2 += pic[i+p][j+q] * masky[p+mr][q+mr];
                        }
                    }
                    tempx[i][j] = sum1;
                    tempy[i][j] = sum2;
                }
            }
            maxival = 0;
            for (i=mr;i<numRows-mr;i++) {
                for (j=mr;j<numCols-mr;j++) {
                    edges[i][j]=(int) (sqrt((double)((tempx[i][j]*tempx[i][j]) +
                    (tempy[i][j]*tempy[i][j]))));
                    if (edges[i][j] > maxival)
                        maxival = edges[i][j];
                }
            }
            for (i=0;i<numRows;i++) { 
                for (j=0;j<numCols;j++) {
                 edges[i][j] = ((edges[i][j]*1.0) / maxival) * 255;                       
                }
            }
    }

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    That's a lot of source code to look through - what is your exact error message and line number?

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ominub View Post
    hey im getting an error in this program
    You obviously have been programming for at least a few minutes, so why don't you mention WHAT error you are getting (a segfault? some message from the compiler?). You could go further to pinpoint the problem and you know it. I know that having the sky reign crap on your head at certain points can be devastating, but honestly, I guarantee the more specific you can be with your question, and the more detail you can go into with it (posting a few hundred lines of extraneous code is NOT "more detail") the more satisfied YOU will be the advice you receive.

    While I'm busy on my soapbox and not pouring thru your code, I would add (honest again) that MOST of the questions I intend to ask on cboard actually get solved in the "post thread/compose message" box BEFORE I post, because I practice what I'm preaching -- usually, once I get it together enough to formulate a plea for help that I think is appropriate for OTHER people to take seriously (maybe that includes writing a short, concise test program to represent the issue as I perceive it), and then I see my own words in the preview, again, honestly, MOST of the time I suddenly see the answer myself and don't have to press submit.

    One problem I see immediately looking at add_disc is that you call scanf twice in a row to retrieve an integer, and generally speaking when not retrieving a string scanf will leave a \n in the console (stdin) buffer*, which means the second scanf will accept that newline as an integer (the ascii value of \n, 10) so you won't get a chance to enter the second number.

    But I'm not going to compile this, and I don't actually know what your "error" (problem?) is.

    *because actually the data is the number, then a newline because you must push "enter".
    Last edited by MK27; 04-10-2009 at 07:12 PM.
    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

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    i am not on my own computer right now so i cant tell you the exact error but i get 2 errors. i get an error in function add_disc where i call setimage() and i get a warning message. before i added the sel4() function the program would compile and run perfect.

    I apologize for assuming it would be easier to post the entire code so you could copy it into your own compiler. but i dont see a reason to be a dick. if you dont want to help, hit the back button. than you, bye now. if you have nothing better to do than complain, why not just copy and paste my code into your compiler and see the error yourself. you obviously had enough time to articulate a long-winded rant. i only need one scanf there thank you.

    if there is anyone who wouldnt mind copying my code into their compiler and attempt to fix it i would really appreciate it. the code is not very advanced. thank you in advance for you help.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    line 176
    Code:
            imgtemp  = setImage();
    see...ask a more specific question, get a more specific answer
    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

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    stupid mistake... thank you for your help.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    well i have another problem now. if you dont mind taking another look, the program crashes after it reads in the radius. i also changed this part of the code a little.

    Code:
    void sel4() {
        
        char*   fileName;
        int**   img;
        
            printf("\nEnter image filename: ");
            scanf("%s", fileName);              
            img = readpic(fileName);            
            printf("Successfully read image file '%s'\n\n", fileName);                                         
            printf("numCols = %d, numRows = %d\n", numCols, numRows);       
        
            add_disc(img, val);        
            
            system("pause");  
    }
    
    
    void add_disc(int** imgtemp, int val) {
        
        int i,j;
        int k;
        int** imgout;
          
            printf("Please enter a radius: ");
            scanf("%d", &val);
            printf("\nEnter value to brighten image by: ");              
            scanf("%d", &k);
            imgout = setImage();
            for(i = 0; i < numRows; i++) {
                for(j = 0; j < numCols; i++) {
                    if (val^2 >= ((((i-(numRows/2))^2) + (j-(numCols/2))^2))) {
                        imgout[i][j] += k;
                    }
                }
            }    
            writeoutpic("out1.pgm",imgout);            
            free(img);            
            img = NULL; 
    }

  8. #8
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by ominub View Post
    Code:
                for(j = 0; j < numCols; i++) {
    are you sure?
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    ahh thanks. another stupid mistake.

    well, i fixed that and it all runs... but the image isnt coming out like it is supposed to.

    just to be thorough, this program reads in a black and white image and sets the brightness values (0-255 with 0 being black and 255 being white) to an int** array.

    the add_disc is supposed to add a disc of brightness to the image. the if statement

    Code:
    if (val^2 >= ((((i-(numRows/2))^2) + (j-(numCols/2))^2)))
    is the equation of a circle. it should only allow the brightness to be added to a circle of a radius that the user inputs. for some reason, the whole image is being brightened. any idea why?

Popular pages Recent additions subscribe to a feed

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