CRotateImage.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "CRotateImage.h"
  2. CRotateImage::CRotateImage(CImageBasis *_org, CImageBasis *_temp)
  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. islocked = false;
  12. }
  13. void CRotateImage::Mirror(){
  14. int memsize = width * height * channels;
  15. uint8_t* odata;
  16. if (ImageTMP)
  17. {
  18. odata = ImageTMP->RGBImageLock();
  19. }
  20. else
  21. {
  22. odata = (unsigned char*)GET_MEMORY(memsize);
  23. }
  24. int x_source, y_source;
  25. stbi_uc* p_target;
  26. stbi_uc* p_source;
  27. RGBImageLock();
  28. for (int x = 0; x < width; ++x)
  29. for (int y = 0; y < height; ++y)
  30. {
  31. p_target = odata + (channels * (y * width + x));
  32. x_source = width - x;
  33. y_source = y;
  34. p_source = rgb_image + (channels * (y_source * width + x_source));
  35. for (int _channels = 0; _channels < channels; ++_channels)
  36. p_target[_channels] = p_source[_channels];
  37. }
  38. // memcpy(rgb_image, odata, memsize);
  39. memCopy(odata, rgb_image, memsize);
  40. if (!ImageTMP)
  41. stbi_image_free(odata);
  42. if (ImageTMP)
  43. ImageTMP->RGBImageRelease();
  44. RGBImageRelease();
  45. }
  46. void CRotateImage::Rotate(float _angle, int _centerx, int _centery)
  47. {
  48. float m[2][3];
  49. float x_center = _centerx;
  50. float y_center = _centery;
  51. _angle = _angle / 180 * M_PI;
  52. m[0][0] = cos(_angle);
  53. m[0][1] = sin(_angle);
  54. m[0][2] = (1 - m[0][0]) * x_center - m[0][1] * y_center;
  55. m[1][0] = -m[0][1];
  56. m[1][1] = m[0][0];
  57. m[1][2] = m[0][1] * x_center + (1 - m[0][0]) * y_center;
  58. int memsize = width * height * channels;
  59. uint8_t* odata;
  60. if (ImageTMP)
  61. {
  62. odata = ImageTMP->RGBImageLock();
  63. }
  64. else
  65. {
  66. odata = (unsigned char*)GET_MEMORY(memsize);
  67. }
  68. int x_source, y_source;
  69. stbi_uc* p_target;
  70. stbi_uc* p_source;
  71. RGBImageLock();
  72. for (int x = 0; x < width; ++x)
  73. for (int y = 0; y < height; ++y)
  74. {
  75. p_target = odata + (channels * (y * width + x));
  76. x_source = int(m[0][0] * x + m[0][1] * y);
  77. y_source = int(m[1][0] * x + m[1][1] * y);
  78. x_source += int(m[0][2]);
  79. y_source += int(m[1][2]);
  80. if ((x_source >= 0) && (x_source < width) && (y_source >= 0) && (y_source < height))
  81. {
  82. p_source = rgb_image + (channels * (y_source * width + x_source));
  83. for (int _channels = 0; _channels < channels; ++_channels)
  84. p_target[_channels] = p_source[_channels];
  85. }
  86. else
  87. {
  88. for (int _channels = 0; _channels < channels; ++_channels)
  89. p_target[_channels] = 255;
  90. }
  91. }
  92. // memcpy(rgb_image, odata, memsize);
  93. memCopy(odata, rgb_image, memsize);
  94. if (!ImageTMP)
  95. {
  96. stbi_image_free(odata);
  97. }
  98. if (ImageTMP)
  99. ImageTMP->RGBImageRelease();
  100. RGBImageRelease();
  101. }
  102. void CRotateImage::Rotate(float _angle)
  103. {
  104. // printf("width %d, height %d\n", width, height);
  105. Rotate(_angle, width / 2, height / 2);
  106. }
  107. void CRotateImage::Translate(int _dx, int _dy)
  108. {
  109. int memsize = width * height * channels;
  110. uint8_t* odata;
  111. if (ImageTMP)
  112. {
  113. odata = ImageTMP->RGBImageLock();
  114. }
  115. else
  116. {
  117. odata = (unsigned char*)GET_MEMORY(memsize);
  118. }
  119. int x_source, y_source;
  120. stbi_uc* p_target;
  121. stbi_uc* p_source;
  122. RGBImageLock();
  123. for (int x = 0; x < width; ++x)
  124. for (int y = 0; y < height; ++y)
  125. {
  126. p_target = odata + (channels * (y * width + x));
  127. x_source = x - _dx;
  128. y_source = y - _dy;
  129. if ((x_source >= 0) && (x_source < width) && (y_source >= 0) && (y_source < height))
  130. {
  131. p_source = rgb_image + (channels * (y_source * width + x_source));
  132. for (int _channels = 0; _channels < channels; ++_channels)
  133. p_target[_channels] = p_source[_channels];
  134. }
  135. else
  136. {
  137. for (int _channels = 0; _channels < channels; ++_channels)
  138. p_target[_channels] = 255;
  139. }
  140. }
  141. // memcpy(rgb_image, odata, memsize);
  142. memCopy(odata, rgb_image, memsize);
  143. if (!ImageTMP)
  144. {
  145. stbi_image_free(odata);
  146. }
  147. if (ImageTMP)
  148. {
  149. ImageTMP->RGBImageRelease();
  150. }
  151. RGBImageRelease();
  152. }