Actually implement cammy_image_copy()
This commit is contained in:
parent
109a3a9efb
commit
775a822035
2 changed files with 26 additions and 8 deletions
|
@ -26,7 +26,7 @@ typedef struct _cammy_image {
|
|||
|
||||
union {
|
||||
uint8_t *buf;
|
||||
cammy_tile *tile;
|
||||
cammy_tile *tiles;
|
||||
};
|
||||
} cammy_image;
|
||||
|
||||
|
|
32
src/image.c
32
src/image.c
|
@ -155,8 +155,8 @@ cammy_image *cammy_image_open_tile(const char *filename,
|
|||
goto error_malloc_image;
|
||||
}
|
||||
|
||||
if ((image->tile = malloc(size)) == NULL) {
|
||||
goto error_malloc_buf;
|
||||
if ((image->tiles = malloc(size)) == NULL) {
|
||||
goto error_malloc_tiles;
|
||||
}
|
||||
|
||||
for (off=0; off<size;) {
|
||||
|
@ -178,9 +178,9 @@ cammy_image *cammy_image_open_tile(const char *filename,
|
|||
return image;
|
||||
|
||||
error_read:
|
||||
free(image->buf);
|
||||
free(image->tiles);
|
||||
|
||||
error_malloc_buf:
|
||||
error_malloc_tiles:
|
||||
free(image);
|
||||
|
||||
error_malloc_image:
|
||||
|
@ -311,9 +311,27 @@ void cammy_image_copy(cammy_image *dest,
|
|||
size_t y_src,
|
||||
size_t width,
|
||||
size_t height) {
|
||||
size_t y, y_max = y_src + height;
|
||||
size_t x_offset,
|
||||
y_offset;
|
||||
|
||||
for (y=y_src; y<y_max; y++) {
|
||||
size_t x, x_max = x_src + width;
|
||||
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 value = tile_read(src->tiles, x_src + x_offset,
|
||||
y_src + y_offset,
|
||||
src->width);
|
||||
|
||||
if (x_dest + x_offset > dest->width) {
|
||||
break;
|
||||
}
|
||||
|
||||
tile_write(dest->tiles, x_dest + x_offset,
|
||||
y_dest + y_offset,
|
||||
dest->width,
|
||||
value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue