diff --git a/bin/export.c b/bin/export.c index 3722029..ea6c1f3 100644 --- a/bin/export.c +++ b/bin/export.c @@ -9,7 +9,7 @@ #include #include -#include +#include #include "pnglite.h" #include "png.h" @@ -38,7 +38,7 @@ static void usage(int argc, char **argv, const char *message, ...) { } int cammy_export(int argc, char **argv) { - cammy_sram *sram; + cammy_camera *camera; int photo = 0; cammy_image *image; @@ -52,18 +52,18 @@ int cammy_export(int argc, char **argv) { photo = atoi(argv[3]); - if (photo < 1 || photo > CAMMY_SRAM_PHOTO_COUNT) { + if (photo < 1 || photo > CAMMY_CAMERA_PHOTO_COUNT) { usage(argc, argv, "Invalid photo number"); } - if ((sram = cammy_sram_open(argv[2])) == NULL) { + if ((camera = cammy_camera_open(argv[2])) == NULL) { fprintf(stderr, "%s: %s: %s: %s\n", - argv[0], "cammy_sram_open()", argv[2], strerror(errno)); + argv[0], "cammy_camera_open()", argv[2], strerror(errno)); - goto error_sram_open; + goto error_camera_open; } - if ((image = cammy_photo_export(&sram->data->photos[photo], + if ((image = cammy_photo_export(&camera->data->photos[photo], CAMMY_IMAGE_BITMAP, CAMMY_IMAGE_24BPP_RGB, palette)) == NULL) { @@ -76,7 +76,7 @@ int cammy_export(int argc, char **argv) { cammy_image_destroy(image); - cammy_sram_close(sram); + cammy_camera_close(camera); return 0; @@ -84,8 +84,8 @@ error_image_save: cammy_image_destroy(image); error_photo_export: - cammy_sram_close(sram); + cammy_camera_close(camera); -error_sram_open: +error_camera_open: return 1; } diff --git a/bin/import.c b/bin/import.c index 43b3e6e..77e3d4d 100644 --- a/bin/import.c +++ b/bin/import.c @@ -5,7 +5,7 @@ #include #include -#include +#include #include "commands.h" @@ -25,7 +25,7 @@ static void usage(int argc, char **argv, const char *message, ...) { } int cammy_import(int argc, char **argv) { - cammy_sram *sram; + cammy_camera *camera; cammy_image *image; int photo = 0; @@ -39,15 +39,15 @@ int cammy_import(int argc, char **argv) { photo = atoi(argv[3]); - if (photo < 1 || photo > CAMMY_SRAM_PHOTO_COUNT) { + if (photo < 1 || photo > CAMMY_CAMERA_PHOTO_COUNT) { usage(argc, argv, "Invalid photo number"); } - if ((sram = cammy_sram_open(argv[2])) == NULL) { + if ((camera = cammy_camera_open(argv[2])) == NULL) { fprintf(stderr, "%s: %s: %s: %s\n", - argv[0], "cammy_sram_open()", argv[2], strerror(errno)); + argv[0], "cammy_camera_open()", argv[2], strerror(errno)); - goto error_sram_open; + goto error_camera_open; } if ((image = cammy_image_open(argv[4])) == NULL) { @@ -62,13 +62,13 @@ int cammy_import(int argc, char **argv) { goto error_invalid_dimensions; } - if (cammy_photo_import(&sram->data->photos[photo-1], image) < 0) { + if (cammy_photo_import(&camera->data->photos[photo-1], image) < 0) { goto error_photo_import; } cammy_image_destroy(image); - cammy_sram_close(sram); + cammy_camera_close(camera); return 0; @@ -77,8 +77,8 @@ error_invalid_dimensions: cammy_image_destroy(image); error_image_open: - cammy_sram_close(sram); + cammy_camera_close(camera); -error_sram_open: +error_camera_open: return 1; } diff --git a/bin/main.c b/bin/main.c index c67af2f..fe8a5c9 100644 --- a/bin/main.c +++ b/bin/main.c @@ -3,7 +3,7 @@ #include #include -#include +#include #include "commands.h" @@ -41,7 +41,7 @@ static void usage(int argc, char **argv, const char *message, ...) { } void cammy_validate_photo_number(int argc, char **argv, int num) { - if (num < 1 || num > CAMMY_SRAM_PHOTO_COUNT) { + if (num < 1 || num > CAMMY_CAMERA_PHOTO_COUNT) { usage(argc, argv, "Invalid photo number"); } } diff --git a/include/cammy/camera.h b/include/cammy/camera.h new file mode 100644 index 0000000..2360317 --- /dev/null +++ b/include/cammy/camera.h @@ -0,0 +1,42 @@ +#ifndef _CAMMY_CAMERA_H +#define _CAMMY_CAMERA_H + +#include + +#include + +#define CAMMY_CAMERA_SCRATCH_SIZE 4604 +#define CAMMY_CAMERA_SCRATCH_2_SIZE 4 +#define CAMMY_CAMERA_PHOTO_COUNT 30 + +#pragma pack(1) +#pragma pack(push) + +typedef struct _cammy_camera_data { + uint8_t scratch[CAMMY_CAMERA_SCRATCH_SIZE]; + + cammy_tile gameface[CAMMY_PHOTO_TILES_HEIGHT] + [CAMMY_PHOTO_TILES_WIDTH]; + + uint8_t scratch_2[CAMMY_CAMERA_SCRATCH_2_SIZE]; + + cammy_photo photos[CAMMY_CAMERA_PHOTO_COUNT]; +} cammy_camera_data; + +#pragma pack(pop) + +typedef struct _cammy_camera { + int fd; + + cammy_camera_data *data; + + size_t size, + page_size, + mapped_size; +} cammy_camera; + +cammy_camera *cammy_camera_open(const char *file); + +void cammy_camera_close(cammy_camera *sram); + +#endif /* _CAMMY_CAMERA_H */ diff --git a/include/cammy/sram.h b/include/cammy/sram.h deleted file mode 100644 index d9314d8..0000000 --- a/include/cammy/sram.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef _CAMMY_SRAM_H -#define _CAMMY_SRAM_H - -#include - -#include - -#define CAMMY_SRAM_SCRATCH_SIZE 4604 -#define CAMMY_SRAM_SCRATCH_2_SIZE 4 -#define CAMMY_SRAM_PHOTO_COUNT 30 - -#pragma pack(1) -#pragma pack(push) - -typedef struct _cammy_sram_data { - uint8_t scratch[CAMMY_SRAM_SCRATCH_SIZE]; - - cammy_tile gameface[CAMMY_PHOTO_TILES_HEIGHT] - [CAMMY_PHOTO_TILES_WIDTH]; - - uint8_t scratch_2[CAMMY_SRAM_SCRATCH_2_SIZE]; - - cammy_photo photos[CAMMY_SRAM_PHOTO_COUNT]; -} cammy_sram_data; - -#pragma pack(pop) - -typedef struct _cammy_sram { - int fd; - - cammy_sram_data *data; - - size_t size, - page_size, - mapped_size; -} cammy_sram; - -cammy_sram *cammy_sram_open(const char *file); - -void cammy_sram_close(cammy_sram *sram); - -#endif /* _CAMMY_SRAM_H */ diff --git a/src/Makefile b/src/Makefile index 7035611..5c09e5c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -11,8 +11,8 @@ HEADERS_LOCAL = HEADERS_BUILD = $(HEADERS_LOCAL) \ $(addprefix $(INCLUDE_PATH)/$(HEADER_SUBDIR)/,$(HEADERS)) -HEADERS = sram.h image.h photo.h tile.h -OBJS = sram.o image.o photo.o +HEADERS = camera.h image.h photo.h tile.h +OBJS = camera.o image.o photo.o VERSION_MAJOR = 0 VERSION_MINOR = 0.1 diff --git a/src/camera.c b/src/camera.c new file mode 100644 index 0000000..daaecef --- /dev/null +++ b/src/camera.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include + +#include + +static inline size_t _mapped_size(size_t size, size_t page_size) { + return size + (page_size - (size % page_size)); +} + +cammy_camera *cammy_camera_open(const char *file) { + cammy_camera *camera; + struct stat st; + + if ((camera = malloc(sizeof(*camera))) == NULL) { + goto error_malloc_camera; + } + + if ((camera->fd = open(file, O_RDWR)) < 0) { + goto error_open; + } + + if (fstat(camera->fd, &st) < 0) { + goto error_stat; + } + + if (st.st_size < sizeof(cammy_camera_data)) { + errno = EINVAL; + + goto error_invalid_file; + } + + camera->size = st.st_size; + camera->page_size = (size_t)sysconf(_SC_PAGESIZE); camera->mapped_size = _mapped_size(st.st_size, camera->page_size); + + if ((camera->data = mmap(NULL, camera->mapped_size, + PROT_READ | PROT_WRITE, MAP_SHARED, camera->fd, 0)) == NULL) { + goto error_mmap; + } + + return camera; + +error_mmap: +error_invalid_file: +error_stat: + close(camera->fd); + +error_open: + free(camera); + +error_malloc_camera: + return NULL; +} + +void cammy_camera_close(cammy_camera *camera) { + munmap(camera->data, camera->mapped_size); + close(camera->fd); + free(camera); +} diff --git a/src/sram.c b/src/sram.c deleted file mode 100644 index f405875..0000000 --- a/src/sram.c +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include - -static inline size_t _mapped_size(size_t size, size_t page_size) { - return size + (page_size - (size % page_size)); -} - -cammy_sram *cammy_sram_open(const char *file) { - cammy_sram *sram; - struct stat st; - - if ((sram = malloc(sizeof(*sram))) == NULL) { - goto error_malloc_sram; - } - - if ((sram->fd = open(file, O_RDWR)) < 0) { - goto error_open; - } - - if (fstat(sram->fd, &st) < 0) { - goto error_stat; - } - - if (st.st_size < sizeof(cammy_sram_data)) { - errno = EINVAL; - - goto error_invalid_file; - } - - sram->size = st.st_size; - sram->page_size = (size_t)sysconf(_SC_PAGESIZE); sram->mapped_size = _mapped_size(st.st_size, sram->page_size); - - if ((sram->data = mmap(NULL, sram->mapped_size, - PROT_READ | PROT_WRITE, MAP_SHARED, sram->fd, 0)) == NULL) { - goto error_mmap; - } - - return sram; - -error_mmap: -error_invalid_file: -error_stat: - close(sram->fd); - -error_open: - free(sram); - -error_malloc_sram: - return NULL; -} - -void cammy_sram_close(cammy_sram *sram) { - munmap(sram->data, sram->mapped_size); - close(sram->fd); - free(sram); -}