~jkaivo/freeonebook

9384cfdbdde770eb680858b29ac5f40c5c22b45f — Jakob Kaivo 3 years ago 3b6859a
split image conversion to a separate file
4 files changed, 70 insertions(+), 33 deletions(-)

M Makefile
A convert.c
A convert.h
M fb.c
M Makefile => Makefile +4 -2
@@ 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

A convert.c => convert.c +57 -0
@@ 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;
}

A convert.h => convert.h +6 -0
@@ 0,0 1,6 @@
#ifndef CONVERT_H
#define CONVERT_H

int convert(const char *in, const char *out, int width, int height);

#endif

M fb.c => fb.c +3 -31
@@ 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);