img.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #pragma once
  2. #include "stdafx.h"
  3. #include <vector>
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace std;
  7. struct Settings
  8. {
  9. //íàñòðîéêè äëÿ bmp ôàéëà
  10. unsigned int imgWidth = 500, imgHeight = 500;
  11. unsigned char signature[2] = { 'B', 'M' };
  12. unsigned int fileSize = 14 + 40 + imgWidth * imgHeight * 3;
  13. unsigned int reserved = 0;
  14. unsigned int offset = 14 + 40;
  15. unsigned int headerSize = 40;
  16. unsigned int dimensions[2] = { imgWidth, imgHeight };
  17. unsigned short colorPlanes = 1;
  18. unsigned short bpp = 32;
  19. unsigned int compression = 0;
  20. unsigned int imgSize = imgWidth * imgHeight * 3;
  21. unsigned int resolution[2] = { 2795, 2795 };
  22. unsigned int pltColors = 0;
  23. unsigned int impColors = 0;
  24. int lineImgSize;
  25. };
  26. class Pixel
  27. {
  28. //ïèêñåëü êàðòèêè
  29. public:
  30. int r;
  31. int g;
  32. int b;
  33. int a;
  34. Pixel()
  35. {
  36. this->r = 10;
  37. this->g = 10;
  38. this->b = 10;
  39. this->a = 0;
  40. };
  41. Pixel(int r, int g, int b, int a)
  42. {
  43. this->r = r;
  44. this->g = g;
  45. this->b = b;
  46. this->a = a;
  47. }
  48. Pixel(int r, int g, int b)
  49. {
  50. this->r = r;
  51. this->g = g;
  52. this->b = b;
  53. this->a = -1;
  54. }
  55. };
  56. template <class T> class Row
  57. {
  58. //ñòðîêà ïèêñåëåé
  59. public:
  60. int width;
  61. T** pixel;
  62. Row() {};
  63. Row(int width)
  64. {
  65. this->width = width;
  66. pixel = new T*[width];
  67. for (int i = 0; i < width; i++)
  68. {
  69. pixel[i] = new T();
  70. }
  71. }
  72. T* operator[](int n)
  73. {
  74. return pixel[n];
  75. }
  76. };
  77. template <class T> class Img
  78. {
  79. //àáñòðàêòíûé êëàññ êàðòèêè
  80. public:
  81. int width, height, amount;
  82. Row <T> *data;
  83. Settings settings;
  84. Img() {};
  85. Img(int width, int height, int amount)
  86. {
  87. this->width = width;
  88. this->height = height;
  89. this->amount = amount;
  90. init();
  91. }
  92. void init()
  93. {
  94. data = new Row <T>[height];
  95. for (int i = 0; i < height; i++)
  96. {
  97. data[i] = Row<T>(width);
  98. }
  99. }
  100. Row <T> operator[](int n)
  101. {
  102. return data[n];
  103. }
  104. };
  105. class BMP : public Img <Pixel>
  106. {
  107. //bmp êàðòèíêè
  108. public:
  109. BMP() {};
  110. void read(string filename)
  111. {
  112. ifstream os(filename);
  113. os.read(reinterpret_cast<char*>(settings.signature), sizeof(settings.signature));
  114. os.read(reinterpret_cast<char*>(&settings.fileSize), sizeof(settings.fileSize));
  115. os.read(reinterpret_cast<char*>(&settings.reserved), sizeof(settings.reserved));
  116. os.read(reinterpret_cast<char*>(&settings.offset), sizeof(settings.offset));
  117. os.read(reinterpret_cast<char*>(&settings.headerSize), sizeof(settings.headerSize));
  118. os.read(reinterpret_cast<char*>(&settings.dimensions), sizeof(settings.dimensions));
  119. settings.imgHeight = settings.dimensions[1];
  120. settings.imgWidth = settings.dimensions[0];
  121. this->width = settings.imgWidth;
  122. this->height = settings.imgHeight;
  123. this->amount = 3;
  124. os.read(reinterpret_cast<char*>(&settings.colorPlanes), sizeof(settings.colorPlanes));
  125. os.read(reinterpret_cast<char*>(&settings.bpp), sizeof(settings.bpp));
  126. os.read(reinterpret_cast<char*>(&settings.compression), sizeof(settings.compression));
  127. os.read(reinterpret_cast<char*>(&settings.imgSize), sizeof(settings.imgSize));
  128. os.read(reinterpret_cast<char*>(settings.resolution), sizeof(settings.resolution));
  129. os.read(reinterpret_cast<char*>(&settings.pltColors), sizeof(settings.pltColors));
  130. os.read(reinterpret_cast<char*>(&settings.impColors), sizeof(settings.impColors));
  131. this->init();
  132. unsigned char b = 0, g = 0, r = 0, a = 0;
  133. for (int i = 0; i < this->height; i++)
  134. {
  135. for (int j = 0; j < this->width; j++)
  136. {
  137. os >> b;
  138. os >> g;
  139. os >> r;
  140. // cout << (int)b << " - " << (int)g << " - " << (int)r << endl;
  141. //system("pause");
  142. (*this)[i][j]->b = (int)b;
  143. (*this)[i][j]->g = (int)g;
  144. (*this)[i][j]->r = (int)r;
  145. }
  146. }
  147. }
  148. void write(string filename)
  149. {
  150. ofstream os(filename);
  151. os.write(reinterpret_cast<char*>(settings.signature), sizeof(settings.signature));
  152. os.write(reinterpret_cast<char*>(&settings.fileSize), sizeof(settings.fileSize));
  153. os.write(reinterpret_cast<char*>(&settings.reserved), sizeof(settings.reserved));
  154. os.write(reinterpret_cast<char*>(&settings.offset), sizeof(settings.offset));
  155. os.write(reinterpret_cast<char*>(&settings.headerSize), sizeof(settings.headerSize));
  156. os.write(reinterpret_cast<char*>(settings.dimensions), sizeof(settings.dimensions));
  157. os.write(reinterpret_cast<char*>(&settings.colorPlanes), sizeof(settings.colorPlanes));
  158. os.write(reinterpret_cast<char*>(&settings.bpp), sizeof(settings.bpp));
  159. os.write(reinterpret_cast<char*>(&settings.compression), sizeof(settings.compression));
  160. os.write(reinterpret_cast<char*>(&settings.imgSize), sizeof(settings.imgSize));
  161. os.write(reinterpret_cast<char*>(settings.resolution), sizeof(settings.resolution));
  162. os.write(reinterpret_cast<char*>(&settings.pltColors), sizeof(settings.pltColors));
  163. os.write(reinterpret_cast<char*>(&settings.impColors), sizeof(settings.impColors));
  164. for (int i = 0; i < this->height; i++)
  165. {
  166. for (int j = 0; j < this->width; j++)
  167. {
  168. os << (unsigned char)(*this)[i][j]->b;
  169. os << (unsigned char)(*this)[i][j]->g;
  170. os << (unsigned char)(*this)[i][j]->r;
  171. }
  172. }
  173. }
  174. };