All pastes #770405 Raw Edit

Ron Fernandes

public text v1 · immutable
·published 2007-11-12 07:24 UTC
rendered paste body
#include <iostream>
using namespace std;
const int SIDE=500;
struct rgbtriple 
{
	unsigned char b;
	unsigned char g;
	unsigned char r;
};
int rgbToInt(rgbtriple t);
rgbtriple intToRgb(int v);
int readBMPheader(FILE *fp, int *pwidth, int *pheight);
void writeBMPheader(FILE *fp, int width, int height);
int readrgbtriple(FILE *fp,struct rgbtriple *pt);
void writeRgbTriple(FILE *fp,rgbtriple t);
void reorderBytes(char * data,int n);

int readBMPFile(char fileName[],rgbtriple image[SIDE][SIDE], int *pwidth, int *pht);
void writeBMPFile(char fileName[],int width,int height,rgbtriple image[SIDE][SIDE]);
void processImage(rgbtriple image[SIDE][SIDE],int width,int height);
void printTriple(rgbtriple t);
int main () 
{
    rgbtriple ourimage[SIDE][SIDE];
	int width,ht;
	char fileName[40];
	char outFileName[40];
	cout<<"what input file? ";
	cin>>fileName;
	cout<<"what output file? ";
	cin>>outFileName;
	if (!readBMPFile(fileName, ourimage, &width, &ht))
	{
		cout<<"Read failed for "<<fileName<<". Try again."<<endl;
		return 1;
	}
	
	processImage(ourimage,width,ht); //do something to the image
	
	writeBMPFile(outFileName,width,ht,ourimage);
	system("PAUSE");
	return 0;
}
void printTriple(rgbtriple t)
{
	cout<<"r: "<<(int)t.r<<" g: "<<(int)t.g<<" b: "<<(int)t.b<<endl;
}
int readBMPFile(char fileName[],rgbtriple image[SIDE][SIDE], int *pwidth,int * pht)
{
	FILE *fp;
	unsigned char padding[3];
	int pad;
	rgbtriple t;
	fp=fopen(fileName,"rb");
	if (!fp)
	{
		cout<<"bad open"<<endl;
		return 0;
	}
	if(!readBMPheader(fp,pwidth,pht))
		return 0;
	pad=(3*(*pwidth))%4;//the % here is the remainder operator
	if(pad!=0)
		pad=4-pad; //if there is a remainder, need to pad out to multiple of 4
	for(int y=0;y<*pht;y++)//there are ht rows of dots
	{
		for(int x=0;x<*pwidth;x++) //there are width dots in each row
		{
			if(!readrgbtriple(fp,&t))
				return 0;
			image[x][y]=t;
			
		}          
		if(pad)fread(&padding,pad,1,fp); //read and ignore padding
	}
	fclose(fp);
	return 1;
}
void writeBMPFile(char fileName[],int width,int ht,rgbtriple image[SIDE][SIDE])
{
	FILE *fp;
	int pad;
	unsigned char padding[3];
		fp=fopen(fileName,"wb");
	writeBMPheader(fp,width,ht);
	pad=(3*width)%4;

	for(int y=0;y<ht;y++)//there are ht rows of dots
	{
		for(int x=0;x<width;x++) //there are width dots in each row
			writeRgbTriple(fp,image[x][y]);
		if(pad)fwrite(&padding,pad,1,fp);
	}
	fclose(fp);
}


int readrgbtriple(FILE *fp,struct rgbtriple *pt)
{
	if(fread(pt,1,3,fp)!=3)
	{
		printf("failed to read triple\n");
		return 0;
	}
	return 1;
}
void writeRgbTriple(FILE *fp,rgbtriple t)
{
	fwrite(&t,1,3,fp);
}
void reorderBytes(char * data,int n)
{
	int i;
	char temp[4];
	for (i=0;i<n;i++)
		temp[i]=data[i];
	for (i=0;i<n;i++)
		data[n-i-1]=temp[i];
}
int needToReorder()
{
	int x=1;
	return (((char *)&x)[0]==0);
}
int readBMPheader(FILE *fp, int *pwidth, int *pheight)
{
	unsigned char header[54];
	short int bitsperpixel;
	if (fread(&header,54,1,fp)!=1)
	{
		cout<<"Failed read"<<endl;
		return 0;
	}
	if((header[0]!='B')||(header[1]!='M'))
	{
		printf("Not a valid BMP file\n");
		return 0;
	}
	if (needToReorder())
	{
		cout<<"reordering..."<<endl;
		reorderBytes((char *)(header+18),4);//width
		reorderBytes((char *)(header+22),4);//height
		reorderBytes((char *)(header+28),2);//bit
		reorderBytes((char *)(header+30),4);//compression
	}
	*pwidth=*(int *)(header+18);
	*pheight=*(int *)(header+22);
	if ((*pwidth>SIDE)||(*pheight>SIDE))
	{
		cout<<"Image is too big."<<endl;
		return 0;
	}
	bitsperpixel=*(short int *)(header+28);
	cout<<"bitsperpixel: "<<bitsperpixel<<endl;
	if(bitsperpixel!=24)
	{
		cout<<"Not 24 bits per pixel\n";
		return 0;
	}
	if (*(header+30)!=0)
	{
		cout<<"Compression not 0\n";
		return 0;
	}
	cout<<"Row length: "<<*pwidth<<endl;
	cout<<"Number of rows: "<<*pheight<<endl;
	return 1;
}

void writeBMPheader(FILE *fp, int width, int height)
{
	unsigned char header[54];
	for(int i=0;i<54;i++)
		header[i]=0;
	header[0]='B';
	header[1]='M';
	int pad=(3*width)%4;
	int size=54+height*(3*width+pad);
	*((int *)(header+2))=size;
	*((int *)(header+10))=54;//offset
	*((int *)(header+14))=40; //bisize
	*((int *)(header+18))=width;
	*((int *)(header+22))=height;
	*((short int *)(header+26))=1; //biplanes
	*((short int *)(header+28))=24; //bits per pixel
	*((int *)(header+30))=0; //compression
	
	if (needToReorder())
	{
		reorderBytes((char *)(header+2),4);//size
		reorderBytes((char *)(header+10),4);//offset
		reorderBytes((char *)(header+14),4);//bisize
		reorderBytes((char *)(header+18),4);//width
		reorderBytes((char *)(header+22),4);//height
		reorderBytes((char *)(header+26),2);//biplanes
		reorderBytes((char *)(header+28),2);//bits per pixel
		reorderBytes((char *)(header+30),4);//compression
	}
	fwrite(header,54,1,fp);
}
rgbtriple bw(int bright, rgbtriple t)
{
    t.b=bright;
    t.r=bright;
    t.g=bright;
    return t;

c.
int brightness(rgbtriple t)
{
    int bright;
    bright=((t.r+t.b+t.g)/2);
    return bright;
}

rgbtriple bw(int bright, rgbtriple t)
{
    t.b=bright;
    t.r=bright;
    t.g=bright;
    return t;
}
void processImage(rgbtriple image[SIDE][SIDE],int width,int height)
{
//this function sets the blue and green values of all pixels to 0
//resulting in an image in shades of green
 for (int y=0;y<height;y++)
  for(int x=0;x<width;x++)
  {
   brightness(image[SIDE][SIDE]);
   bw(bright,image[SIDE][SIDE]);
  }
}

//here is changing colors..  
//void processImage(rgbtriple //image[SIDE][SIDE],int width,int //height)
//{
//this function sets temporary variables //for rgb
//resulting in an image with b->g, g->r, //and r->b
//	unsigned char tmp_b, tmp_g, //tmp_r;
//    for (int y=0;y<height;y++)
//		for(int x=0;x<width;x++)
//		{
//			tmp_r = //image[x][y].r;
//            tmp_g = image[x][y].g;
//            tmp_b = image[x][y].b;
//            image[x][y].b=tmp_g;
//			//image[x][y].g=tmp_r;
//			//image[x][y].r=tmp_b;
//		}
//}
//
//
//please excuse indentation errors