eecs 183 project 4 /** * Graphics.cpp * * EECS 183, Fall 2016 * Project 4: CoolPics * * John Dorsey, Patrick Ahimovic * jsdorsey, paddya * * Functions for the Graphics class and its 2D Array */ eecs 183 project 4 PasteShr eecs 183 project 4 #include #include #include #include #include #include "Graphics.h" #include "bmp.h" eecs 183 project 4 How to use it? eecs 183 project 4 using namespace std; Graphics::Graphics() { initArray(); } void Graphics::clear() { initArray(); } eecs 183 project 4 How to get it? eecs 183 project 4 void Graphics::setPixel(int x, int y, Color col) { // stores color's RGB values as needed int set_red = col.getRed(); int set_blue = col.getBlue(); int set_green = col.getGreen(); if (x <= 99 && x >= 0) { if (y <= 99 && y >= 0) { eecs 183 project 4 How to dowload it? eecs 183 project 4 pixelData[x][y].setRed(set_red); pixelData[x][y].setBlue(set_blue); pixelData[x][y].setGreen(set_green); } } } void Graphics::initArray() { for (int i = 0; i < DIMENSION; i++) { eecs 183 project 4 PasteShr eecs 183 project 4 for (int j = 0; j < DIMENSION; j++) { pixelData[i][j].setRed(0); pixelData[i][j].setBlue(0); pixelData[i][j].setGreen(0); } } } eecs 183 project 4 How to get it for free? eecs 183 project 4 // Your code goes above this line. // Don't change the implementation below! void Graphics::writeFile(string fileName) const { ofstream outFile; outFile.open(fileName); // determine padding int padding = (4 - (DIMENSION * 3) % 4) % 4; eecs 183 project 4 How to get it for free? eecs 183 project 4 // BITMAPFILEHEADER BITMAPFILEHEADER bf; bf.bfType = 0x4d42; // type of file = bitmap bf.bfSize = DIMENSION * (DIMENSION + padding) * 3 + 54; // TODO bf.bfReserved1 = 0; bf.bfReserved2 = 0; bf.bfOffBits = 54; // location of pixels // BITMAPINFOHEADER eecs 183 project 4 PasteShr eecs 183 project 4 BITMAPINFOHEADER bi; bi.biSize = 40; // header size bi.biWidth = DIMENSION; bi.biHeight = -DIMENSION; bi.biPlanes = 1; bi.biBitCount = 24; bi.biCompression = 0; bi.biSizeImage = bi.biWidth * bi.biHeight * 3; bi.biXPelsPerMeter = 2834; bi.biYPelsPerMeter = 2834; eecs 183 project 4 How to get it? eecs 183 project 4 bi.biClrUsed = 0; bi.biClrImportant = 0; // write output BITMAPFILEHEADER outFile.write((char*)&bf, sizeof(BITMAPFILEHEADER)); // write output BITMAPINFOHEADER outFile.write((char*)&bi, sizeof(BITMAPINFOHEADER)); // iterate over lines eecs 183 project 4 How to get it for free? eecs 183 project 4 for (int i = 0; i < DIMENSION; i++) { // iterate over pixels in line for (int j = 0; j < DIMENSION; j++) { // temporary storage Color pixel = pixelData[i][j]; // write RGB triple to outfile outFile << (BYTE) pixel.getBlue() << (BYTE) pixel.getGreen() eecs 183 project 4 How to get it? eecs 183 project 4 << (BYTE) pixel.getRed(); } // write padding to outfile for (int k = 0; k < padding; k++) { outFile << 0; } } eecs 183 project 4 How to use it? eecs 183 project 4 // close file outFile.close(); } eecs 183 project 4