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;
}