summaryrefslogtreecommitdiff
path: root/source/modules/asura-core/image
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-04-02 08:47:15 +0800
committerchai <chaifix@163.com>2019-04-02 08:47:15 +0800
commit250e30d73f09e9da2b5a81d0fbae63744ae12a73 (patch)
tree0f55daf334c073e1779d7a1284799a2056aad714 /source/modules/asura-core/image
parent66fe16dd5ed57ae958fc25158d0defae2e6fae6a (diff)
*misc
Diffstat (limited to 'source/modules/asura-core/image')
-rw-r--r--source/modules/asura-core/image/binding/_image_data.cpp111
-rw-r--r--source/modules/asura-core/image/binding/_image_decode_task.cpp21
-rw-r--r--source/modules/asura-core/image/image_data.cpp62
-rw-r--r--source/modules/asura-core/image/image_data.h85
-rw-r--r--source/modules/asura-core/image/image_decode_task.cpp0
-rw-r--r--source/modules/asura-core/image/image_decode_task.h25
-rw-r--r--source/modules/asura-core/image/image_decoder.h35
-rw-r--r--source/modules/asura-core/image/png_decoder.cpp19
-rw-r--r--source/modules/asura-core/image/png_decoder.h27
-rw-r--r--source/modules/asura-core/image/stb_decoder.cpp73
-rw-r--r--source/modules/asura-core/image/stb_decoder.h28
11 files changed, 486 insertions, 0 deletions
diff --git a/source/modules/asura-core/image/binding/_image_data.cpp b/source/modules/asura-core/image/binding/_image_data.cpp
new file mode 100644
index 0000000..ac9473b
--- /dev/null
+++ b/source/modules/asura-core/image/binding/_image_data.cpp
@@ -0,0 +1,111 @@
+#include <asura-utils/threading/thread.h>
+#include <asura-utils/io/data_buffer.h>
+
+#include "../image_data.h"
+
+using namespace std;
+using namespace AEThreading;
+using namespace AEIO;
+
+namespace AsuraEngine
+{
+ namespace Graphics
+ {
+
+ LUAX_REGISTRY(ImageData)
+ {
+ LUAX_REGISTER_METHODS(state,
+ { "New", _New },
+ { "GetPixel", _GetPixel },
+ { "GetSize", _GetSize },
+ { "GetWidth", _GetWidth },
+ { "GetHeight", _GetHeight },
+ { "GetPixelFormat", _GetPixelFormat },
+ { "Decode", _Decode },
+ { "DecodeAsync", _DecodeAsync },
+ { "IsAvailable", _IsAvailable }
+ );
+ }
+
+ LUAX_POSTPROCESS(ImageData)
+ {
+
+ }
+
+ // ImageData.New()
+ LUAX_IMPL_METHOD(ImageData, _New)
+ {
+ LUAX_STATE(L);
+
+ return 0;
+ }
+
+ // imagedata:GetPixel()
+ LUAX_IMPL_METHOD(ImageData, _GetPixel)
+ {
+ LUAX_PREPARE(L, ImageData);
+
+ return 0;
+ }
+
+ // imagedata:GetSize()
+ LUAX_IMPL_METHOD(ImageData, _GetSize)
+ {
+ LUAX_PREPARE(L, ImageData);
+
+ return 0;
+ }
+
+ // imagedata:GetWidth()
+ LUAX_IMPL_METHOD(ImageData, _GetWidth)
+ {
+ LUAX_PREPARE(L, ImageData);
+
+ return 0;
+ }
+
+ // imagedata:GetHeight()
+ LUAX_IMPL_METHOD(ImageData, _GetHeight)
+ {
+ LUAX_PREPARE(L, ImageData);
+
+ return 0;
+ }
+
+ // imagedata:GetPixelFormat()
+ LUAX_IMPL_METHOD(ImageData, _GetPixelFormat)
+ {
+ LUAX_PREPARE(L, ImageData);
+
+ return 0;
+ }
+
+ // imagedata:Decode()
+ LUAX_IMPL_METHOD(ImageData, _Decode)
+ {
+ LUAX_PREPARE(L, ImageData);
+
+ return 0;
+ }
+
+ // imagedata:DecodeAsync(thread, databuffer, callback)
+ LUAX_IMPL_METHOD(ImageData, _DecodeAsync)
+ {
+ LUAX_PREPARE(L, ImageData);
+
+ Thread* thread = state.CheckUserdata<Thread>(2);
+ DataBuffer* buffer = state.CheckUserdata<DataBuffer>(3);
+
+ return 0;
+ }
+
+ // imagedata:IsAvailable()
+ LUAX_IMPL_METHOD(ImageData, _IsAvailable)
+ {
+ LUAX_PREPARE(L, ImageData);
+
+ return 0;
+ }
+
+ }
+}
diff --git a/source/modules/asura-core/image/binding/_image_decode_task.cpp b/source/modules/asura-core/image/binding/_image_decode_task.cpp
new file mode 100644
index 0000000..76b544b
--- /dev/null
+++ b/source/modules/asura-core/image/binding/_image_decode_task.cpp
@@ -0,0 +1,21 @@
+#include "../image_decode_task.h"
+
+using namespace std;
+
+namespace AsuraEngine
+{
+ namespace Graphics
+ {
+
+ LUAX_REGISTRY(ImageDecodeTask)
+ {
+
+ }
+
+ LUAX_POSTPROCESS(ImageDecodeTask)
+ {
+
+ }
+
+ }
+}
diff --git a/source/modules/asura-core/image/image_data.cpp b/source/modules/asura-core/image/image_data.cpp
new file mode 100644
index 0000000..1a6d3a2
--- /dev/null
+++ b/source/modules/asura-core/image/image_data.cpp
@@ -0,0 +1,62 @@
+#include "image_data.h"
+#include "png_decoder.h"
+#include "stb_decoder.h"
+#include "image_decoder.h"
+
+namespace AsuraEngine
+{
+ namespace Graphics
+ {
+
+ using namespace std;
+
+ // imagedecoderΪԡ
+ list<ImageDecoder*> ImageData::ImageDecoders = {
+ new PNGDecoder(), // png
+ new STBDecoder() // jpeg, tga, bmp
+ };
+
+ ImageData::ImageData()
+ : pixels(nullptr)
+ , size(0)
+ , width(0)
+ , height(0)
+ , format(COLOR_FORMAT_UNKNOWN)
+ {
+ }
+
+ ImageData::~ImageData()
+ {
+ if (pixels)
+ delete[] pixels;
+ }
+
+ void ImageData::Decode(IO::DataBuffer& buffer)
+ {
+ for (ImageDecoder* decoder : ImageDecoders)
+ {
+ if (decoder->CanDecode(buffer))
+ {
+ decoder->Decode(buffer, *this);
+ return;
+ }
+ }
+ }
+
+ Color ImageData::GetPixel(uint x, uint y)
+ {
+ return Color();
+ }
+
+ void ImageData::Lock()
+ {
+ mMutex.Lock();
+ }
+
+ void ImageData::Unlock()
+ {
+ mMutex.Unlock();
+ }
+
+ }
+} \ No newline at end of file
diff --git a/source/modules/asura-core/image/image_data.h b/source/modules/asura-core/image/image_data.h
new file mode 100644
index 0000000..d9427d3
--- /dev/null
+++ b/source/modules/asura-core/image/image_data.h
@@ -0,0 +1,85 @@
+#ifndef __ASURA_ENGINE_IMAGEDATA_H__
+#define __ASURA_ENGINE_IMAGEDATA_H__
+
+#include <list>
+
+#include <asura-utils/scripting/portable.hpp>
+#include <asura-utils/io/decoded_data.h>
+#include <asura-utils/io/data_buffer.h>
+#include <asura-utils/threading/thread.h>
+#include <asura-utils/threading/mutex.h>
+
+#include "../graphics/texture.h"
+#include "../graphics/color.h"
+
+namespace AsuraEngine
+{
+ namespace Graphics
+ {
+
+ class ImageDecoder;
+
+ class ImageData ASURA_FINAL
+ : public AEIO::DecodedData
+ , public Scripting::Portable<ImageData>
+ {
+ public:
+
+ LUAX_DECL_FACTORY(ImageData);
+
+ ///
+ /// ͼƬļϢʧܣ׳쳣
+ ///
+ ImageData();
+ ~ImageData();
+
+ void Decode(AEIO::DataBuffer& buffer) override;
+
+ void Lock();
+ void Unlock();
+
+ Color GetPixel(uint x, uint y);
+
+ //----------------------------------------------------------------------------//
+
+ uint width, height; // سߴ
+ ColorFormat format; // ʽ
+ byte* pixels; //
+ std::size_t size; // ݳ
+
+ //----------------------------------------------------------------------------//
+
+ private:
+
+ //----------------------------------------------------------------------------//
+
+ LUAX_DECL_METHOD(_New);
+ LUAX_DECL_METHOD(_GetPixel);
+ LUAX_DECL_METHOD(_GetSize);
+ LUAX_DECL_METHOD(_GetWidth);
+ LUAX_DECL_METHOD(_GetHeight);
+ LUAX_DECL_METHOD(_GetPixelFormat);
+ LUAX_DECL_METHOD(_Decode);
+ LUAX_DECL_METHOD(_DecodeAsync);
+ LUAX_DECL_METHOD(_IsAvailable);
+
+ //----------------------------------------------------------------------------//
+
+ ///
+ /// ڵһ׼image dataʱṩdecoderڼdecodersмѡԡ
+ ///
+ static std::list<ImageDecoder*> ImageDecoders;
+
+ ///
+ /// дݵ
+ ///
+ AEThreading::Mutex mMutex;
+
+ };
+
+ }
+}
+
+namespace AEGraphics = AsuraEngine::Graphics;
+
+#endif \ No newline at end of file
diff --git a/source/modules/asura-core/image/image_decode_task.cpp b/source/modules/asura-core/image/image_decode_task.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/source/modules/asura-core/image/image_decode_task.cpp
diff --git a/source/modules/asura-core/image/image_decode_task.h b/source/modules/asura-core/image/image_decode_task.h
new file mode 100644
index 0000000..15b0837
--- /dev/null
+++ b/source/modules/asura-core/image/image_decode_task.h
@@ -0,0 +1,25 @@
+#ifndef __ASURA_IMAGE_DECODE_TASK_H__
+#define __ASURA_IMAGE_DECODE_TASK_H__
+
+#include <asura-utils/threading/task.h>
+#include <asura-utils/scripting/portable.hpp>
+
+namespace AsuraEngine
+{
+ namespace Graphics
+ {
+
+ class ImageDecodeTask
+ : public AEScripting::Portable<ImageDecodeTask>
+ , public AEThreading::Task
+ {
+ public:
+
+ LUAX_DECL_FACTORY(ImageDecodeTask);
+
+ };
+
+ }
+}
+
+#endif \ No newline at end of file
diff --git a/source/modules/asura-core/image/image_decoder.h b/source/modules/asura-core/image/image_decoder.h
new file mode 100644
index 0000000..8b82d2b
--- /dev/null
+++ b/source/modules/asura-core/image/image_decoder.h
@@ -0,0 +1,35 @@
+#ifndef __ASURA_ENGINE_IMAGE_DECODER_H__
+#define __ASURA_ENGINE_IMAGE_DECODER_H__
+
+#include <asura-utils/io/data_buffer.h>
+
+#include "image_data.h"
+
+namespace AsuraEngine
+{
+ namespace Graphics
+ {
+
+ ASURA_ABSTRACT class ImageDecoder
+ {
+ public:
+
+ ImageDecoder() {};
+ virtual ~ImageDecoder() {};
+
+ ///
+ /// жڴǷñdecoderѹ
+ ///
+ virtual bool CanDecode(AEIO::DataBuffer& buffer) = 0;
+
+ ///
+ /// һڴ棬һѹImage dataѹʧܷnullptr
+ ///
+ virtual void Decode(AEIO::DataBuffer& buffer, ImageData& data) = 0;
+
+ };
+
+ }
+}
+
+#endif \ No newline at end of file
diff --git a/source/modules/asura-core/image/png_decoder.cpp b/source/modules/asura-core/image/png_decoder.cpp
new file mode 100644
index 0000000..80463d5
--- /dev/null
+++ b/source/modules/asura-core/image/png_decoder.cpp
@@ -0,0 +1,19 @@
+#include "png_decoder.h"
+
+namespace AsuraEngine
+{
+ namespace Graphics
+ {
+
+ bool PNGDecoder::CanDecode(AEIO::DataBuffer& buffer)
+ {
+ return false;
+ }
+
+ void PNGDecoder::Decode(AEIO::DataBuffer& buffer, ImageData& data)
+ {
+
+ }
+
+ }
+}
diff --git a/source/modules/asura-core/image/png_decoder.h b/source/modules/asura-core/image/png_decoder.h
new file mode 100644
index 0000000..6377940
--- /dev/null
+++ b/source/modules/asura-core/image/png_decoder.h
@@ -0,0 +1,27 @@
+#ifndef __ASURA_ENGINE_PNGDECODER_H__
+#define __ASURA_ENGINE_PNGDECODER_H__
+
+#include "image_decoder.h"
+
+namespace AsuraEngine
+{
+ namespace Graphics
+ {
+
+ ///
+ /// ʹlodepngѹpngļ
+ ///
+ class PNGDecoder ASURA_FINAL: public ImageDecoder
+ {
+ public:
+
+ bool CanDecode(AEIO::DataBuffer& buffer) override;
+
+ void Decode(AEIO::DataBuffer& buffer, ImageData& data) override;
+
+ };
+
+ }
+}
+
+#endif \ No newline at end of file
diff --git a/source/modules/asura-core/image/stb_decoder.cpp b/source/modules/asura-core/image/stb_decoder.cpp
new file mode 100644
index 0000000..b19f28b
--- /dev/null
+++ b/source/modules/asura-core/image/stb_decoder.cpp
@@ -0,0 +1,73 @@
+#include <asura-utils/exceptions/exception.h>
+
+#include "stb_decoder.h"
+
+#define STB_IMAGE_IMPLEMENTATION
+#include <stb/stb_image.h>
+
+namespace AsuraEngine
+{
+ namespace Graphics
+ {
+
+ bool STBDecoder::CanDecode(IO::DataBuffer& buffer)
+ {
+ int w = 0;
+ int h = 0;
+ int comp = 0;
+
+ int status = stbi_info_from_memory((const stbi_uc*)buffer.GetData(), buffer.GetSize(), &w, &h, &comp);
+
+ return status == 1 && w > 0 && h > 0;
+ }
+
+ void STBDecoder::Decode(IO::DataBuffer& db, ImageData& imageData)
+ {
+ const stbi_uc *buffer = (const stbi_uc *)db.GetData();
+ // databufferݳ
+ int bufferlen = db.GetSize();
+
+ int width, height;
+ int comp = 0;
+ byte* data = nullptr;
+ ColorFormat format = COLOR_FORMAT_UNKNOWN;
+ std::size_t size = 0;
+
+ if (stbi_is_hdr_from_memory(buffer, bufferlen))
+ {
+ // 4channelfloat
+ data = (byte*)stbi_loadf_from_memory(buffer, bufferlen, &width, &height, &comp, STBI_rgb_alpha);
+ format = COLOR_FORMAT_RGBA32F;
+ size = width * height * 4 * sizeof(float);
+ }
+ else
+ {
+ data = (byte*)stbi_load_from_memory(buffer, bufferlen, &width, &height, &comp, STBI_rgb_alpha);
+ format = COLOR_FORMAT_RGBA8;
+ size = width * height * 4;
+ }
+ if (data)
+ {
+ imageData.Lock();
+
+ if (imageData.pixels)
+ free(imageData.pixels);
+ imageData.pixels = (byte*)data;
+ imageData.format = format;
+ imageData.width = width;
+ imageData.height = height;
+ imageData.size = size;
+
+ imageData.Unlock();
+ }
+ else
+ {
+ const char *err = stbi_failure_reason();
+ if (err == nullptr)
+ err = "unknown error";
+ throw Exception("Could not decode image with stb_image (%s).", err);
+ }
+ }
+
+ }
+} \ No newline at end of file
diff --git a/source/modules/asura-core/image/stb_decoder.h b/source/modules/asura-core/image/stb_decoder.h
new file mode 100644
index 0000000..76e70c3
--- /dev/null
+++ b/source/modules/asura-core/image/stb_decoder.h
@@ -0,0 +1,28 @@
+#ifndef __ASURA_ENGINE_STBDECODER_H__
+#define __ASURA_ENGINE_STBDECODER_H__
+
+#include "image_decoder.h"
+
+namespace AsuraEngine
+{
+ namespace Graphics
+ {
+
+ ///
+ /// ʹstb_imageѹJPEGTGABMPļ
+ ///
+ class STBDecoder ASURA_FINAL
+ : public ImageDecoder
+ {
+ public:
+
+ bool CanDecode(AEIO::DataBuffer& buffer) override;
+
+ void Decode(AEIO::DataBuffer& buffer, ImageData& data) override;
+
+ };
+
+ }
+}
+
+#endif \ No newline at end of file