Skip to content

Commit

Permalink
Updated headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Nov 27, 2024
1 parent f31d59e commit adfa5ae
Show file tree
Hide file tree
Showing 6 changed files with 554 additions and 518 deletions.
150 changes: 78 additions & 72 deletions src/blend2d-command-draw-test.c
Original file line number Diff line number Diff line change
@@ -1,72 +1,78 @@
//
// Blend2D experimental Rebol extension
// ====================================
// Use on your own risc!

#include "blend2d-rebol-extension.h"

// Function for quick native experimenting
int cmd_draw_test(RXIFRM *frm, void *reb_ctx) {
BLResult r;
BLImageCore img;
BLContextCore ctx;
REBXYF size;
REBINT w, h;
REBSER *reb_img = 0;

if (RXA_TYPE(frm, 1) == RXT_PAIR) {
size = RXA_PAIR(frm, 1);
w = ROUND_TO_INT(size.x);
h = ROUND_TO_INT(size.y);
reb_img = (REBSER *)RL_MAKE_IMAGE(w,h);
}
else {
w = RXA_IMAGE_WIDTH(frm, 1);
h = RXA_IMAGE_HEIGHT(frm, 1);
reb_img = (REBSER *)RXA_ARG(frm,1).image;
}
RXA_TYPE(frm, 1) = RXT_IMAGE;
RXA_ARG(frm, 1).width = w;
RXA_ARG(frm, 1).height = h;
RXA_ARG(frm, 1).image = reb_img;

blImageInit(&img);
r = blImageCreateFromData(&img, w, h, BL_FORMAT_PRGB32, reb_img->data, (intptr_t)w * 4, BL_DATA_ACCESS_WRITE, NULL, NULL);
if (r != BL_SUCCESS) return r;

r = blContextInitAs(&ctx, &img, NULL);
if (r != BL_SUCCESS) return r;

// now process some drawing...

BLGradientCore gradient;
BLLinearGradientValues values = { 0, 0, 256, 256 };
r = blGradientInitAs(&gradient, BL_GRADIENT_TYPE_LINEAR, &values, BL_EXTEND_MODE_PAD, NULL, 0, NULL);
if (r != BL_SUCCESS) return 1;

blGradientAddStopRgba32(&gradient, 0.0, 0xFFFFFFFFu);
blGradientAddStopRgba32(&gradient, 0.5, 0xFFFFAF00u);
blGradientAddStopRgba32(&gradient, 1.0, 0xFFFF0000u);

blContextSetFillStyle(&ctx, &gradient);
blContextFillAll(&ctx);
blGradientReset(&gradient);


end_ctx:

// END ...
blContextEnd(&ctx);
trace("ok");

// output to file...
BLImageCodecCore codec;
blImageCodecInit(&codec);
blImageCodecFindByName(&codec, "BMP", SIZE_MAX, NULL);
blImageWriteToFile(&img, "test.bmp", &codec);
blImageCodecReset(&codec);

blImageReset(&img);
blContextReset(&ctx);
return RXR_UNSET;
}
// ____ __ __ ______ __
// / __ \/ /__/ /__ ___ /_ __/__ ____/ /
// / /_/ / / _ / -_|_-<_ / / / -_) __/ _ \
// \____/_/\_,_/\__/___(@)_/ \__/\__/_// /
// ~~~ oldes.huhuman at gmail.com ~~~ /_/
//
// SPDX-License-Identifier: Apache-2.0
// =============================================================================
// Rebol/Blend2D extension commands
// =============================================================================

#include "blend2d-rebol-extension.h"

// Function for quick native experimenting
int cmd_draw_test(RXIFRM *frm, void *reb_ctx) {
BLResult r;
BLImageCore img;
BLContextCore ctx;
REBXYF size;
REBINT w, h;
REBSER *reb_img = 0;

if (RXA_TYPE(frm, 1) == RXT_PAIR) {
size = RXA_PAIR(frm, 1);
w = ROUND_TO_INT(size.x);
h = ROUND_TO_INT(size.y);
reb_img = (REBSER *)RL_MAKE_IMAGE(w,h);
}
else {
w = RXA_IMAGE_WIDTH(frm, 1);
h = RXA_IMAGE_HEIGHT(frm, 1);
reb_img = (REBSER *)RXA_ARG(frm,1).image;
}
RXA_TYPE(frm, 1) = RXT_IMAGE;
RXA_ARG(frm, 1).width = w;
RXA_ARG(frm, 1).height = h;
RXA_ARG(frm, 1).image = reb_img;

blImageInit(&img);
r = blImageCreateFromData(&img, w, h, BL_FORMAT_PRGB32, reb_img->data, (intptr_t)w * 4, BL_DATA_ACCESS_WRITE, NULL, NULL);
if (r != BL_SUCCESS) return r;

r = blContextInitAs(&ctx, &img, NULL);
if (r != BL_SUCCESS) return r;

// now process some drawing...

BLGradientCore gradient;
BLLinearGradientValues values = { 0, 0, 256, 256 };
r = blGradientInitAs(&gradient, BL_GRADIENT_TYPE_LINEAR, &values, BL_EXTEND_MODE_PAD, NULL, 0, NULL);
if (r != BL_SUCCESS) return 1;

blGradientAddStopRgba32(&gradient, 0.0, 0xFFFFFFFFu);
blGradientAddStopRgba32(&gradient, 0.5, 0xFFFFAF00u);
blGradientAddStopRgba32(&gradient, 1.0, 0xFFFF0000u);

blContextSetFillStyle(&ctx, &gradient);
blContextFillAll(&ctx);
blGradientReset(&gradient);


end_ctx:

// END ...
blContextEnd(&ctx);
trace("ok");

// output to file...
BLImageCodecCore codec;
blImageCodecInit(&codec);
blImageCodecFindByName(&codec, "BMP", SIZE_MAX, NULL);
blImageWriteToFile(&img, "test.bmp", &codec);
blImageCodecReset(&codec);

blImageReset(&img);
blContextReset(&ctx);
return RXR_UNSET;
}
96 changes: 51 additions & 45 deletions src/blend2d-command-font.c
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
//
// Blend2D experimental Rebol extension
// ====================================
// Use on your own risc!

#include "blend2d-rebol-extension.h"

void* releaseFontFace(void* font) {
debug_print("releasing font: %p\n", font);
blFontFaceDestroy((BLFontFaceCore*)font);
return NULL;
}


int cmd_font(RXIFRM* frm, void* reb_ctx) {
BLResult ret;
REBSER* src;
BLFontFaceCore* face = NULL;

REBHOB* hob = RL_MAKE_HANDLE_CONTEXT(Handle_BLFontFace);

if (hob == NULL) {
RXA_SERIES(frm,1) = "Blend2D failed to make a font handle!";
return RXR_ERROR;
}

src = RXA_SERIES(frm, 1);
src = RL_ENCODE_UTF8_STRING(SERIES_DATA(src), SERIES_TAIL(src), SERIES_WIDE(src) > 1, FALSE);

face = (BLFontFaceCore*)hob->data;
blFontFaceInit(face);

ret = blFontFaceCreateFromFile(face, SERIES_TEXT(src), BL_FILE_READ_MMAP_ENABLED | BL_FILE_READ_MMAP_AVOID_SMALL);
if (ret != BL_SUCCESS) {
debug_print("Failed to load font: %s, reason: %i\n", SERIES_DATA(src), ret);
RXA_SERIES(frm,1) = "Blend2D's blFontFaceCreateFromFile failed!";
return RXR_ERROR;
}
hob->flags |= HANDLE_CONTEXT; //@@ temp fix!
RXA_HANDLE(frm, 1) = hob;
RXA_HANDLE_TYPE(frm, 1) = hob->sym;
RXA_HANDLE_FLAGS(frm, 1) = hob->flags;
RXA_TYPE(frm, 1) = RXT_HANDLE;
return RXR_VALUE;
}
// ____ __ __ ______ __
// / __ \/ /__/ /__ ___ /_ __/__ ____/ /
// / /_/ / / _ / -_|_-<_ / / / -_) __/ _ \
// \____/_/\_,_/\__/___(@)_/ \__/\__/_// /
// ~~~ oldes.huhuman at gmail.com ~~~ /_/
//
// SPDX-License-Identifier: Apache-2.0
// =============================================================================
// Rebol/Blend2D extension commands
// =============================================================================

#include "blend2d-rebol-extension.h"

void* releaseFontFace(void* font) {
debug_print("releasing font: %p\n", font);
blFontFaceDestroy((BLFontFaceCore*)font);
return NULL;
}


int cmd_font(RXIFRM* frm, void* reb_ctx) {
BLResult ret;
REBSER* src;
BLFontFaceCore* face = NULL;

REBHOB* hob = RL_MAKE_HANDLE_CONTEXT(Handle_BLFontFace);

if (hob == NULL) {
RXA_SERIES(frm,1) = "Blend2D failed to make a font handle!";
return RXR_ERROR;
}

src = RXA_SERIES(frm, 1);
src = RL_ENCODE_UTF8_STRING(SERIES_DATA(src), SERIES_TAIL(src), SERIES_WIDE(src) > 1, FALSE);

face = (BLFontFaceCore*)hob->data;
blFontFaceInit(face);

ret = blFontFaceCreateFromFile(face, SERIES_TEXT(src), BL_FILE_READ_MMAP_ENABLED | BL_FILE_READ_MMAP_AVOID_SMALL);
if (ret != BL_SUCCESS) {
debug_print("Failed to load font: %s, reason: %i\n", SERIES_DATA(src), ret);
RXA_SERIES(frm,1) = "Blend2D's blFontFaceCreateFromFile failed!";
return RXR_ERROR;
}
hob->flags |= HANDLE_CONTEXT; //@@ temp fix!
RXA_HANDLE(frm, 1) = hob;
RXA_HANDLE_TYPE(frm, 1) = hob->sym;
RXA_HANDLE_FLAGS(frm, 1) = hob->flags;
RXA_TYPE(frm, 1) = RXT_HANDLE;
return RXR_VALUE;
}
148 changes: 77 additions & 71 deletions src/blend2d-command-image.c
Original file line number Diff line number Diff line change
@@ -1,72 +1,78 @@
//
// Blend2D experimental Rebol extension
// ====================================
// Use on your own risc!

#include "blend2d-rebol-extension.h"

void* releaseImage(void* image) {
debug_print("releasing image: %p\n", image);
blImageDestroy(image);
return NULL;
}

BLResult b2d_init_image_from_file(BLImageCore* image, REBSER* fileName) {
BLArrayCore codecs;
blImageCodecArrayInitBuiltInCodecs(&codecs);
return blImageReadFromFile(image, SERIES_TEXT(fileName), &codecs);
}
BLResult b2d_init_image_from_image(BLImageCore* image, void* data, REBINT width, REBINT height) {
return blImageCreateFromData(image, width, height, BL_FORMAT_PRGB32, data, (intptr_t)width * 4, BL_DATA_ACCESS_RW, NULL, NULL);
}

BLResult b2d_init_image_from_arg(BLImageCore* image, RXIARG arg, REBCNT type) {
REBHOB* hob;
switch (type) {
// case RXT_HANDLE:
// hob = arg.handle.ptr;
// if (hob->sym != Handle_BLImage)
// return BL_ERROR_INVALID_HANDLE;
// image = (BLImageCore*)hob->data;
// return BL_SUCCESS;
case RXT_IMAGE:
blImageReset(image);
return blImageCreateFromData(image, arg.width, arg.height, BL_FORMAT_PRGB32, ((REBSER*)arg.series)->data, (intptr_t)arg.width * 4, BL_DATA_ACCESS_RW, NULL, NULL);
case RXT_FILE:
blImageReset(image);
return b2d_init_image_from_file(image, (REBSER*)arg.series);
case RXT_PAIR:
blImageReset(image);
return blImageCreate(image, arg.width, arg.height, BL_FORMAT_PRGB32);
}
return BL_ERROR_INVALID_VALUE;
}

int cmd_image(RXIFRM* frm, void* reb_ctx) {
BLResult r;
BLImageCore* image;
REBHOB* hob = RL_MAKE_HANDLE_CONTEXT(Handle_BLImage);
if (hob == NULL) {
r = BL_ERROR_OUT_OF_MEMORY;
goto error;
}

image = (BLImageCore*)hob->data;
debug_print("New image handle: %u data: %p\n", hob->sym, hob->data);
blImageInit(image);

r = b2d_init_image_from_arg(image, RXA_ARG(frm, 1), RXA_TYPE(frm, 1));
if (r != BL_SUCCESS) goto error;

hob->flags |= HANDLE_CONTEXT; //@@ temp fix!
RXA_HANDLE(frm, 1) = hob;
RXA_HANDLE_TYPE(frm, 1) = hob->sym;
RXA_HANDLE_FLAGS(frm, 1) = hob->flags;
RXA_TYPE(frm, 1) = RXT_HANDLE;
return RXR_VALUE;

error:
RXA_SERIES(frm,1) = "Blend2D failed to make an image handle!";
return RXR_ERROR;

// ____ __ __ ______ __
// / __ \/ /__/ /__ ___ /_ __/__ ____/ /
// / /_/ / / _ / -_|_-<_ / / / -_) __/ _ \
// \____/_/\_,_/\__/___(@)_/ \__/\__/_// /
// ~~~ oldes.huhuman at gmail.com ~~~ /_/
//
// SPDX-License-Identifier: Apache-2.0
// =============================================================================
// Rebol/Blend2D extension commands
// =============================================================================

#include "blend2d-rebol-extension.h"

void* releaseImage(void* image) {
debug_print("releasing image: %p\n", image);
blImageDestroy(image);
return NULL;
}

BLResult b2d_init_image_from_file(BLImageCore* image, REBSER* fileName) {
BLArrayCore codecs;
blImageCodecArrayInitBuiltInCodecs(&codecs);
return blImageReadFromFile(image, SERIES_TEXT(fileName), &codecs);
}
BLResult b2d_init_image_from_image(BLImageCore* image, void* data, REBINT width, REBINT height) {
return blImageCreateFromData(image, width, height, BL_FORMAT_PRGB32, data, (intptr_t)width * 4, BL_DATA_ACCESS_RW, NULL, NULL);
}

BLResult b2d_init_image_from_arg(BLImageCore* image, RXIARG arg, REBCNT type) {
REBHOB* hob;
switch (type) {
// case RXT_HANDLE:
// hob = arg.handle.ptr;
// if (hob->sym != Handle_BLImage)
// return BL_ERROR_INVALID_HANDLE;
// image = (BLImageCore*)hob->data;
// return BL_SUCCESS;
case RXT_IMAGE:
blImageReset(image);
return blImageCreateFromData(image, arg.width, arg.height, BL_FORMAT_PRGB32, ((REBSER*)arg.series)->data, (intptr_t)arg.width * 4, BL_DATA_ACCESS_RW, NULL, NULL);
case RXT_FILE:
blImageReset(image);
return b2d_init_image_from_file(image, (REBSER*)arg.series);
case RXT_PAIR:
blImageReset(image);
return blImageCreate(image, arg.width, arg.height, BL_FORMAT_PRGB32);
}
return BL_ERROR_INVALID_VALUE;
}

int cmd_image(RXIFRM* frm, void* reb_ctx) {
BLResult r;
BLImageCore* image;
REBHOB* hob = RL_MAKE_HANDLE_CONTEXT(Handle_BLImage);
if (hob == NULL) {
r = BL_ERROR_OUT_OF_MEMORY;
goto error;
}

image = (BLImageCore*)hob->data;
debug_print("New image handle: %u data: %p\n", hob->sym, hob->data);
blImageInit(image);

r = b2d_init_image_from_arg(image, RXA_ARG(frm, 1), RXA_TYPE(frm, 1));
if (r != BL_SUCCESS) goto error;

hob->flags |= HANDLE_CONTEXT; //@@ temp fix!
RXA_HANDLE(frm, 1) = hob;
RXA_HANDLE_TYPE(frm, 1) = hob->sym;
RXA_HANDLE_FLAGS(frm, 1) = hob->flags;
RXA_TYPE(frm, 1) = RXT_HANDLE;
return RXR_VALUE;

error:
RXA_SERIES(frm,1) = "Blend2D failed to make an image handle!";
return RXR_ERROR;

}
Loading

0 comments on commit adfa5ae

Please sign in to comment.