-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphics.cpp
110 lines (95 loc) · 2.58 KB
/
Graphics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Graphics.cpp
* Project UID 8b3bcc444eb500121e420f7e2e359014
*
* EECS 183, Fall 2019
* Project 4: CoolPics
*
* Tin Long Rex Fung, Isaac Lok-Tin Li
* rexfung, isaliac
*
* This file contains the class "Graphics"
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include "Graphics.h"
#include "bmp.h"
using namespace std;
// TODO: implement constructor, clear, setPixel, initArray.
Graphics::Graphics() { // default constructor for color sets it to black
}
void Graphics::initArray() {
Color initial; // initial color is black
for (int i = 0; i < DIMENSION; i++) {
for (int j = 0; j < DIMENSION; j++) {
pixelData[i][j] = initial;
}
}
return;
}
void Graphics::clear() { // same as initArray()
initArray();
return;
}
void Graphics::setPixel(int x, int y, Color color) {
if (x >= 0 && x < 100 && y >= 0 && y < 100) { // checks if coord is in bounds
pixelData[y][x] = color;
}
return;
}
// Your code goes above this line.
// Don't change the implementation below!
void Graphics::writeFile(string fileName) const
{
ofstream outFile;
outFile.open(fileName, ios::binary);
// determine padding
int padding = (4 - (DIMENSION * 3) % 4) % 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
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;
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
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()
<< (BYTE) pixel.getRed();
}
// write padding to outfile
for (int k = 0; k < padding; k++)
{
outFile << 0;
}
}
// close file
outFile.close();
}