summaryrefslogtreecommitdiffstats
path: root/backend_mitsu.c
blob: a2600c6b8d6847ee5b6c3e56e1d04b4fa3595410 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 *   Mitsubishi Photo Printer Comon Code
 *
 *   (c) 2013-2020 Solomon Peachy <pizza@shaftnet.org>
 *
 *   The latest version of this program can be found at:
 *
 *     http://git.shaftnet.org/cgit/selphy_print.git
 *
 *   This program is free software; you can redistribute it and/or modify it
 *   under the terms of the GNU General Public License as published by the Free
 *   Software Foundation; either version 3 of the License, or (at your option)
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful, but
 *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *   for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, see <https://www.gnu.org/licenses/>.
 *
 *   SPDX-License-Identifier: GPL-3.0+
 *
 */

#include "backend_common.h"
#include "backend_mitsu.h"

int mitsu_loadlib(struct mitsu_lib *lib, int type)
{
	memset(lib, 0, sizeof(*lib));

#if defined(WITH_DYNAMIC)
	DL_INIT();

	DEBUG("Attempting to load image processing library\n");
	lib->dl_handle = DL_OPEN(LIB_NAME_RE);
	if (!lib->dl_handle)
		WARNING("Image processing library not found, using internal fallback code\n");
	if (lib->dl_handle) {
		lib->GetAPIVersion = DL_SYM(lib->dl_handle, "lib70x_getapiversion");
		if (!lib->GetAPIVersion) {
			ERROR("Problem resolving API Version symbol in imaging processing library, too old or not installed?\n");
			DL_CLOSE(lib->dl_handle);
			lib->dl_handle = NULL;
			return CUPS_BACKEND_FAILED;
		}
		if (lib->GetAPIVersion() != REQUIRED_LIB_APIVERSION) {
			ERROR("Image processing library API version mismatch! (%d vs %d)\n", lib->GetAPIVersion(), REQUIRED_LIB_APIVERSION);
			DL_CLOSE(lib->dl_handle);
			lib->dl_handle = NULL;
			return CUPS_BACKEND_FAILED;
		}

		lib->Load3DColorTable = DL_SYM(lib->dl_handle, "CColorConv3D_Load3DColorTable");
		lib->Destroy3DColorTable = DL_SYM(lib->dl_handle, "CColorConv3D_Destroy3DColorTable");
		lib->DoColorConv = DL_SYM(lib->dl_handle, "CColorConv3D_DoColorConv");
		lib->GetCPCData = DL_SYM(lib->dl_handle, "get_CPCData");
		lib->DestroyCPCData = DL_SYM(lib->dl_handle, "destroy_CPCData");
		lib->DoImageEffect60 = DL_SYM(lib->dl_handle, "do_image_effect60");
		lib->DoImageEffect70 = DL_SYM(lib->dl_handle, "do_image_effect70");
		lib->DoImageEffect80 = DL_SYM(lib->dl_handle, "do_image_effect80");
		lib->SendImageData = DL_SYM(lib->dl_handle, "send_image_data");
		lib->CP98xx_DoConvert = DL_SYM(lib->dl_handle, "CP98xx_DoConvert");
		lib->CP98xx_GetData = DL_SYM(lib->dl_handle, "CP98xx_GetData");
		lib->CP98xx_DestroyData = DL_SYM(lib->dl_handle, "CP98xx_DestroyData");
		if (!lib->Load3DColorTable ||
		    !lib->CP98xx_DoConvert || !lib->CP98xx_GetData ||
		    !lib->CP98xx_DestroyData ||
		    !lib->Destroy3DColorTable || !lib->DoColorConv ||
		    !lib->GetCPCData || !lib->DestroyCPCData ||
		    !lib->DoImageEffect60 || !lib->DoImageEffect70 ||
		    !lib->DoImageEffect80 || !lib->SendImageData) {
			ERROR("Problem resolving symbols in imaging processing library\n");
			DL_CLOSE(lib->dl_handle);
			lib->dl_handle = NULL;
			return CUPS_BACKEND_FAILED;
		} else {
			DEBUG("Image processing library successfully loaded\n");
		}
	}

	switch (type) {
	case P_MITSU_D80:
		lib->DoImageEffect = lib->DoImageEffect80;
		break;
	case P_MITSU_K60:
	case P_KODAK_305:
		lib->DoImageEffect = lib->DoImageEffect60;
		break;
	case P_MITSU_D70X:
	case P_FUJI_ASK300:
		lib->DoImageEffect = lib->DoImageEffect70;
		break;
	case P_MITSU_9800:
	case P_MITSU_9800S:
	case P_MITSU_9810:
	default:
		lib->DoImageEffect = NULL;
	}

	return CUPS_BACKEND_OK;
#else
	ERROR("Need dynamic library support for library loading!\n");
	return CUPS_BACKEND_FAILED;
#endif
}

int mitsu_destroylib(struct mitsu_lib *lib)
{
#if defined(WITH_DYNAMIC)
	if (lib->dl_handle) {
		if (lib->cpcdata)
			lib->DestroyCPCData(lib->cpcdata);
		if (lib->ecpcdata)
			lib->DestroyCPCData(lib->ecpcdata);
		if (lib->lut)
			lib->Destroy3DColorTable(lib->lut);
		DL_CLOSE(lib->dl_handle);
	}

	memset(lib, 0, sizeof(*lib));
	DL_EXIT();

#endif
	return CUPS_BACKEND_OK;
}

int mitsu_apply3dlut(struct mitsu_lib *lib, char *lutfname, uint8_t *databuf,
		     uint16_t cols, uint16_t rows, uint16_t stride,
		     int rgb_bgr)
{
#if defined(WITH_DYNAMIC)
	int i;

	if (!lutfname)
		return CUPS_BACKEND_OK;

	if (!lib->lut) {
		uint8_t *buf = malloc(LUT_LEN);
		if (!buf) {
			ERROR("Memory allocation failure!\n");
			return CUPS_BACKEND_RETRY_CURRENT;
		}
		if ((i = dyesub_read_file(lutfname, buf, LUT_LEN, NULL)))
			return i;
		lib->lut = lib->Load3DColorTable(buf);
		free(buf);
		if (!lib->lut) {
			ERROR("Unable to parse LUT file '%s'!\n", lutfname);
			return CUPS_BACKEND_CANCEL;
		}
	}

	if (lib->lut) {
		DEBUG("Running print data through 3D LUT\n");
		lib->DoColorConv(lib->lut, databuf, cols, rows, stride, rgb_bgr);
	}
#endif
	return CUPS_BACKEND_OK;
}

int mitsu_readlamdata(const char *fname, uint16_t lamstride,
		      uint8_t *databuf, uint32_t *datalen,
		      uint16_t rows, uint16_t cols, uint8_t bpp)
{
	int i, j, fd;
	int remain = cols * rows * bpp;

	DEBUG("Reading %d bytes of matte data from disk (%d/%d)\n", cols * rows * bpp, cols, lamstride);
	fd = open(fname, O_RDONLY);
	if (fd < 0) {
		ERROR("Unable to open matte lamination data file '%s'\n", fname);
		return CUPS_BACKEND_CANCEL;
	}

	/* Read in the matte data plane */
	for (j = 0 ; j < rows ; j++) {
		remain = lamstride * bpp;

		/* Read one row of lamination data at a time */
		while (remain) {
			i = read(fd, databuf + *datalen, remain);
			if (i < 0)
				return CUPS_BACKEND_CANCEL;
			if (i == 0) {
				/* We hit EOF, restart from beginning */
				lseek(fd, 0, SEEK_SET);
				continue;
			}
			*datalen += i;
			remain -= i;
		}
		/* Back off the buffer so we "wrap" on the print row. */
		*datalen -= ((lamstride - cols) * 2);
	}

	return CUPS_BACKEND_OK;
}

const char *mitsu_temperatures(uint8_t temp)
{
	switch(temp) {
	case TEMPERATURE_NORMAL:
		return "Normal";
	case TEMPERATURE_PREHEAT:
		return "Warming Up";
	case TEMPERATURE_COOLING:
		return "Cooling Down";
	default:
		break;
	}
	return "Unknown Temperature Status";
}

const char *mitsu_media_types(uint8_t brand, uint8_t type)
{
	if (brand == 0xff && type == 0x01)
		return "CK-D735 (3.5x5)";
	else if (brand == 0xff && type == 0x02)
		return "CK-D746 (4x6)";
	else if (brand == 0xff && type == 0x04)
		return "CK-D757 (5x7)";
	else if (brand == 0xff && type == 0x05)
		return "CK-D769 (6x9)";
	else if (brand == 0xff && type == 0x0f)
		return "CK-D768/CK-D868 (6x8)";
	else if (brand == 0x6c && type == 0x84)
		return "Kodak 5R (5x7)";
	else if (brand == 0x6c && type == 0x8f)
		return "Kodak 6R (6x8)";
	else if (brand == 0x61 && type == 0x84)
		return "CK-K57R (5x7)";
	else if (brand == 0x61 && type == 0x8f)
		return "CK-K76R (6x8)";
	else if (brand == 0x7a && type == 0x01)
		return "RL-CF900 (3.5x5)";
	else if (brand == 0x7a && type == 0x02)
		return "RK-CF800/4R (4x6)";
	else if (brand == 0x7a && type == 0x04)
		return "R2L-CF460/5R (5x7)";
	else if (brand == 0x7a && type == 0x0f)
		return "R68-CF400/6R (6x8)";
	else
		return "Unknown";

// Also CK-D715, CK-D718, CK-D720, CK-D723 (4x6,5x8,6x8,6x9) for D70-S model
//      CK-D746-U for D70-U model
//      CK-D820 (6x8) for D80-S model
// D90 can use _all_ of these types except for the -U!

	// CK-M57S  (5x7 for M1)
	// CK-M68S  (6x8 for M1)
	// CK-M46S  (6x4 for M1)
}