s6145: Switch to using dlopen for the image processing library.

Move the library build entirely out of this makefile, as it's really a
separate project.
This commit is contained in:
Solomon Peachy 2016-01-14 15:00:18 -05:00
parent b92d51dd47
commit ef29c8dfa0
2 changed files with 54 additions and 84 deletions

View file

@ -6,12 +6,6 @@ EXEC_NAME ?= dyesub_backend
DESTDIR ?=
CUPS_BACKEND_DIR ?= $(DESTDIR)/usr/lib/cups/backend
CUPS_DATA_DIR ?= $(DESTDIR)/usr/share/cups
LIBDIR ?= $(DESTDIR)/usr/local/lib
# Don't compile with libS6145ImageProcess by default.
LIBS6145 ?=
# If set, we have the reverse-engineered libS6145ImageProcess.
RE_LIBS6145 ?=
# Tools
CC ?= $(CROSS_COMPILE)gcc
@ -21,43 +15,9 @@ INSTALL ?= install
LN ?= ln
RM ?= rm
### libS6145 nonsense
# Figure out which library to use
ifneq ($(LIBS6145),)
# Figure out OS
UNAME_S := $(shell uname -s)
# Figure out Arch
UNAME_P := $(shell uname -p)
# Lib6145 only works under Linux, and we only have the binary versions
ifeq ($(UNAME_S),Linux)
ifeq ($(UNAME_P),x86_64)
LIBS6145_NAME = S6145ImageProcess-x64
else ifneq ($(filter %86,$(UNAME_P)),)
LIBS6145_NAME = S6145ImageProcess-x32
else
LIBS6145 =
endif
endif # Linux
endif # libS6145
ifneq ($(LIBS6145_RE),)
LIBS6145 = $(LIBS6145_RE)
LIBS6145_NAME = S6145ImageProcessRE
DEPS += $(LIBS6145)/libS6145ImageProcessRE.so
CPPFLAGS += -DWITH_6145_LIB -DS6145_RE -I$(LIBS6145)
endif
# Finally, if we have any version of the library, use it.
ifneq ($(LIBS6145),)
CPPFLAGS += -DWITH_6145_LIB -I$(LIBS6145)
LDFLAGS += -L$(LIBS6145) -l$(LIBS6145_NAME)
endif
### libS6145 nonsense
# Flags
CFLAGS += -Wall -Wextra -g -Os -D_GNU_SOURCE -std=c99
LDFLAGS += `pkg-config --libs libusb-1.0`
CFLAGS += -Wall -Wextra -g -Os -D_GNU_SOURCE -std=c99 # -Wconversion
LDFLAGS += `pkg-config --libs libusb-1.0` -ldl
CPPFLAGS += `pkg-config --cflags libusb-1.0`
# CPPFLAGS += -DLIBUSB_PRE_1_0_10
CPPFLAGS += -DURI_PREFIX=\"$(BACKEND_NAME)\"
@ -70,6 +30,8 @@ DEPS += backend_common.h
SOURCES = backend_common.c $(addsuffix .c,$(addprefix backend_,$(BACKENDS)))
# And now the rules!
.PHONY: clean all install cppcheck
all: $(EXEC_NAME) $(BACKENDS)
$(EXEC_NAME): $(SOURCES) $(DEPS)
@ -86,19 +48,6 @@ install:
$(INSTALL) -o root -m 700 $(EXEC_NAME) $(CUPS_BACKEND_DIR)/$(BACKEND_NAME)
$(MKDIR) -p $(CUPS_DATA_DIR)/usb
$(INSTALL) -o root -m 644 blacklist $(CUPS_DATA_DIR)/usb/net.sf.gimp-print.usb-quirks
ifneq ($(LIBS6145),)
$(INSTALL) -o root -m 755 $(LIBS6145)/lib$(LIBS6145_NAME).so $(LIBDIR)
endif
clean:
$(RM) -f $(EXEC_NAME) $(BACKENDS)
# Reverse-engineered LibS6145ImageProcess
ifneq ($(LIBS6145_RE),)
$(LIBS6145)/libS6145ImageProcessRE.so: $(LIBS6145)/libS6145ImageProcess.o
$(CC) -lm -g -shared -o $@ $<
$(LIBS6145)/libS6145ImageProcess.o: $(LIBS6145)/libS6145ImageProcess.c
$(CC) -c $(CFLAGS) -fno-strict-overflow -fPIC -o $@ $<
endif

View file

@ -50,14 +50,18 @@
#include <signal.h>
#include <time.h>
#include <dlfcn.h>
#define BACKEND shinkos6145_backend
#include "backend_common.h"
#if defined(WITH_6145_LIB)
/* Note that this is a proprietary library, and *NOT* GPL compatible! */
#include "libS6145ImageProcess.h"
#endif
/* Image processing library function prototypes */
typedef int (*ImageProcessingFN)(unsigned char *, unsigned short *, void *);
typedef int (*ImageAvrCalcFN)(unsigned char *, unsigned short, unsigned short, unsigned char *);
#define LIB_NAME "libS6145ImageProcess.so" // Official library
#define LIB_NAME2 "libS6145ImageReProcess.so" // Reimplemented library
enum {
S_IDLE = 0,
@ -248,6 +252,10 @@ struct shinkos6145_ctx {
uint8_t *databuf;
size_t datalen;
void *dl_handle;
ImageProcessingFN ImageProcessing;
ImageAvrCalcFN ImageAvrCalc;
struct shinkos6145_correctionparam *corrdata;
size_t corrdatalen;
};
@ -1635,9 +1643,8 @@ static int shinkos6145_get_imagecorr(struct shinkos6145_ctx *ctx)
}
#if !defined(WITH_6145_LIB)
/* Sanity check correction data */
{
if (!ctx->dl_handle) {
int i;
struct shinkos6145_correctionparam *corrdata = ctx->corrdata;
@ -1692,7 +1699,6 @@ static int shinkos6145_get_imagecorr(struct shinkos6145_ctx *ctx)
goto done;
}
}
#endif
done:
return ret;
@ -1824,7 +1830,25 @@ static void shinkos6145_attach(void *vctx, struct libusb_device_handle *dev,
ctx->type = lookup_printer_type(&shinkos6145_backend,
desc.idVendor, desc.idProduct);
/* Attempt to open the library */
INFO("Attempting to load image processing library\n");
ctx->dl_handle = dlopen(LIB_NAME, RTLD_NOW);
if (!ctx->dl_handle)
ctx->dl_handle = dlopen(LIB_NAME2, RTLD_NOW);
if (!ctx->dl_handle)
WARNING("Image processing library not found, using internal fallback code\n");
if (ctx->dl_handle) {
ctx->ImageProcessing = dlsym(ctx->dl_handle, "ImageProcessing");
ctx->ImageAvrCalc = dlsym(ctx->dl_handle, "ImageAvrCalc");
if (!ctx->ImageProcessing || !ctx->ImageAvrCalc) {
WARNING("Image processing library load problem\n");
dlclose(ctx->dl_handle);
ctx->dl_handle = NULL;
}
INFO("Image processing library successfully loaded\n");
}
/* Ensure jobid is sane */
ctx->jobid = (jobid & 0x7f) + 1;
}
@ -1839,11 +1863,12 @@ static void shinkos6145_teardown(void *vctx) {
free(ctx->databuf);
if (ctx->corrdata)
free(ctx->corrdata);
if (ctx->dl_handle)
dlclose(ctx->dl_handle);
free(ctx);
}
#if !defined (WITH_6145_LIB)
static void lib6145_calc_avg(struct shinkos6145_ctx *ctx, uint16_t rows, uint16_t cols)
{
uint32_t plane, i, planelen;
@ -1934,8 +1959,6 @@ static void lib6145_process_image(uint8_t *src, uint16_t *dest,
}
}
}
#endif
static int shinkos6145_read_parse(void *vctx, int data_fd) {
struct shinkos6145_ctx *ctx = vctx;
@ -2184,24 +2207,22 @@ top:
}
/* Perform the actual library transform */
#if defined(WITH_6145_LIB)
#if defined(S6145_RE)
INFO("Calling Reverse-Engineered Image Processing Library...\n");
#else
INFO("Calling Sinfonia Image Processing Library...\n");
#endif
if (ImageAvrCalc(ctx->databuf, le32_to_cpu(ctx->hdr.columns), le32_to_cpu(ctx->hdr.rows), ctx->image_avg)) {
ERROR("Library returned error!\n");
return CUPS_BACKEND_FAILED;
if (ctx->dl_handle) {
INFO("Calling Image Processing Library...\n");
if (ctx->ImageAvrCalc(ctx->databuf, le32_to_cpu(ctx->hdr.columns), le32_to_cpu(ctx->hdr.rows), ctx->image_avg)) {
ERROR("Library returned error!\n");
return CUPS_BACKEND_FAILED;
}
ctx->ImageProcessing(ctx->databuf, databuf2, ctx->corrdata);
} else {
INFO("Calling Internal Fallback Image Processing Library...\n");
lib6145_calc_avg(ctx, le32_to_cpu(ctx->hdr.columns), le32_to_cpu(ctx->hdr.rows));
lib6145_process_image(ctx->databuf, databuf2, ctx->corrdata, oc_mode);
}
ImageProcessing(ctx->databuf, databuf2, ctx->corrdata);
#else
INFO("Calling Internal Fallback Image Processing Library...\n");
lib6145_calc_avg(ctx, le32_to_cpu(ctx->hdr.columns), le32_to_cpu(ctx->hdr.rows));
lib6145_process_image(ctx->databuf, databuf2, ctx->corrdata, oc_mode);
#endif
free(ctx->databuf);
ctx->databuf = (uint8_t*) databuf2;
ctx->datalen = newlen;
@ -2326,7 +2347,7 @@ static int shinkos6145_query_serno(struct libusb_device_handle *dev, uint8_t end
struct dyesub_backend shinkos6145_backend = {
.name = "Shinko/Sinfonia CHC-S6145",
.version = "0.14WIP",
.version = "0.15WIP",
.uri_prefix = "shinkos6145",
.cmdline_usage = shinkos6145_cmdline,
.cmdline_arg = shinkos6145_cmdline_arg,