Просмотр исходного кода

Restore used image translation and prune unused helpers

SybexXx 3 дней назад
Родитель
Сommit
b62cfcc365

+ 0 - 155
code/components/jomjol_helper/Helper.cpp

@@ -92,10 +92,6 @@ size_t getESPHeapSize()
 	return heap_caps_get_free_size(MALLOC_CAP_8BIT);
 }
 
-size_t getInternalESPHeapSize()
-{
-	return heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
-}
 
 string getSDCardPartitionSize()
 {
@@ -180,139 +176,12 @@ string getSDCardSectorSize()
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
 
-bool copy_string(char *dst, size_t dst_size, const char *src)
-{
-	if (dst == nullptr || src == nullptr || dst_size == 0)
-	{
-		return false;
-	}
 
-	size_t len = strlen(src);
 
-	if (len >= dst_size)
-	{
-		dst[0] = '\0';
-		return false; // Target too small
-	}
 
-	memcpy(dst, src, len + 1);
-	return true;
-}
 
-void mem_copy(uint8_t *_target, uint8_t *_source, int _size)
-{
-	if ((_target != nullptr) && (_source != nullptr) && (_size > 0))
-	{
-		uint8_t *dst = _target;
-		uint8_t *src = _source;
 
-		for (int i = 0; i < _size; ++i)
-		{
-			*dst++ = *src++;
-		}
-	}
-}
 
-void mem_copy32(void *_target, const void *_source, size_t _size)
-{
-	if ((_target != nullptr) && (_source != nullptr) && (_size > 0))
-	{
-		uint32_t *d32 = (uint32_t *)_target;
-		const uint32_t *s32 = (const uint32_t *)_source;
-
-		size_t words = _size / 4;
-
-		for (size_t i = 0; i < words; i++)
-		{
-			d32[i] = s32[i];
-		}
-
-		// Copy remaining bytes
-		uint8_t *d8 = (uint8_t *)(d32 + words);
-		const uint8_t *s8 = (const uint8_t *)(s32 + words);
-
-		for (size_t i = 0; i < (_size % 4); i++)
-		{
-			d8[i] = s8[i];
-		}
-	}
-}
-
-float fast_atan2(float y, float x)
-{
-	float abs_y = fabs(y) + 1e-10;
-	float r = (x - abs_y) / (x + abs_y);
-	float angle = 0.78539816339 - 0.78539816339 * r;
-	return (y < 0) ? -angle : angle;
-}
-
-double pow10_double(int pot)
-{
-	static const double pow10_table[] = {
-		1.0,
-		10.0,
-		100.0,
-		1000.0,
-		10000.0,
-		100000.0,
-		1000000.0,
-		10000000.0,
-		100000000.0,
-		1000000000.0};
-
-	if (pot >= 0 && pot < (int)(sizeof(pow10_table) / sizeof(pow10_table[0])))
-	{
-		return pow10_table[pot];
-	}
-
-	// Fallback if pot is larger
-	double result = 1.0;
-	for (int i = 0; i < pot; ++i)
-	{
-		result *= 10.0;
-	}
-
-	return result;
-}
-
-uint32_t pow10_int(int pot)
-{
-	// Lookup table for small exponents 0-9
-	static const uint32_t table[] = {
-		1,		   // 10^0
-		10,		   // 10^1
-		100,	   // 10^2
-		1000,	   // 10^3
-		10000,	   // 10^4
-		100000,	   // 10^5
-		1000000,   // 10^6
-		10000000,  // 10^7
-		100000000, // 10^8
-		1000000000 // 10^9
-	};
-
-	if (pot >= 0 && pot < (int)(sizeof(table) / sizeof(table[0])))
-	{
-		return table[pot];
-	}
-
-	// Fallback for larger pot
-	uint32_t result = 1;
-	for (int i = 0; i < pot; ++i)
-	{
-		result *= 10;
-	}
-
-	return result;
-}
-
-void memCopyGen(uint8_t *_source, uint8_t *_target, int _size)
-{
-	for (int i = 0; i < _size; ++i)
-	{
-		*(_target + i) = *(_source + i);
-	}
-}
 
 std::string FormatFileName(std::string input)
 {
@@ -545,20 +414,6 @@ bool FileExists(string filename)
 	return true;
 }
 
-bool FolderExists(string foldername)
-{
-	DIR *fpSourceFolder = opendir(foldername.c_str());
-
-	// Sourcefolder does not exist
-	if (!fpSourceFolder)
-	{
-		return false;
-	}
-
-	closedir(fpSourceFolder);
-
-	return true;
-}
 
 bool DeleteFile(string filename)
 {
@@ -1194,16 +1049,6 @@ string RundeOutput(double _in, int _anzNachkomma)
 	return stream.str();
 }
 
-string getMac(void)
-{
-	uint8_t macInt[6];
-	char macFormated[6 * 2 + 5 + 1]; // AA:BB:CC:DD:EE:FF
-
-	esp_read_mac(macInt, ESP_MAC_WIFI_STA);
-	sprintf(macFormated, "%02X:%02X:%02X:%02X:%02X:%02X", macInt[0], macInt[1], macInt[2], macInt[3], macInt[4], macInt[5]);
-
-	return macFormated;
-}
 
 void setSystemStatusFlag(SystemStatusFlag_t flag)
 {

+ 1 - 14
code/components/jomjol_helper/Helper.h

@@ -21,7 +21,6 @@ bool RenameFile(string from, string to);
 bool RenameFolder(string from, string to);
 bool MakeDir(std::string _what);
 bool FileExists(string filename);
-bool FolderExists(string foldername);
 
 string RundeOutput(double _in, int _anzNachkomma);
 
@@ -45,21 +44,11 @@ float temperatureRead();
 std::string intToHexString(int _valueInt);
 time_t addDays(time_t startTime, int days);
 
-bool copy_string(char *dst, size_t dst_size, const char *src);
-void mem_copy(uint8_t *_target, uint8_t *_source, int _size);
-void mem_copy32(void *_target, const void *_source, size_t size);
-float fast_atan2(float y, float x);
-
-double pow10_double(int pot); // faster than pow(10, pot)
-uint32_t pow10_int(int pot);  // faster than pow(10, pot)
-
-void memCopyGen(uint8_t* _source, uint8_t* _target, int _size);
-
 std::vector<string> HelperZerlegeZeile(std::string input, std::string _delimiter);
 std::vector<std::string> ZerlegeZeile(std::string input, std::string delimiter = " =, \t");
 
 ///////////////////////////
-size_t getInternalESPHeapSize();
+
 size_t getESPHeapSize();
 string getESPHeapInfo();
 
@@ -75,8 +64,6 @@ string getSDCardName();
 string getSDCardCapacity();
 string getSDCardSectorSize();
 
-string getMac(void);
-
 /* Error bit fields
    One bit per error
    Make sure it matches https://jomjol.github.io/AI-on-the-edge-device-docs/Error-Codes */

+ 79 - 0
code/components/jomjol_image_proc/CRotateImage.cpp

@@ -369,4 +369,83 @@ void CRotateImage::RotateAntiAliasing(float _angle)
     RotateAntiAliasing(_angle, width / 2, height / 2);
 }
 
+void CRotateImage::Translate(int _dx, int _dy)
+{
+    ScopedImageAccess sourceAccess(ImageOrg != NULL ? ImageOrg : this);
+    if (!sourceAccess.isLocked()) {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image transform: unable to lock source image");
+        return;
+    }
+    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();
+
+}
 

+ 27 - 25
code/components/jomjol_image_proc/CRotateImage.h

@@ -1,26 +1,28 @@
-#pragma once
-
-#ifndef CROTATEIMAGE_H
-#define CROTATEIMAGE_H
-
-#include "CImageBasis.h"
-
-
-class CRotateImage: public CImageBasis
-{
-
-    public:
-        CImageBasis *ImageTMP, *ImageOrg;
-        bool doflip;
-        CRotateImage(std::string name, std::string _image, bool _flip = false) : CImageBasis(name, _image) {ImageTMP = NULL; ImageOrg = NULL; doflip = _flip;};
-        CRotateImage(std::string name, uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp, bool _flip = false) : CImageBasis(name, _rgb_image, _channels, _width, _height, _bpp) {ImageTMP = NULL;  ImageOrg = NULL; doflip = _flip;};
-        CRotateImage(std::string name, CImageBasis *_org, CImageBasis *_temp, bool _flip = false);
-
-        void Rotate(float _angle);
-        void RotateAntiAliasing(float _angle);
-       
-        void Rotate(float _angle, int _centerx, int _centery);
-        void RotateAntiAliasing(float _angle, int _centerx, int _centery);
-};
-
+#pragma once
+
+#ifndef CROTATEIMAGE_H
+#define CROTATEIMAGE_H
+
+#include "CImageBasis.h"
+
+
+class CRotateImage: public CImageBasis
+{
+
+    public:
+        CImageBasis *ImageTMP, *ImageOrg;
+        bool doflip;
+        CRotateImage(std::string name, std::string _image, bool _flip = false) : CImageBasis(name, _image) {ImageTMP = NULL; ImageOrg = NULL; doflip = _flip;};
+        CRotateImage(std::string name, uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp, bool _flip = false) : CImageBasis(name, _rgb_image, _channels, _width, _height, _bpp) {ImageTMP = NULL;  ImageOrg = NULL; doflip = _flip;};
+        CRotateImage(std::string name, CImageBasis *_org, CImageBasis *_temp, bool _flip = false);
+
+        void Rotate(float _angle);
+        void RotateAntiAliasing(float _angle);
+       
+        void Rotate(float _angle, int _centerx, int _centery);
+        void RotateAntiAliasing(float _angle, int _centerx, int _centery);
+
+        void Translate(int _dx, int _dy);
+};
+
 #endif //CROTATEIMAGE_H