aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/Bitmap.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/Graphics/Bitmap.cpp')
-rw-r--r--src/libjin/Graphics/Bitmap.cpp276
1 files changed, 138 insertions, 138 deletions
diff --git a/src/libjin/Graphics/Bitmap.cpp b/src/libjin/Graphics/Bitmap.cpp
index 160fda2..8769e8f 100644
--- a/src/libjin/Graphics/Bitmap.cpp
+++ b/src/libjin/Graphics/Bitmap.cpp
@@ -7,144 +7,144 @@ using namespace jin::math;
namespace jin
{
-namespace graphics
-{
-
- /* pixelbitmap */
- Bitmap* Bitmap::createBitmap(const void* pixel, unsigned width, unsigned height)
- {
- Bitmap* bitmap = new Bitmap(width, height);
- memcpy(bitmap->pixels, pixel, width*height * sizeof(Color));
- return bitmap;
- }
-
- /*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, Color color)
- {
- Bitmap* bitmap = new Bitmap(w, h);
- if (color != Color::BLACK)
- bitmap->setPixels(color);
- return bitmap;
- }
-
- /*static */ Bitmap* Bitmap::clone(const Bitmap* bitmap)
- {
- Bitmap* b = new Bitmap();
- int w = bitmap->getWidth();
- int h = bitmap->getHeight();
- b->resetPixels(bitmap->getPixels(), w, h);
- 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];
- }
-
- 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];
- 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)
- return;
- if (without<int>(x, 0, width - 1) || without<int>(y, 0, height - 1))
- return;
- 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];
- size_t s = w * h * sizeof(Color);
- 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)
- {
- 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];
- }
+ namespace graphics
+ {
+
+ /* pixelbitmap */
+ Bitmap* Bitmap::createBitmap(const void* pixel, unsigned width, unsigned height)
+ {
+ Bitmap* bitmap = new Bitmap(width, height);
+ memcpy(bitmap->pixels, pixel, width*height * sizeof(Color));
+ return bitmap;
+ }
+
+ /*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, Color color)
+ {
+ Bitmap* bitmap = new Bitmap(w, h);
+ if (color != Color::BLACK)
+ bitmap->setPixels(color);
+ return bitmap;
+ }
+
+ /*static */ Bitmap* Bitmap::clone(const Bitmap* bitmap)
+ {
+ Bitmap* b = new Bitmap();
+ int w = bitmap->getWidth();
+ int h = bitmap->getHeight();
+ b->resetPixels(bitmap->getPixels(), w, h);
+ 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];
+ }
+
+ 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];
+ 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)
+ return;
+ if (without<int>(x, 0, width - 1) || without<int>(y, 0, height - 1))
+ return;
+ 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];
+ size_t s = w * h * sizeof(Color);
+ 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)
+ {
+ 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;
- }
+ const Color* Bitmap::getPixels() const
+ {
+ return pixels;
+ }
-}
+ }
} \ No newline at end of file