aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/Bitmap.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-09-06 19:57:40 +0800
committerchai <chaifix@163.com>2018-09-06 19:57:40 +0800
commit50084b0b3451328a4dfe6db65c78a225e9c8f288 (patch)
tree020e5b6c834b792aada4cd23cd2551a6b81c22dc /src/libjin/Graphics/Bitmap.cpp
parent7281a70b1e38fc2bdfb2d4b6d8f7daf7ceedbd1a (diff)
+bitmap
Diffstat (limited to 'src/libjin/Graphics/Bitmap.cpp')
-rw-r--r--src/libjin/Graphics/Bitmap.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/libjin/Graphics/Bitmap.cpp b/src/libjin/Graphics/Bitmap.cpp
new file mode 100644
index 0000000..93ea1a9
--- /dev/null
+++ b/src/libjin/Graphics/Bitmap.cpp
@@ -0,0 +1,96 @@
+#include "Bitmap.h"
+#include "../3rdparty/stb/stb_image.h"
+#include "../Math/math.h"
+
+using namespace jin::math;
+
+namespace jin
+{
+namespace graphics
+{
+
+ /*static*/ Bitmap* Bitmap::createBitmap(const void* imgData, size_t size)
+ {
+ if (imgData == nullptr)
+ return nullptr;
+ int w, h;
+ void* data = stbi_load_from_memory((unsigned char *)imgData, size, &w, &h, NULL, STBI_rgb_alpha);
+ if (data == nullptr)
+ return nullptr;
+ Bitmap* bitmap = new Bitmap();
+ bitmap->pixels = (Color*)data;
+ bitmap->width = w;
+ bitmap->height = h;
+ return bitmap;
+ }
+
+ /*static*/ Bitmap* Bitmap::createBitmap(int w, int h)
+ {
+ Bitmap* bitmap = new Bitmap(w, h);
+ return bitmap;
+ }
+
+ Bitmap::Bitmap()
+ : width(0)
+ , height(0)
+ , pixels(nullptr)
+ {
+ }
+
+ Bitmap::Bitmap(int w, int h)
+ {
+ width = w;
+ height = h;
+ pixels = (Color*)calloc(1, w*h*sizeof(Color));
+ }
+
+ Bitmap::~Bitmap()
+ {
+ stbi_image_free(pixels);
+ }
+
+ void Bitmap::bindPixels(Color* p, int w, int h)
+ {
+ if (pixels != nullptr)
+ delete[] pixels;
+ pixels = p;
+ width = w;
+ height = h;
+ }
+
+ void Bitmap::setPixels(Color* p, int w, int h)
+ {
+ if (pixels != nullptr)
+ delete[] pixels;
+ size_t s = w * h * sizeof(Color);
+ pixels = (Color*)calloc(1, s);
+ memcpy(pixels, p, s);
+ width = w;
+ height = h;
+ }
+
+ void Bitmap::setPixel(const Color& pixel, int x, int y)
+ {
+ if (pixels == nullptr)
+ return;
+ if (without<int>(x, 0, width - 1) || without<int>(y, 0, height - 1))
+ return;
+ pixels[x + y * width] = pixel;
+ }
+
+ Color Bitmap::getPixel(int x, int y)
+ {
+ if (pixels == nullptr)
+ return Color::BLACK;
+ if (without<int>(x, 0, width - 1) || without<int>(y, 0, height - 1))
+ return Color::BLACK;
+ return pixels[x + y * width];
+ }
+
+ const Color* Bitmap::getPixels()
+ {
+ return pixels;
+ }
+
+}
+} \ No newline at end of file