CRotateImage.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include <string>
  2. #include "CRotateImage.h"
  3. #include "psram.h"
  4. static const char *TAG = "C ROTATE IMG";
  5. CRotateImage::CRotateImage(std::string _name, CImageBasis *_org, CImageBasis *_temp, bool _flip) : CImageBasis(_name)
  6. {
  7. rgb_image = _org->rgb_image;
  8. channels = _org->channels;
  9. width = _org->width;
  10. height = _org->height;
  11. bpp = _org->bpp;
  12. externalImage = true;
  13. ImageTMP = _temp;
  14. ImageOrg = _org;
  15. islocked = false;
  16. doflip = _flip;
  17. }
  18. void CRotateImage::Rotate(float _angle, int _centerx, int _centery)
  19. {
  20. int org_width, org_height;
  21. float m[2][3];
  22. float x_center = _centerx;
  23. float y_center = _centery;
  24. _angle = _angle / 180 * M_PI;
  25. if (doflip)
  26. {
  27. org_width = width;
  28. org_height = height;
  29. height = org_width;
  30. width = org_height;
  31. x_center = x_center - (org_width / 2) + (org_height / 2);
  32. y_center = y_center + (org_width / 2) - (org_height / 2);
  33. if (ImageOrg)
  34. {
  35. ImageOrg->height = height;
  36. ImageOrg->width = width;
  37. }
  38. }
  39. else
  40. {
  41. org_width = width;
  42. org_height = height;
  43. }
  44. m[0][0] = cos(_angle);
  45. m[0][1] = sin(_angle);
  46. m[0][2] = (1 - m[0][0]) * x_center - m[0][1] * y_center;
  47. m[1][0] = -m[0][1];
  48. m[1][1] = m[0][0];
  49. m[1][2] = m[0][1] * x_center + (1 - m[0][0]) * y_center;
  50. if (doflip)
  51. {
  52. m[0][2] = m[0][2] + (org_width / 2) - (org_height / 2);
  53. m[1][2] = m[1][2] - (org_width / 2) + (org_height / 2);
  54. }
  55. int memsize = width * height * channels;
  56. uint8_t *temp_image;
  57. if (ImageTMP)
  58. {
  59. temp_image = ImageTMP->RGBImageLock();
  60. }
  61. else
  62. {
  63. temp_image = (unsigned char *)malloc_psram_heap(std::string(TAG) + "->temp_image", memsize, MALLOC_CAP_SPIRAM);
  64. }
  65. int x_source, y_source;
  66. stbi_uc *p_target;
  67. stbi_uc *p_source;
  68. RGBImageLock();
  69. for (int x = 0; x < width; ++x)
  70. {
  71. for (int y = 0; y < height; ++y)
  72. {
  73. p_target = temp_image + (channels * (y * width + x));
  74. x_source = int(m[0][0] * x + m[0][1] * y);
  75. y_source = int(m[1][0] * x + m[1][1] * y);
  76. x_source += int(m[0][2]);
  77. y_source += int(m[1][2]);
  78. if ((x_source >= 0) && (x_source < org_width) && (y_source >= 0) && (y_source < org_height))
  79. {
  80. p_source = rgb_image + (channels * (y_source * org_width + x_source));
  81. for (int _channels = 0; _channels < channels; ++_channels)
  82. {
  83. p_target[_channels] = p_source[_channels];
  84. }
  85. }
  86. else
  87. {
  88. for (int _channels = 0; _channels < channels; ++_channels)
  89. {
  90. // p_target[_channels] = 255; // nicht vorhandene Pixel werden Weiß gemacht
  91. // p_target[_channels] = 0; // nicht vorhandene Pixel werden Schwarz gemacht
  92. // p_target[_channels] = 128; // nicht vorhandene Pixel werden Grau gemacht
  93. p_target[_channels] = pixel_fill_color;
  94. }
  95. }
  96. }
  97. }
  98. memCopy(temp_image, rgb_image, memsize);
  99. if (ImageTMP)
  100. {
  101. ImageTMP->RGBImageRelease();
  102. }
  103. else
  104. {
  105. free_psram_heap(std::string(TAG) + "->temp_image", temp_image);
  106. }
  107. RGBImageRelease();
  108. }
  109. void CRotateImage::Rotate(float _angle)
  110. {
  111. // ESP_LOGD(TAG, "width %d, height %d", width, height);
  112. Rotate(_angle, width / 2, height / 2);
  113. }
  114. void CRotateImage::RotateAntiAliasing(float _angle, int _centerx, int _centery)
  115. {
  116. int org_width, org_height;
  117. float m[2][3];
  118. float x_center = _centerx;
  119. float y_center = _centery;
  120. _angle = _angle / 180 * M_PI;
  121. if (doflip)
  122. {
  123. org_width = width;
  124. org_height = height;
  125. height = org_width;
  126. width = org_height;
  127. x_center = x_center - (org_width / 2) + (org_height / 2);
  128. y_center = y_center + (org_width / 2) - (org_height / 2);
  129. if (ImageOrg)
  130. {
  131. ImageOrg->height = height;
  132. ImageOrg->width = width;
  133. }
  134. }
  135. else
  136. {
  137. org_width = width;
  138. org_height = height;
  139. }
  140. m[0][0] = cos(_angle);
  141. m[0][1] = sin(_angle);
  142. m[0][2] = (1 - m[0][0]) * x_center - m[0][1] * y_center;
  143. m[1][0] = -m[0][1];
  144. m[1][1] = m[0][0];
  145. m[1][2] = m[0][1] * x_center + (1 - m[0][0]) * y_center;
  146. if (doflip)
  147. {
  148. m[0][2] = m[0][2] + (org_width / 2) - (org_height / 2);
  149. m[1][2] = m[1][2] - (org_width / 2) + (org_height / 2);
  150. }
  151. int memsize = width * height * channels;
  152. uint8_t *temp_image;
  153. if (ImageTMP)
  154. {
  155. temp_image = ImageTMP->RGBImageLock();
  156. }
  157. else
  158. {
  159. temp_image = (unsigned char *)malloc_psram_heap(std::string(TAG) + "->temp_image", memsize, MALLOC_CAP_SPIRAM);
  160. }
  161. int x_source_1, y_source_1, x_source_2, y_source_2;
  162. float x_source, y_source;
  163. float quad_ul, quad_ur, quad_ol, quad_or;
  164. stbi_uc *p_target;
  165. stbi_uc *p_source_ul, *p_source_ur, *p_source_ol, *p_source_or;
  166. RGBImageLock();
  167. for (int x = 0; x < width; ++x)
  168. {
  169. for (int y = 0; y < height; ++y)
  170. {
  171. p_target = temp_image + (channels * (y * width + x));
  172. x_source = (m[0][0] * x + m[0][1] * y);
  173. y_source = (m[1][0] * x + m[1][1] * y);
  174. x_source += (m[0][2]);
  175. y_source += (m[1][2]);
  176. x_source_1 = (int)x_source;
  177. x_source_2 = x_source_1 + 1;
  178. y_source_1 = (int)y_source;
  179. y_source_2 = y_source_1 + 1;
  180. quad_ul = (x_source_2 - x_source) * (y_source_2 - y_source);
  181. quad_ur = (1 - (x_source_2 - x_source)) * (y_source_2 - y_source);
  182. quad_or = (x_source_2 - x_source) * (1 - (y_source_2 - y_source));
  183. quad_ol = (1 - (x_source_2 - x_source)) * (1 - (y_source_2 - y_source));
  184. if ((x_source_1 >= 0) && (x_source_2 < org_width) && (y_source_1 >= 0) && (y_source_2 < org_height))
  185. {
  186. p_source_ul = rgb_image + (channels * (y_source_1 * org_width + x_source_1));
  187. p_source_ur = rgb_image + (channels * (y_source_1 * org_width + x_source_2));
  188. p_source_or = rgb_image + (channels * (y_source_2 * org_width + x_source_1));
  189. p_source_ol = rgb_image + (channels * (y_source_2 * org_width + x_source_2));
  190. for (int _channels = 0; _channels < channels; ++_channels)
  191. {
  192. p_target[_channels] = (int)((float)p_source_ul[_channels] * quad_ul + (float)p_source_ur[_channels] * quad_ur + (float)p_source_or[_channels] * quad_or + (float)p_source_ol[_channels] * quad_ol);
  193. }
  194. }
  195. else
  196. {
  197. for (int _channels = 0; _channels < channels; ++_channels)
  198. {
  199. // p_target[_channels] = 255; // nicht vorhandene Pixel werden Weiß gemacht
  200. // p_target[_channels] = 0; // nicht vorhandene Pixel werden Schwarz gemacht
  201. // p_target[_channels] = 128; // nicht vorhandene Pixel werden Grau gemacht
  202. p_target[_channels] = pixel_fill_color;
  203. }
  204. }
  205. }
  206. }
  207. memCopy(temp_image, rgb_image, memsize);
  208. if (ImageTMP)
  209. {
  210. ImageTMP->RGBImageRelease();
  211. }
  212. else
  213. {
  214. free_psram_heap(std::string(TAG) + "->temp_image", temp_image);
  215. }
  216. RGBImageRelease();
  217. }
  218. void CRotateImage::RotateAntiAliasing(float _angle)
  219. {
  220. // ESP_LOGD(TAG, "width %d, height %d", width, height);
  221. RotateAntiAliasing(_angle, width / 2, height / 2);
  222. }
  223. void CRotateImage::Translate(int _dx, int _dy)
  224. {
  225. int memsize = width * height * channels;
  226. uint8_t *temp_image;
  227. if (ImageTMP)
  228. {
  229. temp_image = ImageTMP->RGBImageLock();
  230. }
  231. else
  232. {
  233. temp_image = (unsigned char *)malloc_psram_heap(std::string(TAG) + "->temp_image", memsize, MALLOC_CAP_SPIRAM);
  234. }
  235. int x_source, y_source;
  236. stbi_uc *p_target;
  237. stbi_uc *p_source;
  238. RGBImageLock();
  239. for (int x = 0; x < width; ++x)
  240. {
  241. for (int y = 0; y < height; ++y)
  242. {
  243. p_target = temp_image + (channels * (y * width + x));
  244. x_source = x - _dx;
  245. y_source = y - _dy;
  246. if ((x_source >= 0) && (x_source < width) && (y_source >= 0) && (y_source < height))
  247. {
  248. p_source = rgb_image + (channels * (y_source * width + x_source));
  249. for (int _channels = 0; _channels < channels; ++_channels)
  250. {
  251. p_target[_channels] = p_source[_channels];
  252. }
  253. }
  254. else
  255. {
  256. for (int _channels = 0; _channels < channels; ++_channels)
  257. {
  258. // p_target[_channels] = 255; // nicht vorhandene Pixel werden Weiß gemacht
  259. // p_target[_channels] = 0; // nicht vorhandene Pixel werden Schwarz gemacht
  260. // p_target[_channels] = 128; // nicht vorhandene Pixel werden Grau gemacht
  261. p_target[_channels] = pixel_fill_color;
  262. }
  263. }
  264. }
  265. }
  266. memCopy(temp_image, rgb_image, memsize);
  267. if (ImageTMP)
  268. {
  269. ImageTMP->RGBImageRelease();
  270. }
  271. else
  272. {
  273. free_psram_heap(std::string(TAG) + "->temp_image", temp_image);
  274. }
  275. RGBImageRelease();
  276. }