Gama C Library
Gama C API Documentation
image.h
Go to the documentation of this file.
1#pragma once
2#ifndef STB_IMAGE_IMPLEMENTATION
3#define STB_IMAGE_IMPLEMENTATION
4#endif
5
6#include "../stb/stb_image.h"
7#include <stdint.h>
8#include <string.h>
9#include "color.h"
10
11/**
12 * @brief A container for raw, CPU-side image pixel data.
13 *
14 * This struct holds raw pixel data decoded from an image file. It is typically
15 * used as an intermediate step before creating a `gmImage`, which manages the
16 * texture on the GPU.
17 */
18typedef struct {
19 int32_t width, height; /**< Dimensions of the image in pixels. */
20 unsigned char *data; /**< Pointer to the raw RGBA pixel data. */
22
23/**
24 * @brief Loads image file from disk into a gmImageData struct.
25 * @param data A pointer to the gmImageData struct to be filled.
26 * @param path The file path of the image to load.
27 * @return 0 on success, -1 on failure.
28 */
29int32_t gm_image_data_load(gmImageData *data, const char *path) {
30 memset(data, 0, sizeof(gmImageData));
31 data->data = stbi_load(path, &data->width, &data->height, NULL, 4);
32 return data->data ? 0 : -1;
33}
34
35/**
36 * @brief Loads image data from an in-memory buffer.
37 * @param data Pointer to the gmImageData structure to fill.
38 * @param buffer Pointer to the buffer containing the raw image file data.
39 * @param len The length of the buffer in bytes.
40 * @return 0 on success, -1 on failure.
41 */
43 const unsigned char *buffer, int len) {
44 memset(data, 0, sizeof(gmImageData));
45 data->data =
46 stbi_load_from_memory(buffer, len, &data->width, &data->height, NULL, 4);
47 return data->data ? 0 : -1;
48}
49
50/**
51 * @brief Frees the pixel data buffer of a gmImageData struct.
52 * @param d A pointer to the gmImageData struct to free.
53 * @return 0 on success.
54 */
56 free(d->data);
57 d->width = 0;
58 d->height = 0;
59 return 0;
60}
61
62/**
63 * @brief Calculates the average color of an image's raw pixel data.
64 * @param data Pointer to the gmImageData structure.
65 * @return The average color as a gmColor.
66 */
68 if (!data || !data->data || data->width <= 0 || data->height <= 0) {
69 return 0; // Return black for invalid data
70 }
71
72 long long total_r = 0;
73 long long total_g = 0;
74 long long total_b = 0;
75 size_t pixel_count = data->width * data->height;
76
77 for (size_t i = 0; i < pixel_count; ++i) {
78 total_r += data->data[i * 4 + 0];
79 total_g += data->data[i * 4 + 1];
80 total_b += data->data[i * 4 + 2];
81 }
82
83 unsigned char avg_r = (unsigned char)(total_r / pixel_count);
84 unsigned char avg_g = (unsigned char)(total_g / pixel_count);
85 unsigned char avg_b = (unsigned char)(total_b / pixel_count);
86
87 return gm_rgb(avg_r, avg_g, avg_b);
88}
89
90
91/**
92 * @brief A handle to a GPU-managed image or texture.
93 *
94 * This struct represents an image that has been uploaded to the graphics
95 * hardware for efficient rendering.
96 */
97typedef struct {
98 uint32_t handle; /**< Internal handle for the image */
99 int width; /**< Width of the image in pixels */
100 int height; /**< Height of the image in pixels */
101} gmImage;
102
103#ifndef GM_NO_GAPI
104#include "gapi.h"
105
106/**
107 * @brief Creates a GPU-managed image from a file path.
108 *
109 * This function loads an image file from disk, uploads its data to the GPU,
110 * and then discards the CPU-side copy.
111 *
112 * @param path The file path to the image.
113 * @return A `gmImage` handle.
114 */
115gmImage gm_image_create(const char *path) {
116 gmImage img;
117 gmImageData data;
118 gm_image_data_load(&data, path);
119 img.width = data.width;
120 img.height = data.height;
121 img.handle = gapi_create_image(data.data, img.width, img.height);
122 gm_image_data_free(&data);
123 return img;
124}
125
126/**
127 * @brief Creates a GPU-managed image from in-memory data.
128 *
129 * This function decodes an image from a memory buffer, uploads its data to the
130 * GPU, and then discards the CPU-side copy.
131 *
132 * @param buffer Pointer to the buffer containing the raw image file data.
133 * @param len The length of the buffer in bytes.
134 * @return A `gmImage` handle.
135 */
136gmImage gm_image_create_from_memory(const unsigned char *buffer, int len) {
137 gmImage img;
138 gmImageData data;
139 gm_image_data_load_from_memory(&data, buffer, len);
140 img.width = data.width;
141 img.height = data.height;
142 img.handle = gapi_create_image(data.data, img.width, img.height);
143 gm_image_data_free(&data);
144 return img;
145}
146
147/**
148 * @brief Draws an entire image, centered at the specified position.
149 * @param i The image to draw.
150 * @param x The x-coordinate of the center of the image.
151 * @param y The y-coordinate of the center of the image.
152 * @param w The width to draw the image.
153 * @param h The height to draw the image.
154 */
155void gm_image_draw(gmImage i, double x, double y, double w, double h) {
156 gapi_draw_image(i.handle, x, y, w, h);
157}
158
159/**
160 * @brief Draws a rectangular sub-region of an image.
161 * @param i The source image to draw from.
162 * @param slice_x The x-coordinate of the top-left corner of the sub-region.
163 * @param slice_y The y-coordinate of the top-left corner of the sub-region.
164 * @param slice_width The width of the sub-region.
165 * @param slice_height The height of the sub-region.
166 * @param x The x-coordinate of the center of the destination rectangle.
167 * @param y The y-coordinate of the center of the destination rectangle.
168 * @param w The width to draw the sub-region.
169 * @param h The height to draw the sub-region.
170 */
171void gm_image_draw_part(gmImage i, int slice_x, int slice_y, int slice_width,
172 int slice_height, double x, double y, double w,
173 double h) {
174 gapi_draw_image_part(i.handle, slice_x, slice_y, slice_width, slice_height, x,
175 y, w, h);
176}
177#endif
178
uint32_t gmColor
Type definition for color values, stored as a 32-bit unsigned integer. The color components are packe...
Definition color.h:13
Graphics API (GAPI) abstraction layer for Gama.
int32_t gapi_draw_image(uint32_t handle, double x, double y, double width, double height)
Draws an image referenced by its handle on the screen.
uint32_t gapi_create_image(const unsigned char *data, uint32_t width, uint32_t height)
Creates a platform-specific image handle from raw pixel data.
int32_t gapi_draw_image_part(uint32_t handle, uint32_t slice_x, uint32_t slice_y, uint32_t slice_width, uint32_t slice_height, double x, double y, double width, double height)
Draws a part of an image referenced by its handle on the screen.
int32_t gm_image_data_load_from_memory(gmImageData *data, const unsigned char *buffer, int len)
Loads image data from an in-memory buffer.
Definition image.h:42
gmColor gm_image_data_average_color(const gmImageData *data)
Calculates the average color of an image's raw pixel data.
Definition image.h:67
int32_t gm_image_data_free(gmImageData *d)
Frees the pixel data buffer of a gmImageData struct.
Definition image.h:55
int32_t gm_image_data_load(gmImageData *data, const char *path)
Loads image file from disk into a gmImageData struct.
Definition image.h:29
gmImage gm_image_create_from_memory(const unsigned char *buffer, int len)
Creates a GPU-managed image from in-memory data.
Definition image.h:136
void gm_image_draw_part(gmImage i, int slice_x, int slice_y, int slice_width, int slice_height, double x, double y, double w, double h)
Draws a rectangular sub-region of an image.
Definition image.h:171
gmImage gm_image_create(const char *path)
Creates a GPU-managed image from a file path.
Definition image.h:115
void gm_image_draw(gmImage i, double x, double y, double w, double h)
Draws an entire image, centered at the specified position.
Definition image.h:155
void free(void *ptr)
Custom implementation of free for memory allocated by malloc (this custom version).
Definition malloc.h:189
A container for raw, CPU-side image pixel data.
Definition image.h:18
int32_t width
Definition image.h:19
int32_t height
Definition image.h:19
unsigned char * data
Definition image.h:20
A handle to a GPU-managed image or texture.
Definition image.h:97
uint32_t handle
Definition image.h:98
int width
Definition image.h:99
int height
Definition image.h:100