| 1 | /* |
| 2 | * |
| 3 | * Print plug-in RAW driver for the GIMP. |
| 4 | * |
| 5 | * Copyright 1997-2000 Michael Sweet ([email protected]) and |
| 6 | * Robert Krawitz ([email protected]) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
| 10 | * Software Foundation; either version 2 of the License, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 16 | * for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * This file must include only standard C header files. The core code must |
| 24 | * compile on generic platforms that don't support glib, gimp, gtk, etc. |
| 25 | */ |
| 26 | |
| 27 | /* |
| 28 | * To use this driver, we recommend this: |
| 29 | * |
| 30 | * stp_set_driver(v, "raw-data-8"); // or raw_data-16 |
| 31 | * stp_set_string_parameter(v, "PageSize", "Custom"); |
| 32 | * stp_set_page_height(v, HEIGHT); |
| 33 | * stp_set_page_width(v, WIDTH); |
| 34 | * |
| 35 | * For further details, see compute_thumbnail() in gutenprintui2/panel.c |
| 36 | */ |
| 37 | |
| 38 | #ifdef HAVE_CONFIG_H |
| 39 | #include <config.h> |
| 40 | #endif |
| 41 | #include <gutenprint/gutenprint.h> |
| 42 | #include "gutenprint-internal.h" |
| 43 | #include <gutenprint/gutenprint-intl-internal.h> |
| 44 | #include <string.h> |
| 45 | #include <stdio.h> |
| 46 | |
| 47 | #ifdef __GNUC__ |
| 48 | #define inline __inline__ |
| 49 | #endif |
| 50 | |
| 51 | typedef struct |
| 52 | { |
| 53 | const char *output_type; |
| 54 | int output_channels; |
| 55 | int rotate_channels; |
| 56 | const char *name; |
| 57 | } ink_t; |
| 58 | |
| 59 | typedef struct raw_printer |
| 60 | { |
| 61 | int output_bits; |
| 62 | } raw_printer_t; |
| 63 | |
| 64 | static const raw_printer_t raw_model_capabilities[] = |
| 65 | { |
| 66 | { |
| 67 | 16 |
| 68 | }, |
| 69 | { |
| 70 | 8 |
| 71 | }, |
| 72 | }; |
| 73 | |
| 74 | static const ink_t inks[] = |
| 75 | { |
| 76 | { "RGB", 3, 0, "RGB" }, |
| 77 | { "CMY", 3, 0, "CMY" }, |
| 78 | { "KCMY", 4, 1, "CMYK" }, |
| 79 | { "KCMY", 4, 0, "KCMY" }, |
| 80 | { "Whitescale", 1, 0, "RGBGray" }, |
| 81 | { "Grayscale", 1, 0, "CMYGray" }, |
| 82 | }; |
| 83 | |
| 84 | static const int ink_count = sizeof(inks) / sizeof(ink_t); |
| 85 | |
| 86 | static const stp_parameter_t the_parameters[] = |
| 87 | { |
| 88 | { |
| 89 | "InkType", N_("Ink Type"), "Color=Yes,Category=Advanced Printer Setup", |
| 90 | N_("Type of ink in the printer"), |
| 91 | STP_PARAMETER_TYPE_STRING_LIST, STP_PARAMETER_CLASS_FEATURE, |
| 92 | STP_PARAMETER_LEVEL_BASIC, 1, 1, STP_CHANNEL_NONE, 1, 0 |
| 93 | }, |
| 94 | { |
| 95 | "PrintingMode", N_("Printing Mode"), "Color=Yes,Category=Core Parameter", |
| 96 | N_("Printing Output Mode"), |
| 97 | STP_PARAMETER_TYPE_STRING_LIST, STP_PARAMETER_CLASS_CORE, |
| 98 | STP_PARAMETER_LEVEL_BASIC, 1, 1, STP_CHANNEL_NONE, 1, 0 |
| 99 | }, |
| 100 | { |
| 101 | "PageSize", N_("Page Size"), "Color=No,Category=Basic Printer Setup", |
| 102 | N_("Size of the paper being printed to"), |
| 103 | STP_PARAMETER_TYPE_STRING_LIST, STP_PARAMETER_CLASS_CORE, |
| 104 | STP_PARAMETER_LEVEL_BASIC, 1, 1, STP_CHANNEL_NONE, 1, 0 |
| 105 | }, |
| 106 | }; |
| 107 | |
| 108 | static const int the_parameter_count = |
| 109 | sizeof(the_parameters) / sizeof(const stp_parameter_t); |
| 110 | |
| 111 | static stp_parameter_list_t |
| 112 | raw_list_parameters(const stp_vars_t *v) |
| 113 | { |
| 114 | stp_parameter_list_t *ret = stp_parameter_list_create(); |
| 115 | int i; |
| 116 | (void)v; |
| 117 | for (i = 0; i < the_parameter_count; i++) |
| 118 | stp_parameter_list_add_param(ret, &(the_parameters[i])); |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | static void |
| 123 | raw_parameters(const stp_vars_t *v, const char *name, |
| 124 | stp_parameter_t *description) |
| 125 | { |
| 126 | int i; |
| 127 | (void)v; |
| 128 | description->p_type = STP_PARAMETER_TYPE_INVALID; |
| 129 | if (name == NULL) |
| 130 | return; |
| 131 | |
| 132 | description->deflt.str = NULL; |
| 133 | for (i = 0; i < the_parameter_count; i++) |
| 134 | if (strcmp(name, the_parameters[i].name) == 0) |
| 135 | { |
| 136 | stp_fill_parameter_settings(description, &(the_parameters[i])); |
| 137 | break; |
| 138 | } |
| 139 | if (strcmp(name, "InkType") == 0) |
| 140 | { |
| 141 | description->bounds.str = stp_string_list_create(); |
| 142 | for (i = 0; i < ink_count; i++) |
| 143 | stp_string_list_add_string(description->bounds.str, |
| 144 | inks[i].name, inks[i].name); |
| 145 | description->deflt.str = |
| 146 | stp_string_list_param(description->bounds.str, 0)->name; |
| 147 | } |
| 148 | else if (strcmp(name, "PrintingMode") == 0) |
| 149 | { |
| 150 | description->bounds.str = stp_string_list_create(); |
| 151 | stp_string_list_add_string |
| 152 | (description->bounds.str, "Color", _("Color")); |
| 153 | stp_string_list_add_string |
| 154 | (description->bounds.str, "BW", _("Black and White")); |
| 155 | description->deflt.str = |
| 156 | stp_string_list_param(description->bounds.str, 0)->name; |
| 157 | } |
| 158 | else if (strcmp(name, "PageSize") == 0) |
| 159 | { |
| 160 | const stp_papersize_list_t *paper_sizes = |
| 161 | stpi_get_standard_papersize_list(); |
| 162 | const stp_papersize_list_item_t *ptli = |
| 163 | stpi_papersize_list_get_start(paper_sizes); |
| 164 | description->bounds.str = stp_string_list_create(); |
| 165 | while (ptli) |
| 166 | { |
| 167 | const stp_papersize_t *pt = stpi_paperlist_item_get_data(ptli); |
| 168 | stp_string_list_add_string(description->bounds.str, |
| 169 | pt->name, gettext(pt->text)); |
| 170 | ptli = stpi_paperlist_item_next(ptli); |
| 171 | } |
| 172 | description->deflt.str = |
| 173 | stp_string_list_param(description->bounds.str, 0)->name; |
| 174 | } |
| 175 | else |
| 176 | description->is_active = 0; |
| 177 | } |
| 178 | |
| 179 | static void |
| 180 | raw_imageable_area(const stp_vars_t *v, |
| 181 | stp_dimension_t *left, |
| 182 | stp_dimension_t *right, |
| 183 | stp_dimension_t *bottom, |
| 184 | stp_dimension_t *top) |
| 185 | { |
| 186 | *left = 0; |
| 187 | *top = 0; |
| 188 | *right = stp_get_page_width(v); |
| 189 | *bottom = stp_get_page_height(v); |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | raw_limit(const stp_vars_t *v, /* I */ |
| 194 | stp_dimension_t *width, stp_dimension_t *height, |
| 195 | stp_dimension_t *min_width, stp_dimension_t *min_height) |
| 196 | { |
| 197 | *width = 65535; |
| 198 | *height = 65535; |
| 199 | *min_width = 1; |
| 200 | *min_height = 1; |
| 201 | (void)v; |
| 202 | } |
| 203 | |
| 204 | static void |
| 205 | raw_describe_resolution(const stp_vars_t *v, |
| 206 | stp_resolution_t *x, stp_resolution_t *y) |
| 207 | { |
| 208 | *x = 72; |
| 209 | *y = 72; |
| 210 | (void)v; |
| 211 | } |
| 212 | |
| 213 | static const char * |
| 214 | raw_describe_output(const stp_vars_t *v) |
| 215 | { |
| 216 | const char *ink_type = stp_get_string_parameter(v, "InkType"); |
| 217 | if (ink_type) |
| 218 | { |
| 219 | int i; |
| 220 | for (i = 0; i < ink_count; i++) |
| 221 | if (strcmp(ink_type, inks[i].name) == 0) |
| 222 | return inks[i].output_type; |
| 223 | } |
| 224 | return "RGB"; |
| 225 | } |
| 226 | |
| 227 | static int |
| 228 | raw_print(const stp_vars_t *v, stp_image_t *image) |
| 229 | { |
| 230 | int model = stp_get_model_id(v); |
| 231 | int width = stp_get_page_width(v); |
| 232 | int height = stp_get_page_height(v); |
| 233 | int i, j; |
| 234 | int y; /* Looping vars */ |
| 235 | stp_vars_t *nv = stp_vars_create_copy(v); |
| 236 | int out_channels; |
| 237 | unsigned short *final_out = NULL; |
| 238 | int status = 1; |
| 239 | int bytes_per_channel = raw_model_capabilities[model].output_bits / 8; |
| 240 | int ink_channels = 1; |
| 241 | int rotate_output = 0; |
| 242 | const char *ink_type = stp_get_string_parameter(nv, "InkType"); |
| 243 | stp_image_init(image); |
| 244 | |
| 245 | if (!stp_verify(nv)) |
| 246 | { |
| 247 | stp_eprintf(nv, _("Print options not verified; cannot print.\n")); |
| 248 | stp_vars_destroy(nv); |
| 249 | stp_image_conclude(image); |
| 250 | return 0; |
| 251 | } |
| 252 | if (width != stp_image_width(image) || height != stp_image_height(image)) |
| 253 | { |
| 254 | stp_eprintf(nv, _("Image dimensions must match paper dimensions")); |
| 255 | stp_vars_destroy(nv); |
| 256 | stp_image_conclude(image); |
| 257 | return 0; |
| 258 | } |
| 259 | if (ink_type) |
| 260 | { |
| 261 | for (i = 0; i < ink_count; i++) |
| 262 | if (strcmp(ink_type, inks[i].name) == 0) |
| 263 | { |
| 264 | stp_set_string_parameter(nv, "STPIOutputType", inks[i].output_type); |
| 265 | ink_channels = inks[i].output_channels; |
| 266 | rotate_output = inks[i].rotate_channels; |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | stp_set_boolean_parameter(nv, "SimpleGamma", 1); |
| 272 | stp_channel_reset(nv); |
| 273 | for (i = 0; i < ink_channels; i++) |
| 274 | stp_channel_add(nv, i, 0, 1.0); |
| 275 | |
| 276 | if (bytes_per_channel == 1) |
| 277 | out_channels = stp_color_init(nv, image, 256); |
| 278 | else |
| 279 | out_channels = stp_color_init(nv, image, 65536); |
| 280 | |
| 281 | if (out_channels != ink_channels && out_channels != 1 && ink_channels != 1) |
| 282 | { |
| 283 | stp_eprintf(nv, "Internal error! Output channels or input channels must be 1\n"); |
| 284 | stp_vars_destroy(nv); |
| 285 | stp_image_conclude(image); |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | if (out_channels != ink_channels) |
| 290 | final_out = stp_malloc(width * ink_channels * 2); |
| 291 | |
| 292 | for (y = 0; y < height; y++) |
| 293 | { |
| 294 | unsigned short *out; |
| 295 | unsigned short *real_out; |
| 296 | unsigned zero_mask; |
| 297 | if (stp_color_get_row(nv, image, y, &zero_mask)) |
| 298 | { |
| 299 | status = 2; |
| 300 | break; |
| 301 | } |
| 302 | out = stp_channel_get_input(nv); |
| 303 | real_out = out; |
| 304 | if (rotate_output) |
| 305 | { |
| 306 | unsigned short *tmp_out = real_out; |
| 307 | for (i = 0; i < width; i++) |
| 308 | { |
| 309 | unsigned short tmp = tmp_out[0]; |
| 310 | for (j = 0; j < ink_channels - 1; j++) |
| 311 | tmp_out[j] = tmp_out[j + 1]; |
| 312 | tmp_out[ink_channels - 1] = tmp; |
| 313 | tmp_out += ink_channels; |
| 314 | } |
| 315 | } |
| 316 | if (out_channels != ink_channels) |
| 317 | { |
| 318 | real_out = final_out; |
| 319 | if (out_channels < ink_channels) |
| 320 | { |
| 321 | for (i = 0; i < width; i++) |
| 322 | { |
| 323 | for (j = 0; j < ink_channels; j++) |
| 324 | final_out[i * ink_channels + j] = out[i]; |
| 325 | } |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | for (i = 0; i < width; i++) |
| 330 | { |
| 331 | int avg = 0; |
| 332 | for (j = 0; j < out_channels; j++) |
| 333 | avg += out[i * out_channels + j]; |
| 334 | final_out[i] = avg / out_channels; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | if (bytes_per_channel == 1) |
| 339 | { |
| 340 | unsigned char *char_out = (unsigned char *) real_out; |
| 341 | for (i = 0; i < width * ink_channels; i++) |
| 342 | char_out[i] = real_out[i] / 257; |
| 343 | } |
| 344 | stp_zfwrite((char *) real_out, |
| 345 | width * ink_channels * bytes_per_channel, 1, nv); |
| 346 | } |
| 347 | stp_image_conclude(image); |
| 348 | if (final_out) |
| 349 | stp_free(final_out); |
| 350 | stp_vars_destroy(nv); |
| 351 | return status; |
| 352 | } |
| 353 | |
| 354 | static const stp_printfuncs_t print_raw_printfuncs = |
| 355 | { |
| 356 | raw_list_parameters, |
| 357 | raw_parameters, |
| 358 | stp_default_media_size, |
| 359 | raw_imageable_area, |
| 360 | raw_imageable_area, |
| 361 | raw_limit, |
| 362 | raw_print, |
| 363 | raw_describe_resolution, |
| 364 | raw_describe_output, |
| 365 | stp_verify_printer_params, |
| 366 | NULL, |
| 367 | NULL, |
| 368 | NULL, |
| 369 | stpi_standard_describe_papersize |
| 370 | }; |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | static stp_family_t print_raw_module_data = |
| 376 | { |
| 377 | &print_raw_printfuncs, |
| 378 | NULL |
| 379 | }; |
| 380 | |
| 381 | |
| 382 | static int |
| 383 | print_raw_module_init(void) |
| 384 | { |
| 385 | return stpi_family_register(print_raw_module_data.printer_list); |
| 386 | } |
| 387 | |
| 388 | |
| 389 | static int |
| 390 | print_raw_module_exit(void) |
| 391 | { |
| 392 | return stpi_family_unregister(print_raw_module_data.printer_list); |
| 393 | } |
| 394 | |
| 395 | |
| 396 | /* Module header */ |
| 397 | #define stp_module_version print_raw_LTX_stp_module_version |
| 398 | #define stp_module_data print_raw_LTX_stp_module_data |
| 399 | |
| 400 | stp_module_version_t stp_module_version = {0, 0}; |
| 401 | |
| 402 | stp_module_t stp_module_data = |
| 403 | { |
| 404 | "raw", |
| 405 | VERSION, |
| 406 | "RAW family driver", |
| 407 | STP_MODULE_CLASS_FAMILY, |
| 408 | NULL, |
| 409 | print_raw_module_init, |
| 410 | print_raw_module_exit, |
| 411 | (void *) &print_raw_module_data |
| 412 | }; |