From e495a05249709737cb7018eafd6ef4a0df5ed618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Minisini?= Date: Sat, 10 Dec 2022 00:06:50 +0100 Subject: [PATCH] The Image constructor now fills the image data with 'Color.Transparent' color by default. [GB.IMAGE] * NEW: The Image constructor now fills the image data with 'Color.Transparent' color by default. To get uninitialized image data, you must specified 'Color.Default' as fill color. --- main/lib/image/CImage.c | 14 +++++++------- main/lib/image/c_color.c | 2 +- main/lib/image/image.c | 12 +++++++++--- main/lib/image/image.h | 2 +- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/main/lib/image/CImage.c b/main/lib/image/CImage.c index bea628f8f..43715b29f 100644 --- a/main/lib/image/CImage.c +++ b/main/lib/image/CImage.c @@ -43,7 +43,7 @@ BEGIN_METHOD(Image_new, GB_INTEGER w; GB_INTEGER h; GB_INTEGER col; GB_INTEGER f if (VARGOPT(format, 0) == 1) format = GB_IMAGE_FMT_SET_PREMULTIPLIED(format); - IMAGE_create(THIS_IMAGE, VARGOPT(w, 0), VARGOPT(h, 0), format); + IMAGE_create(THIS_IMAGE, VARGOPT(w, 0), VARGOPT(h, 0), format, VARGOPT(col, 0)); if (!MISSING(col)) IMAGE_fill(THIS_IMAGE, VARG(col)); @@ -206,7 +206,7 @@ BEGIN_METHOD(Image_Copy, GB_INTEGER x; GB_INTEGER y; GB_INTEGER w; GB_INTEGER h) image = GB.New(GB.FindClass("Image"), NULL, NULL); - IMAGE_create(&image->image, w, h, THIS_IMAGE->format); + IMAGE_create(&image->image, w, h, THIS_IMAGE->format, GB_COLOR_DEFAULT); if (w > 0 && h > 0) IMAGE_bitblt(&image->image, 0, 0, -1, -1, THIS_IMAGE, x, y, w, h); @@ -226,7 +226,7 @@ BEGIN_METHOD(Image_Resize, GB_INTEGER width; GB_INTEGER height) //IMAGE_convert(THIS_IMAGE, IMAGE_get_default_format()); //fprintf(stderr, "format = %d\n", THIS_IMAGE->format); tmp.ob = THIS_IMAGE->ob; - IMAGE_create(&tmp, w, h, THIS_IMAGE->format); + IMAGE_create(&tmp, w, h, THIS_IMAGE->format, GB_COLOR_DEFAULT); IMAGE_bitblt(&tmp, 0, 0, -1, -1, THIS_IMAGE, 0, 0, w, h); IMAGE_delete(THIS_IMAGE); @@ -240,7 +240,7 @@ BEGIN_METHOD(Image_Mirror, GB_BOOLEAN horz; GB_BOOLEAN vert) GB_IMG tmp; tmp.ob = THIS_IMAGE->ob; - IMAGE_create(&tmp, THIS_IMAGE->width, THIS_IMAGE->height, THIS_IMAGE->format); + IMAGE_create(&tmp, THIS_IMAGE->width, THIS_IMAGE->height, THIS_IMAGE->format, GB_COLOR_DEFAULT); IMAGE_mirror(THIS_IMAGE, &tmp, VARG(horz), VARG(vert)); IMAGE_delete(THIS_IMAGE); *THIS_IMAGE = tmp; @@ -253,7 +253,7 @@ BEGIN_METHOD_VOID(Image_RotateLeft) GB_IMG tmp; tmp.ob = THIS_IMAGE->ob; - IMAGE_create(&tmp, THIS_IMAGE->height, THIS_IMAGE->width, THIS_IMAGE->format); + IMAGE_create(&tmp, THIS_IMAGE->height, THIS_IMAGE->width, THIS_IMAGE->format, GB_COLOR_DEFAULT); IMAGE_rotate(THIS_IMAGE, &tmp, TRUE); IMAGE_delete(THIS_IMAGE); *THIS_IMAGE = tmp; @@ -266,7 +266,7 @@ BEGIN_METHOD_VOID(Image_RotateRight) GB_IMG tmp; tmp.ob = THIS_IMAGE->ob; - IMAGE_create(&tmp, THIS_IMAGE->height, THIS_IMAGE->width, THIS_IMAGE->format); + IMAGE_create(&tmp, THIS_IMAGE->height, THIS_IMAGE->width, THIS_IMAGE->format, GB_COLOR_DEFAULT); IMAGE_rotate(THIS_IMAGE, &tmp, FALSE); IMAGE_delete(THIS_IMAGE); *THIS_IMAGE = tmp; @@ -298,7 +298,7 @@ BEGIN_METHOD(CIMAGE_transform, GB_FLOAT sx; GB_FLOAT sy; GB_FLOAT dx; GB_FLOAT d if (!w || !h) return; - IMAGE_create(&image->image, w, h, THIS_IMAGE->format); + IMAGE_create(&image->image, w, h, THIS_IMAGE->format, GB_COLOR_DEFAULT); IMAGE_transform(&image->image, THIS_IMAGE, VARG(sx), VARG(sy), VARG(dx), VARG(dy)); END_METHOD diff --git a/main/lib/image/c_color.c b/main/lib/image/c_color.c index f7eb881f4..c7e35e8ab 100644 --- a/main/lib/image/c_color.c +++ b/main/lib/image/c_color.c @@ -649,7 +649,7 @@ GB_DESC CColorDesc[] = { GB_DECLARE_STATIC("Color"), - GB_CONSTANT("Default", "i", COLOR_DEFAULT), + GB_CONSTANT("Default", "i", GB_COLOR_DEFAULT), GB_CONSTANT("Black", "i", 0x000000), GB_CONSTANT("White", "i", 0xFFFFFF), diff --git a/main/lib/image/image.c b/main/lib/image/image.c index 8b4838284..1798a6711 100644 --- a/main/lib/image/image.c +++ b/main/lib/image/image.c @@ -554,7 +554,7 @@ int IMAGE_size(GB_IMG *img) return img->width * img->height * (GB_IMAGE_FMT_IS_24_BITS(img->format) ? 3 : 4); } -void IMAGE_create(GB_IMG *img, int width, int height, int format) +void IMAGE_create(GB_IMG *img, int width, int height, int format, GB_COLOR fill) { GB_BASE save = img->ob; CLEAR(img); @@ -570,8 +570,14 @@ void IMAGE_create(GB_IMG *img, int width, int height, int format) img->width = width; img->height = height; img->format = format; - GB.Alloc(POINTER(&img->data), IMAGE_size(img)); + if (fill == 0) + GB.AllocZero(POINTER(&img->data), IMAGE_size(img)); + else + GB.Alloc(POINTER(&img->data), IMAGE_size(img)); img->owner_handle = img->data; + + if (fill && fill != GB_COLOR_DEFAULT) + IMAGE_fill(img, fill); #ifdef DEBUG_ME fprintf(stderr, "IMAGE_create: %p\n", img); @@ -581,7 +587,7 @@ void IMAGE_create(GB_IMG *img, int width, int height, int format) void IMAGE_create_with_data(GB_IMG *img, int width, int height, int format, unsigned char *data) { - IMAGE_create(img, width, height, format); + IMAGE_create(img, width, height, format, GB_COLOR_DEFAULT); if (data && !IMAGE_is_void(img)) memcpy(img->data, data, IMAGE_size(img)); } diff --git a/main/lib/image/image.h b/main/lib/image/image.h index fd40890f5..1e9acdf19 100644 --- a/main/lib/image/image.h +++ b/main/lib/image/image.h @@ -41,7 +41,7 @@ static inline int GRAY(uint rgba) { return (RED(rgba) * 11 + GREEN(rgba) * 16 + int IMAGE_size(GB_IMG *img); -void IMAGE_create(GB_IMG *img, int width, int height, int format); +void IMAGE_create(GB_IMG *img, int width, int height, int format, GB_COLOR fill); void IMAGE_create_with_data(GB_IMG *img, int width, int height, int format, unsigned char *data); void IMAGE_take(GB_IMG *img, GB_IMG_OWNER *owner, void *owner_handle, int width, int height, unsigned char *data);