2020-08-26 15:38:01 +08:00
|
|
|
#pragma once
|
2022-10-02 04:29:51 +08:00
|
|
|
#include <blah_color.h>
|
|
|
|
#include <blah_spatial.h>
|
|
|
|
#include <blah_filesystem.h>
|
|
|
|
#include <blah_stream.h>
|
2020-08-26 15:38:01 +08:00
|
|
|
|
|
|
|
namespace Blah
|
|
|
|
{
|
|
|
|
class Stream;
|
|
|
|
|
2021-03-23 07:53:27 +08:00
|
|
|
// A 2D Bitmap stored on the CPU.
|
|
|
|
// For drawing images to the screen, use a Texture.
|
2020-10-19 15:49:37 +08:00
|
|
|
class Image
|
2020-08-26 15:38:01 +08:00
|
|
|
{
|
2020-10-19 15:49:37 +08:00
|
|
|
public:
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// width of the image, in pixels.
|
2020-08-26 15:38:01 +08:00
|
|
|
int width = 0;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// height of the image, in pixels.
|
2020-08-26 15:38:01 +08:00
|
|
|
int height = 0;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// pixel data of the image.
|
|
|
|
// this can be nullptr if the image is never assigned to anything.
|
2020-08-26 15:38:01 +08:00
|
|
|
Color* pixels = nullptr;
|
|
|
|
|
|
|
|
Image();
|
|
|
|
Image(Stream& stream);
|
2021-03-21 17:08:28 +08:00
|
|
|
Image(const FilePath& file);
|
2020-08-26 15:38:01 +08:00
|
|
|
Image(int width, int height);
|
|
|
|
Image(const Image& src);
|
|
|
|
Image& operator=(const Image& src);
|
|
|
|
Image(Image&& src) noexcept;
|
|
|
|
Image& operator=(Image&& src) noexcept;
|
|
|
|
~Image();
|
|
|
|
|
2021-05-26 12:31:18 +08:00
|
|
|
// creates the image from a stream, and returns true if successful
|
|
|
|
bool from_stream(Stream& stream);
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// disposes the image and resets its values to defaults
|
2020-08-26 15:38:01 +08:00
|
|
|
void dispose();
|
|
|
|
|
2021-03-21 17:08:28 +08:00
|
|
|
// applies alpha premultiplication to the image data
|
2020-08-26 15:38:01 +08:00
|
|
|
void premultiply();
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// sets the pixels at the provided rectangle to the given data
|
|
|
|
// data must be at least rect.w * rect.h in size!
|
2021-12-13 12:41:23 +08:00
|
|
|
void set_pixels(const Recti& rect, Color* data);
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// saves the image to a png file
|
|
|
|
bool save_png(const FilePath& file) const;
|
|
|
|
|
|
|
|
// saves the image to a png file
|
2020-08-26 15:38:01 +08:00
|
|
|
bool save_png(Stream& stream) const;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// saves the image to a jpg file
|
|
|
|
bool save_jpg(const FilePath& file, int quality) const;
|
|
|
|
|
|
|
|
// saves the image to a jpg file
|
2020-08-26 15:38:01 +08:00
|
|
|
bool save_jpg(Stream& stream, int quality) const;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// gets the pixels from the given source rectangle
|
2021-12-13 12:41:23 +08:00
|
|
|
void get_pixels(Color* dest, const Point& dest_pos, const Point& dest_size, Recti source_rect) const;
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// gets a sub image from this image
|
2021-12-13 12:41:23 +08:00
|
|
|
Image get_sub_image(const Recti& source_rect);
|
2020-10-19 15:49:37 +08:00
|
|
|
|
|
|
|
private:
|
2021-03-21 17:08:28 +08:00
|
|
|
|
|
|
|
// whether the stbi library owns the image data.
|
|
|
|
// we should let it free the data if it created it.
|
2020-10-19 15:49:37 +08:00
|
|
|
bool m_stbi_ownership;
|
2020-08-26 15:38:01 +08:00
|
|
|
};
|
|
|
|
}
|