From ce8d9ebb240f5afe28af2fe45ec156d20cb4b157 Mon Sep 17 00:00:00 2001 From: Anton Lydike Date: Fri, 13 Mar 2020 22:46:09 +0100 Subject: [PATCH] modularized code a bit, added readme --- README.md | 22 ++++++++++++-- images/color.c | 32 +++++++++++++++++++++ images/drawing.c | 36 +++++++++++++++++++++++ images/images.c | 74 ------------------------------------------------ images/images.h | 4 ++- 5 files changed, 91 insertions(+), 77 deletions(-) create mode 100644 images/color.c create mode 100644 images/drawing.c diff --git a/README.md b/README.md index 71a16b0..6ff7885 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,21 @@ -# C learning workplace +# `images.h` - Image manipulation in C -here we learn C. \ No newline at end of file +An easy to use image creation tool for C. + +## Features + +* Save images as `.bmp` bitmaps +* Draw Rectangles, squares and circles +* Colors with transparency +* draw on individual pixels + +## Might be added later + +* draw lines +* draw lines on a path +* complex shapes? + + +## Performance + +While perfomance is not through the roof, creating a 4k image with a simple color gradient takes about 1.5s on a mobile ryzen 5 chip. Smaller graphics (like the transparency demo) are drawn and saved within 20ms, so that's okay, I guess... \ No newline at end of file diff --git a/images/color.c b/images/color.c new file mode 100644 index 0000000..4e36cd2 --- /dev/null +++ b/images/color.c @@ -0,0 +1,32 @@ +#include "images.h" + +Color color_new(int r, int g, int b) { + Color c; + c.r = r; + c.g = g; + c.b = b; + c.alpha = 1; + return c; +} + +Color color_new_alpha(int r, int g, int b, float alpha) { + Color c; + c.r = r; + c.g = g; + c.b = b; + c.alpha = alpha; + return c; +} + +void print_color(Color c) { + printf("color is: #%02hhX%02hhX%02hhX\n", c.r, c.g, c.b); +} + +Color color_mix(Color c1, Color c2, float ratio) { + return color_new_alpha( + ratio * c1.r + ((1 - ratio) * c2.r), + ratio * c1.g + ((1 - ratio) * c2.g), + ratio * c1.b + ((1 - ratio) * c2.b), + ratio * c1.alpha + ((1 - ratio) * c2.alpha) + ); +} diff --git a/images/drawing.c b/images/drawing.c new file mode 100644 index 0000000..5e8d001 --- /dev/null +++ b/images/drawing.c @@ -0,0 +1,36 @@ +#include "images.h" + +int image_draw_rect(Image image, int x1, int y1, int x2, int y2, Color color) { + int retval = 1; + int y; + for (; x1 < x2; x1++) { + for (y = y1; y < y2; y++) { + if (!image_set_px_c(image, x1, y, color)) retval = 0; + } + } + + return retval; +} + +int image_draw_circ(Image image, int x, int y, int r, Color color) { + int retval = 1; + int i, j, r2 = r * r; + for (i = -r+1; i < r; i++) { + for (j = -r+1; j < r; j++) { + if (i*i + j*j >= r2) continue; + if (!image_set_px_c(image, x + i, y + j, color)) retval = 0; + } + } + return retval; +} + +int image_draw_square(Image image, int x, int y, int size, Color color) { + int retval = 1; + int i, j; + for (i = -size / 2; i <= size / 2; i++) { + for (j = -size / 2; j <= size / 2; j++) { + if (!image_set_px_c(image, x + i, y + j, color)) retval = 0; + } + } + return retval; +} \ No newline at end of file diff --git a/images/images.c b/images/images.c index 410577c..04debae 100644 --- a/images/images.c +++ b/images/images.c @@ -64,77 +64,3 @@ int image_destroy(Image image) { return 1; } - - -///////// Colors - -Color color_new(int r, int g, int b) { - Color c; - c.r = r; - c.g = g; - c.b = b; - c.alpha = 1; - return c; -} - -Color color_new_alpha(int r, int g, int b, float alpha) { - Color c; - c.r = r; - c.g = g; - c.b = b; - c.alpha = alpha; - return c; -} - -void print_color(Color c) { - printf("color is: #%02hhX%02hhX%02hhX\n", c.r, c.g, c.b); -} - -Color color_mix(Color c1, Color c2, float ratio) { - return color_new_alpha( - ratio * c1.r + ((1 - ratio) * c2.r), - ratio * c1.g + ((1 - ratio) * c2.g), - ratio * c1.b + ((1 - ratio) * c2.b), - ratio * c1.alpha + ((1 - ratio) * c2.alpha) - ); -} - - - - -//// IMAGE DRAWING HELPERS - -int image_draw_rect(Image image, int x1, int y1, int x2, int y2, Color color) { - int retval = 1; - int y; - for (; x1 < x2; x1++) { - for (y = y1; y < y2; y++) { - if (!image_set_px_c(image, x1, y, color)) retval = 0; - } - } - - return retval; -} - -int image_draw_circ(Image image, int x, int y, int r, Color color) { - int retval = 1; - int i, j, r2 = r * r; - for (i = -r+1; i < r; i++) { - for (j = -r+1; j < r; j++) { - if (i*i + j*j >= r2) continue; - if (!image_set_px_c(image, x + i, y + j, color)) retval = 0; - } - } - return retval; -} - -int image_draw_square(Image image, int x, int y, int size, Color color) { - int retval = 1; - int i, j; - for (i = -size / 2; i <= size / 2; i++) { - for (j = -size / 2; j <= size / 2; j++) { - if (!image_set_px_c(image, x + i, y + j, color)) retval = 0; - } - } - return retval; -} \ No newline at end of file diff --git a/images/images.h b/images/images.h index 527e580..22d6018 100644 --- a/images/images.h +++ b/images/images.h @@ -1,5 +1,5 @@ #ifndef IMAGES_H -#define IMAGES_H +#define IMAGES_H "v0.1.0" typedef struct _color { unsigned char r; @@ -47,5 +47,7 @@ int image_save_bmp(Image image, char* path); #include "images.c" #include "bmp-format.c" +#include "color.c" +#include "drawing.c" #endif \ No newline at end of file