I have an image (frame.bmp, uncompressed, resolution 640*480, 3bit depth, 900KB size) stored at C:\Frame.bmp. I want to load it and put it in my program as an array a[N][M], N=479, M=639. After that to modify its elements (e.g thresholding) and create a new array B[479][639]. Finally, to store it in the path as C:\Frame2.bmp and display the new image, lets say with paint application.
My program is:
#include <stdio.h>
#include <stdlib.h>
unsigned int** bufalloc(int N,int M);
int threshold(unsigned int** b,unsigned int** a,unsigned int T,int N,int M);
int fromdisk(char* imfile,unsigned int** a,int N,int M);
int todisk(char* imfile,unsigned int** a,int N,int M);
unsigned int** af;
unsigned int** bf;
int main(){
int f1,f2,f3;
af = bufalloc(479,679);
if(af==NULL) return(-100);
bf = bufalloc(479,1024);
if(bf==NULL) return(-100);
f1 = fromdisk("F.bmp",af,479, 679);
f2 = threshold(bf,af,40, 479, 679);
f3 = todisk("tF.bmp",bf, 479, 679);
return(0);
}
///////////////////////////////////////////////////////////////
int fromdisk(char* imfile,unsigned int** a,int N,int M){
int i,j;
unsigned int* buc;
FILE* fp;
if (a==NULL) return(-1);
if (N<0 || M<0) return(-21);
fp = fopen(imfile, "rb");
if (fp==NULL) return(-300);
buc = (unsigned int*)malloc(M*sizeof(unsigned int));
if(buc==NULL) return(-10);
for(i=0; i<N; i++){
fread(buc,sizeof(unsigned int),M,fp);
for(j=0; j<M; j++)
a[i][j] = buc[j];
}
free(buc);
fclose(fp);
return(0);
}
///////////////////////////////////////////////////////////
int todisk(char* imfile,unsigned int** a,int N,int M){
int k,l;
unsigned int* buc;
FILE* fp;
fp = fopen(imfile, "wb");
if (fp==NULL) return(-300);
buc = (unsigned int*)malloc(M*sizeof(unsigned int));
if(buc==NULL) return(-10);
for(k=0; k<N; k++){
for(l=0; l<M; l++)
buc[l] = a[k][l];
fwrite(buc,sizeof(unsigned int),M,fp);
}
free(buc);
fclose(fp);
return(0);
}
/////////////////////////////////////////////////////////////////////////////////////
int threshold(unsigned int** b,unsigned int** a,unsigned int T,int N,int M){
int i,j;
for(i=0; i<N; i++){
for(j=0; j<M; j++){
if (a[i][j]<T)
b[i][j]=0;
else b[i][j]=1;
}
}
return(0);
}
//////////////////////////////////////////
unsigned int** bufalloc(int N,int M){
unsigned int** m;
int r;
m = (unsigned int**)malloc(N*sizeof(unsigned int*));
for(r=0; r<N; r++){
m[r] = (unsigned int*)malloc((M*sizeof(unsigned int)));
if(m[r]==NULL)
return(NULL);
}
return(m);
}
But, it does not work corectly (the new image is now 991KB and cannot open)! What happens with the header? Any idea how it can work?