initial commit
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I6a6a6964ee9e6ebe436ca8328c6e4a7ec7c9d8d4
This commit is contained in:
commit
fcc080871a
11 changed files with 12536 additions and 0 deletions
268
src/image.c
Normal file
268
src/image.c
Normal file
|
|
@ -0,0 +1,268 @@
|
|||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "../include/stb_image.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "../include/chroma.h"
|
||||
|
||||
// Check if file exists and is readable
|
||||
static int file_exists(const char *path) {
|
||||
struct stat st;
|
||||
return (stat(path, &st) == 0 && S_ISREG(st.st_mode));
|
||||
}
|
||||
|
||||
// Get file size
|
||||
static long get_file_size(const char *path) {
|
||||
struct stat st;
|
||||
if (stat(path, &st) != 0) {
|
||||
return -1;
|
||||
}
|
||||
return st.st_size;
|
||||
}
|
||||
|
||||
// Load image from file
|
||||
int chroma_image_load(chroma_image_t *image, const char *path) {
|
||||
if (!image || !path) {
|
||||
chroma_log("ERROR", "Invalid parameters for image loading");
|
||||
return CHROMA_ERROR_INIT;
|
||||
}
|
||||
|
||||
// Initialize image structure
|
||||
memset(image, 0, sizeof(chroma_image_t));
|
||||
strncpy(image->path, path, MAX_PATH_LEN - 1);
|
||||
image->path[MAX_PATH_LEN - 1] = '\0';
|
||||
|
||||
// Check if file exists
|
||||
if (!file_exists(path)) {
|
||||
chroma_log("ERROR", "Image file does not exist: %s", path);
|
||||
return CHROMA_ERROR_IMAGE;
|
||||
}
|
||||
|
||||
// Get file size for logging
|
||||
long file_size = get_file_size(path);
|
||||
if (file_size > 0) {
|
||||
chroma_log("DEBUG", "Loading image: %s (%.2f MB)", path,
|
||||
(double)file_size / (1024.0 * 1024.0));
|
||||
}
|
||||
|
||||
// Load image data using stb_image
|
||||
stbi_set_flip_vertically_on_load(0); // Keep images right-side up
|
||||
|
||||
image->data =
|
||||
stbi_load(path, &image->width, &image->height, &image->channels, 0);
|
||||
if (!image->data) {
|
||||
chroma_log("ERROR", "Failed to load image %s: %s", path,
|
||||
stbi_failure_reason());
|
||||
return CHROMA_ERROR_IMAGE;
|
||||
}
|
||||
|
||||
// Validate image dimensions
|
||||
if (image->width <= 0 || image->height <= 0) {
|
||||
chroma_log("ERROR", "Invalid image dimensions: %dx%d", image->width,
|
||||
image->height);
|
||||
chroma_image_free(image);
|
||||
return CHROMA_ERROR_IMAGE;
|
||||
}
|
||||
|
||||
// Check supported formats
|
||||
if (image->channels < 3 || image->channels > 4) {
|
||||
chroma_log("ERROR",
|
||||
"Unsupported image format: %d channels (need RGB or RGBA)",
|
||||
image->channels);
|
||||
chroma_image_free(image);
|
||||
return CHROMA_ERROR_IMAGE;
|
||||
}
|
||||
|
||||
// Convert RGB to RGBA if necessary for consistent handling
|
||||
if (image->channels == 3) {
|
||||
int pixel_count = image->width * image->height;
|
||||
unsigned char *rgba_data = malloc(pixel_count * 4);
|
||||
if (!rgba_data) {
|
||||
chroma_log("ERROR", "Failed to allocate memory for RGBA conversion");
|
||||
chroma_image_free(image);
|
||||
return CHROMA_ERROR_MEMORY;
|
||||
}
|
||||
|
||||
// Convert RGB to RGBA
|
||||
for (int i = 0; i < pixel_count; i++) {
|
||||
rgba_data[i * 4 + 0] = image->data[i * 3 + 0]; // R
|
||||
rgba_data[i * 4 + 1] = image->data[i * 3 + 1]; // G
|
||||
rgba_data[i * 4 + 2] = image->data[i * 3 + 2]; // B
|
||||
rgba_data[i * 4 + 3] = 255; // A
|
||||
}
|
||||
|
||||
// Replace original data
|
||||
stbi_image_free(image->data);
|
||||
image->data = rgba_data;
|
||||
image->channels = 4;
|
||||
}
|
||||
|
||||
image->loaded = true;
|
||||
|
||||
chroma_log("INFO", "Loaded image: %s (%dx%d, %d channels, %.2f MB)", path,
|
||||
image->width, image->height, image->channels,
|
||||
(double)(image->width * image->height * image->channels) /
|
||||
(1024.0 * 1024.0));
|
||||
|
||||
return CHROMA_OK;
|
||||
}
|
||||
|
||||
// Free image data
|
||||
void chroma_image_free(chroma_image_t *image) {
|
||||
if (!image) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (image->data) {
|
||||
if (image->channels == 4 && strlen(image->path) > 0) {
|
||||
// If we converted from RGB to RGBA, use regular free()
|
||||
free(image->data);
|
||||
} else {
|
||||
// If loaded directly by stb_image, use stbi_image_free()
|
||||
stbi_image_free(image->data);
|
||||
}
|
||||
image->data = NULL;
|
||||
}
|
||||
|
||||
image->width = 0;
|
||||
image->height = 0;
|
||||
image->channels = 0;
|
||||
image->loaded = false;
|
||||
|
||||
if (strlen(image->path) > 0) {
|
||||
chroma_log("DEBUG", "Freed image: %s", image->path);
|
||||
}
|
||||
|
||||
memset(image->path, 0, sizeof(image->path));
|
||||
}
|
||||
|
||||
// Find image by path in state
|
||||
chroma_image_t *chroma_image_find_by_path(chroma_state_t *state,
|
||||
const char *path) {
|
||||
if (!state || !path) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (int i = 0; i < state->image_count; i++) {
|
||||
if (strcmp(state->images[i].path, path) == 0) {
|
||||
return &state->images[i];
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Load image if not already loaded
|
||||
chroma_image_t *chroma_image_get_or_load(chroma_state_t *state,
|
||||
const char *path) {
|
||||
if (!state || !path) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check if already loaded
|
||||
chroma_image_t *existing = chroma_image_find_by_path(state, path);
|
||||
if (existing && existing->loaded) {
|
||||
chroma_log("DEBUG", "Using cached image: %s", path);
|
||||
return existing;
|
||||
}
|
||||
|
||||
// Find empty slot or reuse existing
|
||||
chroma_image_t *image = existing;
|
||||
if (!image) {
|
||||
if (state->image_count >= MAX_OUTPUTS) {
|
||||
chroma_log("ERROR", "Maximum number of images reached");
|
||||
return NULL;
|
||||
}
|
||||
image = &state->images[state->image_count];
|
||||
state->image_count++;
|
||||
}
|
||||
|
||||
// Load the image
|
||||
if (chroma_image_load(image, path) != CHROMA_OK) {
|
||||
// If this was a new slot, decrement count
|
||||
if (!existing) {
|
||||
state->image_count--;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
// Validate image file format
|
||||
int chroma_image_validate(const char *path) {
|
||||
if (!path || !file_exists(path)) {
|
||||
return CHROMA_ERROR_IMAGE;
|
||||
}
|
||||
|
||||
// Check file extension (basic validation)
|
||||
const char *ext = strrchr(path, '.');
|
||||
if (!ext) {
|
||||
return CHROMA_ERROR_IMAGE;
|
||||
}
|
||||
|
||||
ext++; // Skip the dot
|
||||
|
||||
// Check supported extensions
|
||||
if (strcasecmp(ext, "jpg") == 0 || strcasecmp(ext, "jpeg") == 0 ||
|
||||
strcasecmp(ext, "png") == 0 || strcasecmp(ext, "bmp") == 0 ||
|
||||
strcasecmp(ext, "tga") == 0 || strcasecmp(ext, "psd") == 0 ||
|
||||
strcasecmp(ext, "gif") == 0 || strcasecmp(ext, "hdr") == 0 ||
|
||||
strcasecmp(ext, "pic") == 0 || strcasecmp(ext, "ppm") == 0 ||
|
||||
strcasecmp(ext, "pgm") == 0) {
|
||||
return CHROMA_OK;
|
||||
}
|
||||
|
||||
chroma_log("WARN", "Potentially unsupported image format: %s", ext);
|
||||
return CHROMA_ERROR_IMAGE;
|
||||
}
|
||||
|
||||
// Get image info without loading full data
|
||||
int chroma_image_get_info(const char *path, int *width, int *height,
|
||||
int *channels) {
|
||||
if (!path || !width || !height || !channels) {
|
||||
return CHROMA_ERROR_INIT;
|
||||
}
|
||||
|
||||
if (!file_exists(path)) {
|
||||
return CHROMA_ERROR_IMAGE;
|
||||
}
|
||||
|
||||
if (!stbi_info(path, width, height, channels)) {
|
||||
chroma_log("ERROR", "Failed to get image info for %s: %s", path,
|
||||
stbi_failure_reason());
|
||||
return CHROMA_ERROR_IMAGE;
|
||||
}
|
||||
|
||||
return CHROMA_OK;
|
||||
}
|
||||
|
||||
// Cleanup all images in state
|
||||
void chroma_images_cleanup(chroma_state_t *state) {
|
||||
if (!state) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < state->image_count; i++) {
|
||||
chroma_image_free(&state->images[i]);
|
||||
}
|
||||
|
||||
state->image_count = 0;
|
||||
chroma_log("INFO", "Cleaned up all images");
|
||||
}
|
||||
|
||||
// Preload common image formats for validation
|
||||
void chroma_image_init_stb(void) {
|
||||
// Set stb_image options
|
||||
stbi_set_flip_vertically_on_load(0);
|
||||
|
||||
// These could be made configurable
|
||||
stbi_ldr_to_hdr_gamma(2.2f);
|
||||
stbi_ldr_to_hdr_scale(1.0f);
|
||||
|
||||
chroma_log("DEBUG", "Initialized stb_image library");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue