From 2104893b805fb5a4c0717a64fc48c097f4d3d367 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 5 Dec 2021 13:30:27 -0500 Subject: [PATCH] bad drone >:( --- src/image.c | 194 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 133 insertions(+), 61 deletions(-) diff --git a/src/image.c b/src/image.c index a8092ba..b136918 100644 --- a/src/image.c +++ b/src/image.c @@ -105,36 +105,48 @@ static uint8_t rgb_to_2bpp(uint8_t r, uint8_t g, uint8_t b, size_t x, size_t y) } static inline uint8_t bitmap_read_channel(uint8_t *buf, - size_t x, size_t y, size_t stride, + size_t stride, int channels, - int channel) { + int channel, + size_t x, + size_t y) { return buf[channels*stride*y + channels*x + channel]; } static inline void bitmap_read(uint8_t *buf, - size_t x, size_t y, size_t stride, - uint8_t *r, uint8_t *g, uint8_t *b, - int channels) { - *r = bitmap_read_channel(buf, x, y, stride, channels, 0); - *g = bitmap_read_channel(buf, x, y, stride, channels, 1); - *b = bitmap_read_channel(buf, x, y, stride, channels, 2); + size_t stride, + int channels, + size_t x, + size_t y, + uint8_t *r, + uint8_t *g, + uint8_t *b) { + *r = bitmap_read_channel(buf, stride, channels, 0, x, y); + *g = bitmap_read_channel(buf, stride, channels, 1, x, y); + *b = bitmap_read_channel(buf, stride, channels, 2, x, y); } static inline void bitmap_write_channel(uint8_t *buf, - size_t x, size_t y, size_t stride, + size_t stride, int channels, int channel, + size_t x, + size_t y, uint8_t v) { buf[channels*stride*y + channels*x + channel] = v; } static inline void bitmap_write(uint8_t *buf, - size_t x, size_t y, size_t stride, + size_t stride, int channels, - uint8_t r, uint8_t g, uint8_t b) { - bitmap_write_channel(buf, x, y, stride, channels, 0, r); - bitmap_write_channel(buf, x, y, stride, channels, 1, g); - bitmap_write_channel(buf, x, y, stride, channels, 2, b); + size_t x, + size_t y, + uint8_t r, + uint8_t g, + uint8_t b) { + bitmap_write_channel(buf, stride, channels, 0, x, y, r); + bitmap_write_channel(buf, stride, channels, 1, x, y, g); + bitmap_write_channel(buf, stride, channels, 2, x, y, b); } static inline size_t size_2bpp_tile(size_t width, size_t height) { @@ -477,9 +489,9 @@ int cammy_image_save(cammy_image *image, const char *filename) { } static inline uint8_t tile_read(cammy_tile *tiles, + size_t stride, size_t x, - size_t y, - int stride) { + size_t y) { cammy_tile *tile = CAMMY_TILE_INDEXED(tiles, x, y, stride); int tile_x = x & 7, @@ -491,9 +503,9 @@ static inline uint8_t tile_read(cammy_tile *tiles, } static inline void tile_write(cammy_tile *tiles, + size_t stride, size_t x, size_t y, - int stride, uint8_t value) { cammy_tile *tile = CAMMY_TILE_INDEXED(tiles, x, y, stride); @@ -511,22 +523,36 @@ static void tile_copy(cammy_image *dest, cammy_image *src, cammy_image_point *to, cammy_image_region *from) { - size_t x, - y; + size_t x, y; - for (y=from->y; yy + from->height; y++) { - if (y > dest->height) { + for (y=0; yheight; y++) { + size_t y_src = y + from->y, + y_dest = y + to->y; + + if (y_src >= src->height || y_dest >= dest->height) { break; } - for (x=from->x; xx + from->width; x++) { - uint8_t value = tile_read((cammy_tile *)src->buf, x, y, src->width); + for (x=0; xwidth; x++) { + size_t x_src = x + from->x, + x_dest = x + to->x; - if (to->x + x > dest->width) { + uint8_t v; + + if (x_src >= src->width || x_dest >= dest->width) { break; } - tile_write((cammy_tile *)dest->buf, x, y, dest->width, value); + v = tile_read((cammy_tile *)src->buf, + src->width, + x_src, + y_src); + + tile_write((cammy_tile *)dest->buf, + dest->width, + x_dest, + y_dest, + v); } } } @@ -540,29 +566,36 @@ static void bitmap_copy(cammy_image *dest, int channels_dest = depth_channels(dest->depth), channels_src = depth_channels(src->depth); - for (y=from->y; yy + from->height; y++) { - if (y > dest->height) { + for (y=0; yheight; y++) { + size_t y_src = y + from->y, + y_dest = y + to->y; + + if (y_src >= src->height || y_dest >= dest->height) { break; } - for (x=from->x; xx + from->width; x++) { + for (x=0; xwidth; x++) { + size_t x_src = x + from->x, + x_dest = x + to->x; + uint8_t r, g, b; - if (x > from->width) { + if (x_src >= src->width || x_dest >= dest->width) { break; } bitmap_read(src->buf, - from->x + x, - from->y + y, src->width, - &r, &g, &b, channels_src); + channels_src, + x_src, + y_src, + &r, &g, &b); bitmap_write(dest->buf, - to->x + x, - to->y + y, dest->width, channels_dest, + x_dest, + y_dest, r, g, b); } } @@ -576,28 +609,35 @@ static void bitmap_copy_to_tile(cammy_image *dest, int channels = depth_channels(src->depth); - for (y=from->y; yy + from->height; y++) { - if (y > dest->height) { + for (y=0; yheight; y++) { + size_t y_src = y + from->y, + y_dest = y + to->y; + + if (y_src >= src->height || y_dest >= dest->height) { break; } for (x=from->x; xx + from->width; x++) { uint8_t r, g, b; - if (x > from->width) { + size_t x_src = x + from->x, + x_dest = x + to->x; + + if (x_src >= src->width || x_dest >= dest->width) { break; } bitmap_read(src->buf, - from->x + x, - from->y + y, - from->width, - &r, &g, &b, channels); + src->width, + channels, + x_src, + y_src, + &r, &g, &b); tile_write((cammy_tile *)dest->buf, - to->x + x, - to->y + y, dest->width, + x_dest, + y_dest, rgb_to_2bpp(r, g, b, to->x + x, to->y + y)); } @@ -612,28 +652,37 @@ static void tile_copy_to_bitmap(cammy_image *dest, int channels = depth_channels(dest->depth); - for (y=from->y; yy + from->height; y++) { - if (y > dest->height) { + for (y=0; yheight; y++) { + size_t y_src = y + from->y, + y_dest = y + to->y; + + if (y_src >= src->height || y_dest >= dest->height) { break; } - for (x=from->x; xx + from->width; x++) { - uint8_t c; + for (x=0; xwidth; x++) { + size_t x_src = x + from->x, + x_dest = x + to->x; - if (x > from->width) { + uint8_t v; + + if (x_src >= src->width || x_dest >= dest->width) { break; } - c = tile_read((cammy_tile *)src->buf, x, y, from->width); + v = tile_read((cammy_tile *)src->buf, + from->width, + x_src, + y_src); bitmap_write(dest->buf, - x, - y, from->width, channels, - src->palette[c].r, - src->palette[c].g, - src->palette[c].b); + x_dest, + y_dest, + src->palette[v].r, + src->palette[v].g, + src->palette[v].b); } } } @@ -690,22 +739,45 @@ static void bitmap_copy_channel(cammy_image *dest, int channels_dest = depth_channels(dest->depth), channels_src = depth_channels(src->depth); - for (y=from->y; yy + from->height; y++) { - if (y >= dest->height) { + for (y=0; yheight; y++) { + size_t y_src = y + from->y, + y_dest = y + to->y; + + if (y_src >= src->height || y_dest >= dest->height) { break; } - for (x=0; xx + from->width; x++) { + for (x=0; xwidth; x++) { + size_t x_src = x + from->x, + x_dest = x + to->x; + uint8_t v; - if (x >= dest->width) { + if (x_src >= src->width || x_dest >= dest->width) { break; } - v = bitmap_read_channel(src->buf, x, y, src->width, channels_src, channel); + v = bitmap_read_channel(src->buf, + src->width, + channels_src, + channel, + x_src, + y_src); - bitmap_write(dest->buf, x, y, from->width, channels_dest, 0, 0, 0); - bitmap_write_channel(dest->buf, x, y, from->width, channels_dest, channel, v); + bitmap_write(dest->buf, + dest->width, + channels_dest, + x_dest, + y_dest, + 0, 0, 0); + + bitmap_write_channel(dest->buf, + dest->width, + channels_dest, + channel, + x_dest, + y_dest, + v); } } }