aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/graphics/bitmap.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2020-02-11 11:29:07 +0800
committerchai <chaifix@163.com>2020-02-11 11:29:07 +0800
commit160e1299ef3d95f8e8c48706d7f61dd3dc6c6b60 (patch)
treeabe5ae5242d9cc6caf6edf103e662c44e978fca0 /src/libjin/graphics/bitmap.cpp
parente095043485d1d298571af6d9eca7f0db9009ea7a (diff)
*修改 tab大小HEADmaster
Diffstat (limited to 'src/libjin/graphics/bitmap.cpp')
-rw-r--r--src/libjin/graphics/bitmap.cpp348
1 files changed, 174 insertions, 174 deletions
diff --git a/src/libjin/graphics/bitmap.cpp b/src/libjin/graphics/bitmap.cpp
index cae6f0b..b038d6c 100644
--- a/src/libjin/graphics/bitmap.cpp
+++ b/src/libjin/graphics/bitmap.cpp
@@ -12,178 +12,178 @@ using namespace JinEngine::Math;
namespace JinEngine
{
- namespace Graphics
- {
-
- Bitmap* Bitmap::clone()
- {
- Bitmap* b = new Bitmap(this);
- return b;
- }
-
- Bitmap::Bitmap()
- : width(0)
- , height(0)
- , pixels(nullptr)
- {
- }
-
- Bitmap::Bitmap(unsigned w, unsigned h)
- {
- width = w;
- height = h;
- pixels = new Color[w*h];
- if (pixels == nullptr)
- throw Exception("No enough memory.");
- }
-
- Bitmap::Bitmap(const char* path)
- {
- AssetDatabase* ad = AssetDatabase::get();
- Buffer buffer;
- ad->read(path, buffer);
- new (this) Bitmap(&buffer, buffer.size());
- }
-
- Bitmap::Bitmap(const void* pix, unsigned w, unsigned h)
- {
- new (this) Bitmap(w, h);
- memcpy(pixels, pix, w * h * sizeof(Color));
- }
-
- Bitmap::Bitmap(const void* imgData, size_t size)
- {
- if (imgData == nullptr)
- return;
- int w, h;
- void* data = stbi_load_from_memory((unsigned char *)imgData, size, &w, &h, NULL, STBI_rgb_alpha);
- if (data == nullptr)
- {
- throw Exception("Could not create bitmap from image data.");
- return;
- }
- new (this) Bitmap();
- pixels = (Color*)data;
- width = w;
- height = h;
- }
-
- Bitmap::Bitmap(int w, int h, Color color)
- {
- new (this) Bitmap(w, h);
- if (color != Color::BLACK)
- setPixels(color);
- }
-
- Bitmap::Bitmap(int w, int h, std::function<Color(int, int, int, int)> drawer)
- {
- new (this) Bitmap(w, h);
- for (int y = 0; y < height; ++y)
- {
- for (int x = 0; x < width; ++x)
- {
- Color c = drawer(width, height, x, y);
- setPixel(c, x, y);
- }
- }
- }
-
- Bitmap::Bitmap(const Bitmap* bitmap)
- {
- new (this) Bitmap();
- int w = bitmap->getWidth();
- int h = bitmap->getHeight();
- resetPixels(bitmap->getPixels(), w, h);
- }
-
- Bitmap::~Bitmap()
- {
- stbi_image_free(pixels);
- }
-
- void Bitmap::bind(Color* p, int w, int h)
- {
- if (pixels != nullptr)
- delete[] pixels;
- pixels = p;
- width = w;
- height = h;
- }
-
- void Bitmap::resetPixels(const Color* p, int w, int h)
- {
- if (pixels != nullptr)
- delete[] pixels;
- pixels = new Color[w*h];
- if (pixels == nullptr)
- throw Exception("Not enough memory.");
- size_t s = w * h * sizeof(Color);
- memcpy(pixels, p, s);
- width = w;
- height = h;
- }
-
- void Bitmap::setPixel(const Color& c, int x, int y)
- {
- if (pixels == nullptr)
- throw Exception("Bitmap don't have pixel space.");
- if (without<int>(x, 0, width - 1) || without<int>(y, 0, height - 1))
- return;
- if (x + y * width >= width * height)
- throw Exception("Pixel <%d, %d> of bitmap is out of range.", x, y);
- pixels[x + y * width] = c;
- }
-
- void Bitmap::resetPixels(const Color& c, int w, int h)
- {
- if (pixels != nullptr)
- delete[] pixels;
- pixels = new Color[w*h];
- if (pixels == nullptr)
- throw Exception("Not enough memory.");
- width = w;
- height = h;
- for (int x = 0; x < w; ++x)
- {
- for (int y = 0; y < h; ++y)
- {
- pixels[x + y * w] = c;
- }
- }
- }
-
- void Bitmap::setPixels(Color* p, int count)
- {
- if (count > width * height)
- throw Exception("Pixels are out of range.");
- size_t s = width * height * sizeof(Color);
- memcpy(pixels, p, s);
- }
-
- void Bitmap::setPixels(Color c)
- {
- for (int x = 0; x < width; ++x)
- {
- for (int y = 0; y < height; ++y)
- {
- pixels[x + y * width] = c;
- }
- }
- }
-
- 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() const
- {
- return pixels;
- }
-
- } // namespace Graphics
+ namespace Graphics
+ {
+
+ Bitmap* Bitmap::clone()
+ {
+ Bitmap* b = new Bitmap(this);
+ return b;
+ }
+
+ Bitmap::Bitmap()
+ : width(0)
+ , height(0)
+ , pixels(nullptr)
+ {
+ }
+
+ Bitmap::Bitmap(unsigned w, unsigned h)
+ {
+ width = w;
+ height = h;
+ pixels = new Color[w*h];
+ if (pixels == nullptr)
+ throw Exception("No enough memory.");
+ }
+
+ Bitmap::Bitmap(const char* path)
+ {
+ AssetDatabase* ad = AssetDatabase::get();
+ Buffer buffer;
+ ad->read(path, buffer);
+ new (this) Bitmap(&buffer, buffer.size());
+ }
+
+ Bitmap::Bitmap(const void* pix, unsigned w, unsigned h)
+ {
+ new (this) Bitmap(w, h);
+ memcpy(pixels, pix, w * h * sizeof(Color));
+ }
+
+ Bitmap::Bitmap(const void* imgData, size_t size)
+ {
+ if (imgData == nullptr)
+ return;
+ int w, h;
+ void* data = stbi_load_from_memory((unsigned char *)imgData, size, &w, &h, NULL, STBI_rgb_alpha);
+ if (data == nullptr)
+ {
+ throw Exception("Could not create bitmap from image data.");
+ return;
+ }
+ new (this) Bitmap();
+ pixels = (Color*)data;
+ width = w;
+ height = h;
+ }
+
+ Bitmap::Bitmap(int w, int h, Color color)
+ {
+ new (this) Bitmap(w, h);
+ if (color != Color::BLACK)
+ setPixels(color);
+ }
+
+ Bitmap::Bitmap(int w, int h, std::function<Color(int, int, int, int)> drawer)
+ {
+ new (this) Bitmap(w, h);
+ for (int y = 0; y < height; ++y)
+ {
+ for (int x = 0; x < width; ++x)
+ {
+ Color c = drawer(width, height, x, y);
+ setPixel(c, x, y);
+ }
+ }
+ }
+
+ Bitmap::Bitmap(const Bitmap* bitmap)
+ {
+ new (this) Bitmap();
+ int w = bitmap->getWidth();
+ int h = bitmap->getHeight();
+ resetPixels(bitmap->getPixels(), w, h);
+ }
+
+ Bitmap::~Bitmap()
+ {
+ stbi_image_free(pixels);
+ }
+
+ void Bitmap::bind(Color* p, int w, int h)
+ {
+ if (pixels != nullptr)
+ delete[] pixels;
+ pixels = p;
+ width = w;
+ height = h;
+ }
+
+ void Bitmap::resetPixels(const Color* p, int w, int h)
+ {
+ if (pixels != nullptr)
+ delete[] pixels;
+ pixels = new Color[w*h];
+ if (pixels == nullptr)
+ throw Exception("Not enough memory.");
+ size_t s = w * h * sizeof(Color);
+ memcpy(pixels, p, s);
+ width = w;
+ height = h;
+ }
+
+ void Bitmap::setPixel(const Color& c, int x, int y)
+ {
+ if (pixels == nullptr)
+ throw Exception("Bitmap don't have pixel space.");
+ if (without<int>(x, 0, width - 1) || without<int>(y, 0, height - 1))
+ return;
+ if (x + y * width >= width * height)
+ throw Exception("Pixel <%d, %d> of bitmap is out of range.", x, y);
+ pixels[x + y * width] = c;
+ }
+
+ void Bitmap::resetPixels(const Color& c, int w, int h)
+ {
+ if (pixels != nullptr)
+ delete[] pixels;
+ pixels = new Color[w*h];
+ if (pixels == nullptr)
+ throw Exception("Not enough memory.");
+ width = w;
+ height = h;
+ for (int x = 0; x < w; ++x)
+ {
+ for (int y = 0; y < h; ++y)
+ {
+ pixels[x + y * w] = c;
+ }
+ }
+ }
+
+ void Bitmap::setPixels(Color* p, int count)
+ {
+ if (count > width * height)
+ throw Exception("Pixels are out of range.");
+ size_t s = width * height * sizeof(Color);
+ memcpy(pixels, p, s);
+ }
+
+ void Bitmap::setPixels(Color c)
+ {
+ for (int x = 0; x < width; ++x)
+ {
+ for (int y = 0; y < height; ++y)
+ {
+ pixels[x + y * width] = c;
+ }
+ }
+ }
+
+ 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() const
+ {
+ return pixels;
+ }
+
+ } // namespace Graphics
} // namespace JinEngine \ No newline at end of file