CImageBasis.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma once
  2. #ifndef CIMAGEBASIS_H
  3. #define CIMAGEBASIS_H
  4. #include <stdint.h>
  5. #include <stddef.h>
  6. #include <new>
  7. #include <stdlib.h>
  8. #include <string>
  9. #include <esp_http_server.h>
  10. #include "../../include/defines.h"
  11. #include <math.h>
  12. #include "../stb/stb_image.h"
  13. #include "../stb/stb_image_write.h"
  14. #include "../stb/stb_image_resize.h"
  15. #include "esp_heap_caps.h"
  16. struct ImageData
  17. {
  18. uint8_t data[MAX_JPG_SIZE];
  19. size_t size = 0;
  20. static void* operator new(size_t size) {
  21. void* ptr = heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  22. if (ptr == nullptr) abort();
  23. return ptr;
  24. }
  25. static void* operator new(size_t size, const std::nothrow_t&) noexcept {
  26. return heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  27. }
  28. static void operator delete(void* ptr) noexcept { heap_caps_free(ptr); }
  29. static void operator delete(void* ptr, const std::nothrow_t&) noexcept { heap_caps_free(ptr); }
  30. };
  31. class CImageBasis
  32. {
  33. protected:
  34. bool externalImage;
  35. bool ownsSharedTempImageLease = false;
  36. std::string filename;
  37. std::string name; // Just used for diagnostics
  38. int memsize = 0;
  39. void memCopy(uint8_t* _source, uint8_t* _target, int _size);
  40. bool isInImage(int x, int y);
  41. bool islocked;
  42. public:
  43. uint8_t* rgb_image = NULL;
  44. int channels;
  45. int width, height, bpp;
  46. uint8_t * RGBImageLock(int _waitmaxsec = 60);
  47. void RGBImageRelease();
  48. int getWidth(){return this->width;};
  49. int getHeight(){return this->height;};
  50. int getChannels(){return this->channels;};
  51. void drawRect(int x, int y, int dx, int dy, int r = 255, int g = 255, int b = 255, int thickness = 1);
  52. void drawLine(int x1, int y1, int x2, int y2, int r, int g, int b, int thickness = 1);
  53. void drawCircle(int x1, int y1, int rad, int r, int g, int b, int thickness = 1);
  54. void drawEllipse(int x1, int y1, int radx, int rady, int r, int g, int b, int thickness = 1);
  55. void setPixelColor(int x, int y, int r, int g, int b);
  56. void Negative(void);
  57. void Contrast(float _contrast);
  58. bool ImageOkay();
  59. bool CopyFromMemory(uint8_t* _source, int _size);
  60. bool SetIndepended();
  61. void CreateEmptyImage(int _width, int _height, int _channels);
  62. void EmptyImage();
  63. CImageBasis(std::string name);
  64. CImageBasis(std::string name, std::string _image);
  65. CImageBasis(std::string name, uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp);
  66. CImageBasis(std::string name, int _width, int _height, int _channels);
  67. CImageBasis(std::string name, CImageBasis *_copyfrom);
  68. void Resize(int _new_dx, int _new_dy);
  69. bool Resize(int _new_dx, int _new_dy, CImageBasis *_target);
  70. void crop_image(unsigned short cropLeft, unsigned short cropRight, unsigned short cropTop, unsigned short cropBottom);
  71. bool LoadFromMemory(stbi_uc *_buffer, int len);
  72. ImageData* writeToMemoryAsJPG(const int quality = 90);
  73. void writeToMemoryAsJPG(ImageData* ii, const int quality = 90);
  74. esp_err_t SendJPGtoHTTP(httpd_req_t *req, const int quality = 90);
  75. ~CImageBasis();
  76. void SaveToFile(std::string _imageout);
  77. };
  78. #endif //CIMAGEBASIS_H