Thursday, January 3, 2013

CHANNEL SAPARATION ( RGB )...

READ AND COPY 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*);

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

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


void main()

FILE *f1=NULL;
FILE* f2=NULL;
FILE* f3=NULL;
FILE* f4=NULL;

f1=fopen("C:\\Users\\shubham\\Downloads\\parrot-big.bmp","rb");
f2=fopen("C:\\Users\\shubham\\Downloads\\parrot-copyred.bmp","wb ");
f3=fopen("C:\\Users\\shubham\\Downloads\\parrot-copyblue.bmp","wb ");
f4=fopen("C:\\Users\\shubham\\Downloads\\parrot-copygreen.bmp","wb ");



char Red;

if(f1==NULL)
{
cout<<"file is empty";
}
else
{
unsigned int StartingImageAddress =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:  "<<StartingImageAddress<<endl;
cout<<"Width of Image in Bytes:  "<<WidthInBytes<<endl;
cout<<"height of Image in Bytes:  "<<HeightInBytes<<endl;

ReadCopyHeaderred(f1, f2);
RedColorArrayf(f1,f2, RedColorArray, StartingImageAddress);
        ReadCopyHeaderblue(f1, f3);
BlueColorArrayf(f1,f3, BlueColorArray,StartingImageAddress);
ReadCopyHeadergreen(f1, f4);
GreenColorArrayf(f1,f4, GreenColorArray, StartingImageAddress);

free(RedColorArray);
   free(BlueColorArray);
   free(GreenColorArray);
}

fclose(f1);
fclose(f2);
getch();
}


int ReadCopyHeaderred(FILE* f1,FILE* f2)//copy
{
char character;
int c=0;
fseek(f1,0,SEEK_SET);
character=(char)fgetc(f1);

for(int m=0; m<53; m++)
{
(char)putc(character,f2);
character=(char)fgetc(f1);
cout<<character;
c++;
}
cout<<"count:  "<<c<<endl;

return(character);
}

int ReadCopyHeaderblue(FILE* f1,FILE* f3)//copy
{
char character;
int c=0;
fseek(f1,0,SEEK_SET);
character=(char)fgetc(f1);

for(int m=0; m<53; m++)
{
(char)putc(character,f3);
character=(char)fgetc(f1);
cout<<character;
c++;
}
cout<<"count:  "<<c<<endl;

return(character);
}


int ReadCopyHeadergreen(FILE* f1,FILE* f4)//copy
{
char character;
int c=0;
fseek(f1,0,SEEK_SET);
character=(char)fgetc(f1);

for(int m=0; m<53; m++)
{
(char)putc(character,f4);
character=(char)fgetc(f1);
cout<<character;
c++;
}
cout<<"count:  "<<c<<endl;

return(character);
}



int StartingAddressofBMP(FILE*f1)
{

rewind(f1);

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);
return(wdtbmp);
}

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

signed int hgtbmp;
fread(&hgtbmp,4,1,f1);
return(hgtbmp);
}


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

fread(&ch, 1, 1, f1);
fwrite(&ch,1,3,f2);

while(!feof(f1))
{
RedColorArray[j] =ch;

c++;

StartingImageAddress = StartingImageAddress + 3;
fseek(f1,StartingImageAddress,SEEK_SET);
        fread(&ch, 1, 1, f1);
fwrite(&ch,1,3,f2);
}
cout<<endl<<"Size Of ReD Image Data:  "<<c<<endl;
return(ch);
}


int BlueColorArrayf(FILE* f1,FILE* f3, unsigned char* BlueColorArray, int StartingImageAddress)
{
StartingImageAddress += 1 ;
    fseek(f1,StartingImageAddress,SEEK_SET);
fseek(f3,StartingImageAddress,SEEK_SET);
char ch1;
int k=0;
int c=0;

fread(&ch1,1,1,f1);
fwrite(&ch1,1,3,f3);
while(!feof(f1))
{
BlueColorArray[k] =ch1;
c++;
StartingImageAddress=StartingImageAddress + 3;
fseek(f1,StartingImageAddress,SEEK_SET);
fread(&ch1, 1, 1, f1);
fwrite(&ch1,1,3,f3);
}
cout<<"Size Of Blue Image Data:  "<<c<<endl;

return(ch1);
}


int GreenColorArrayf(FILE* f1,FILE* f4, unsigned char* GreenColorArray,int StartingImageAddress)
{
StartingImageAddress +2;
fseek(f1,StartingImageAddress,SEEK_SET);
fseek(f1,StartingImageAddress,SEEK_SET);
char ch2;
int l=0;
int c=0;

fread(&ch2,1,1,f1);
fwrite(&ch2,1,3,f4);
while(!feof(f1))
{
GreenColorArray[l] =ch2;
c++;
StartingImageAddress=StartingImageAddress + 3;
fseek(f1,StartingImageAddress,SEEK_SET);
fread(&ch2, 1, 1, f1);
fwrite(&ch2,1,3,f4);

}
cout<<"Size Of Green Image Data:  "<<c;
return(ch2);
}

No comments:

Post a Comment