| 1 | /* |
| 2 | * Mitsubishi Photo Printer Comon Code |
| 3 | * |
| 4 | * (c) 2013-2024 Solomon Peachy <[email protected]> |
| 5 | * |
| 6 | * The latest version of this program can be found at: |
| 7 | * |
| 8 | * https://git.shaftnet.org/gitea/slp/selphy_print.git |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the Free |
| 12 | * Software Foundation; either version 2 of the License, or (at your option) |
| 13 | * any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, but |
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 17 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 18 | * for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | * SPDX-License-Identifier: GPL-2.0+ |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include "backend_common.h" |
| 28 | #include "backend_mitsu.h" |
| 29 | |
| 30 | int mitsu_loadlib(struct mitsu_lib *lib, int type) |
| 31 | { |
| 32 | memset(lib, 0, sizeof(*lib)); |
| 33 | |
| 34 | #if defined(WITH_DYNAMIC) |
| 35 | DL_INIT(); |
| 36 | |
| 37 | DEBUG("Attempting to load image processing library\n"); |
| 38 | lib->dl_handle = DL_OPEN(LIB_NAME_RE); |
| 39 | if (!lib->dl_handle) |
| 40 | WARNING("Image processing library not found, using internal fallback code\n"); |
| 41 | if (lib->dl_handle) { |
| 42 | lib->GetAPIVersion = DL_SYM(lib->dl_handle, "lib70x_getapiversion"); |
| 43 | if (!lib->GetAPIVersion) { |
| 44 | ERROR("Problem resolving API Version symbol in imaging processing library, too old or not installed?\n"); |
| 45 | DL_CLOSE(lib->dl_handle); |
| 46 | lib->dl_handle = NULL; |
| 47 | return CUPS_BACKEND_FAILED; |
| 48 | } |
| 49 | if (lib->GetAPIVersion() != REQUIRED_LIB_APIVERSION) { |
| 50 | ERROR("Image processing library API version mismatch! (%d vs %d)\n", lib->GetAPIVersion(), REQUIRED_LIB_APIVERSION); |
| 51 | DL_CLOSE(lib->dl_handle); |
| 52 | lib->dl_handle = NULL; |
| 53 | return CUPS_BACKEND_FAILED; |
| 54 | } |
| 55 | lib->DumpAnnounce = DL_SYM(lib->dl_handle, "dump_announce"); |
| 56 | |
| 57 | lib->Get3DColorTable = DL_SYM(lib->dl_handle, "CColorConv3D_Get3DColorTable"); |
| 58 | lib->Load3DColorTable = DL_SYM(lib->dl_handle, "CColorConv3D_Load3DColorTable"); |
| 59 | lib->Destroy3DColorTable = DL_SYM(lib->dl_handle, "CColorConv3D_Destroy3DColorTable"); |
| 60 | lib->DoColorConv = DL_SYM(lib->dl_handle, "CColorConv3D_DoColorConv"); |
| 61 | lib->DoColorConvPlane = DL_SYM(lib->dl_handle, "CColorConv3D_DoColorConvPlane"); |
| 62 | lib->GetCPCData = DL_SYM(lib->dl_handle, "get_CPCData"); |
| 63 | lib->DestroyCPCData = DL_SYM(lib->dl_handle, "destroy_CPCData"); |
| 64 | lib->DoImageEffect60 = DL_SYM(lib->dl_handle, "do_image_effect60"); |
| 65 | lib->DoImageEffect70 = DL_SYM(lib->dl_handle, "do_image_effect70"); |
| 66 | lib->DoImageEffect80 = DL_SYM(lib->dl_handle, "do_image_effect80"); |
| 67 | lib->SendImageData = DL_SYM(lib->dl_handle, "send_image_data"); |
| 68 | lib->CP98xx_DoConvert = DL_SYM(lib->dl_handle, "CP98xx_DoConvert"); |
| 69 | lib->CP98xx_GetData = DL_SYM(lib->dl_handle, "CP98xx_GetData"); |
| 70 | lib->CP98xx_DestroyData = DL_SYM(lib->dl_handle, "CP98xx_DestroyData"); |
| 71 | lib->M1_GetCPCData = DL_SYM(lib->dl_handle, "M1_GetCPCData"); |
| 72 | lib->M1_DestroyCPCData = DL_SYM(lib->dl_handle, "M1_DestroyCPCData"); |
| 73 | lib->M1_Gamma8to14 = DL_SYM(lib->dl_handle, "M1_Gamma8to14"); |
| 74 | lib->M1_CLocalEnhancer = DL_SYM(lib->dl_handle, "M1_CLocalEnhancer"); |
| 75 | lib->M1_CalcRGBRate = DL_SYM(lib->dl_handle, "M1_CalcRGBRate"); |
| 76 | lib->M1_CalcOpRateMatte = DL_SYM(lib->dl_handle, "M1_CalcOpRateMatte"); |
| 77 | lib->M1_CalcOpRateGloss = DL_SYM(lib->dl_handle, "M1_CalcOpRateGloss"); |
| 78 | lib->CPD30_GetData = DL_SYM(lib->dl_handle, "CPD30_GetData"); |
| 79 | lib->CPD30_DestroyData = DL_SYM(lib->dl_handle, "CPD30_DestroyData"); |
| 80 | lib->CPD30_DoConvert = DL_SYM(lib->dl_handle, "CPD30_DoConvert"); |
| 81 | |
| 82 | if (!lib->Get3DColorTable || !lib->Load3DColorTable || |
| 83 | !lib->CP98xx_DoConvert || !lib->CP98xx_GetData || |
| 84 | !lib->CP98xx_DestroyData || |
| 85 | !lib->M1_GetCPCData || !lib->M1_DestroyCPCData || |
| 86 | !lib->M1_Gamma8to14 || !lib->M1_CLocalEnhancer || |
| 87 | !lib->M1_CalcOpRateMatte || !lib->M1_CalcOpRateGloss || |
| 88 | !lib->M1_CalcRGBRate || |
| 89 | !lib->CPD30_GetData || !lib->CPD30_DestroyData || |
| 90 | !lib->CPD30_DoConvert || |
| 91 | !lib->Destroy3DColorTable || !lib->DoColorConv || |
| 92 | !lib->DoColorConvPlane || |
| 93 | !lib->GetCPCData || !lib->DestroyCPCData || |
| 94 | !lib->DoImageEffect60 || !lib->DoImageEffect70 || |
| 95 | !lib->DoImageEffect80 || !lib->SendImageData) { |
| 96 | ERROR("Problem resolving symbols in imaging processing library\n"); |
| 97 | DL_CLOSE(lib->dl_handle); |
| 98 | lib->dl_handle = NULL; |
| 99 | return CUPS_BACKEND_FAILED; |
| 100 | } else { |
| 101 | DEBUG("Image processing library successfully loaded\n"); |
| 102 | if (!stats_only && lib->DumpAnnounce) |
| 103 | lib->DumpAnnounce(logger); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | switch (type) { |
| 108 | case P_MITSU_D80: |
| 109 | lib->DoImageEffect = lib->DoImageEffect80; |
| 110 | break; |
| 111 | case P_MITSU_K60: |
| 112 | case P_KODAK_305: |
| 113 | lib->DoImageEffect = lib->DoImageEffect60; |
| 114 | break; |
| 115 | case P_MITSU_D70X: |
| 116 | case P_FUJI_ASK300: |
| 117 | case P_DNP_DSX80: |
| 118 | lib->DoImageEffect = lib->DoImageEffect70; |
| 119 | break; |
| 120 | case P_MITSU_9800: |
| 121 | case P_MITSU_9800S: |
| 122 | case P_MITSU_9810: |
| 123 | default: |
| 124 | lib->DoImageEffect = NULL; |
| 125 | } |
| 126 | |
| 127 | return CUPS_BACKEND_OK; |
| 128 | #else |
| 129 | ERROR("Need dynamic library support for library loading!\n"); |
| 130 | return CUPS_BACKEND_FAILED; |
| 131 | #endif |
| 132 | } |
| 133 | |
| 134 | int mitsu_destroylib(struct mitsu_lib *lib) |
| 135 | { |
| 136 | #if defined(WITH_DYNAMIC) |
| 137 | if (lib->dl_handle) { |
| 138 | if (lib->cpcdata) |
| 139 | lib->DestroyCPCData(lib->cpcdata); |
| 140 | if (lib->ecpcdata) |
| 141 | lib->DestroyCPCData(lib->ecpcdata); |
| 142 | if (lib->lut) |
| 143 | lib->Destroy3DColorTable(lib->lut); |
| 144 | DL_CLOSE(lib->dl_handle); |
| 145 | } |
| 146 | |
| 147 | memset(lib, 0, sizeof(*lib)); |
| 148 | DL_EXIT(); |
| 149 | |
| 150 | #endif |
| 151 | return CUPS_BACKEND_OK; |
| 152 | } |
| 153 | |
| 154 | int mitsu_apply3dlut_packed(struct mitsu_lib *lib, const char *lutfname, uint8_t *databuf, |
| 155 | uint16_t cols, uint16_t rows, uint16_t stride, |
| 156 | int rgb_bgr) |
| 157 | { |
| 158 | #if defined(WITH_DYNAMIC) |
| 159 | char full[2048]; |
| 160 | int i; |
| 161 | |
| 162 | if (!lutfname) |
| 163 | return CUPS_BACKEND_OK; |
| 164 | if (!lib->dl_handle) |
| 165 | return CUPS_BACKEND_OK; |
| 166 | |
| 167 | snprintf(full, sizeof(full), "%s/%s", corrtable_path, lutfname); |
| 168 | |
| 169 | if (!lib->lut) { |
| 170 | uint8_t *buf = malloc(LUT_LEN); |
| 171 | if (!buf) { |
| 172 | ERROR("Memory allocation failure!\n"); |
| 173 | return CUPS_BACKEND_RETRY_CURRENT; |
| 174 | } |
| 175 | if ((i = dyesub_read_file(full, buf, LUT_LEN, NULL))) |
| 176 | return i; |
| 177 | lib->lut = lib->Load3DColorTable(buf); |
| 178 | free(buf); |
| 179 | if (!lib->lut) { |
| 180 | ERROR("Unable to parse LUT file '%s'!\n", full); |
| 181 | return CUPS_BACKEND_CANCEL; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (lib->lut) { |
| 186 | DEBUG("Running print data through 3D LUT\n"); |
| 187 | lib->DoColorConv(lib->lut, databuf, cols, rows, stride, rgb_bgr); |
| 188 | } |
| 189 | #endif |
| 190 | return CUPS_BACKEND_OK; |
| 191 | } |
| 192 | |
| 193 | int mitsu_apply3dlut_plane(struct mitsu_lib *lib, const char *lutfname, |
| 194 | uint8_t *data_r, uint8_t *data_g, uint8_t *data_b, |
| 195 | uint16_t cols, uint16_t rows) |
| 196 | { |
| 197 | #if defined(WITH_DYNAMIC) |
| 198 | char full[2048]; |
| 199 | int i; |
| 200 | |
| 201 | if (!lutfname) |
| 202 | return CUPS_BACKEND_OK; |
| 203 | if (!lib->dl_handle) |
| 204 | return CUPS_BACKEND_OK; |
| 205 | |
| 206 | snprintf(full, sizeof(full), "%s/%s", corrtable_path, lutfname); |
| 207 | |
| 208 | if (!lib->lut) { |
| 209 | uint8_t *buf = malloc(LUT_LEN); |
| 210 | if (!buf) { |
| 211 | ERROR("Memory allocation failure!\n"); |
| 212 | return CUPS_BACKEND_RETRY_CURRENT; |
| 213 | } |
| 214 | if ((i = dyesub_read_file(full, buf, LUT_LEN, NULL))) |
| 215 | return i; |
| 216 | lib->lut = lib->Load3DColorTable(buf); |
| 217 | free(buf); |
| 218 | if (!lib->lut) { |
| 219 | ERROR("Unable to parse LUT file '%s'!\n", full); |
| 220 | return CUPS_BACKEND_CANCEL; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (lib->lut) { |
| 225 | DEBUG("Running print data through 3D LUT\n"); |
| 226 | lib->DoColorConvPlane(lib->lut, data_r, data_g, data_b, cols * rows); |
| 227 | } |
| 228 | #endif |
| 229 | return CUPS_BACKEND_OK; |
| 230 | } |
| 231 | |
| 232 | int mitsu_readlamdata(const char *fname, uint16_t lamstride, |
| 233 | uint8_t *databuf, uint32_t *datalen, |
| 234 | uint16_t rows, uint16_t cols, uint8_t bpp) |
| 235 | { |
| 236 | int i, j, fd; |
| 237 | char full[2048]; |
| 238 | |
| 239 | snprintf(full, sizeof(full), "%s/%s", corrtable_path, fname); |
| 240 | |
| 241 | DEBUG("Reading %d bytes of matte data from disk (%d/%d)\n", cols * rows * bpp, cols, lamstride); |
| 242 | fd = open(full, O_RDONLY); |
| 243 | if (fd < 0) { |
| 244 | ERROR("Unable to open matte lamination data file '%s'\n", full); |
| 245 | return CUPS_BACKEND_CANCEL; |
| 246 | } |
| 247 | |
| 248 | /* Read in the matte data plane */ |
| 249 | for (j = 0 ; j < rows ; j++) { |
| 250 | int remain = lamstride * bpp; |
| 251 | |
| 252 | /* Read one row of lamination data at a time */ |
| 253 | while (remain) { |
| 254 | i = read(fd, databuf + *datalen, remain); |
| 255 | if (i < 0) |
| 256 | return CUPS_BACKEND_CANCEL; |
| 257 | if (i == 0) { |
| 258 | /* We hit EOF, restart from beginning */ |
| 259 | lseek(fd, 0, SEEK_SET); |
| 260 | continue; |
| 261 | } |
| 262 | *datalen += i; |
| 263 | remain -= i; |
| 264 | } |
| 265 | /* Back off the buffer so we "wrap" on the print row. */ |
| 266 | *datalen -= ((lamstride - cols) * bpp); |
| 267 | } |
| 268 | |
| 269 | return CUPS_BACKEND_OK; |
| 270 | } |
| 271 | |
| 272 | const char *mitsu_temperatures(uint8_t temp) |
| 273 | { |
| 274 | switch(temp) { |
| 275 | case TEMPERATURE_NORMAL: |
| 276 | return "Normal"; |
| 277 | case TEMPERATURE_PREHEAT: |
| 278 | return "Warming Up"; |
| 279 | case TEMPERATURE_COOLING: |
| 280 | return "Cooling Down"; |
| 281 | default: |
| 282 | break; |
| 283 | } |
| 284 | return "Unknown Temperature Status"; |
| 285 | } |
| 286 | |
| 287 | const char *mitsu_media_types(int printer, uint8_t brand, uint8_t type) |
| 288 | { |
| 289 | if (brand == 0xff) { /* Mitsubishi */ |
| 290 | if (printer == P_MITSU_M1) { |
| 291 | if (type == 0x02) |
| 292 | return "CK-M46S (4x6)"; |
| 293 | else if (type == 0x04) |
| 294 | return "CK-M57S (5x7)"; |
| 295 | else if (type == 0x0f) |
| 296 | return "CK-M68S (6x8)"; |
| 297 | } else if (printer == P_MITSU_D80) { |
| 298 | if (type == 0x0f) |
| 299 | return "CK-D868 (6x8)"; |
| 300 | } else if (printer == P_MITSU_D90) { |
| 301 | if (type == 0x0f) |
| 302 | return "CK-D768/CK-D868 (6x8)"; |
| 303 | } |
| 304 | |
| 305 | /* Mitsu D70, and D90 fallthrough */ |
| 306 | if (type == 0x01) |
| 307 | return "CK-D735 (3.5x5)"; |
| 308 | else if (type == 0x02) |
| 309 | return "CK-D746 (4x6)"; |
| 310 | else if (type == 0x04) |
| 311 | return "CK-D757 (5x7)"; |
| 312 | else if (type == 0x05) |
| 313 | return "CK-D769 (6x9)"; |
| 314 | else if (type == 0x0f) |
| 315 | return "CK-D768 (6x8)"; |
| 316 | } else if (brand == 0xe0) { /* SilverLab */ |
| 317 | if (type == 0x0f) |
| 318 | return "CK-D868 SL (6x8)"; |
| 319 | } else if (brand == 0x61) { /* Mitsubishi (K60 series & W5000 series) */ |
| 320 | if (type == 0x84) |
| 321 | return "CK-K57R (5x7)"; |
| 322 | else if (type == 0x8f) |
| 323 | return "CK-K76R (6x8)"; |
| 324 | else if (type == 0xb2) |
| 325 | return "PK58125 + CK5000"; |
| 326 | } else if (brand == 0x6c) { /* Kodak */ |
| 327 | if (type == 0x84) |
| 328 | return "Kodak 5R (5x7)"; |
| 329 | else if (type == 0x8f) |
| 330 | return "Kodak 6R (6x8)"; |
| 331 | } else if (brand == 0x7a || |
| 332 | brand == 0xda) { /* Fujifilm*/ |
| 333 | if (type == 0x01) |
| 334 | return "RL-CF900 (3.5x5)"; |
| 335 | else if (type == 0x02) |
| 336 | return "RK-CF800/4R (4x6)"; |
| 337 | else if (type == 0x04) |
| 338 | return "R2L-CF460/5R (5x7)"; |
| 339 | else if (type == 0x0f) |
| 340 | return "R68-CF400/6R (6x8)"; |
| 341 | } else if (brand == 0xd1) { /* Mitsubishi (D70/D80 -S series) */ |
| 342 | if (type == 0x02) |
| 343 | return "CK-D715 (4x6)"; |
| 344 | else if (type == 0x04) |
| 345 | return "CK-D718 (5x7)"; |
| 346 | else if (type == 0x05) |
| 347 | return "CK-D723 (6x9)"; |
| 348 | else if (type == 0x0f) |
| 349 | return "CK-D720 (6x8)"; |
| 350 | } else if (brand == 0xd6) { /* DNP */ |
| 351 | if (type == 0x74) |
| 352 | return "DNP 5x7"; |
| 353 | else if (type == 0x7f) |
| 354 | return "DNP 6x8"; |
| 355 | else if (type == 0x90) |
| 356 | return "DNP 4x8"; |
| 357 | } |
| 358 | |
| 359 | /* Default fallback */ |
| 360 | if ((type & 0xf) == 0x01) |
| 361 | return "Unknown (3.5x5)"; |
| 362 | else if ((type & 0xf) == 0x02) |
| 363 | return "Unknown (4x6)"; |
| 364 | else if ((type & 0xf) == 0x04) |
| 365 | return "Unknown (5x7)"; |
| 366 | else if ((type & 0xf) == 0x05) |
| 367 | return "Unknown (6x9)"; |
| 368 | else if ((type & 0xf) == 0x0f) |
| 369 | return "Unknown (6x8)"; |
| 370 | |
| 371 | return "Unknown"; |
| 372 | |
| 373 | // Also CK-D746-U for D70-U model |
| 374 | // CK-D820 (6x8) for D80-S model |
| 375 | // D90 can use _all_ of these types except for the -U! |
| 376 | |
| 377 | // CK-M15S (6x4 for M15) |
| 378 | // CK-M18S (5x7 for M15) |
| 379 | // CK-M20S (6x8 for M15) |
| 380 | } |