else if((column-(width/2))*(column-(width/2))+(line-height/2)*(line-height/2)<(height/4)*(height/4)) // blue circle
{
green=red=0;
blue=char(255);
}
else if((line*line+column*column)<(2*height/3)*(2*height/3)) // top left circle
{
blue=red=0;
green=char(255);
}
else if((column-width)*(column-width)+(line-2*height/3)*(line-2*height/3)<(width/2)*(width/2)) // red circle
{
if (0<=line<=166)
green=blue=-1.5349*line+255.31;
red=255;
}
else // bottom right half: white
red=green=blue=255;
/* CS 150 Assignment 2 Listing: Generating a bmp File Dr. Woelfl
The purpose of this program is to produce a bmp file for Window's Paint program.
Assignment will require modifying the if statements to produce a specific image.
Function fileHeader() is used to fill in a character array with required information
such as the file size, the image size, and what type of color mode is used.
BMP (BitMap Files) are image files that have a relatively simple format:
The first 54 bytes contain file header information:
* The first two bytes (0 and 1) must be BM
* Next 4 bytes stores the file size
* Bytes 18 through 21 contain the image width in pixels
* Bytes 22 through 25 contain the image height in pixels
* Byte 28 must be 24 for 24 bit BMP files
There are a few other required bytes.
Image data is stored in the following format:
* Scan lines are stored bottom to top
* Each scan line is padded to the next four byte boundary.
* RGB vales are stored in reverse order: Blue, Green, and Red.
After running this program
Open the output file, out1.bmp, in Window's Paint program:
* Select Image and Attributes to see the size (300 pixels wide by 200 high).
* Select "Pick Color" (eye dropper), and choose a pixel,
select Options, Edit Colors, Define Custom Colors to verify the RGB values
of pixels.
* Updated views of the output file can be viewed using Edit, Paste From ...
*/
#include<iostream>
#include<fstream>
using namespace std;
void int_to_fourByte(char*, int);
void fillHeader(char*, int image_hieght, int image_width);
void main()
{
const int width=300, height=200; // image size
int paddingSize = width%4; // compute padding for each line
char red, green, blue; // color values: 0 to 255
// open file for binary output
ofstream fileOut;
fileOut.open("out1.bmp", ios::out | ios::binary);
if(!fileOut.good())
{
cout<<"Can't open output file"<<endl;
exit(0);
}
// evaluate and write the first 54 bytes of header information to the output file
char header_text[54];
fillHeader(header_text, height, width);
fileOut.write(header_text,54);
// write each line, starting from the bottom.
cout << "Creating bmp file" << endl;
for(double line=height-1;line>=0; line--) // loop through lines
{
for(int column=0; column<width; column++) // loop through columns
{
/////////////// MAKE CHANGES HERE ///////////////
if((line>-column+(width/2+height/2))&&(line>column-height/5-width/30)) // triangle
{
blue=0;
green=red=char(255);
}
else if((column-(width/2))*(column-(width/2))+(line-height/2)*(line-height/2)<(height/4)*(height/4)) // blue circle
{
green=red=0;
blue=char(255);
}
else if((line*line+column*column)<(2*height/3)*(2*height/3)) // top left circle
{
blue=red=0;
green=char(255);
}
else if((column-width)*(column-width)+(line-2*height/3)*(line-2*height/3)<(width/2)*(width/2)) // red circle
{
if (0<=line<=166)
green=blue=-1.5349*line+255.31;
red=255;
}
else // bottom right half: white
red=green=blue=255;
// pad each line of output file with "junk"
for(int p=0; p<paddingSize; p++)
fileOut.write(&red,1);
} // end line loop
fileOut.close();
}
// function fillHeader() develops the 54 byte file header for a bitMap file
// For a bmp file, the first 54 bytes contain file header information:
// * The first two bytes (0 and 1) must be BM
// * Next 4 bytes stores the file size
// * Bytes 18 through 21 contain the image width in pixels
// * Bytes 22 through 25 contain the image height in pixels
// * Byte 28 must be 24 for 24 bit BMP files
// written 12/1/2001 by woelfl@msoe.edu
void fillHeader(char header[], int ht, int wid)
{
int padding = wid%4;
int lineLength = wid*3 + padding;
int fileSize=54+lineLength*wid;
for(int i=2; i<54; i++) // set all bytes to null
header[i]='\0';
// function int_to_fourByte() converts an integer into four-byte binary
// the most significant byte is stored first in the char array s
// written 12/1/2001 by woelfl@msoe.edu
void int_to_fourByte(char*s, int num)
{
s[3]=unsigned char(num/0x1000000); // extract most significant byte
num%=0x1000000;
s[2]=unsigned char(num/0x10000);
num%=0x10000;
s[1]=unsigned char(num/0x100);
num%=0x100;
s[0]=unsigned char(num); // remaining value is least significant byte
}