|
|
@@ -1,326 +1,409 @@
|
|
|
-#include <string>
|
|
|
-#include "CRotateImage.h"
|
|
|
-#include "psram.h"
|
|
|
-
|
|
|
-static const char *TAG = "C ROTATE IMG";
|
|
|
-
|
|
|
-// Value written to target pixels whose source lies outside the image (the gaps left by a rotation or
|
|
|
-// translation). It has to be black: the reference image is rotated in the browser (edit_reference.html),
|
|
|
-// which exports the uncovered canvas area as black. The alignment markers are cut from that reference,
|
|
|
-// so a marker that reaches into the gap would otherwise be matched against the wrong colour (#4169).
|
|
|
-static const stbi_uc FILL_VALUE = 0;
|
|
|
-
|
|
|
-CRotateImage::CRotateImage(std::string _name, CImageBasis *_org, CImageBasis *_temp, bool _flip) : CImageBasis(_name)
|
|
|
-{
|
|
|
- rgb_image = _org->rgb_image;
|
|
|
- channels = _org->channels;
|
|
|
- width = _org->width;
|
|
|
- height = _org->height;
|
|
|
- bpp = _org->bpp;
|
|
|
- externalImage = true;
|
|
|
- ImageTMP = _temp;
|
|
|
- ImageOrg = _org;
|
|
|
- islocked = false;
|
|
|
- doflip = _flip;
|
|
|
-}
|
|
|
-
|
|
|
-void CRotateImage::Rotate(float _angle, int _centerx, int _centery)
|
|
|
-{
|
|
|
- if ((_angle == 0) && !doflip) { // identity transform, the copy would leave the image unchanged
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- int org_width, org_height;
|
|
|
- float m[2][3];
|
|
|
-
|
|
|
- float x_center = _centerx;
|
|
|
- float y_center = _centery;
|
|
|
- _angle = _angle / 180 * M_PI;
|
|
|
-
|
|
|
- if (doflip)
|
|
|
- {
|
|
|
- org_width = width;
|
|
|
- org_height = height;
|
|
|
- height = org_width;
|
|
|
- width = org_height;
|
|
|
- x_center = x_center - (org_width/2) + (org_height/2);
|
|
|
- y_center = y_center + (org_width/2) - (org_height/2);
|
|
|
- if (ImageOrg)
|
|
|
- {
|
|
|
- ImageOrg->height = height;
|
|
|
- ImageOrg->width = width;
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- org_width = width;
|
|
|
- org_height = height;
|
|
|
- }
|
|
|
-
|
|
|
- m[0][0] = cos(_angle);
|
|
|
- m[0][1] = sin(_angle);
|
|
|
- m[0][2] = (1 - m[0][0]) * x_center - m[0][1] * y_center;
|
|
|
-
|
|
|
- m[1][0] = -m[0][1];
|
|
|
- m[1][1] = m[0][0];
|
|
|
- m[1][2] = m[0][1] * x_center + (1 - m[0][0]) * y_center;
|
|
|
-
|
|
|
- if (doflip)
|
|
|
- {
|
|
|
- m[0][2] = m[0][2] + (org_width/2) - (org_height/2);
|
|
|
- m[1][2] = m[1][2] - (org_width/2) + (org_height/2);
|
|
|
- }
|
|
|
-
|
|
|
- int memsize = width * height * channels;
|
|
|
- uint8_t* odata;
|
|
|
- if (ImageTMP)
|
|
|
- {
|
|
|
- odata = ImageTMP->RGBImageLock();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- odata = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->odata", memsize, MALLOC_CAP_SPIRAM);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- int x_source, y_source;
|
|
|
- stbi_uc* p_target;
|
|
|
- stbi_uc* p_source;
|
|
|
-
|
|
|
- RGBImageLock();
|
|
|
-
|
|
|
- // y outer / x inner: write the target (and mostly read the source) sequentially in PSRAM
|
|
|
- for (int y = 0; y < height; ++y)
|
|
|
- for (int x = 0; x < width; ++x)
|
|
|
- {
|
|
|
- p_target = odata + (channels * (y * width + x));
|
|
|
-
|
|
|
- x_source = int(m[0][0] * x + m[0][1] * y);
|
|
|
- y_source = int(m[1][0] * x + m[1][1] * y);
|
|
|
-
|
|
|
- x_source += int(m[0][2]);
|
|
|
- y_source += int(m[1][2]);
|
|
|
-
|
|
|
- if ((x_source >= 0) && (x_source < org_width) && (y_source >= 0) && (y_source < org_height))
|
|
|
- {
|
|
|
- p_source = rgb_image + (channels * (y_source * org_width + x_source));
|
|
|
- for (int _channels = 0; _channels < channels; ++_channels)
|
|
|
- p_target[_channels] = p_source[_channels];
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- for (int _channels = 0; _channels < channels; ++_channels)
|
|
|
- p_target[_channels] = FILL_VALUE;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // memcpy(rgb_image, odata, memsize);
|
|
|
- memCopy(odata, rgb_image, memsize);
|
|
|
-
|
|
|
- if (!ImageTMP)
|
|
|
- {
|
|
|
- free_psram_heap(std::string(TAG) + "->odata", odata);
|
|
|
- }
|
|
|
- if (ImageTMP)
|
|
|
- ImageTMP->RGBImageRelease();
|
|
|
-
|
|
|
- RGBImageRelease();
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-void CRotateImage::RotateAntiAliasing(float _angle, int _centerx, int _centery)
|
|
|
-{
|
|
|
- int org_width, org_height;
|
|
|
- float m[2][3];
|
|
|
-
|
|
|
- float x_center = _centerx;
|
|
|
- float y_center = _centery;
|
|
|
- _angle = _angle / 180 * M_PI;
|
|
|
-
|
|
|
- if (doflip)
|
|
|
- {
|
|
|
- org_width = width;
|
|
|
- org_height = height;
|
|
|
- height = org_width;
|
|
|
- width = org_height;
|
|
|
- x_center = x_center - (org_width/2) + (org_height/2);
|
|
|
- y_center = y_center + (org_width/2) - (org_height/2);
|
|
|
- if (ImageOrg)
|
|
|
- {
|
|
|
- ImageOrg->height = height;
|
|
|
- ImageOrg->width = width;
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- org_width = width;
|
|
|
- org_height = height;
|
|
|
- }
|
|
|
-
|
|
|
- m[0][0] = cos(_angle);
|
|
|
- m[0][1] = sin(_angle);
|
|
|
- m[0][2] = (1 - m[0][0]) * x_center - m[0][1] * y_center;
|
|
|
-
|
|
|
- m[1][0] = -m[0][1];
|
|
|
- m[1][1] = m[0][0];
|
|
|
- m[1][2] = m[0][1] * x_center + (1 - m[0][0]) * y_center;
|
|
|
-
|
|
|
- if (doflip)
|
|
|
- {
|
|
|
- m[0][2] = m[0][2] + (org_width/2) - (org_height/2);
|
|
|
- m[1][2] = m[1][2] - (org_width/2) + (org_height/2);
|
|
|
- }
|
|
|
-
|
|
|
- int memsize = width * height * channels;
|
|
|
- uint8_t* odata;
|
|
|
- if (ImageTMP)
|
|
|
- {
|
|
|
- odata = ImageTMP->RGBImageLock();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- odata = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->odata", memsize, MALLOC_CAP_SPIRAM);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- int x_source_1, y_source_1, x_source_2, y_source_2;
|
|
|
- float x_source, y_source;
|
|
|
- float quad_ul, quad_ur, quad_ol, quad_or;
|
|
|
- stbi_uc* p_target;
|
|
|
- stbi_uc *p_source_ul, *p_source_ur, *p_source_ol, *p_source_or;
|
|
|
-
|
|
|
- RGBImageLock();
|
|
|
-
|
|
|
- // y outer / x inner: write the target (and mostly read the source) sequentially in PSRAM
|
|
|
- for (int y = 0; y < height; ++y)
|
|
|
- for (int x = 0; x < width; ++x)
|
|
|
- {
|
|
|
- p_target = odata + (channels * (y * width + x));
|
|
|
-
|
|
|
- x_source = (m[0][0] * x + m[0][1] * y);
|
|
|
- y_source = (m[1][0] * x + m[1][1] * y);
|
|
|
-
|
|
|
- x_source += (m[0][2]);
|
|
|
- y_source += (m[1][2]);
|
|
|
-
|
|
|
- x_source_1 = (int)x_source;
|
|
|
- x_source_2 = x_source_1 + 1;
|
|
|
- y_source_1 = (int)y_source;
|
|
|
- y_source_2 = y_source_1 + 1;
|
|
|
-
|
|
|
- quad_ul = (x_source_2 - x_source) * (y_source_2 - y_source);
|
|
|
- quad_ur = (1- (x_source_2 - x_source)) * (y_source_2 - y_source);
|
|
|
- quad_or = (x_source_2 - x_source) * (1-(y_source_2 - y_source));
|
|
|
- quad_ol = (1- (x_source_2 - x_source)) * (1-(y_source_2 - y_source));
|
|
|
-
|
|
|
-
|
|
|
- if ((x_source_1 >= 0) && (x_source_2 < org_width) && (y_source_1 >= 0) && (y_source_2 < org_height))
|
|
|
- {
|
|
|
- p_source_ul = rgb_image + (channels * (y_source_1 * org_width + x_source_1));
|
|
|
- p_source_ur = rgb_image + (channels * (y_source_1 * org_width + x_source_2));
|
|
|
- p_source_or = rgb_image + (channels * (y_source_2 * org_width + x_source_1));
|
|
|
- p_source_ol = rgb_image + (channels * (y_source_2 * org_width + x_source_2));
|
|
|
- for (int _channels = 0; _channels < channels; ++_channels)
|
|
|
- {
|
|
|
- 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);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- for (int _channels = 0; _channels < channels; ++_channels)
|
|
|
- p_target[_channels] = FILL_VALUE;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // memcpy(rgb_image, odata, memsize);
|
|
|
- memCopy(odata, rgb_image, memsize);
|
|
|
-
|
|
|
- if (!ImageTMP)
|
|
|
- {
|
|
|
- free_psram_heap(std::string(TAG) + "->odata", odata);
|
|
|
- }
|
|
|
- if (ImageTMP)
|
|
|
- ImageTMP->RGBImageRelease();
|
|
|
-
|
|
|
- RGBImageRelease();
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void CRotateImage::Rotate(float _angle)
|
|
|
-{
|
|
|
-// ESP_LOGD(TAG, "width %d, height %d", width, height);
|
|
|
- Rotate(_angle, width / 2, height / 2);
|
|
|
-}
|
|
|
-
|
|
|
-void CRotateImage::RotateAntiAliasing(float _angle)
|
|
|
-{
|
|
|
-// ESP_LOGD(TAG, "width %d, height %d", width, height);
|
|
|
- RotateAntiAliasing(_angle, width / 2, height / 2);
|
|
|
-}
|
|
|
-
|
|
|
-void CRotateImage::Translate(int _dx, int _dy)
|
|
|
-{
|
|
|
- if ((_dx == 0) && (_dy == 0)) { // identity transform, the copy would leave the image unchanged
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- int memsize = width * height * channels;
|
|
|
- uint8_t* odata;
|
|
|
- if (ImageTMP)
|
|
|
- {
|
|
|
- odata = ImageTMP->RGBImageLock();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- odata = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->odata", memsize, MALLOC_CAP_SPIRAM);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- int x_source, y_source;
|
|
|
- stbi_uc* p_target;
|
|
|
- stbi_uc* p_source;
|
|
|
-
|
|
|
- RGBImageLock();
|
|
|
-
|
|
|
- // y outer / x inner: write the target (and mostly read the source) sequentially in PSRAM
|
|
|
- for (int y = 0; y < height; ++y)
|
|
|
- for (int x = 0; x < width; ++x)
|
|
|
- {
|
|
|
- p_target = odata + (channels * (y * width + x));
|
|
|
-
|
|
|
- x_source = x - _dx;
|
|
|
- y_source = y - _dy;
|
|
|
-
|
|
|
- if ((x_source >= 0) && (x_source < width) && (y_source >= 0) && (y_source < height))
|
|
|
- {
|
|
|
- p_source = rgb_image + (channels * (y_source * width + x_source));
|
|
|
- for (int _channels = 0; _channels < channels; ++_channels)
|
|
|
- p_target[_channels] = p_source[_channels];
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- for (int _channels = 0; _channels < channels; ++_channels)
|
|
|
- p_target[_channels] = FILL_VALUE;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // memcpy(rgb_image, odata, memsize);
|
|
|
- memCopy(odata, rgb_image, memsize);
|
|
|
- if (!ImageTMP)
|
|
|
- {
|
|
|
- free_psram_heap(std::string(TAG) + "->odata", odata);
|
|
|
- }
|
|
|
-
|
|
|
- if (ImageTMP)
|
|
|
- {
|
|
|
- ImageTMP->RGBImageRelease();
|
|
|
- }
|
|
|
- RGBImageRelease();
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
+#include <string>
|
|
|
+#include <limits.h>
|
|
|
+#include "CRotateImage.h"
|
|
|
+#include "psram.h"
|
|
|
+
|
|
|
+static const char *TAG = "C ROTATE IMG";
|
|
|
+
|
|
|
+static bool getRotateBufferSize(int width, int height, int channels, size_t* size)
|
|
|
+{
|
|
|
+ if (size == NULL || width <= 0 || height <= 0 || channels <= 0) return false;
|
|
|
+ const size_t w = static_cast<size_t>(width);
|
|
|
+ const size_t h = static_cast<size_t>(height);
|
|
|
+ const size_t c = static_cast<size_t>(channels);
|
|
|
+ if (w > SIZE_MAX / h || w * h > SIZE_MAX / c) return false;
|
|
|
+ const size_t total = w * h * c;
|
|
|
+ if (total > static_cast<size_t>(INT_MAX)) return false;
|
|
|
+ *size = total;
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+// Value written to target pixels whose source lies outside the image (the gaps left by a rotation or
|
|
|
+// translation). It has to be black: the reference image is rotated in the browser (edit_reference.html),
|
|
|
+// which exports the uncovered canvas area as black. The alignment markers are cut from that reference,
|
|
|
+// so a marker that reaches into the gap would otherwise be matched against the wrong colour (#4169).
|
|
|
+static const stbi_uc FILL_VALUE = 0;
|
|
|
+
|
|
|
+CRotateImage::CRotateImage(std::string _name, CImageBasis *_org, CImageBasis *_temp, bool _flip) : CImageBasis(_name)
|
|
|
+{
|
|
|
+ rgb_image = _org->rgb_image;
|
|
|
+ channels = _org->channels;
|
|
|
+ width = _org->width;
|
|
|
+ height = _org->height;
|
|
|
+ bpp = _org->bpp;
|
|
|
+ externalImage = true;
|
|
|
+ ImageTMP = _temp;
|
|
|
+ ImageOrg = _org;
|
|
|
+ islocked = false;
|
|
|
+ doflip = _flip;
|
|
|
+}
|
|
|
+
|
|
|
+void CRotateImage::Rotate(float _angle, int _centerx, int _centery)
|
|
|
+{
|
|
|
+ if ((_angle == 0) && !doflip) { // identity transform, the copy would leave the image unchanged
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ int org_width, org_height;
|
|
|
+ float m[2][3];
|
|
|
+
|
|
|
+ float x_center = _centerx;
|
|
|
+ float y_center = _centery;
|
|
|
+ _angle = _angle / 180 * M_PI;
|
|
|
+
|
|
|
+ if (doflip)
|
|
|
+ {
|
|
|
+ org_width = width;
|
|
|
+ org_height = height;
|
|
|
+ height = org_width;
|
|
|
+ width = org_height;
|
|
|
+ x_center = x_center - (org_width/2) + (org_height/2);
|
|
|
+ y_center = y_center + (org_width/2) - (org_height/2);
|
|
|
+ if (ImageOrg)
|
|
|
+ {
|
|
|
+ ImageOrg->height = height;
|
|
|
+ ImageOrg->width = width;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ org_width = width;
|
|
|
+ org_height = height;
|
|
|
+ }
|
|
|
+
|
|
|
+ m[0][0] = cos(_angle);
|
|
|
+ m[0][1] = sin(_angle);
|
|
|
+ m[0][2] = (1 - m[0][0]) * x_center - m[0][1] * y_center;
|
|
|
+
|
|
|
+ m[1][0] = -m[0][1];
|
|
|
+ m[1][1] = m[0][0];
|
|
|
+ m[1][2] = m[0][1] * x_center + (1 - m[0][0]) * y_center;
|
|
|
+
|
|
|
+ if (doflip)
|
|
|
+ {
|
|
|
+ m[0][2] = m[0][2] + (org_width/2) - (org_height/2);
|
|
|
+ m[1][2] = m[1][2] - (org_width/2) + (org_height/2);
|
|
|
+ }
|
|
|
+
|
|
|
+ size_t bufferSize = 0;
|
|
|
+ if (!getRotateBufferSize(width, height, channels, &bufferSize)) {
|
|
|
+ if (doflip) {
|
|
|
+ width = org_width;
|
|
|
+ height = org_height;
|
|
|
+ if (ImageOrg) { ImageOrg->width = width; ImageOrg->height = height; }
|
|
|
+ }
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image transform: invalid dimensions or size overflow");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const int memsize = static_cast<int>(bufferSize);
|
|
|
+ uint8_t* odata;
|
|
|
+ if (ImageTMP) {
|
|
|
+ odata = ImageTMP->RGBImageLock();
|
|
|
+ } else {
|
|
|
+ odata = static_cast<unsigned char*>(malloc_psram_heap(
|
|
|
+ std::string(TAG) + "->odata", bufferSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
|
+ }
|
|
|
+ if (odata == NULL) {
|
|
|
+ if (doflip) {
|
|
|
+ width = org_width;
|
|
|
+ height = org_height;
|
|
|
+ if (ImageOrg) { ImageOrg->width = width; ImageOrg->height = height; }
|
|
|
+ }
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image transform: temporary PSRAM allocation failed");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ int x_source, y_source;
|
|
|
+ stbi_uc* p_target;
|
|
|
+ stbi_uc* p_source;
|
|
|
+
|
|
|
+ uint8_t* source = RGBImageLock();
|
|
|
+ if (source == NULL) {
|
|
|
+ if (ImageTMP) ImageTMP->RGBImageRelease();
|
|
|
+ else free_psram_heap(std::string(TAG) + "->odata", odata);
|
|
|
+ if (doflip) {
|
|
|
+ width = org_width;
|
|
|
+ height = org_height;
|
|
|
+ if (ImageOrg) { ImageOrg->width = width; ImageOrg->height = height; }
|
|
|
+ }
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image transform: source image is unavailable");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // y outer / x inner: write the target (and mostly read the source) sequentially in PSRAM
|
|
|
+ for (int y = 0; y < height; ++y)
|
|
|
+ for (int x = 0; x < width; ++x)
|
|
|
+ {
|
|
|
+ p_target = odata + (channels * (y * width + x));
|
|
|
+
|
|
|
+ x_source = int(m[0][0] * x + m[0][1] * y);
|
|
|
+ y_source = int(m[1][0] * x + m[1][1] * y);
|
|
|
+
|
|
|
+ x_source += int(m[0][2]);
|
|
|
+ y_source += int(m[1][2]);
|
|
|
+
|
|
|
+ if ((x_source >= 0) && (x_source < org_width) && (y_source >= 0) && (y_source < org_height))
|
|
|
+ {
|
|
|
+ p_source = source + (channels * (y_source * org_width + x_source));
|
|
|
+ for (int _channels = 0; _channels < channels; ++_channels)
|
|
|
+ p_target[_channels] = p_source[_channels];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ for (int _channels = 0; _channels < channels; ++_channels)
|
|
|
+ p_target[_channels] = FILL_VALUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // memcpy(rgb_image, odata, memsize);
|
|
|
+ memCopy(odata, source, memsize);
|
|
|
+
|
|
|
+ if (!ImageTMP)
|
|
|
+ {
|
|
|
+ free_psram_heap(std::string(TAG) + "->odata", odata);
|
|
|
+ }
|
|
|
+ if (ImageTMP)
|
|
|
+ ImageTMP->RGBImageRelease();
|
|
|
+
|
|
|
+ RGBImageRelease();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+void CRotateImage::RotateAntiAliasing(float _angle, int _centerx, int _centery)
|
|
|
+{
|
|
|
+ int org_width, org_height;
|
|
|
+ float m[2][3];
|
|
|
+
|
|
|
+ float x_center = _centerx;
|
|
|
+ float y_center = _centery;
|
|
|
+ _angle = _angle / 180 * M_PI;
|
|
|
+
|
|
|
+ if (doflip)
|
|
|
+ {
|
|
|
+ org_width = width;
|
|
|
+ org_height = height;
|
|
|
+ height = org_width;
|
|
|
+ width = org_height;
|
|
|
+ x_center = x_center - (org_width/2) + (org_height/2);
|
|
|
+ y_center = y_center + (org_width/2) - (org_height/2);
|
|
|
+ if (ImageOrg)
|
|
|
+ {
|
|
|
+ ImageOrg->height = height;
|
|
|
+ ImageOrg->width = width;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ org_width = width;
|
|
|
+ org_height = height;
|
|
|
+ }
|
|
|
+
|
|
|
+ m[0][0] = cos(_angle);
|
|
|
+ m[0][1] = sin(_angle);
|
|
|
+ m[0][2] = (1 - m[0][0]) * x_center - m[0][1] * y_center;
|
|
|
+
|
|
|
+ m[1][0] = -m[0][1];
|
|
|
+ m[1][1] = m[0][0];
|
|
|
+ m[1][2] = m[0][1] * x_center + (1 - m[0][0]) * y_center;
|
|
|
+
|
|
|
+ if (doflip)
|
|
|
+ {
|
|
|
+ m[0][2] = m[0][2] + (org_width/2) - (org_height/2);
|
|
|
+ m[1][2] = m[1][2] - (org_width/2) + (org_height/2);
|
|
|
+ }
|
|
|
+
|
|
|
+ size_t bufferSize = 0;
|
|
|
+ if (!getRotateBufferSize(width, height, channels, &bufferSize)) {
|
|
|
+ if (doflip) {
|
|
|
+ width = org_width;
|
|
|
+ height = org_height;
|
|
|
+ if (ImageOrg) { ImageOrg->width = width; ImageOrg->height = height; }
|
|
|
+ }
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image transform: invalid dimensions or size overflow");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const int memsize = static_cast<int>(bufferSize);
|
|
|
+ uint8_t* odata;
|
|
|
+ if (ImageTMP) {
|
|
|
+ odata = ImageTMP->RGBImageLock();
|
|
|
+ } else {
|
|
|
+ odata = static_cast<unsigned char*>(malloc_psram_heap(
|
|
|
+ std::string(TAG) + "->odata", bufferSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
|
+ }
|
|
|
+ if (odata == NULL) {
|
|
|
+ if (doflip) {
|
|
|
+ width = org_width;
|
|
|
+ height = org_height;
|
|
|
+ if (ImageOrg) { ImageOrg->width = width; ImageOrg->height = height; }
|
|
|
+ }
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image transform: temporary PSRAM allocation failed");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ int x_source_1, y_source_1, x_source_2, y_source_2;
|
|
|
+ float x_source, y_source;
|
|
|
+ float quad_ul, quad_ur, quad_ol, quad_or;
|
|
|
+ stbi_uc* p_target;
|
|
|
+ stbi_uc *p_source_ul, *p_source_ur, *p_source_ol, *p_source_or;
|
|
|
+
|
|
|
+ uint8_t* source = RGBImageLock();
|
|
|
+ if (source == NULL) {
|
|
|
+ if (ImageTMP) ImageTMP->RGBImageRelease();
|
|
|
+ else free_psram_heap(std::string(TAG) + "->odata", odata);
|
|
|
+ if (doflip) {
|
|
|
+ width = org_width;
|
|
|
+ height = org_height;
|
|
|
+ if (ImageOrg) { ImageOrg->width = width; ImageOrg->height = height; }
|
|
|
+ }
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image transform: source image is unavailable");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // y outer / x inner: write the target (and mostly read the source) sequentially in PSRAM
|
|
|
+ for (int y = 0; y < height; ++y)
|
|
|
+ for (int x = 0; x < width; ++x)
|
|
|
+ {
|
|
|
+ p_target = odata + (channels * (y * width + x));
|
|
|
+
|
|
|
+ x_source = (m[0][0] * x + m[0][1] * y);
|
|
|
+ y_source = (m[1][0] * x + m[1][1] * y);
|
|
|
+
|
|
|
+ x_source += (m[0][2]);
|
|
|
+ y_source += (m[1][2]);
|
|
|
+
|
|
|
+ x_source_1 = (int)x_source;
|
|
|
+ x_source_2 = x_source_1 + 1;
|
|
|
+ y_source_1 = (int)y_source;
|
|
|
+ y_source_2 = y_source_1 + 1;
|
|
|
+
|
|
|
+ quad_ul = (x_source_2 - x_source) * (y_source_2 - y_source);
|
|
|
+ quad_ur = (1- (x_source_2 - x_source)) * (y_source_2 - y_source);
|
|
|
+ quad_or = (x_source_2 - x_source) * (1-(y_source_2 - y_source));
|
|
|
+ quad_ol = (1- (x_source_2 - x_source)) * (1-(y_source_2 - y_source));
|
|
|
+
|
|
|
+
|
|
|
+ if ((x_source_1 >= 0) && (x_source_2 < org_width) && (y_source_1 >= 0) && (y_source_2 < org_height))
|
|
|
+ {
|
|
|
+ p_source_ul = source + (channels * (y_source_1 * org_width + x_source_1));
|
|
|
+ p_source_ur = source + (channels * (y_source_1 * org_width + x_source_2));
|
|
|
+ p_source_or = source + (channels * (y_source_2 * org_width + x_source_1));
|
|
|
+ p_source_ol = source + (channels * (y_source_2 * org_width + x_source_2));
|
|
|
+ for (int _channels = 0; _channels < channels; ++_channels)
|
|
|
+ {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ for (int _channels = 0; _channels < channels; ++_channels)
|
|
|
+ p_target[_channels] = FILL_VALUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // memcpy(rgb_image, odata, memsize);
|
|
|
+ memCopy(odata, source, memsize);
|
|
|
+
|
|
|
+ if (!ImageTMP)
|
|
|
+ {
|
|
|
+ free_psram_heap(std::string(TAG) + "->odata", odata);
|
|
|
+ }
|
|
|
+ if (ImageTMP)
|
|
|
+ ImageTMP->RGBImageRelease();
|
|
|
+
|
|
|
+ RGBImageRelease();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void CRotateImage::Rotate(float _angle)
|
|
|
+{
|
|
|
+// ESP_LOGD(TAG, "width %d, height %d", width, height);
|
|
|
+ Rotate(_angle, width / 2, height / 2);
|
|
|
+}
|
|
|
+
|
|
|
+void CRotateImage::RotateAntiAliasing(float _angle)
|
|
|
+{
|
|
|
+// ESP_LOGD(TAG, "width %d, height %d", width, height);
|
|
|
+ RotateAntiAliasing(_angle, width / 2, height / 2);
|
|
|
+}
|
|
|
+
|
|
|
+void CRotateImage::Translate(int _dx, int _dy)
|
|
|
+{
|
|
|
+ if ((_dx == 0) && (_dy == 0)) { // identity transform, the copy would leave the image unchanged
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ size_t bufferSize = 0;
|
|
|
+ if (!getRotateBufferSize(width, height, channels, &bufferSize)) {
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image transform: invalid dimensions or size overflow");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const int memsize = static_cast<int>(bufferSize);
|
|
|
+ uint8_t* odata;
|
|
|
+ if (ImageTMP) {
|
|
|
+ odata = ImageTMP->RGBImageLock();
|
|
|
+ } else {
|
|
|
+ odata = static_cast<unsigned char*>(malloc_psram_heap(
|
|
|
+ std::string(TAG) + "->odata", bufferSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
|
+ }
|
|
|
+ if (odata == NULL) {
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image transform: temporary PSRAM allocation failed");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ int x_source, y_source;
|
|
|
+ stbi_uc* p_target;
|
|
|
+ stbi_uc* p_source;
|
|
|
+
|
|
|
+ uint8_t* source = RGBImageLock();
|
|
|
+ if (source == NULL) {
|
|
|
+ if (ImageTMP) ImageTMP->RGBImageRelease();
|
|
|
+ else free_psram_heap(std::string(TAG) + "->odata", odata);
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image transform: source image is unavailable");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // y outer / x inner: write the target (and mostly read the source) sequentially in PSRAM
|
|
|
+ for (int y = 0; y < height; ++y)
|
|
|
+ for (int x = 0; x < width; ++x)
|
|
|
+ {
|
|
|
+ p_target = odata + (channels * (y * width + x));
|
|
|
+
|
|
|
+ x_source = x - _dx;
|
|
|
+ y_source = y - _dy;
|
|
|
+
|
|
|
+ if ((x_source >= 0) && (x_source < width) && (y_source >= 0) && (y_source < height))
|
|
|
+ {
|
|
|
+ p_source = source + (channels * (y_source * width + x_source));
|
|
|
+ for (int _channels = 0; _channels < channels; ++_channels)
|
|
|
+ p_target[_channels] = p_source[_channels];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ for (int _channels = 0; _channels < channels; ++_channels)
|
|
|
+ p_target[_channels] = FILL_VALUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // memcpy(rgb_image, odata, memsize);
|
|
|
+ memCopy(odata, source, memsize);
|
|
|
+ if (!ImageTMP)
|
|
|
+ {
|
|
|
+ free_psram_heap(std::string(TAG) + "->odata", odata);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ImageTMP)
|
|
|
+ {
|
|
|
+ ImageTMP->RGBImageRelease();
|
|
|
+ }
|
|
|
+ RGBImageRelease();
|
|
|
+
|
|
|
+}
|
|
|
+
|