Skip to content

Commit

Permalink
improve image api
Browse files Browse the repository at this point in the history
  • Loading branch information
recp committed Oct 17, 2023
1 parent 182c299 commit 55b6651
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 58 deletions.
36 changes: 17 additions & 19 deletions include/ak/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,34 +101,32 @@ typedef struct AkImage {
AkTree *extra;
struct AkImage *next;

AkBool renderable;
AkBool renderableShare;
AkBool renderable;
AkBool renderableShare;

bool flipOnLoad;
} AkImage;

AK_EXPORT
void
ak_imageLoad(AkImage * __restrict image);

/* Loader Configurator */
typedef void* (*AkImageLoadFromFileFn)(AkHeap * __restrict heap,
void * __restrict memparent,
const char * __restrict path,
int * __restrict width,
int * __restrict height,
int * __restrict components);
typedef void* (*AkImageLoadFromMemoryFn)(AkHeap * __restrict heap,
void * __restrict memparent,
const char * __restrict data,
size_t len,
int * __restrict width,
int * __restrict height,
int * __restrict components);
typedef void (*AkImageFlipVerticallyOnLoad)(bool flip);
typedef AkImageData* (*AkImageLoadFromFileFn)(AkHeap * __restrict heap,
AkImage * __restrict image,
const char * __restrict path,
bool flipVertically);

typedef AkImageData* (*AkImageLoadFromMemoryFn)(AkHeap * __restrict heap,
AkImage * __restrict image,
AkBuffer * __restrict buff,
bool flipVertically);

typedef void (*AkImageFlipVerticallyOnLoad)(bool flip);

AK_EXPORT
void
ak_imageInitLoader(AkImageLoadFromFileFn fromFile,
AkImageLoadFromMemoryFn fromMemory,
AkImageFlipVerticallyOnLoad flipper);
ak_imageInitLoader(AkImageLoadFromFileFn fromFile,
AkImageLoadFromMemoryFn fromMemory);

#endif /* assetkit_image_h */
51 changes: 12 additions & 39 deletions src/image/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,18 @@
#endif

typedef struct AkImageConf {
AkImageLoadFromFileFn loadFromFile;
AkImageLoadFromMemoryFn loadFromMemory;
AkImageFlipVerticallyOnLoad flipVerticallyOnLoad;
AkImageLoadFromFileFn loadFromFile;
AkImageLoadFromMemoryFn loadFromMemory;
} AkImageConf;

static AkImageConf ak__img_conf = {0};

AK_EXPORT
void
ak_imageInitLoader(AkImageLoadFromFileFn fromFile,
AkImageLoadFromMemoryFn fromMemory,
AkImageFlipVerticallyOnLoad flipper) {
ak_imageInitLoader(AkImageLoadFromFileFn fromFile,
AkImageLoadFromMemoryFn fromMemory) {
ak__img_conf.loadFromFile = fromFile;
ak__img_conf.loadFromMemory = fromMemory;
ak__img_conf.flipVerticallyOnLoad = flipper;
}

AK_EXPORT
Expand All @@ -55,15 +52,15 @@ ak_imageLoad(AkImage * __restrict image) {
if (image->data)
return;

idata = NULL;
data = NULL;
heap = ak_heap_getheap(image);
doc = ak_heap_data(heap);
idata = NULL;
data = NULL;
heap = ak_heap_getheap(image);
doc = ak_heap_data(heap);
flipImage = false;

/* glTF uses top-left as origin */
if (doc->inf->flipImage) {
flipImage = ak_opt_get(AK_OPT_IMAGE_LOAD_FLIP_VERTICALLY);
ak__img_conf.flipVerticallyOnLoad(flipImage);
}

if (image->initFrom) {
Expand All @@ -78,37 +75,13 @@ ak_imageLoad(AkImage * __restrict image) {
if (!ak__img_conf.loadFromFile)
return;

path = ak_fullpath(doc, initFrom->ref, pathbuf);
data = ak__img_conf.loadFromFile(heap, image, path, &x, &y, &ch);
if (!data)
return;

idata = ak_heap_calloc(heap, image, sizeof(*idata));
idata->width = x;
idata->height = y;
idata->comp = ch;
idata->data = data;

image->data = idata;
path = ak_fullpath(doc, initFrom->ref, pathbuf);
image->data = ak__img_conf.loadFromFile(heap, image, path, flipImage);
} else if (initFrom->buff && initFrom->buff->data) {
if (!ak__img_conf.loadFromMemory)
return;

data = ak__img_conf.loadFromMemory(heap,
image,
initFrom->buff->data,
(int)initFrom->buff->length,
&x, &y, &ch);

if (!data)
return;

idata = ak_heap_calloc(heap, image, sizeof(*idata));
idata->width = x;
idata->height = y;
idata->comp = ch;
idata->data = data;
image->data = idata;
image->data = ak__img_conf.loadFromMemory(heap, image, initFrom->buff, flipImage);
} else if (initFrom->hex) {
/* TODO: */
}
Expand Down

0 comments on commit 55b6651

Please sign in to comment.