blah/include/blah_image.h

74 lines
1.9 KiB
C
Raw Normal View History

2020-08-26 15:38:01 +08:00
#pragma once
#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;
// A 2D Bitmap stored on the CPU.
// For drawing images to the screen, use a Texture.
class Image
2020-08-26 15:38:01 +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!
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
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
Image get_sub_image(const Recti& source_rect);
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.
bool m_stbi_ownership;
2020-08-26 15:38:01 +08:00
};
}