This is working out pretty well now
This commit is contained in:
parent
05fd969619
commit
e93fc3aad8
4 changed files with 9669 additions and 86 deletions
|
@ -4,7 +4,7 @@ CC = $(CROSS)cc
|
||||||
|
|
||||||
INCLUDE_PATH = ../include
|
INCLUDE_PATH = ../include
|
||||||
|
|
||||||
CFLAGS += -I$(INCLUDE_PATH)
|
CFLAGS += -fPIC -Wall -O2 -I$(INCLUDE_PATH)
|
||||||
LDFLAGS =
|
LDFLAGS =
|
||||||
|
|
||||||
CAMMY = cammy
|
CAMMY = cammy
|
||||||
|
@ -12,7 +12,7 @@ CAMMY_OBJS = main.o export.o import.o dither.o
|
||||||
|
|
||||||
CAMMY_HEADERS = commands.h
|
CAMMY_HEADERS = commands.h
|
||||||
|
|
||||||
CAMMY_LDFLAGS = ../src/libcammy.a -lz
|
CAMMY_LDFLAGS = ../src/libcammy.a -lz -lm
|
||||||
|
|
||||||
PROGRAMS = cammy
|
PROGRAMS = cammy
|
||||||
|
|
||||||
|
|
130
src/image.c
130
src/image.c
|
@ -10,7 +10,11 @@
|
||||||
#include <cammy/screen.h>
|
#include <cammy/screen.h>
|
||||||
#include <cammy/image.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] = {
|
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) {
|
static inline size_t size_2bpp_tile(size_t width, size_t height) {
|
||||||
size_t tiles_width = width >> 3,
|
size_t tiles_width = width >> 3,
|
||||||
tiles_height = height >> 3;
|
tiles_height = height >> 3;
|
||||||
|
|
||||||
if (width & 7) tiles_width++;
|
if (width & 7) tiles_width++;
|
||||||
if (height & 7) tiles_height++;
|
if (height & 7) tiles_height++;
|
||||||
|
@ -166,14 +170,16 @@ int cammy_image_init(cammy_image *image,
|
||||||
if ((image->buf = malloc(size)) == NULL) {
|
if ((image->buf = malloc(size)) == NULL) {
|
||||||
goto error_malloc_buf;
|
goto error_malloc_buf;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
memset(image->buf, '\0', size);
|
memset(image->buf, '\xff', size);
|
||||||
|
}
|
||||||
|
|
||||||
if (palette == NULL) {
|
if (palette == NULL) {
|
||||||
palette = default_palette;
|
palette = default_palette;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
memcpy(&image->palette, palette, sizeof(image->palette));
|
||||||
|
|
||||||
image->format = format;
|
image->format = format;
|
||||||
image->size = size;
|
image->size = size;
|
||||||
image->width = width;
|
image->width = width;
|
||||||
|
@ -269,18 +275,17 @@ error_open:
|
||||||
}
|
}
|
||||||
|
|
||||||
static cammy_image *load_png(const char *file) {
|
static cammy_image *load_png(const char *file) {
|
||||||
png_t png;
|
uint8_t *buf;
|
||||||
|
|
||||||
cammy_image *image;
|
cammy_image *image;
|
||||||
cammy_image_format format = CAMMY_IMAGE_NONE;
|
cammy_image_format format = CAMMY_IMAGE_NONE;
|
||||||
|
|
||||||
png_init(malloc, free);
|
int width, height, channels;
|
||||||
|
|
||||||
if (png_open_file_read(&png, file) < 0) {
|
if ((buf = stbi_load(file, &width, &height, &channels, 4)) == NULL) {
|
||||||
goto error_png_open_file_read;
|
goto error_stbi_load;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (png.bpp) {
|
switch (channels) {
|
||||||
case 3:
|
case 3:
|
||||||
format = CAMMY_IMAGE_24BPP_RGB;
|
format = CAMMY_IMAGE_24BPP_RGB;
|
||||||
break;
|
break;
|
||||||
|
@ -294,35 +299,24 @@ static cammy_image *load_png(const char *file) {
|
||||||
goto error_invalid_format;
|
goto error_invalid_format;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((image = cammy_image_new(format,
|
if ((image = malloc(sizeof(*image))) == NULL) {
|
||||||
NULL,
|
goto error_malloc_image;
|
||||||
png.width,
|
|
||||||
png.height)) == NULL) {
|
|
||||||
goto error_image_new;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Got buf %p size %zu\n", image->buf, image->size);
|
if (cammy_image_init(image, format, default_palette, width, height, buf) < 0) {
|
||||||
|
goto error_image_init;
|
||||||
if (png_get_data(&png, image->buf) < 0) {
|
|
||||||
goto error_png_get_data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Still here\n");
|
|
||||||
|
|
||||||
png_close_file(&png);
|
|
||||||
|
|
||||||
printf("Hmmst\n");
|
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
|
|
||||||
error_png_get_data:
|
error_image_init:
|
||||||
cammy_image_destroy(image);
|
free(image);
|
||||||
|
|
||||||
error_image_new:
|
|
||||||
error_invalid_format:
|
error_invalid_format:
|
||||||
png_close_file(&png);
|
error_malloc_image:
|
||||||
|
free(buf);
|
||||||
|
|
||||||
error_png_open_file_read:
|
error_stbi_load:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -356,11 +350,11 @@ cammy_image *cammy_image_open(const char *file) {
|
||||||
|
|
||||||
return loader(file);
|
return loader(file);
|
||||||
|
|
||||||
error_invalid_format:
|
|
||||||
error_read:
|
error_read:
|
||||||
error_lseek:
|
error_lseek:
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
|
error_invalid_format:
|
||||||
error_open:
|
error_open:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -412,60 +406,34 @@ error_open:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void format_info_png(cammy_image_format format,
|
static inline int format_channels(cammy_image_format format) {
|
||||||
int *depth,
|
|
||||||
int *color) {
|
|
||||||
switch (format) {
|
switch (format) {
|
||||||
case CAMMY_IMAGE_24BPP_RGB:
|
case CAMMY_IMAGE_24BPP_RGB:
|
||||||
if (depth) *depth = 3;
|
return 3;
|
||||||
if (color) *color = PNG_TRUECOLOR;
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case CAMMY_IMAGE_32BPP_RGBA:
|
case CAMMY_IMAGE_32BPP_RGBA:
|
||||||
if (depth) *depth = 4;
|
return 4;
|
||||||
if (color) *color = PNG_TRUECOLOR_ALPHA;
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if (depth) *depth = 0;
|
return 0;
|
||||||
if (color) *color = 0;
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int save_png(cammy_image *image, const char *file) {
|
static int save_png(cammy_image *image, const char *file) {
|
||||||
png_t png;
|
int channels = format_channels(image->format);
|
||||||
|
|
||||||
int depth, color;
|
if (!stbi_write_png(file,
|
||||||
|
image->width,
|
||||||
format_info_png(image->format, &depth, &color);
|
image->height,
|
||||||
|
channels,
|
||||||
png_init(malloc, free);
|
image->buf,
|
||||||
|
channels * image->width)) {
|
||||||
if (png_open_file_write(&png, file) < 0) {
|
goto error_stbi_write_png;
|
||||||
goto error_png_open_file_write;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
return 0;
|
||||||
|
|
||||||
error_png_set_data:
|
error_stbi_write_png:
|
||||||
png_close_file(&png);
|
|
||||||
|
|
||||||
error_png_open_file_write:
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -555,9 +523,7 @@ static void copy_buf(cammy_image *dest,
|
||||||
cammy_image_region *from) {
|
cammy_image_region *from) {
|
||||||
size_t x, y;
|
size_t x, y;
|
||||||
|
|
||||||
int depth, color;
|
int channels = format_channels(dest->format);
|
||||||
|
|
||||||
format_info_png(dest->format, &depth, &color);
|
|
||||||
|
|
||||||
for (y=0; y < from->height; y++) {
|
for (y=0; y < from->height; y++) {
|
||||||
if (to->y + y > dest->height) {
|
if (to->y + y > dest->height) {
|
||||||
|
@ -575,13 +541,13 @@ static void copy_buf(cammy_image *dest,
|
||||||
from->x + x,
|
from->x + x,
|
||||||
from->y + y,
|
from->y + y,
|
||||||
from->width,
|
from->width,
|
||||||
&r, &g, &b, depth);
|
&r, &g, &b, channels);
|
||||||
|
|
||||||
buf_write(dest->buf,
|
buf_write(dest->buf,
|
||||||
to->x + x,
|
to->x + x,
|
||||||
to->y + y,
|
to->y + y,
|
||||||
dest->width,
|
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) {
|
cammy_image_region *from) {
|
||||||
size_t x, y;
|
size_t x, y;
|
||||||
|
|
||||||
int depth, color;
|
int channels = format_channels(src->format);
|
||||||
|
|
||||||
format_info_png(dest->format, &depth, &color);
|
|
||||||
|
|
||||||
for (y=0; y < from->height; y++) {
|
for (y=0; y < from->height; y++) {
|
||||||
if (to->y + y > dest->height) {
|
if (to->y + y > dest->height) {
|
||||||
|
@ -611,14 +575,14 @@ static void copy_rgb_to_2bpp_tile(cammy_image *dest,
|
||||||
from->x + x,
|
from->x + x,
|
||||||
from->y + y,
|
from->y + y,
|
||||||
from->width,
|
from->width,
|
||||||
&r, &g, &b, depth);
|
&r, &g, &b, channels);
|
||||||
|
|
||||||
tile_write((cammy_tile *)dest->buf,
|
tile_write((cammy_tile *)dest->buf,
|
||||||
to->x + x,
|
to->x + x,
|
||||||
to->y + y,
|
to->y + y,
|
||||||
dest->width,
|
dest->width,
|
||||||
rgb_to_tile_2bpp(r, g, b, to->x + x,
|
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) {
|
cammy_image_region *from) {
|
||||||
size_t x, y;
|
size_t x, y;
|
||||||
|
|
||||||
int depth, color;
|
int channels = format_channels(dest->format);
|
||||||
|
|
||||||
format_info_png(dest->format, &depth, &color);
|
|
||||||
|
|
||||||
for (y=0; y < from->height; y++) {
|
for (y=0; y < from->height; y++) {
|
||||||
if (to->y + y > dest->height) {
|
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].r,
|
||||||
src->palette[c].g,
|
src->palette[c].g,
|
||||||
src->palette[c].b,
|
src->palette[c].b,
|
||||||
depth);
|
channels);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
7897
src/stb_image.h
Normal file
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
1724
src/stb_image_write.h
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue