CRotateImage.cpp 4.2 KB

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