Skip to content

Commit

Permalink
extended image functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfriend99 committed Sep 25, 2024
1 parent 2ae2503 commit 2b3b040
Show file tree
Hide file tree
Showing 4 changed files with 478 additions and 3 deletions.
111 changes: 109 additions & 2 deletions packages/imagine/imagine.c
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,20 @@ DECLARE_MODULE_METHOD(imagine__truecolortopalette) {
RETURN_NUMBER(gdImageTrueColorToPalette(image, AS_NUMBER(args[1]), AS_NUMBER(args[2])));
}

DECLARE_MODULE_METHOD(imagine__colormatch) {
ENFORCE_ARG_COUNT(colormatch, 3);
ENFORCE_ARG_TYPE(colormatch, 0, IS_PTR);
ENFORCE_ARG_TYPE(colormatch, 1, IS_PTR);

gdImagePtr image1 = (gdImagePtr)AS_PTR(args[0])->pointer;
CHECK_IMAGE_PTR(image1);

gdImagePtr image2 = (gdImagePtr)AS_PTR(args[1])->pointer;
CHECK_IMAGE_PTR(image2);

RETURN_NUMBER(gdImageColorMatch(image1, image2));
}

DECLARE_MODULE_METHOD(imagine__palettetotruecolor) {
ENFORCE_ARG_COUNT(palettetotruecolor, 1);
ENFORCE_ARG_TYPE(truecolortopalette, 0, IS_PTR);
Expand Down Expand Up @@ -1467,8 +1481,8 @@ DECLARE_MODULE_METHOD(imagine__brightness) {
}

DECLARE_MODULE_METHOD(imagine__meta) {
ENFORCE_ARG_COUNT(size, 1);
ENFORCE_ARG_TYPE(size, 0, IS_PTR);
ENFORCE_ARG_COUNT(meta, 1);
ENFORCE_ARG_TYPE(meta, 0, IS_PTR);

gdImagePtr image = (gdImagePtr)AS_PTR(args[0])->pointer;
CHECK_IMAGE_PTR(image);
Expand Down Expand Up @@ -1612,6 +1626,97 @@ DECLARE_MODULE_METHOD(imagine__rotate) {
RETURN_CLOSABLE_NAMED_PTR(new_image, IMAGINE_IMAGE_PTR_NAME, imagine_free_image_ptrs);
}

DECLARE_MODULE_METHOD(imagine__imagecompare) {
ENFORCE_ARG_COUNT(imagecompare, 2);
ENFORCE_ARG_TYPE(imagecompare, 0, IS_PTR);
ENFORCE_ARG_TYPE(imagecompare, 1, IS_PTR);

// I couldn't use GD's version directly as it has disabled the alpha
// channel check leaving it in a severely buggy state.

gdImagePtr im1 = (gdImagePtr)AS_PTR(args[0])->pointer;
CHECK_IMAGE_PTR(im1);

gdImagePtr im2 = (gdImagePtr)AS_PTR(args[0])->pointer;
CHECK_IMAGE_PTR(im2);

int x, y;
int p1, p2;
int cmpStatus = 0;
int sx, sy;

if (im1->interlace != im2->interlace) {
cmpStatus |= GD_CMP_INTERLACE;
}

if (im1->transparent != im2->transparent) {
cmpStatus |= GD_CMP_TRANSPARENT;
}

if (im1->trueColor != im2->trueColor) {
cmpStatus |= GD_CMP_TRUECOLOR;
}

sx = im1->sx;
if (im1->sx != im2->sx) {
cmpStatus |= GD_CMP_SIZE_X + GD_CMP_IMAGE;
if (im2->sx < im1->sx) {
sx = im2->sx;
}
}

sy = im1->sy;
if (im1->sy != im2->sy) {
cmpStatus |= GD_CMP_SIZE_Y + GD_CMP_IMAGE;
if (im2->sy < im1->sy) {
sy = im2->sy;
}
}

if (im1->colorsTotal != im2->colorsTotal) {
cmpStatus |= GD_CMP_NUM_COLORS;
}

for (y = 0; (y < sy); y++) {
for (x = 0; (x < sx); x++) {
p1 =
im1->trueColor ? gdImageTrueColorPixel (im1, x,
y) :
gdImagePalettePixel (im1, x, y);
p2 =
im2->trueColor ? gdImageTrueColorPixel (im2, x,
y) :
gdImagePalettePixel (im2, x, y);

if (gdImageRed (im1, p1) != gdImageRed (im2, p2)) {
cmpStatus |= GD_CMP_COLOR + GD_CMP_IMAGE;
break;
}
if (gdImageGreen (im1, p1) != gdImageGreen (im2, p2)) {
cmpStatus |= GD_CMP_COLOR + GD_CMP_IMAGE;
break;
}
if (gdImageBlue (im1, p1) != gdImageBlue (im2, p2)) {
cmpStatus |= GD_CMP_COLOR + GD_CMP_IMAGE;
break;
}

if(im1->trueColor && im2->trueColor) {
if (gdImageAlpha (im1, p1) != gdImageAlpha (im2, p2)) {
cmpStatus |= GD_CMP_COLOR + GD_CMP_IMAGE;
break;
}
}
}

if (cmpStatus & GD_CMP_COLOR) {
break;
};
}

RETURN_NUMBER(cmpStatus);
}

CREATE_MODULE_LOADER(imagine) {
static b_field_reg module_fields[] = {
// Fonts
Expand Down Expand Up @@ -1737,6 +1842,8 @@ CREATE_MODULE_LOADER(imagine) {
{"setclip", true, GET_MODULE_METHOD(imagine__setclip)},
{"getclip", true, GET_MODULE_METHOD(imagine__getclip)},
{"setresolution", true, GET_MODULE_METHOD(imagine__setresolution)},
{"colormatch", true, GET_MODULE_METHOD(imagine__colormatch)},
{"imagecompare", true, GET_MODULE_METHOD(imagine__imagecompare)},
{"truecolortopalette", true, GET_MODULE_METHOD(imagine__truecolortopalette)},
{"palettetotruecolor", true, GET_MODULE_METHOD(imagine__palettetotruecolor)},

Expand Down
11 changes: 11 additions & 0 deletions packages/imagine/imagine/blurs.b
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Image blurs

/**
* Blurs the image using the Gaussian method.
*/
var BLUR_SELECTIVE = 1

/**
* Blurs the image.
*/
var BLUR_GAUSSIAN = 2
54 changes: 54 additions & 0 deletions packages/imagine/imagine/index.b
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,57 @@ import .crops { * }
import .arcs { * }
import .colors { * }
import .image { * }


/**
* Compose a truecolor value from its components.
*
* @param {number?} r - The red channel (0-255) - Default: 0
* @param {number?} g - The green channel (0-255) - Default: 0
* @param {number?} b - The blue channel (0-255) - Default: 0
* @param {number?} a - The alpha channel (0-127, where 127 is
* fully transparent, and 0 is completely opaque)
* - Default: 0.
* @returns {number}
*/
def true_color(r, g, b, a) {
if r == nil r = 0
if g == nil g = 0
if b == nil b = 0
if a == nil a = 0

if !is_number(r) or !is_number(g) or !is_number(b) or !is_number(a) {
die Exception('number expected')
}

if a == 0 {
return (a << 24) + (r << 16) + (g << 8) + b
} else {
return (r << 16) + (g << 8) + b
}
}


/**
* Decomposes an Image true color number into it's respective
* RGBA components.
*
* The function returns a dictionary that contains the following
* decomposed items:
*
* - `r` - The red channel value
* - `g` - The green channel value
* - `b` - The blue channel value
* - `a` - The alpha channel value
*
* @param {number} color
* @returns {dict}
*/
def decompose(color) {
var r = (c & 0xFF0000) >> 16
var g = (c & 0x00FF00) >> 8
var b = (c & 0x0000FF)
var a = (c & 0x7F000000) >> 24

return { r, g, b, a}
}
Loading

0 comments on commit 2b3b040

Please sign in to comment.