diff options
author | Jakob Kaivo <jkk@xcc.kaivo.net> | 2018-11-12 18:37:30 -0500 |
---|---|---|
committer | Jakob Kaivo <jkk@xcc.kaivo.net> | 2018-11-12 18:37:30 -0500 |
commit | 9384cfdbdde770eb680858b29ac5f40c5c22b45f (patch) | |
tree | ee2ca1906bacd47a0e05bbc1c0d9eedd23c24b0f | |
parent | 3b6859a3668624f49a7bcc7fe53d41cb5f4be243 (diff) |
split image conversion to a separate file
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | convert.c | 57 | ||||
-rw-r--r-- | convert.h | 6 | ||||
-rw-r--r-- | fb.c | 34 |
4 files changed, 70 insertions, 33 deletions
@@ -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 <stdio.h> +#include <string.h> + +#define MAGICKCORE_QUANTUM_DEPTH 8 +#define MAGICKCORE_HDRI_ENABLE 0 +#include <MagickCore/MagickCore.h> + +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 @@ -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); |