Thread: Storing color components in 1D arrays

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    16

    Question Storing color components in 1D arrays

    Ive read a .BMP file correctly, getting the width height and image offset data where the image files begin. i now need to read the struct containing the imagecomponents of the image (blue green and red). Then i need to create three dynamic arrays to store each colour into a seperate dynamic arrays, using that i have to find the avarage values of each colour.

    Im not sure how to store it into the input buffer and then from there into three dynamic arrays... this is what ive done so far, its a very rough draft so just ignore anything extra thats in there... novice programmer who would take any advice given, thanks

    Code:
    #include <stdio.h> 
     
    #pragma pack(push, 1); 
    void* malloc(); 
    void* calloc(); 
    void free(void *ptr); 
     
    /* structs  */ 
    typedef struct 
    { 
    unsigned char fileMarker1; /* 'B' */ 
    unsigned char fileMarker2; /* 'M' */ 
    unsigned int bfSize; 
    unsigned short unused1; 
    unsigned short unused2; 
    unsigned int imageDataOffset; /* Offset to the start of image data */ 
    }FILEHEADER; 
     
    typedef struct 
    { 
    unsigned int biSize; 
    int width; /* Width of the image */ 
    int height; /* Height of the image */ 
    unsigned short planes; 
    unsigned short bitPix; 
    unsigned int biCompression; 
    unsigned int biSizeImage; 
    int biXPelsPerMeter; 
    int biYPelsPerMeter; 
    unsigned int biClrUsed; 
    unsigned int biClrImportant; 
    }INFOHEADER; 
     
    typedef struct 
    { 
    unsigned char b; /* Blue value */ 
    unsigned char g; /* Green value */ 
    unsigned char r; /* Red value */ 
    }IMAGECOMPONENT; 
     
    
    int main(int argc, char *argv[] ) 
     {    
     #pragma pop; 
     FILEHEADER fileHeader;  /* make an instance of the fileheader struct */ 
      
     FILE *outbin; 
     char image[256]; 
     FILEHEADER data; 
     INFOHEADER info; 
     IMAGECOMPONENT daaa; 
    
             printf("Filename: "); 
         scanf("%s", &image); 
      
          FILE *fp= fopen(image, "rb"); 
             
     
            fread(&data, sizeof(FILEHEADER), 1, fp); 
            fread(&info, sizeof(INFOHEADER), 1, fp); 
            fread(&daaa, sizeof(IMAGECOMPONENT), 1, fp); 
          
            if (fp == NULL)  
            { 
            printf("The file cannot be opened.\n"); 
            } 
            else  
            { 
            printf("filemarker1 = %c\n", data.fileMarker1); 
            printf("filemarker2 = %c\n", data.fileMarker2); 
            printf("Image offset data %d\n", data.imageDataOffset); 
            printf("\nSize of header: %d", sizeof(FILEHEADER)); 
            printf("\nSize of info header: %d\n", sizeof(INFOHEADER)); 
            printf("Size of image componant: %d\n\n", sizeof(IMAGECOMPONENT)); 
            printf("Image height : %d picture elements\n", info.height); 
            printf("Image width : %d picture elements\n", info.width); 
            } 
             
            padding = info.width % 4;  
            if(padding != 0 )    
            {   
            padding = 4 - padding; 
            } 
             
             
        fseek(fp,data.imageDataOffset,SEEK_SET); 
        IMAGECOMPONENT *ptr; 
        int i; 
        int j; 
       /* allocate to buffer */
        ptr = malloc( sizeof(IMAGECOMPONENT) ); 
        if(ptr == NULL) 
        { 
        fprintf(stderr, "memory allocation failure"); 
        return (-1); 
        } 
        else 
        { 
        printf("memorry allocation succuss\n"); 
        } 
         
            /* creates 2d array */ 
         
        IMAGECOMPONENT *matrixONE;     
        IMAGECOMPONENT *matrixTWO; 
        IMAGECOMPONENT *matrixTHREE; 
     
          matrixONE = calloc((info.height*info.width), sizeof(unsigned char) ); 
         if(matrixONE == NULL) 
          { 
         fprintf(stderr, "\nmemory allocation failure"); 
         return (-1); 
          } 
          else 
          { 
          fprintf(stderr, "\nmemory allocation success matrixONE"); 
          } 
           
     
          matrixTWO = calloc((info.height*info.width), sizeof(unsigned char) ); 
         if(matrixTWO == NULL) 
          { 
         fprintf(stderr, "\nmemory allocation failure"); 
         return (-1); 
          } 
          else 
          { 
          fprintf(stderr, "\nmemory allocation success matrixTWO"); 
          } 
     
          matrixTHREE = calloc((info.height*info.width), sizeof(unsigned char) ); 
         if(matrixTHREE == NULL) 
          { 
         fprintf(stderr, "\nmemory allocation failure"); 
         return (-1); 
          } 
          else 
          { 
          fprintf(stderr, "\nmemory allocation success matrixTHREE\n"); 
          } 
          
     
         fclose (fp); 
      
            
           
     
     
         
    return 0; 
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    FILE *fp= fopen(image, "rb");                   fread(&data, sizeof(FILEHEADER), 1, fp);         fread(&info, sizeof(INFOHEADER), 1, fp);         fread(&daaa, sizeof(IMAGECOMPONENT), 1, fp);               if (fp == NULL)
    Shouldn't you be checking to see whether fp is open before reading from it?

    If it doesn't open, there is a lot of instruction that you don't want to run.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    void* malloc();
    void* calloc();
    void free(void *ptr);
    Don't do this.

    Use
    #include <stdlib.h>


    > matrixONE = calloc((info.height*info.width), sizeof(unsigned char) );
    You have a pointer to a structure, so you need to allocate for the size of this structure, not the size of a single char.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    16
    Thanks for the replies guys, @Click_here there is an if statement checking to see if fp exists there, and it prints out the values correctly so it does read it.

    @Salem thankyou for that im not sure that was right, im not sure how im meant to store each individual colour into the arrays thorugh... blue in matrixONE green in matrixTWO etc

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by igor
    there is an if statement checking to see if fp exists there, and it prints out the values correctly so it does read it.
    You might want to check that in the code above, because you call fread() BEFORE you check if fp is NULL. I don't doubt that you can read it, but make a typo when calling the program and you might be in a bit of trouble

    And if it is NULL, exit the program, or prompt for the input again. You are just printing a warning and then trying to use fp as if the NULL was never found
    Quote Originally Posted by igor
    Code:
    fread(&data, sizeof(FILEHEADER), 1, fp); 
    fread(&info, sizeof(INFOHEADER), 1, fp); 
    fread(&daaa, sizeof(IMAGECOMPONENT), 1, fp); 
          
    ...
    
    
    
    
    fseek(fp,data.imageDataOffset,SEEK_SET); 
    
    
    ...
    
    
    fclose (fp);
    Also, your memory allocations need a good fp open


    You should have something like this in your code -
    Code:
    FILE *fp;
    
    if (fopen(image, "rb") == NULL)
    {
        fputs("Error: Can't open file", stderr);
        return -1;
    }
    That way you can exit if there is a typo on input
    Last edited by Click_here; 09-10-2012 at 10:42 PM.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    1
    Hey was wondering if you were able to get the dynamic arrays working and if you were able to find the averages and how you did so. i'm new to programming and am try to understand it, your code is a good example but the last bit would help me understand the dynamic arrays and was wondering how you came up with the type-def structs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with storing arrays
    By tcumech in forum C Programming
    Replies: 12
    Last Post: 05-06-2010, 02:34 AM
  2. Arrays and storing values
    By atlas07 in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2010, 08:46 PM
  3. storing strings in arrays
    By smp in forum C Programming
    Replies: 6
    Last Post: 12-16-2008, 09:37 AM
  4. Storing components of a complex number
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-19-2007, 01:37 PM
  5. Storing values in arrays
    By Ripper1 in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2003, 11:04 AM

Tags for this Thread