initial commit of refactor

This commit is contained in:
XANTRONIX Development 2021-11-28 14:38:35 -05:00
parent d5158d2738
commit a9c9ef12c9
2 changed files with 92 additions and 79 deletions

View file

@ -62,20 +62,6 @@ void cammy_image_dither(uint8_t *dest,
int depth, int depth,
cammy_tile_palette *palette); cammy_tile_palette *palette);
void cammy_image_dither_to_tile(cammy_tile *dest,
uint8_t *src,
size_t x_dest,
size_t y_dest,
size_t x_src,
size_t y_src,
size_t width,
size_t height,
size_t dest_width,
size_t dest_height,
size_t src_width,
size_t src_height,
int depth);
void cammy_image_split_to_tiles(cammy_tile *destr, void cammy_image_split_to_tiles(cammy_tile *destr,
cammy_tile *destg, cammy_tile *destg,
cammy_tile *destb, cammy_tile *destb,

View file

@ -144,22 +144,27 @@ static inline void buf_write(uint8_t *buf,
buf[depth*stride*y + depth*x + 2] = b; buf[depth*stride*y + depth*x + 2] = b;
} }
cammy_image *cammy_image_new(cammy_image_format format, static inline size_t size_2bpp_tile(size_t width, size_t height) {
size_t width,
size_t height) {
cammy_image *image;
size_t size;
switch (format) {
case CAMMY_IMAGE_2BPP_TILE: {
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++;
size = CAMMY_TILE_SIZE * tiles_width * tiles_height; return CAMMY_TILE_SIZE * tiles_width * tiles_height;
}
cammy_image *cammy_image_new(cammy_image_format format,
size_t width,
size_t height) {
cammy_image *image;
uint8_t *buf;
size_t size;
switch (format) {
case CAMMY_IMAGE_2BPP_TILE: {
size = size_2bpp_tile(width, height);
break; break;
} }
@ -183,10 +188,11 @@ cammy_image *cammy_image_new(cammy_image_format format,
goto error_malloc_image; goto error_malloc_image;
} }
if ((image->buf = malloc(size)) == NULL) { if ((buf = malloc(size)) == NULL) {
goto error_malloc_buf; goto error_malloc_buf;
} }
image->buf = buf;
image->format = format; image->format = format;
image->size = size; image->size = size;
image->width = width; image->width = width;
@ -351,43 +357,6 @@ void cammy_image_copy_from_tile(uint8_t *dest,
} }
} }
void cammy_image_dither_to_tile(cammy_tile *dest,
uint8_t *src,
size_t x_dest,
size_t y_dest,
size_t x_src,
size_t y_src,
size_t width,
size_t height,
size_t dest_width,
size_t dest_height,
size_t src_width,
size_t src_height,
int depth) {
size_t x_offset, y_offset;
for (y_offset=0; y_offset<height; y_offset++) {
if (y_dest + y_offset > dest_height) {
break;
}
for (x_offset=0; x_offset<width; x_offset++) {
uint8_t r, g, b;
if (x_dest + x_offset > dest_width) {
break;
}
buf_read(src, x_src + x_offset, y_src + y_offset, src_width,
&r, &g, &b, depth);
tile_write(dest, x_dest + x_offset, y_dest + y_offset,
dest_width, rgb_to_tile_2bpp(r, g, b, x_dest + x_offset,
y_dest + y_offset) ^ 3);
}
}
}
void cammy_image_merge_tiles(uint8_t *dest, void cammy_image_merge_tiles(uint8_t *dest,
cammy_tile *srcr, cammy_tile *srcr,
cammy_tile *srcg, cammy_tile *srcg,
@ -439,6 +408,41 @@ static void copy_2bpp(cammy_image *dest,
} }
} }
static void copy_to_2bpp_tile(cammy_image *dest,
cammy_image *src,
cammy_image_point *to,
cammy_image_region *from,
int depth) {
size_t x_offset, y_offset;
for (y_offset=0; y_offset < from->height; y_offset++) {
if (to->y + y_offset > dest->height) {
break;
}
for (x_offset=0; x_offset < from->width; x_offset++) {
uint8_t r, g, b;
if (to->x + x_offset > from->width) {
break;
}
buf_read(src->buf,
from->x + x_offset,
from->y + y_offset,
from->width,
&r, &g, &b, depth);
tile_write(dest->tiles,
to->x + x_offset,
to->y + y_offset,
dest->width,
rgb_to_tile_2bpp(r, g, b, to->x + x_offset,
to->y + y_offset) ^ 3);
}
}
}
void cammy_image_copy(cammy_image *dest, void cammy_image_copy(cammy_image *dest,
cammy_image *src, cammy_image *src,
cammy_image_point *to, cammy_image_point *to,
@ -451,6 +455,23 @@ void cammy_image_copy(cammy_image *dest,
default: default:
break; break;
} }
return;
}
if (dest->format == CAMMY_IMAGE_2BPP_TILE) {
switch (src->format) {
case CAMMY_IMAGE_24BPP_RGB:
copy_to_2bpp_tile(dest, src, to, from, 3);
break;
case CAMMY_IMAGE_32BPP_RGBA:
copy_to_2bpp_tile(dest, src, to, from, 4);
break;
default:
break;
}
} }
} }
@ -499,42 +520,48 @@ int cammy_image_slice(uint8_t *buf,
size_t x_pad, size_t x_pad,
size_t y_pad, size_t y_pad,
int depth) { int depth) {
cammy_tile *tiles; cammy_image src = {
.format = CAMMY_IMAGE_2BPP_TILE,
.size = size_2bpp_tile(width, height),
.width = width,
.height = height,
.buf = buf
}, *dest;
int file = 0; int file = 0;
size_t x, y; size_t x, y;
if ((tiles = malloc(CAMMY_SCREEN_SIZE)) == NULL) { if ((dest = cammy_image_new(CAMMY_IMAGE_2BPP_TILE, width, height)) == NULL) {
goto error_malloc_tiles; goto error_image_new;
} }
for (x=0; x<width; x+=CAMMY_SCREEN_WIDTH + (2 * x_pad)) { for (x=0; x<width; x+=CAMMY_SCREEN_WIDTH + (2 * x_pad)) {
for (y=0; y<height; y+=CAMMY_SCREEN_HEIGHT + y_pad) { for (y=0; y<height; y+=CAMMY_SCREEN_HEIGHT + y_pad) {
cammy_image_dither_to_tile(tiles, buf, cammy_image_point to = {
0, 0, 0, 0
x + x_pad, y, };
CAMMY_SCREEN_WIDTH,
CAMMY_SCREEN_HEIGHT,
CAMMY_SCREEN_WIDTH,
CAMMY_SCREEN_HEIGHT,
width,
height,
depth);
if (save_tile_to_file(tiles, CAMMY_SCREEN_WIDTH, cammy_image_region from = {
x + x_pad, y, CAMMY_SCREEN_WIDTH, CAMMY_SCREEN_HEIGHT
};
cammy_image_copy(dest, &src, &to, &from);
if (save_tile_to_file(dest->tiles, CAMMY_SCREEN_WIDTH,
CAMMY_SCREEN_HEIGHT, file++) < 0) { CAMMY_SCREEN_HEIGHT, file++) < 0) {
goto error_save_tile_to_file; goto error_save_tile_to_file;
} }
} }
} }
free(tiles); cammy_image_destroy(dest);
return 0; return 0;
error_save_tile_to_file: error_save_tile_to_file:
free(tiles); cammy_image_destroy(dest);
error_malloc_tiles: error_image_new:
return 1; return -1;
} }