From 9384cfdbdde770eb680858b29ac5f40c5c22b45f Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 12 Nov 2018 18:37:30 -0500 Subject: split image conversion to a separate file --- Makefile | 6 ++++-- convert.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ convert.h | 6 ++++++ fb.c | 34 +++------------------------------- 4 files changed, 70 insertions(+), 33 deletions(-) create mode 100644 convert.c create mode 100644 convert.h diff --git a/Makefile b/Makefile index 8692a6d..dfe7230 100644 --- a/Makefile +++ b/Makefile @@ -3,13 +3,15 @@ CFLAGS=-Wall -Wextra -I. -IImageMagick LDFLAGS=-lpthread -LImageMagick/MagickCore/.libs LDLIBS=-lMagickCore-7.Q8 -lm -freeonebook: freeonebook.o gpio.o fb.o +freeonebook: freeonebook.o gpio.o fb.o convert.o freeonebook.o: freeonebook.c gpio.h fb.h gpio.o: gpio.c gpio.h -fb.o: fb.c fb.h +fb.o: fb.c fb.h convert.h + +convert.o: convert.c convert.h clean: rm -f freeonebook *.o diff --git a/convert.c b/convert.c new file mode 100644 index 0000000..333d79d --- /dev/null +++ b/convert.c @@ -0,0 +1,57 @@ +#include +#include + +#define MAGICKCORE_QUANTUM_DEPTH 8 +#define MAGICKCORE_HDRI_ENABLE 0 +#include + +int convert(const char *in, const char *out, int width, int height) +{ + if (!IsMagickCoreInstantiated()) { + MagickCoreGenesis(NULL, MagickFalse); + } + + printf("converting image %s\n", in); + + ExceptionInfo *exception = AcquireExceptionInfo(); + + printf("reading original image\n"); + ImageInfo *info = CloneImageInfo((ImageInfo*)NULL); + if (info == NULL) { + printf("couldn't create new ImageInfo\n"); + return 1; + } + strcpy(info->filename, in); + Image *original = ReadImage(info, exception); + if (original == NULL) { + printf("couldn't read original\n"); + return 0; + } + + printf("resizing\n"); + Image *resized = ResizeImage(original, width, height, LanczosFilter, exception); + DestroyImage(original); + if (resized == NULL) { + printf("couldn't resize\n"); + return 0; + } + + printf("rotating\n"); + Image *rotated = IntegralRotateImage(resized, 3, exception); + DestroyImage(resized); + if (rotated == NULL) { + printf("couldn't rotate\n"); + return 0; + } + + printf("writing grayscale\n"); + strcpy(info->filename, out); + WriteImage(info, rotated, exception); + + printf("cleaning up\n"); + DestroyImage(rotated); + DestroyImageInfo(info); + DestroyExceptionInfo(exception); + + return 1; +} diff --git a/convert.h b/convert.h new file mode 100644 index 0000000..f7811ae --- /dev/null +++ b/convert.h @@ -0,0 +1,6 @@ +#ifndef CONVERT_H +#define CONVERT_H + +int convert(const char *in, const char *out, int width, int height); + +#endif diff --git a/fb.c b/fb.c index 7981961..2a46987 100644 --- a/fb.c +++ b/fb.c @@ -68,36 +68,10 @@ void fb_blank(void) void fb_loadimage(int screen, const char *path) { printf("loading image %s\n", path); + convert(path, "/tmp/image.gray", fb.vsi.xres, fb.vsi.yres); fflush(NULL); sync(); - ExceptionInfo *exception = AcquireExceptionInfo(); - - printf("reading original image\n"); - ImageInfo *info = CloneImageInfo((ImageInfo*)NULL); - strcpy(info->filename, path); - Image *images = ReadImage(info, exception); - - printf("resizing\n"); - Image *original = RemoveFirstImageFromList(&images); - Image *resized = ResizeImage(original, fb.vsi.xres, fb.vsi.yres, LanczosFilter, exception); - DestroyImage(original); - - printf("rotating\n"); - Image *rotated = IntegralRotateImage(resized, 3, exception); - DestroyImage(rotated); - - printf("writing grayscale\n"); - AppendImageToList(&images, rotated); - strcpy(info->filename, "/tmp/converted.gray"); - WriteImage(info, images, exception); - - printf("cleaning up\n"); - DestroyImageList(images); - DestroyImage(rotated); - DestroyImageInfo(info); - DestroyExceptionInfo(exception); - - int fd = open("/tmp/converted.gray", O_RDONLY); + int fd = open("/tmp/image.gray", O_RDONLY); if (fd == -1) { printf("not found\n"); fflush(NULL); @@ -118,7 +92,7 @@ void fb_loadimage(int screen, const char *path) printf("copying image..."); fflush(NULL); sync(); unsigned char *buffer = fb.addr + (screen * fb.size / 2); - memcpy(buffer, img, st.st_size < fb.size ? st.st_size : fb.size); + memcpy(buffer, img, st.st_size); printf("done\n"); fflush(NULL); sync(); munmap(img, st.st_size); @@ -129,8 +103,6 @@ void fb_loadimage(int screen, const char *path) void fb_init(void) { - MagickCoreGenesis(NULL, MagickFalse); - fb.fd = open("/dev/fb0", O_RDWR); ioctl(fb.fd, FBIOGET_FSCREENINFO, &fb.fsi); -- cgit v1.2.1