Skip to content

Commit

Permalink
Merge pull request #17 from RobLoach/stb_image
Browse files Browse the repository at this point in the history
Add stb_image for image loading
  • Loading branch information
grz0zrg authored Jan 5, 2022
2 parents 63874be + 4f4b8db commit da41067
Show file tree
Hide file tree
Showing 4 changed files with 7,954 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Features :
* OpenGL ES 2.0 rendering backend for Raspberry PI or through fbdev (tested on Nano PI Fire 3 board)
* dispmanx rendering backend (Video Core IV; Raspberry PI)
* Optional : Full parallelism, execute graphics code on multiple CPU cores **with a single function**
* PNG/JPEG images loading (provided by [LodePNG](https://lodev.org/lodepng/) and [NanoJPEG](http://keyj.emphy.de/nanojpeg/))
* Image loading (provided by [LodePNG](https://lodev.org/lodepng/), [NanoJPEG](http://keyj.emphy.de/nanojpeg/), and [stb_image](https://github.com/nothings/stb/blob/master/stb_image.h))
* Bitmap fonts for drawing texts
* Bare-metal graphics primitive (pixels, rectangles, lines, polygon)
* Easy to do fading, clipping and screen-clearing related effects (motion blur etc.)
Expand Down Expand Up @@ -393,7 +393,10 @@ To compile liblfds parallel examples, just copy `liblfds711.a` / `liblfds711.h`
This library may be used for size optimized executable for things like [demos](https://en.wikipedia.org/wiki/Demoscene)
PNG and JPEG support can be disabled with the `WITHOUT_JPEG` and `WITHOUT_PNG` define.
Image support can be disabled with the following defines:
- `WITHOUT_JPEG`
- `WITHOUT_PNG`
- `WITHOUT_STB_IMAGE`
See `tiny` makefile rule inside the `custom_backend` or `examples` folder for some compiler optimizations related to executable size.
Expand Down
40 changes: 40 additions & 0 deletions src/fbgraphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
#include "nanojpeg/nanojpeg.c"
#endif

#ifndef WITHOUT_STB_IMAGE
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#endif

#include "fbgraphics.h"

#ifdef FBG_PARALLEL
Expand Down Expand Up @@ -1413,6 +1418,35 @@ struct _fbg_img *fbg_loadPNG(struct _fbg *fbg, const char *filename) {
}
#endif

#ifndef WITHOUT_STB_IMAGE
struct _fbg_img *fbg_loadSTBImage(struct _fbg *fbg, const char *filename) {
unsigned char *data;
int width;
int height;
int components;

data = stbi_load(filename, &width, &height, &components, fbg->components);
if (!data) {
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_loadSTBImage: Image '%s' data allocation failed\n", filename);

stbi_image_free(data);

return NULL;
}

img->data = data;

return img;
}
#endif

struct _fbg_img *fbg_loadImage(struct _fbg *fbg, const char *filename) {
struct _fbg_img *img = NULL;

Expand All @@ -1426,6 +1460,12 @@ struct _fbg_img *fbg_loadImage(struct _fbg *fbg, const char *filename) {
}
#endif

#ifndef WITHOUT_STB_IMAGE
if (img == NULL) {
img = fbg_loadSTBImage(fbg, filename);
}
#endif

return img;
}

Expand Down
12 changes: 12 additions & 0 deletions src/fbgraphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,18 @@
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()
*/
extern struct _fbg_img *fbg_loadSTBImage(struct _fbg *fbg, const char *filename);
#endif

//! load an image (PNG or JPEG)
/*!
\param fbg pointer to a FBG context / data structure
Expand Down
Loading

0 comments on commit da41067

Please sign in to comment.