CRotateImage.cpp 9.0 KB

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