Skip to content

Commit

Permalink
Add fbg_loadImageFromMemory() to allow loading images from data buffers
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
RobLoach committed Jan 9, 2022
1 parent da41067 commit 0092e5f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
48 changes: 48 additions & 0 deletions src/fbgraphics.c
Expand Up @@ -27,6 +27,7 @@

#include <string.h>
#include <stdlib.h>
// TODO: Allow implementation of our own error reporting through WITHOUT_STDIO
#include <stdio.h>

#ifndef WITHOUT_PNG
Expand Down Expand Up @@ -1268,6 +1269,7 @@ struct _fbg_img *fbg_createImage(struct _fbg *fbg, unsigned int width, unsigned
return img;
}

#ifndef WITHOUT_STDIO
#ifndef WITHOUT_JPEG
struct _fbg_img *fbg_loadJPEG(struct _fbg *fbg, const char *filename) {
unsigned char *data;
Expand Down Expand Up @@ -1441,6 +1443,7 @@ struct _fbg_img *fbg_loadSTBImage(struct _fbg *fbg, const char *filename) {
return NULL;
}

free(img->data);
img->data = data;

return img;
Expand Down Expand Up @@ -1468,6 +1471,51 @@ struct _fbg_img *fbg_loadImage(struct _fbg *fbg, const char *filename) {

return img;
}
#endif // WITHOUT_STDIO

#ifndef WITHOUT_STB_IMAGE
struct _fbg_img *fbg_loadSTBImageFromMemory(struct _fbg *fbg, const unsigned char *buffer, int size) {
unsigned char *output;
int width;
int height;
int components;
output = stbi_load_from_memory(buffer, size, &width, &height, &components, fbg->components);
if (!output) {
fprintf(stderr, "fbg_loadSTBImage: %s\n", stbi_failure_reason());

return NULL;
}

struct _fbg_img *img = fbg_createImage(fbg, width, height);
if (!img) {
fprintf(stderr, "fbg_loadSTBImageFromMemory: Image of %ix%i data allocation failed\n", width, height);

stbi_image_free(output);

return NULL;
}

free(img->data);
img->data = output;

return img;
}
#endif

struct _fbg_img *fbg_loadImageFromMemory(struct _fbg *fbg, const unsigned char *data, int size) {
struct _fbg_img *img = NULL;

// TODO: Implement fbg_loadPNGFromMemory()
// TODO: Implement fbg_loadJPEGFromMemory()

#ifndef WITHOUT_STB_IMAGE
if (img == NULL) {
img = fbg_loadSTBImageFromMemory(fbg, data, size);
}
#endif

return img;
}

void fbg_image(struct _fbg *fbg, struct _fbg_img *img, int x, int y) {
unsigned char *pix_pointer = (unsigned char *)(fbg->back_buffer + (y * fbg->line_length) + x * fbg->components);
Expand Down
27 changes: 25 additions & 2 deletions src/fbgraphics.h
Expand Up @@ -589,6 +589,7 @@
*/
extern struct _fbg_img *fbg_createImage(struct _fbg *fbg, unsigned int width, unsigned int height);

#ifndef WITHOUT_STDIO
//! load a PNG image from a file (lodePNG library)
/*!
\param fbg pointer to a FBG context / data structure
Expand All @@ -611,15 +612,14 @@
extern struct _fbg_img *fbg_loadJPEG(struct _fbg *fbg, const char *filename);
#endif

#ifndef WITHOUT_STB_IMAGE

//! load an image from a file (STB Image library)
/*!
\param fbg pointer to a FBG context / data structure
\param filename image filename
\return _fbg_img data structure pointer
\sa fbg_freeImage(), fbg_image(), fbg_imageFlip(), fbg_createFont(), fbg_imageClip(), fbg_loadImage(), fbg_imageEx(), fbg_imageScale(), fbg_imageColorkey()
*/
#ifndef WITHOUT_STB_IMAGE
extern struct _fbg_img *fbg_loadSTBImage(struct _fbg *fbg, const char *filename);
#endif

Expand All @@ -631,6 +631,29 @@
\sa fbg_freeImage(), fbg_image(), fbg_imageFlip(), fbg_createFont(), fbg_imageClip(), fbg_loadPNG(), fbg_loadJPEG(), fbg_imageEx(), fbg_imageScale(), fbg_imageColorkey()
*/
extern struct _fbg_img *fbg_loadImage(struct _fbg *fbg, const char *filename);
#endif // WITHOUT_STDIO

//! load an image from memory (STB Image library)
/*!
\param fbg pointer to a FBG context / data structure
\param data The image data from memory.
\param size The size of the image in bytes.
\return _fbg_img data structure pointer
\sa fbg_freeImage(), fbg_image(), fbg_imageFlip(), fbg_createFont(), fbg_imageClip(), fbg_loadPNG(), fbg_loadJPEG(), fbg_imageEx(), fbg_imageScale(), fbg_imageColorkey()
*/
#ifndef WITHOUT_STB_IMAGE
extern struct _fbg_img *fbg_loadSTBImageFromMemory(struct _fbg *fbg, const unsigned char *data, int size);
#endif

//! load an image from memory
/*!
\param fbg pointer to a FBG context / data structure
\param data The image data from memory.
\param size The size of the image in bytes.
\return _fbg_img data structure pointer
\sa fbg_freeImage(), fbg_image(), fbg_imageFlip(), fbg_createFont(), fbg_imageClip(), fbg_loadPNG(), fbg_loadJPEG(), fbg_imageEx(), fbg_imageScale(), fbg_imageColorkey()
*/
extern struct _fbg_img *fbg_loadImageFromMemory(struct _fbg *fbg, const unsigned char *data, int size);

//! draw an image
/*!
Expand Down

0 comments on commit 0092e5f

Please sign in to comment.