summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/painter/canvas.h76
-rw-r--r--src/blender/canvas.c23
-rw-r--r--src/painter/canvas.c27
3 files changed, 103 insertions, 23 deletions
diff --git a/include/painter/canvas.h b/include/painter/canvas.h
new file mode 100644
index 0000000..e354938
--- /dev/null
+++ b/include/painter/canvas.h
@@ -0,0 +1,76 @@
1#ifndef UPEM_MORPHING_CANVAS
2#define UPEM_MORPHING_CANVAS
3
4/**
5 * File: canvas.h
6 * "Everyday is a good day when you paint" – Bob Ross
7 */
8
9#include <MLV/MLV_image.h>
10#include "common/geom.h"
11#include "painter/color.h"
12
13/**
14 * Type: Canvas
15 * Represents a fixed size RGBa pixel matrix.
16 */
17typedef struct {
18 MLV_Image *mlv;
19} Canvas;
20
21/**
22 * Function: canvas_create
23 * Initialises a canvas of the given size
24 *
25 * Parameters:
26 * width - the width in pixels
27 * height - the height in pixels
28 */
29Canvas *canvas_create(IntVector width, IntVector height);
30
31/**
32 * Function: canvas_destroy
33 * Frees all memory allocated to a canvas.
34 *
35 * Parameters:
36 * *c - the canvas to destroy
37 */
38void canvas_destroy(Canvas *c);
39
40/**
41 * Function: canvas_set_pixel
42 * Sets the pixel colour at the given coordinates.
43 *
44 * Parameters:
45 * *c - the canvas to alter
46 * pos - the coordinate of the pixel to set
47 * color - the new colour to set
48 */
49void canvas_set_pixel(Canvas *c, CartesianVector pos, Color color);
50
51/**
52 * Function: canvas_get_pixel
53 * Returns the colour of the pixel at the given position.
54 *
55 * Parameters:
56 * *c - the base canvas
57 * pos - the coordinate of the pixel to get
58 *
59 * Returns:
60 * The colour of the requested pixel
61 */
62Color canvas_get_pixel(Canvas *c, CartesianVector pos);
63
64/**
65 * Function: canvas_get_dim
66 * Returns the size (in pixels) of the given canvas.
67 *
68 * Parameters:
69 * *c - the canvas
70 *
71 * Returns:
72 * The size of the canvas
73 */
74CartesianVector canvas_get_dim(Canvas *c);
75
76#endif
diff --git a/src/blender/canvas.c b/src/blender/canvas.c
deleted file mode 100644
index b7cd9dc..0000000
--- a/src/blender/canvas.c
+++ /dev/null
@@ -1,23 +0,0 @@
1#include "blender/canvas.h"
2
3void canvas_init(Canvas *canvas, IntVector width, IntVector height) {
4 canvas->mlv = MLV_create_image(width, height);
5}
6
7void canvas_free(Canvas *canvas) {
8 MLV_free_image(canvas->mlv);
9}
10
11void canvas_set_pixel(Canvas *canvas, CartesianVector position, Color color) {
12 MLV_set_pixel_on_image(position.x, position.y, color.mlv, canvas->mlv);
13}
14
15Color canvas_get_pixel(Canvas *canvas, CartesianVector position) {
16 int r, g, b, a;
17 MLV_get_pixel_on_image(canvas->mlv, position.x, position.y, &r, &g, &b, &a);
18 return (Color) {{r, g, b, a}};
19}
20
21CartesianVector canvas_get_dim(Canvas *canvas) {
22 return (CartesianVector) {MLV_get_image_width(canvas->mlv), MLV_get_image_height(canvas->mlv)};
23}
diff --git a/src/painter/canvas.c b/src/painter/canvas.c
new file mode 100644
index 0000000..53deeb9
--- /dev/null
+++ b/src/painter/canvas.c
@@ -0,0 +1,27 @@
1#include "painter/canvas.h"
2#include "common/mem.h"
3
4Canvas *canvas_create(IntVector width, IntVector height) {
5 Canvas *c = malloc_or_die(sizeof(Canvas));
6 c->mlv = MLV_create_image(width, height);
7 return c;
8}
9
10void canvas_destroy(Canvas *c) {
11 MLV_free_image(c->mlv);
12 free(c);
13}
14
15void canvas_set_pixel(Canvas *c, CartesianVector pos, Color color) {
16 MLV_set_pixel_on_image(pos.x, pos.y, color.mlv, c->mlv);
17}
18
19Color canvas_get_pixel(Canvas *c, CartesianVector pos) {
20 int r, g, b, a;
21 MLV_get_pixel_on_image(c->mlv, pos.x, pos.y, &r, &g, &b, &a);
22 return (Color) {{a, b, g, r}};
23}
24
25CartesianVector canvas_get_dim(Canvas *c) {
26 return (CartesianVector) {MLV_get_image_width(c->mlv), MLV_get_image_height(c->mlv)};
27}