Debian Codegraph @

src/gimp2/print.c

raw symbols SCIP
1 /*
2 *
3 * Print plug-in 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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <gutenprintui2/gutenprintui.h>
27 #include "print_gimp.h"
28
29 #include <sys/types.h>
30 #include <signal.h>
31 #include <ctype.h>
32 #include <sys/wait.h>
33
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38
39 #include "print-intl.h"
40
41 /*
42 * Local functions...
43 */
44
45 static void query (void);
46 static void run (const char *name,
47 gint nparams,
48 const GimpParam *param,
49 gint *nreturn_vals,
50 GimpParam **return_vals);
51 static int do_print_dialog (const gchar *proc_name,
52 gint32 image_ID);
53
54 /*
55 * Globals...
56 */
57
58 GimpPlugInInfo PLUG_IN_INFO = /* Plug-in information */
59 {
60 NULL, /* init_proc */
61 NULL, /* quit_proc */
62 query, /* query_proc */
63 run, /* run_proc */
64 };
65
66 static stpui_plist_t gimp_vars;
67
68 /*
69 * 'main()' - Main entry - just call gimp_main()...
70 */
71
72 MAIN()
73
74 /*
75 * 'query()' - Respond to a plug-in query...
76 */
77
78 #pragma GCC diagnostic push
79 #pragma GCC diagnostic ignored "-Wcast-qual"
80 static void
81 query (void)
82 {
83 static GimpParamDef args[] =
84 {
85 { GIMP_PDB_INT32, (BAD_CONST_CHAR) "run_mode",(BAD_CONST_CHAR) "Interactive, non-interactive" },
86 { GIMP_PDB_IMAGE, (BAD_CONST_CHAR) "image", (BAD_CONST_CHAR) "Input image" },
87 { GIMP_PDB_DRAWABLE,(BAD_CONST_CHAR) "drawable",(BAD_CONST_CHAR) "Input drawable" },
88 { GIMP_PDB_STRING, (BAD_CONST_CHAR) "driver", (BAD_CONST_CHAR) "Printer driver short name" },
89 { GIMP_PDB_STRING, (BAD_CONST_CHAR) "printer_queue",(BAD_CONST_CHAR) "CUPS Printer Queue" },
90 { GIMP_PDB_FLOAT, (BAD_CONST_CHAR) "left", (BAD_CONST_CHAR) "Left offset (points, -1 = centered)" },
91 { GIMP_PDB_FLOAT, (BAD_CONST_CHAR) "top", (BAD_CONST_CHAR) "Top offset (points, -1 = centered)" },
92 { GIMP_PDB_INT32, (BAD_CONST_CHAR) "length_key_value_array", (BAD_CONST_CHAR) "Length of the key-value array" },
93 { GIMP_PDB_STRINGARRAY, (BAD_CONST_CHAR) "keys", (BAD_CONST_CHAR) "Key-value pairs for Gutenprint Settings" },
94 };
95
96 static const gchar *blurb = "This plug-in prints images from The GIMP using Gutenprint directly.";
97 static const gchar *help = "Prints images to many printers.";
98 static const gchar *auth = "Michael Sweet <[email protected]> and Robert Krawitz <[email protected]>";
99 static const gchar *copy = "Copyright 1997-2006 by Michael Sweet and Robert Krawitz";
100 static const gchar *types = "RGB*,GRAY*,INDEXED*";
101
102 gimp_plugin_domain_register (cast_safe(PACKAGE), cast_safe(PACKAGE_LOCALE_DIR));
103
104 do_gimp_install_procedure(blurb, help, auth, copy, types, G_N_ELEMENTS(args),
105 args);
106 }
107 #pragma GCC diagnostic pop
108
109 static guchar *gimp_thumbnail_data = NULL;
110
111 static guchar *
112 stpui_get_thumbnail_data_function(void *image_ID, gint *width, gint *height,
113 gint *bpp, gint page)
114 {
115 if (gimp_thumbnail_data)
116 g_free(gimp_thumbnail_data);
117 gint x = gimp_image_width(p2gint(image_ID));
118 gint y = gimp_image_height(p2gint(image_ID));
119
120 /* GIMP limits thumbnails to 1024px! */
121 if (*width > 1024)
122 *width = 1024;
123 if (*height > 1024)
124 *height = 1024;
125
126 if (*width > x)
127 *width = x;
128 if (*height > y)
129 *height = y;
130 gimp_thumbnail_data =
131 gimp_image_get_thumbnail_data(p2gint(image_ID), width, height, bpp);
132 return gimp_thumbnail_data;
133 }
134
135 /*
136 * 'run()' - Run the plug-in...
137 */
138
139 volatile int SDEBUG = 1;
140
141 static void
142 run (const char *name, /* I - Name of print program. */
143 gint nparams, /* I - Number of parameters passed in */
144 const GimpParam *param, /* I - Parameter values */
145 gint *nreturn_vals, /* O - Number of return values */
146 GimpParam **return_vals) /* O - Return values */
147 {
148 GimpDrawable *drawable; /* Drawable for image */
149 GimpRunMode run_mode; /* Current run mode */
150 GimpParam *values; /* Return values */
151 gint32 drawable_ID; /* drawable ID */
152 GimpExportReturn export = GIMP_EXPORT_CANCEL; /* return value of gimp_export_image() */
153 gdouble xres, yres;
154 char *image_filename;
155 stpui_image_t *image;
156 gint32 image_ID;
157 gint32 base_type;
158 stp_parameter_t desc;
159 if (getenv("STP_DEBUG_STARTUP"))
160 {
161 fprintf(stderr, "pid is %d\n", getpid());
162 while (SDEBUG)
163 ;
164 }
165
166 /*
167 * Initialise libgutenprint
168 */
169
170 stp_init();
171
172 stp_set_output_codeset("UTF-8");
173
174 #ifdef INIT_I18N_UI
175 INIT_I18N_UI();
176 #else
177 /*
178 * With GCC and glib 1.2, there will be a warning here about braces in
179 * expressions. Getting rid of it causes more problems than it solves.
180 * In particular, turning on -ansi on the command line causes a number of
181 * other useful things, such as strcasecmp, popen, and snprintf to go away
182 */
183 INIT_LOCALE (PACKAGE);
184 #endif
185
186 stpui_printer_initialize(&gimp_vars);
187 /*
188 * Initialize parameter data...
189 */
190
191 run_mode = (GimpRunMode)param[0].data.d_int32;
192
193 values = g_new (GimpParam, 1);
194
195 values[0].type = GIMP_PDB_STATUS;
196 values[0].data.d_status = GIMP_PDB_SUCCESS;
197
198 *nreturn_vals = 1;
199 *return_vals = values;
200
201 image_ID = param[1].data.d_int32;
202 drawable_ID = param[2].data.d_int32;
203
204 image_filename = gimp_image_get_filename (image_ID);
205 if (image_filename)
206 {
207 if (strchr(image_filename, '/'))
208 image_filename = strrchr(image_filename, '/') + 1;
209 stpui_set_image_filename(image_filename);
210 /* g_free(image_filename); */
211 }
212 else
213 stpui_set_image_filename("Untitled");
214
215 /* eventually export the image */
216 switch (run_mode)
217 {
218 case GIMP_RUN_INTERACTIVE:
219 case GIMP_RUN_WITH_LAST_VALS:
220 gimp_ui_init ("print", TRUE);
221 export = gimp_export_image (&image_ID, &drawable_ID, "Print",
222 (GIMP_EXPORT_CAN_HANDLE_RGB |
223 GIMP_EXPORT_CAN_HANDLE_GRAY |
224 GIMP_EXPORT_CAN_HANDLE_INDEXED |
225 GIMP_EXPORT_CAN_HANDLE_ALPHA));
226 if (export == GIMP_EXPORT_CANCEL)
227 {
228 values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
229 fprintf(stderr,"Cannot handle image type\n");
230 return;
231 }
232 break;
233 default:
234 break;
235 }
236
237 /*
238 * Get drawable...
239 */
240
241 drawable = gimp_drawable_get (drawable_ID);
242 stpui_set_image_dimensions(drawable->width, drawable->height);
243 gimp_image_get_resolution (image_ID, &xres, &yres);
244 stpui_set_image_resolution(xres, yres);
245 stpui_set_image_channel_depth(8);
246 base_type = gimp_image_base_type(image_ID);
247 switch (base_type)
248 {
249 case GIMP_INDEXED:
250 case GIMP_RGB:
251 stpui_set_image_type("RGB");
252 break;
253 case GIMP_GRAY:
254 stpui_set_image_type("Whitescale");
255 break;
256 default:
257 break;
258 }
259
260 image = Image_GimpDrawable_new(drawable, image_ID);
261 stp_set_float_parameter(gimp_vars.v, "AppGamma", gimp_gamma());
262
263 /*
264 * See how we will run
265 */
266
267 switch (run_mode)
268 {
269 case GIMP_RUN_INTERACTIVE:
270 /*
271 * Get information from the dialog...
272 */
273
274 if (!do_print_dialog (name, image_ID))
275 {
276 values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
277 }
278 else
279 {
280 stpui_plist_copy(&gimp_vars, stpui_get_current_printer());
281 }
282 break;
283
284 case GIMP_RUN_NONINTERACTIVE:
285 /*
286 * Make sure all the arguments are present...
287 */
288 if (nparams < 9)
289 values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
290 else
291 {
292 gimp_image_get_resolution(image_ID, &xres, &yres);
293 gdouble pixwidth = gimp_drawable_width(drawable_ID);
294 gdouble pixheight = gimp_drawable_height(drawable_ID);
295 gdouble pointwidth = gimp_pixels_to_units(pixwidth, GIMP_UNIT_POINT, xres);
296 gdouble pointheight = gimp_pixels_to_units(pixheight, GIMP_UNIT_POINT, yres);
297
298 stp_set_height(gimp_vars.v, pointheight);
299 stp_set_width(gimp_vars.v, pointwidth);
300
301 /*
302 * Avoid calling stpui_print with an invalid driver (SEGFAULT)
303 */
304
305 if (! stp_get_printer_by_driver(param[3].data.d_string) )
306 {
307 values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
308 fprintf(stderr, "Unknown driver %s\n", stp_get_driver(gimp_vars.v));
309 break;
310 }
311
312 stp_set_driver(gimp_vars.v, param[3].data.d_string);
313
314 stpui_plist_set_queue_name(&gimp_vars, param[4].data.d_string);
315
316 /*
317 * Left offset (points, -1 = centered)
318 */
319
320 stp_set_left(gimp_vars.v, param[6].data.d_float);
321
322 /*
323 * Top offset (points, -1 = centered)
324 */
325
326 stp_set_top(gimp_vars.v, param[5].data.d_float);
327
328 /*
329 * Parse remaining parameters from key-value string array
330 */
331
332 int kv_arr_len = param[7].data.d_int32;
333
334 if (kv_arr_len % 2 != 0)
335 {
336 /*
337 * Key with no Value
338 */
339
340 values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
341 fprintf(stderr,"Key with no value provided\n");
342 } else {
343
344 int k = 0;
345 for( k=0; k<kv_arr_len && values[0].data.d_status == GIMP_PDB_SUCCESS; k+=2 )
346 {
347 char *key = param[8].data.d_stringarray[k];
348 char *value = param[8].data.d_stringarray[k + 1];
349 char *endptr = NULL;
350 float float_value = 0;
351
352 stp_describe_parameter(gimp_vars.v, key, &desc);
353
354 switch(desc.p_type)
355 {
356 case STP_PARAMETER_TYPE_STRING_LIST:
357
358 /*
359 * Some useful string stp parameters
360 *
361 * "PrintingMode": BW, Color
362 * "Resolution": "300", "720", etc.
363 * "PageSize": "Letter", "A4", etc. TODO: Support Custom
364 * "MediaType": "Plain", "Glossy", etc.
365 * "InputSlot": "Tray1", "Manual", etc.
366 * "ColorCorrection": Color Correction model
367 * "InkType": Type of ink or cartridge
368 * "InkSet": Set of inks to use
369 * "DitherAlgorithm": Dither algorithm
370 * "Weave": Weave method
371 * "PrintingDirection": "Bidirectional", "Unidirectional"
372 */
373
374 stp_set_string_parameter(gimp_vars.v, key, value);
375 break;
376
377 case STP_PARAMETER_TYPE_INT:
378 stp_set_int_parameter(gimp_vars.v, key, atoi(value));
379 break;
380
381 case STP_PARAMETER_TYPE_BOOLEAN:
382 stp_set_boolean_parameter(gimp_vars.v, key, atoi(value));
383 break;
384
385 case STP_PARAMETER_TYPE_DOUBLE:
386 case STP_PARAMETER_TYPE_DIMENSION:
387
388 /*
389 * Some useful floating point stp parameters
390 *
391 * "Brightness" 0-400%
392 * "Gamma" Output gamma 0.1 - 3.0
393 * "Contrast" 0.1 - 3.0
394 * "Saturation" 0-1000%
395 * "Density" 0-200%
396 * "DropSize1" 0.0-1.0
397 * "DropSize2" 0.0-1.0
398 * "DropSize3" 0.0-1.0
399 */
400
401 float_value = strtof(value, &endptr);
402 if (float_value == 0 && endptr == value)
403 {
404 /*
405 * No conversion was performed -- invalid floating point number
406 */
407 *nreturn_vals = 1;
408 values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
409 fprintf(stderr,"Invalid floating point value provided for key: %s\n", key);
410 }
411 else
412 {
413 if (desc.p_type == STP_PARAMETER_TYPE_DOUBLE)
414 stp_set_float_parameter(gimp_vars.v, key, float_value);
415 else
416 stp_set_dimension_parameter(gimp_vars.v, key, float_value);
417 }
418 break;
419
420 case STP_PARAMETER_TYPE_CURVE:
421 case STP_PARAMETER_TYPE_FILE:
422 case STP_PARAMETER_TYPE_RAW:
423 case STP_PARAMETER_TYPE_ARRAY:
424 values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
425 fprintf(stderr,"Parameter type unsupported in gimp2 plugin for parameter %s\n", key);
426 break;
427
428 case STP_PARAMETER_TYPE_INVALID:
429
430 /*
431 * Output scaling (0-100%, -PPI)
432 */
433
434 if (strncmp("Scaling", key, 7) == 0)
435 {
436 float_value = strtof(value, &endptr);
437 if (float_value == 0 && endptr == value)
438 {
439 /*
440 * No conversion was performed -- invalid floating point number
441 */
442
443 values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
444 fprintf(stderr,"Invalid floating point value provided for key: Scaling\n");
445 }
446 else
447 {
448 gimp_vars.scaling = float_value;
449
450 if (gimp_vars.scaling == 0) {
451 values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
452 fprintf(stderr,"Scaling cannot be 0\n");
453 }
454 else if (gimp_vars.scaling > 0)
455 {
456 /*
457 * Scaling > 0 in %
458 */
459
460 stp_set_width(gimp_vars.v, pointwidth * gimp_vars.scaling / 100.);
461 stp_set_height(gimp_vars.v, pointheight * gimp_vars.scaling / 100.);
462 }
463 else /* gimp_vars < 0 */
464 {
465 /*
466 * Scaling < 0 in DPI
467 */
468
469 pointwidth = gimp_pixels_to_units(pixwidth, GIMP_UNIT_POINT, -gimp_vars.scaling);
470 pointheight = gimp_pixels_to_units(pixheight, GIMP_UNIT_POINT, -gimp_vars.scaling);
471 stp_set_width(gimp_vars.v, pointwidth);
472 stp_set_height(gimp_vars.v, pointheight);
473 }
474 }
475 break;
476 }
477
478 /*
479 * Output orientation (-1 = auto, 0 = portrait, 1 = landscape)
480 */
481
482 else if (strncmp("Orientation", key, 11) == 0)
483 {
484 gimp_vars.orientation = atoi(value);
485 break;
486 }
487 /* Intentional fallthrough */
488 default:
489 values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
490 fprintf(stderr,"Parameter unsupported in gimp2 plugin for parameter %s\n", key);
491 break;
492 }
493 stp_parameter_description_destroy(&desc);
494 }
495 }
496 }
497 break;
498
499 case GIMP_RUN_WITH_LAST_VALS:
500 values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
501 break;
502
503 default:
504 values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
505 break;
506 }
507
508 if (gimp_thumbnail_data)
509 g_free(gimp_thumbnail_data);
510
511 /*
512 * Print the image...
513 */
514 if (values[0].data.d_status == GIMP_PDB_SUCCESS)
515 {
516 /*
517 * Set the tile cache size...
518 */
519
520 if (drawable->height > drawable->width)
521 gimp_tile_cache_ntiles ((drawable->height + gimp_tile_width () - 1) /
522 gimp_tile_width () + 1);
523 else
524 gimp_tile_cache_ntiles ((drawable->width + gimp_tile_width () - 1) /
525 gimp_tile_width () + 1);
526
527 if (! stpui_print(&gimp_vars, image))
528 {
529 values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
530 }
531
532 /*
533 * Store data...
534 * FIXME! This is broken!
535 */
536
537 #if 0
538 if (run_mode == GIMP_RUN_INTERACTIVE)
539 gimp_set_data (PLUG_IN_NAME, vars, sizeof (vars));
540 #endif
541 }
542
543 /*
544 * Detach from the drawable...
545 */
546 gimp_drawable_detach (drawable);
547
548 if (export == GIMP_EXPORT_EXPORT)
549 gimp_image_delete (image_ID);
550 stp_vars_destroy(gimp_vars.v);
551 }
552
553 /*
554 * 'do_print_dialog()' - Pop up the print dialog...
555 */
556
557 #pragma GCC diagnostic push
558 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
559 static void
560 gimp_errfunc(void *file, const char *buf, size_t bytes)
561 {
562 char formatbuf[32];
563 snprintf(formatbuf, 31, "%%%lus", (unsigned long) bytes);
564 g_message(formatbuf, buf);
565 }
566 #pragma GCC diagnostic pop
567
568 static gint
569 do_print_dialog (const gchar *proc_name,
570 gint32 image_ID)
571 {
572 /*
573 * Generate the filename for the current user...
574 */
575 char *filename = gimp_personal_rc_file (cast_safe("printrc"));
576 stpui_set_printrc_file(filename);
577 g_free(filename);
578 if (! getenv("STP_PRINT_MESSAGES_TO_STDERR"))
579 stpui_set_errfunc(gimp_errfunc);
580 stpui_set_thumbnail_func(stpui_get_thumbnail_data_function);
581 stpui_set_thumbnail_data(gint2p(image_ID));
582 return stpui_do_print_dialog();
583 }