This is working out pretty well now

This commit is contained in:
XANTRONIX Development 2021-11-30 19:56:30 -05:00
parent 05fd969619
commit e93fc3aad8
4 changed files with 9669 additions and 86 deletions

View file

@ -4,7 +4,7 @@ CC = $(CROSS)cc
INCLUDE_PATH = ../include
CFLAGS += -I$(INCLUDE_PATH)
CFLAGS += -fPIC -Wall -O2 -I$(INCLUDE_PATH)
LDFLAGS =
CAMMY = cammy
@ -12,7 +12,7 @@ CAMMY_OBJS = main.o export.o import.o dither.o
CAMMY_HEADERS = commands.h
CAMMY_LDFLAGS = ../src/libcammy.a -lz
CAMMY_LDFLAGS = ../src/libcammy.a -lz -lm
PROGRAMS = cammy

View file

@ -10,7 +10,11 @@
#include <cammy/screen.h>
#include <cammy/image.h>
#include "pnglite.h"
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image.h"
#include "stb_image_write.h"
static uint32_t bayer_matrix[256] = {
/*
@ -120,7 +124,7 @@ static inline void buf_write(uint8_t *buf,
static inline size_t size_2bpp_tile(size_t width, size_t height) {
size_t tiles_width = width >> 3,
tiles_height = height >> 3;
tiles_height = height >> 3;
if (width & 7) tiles_width++;
if (height & 7) tiles_height++;
@ -166,14 +170,16 @@ int cammy_image_init(cammy_image *image,
if ((image->buf = malloc(size)) == NULL) {
goto error_malloc_buf;
}
}
memset(image->buf, '\0', size);
memset(image->buf, '\xff', size);
}
if (palette == NULL) {
palette = default_palette;
}
memcpy(&image->palette, palette, sizeof(image->palette));
image->format = format;
image->size = size;
image->width = width;
@ -269,18 +275,17 @@ error_open:
}
static cammy_image *load_png(const char *file) {
png_t png;
uint8_t *buf;
cammy_image *image;
cammy_image_format format = CAMMY_IMAGE_NONE;
png_init(malloc, free);
int width, height, channels;
if (png_open_file_read(&png, file) < 0) {
goto error_png_open_file_read;
if ((buf = stbi_load(file, &width, &height, &channels, 4)) == NULL) {
goto error_stbi_load;
}
switch (png.bpp) {
switch (channels) {
case 3:
format = CAMMY_IMAGE_24BPP_RGB;
break;
@ -294,35 +299,24 @@ static cammy_image *load_png(const char *file) {
goto error_invalid_format;
}
if ((image = cammy_image_new(format,
NULL,
png.width,
png.height)) == NULL) {
goto error_image_new;
if ((image = malloc(sizeof(*image))) == NULL) {
goto error_malloc_image;
}
printf("Got buf %p size %zu\n", image->buf, image->size);
if (png_get_data(&png, image->buf) < 0) {
goto error_png_get_data;
if (cammy_image_init(image, format, default_palette, width, height, buf) < 0) {
goto error_image_init;
}
printf("Still here\n");
png_close_file(&png);
printf("Hmmst\n");
return image;
error_png_get_data:
cammy_image_destroy(image);
error_image_init:
free(image);
error_image_new:
error_invalid_format:
png_close_file(&png);
error_malloc_image:
free(buf);
error_png_open_file_read:
error_stbi_load:
return NULL;
}
@ -356,11 +350,11 @@ cammy_image *cammy_image_open(const char *file) {
return loader(file);
error_invalid_format:
error_read:
error_lseek:
close(fd);
error_invalid_format:
error_open:
return NULL;
}
@ -412,60 +406,34 @@ error_open:
return -1;
}
static inline void format_info_png(cammy_image_format format,
int *depth,
int *color) {
static inline int format_channels(cammy_image_format format) {
switch (format) {
case CAMMY_IMAGE_24BPP_RGB:
if (depth) *depth = 3;
if (color) *color = PNG_TRUECOLOR;
break;
return 3;
case CAMMY_IMAGE_32BPP_RGBA:
if (depth) *depth = 4;
if (color) *color = PNG_TRUECOLOR_ALPHA;
break;
return 4;
default:
if (depth) *depth = 0;
if (color) *color = 0;
break;
return 0;
}
}
static int save_png(cammy_image *image, const char *file) {
png_t png;
int channels = format_channels(image->format);
int depth, color;
format_info_png(image->format, &depth, &color);
png_init(malloc, free);
if (png_open_file_write(&png, file) < 0) {
goto error_png_open_file_write;
if (!stbi_write_png(file,
image->width,
image->height,
channels,
image->buf,
channels * image->width)) {
goto error_stbi_write_png;
}
if (png_set_data(&png,
image->width,
image->height,
depth,
color,
image->buf) < 0) {
goto error_png_set_data;
}
png_close_file(&png);
return 0;
error_png_set_data:
png_close_file(&png);
error_png_open_file_write:
error_stbi_write_png:
return -1;
}
@ -555,9 +523,7 @@ static void copy_buf(cammy_image *dest,
cammy_image_region *from) {
size_t x, y;
int depth, color;
format_info_png(dest->format, &depth, &color);
int channels = format_channels(dest->format);
for (y=0; y < from->height; y++) {
if (to->y + y > dest->height) {
@ -575,13 +541,13 @@ static void copy_buf(cammy_image *dest,
from->x + x,
from->y + y,
from->width,
&r, &g, &b, depth);
&r, &g, &b, channels);
buf_write(dest->buf,
to->x + x,
to->y + y,
dest->width,
r, g, b, depth);
r, g, b, channels);
}
}
}
@ -591,9 +557,7 @@ static void copy_rgb_to_2bpp_tile(cammy_image *dest,
cammy_image_region *from) {
size_t x, y;
int depth, color;
format_info_png(dest->format, &depth, &color);
int channels = format_channels(src->format);
for (y=0; y < from->height; y++) {
if (to->y + y > dest->height) {
@ -611,14 +575,14 @@ static void copy_rgb_to_2bpp_tile(cammy_image *dest,
from->x + x,
from->y + y,
from->width,
&r, &g, &b, depth);
&r, &g, &b, channels);
tile_write((cammy_tile *)dest->buf,
to->x + x,
to->y + y,
dest->width,
rgb_to_tile_2bpp(r, g, b, to->x + x,
to->y + y) ^ 3);
to->y + y));
}
}
}
@ -629,9 +593,7 @@ static void copy_2bpp_tile_to_rgb(cammy_image *dest,
cammy_image_region *from) {
size_t x, y;
int depth, color;
format_info_png(dest->format, &depth, &color);
int channels = format_channels(dest->format);
for (y=0; y < from->height; y++) {
if (to->y + y > dest->height) {
@ -657,7 +619,7 @@ static void copy_2bpp_tile_to_rgb(cammy_image *dest,
src->palette[c].r,
src->palette[c].g,
src->palette[c].b,
depth);
channels);
}
}
}

7897
src/stb_image.h Normal file

File diff suppressed because it is too large Load diff

1724
src/stb_image_write.h Normal file

File diff suppressed because it is too large Load diff