CRotateImage.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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::Rotate(float _angle)
  130. {
  131. // printf("width %d, height %d\n", width, height);
  132. Rotate(_angle, width / 2, height / 2);
  133. }
  134. void CRotateImage::Translate(int _dx, int _dy)
  135. {
  136. int memsize = width * height * channels;
  137. uint8_t* odata;
  138. if (ImageTMP)
  139. {
  140. odata = ImageTMP->RGBImageLock();
  141. }
  142. else
  143. {
  144. odata = (unsigned char*)GET_MEMORY(memsize);
  145. }
  146. int x_source, y_source;
  147. stbi_uc* p_target;
  148. stbi_uc* p_source;
  149. RGBImageLock();
  150. for (int x = 0; x < width; ++x)
  151. for (int y = 0; y < height; ++y)
  152. {
  153. p_target = odata + (channels * (y * width + x));
  154. x_source = x - _dx;
  155. y_source = y - _dy;
  156. if ((x_source >= 0) && (x_source < width) && (y_source >= 0) && (y_source < height))
  157. {
  158. p_source = rgb_image + (channels * (y_source * width + x_source));
  159. for (int _channels = 0; _channels < channels; ++_channels)
  160. p_target[_channels] = p_source[_channels];
  161. }
  162. else
  163. {
  164. for (int _channels = 0; _channels < channels; ++_channels)
  165. p_target[_channels] = 255;
  166. }
  167. }
  168. // memcpy(rgb_image, odata, memsize);
  169. memCopy(odata, rgb_image, memsize);
  170. if (!ImageTMP)
  171. {
  172. stbi_image_free(odata);
  173. }
  174. if (ImageTMP)
  175. {
  176. ImageTMP->RGBImageRelease();
  177. }
  178. RGBImageRelease();
  179. }