home | blog | art | now | git gpg | email | rss

morph

Generate xresources colors from an image.
git clone https://pollux.codes/git/morph.git
Log | Files | Refs | README | LICENSE
commit 332e7e3c605acffc04c48da0befab503eb908df1
parent b69e5e904d1499a13d7062b481d311b808592fed
Author: Pollux <pollux@pollux.codes>
Date:   Fri, 18 Jul 2025 19:43:24 -0500

feat: Add image loading functionality

Signed-off-by: Pollux <pollux@pollux.codes>

Diffstat:
Mmorph.c | 44++++++++++++++++++++++++++++++++++++++------
1 file changed, 38 insertions(+), 6 deletions(-)

diff --git a/morph.c b/morph.c @@ -2,7 +2,8 @@ /* See LICENSE file for copyright and license details. */ #include <math.h> -#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> #include <Imlib2.h> @@ -86,12 +87,43 @@ lab_to_rgb(col_lab_t lab) { return rgb; } +/// Loads the image located at the given path and extracts the colors in the +/// pixels into an array of col_rgb_t. The caller is responsible for freeing +/// the return value. +col_rgb_t * +get_image_pixel_data(const char *path) { + + int image_width, image_height; + col_rgb_t *image_pixels; + + Imlib_Image image = imlib_load_image(path); // TODO: Make sure image loaded + + imlib_context_set_image(image); + + image_width = imlib_image_get_width(); + image_height = imlib_image_get_height(); + + image_pixels = malloc(image_width * image_height * sizeof(col_rgb_t)); + + DATA32 *image_data = imlib_image_get_data_for_reading_only(); + + for(int pixel = 0; pixel < image_width * image_height; pixel++) { + DATA32 color = *(image_data + pixel); + + image_pixels[pixel].r = (color & 0x000000ff); + image_pixels[pixel].g = (color & 0x0000ff00) >> 8; + image_pixels[pixel].b = (color & 0x00ff0000) >> 16; + } + + imlib_free_image(); + + return image_pixels; +} + int main(int argc, char **argv) { - col_rgb_t test_color = {.r = 1.0f,.g = 1.0f,.b = 1.0f }; - col_lab_t lab_color = rgb_to_lab(test_color); - col_rgb_t out_color = lab_to_rgb(lab_color); - printf("Output color: R=%f, G=%f, B=%f\n", out_color.r, out_color.g, - out_color.b); + col_rgb_t *image_pixels = get_image_pixel_data("test.jpg"); + + free(image_pixels); }