Thursday, January 3, 2013

CHANNEL SAPARATION (RGB)

READING OF 24 BIT BMP IMAGE IN RGB ( RED, BLUE & GREEN ):- 


#include<iostream> 
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>

using namespace std;

int getWidthInPixels(FILE*);
int getHeightInPixels(FILE*);
int StartingAddressofBMP(FILE*);
//void getImageData(FILE*, unsigned char*, unsigned char*, unsigned char*, unsigned char*);

int WidthInPixels=0, HeightInPixels=0;
int WidthInBytes=0, HeightInBytes=0;
int forColorMatrixHeight=0; 
int forColorMatrixWidth=0;

int RedColorArrayf(FILE*, unsigned char*);
int GreenColorArrayf(FILE*, unsigned char*);
int BlueColorArrayf(FILE*, unsigned char*);

void main()

FILE *f1=NULL;

f1=fopen("C:\\Users\\shubham\\Downloads\\red24bit.bmp","rb");

//unsigned char* imageData=(unsigned char*)malloc(HeightInBytes*WidthInBytes);

unsigned int StartingAddress;
char Red;

if(f1==NULL)
{
cout<<"file is empty";
}
else
{
StartingAddress =StartingAddressofBMP(f1);
WidthInPixels=getWidthInPixels(f1);
WidthInBytes=3*WidthInPixels;
HeightInPixels=getHeightInPixels(f1);
HeightInBytes=3*HeightInPixels;
forColorMatrixHeight=HeightInBytes/3;
forColorMatrixWidth=WidthInBytes/3;

unsigned char* RedColorArray= (unsigned char*)malloc(forColorMatrixHeight * forColorMatrixWidth);
   unsigned char* BlueColorArray= (unsigned char*)malloc(forColorMatrixHeight * forColorMatrixWidth);
   unsigned char* GreenColorArray= (unsigned char*)malloc(forColorMatrixHeight * forColorMatrixWidth);



cout<<"Starting Address Of BMP:  "<<StartingAddress<<endl;
cout<<"Width of Image in Bytes:  "<<WidthInBytes<<endl;
cout<<"height of Image in Bytes:  "<<HeightInBytes<<endl;

RedColorArrayf(f1, RedColorArray);
BlueColorArrayf(f1, BlueColorArray);
GreenColorArrayf(f1, GreenColorArray);

//cout<<"Red Image Data:  "<<Red;
free(RedColorArray);
   free(BlueColorArray);
   free(GreenColorArray);
}

//free(imageData);

fclose(f1);
getch();
}

int StartingAddressofBMP(FILE*f1)
{

rewind(f1);

//int saddress;

fseek(f1,10,0);

unsigned int saobmp;
fread(&saobmp,4,1,f1);

return(saobmp);
}


int getWidthInPixels(FILE*f1)
{

rewind(f1);
fseek(f1,18,0);
signed int wdtbmp;
fread(&wdtbmp,4,1,f1);
//cout<<"Width of dib in pixels:  "<<wdtbmp<<endl;
return(wdtbmp);
}

int getHeightInPixels(FILE*f1)
{
rewind(f1);
fseek(f1,22,0);

signed int hgtbmp;
fread(&hgtbmp,4,1,f1);
//cout<<"heigh of dib in pixels:  "<<hgtbmp<<endl;
return(hgtbmp);
}


int RedColorArrayf(FILE* f1, unsigned char* RedColorArray)
{
int StartingImageData=54;
fseek(f1,StartingImageData,SEEK_SET);
char ch;
int j=0;
int c=0;

fread(&ch, 1, 1, f1);
while(!feof(f1))
{
RedColorArray[j] =ch;

c++;

StartingImageData = StartingImageData + 3;
fseek(f1,StartingImageData,SEEK_SET);
        fread(&ch, 1, 1, f1);

}
cout<<"Size Of ReD Image Data:  "<<c<<endl;
return(ch);
}


int BlueColorArrayf(FILE* f1, unsigned char* BlueColorArray)
{
int StartingImageData=55;
    fseek(f1,StartingImageData,SEEK_SET);
char ch1;
int k=0;
int c=0;
//unsigned char* RedColorData= (unsigned char*)malloc(forColorMatrixHeight * forColorMatrixWidth );

fread(&ch1,1,1,f1);
while(!feof(f1))
{
BlueColorArray[k] =ch1;
c++;
StartingImageData=StartingImageData+ 3;
fread(&ch1, 1, 1, f1);

}
cout<<"Size Of Blue Image Data:  "<<c<<endl;
return(ch1);
}


int GreenColorArrayf(FILE* f1, unsigned char* GreenColorArray)
{
int StartingImageData=56;
fseek(f1,StartingImageData,SEEK_SET);
char ch2;
int l=0;
int c=0;
//unsigned char* RedColorData= (unsigned char*)malloc(forColorMatrixHeight * forColorMatrixWidth );

fread(&ch2,1,1,f1);
while(!feof(f1))
{
GreenColorArray[l] =ch2;
c++;
StartingImageData=StartingImageData+ 3;
fread(&ch2, 1, 1, f1);
}
cout<<"Size Of Green Image Data:  "<<c;
return(ch2);
}

No comments:

Post a Comment