Advertisement
Guest User

Untitled

a guest
Nov 11th, 2016
2,989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. /**
  2. * Graphics.cpp
  3. *
  4. * EECS 183, Fall 2016
  5. * Project 4: CoolPics
  6. *
  7. * John Dorsey, Patrick Ahimovic
  8. * jsdorsey, paddya
  9. *
  10. * Functions for the Graphics class and its 2D Array
  11. */
  12.  
  13. #include <iostream>
  14. #include <fstream>
  15. #include <string>
  16. #include <cmath>
  17. #include <algorithm>
  18.  
  19. #include "Graphics.h"
  20. #include "bmp.h"
  21.  
  22. using namespace std;
  23.  
  24.  
  25. Graphics::Graphics() {
  26. initArray();
  27. }
  28.  
  29. void Graphics::clear() {
  30. initArray();
  31. }
  32.  
  33. void Graphics::setPixel(int x, int y, Color col) {
  34.  
  35. // stores color's RGB values as needed
  36. int set_red = col.getRed();
  37. int set_blue = col.getBlue();
  38. int set_green = col.getGreen();
  39.  
  40. if (x <= 99 && x >= 0) {
  41. if (y <= 99 && y >= 0) {
  42. pixelData[x][y].setRed(set_red);
  43. pixelData[x][y].setBlue(set_blue);
  44. pixelData[x][y].setGreen(set_green);
  45. }
  46. }
  47. }
  48.  
  49. void Graphics::initArray() {
  50.  
  51. for (int i = 0; i < DIMENSION; i++) {
  52.  
  53. for (int j = 0; j < DIMENSION; j++) {
  54. pixelData[i][j].setRed(0);
  55. pixelData[i][j].setBlue(0);
  56. pixelData[i][j].setGreen(0);
  57. }
  58. }
  59.  
  60. }
  61.  
  62. // Your code goes above this line.
  63. // Don't change the implementation below!
  64.  
  65. void Graphics::writeFile(string fileName) const
  66. {
  67. ofstream outFile;
  68. outFile.open(fileName);
  69.  
  70. // determine padding
  71. int padding = (4 - (DIMENSION * 3) % 4) % 4;
  72.  
  73. // BITMAPFILEHEADER
  74. BITMAPFILEHEADER bf;
  75. bf.bfType = 0x4d42; // type of file = bitmap
  76. bf.bfSize = DIMENSION * (DIMENSION + padding) * 3 + 54; // TODO
  77. bf.bfReserved1 = 0;
  78. bf.bfReserved2 = 0;
  79. bf.bfOffBits = 54; // location of pixels
  80.  
  81. // BITMAPINFOHEADER
  82. BITMAPINFOHEADER bi;
  83. bi.biSize = 40; // header size
  84. bi.biWidth = DIMENSION;
  85. bi.biHeight = -DIMENSION;
  86. bi.biPlanes = 1;
  87. bi.biBitCount = 24;
  88. bi.biCompression = 0;
  89. bi.biSizeImage = bi.biWidth * bi.biHeight * 3;
  90. bi.biXPelsPerMeter = 2834;
  91. bi.biYPelsPerMeter = 2834;
  92. bi.biClrUsed = 0;
  93. bi.biClrImportant = 0;
  94.  
  95. // write output BITMAPFILEHEADER
  96. outFile.write((char*)&bf, sizeof(BITMAPFILEHEADER));
  97.  
  98. // write output BITMAPINFOHEADER
  99. outFile.write((char*)&bi, sizeof(BITMAPINFOHEADER));
  100.  
  101. // iterate over lines
  102. for (int i = 0; i < DIMENSION; i++)
  103. {
  104. // iterate over pixels in line
  105. for (int j = 0; j < DIMENSION; j++)
  106. {
  107. // temporary storage
  108. Color pixel = pixelData[i][j];
  109.  
  110. // write RGB triple to outfile
  111. outFile << (BYTE) pixel.getBlue() << (BYTE) pixel.getGreen()
  112. << (BYTE) pixel.getRed();
  113. }
  114.  
  115. // write padding to outfile
  116. for (int k = 0; k < padding; k++)
  117. {
  118. outFile << 0;
  119. }
  120. }
  121.  
  122. // close file
  123. outFile.close();
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement