Util: More image creation functions

This commit is contained in:
Vicki Pfau 2023-04-02 02:59:11 -07:00
parent 285f22927b
commit 42527b4c5e
2 changed files with 16 additions and 1 deletions

View File

@ -92,6 +92,8 @@ struct mImage {
struct VFile; struct VFile;
struct mImage* mImageCreate(unsigned width, unsigned height, enum mColorFormat format); struct mImage* mImageCreate(unsigned width, unsigned height, enum mColorFormat format);
struct mImage* mImageCreateWithStride(unsigned width, unsigned height, unsigned stride, enum mColorFormat format);
struct mImage* mImageCreateFromConstBuffer(unsigned width, unsigned height, unsigned stride, enum mColorFormat format, const void* pixels);
struct mImage* mImageLoad(const char* path); struct mImage* mImageLoad(const char* path);
struct mImage* mImageLoadVF(struct VFile* vf); struct mImage* mImageLoadVF(struct VFile* vf);
struct mImage* mImageConvertToFormat(const struct mImage*, enum mColorFormat format); struct mImage* mImageConvertToFormat(const struct mImage*, enum mColorFormat format);

View File

@ -14,6 +14,10 @@
#define ROW(IM, Y) PIXEL(IM, 0, Y) #define ROW(IM, Y) PIXEL(IM, 0, Y)
struct mImage* mImageCreate(unsigned width, unsigned height, enum mColorFormat format) { struct mImage* mImageCreate(unsigned width, unsigned height, enum mColorFormat format) {
return mImageCreateWithStride(width, height, width, format);
}
struct mImage* mImageCreateWithStride(unsigned width, unsigned height, unsigned stride, enum mColorFormat format) {
if (width < 1 || height < 1) { if (width < 1 || height < 1) {
return NULL; return NULL;
} }
@ -23,7 +27,7 @@ struct mImage* mImageCreate(unsigned width, unsigned height, enum mColorFormat f
} }
image->width = width; image->width = width;
image->height = height; image->height = height;
image->stride = width; image->stride = stride;
image->format = format; image->format = format;
image->depth = mColorFormatBytes(format); image->depth = mColorFormatBytes(format);
image->data = calloc(width * height, image->depth); image->data = calloc(width * height, image->depth);
@ -34,6 +38,15 @@ struct mImage* mImageCreate(unsigned width, unsigned height, enum mColorFormat f
return image; return image;
} }
struct mImage* mImageCreateFromConstBuffer(unsigned width, unsigned height, unsigned stride, enum mColorFormat format, const void* pixels) {
struct mImage* image = mImageCreateWithStride(width, height, stride, format);
if (!image) {
return NULL;
}
memcpy(image->data, pixels, height * stride * image->depth);
return image;
}
struct mImage* mImageLoad(const char* path) { struct mImage* mImageLoad(const char* path) {
struct VFile* vf = VFileOpen(path, O_RDONLY); struct VFile* vf = VFileOpen(path, O_RDONLY);
if (!vf) { if (!vf) {