Add support Images in nativeEditors

This commit is contained in:
Oleg.Korshul
2024-10-26 15:06:09 +03:00
parent cd1bb3b658
commit a84c8cc471
9 changed files with 254 additions and 60 deletions

View File

@ -555,12 +555,12 @@ namespace NSJSON
#ifdef JSON_DEBUG
throw std::bad_cast();
#endif
return ifInvalid;
return ImageFormat::ifInvalid;
}
return static_cast<CImage*>(m_internal->m_value.get())->getFormat();
}
void IValue::Externalize()
void IValue::ImageExternalize()
{
if (m_internal->m_type != CTypedValue::vtImage)
{
@ -572,6 +572,18 @@ namespace NSJSON
static_cast<CImage*>(m_internal->m_value.get())->externalize();
}
void IValue::ImageAlloc(const int& width, const int& height, const ImageFormat& format)
{
if (m_internal->m_type != CTypedValue::vtImage)
{
#ifdef JSON_DEBUG
throw std::bad_cast();
#endif
return;
}
static_cast<CImage*>(m_internal->m_value.get())->alloc(width, height, format);
}
CValue::CValue() : IValue()
{
}
@ -675,9 +687,17 @@ namespace NSJSON
return ret;
}
CValue CValue::CreateEmptyImage(ImageFormat format)
{
CValue ret;
ret.m_internal->m_value = std::make_shared<CImage>((BYTE*)NULL, 0, 0, format, false);
ret.m_internal->m_type = CTypedValue::vtImage;
return ret;
}
BYTE* CValue::AllocImageBits(int width, int height)
{
return new BYTE[width * height];
return new BYTE[4 * width * height];
}
void CValue::FreeImageBits(BYTE* bits)

View File

@ -30,7 +30,7 @@ namespace NSJSON
{
typedef unsigned char BYTE;
enum ImageFormat
enum class ImageFormat
{
ifRGBA,
ifBGRA,
@ -231,7 +231,12 @@ namespace NSJSON
/**
* Make image bits external.
*/
void Externalize();
void ImageExternalize();
/**
* Alloc image bits as internal.
*/
void ImageAlloc(const int& width, const int& height, const ImageFormat& format);
protected:
std::shared_ptr<CTypedValue> m_internal;
@ -301,7 +306,8 @@ namespace NSJSON
* @param isExternalize If true the memory will not be reclaimed when the created image is destroyed.
* If this parameter is false then the memory will be released using FreeImageBits() during the image object destruction.
*/
static CValue CreateImage(BYTE* bits, int width, int height, ImageFormat format = ifBGRA, bool isExternalize = true);
static CValue CreateImage(BYTE* bits, int width, int height, ImageFormat format = ImageFormat::ifBGRA, bool isExternalize = true);
static CValue CreateEmptyImage(ImageFormat format = ImageFormat::ifBGRA);
/**
* Allocates the memory for an image.
* @param width The width of the image.

View File

@ -212,7 +212,8 @@ namespace NSJSON
return ret;
}
CImage::CImage(BYTE* bits, int width, int height, ImageFormat format, bool isExternalize) : m_bits(bits), m_width(width), m_height(height), m_format(format), m_isExternalize(isExternalize)
CImage::CImage(BYTE* bits, const int& width, const int& height, const ImageFormat& format, const bool& isExternalize) :
m_bits(bits), m_width(width), m_height(height), m_format(format), m_isExternalize(isExternalize)
{
}
@ -224,6 +225,20 @@ namespace NSJSON
}
}
void CImage::alloc(const int& width, const int& height, const ImageFormat& format)
{
if (!m_isExternalize && m_bits)
{
CValue::FreeImageBits(m_bits);
}
m_bits = CValue::AllocImageBits(width, height);
m_width = width;
m_height = height;
m_format = format;
m_isExternalize = false;
}
BYTE* CImage::getBits()
{
return m_bits;

View File

@ -117,7 +117,7 @@ namespace NSJSON
class CImage : public IBaseValue
{
public:
CImage(BYTE* bits, int width, int height, ImageFormat format, bool isExternalize = true);
CImage(BYTE* bits, const int& width, const int& height, const ImageFormat& format, const bool& isExternalize = true);
~CImage();
public:
@ -126,6 +126,7 @@ namespace NSJSON
int getHeight();
ImageFormat getFormat();
void externalize();
void alloc(const int& width, const int& height, const ImageFormat& format);
private:
BYTE* m_bits;

View File

@ -3,14 +3,49 @@
#include "json.h"
#include "../js_internal/js_base.h"
#include "../embed/GraphicsEmbed.h"
#include <cmath>
class CAppImage : public CGraphicsAppImage
{
private:
NSJSON::CValueRef* m_image;
public:
CAppImage(const NSJSON::CValue& image) : CGraphicsAppImage()
{
m_image = new NSJSON::CValueRef(image);
}
virtual ~CAppImage()
{
if (m_image)
delete m_image;
}
public:
virtual unsigned char* GetBits(int& w, int& h)
{
unsigned char* bits = m_image->GetImageBits();
if (NULL != bits)
{
w = m_image->GetImageWidth();
h = m_image->GetImageHeight();
}
return bits;
}
virtual unsigned char* AllocBits(const int& w, const int& h)
{
m_image->ImageAlloc(w, h, GetRgba() ? NSJSON::ImageFormat::ifRGBA : NSJSON::ImageFormat::ifBGRA);
return m_image->GetImageBits();
}
};
namespace NSJSON
{
static JSSmart<NSJSBase::CJSValue> toJS(const CValue& value)
{
if (value.IsUndefined() || value.IsImage())
if (value.IsUndefined())
return NSJSBase::CJSContext::createUndefined();
if (value.IsNull())
return NSJSBase::CJSContext::createNull();
@ -50,6 +85,12 @@ namespace NSJSON
JSSmart<NSJSBase::CJSTypedArray> jsTypedArr = NSJSBase::CJSContext::createUint8Array(const_cast<BYTE*>(value.GetData()), value.GetCount());
ret = jsTypedArr->toValue();
}
else if (value.IsImage())
{
JSSmart<CJSObject> wrap = CJSContext::createEmbedObject("CGraphicsEmbed");
((CGraphicsEmbed*)wrap->getNative())->SetAppImage(new CAppImage(value));
ret = wrap->toValue();
}
// objects (there is no need for IsObject())
else
{