Debian Codegraph @

examples/liquidrescale.cpp

raw
1 /* LiquidRescaling Library DEMO program
2 * Copyright (C) 2007-2009 Carlo Baldassi (the "Author") <[email protected]>.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 3 dated June, 2007.
8
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>
16 */
17
18 #include <pngwriter.h>
19 #include <lqr.h>
20 #include <getopt.h>
21 #include "liquidrescale.h"
22
23 using namespace std;
24
25 LqrCarver *carver;
26
27 gchar *infile = NULL;
28 gchar *outfile = NULL;
29 gchar *pres_infile = NULL;
30 gchar *pres_outfile = NULL;
31 gchar *disc_infile = NULL;
32 gchar *disc_outfile = NULL;
33 gchar *vmap_infile = NULL;
34 gchar *vmap_outfile = NULL;
35 gchar *rigmask_infile = NULL;
36 gchar *rigmask_outfile = NULL;
37 gchar *energy_w_outfile = NULL;
38 gchar *energy_h_outfile = NULL;
39 gint new_width = 0;
40 gint new_height = 0;
41 gfloat rigidity = 0;
42 gint max_step = 1;
43 gint pres_strength = 1000;
44 gint disc_strength = 1000;
45 LqrResizeOrder res_order = LQR_RES_ORDER_HOR;
46 gint side_switch_frequency = 0;
47 gfloat enl_step = 1.5;
48 gchar *energy_function = NULL;
49
50 gfloat new_width_p = 0;
51 gfloat new_height_p = 0;
52
53 gboolean quiet = FALSE;
54
55 clock_t clock_start, clock_now;
56
57 /*** MAIN ***/
58
59 int
60 main(int argc, char **argv)
61 {/*{{{*/
62
63 /*** init glib multithreading ***/
64 /* (currently only used for
65 * cancelling) */
66 if (!g_thread_supported()) {
67 g_thread_init(NULL);
68 }
69
70 /*** read the command line ***/
71 TRAP(parse_command_line(argc, argv));
72
73 /*** open input and output files ***/
74 pngwriter png(1, 1, 0, outfile);
75 png.readfromfile(infile);
76
77 /*** old and new size ***/
78 gint old_width = png.getwidth();
79 gint old_height = png.getheight();
80
81 if (new_width_p) {
82 new_width = (gint) (new_width_p * old_width / 100);
83 }
84 if (new_height_p) {
85 new_height = (gint) (new_height_p * old_height / 100);
86 }
87
88 new_width = (new_width ? new_width : old_width);
89 new_height = (new_height ? new_height : old_height);
90
91 if (new_width < 2) {
92 cerr << "The width should be greater than 2" << endl;
93 exit(1);
94 }
95
96 if (new_height < 2) {
97 cerr << "The height should be greater than 2" << endl;
98 exit(1);
99 }
100
101 /*** print some information on screen ***/
102 if (!quiet) {
103 cout << "resizing " << infile << " [" << old_width << "x" << old_height << "] into " <<
104 outfile << " [" << new_width << "x" << new_height << "]" << endl << endl << flush;
105 }
106
107 if (vmap_infile) {
108 info_msg("will read VMap from", vmap_infile);
109 }
110 if (vmap_outfile) {
111 info_msg("will write VMap to", vmap_outfile);
112 }
113
114 if ((new_height != old_height) && (new_width != old_width) && (vmap_outfile)) {
115 cerr << "Warning: only the last computed visibility map will be written" << endl;
116 }
117
118 /*** read and check the feature masks ***/
119 pngwriter png_pmask;
120 pngwriter png_dmask;
121
122 if (pres_infile) {
123 info_msg("will read preservation mask from", pres_infile);
124 if (pres_outfile) {
125 info_msg("will write preservation mask to", pres_outfile);
126 png_pmask.pngwriter_rename(pres_outfile);
127 }
128 png_pmask.readfromfile(pres_infile);
129 if (pres_outfile) {
130 if (png_pmask.getwidth() != old_width) {
131 cerr << "Fatal error: preservation mask width does not match input file width" << endl;
132 cerr << "cannot honour the --pres-out-file option" << endl;
133 exit(1);
134 }
135 if (png_pmask.getheight() != old_height) {
136 cerr << "Fatal error: preservation mask height does not match input file height" << endl;
137 cerr << "cannot honour the --pres-out-file option" << endl;
138 exit(1);
139 }
140 }
141 }
142
143 if (disc_infile) {
144 info_msg("will read discard mask from", disc_infile);
145 if (disc_outfile) {
146 info_msg("will write discrad mask to", disc_outfile);
147 png_dmask.pngwriter_rename(disc_outfile);
148 }
149 png_dmask.readfromfile(disc_infile);
150 if (disc_outfile) {
151 if (png_dmask.getwidth() != old_width) {
152 cerr << "Fatal error: discard mask width does not match input file width" << endl;
153 cerr << "cannot honour the --disc-out-file option" << endl;
154 exit(1);
155 }
156 if (png_dmask.getheight() != old_height) {
157 cerr << "Fatal error: discard mask height does not match input file height" << endl;
158 cerr << "cannot honour the --disc-out-file option" << endl;
159 exit(1);
160 }
161 }
162 }
163
164 /*** read and check the rigidity mask ***/
165 pngwriter png_rigmask;
166
167 if (rigmask_infile) {
168 info_msg("will read rigidity mask from", rigmask_infile);
169 if (rigmask_outfile) {
170 info_msg("will write rigidity mask to", rigmask_outfile);
171 png_pmask.pngwriter_rename(rigmask_outfile);
172 }
173 png_rigmask.readfromfile(rigmask_infile);
174 if (rigmask_outfile) {
175 if (png_rigmask.getwidth() != old_width) {
176 cerr << "Fatal error: rigidity mask width does not match input file width" << endl;
177 cerr << "cannot honour the --rigmask-out-file option" << endl;
178 exit(1);
179 }
180 if (png_rigmask.getheight() != old_height) {
181 cerr << "Fatal error: rigidity mask height does not match input file height" << endl;
182 cerr << "cannot honour the --rigmask-out-file option" << endl;
183 exit(1);
184 }
185 }
186 }
187
188 if (energy_w_outfile) {
189 info_msg("will write horizontal energy to", energy_w_outfile);
190 }
191 if (energy_h_outfile) {
192 info_msg("will write vertical energy to", energy_h_outfile);
193 }
194
195 /* convert the images into rgb buffers to use them with the library */
196
197 guchar *rgb_buffer;
198 guchar *rgb_pres_buffer = NULL;
199 guchar *rgb_disc_buffer = NULL;
200 guchar *rgb_rigmask_buffer = NULL;
201
202 TRAP_N(rgb_buffer = rgb_buffer_from_image(&png));
203 if (pres_infile) {
204 TRAP_N(rgb_pres_buffer = rgb_buffer_from_image(&png_pmask));
205 }
206 if (disc_infile) {
207 TRAP_N(rgb_disc_buffer = rgb_buffer_from_image(&png_dmask));
208 }
209 if (rigmask_infile) {
210 TRAP_N(rgb_rigmask_buffer = rgb_buffer_from_image(&png_rigmask));
211 }
212
213 if (!quiet) {
214 cout << endl;
215 }
216
217 /**** (I) GENERATE THE MULTISIZE REPRESENTATION ****/
218
219 /* (I.1) swallow the buffer in a (minimal) LqrCarver object
220 * (arguments are width, height and number of colour channels) */
221 TRAP_N(carver = lqr_carver_new(rgb_buffer, old_width, old_height, 3));
222
223 /* (I.2) if we have to attach other images, we have to do so
224 * immediately after the carver construction (we might
225 * initialize the carver first, but not load a visibility map)*/
226 LqrCarver *pres_carver;
227 if (pres_outfile) {
228 TRAP_N(pres_carver = lqr_carver_new(rgb_pres_buffer, old_width, old_height, 3));
229 TRAP(lqr_carver_attach(carver, pres_carver));
230 /* note : this way the rgb_pres_buffer is lost, and we will have
231 * to read it again in order to use it as a bias (this could
232 * be avoided but the code would be harder to read) */
233 }
234
235 LqrCarver *disc_carver;
236 if (disc_outfile) {
237 TRAP_N(disc_carver = lqr_carver_new(rgb_disc_buffer, old_width, old_height, 3));
238 TRAP(lqr_carver_attach(carver, disc_carver));
239 }
240
241 LqrCarver *rigmask_carver;
242 if (rigmask_outfile) {
243 TRAP_N(rigmask_carver = lqr_carver_new(rgb_rigmask_buffer, old_width, old_height, 3));
244 TRAP(lqr_carver_attach(carver, rigmask_carver));
245 }
246
247 /* (I.3) next step depends on whether we have a pre-computed
248 * map to use or not*/
249 if (!vmap_infile) {
250 /* (I.3a.1) initialize the carver (with default values),
251 * so that we can do the resizing */
252 TRAP(lqr_carver_init(carver, max_step, rigidity));
253
254 /* (I.3a.2) add the bias (positive to preserve, negative
255 * to discard) and the rigidity mask*/
256 if (pres_infile) {
257 TRAP_N(rgb_pres_buffer = rgb_buffer_from_image(&png_pmask));
258 TRAP(lqr_carver_bias_add_rgb(carver, rgb_pres_buffer, pres_strength, 3));
259 }
260 if (disc_infile) {
261 TRAP_N(rgb_disc_buffer = rgb_buffer_from_image(&png_dmask));
262 TRAP(lqr_carver_bias_add_rgb(carver, rgb_disc_buffer, -disc_strength, 3));
263 }
264 if (rigmask_infile) {
265 TRAP_N(rgb_rigmask_buffer = rgb_buffer_from_image(&png_rigmask));
266 TRAP(lqr_carver_rigmask_add_rgb(carver, rgb_rigmask_buffer, 3));
267 }
268
269 /* (I.3a.3) set the energy function */
270 TRAP(set_energy(carver, energy_function));
271
272 /* (I.3a.4) output the energy function */
273 if (energy_w_outfile) {
274 TRAP(write_energy(carver, energy_w_outfile, 0));
275 }
276 if (energy_h_outfile) {
277 TRAP(write_energy(carver, energy_h_outfile, 0));
278 }
279
280 /* (I.3b.5) set the side switch frequency */
281 lqr_carver_set_side_switch_frequency(carver, side_switch_frequency);
282
283 /* (I.3b.6) set the enlargement step */
284 TRAP(lqr_carver_set_enl_step(carver, enl_step));
285 } else {
286 /* (I.3b.1) read a visibility map from a file */
287 LqrVMap *vmap;
288 vmap = load_vmap_from_file(vmap_infile);
289 if (vmap == NULL) {
290 cerr << "Unable to load vmap, aborting" << endl;
291 exit(1);
292 }
293
294 /* (I.3b.2) load it in the carver */
295 TRAP(lqr_vmap_load(carver, vmap));
296 }
297
298 /* (I.4) if we want to access the visibility maps, we have
299 * to set it in the carver before rescaling occurs */
300 if (vmap_outfile) {
301 lqr_carver_set_dump_vmaps(carver);
302 }
303
304 /**** (II) SET UP THE PROGRESS INDICATOR ****/
305
306 if (!quiet) {
307 /* (II.1) generate a progress with default values */
308 LqrProgress *progress;
309 TRAP_N(progress = lqr_progress_new());
310
311 /* (II.2) set up with custom commands */
312 init_progress(progress);
313
314 /* (II.3) attach the progress to out multisize image */
315 lqr_carver_set_progress(carver, progress);
316 }
317
318 /**** (III) LIQUID RESCALE ****/
319
320 /* (III.1) set the rescaling order */
321 lqr_carver_set_resize_order(carver, res_order);
322
323 /* (III.2) catch the INT signal */
324 signal(SIGINT, cancel_handler);
325
326 /* (III.3) invoke the rescaling function
327 * this step could be reiterated at wish */
328 TRAP(lqr_carver_resize(carver, new_width, new_height));
329
330 /**** (IV) SAVE THE VISIBILITY MAP ****/
331
332 if (vmap_outfile) {
333 LqrVMap *vmap;
334 TRAP_N(vmap = lqr_vmap_dump(carver));
335 TRAP(save_vmap_to_file(vmap, vmap_outfile));
336 }
337 #if 0
338 /* alternative way */
339 LqrVMapList *vmap_list = lqr_vmap_list_start(carver);
340 if (vmap_outfile) {
341 TRAP(save_vmap_to_file(lqr_vmap_list_current(vmap_list), vmap_outfile));
342 lqr_vmap_list_next(vmap_list);
343 }
344 #endif
345
346 /**** (V) READOUT THE MULTISIZE IMAGE ****/
347
348 /* (V.1) readout the main image */
349 TRAP(write_carver_to_image(carver, &png));
350 /* (V.2) readout the atteched images */
351 LqrCarverList *carver_list = lqr_carver_list_start(carver);
352 if (pres_outfile) {
353 TRAP(write_carver_to_image(lqr_carver_list_current(carver_list), &png_pmask));
354 lqr_carver_list_next(carver_list);
355 }
356 if (disc_outfile) {
357 TRAP(write_carver_to_image(lqr_carver_list_current(carver_list), &png_dmask));
358 lqr_carver_list_next(carver_list);
359 }
360 if (rigmask_outfile) {
361 TRAP(write_carver_to_image(lqr_carver_list_current(carver_list), &png_rigmask));
362 }
363
364 /**** (VI) DESTROY THE CARVER OBJECT ****/
365
366 lqr_carver_destroy(carver);
367
368 /*** close files (write the images on disk) ***/
369
370 png.close();
371 if (pres_outfile) {
372 png_pmask.close();
373 }
374 if (disc_outfile) {
375 png_dmask.close();
376 }
377 if (rigmask_outfile) {
378 png_rigmask.close();
379 }
380
381 return 0;
382 }/*}}}*/
383
384 /*** PARSE COMMAND LINE ***/
385
386 LqrRetVal
387 parse_command_line(int argc, char **argv)
388 {/*{{{*/
389 int i;
390 int c;
391 struct option lopts[] = {
392 {"file", required_argument, NULL, 'f'},
393 {"out-file", required_argument, NULL, 'o'},
394 {"width", required_argument, NULL, 'w'},
395 {"height", required_argument, NULL, 'h'},
396 {"rigidity", required_argument, NULL, 'r'},
397 {"max-step", required_argument, NULL, 's'},
398 {"pres-file", required_argument, NULL, 'p'},
399 {"pres-out-file", required_argument, NULL, 'P'},
400 {"pres-strength", required_argument, NULL, 'z'},
401 {"disc-file", required_argument, NULL, 'd'},
402 {"disc-out-file", required_argument, NULL, 'D'},
403 {"disc-strength", required_argument, NULL, 'x'},
404 {"rigmask-file", required_argument, NULL, 'k'},
405 {"rigmask-out-file", required_argument, NULL, 'K'},
406 {"vmap-out-file", required_argument, NULL, 'v'},
407 {"vmap-in-file", required_argument, NULL, 'V'},
408 {"vertical-first", no_argument, NULL, 't'},
409 {"side-switch-frequency", required_argument, NULL, 'n'},
410 {"enl-step", required_argument, NULL, 'e'},
411 {"energy-function", required_argument, NULL, 'E'},
412 {"energy-w-out-file", required_argument, NULL, 'W'},
413 {"energy-h-out-file", required_argument, NULL, 'H'},
414 {"quiet", no_argument, NULL, 'q'},
415 {"help", no_argument, NULL, '#'},
416 {NULL, 0, NULL, 0}
417 };
418
419 while ((c =
420 getopt_long(argc, argv, "f:,o:,w:,h:,r:,s:,p:,P:,z:,d:,D:,x:,k:,K:,v:,V:,t,n:,e:,E:,W:,H:,q", lopts,
421 &i)) != EOF) {
422 switch (c) {
423 case 'f':
424 infile = optarg;
425 break;
426 case 'o':
427 outfile = optarg;
428 break;
429 case 'w':
430 if (optarg[strlen(optarg) - 1] == '%') {
431 new_width_p = atof(optarg);
432 } else {
433 new_width = atoi(optarg);
434 new_width_p = 0;
435 }
436 break;
437 case 'h':
438 if (optarg[strlen(optarg) - 1] == '%') {
439 new_height_p = atof(optarg);
440 } else {
441 new_height = atoi(optarg);
442 new_height_p = 0;
443 }
444 break;
445 case 'r':
446 rigidity = atof(optarg);
447 break;
448 case 's':
449 max_step = atoi(optarg);
450 break;
451 case 'p':
452 pres_infile = optarg;
453 break;
454 case 'P':
455 pres_outfile = optarg;
456 break;
457 case 'z':
458 pres_strength = atoi(optarg);
459 break;
460 case 'd':
461 disc_infile = optarg;
462 break;
463 case 'D':
464 disc_outfile = optarg;
465 break;
466 case 'x':
467 disc_strength = atoi(optarg);
468 break;
469 case 'k':
470 rigmask_infile = optarg;
471 break;
472 case 'K':
473 rigmask_outfile = optarg;
474 break;
475 case 'v':
476 vmap_infile = optarg;
477 break;
478 case 'V':
479 vmap_outfile = optarg;
480 break;
481 case 't':
482 res_order = LQR_RES_ORDER_VERT;
483 break;
484 case 'n':
485 side_switch_frequency = atoi(optarg);
486 break;
487 case 'e':
488 enl_step = atof(optarg);
489 break;
490 case 'E':
491 energy_function = optarg;
492 break;
493 case 'W':
494 energy_w_outfile = optarg;
495 break;
496 case 'H':
497 energy_h_outfile = optarg;
498 break;
499 case 'q':
500 quiet = 1;
501 break;
502 case '#':
503 help(argv[0]);
504 exit(0);
505 default:
506 cerr << "Error parsing command line. Use " << argv[0] << " --help for usage instructions." << endl;
507 return LQR_ERROR;
508 }
509 }
510
511 if (!infile) {
512 cerr << "Input file missing." << endl;
513 return LQR_ERROR;
514 }
515
516 if (!outfile) {
517 cerr << "Output file missing." << endl;
518 return LQR_ERROR;
519 }
520
521 if (!new_width && !new_height && !new_width_p && !new_height_p) {
522 cerr << "At least one of --width or --height has to be specified (and be different from 0)." << endl;
523 return LQR_ERROR;
524 }
525
526 if (pres_outfile && !pres_infile) {
527 cerr << "Option --pres-out-file can't be used without --pres-in-file." << endl;
528 return LQR_ERROR;
529 }
530
531 if (disc_outfile && !disc_infile) {
532 cerr << "Option --disc-out-file can't be used without --disc-in-file." << endl;
533 return LQR_ERROR;
534 }
535
536 if (rigmask_outfile && !rigmask_infile) {
537 cerr << "Option --rigmask-out-file can't be used without --rigmask-in-file." << endl;
538 return LQR_ERROR;
539 }
540
541 if (pres_strength < 0) {
542 cerr << "Preservation strength cannot be negative." << endl;
543 return LQR_ERROR;
544 }
545
546 if (disc_strength < 0) {
547 cerr << "Discard strength cannot be negative." << endl;
548 return LQR_ERROR;
549 }
550
551 if (rigidity < 0) {
552 cerr << "Rigidity cannot be negative." << endl;
553 return LQR_ERROR;
554 }
555
556 if (max_step < 0) {
557 cerr << "Max step cannot be negative." << endl;
558 return LQR_ERROR;
559 }
560
561 if ((enl_step <= 1) || (enl_step > 2)) {
562 cerr << "Enlargement step must be greater than 1 and not greater than 2." << endl;
563 return LQR_ERROR;
564 }
565
566 return LQR_OK;
567 }/*}}}*/
568
569 void
570 help(char *command)
571 {/*{{{*/
572 cout << "Usage: " << command << " -f <file> -o <out-file> [ -w <width> | -h <height> ] [ ... ]" << endl;
573 cout << " Options:" << endl;
574 cout << " -f <file> or --file <file>" << endl;
575 cout << " Specifies input file. Must be in PNG format, in RGB without alpha channel" << endl;
576 cout << " -o <out-file> or --out-file <out-file>" << endl;
577 cout << " Specifies the output file." << endl;
578 cout << " -w <width> or --width <width>" << endl;
579 cout << " The new width. It must be greater than 2." << endl;
580 cout << " If it is 0, or it is not given, the width is unchanged." << endl;
581 cout << " If it is followed by a %, it is interpreted as a percentage with" << endl;
582 cout << " respect to the original width (and needs not to be an integer)." << endl;
583 cout << " -h <height> or --height <height>" << endl;
584 cout << " Same as -w for the height." << endl;
585 cout << " -r <rigidity> or --rigidity < rigidity>" << endl;
586 cout << " Seams rigidity. Any non-negative value is allowed. Defaults to 0." << endl;
587 cout << " -s <max-step> or --max-step <max-step>" << endl;
588 cout << " Maximum seam transversal step. Default value is 1." << endl;
589 cout << " -p <pres-file> or --pres-file <pres-file>" << endl;
590 cout << " File to be used as a mask for features preservation. It must be in the" << endl;
591 cout << " same format as the input file." << endl;
592 cout << " -P <pres-out-file> or --pres-out-file <pres-out-file>" << endl;
593 cout << " If specified, the preservation mask will be rescaled along with the" << endl;
594 cout << " input file, and the output will be written in the specified file." << endl;
595 cout << " The size of the preservation mask has to match that of the original" << endl;
596 cout << " image." << endl;
597 cout << " -z <pres-strength> or --pres-strength <pres-strength>" << endl;
598 cout << " Preservation mask strength. Any integer non-negative value is allowed." << endl;
599 cout << " Defaults to 1000." << endl;
600 cout << " -d <disc-file> or --disc-file <disc-file>" << endl;
601 cout << " File to be used as a mask for features discard. It must be in the" << endl;
602 cout << " same format as the input file." << endl;
603 cout << " -D <disc-out-file> or --disc-out-file <disc-out-file>" << endl;
604 cout << " Same as -P for the discard mask." << endl;
605 cout << " -x <disc-strength> or --disc-strength <disc-strength>" << endl;
606 cout << " Same as -z for the discard mask." << endl;
607 cout << " -v <vmap-file> or --vmap-file <vmap-file>" << endl;
608 cout << " Reads the visibility map from the specified file. The rescaling will fail" << endl;
609 cout << " if it is asked to go beyond the depth of the map." << endl;
610 cout << " -V <vmap-out-file> or --vmap-out-file <vmap-out-file>" << endl;
611 cout << " Writes the visibility map in the specified file. Currently, only the first one." << endl;
612 cout << " -t or --vertical-first" << endl;
613 cout << " Rescale vertically first (instead of horizontally)." << endl;
614 cout << " -n <frequency> or --side-switch-frequency <frequency>" << endl;
615 cout << " Set the number of switches of the side choice for each size modification." << endl;
616 cout << " -e <step> or --enl-step <step>" << endl;
617 cout << " Set the maximum enlargement in a single step." << endl;
618 cout << " It must be greater than 1 and not greater than 2 (default = 1.5)" << endl;
619 cout << " -E <energy-function> or --energy-function <energy-function>" << endl;
620 cout << " Possible values are: xabs, sumabs, norm, xsobel, sobel" << endl;
621 cout << " -W <energy-w-out-file> or --energy-w-out-file <energy-w-out-file>" << endl;
622 cout << " Writes the energy map for horizontal scalings in the specified file" << endl;
623 cout << " -H <energy-h-out-file> or --energy-h-out-file <energy-h-out-file>" << endl;
624 cout << " Writes the energy map for vertical scalings in the specified file" << endl;
625 cout << " -q or --quiet" << endl;
626 cout << " Quiet mode." << endl;
627 cout << " --help" << endl;
628 cout << " This help." << endl;
629 }/*}}}*/
630
631 /*** AUXILIARY I/O FUNCTIONS ***/
632
633 /* convert the image in the right format */
634 guchar *
635 rgb_buffer_from_image(pngwriter *png)
636 {/*{{{*/
637 gint x, y, k, channels;
638 gint w, h;
639 guchar *buffer;
640
641 /* get info from the image */
642 w = png->getwidth();
643 h = png->getheight();
644 channels = 3; // we assume an RGB image here
645
646 /* allocate memory to store w * h * channels unsigned chars */
647 buffer = g_try_new(guchar, channels * w * h);
648 g_assert(buffer != NULL);
649
650 /* start iteration (always y first, then x, then colours) */
651 for (y = 0; y < h; y++) {
652 for (x = 0; x < w; x++) {
653 for (k = 0; k < channels; k++) {
654 /* read the image channel k at position x,y */
655 buffer[(y * w + x) * channels + k] = (guchar) (png->dread(x + 1, y + 1, k + 1) * 255);
656 /* note : the x+1,y+1,k+1 on the right side are
657 * specific the pngwriter library */
658 }
659 }
660 }
661
662 return buffer;
663 }/*}}}*/
664
665 /* readout the multizie image */
666 LqrRetVal
667 write_carver_to_image(LqrCarver *r, pngwriter *png)
668 {/*{{{*/
669 gint x, y;
670 guchar *rgb;
671 gdouble red, green, blue;
672 gint w, h;
673
674 /* make sure the image is RGB */
675 LQR_CATCH_F(lqr_carver_get_channels(r) == 3);
676
677 /* resize the image canvas as needed to
678 * fit for the new size */
679 w = lqr_carver_get_width(r);
680 h = lqr_carver_get_height(r);
681 png->resize(w, h);
682
683 /* initialize image reading */
684 lqr_carver_scan_reset(r);
685
686 /* readout (no need to init rgb) */
687 while (lqr_carver_scan(r, &x, &y, &rgb)) {
688 /* convert the output into doubles */
689 red = (gdouble) rgb[0] / 255;
690 green = (gdouble) rgb[1] / 255;
691 blue = (gdouble) rgb[2] / 255;
692
693 /* plot (pngwriter's coordinates start from 1,1) */
694 png->plot(x + 1, y + 1, red, green, blue);
695 }
696
697 return LQR_OK;
698 }/*}}}*/
699
700 /*** ENERGY FUNCTIONS ***/
701
702 /* define custom energy function: sobelx */
703 gfloat
704 sobelx(gint x, gint y, gint w, gint h, LqrReadingWindow *rw, gpointer extra_data)
705 {/*{{{*/
706 gint i, j;
707 gdouble e = 0;
708 gdouble k[3][3] = { {0.125, 0.25, 0.125}, {0, 0, 0}, {-0.125, -0.25, -0.125} };
709
710 for (i = -1; i <= 1; i++) {
711 for (j = -1; j <= 1; j++) {
712 e += k[i + 1][j + 1] * lqr_rwindow_read(rw, i, j, 0);
713 }
714 }
715 return (gfloat) fabs(e);
716 }/*}}}*/
717
718 /* define custom energy function: sobel */
719 gfloat
720 sobel(gint x, gint y, gint w, gint h, LqrReadingWindow *rw, gpointer extra_data)
721 {/*{{{*/
722 gint i, j;
723 gdouble ex = 0;
724 gdouble ey = 0;
725 gdouble k[3][3] = { {0.125, 0.25, 0.125}, {0, 0, 0}, {-0.125, -0.25, -0.125} };
726
727 for (i = -1; i <= 1; i++) {
728 for (j = -1; j <= 1; j++) {
729 ex += k[i + 1][j + 1] * lqr_rwindow_read(rw, i, j, 0);
730 ey += k[j + 1][i + 1] * lqr_rwindow_read(rw, i, j, 0);
731 }
732 }
733 return (gfloat) (sqrt(ex * ex + ey * ey));
734 }/*}}}*/
735
736 /* set the energy function */
737 LqrRetVal
738 set_energy(LqrCarver *carver, gchar *energy_function)
739 {/*{{{*/
740 if (energy_function == NULL) {
741 return LQR_OK;
742 } else if (g_strcmp0(energy_function, "xabs") == 0) {
743 LQR_CATCH(lqr_carver_set_energy_function_builtin(carver, LQR_EF_GRAD_XABS));
744 } else if (g_strcmp0(energy_function, "sumabs") == 0) {
745 LQR_CATCH(lqr_carver_set_energy_function_builtin(carver, LQR_EF_GRAD_SUMABS));
746 } else if (g_strcmp0(energy_function, "norm") == 0) {
747 LQR_CATCH(lqr_carver_set_energy_function_builtin(carver, LQR_EF_GRAD_NORM));
748 } else if (g_strcmp0(energy_function, "sobelx") == 0) {
749 LQR_CATCH(lqr_carver_set_energy_function(carver, sobelx, 1, LQR_ER_BRIGHTNESS, NULL));
750 } else if (g_strcmp0(energy_function, "sobel") == 0) {
751 LQR_CATCH(lqr_carver_set_energy_function(carver, sobel, 1, LQR_ER_BRIGHTNESS, NULL));
752 } else {
753 cerr << "Unknown energy function: " << energy_function << endl;
754 exit(1);
755 }
756 return LQR_OK;
757 }/*}}}*/
758
759 /* write out the energy */
760 LqrRetVal
761 write_energy(LqrCarver *carver, gchar *energy_outfile, gint orientation)
762 {/*{{{*/
763 gfloat *nrg_buffer;
764
765 pngwriter png_nrg;
766
767 png_nrg.pngwriter_rename(energy_outfile);
768
769 gint x, y;
770 gfloat en;
771 gdouble red, green, blue;
772
773 gint w = lqr_carver_get_width(carver);
774 gint h = lqr_carver_get_height(carver);
775
776 png_nrg.resize(w, h);
777
778 LQR_CATCH_MEM(nrg_buffer = g_try_new0(gfloat, w * h));
779 LQR_CATCH(lqr_carver_get_energy(carver, nrg_buffer, orientation));
780
781 for (y = 0; y < h; y++) {
782 for (x = 0; x < w; x++) {
783 en = nrg_buffer[y * w + x];
784 red = (gdouble) en;
785 green = (gdouble) en;
786 blue = (gdouble) en;
787
788 /* plot (pngwriter's coordinates start from 1,1) */
789 png_nrg.plot(x + 1, y + 1, red, green, blue);
790 }
791 }
792
793 g_free(nrg_buffer);
794
795 png_nrg.close();
796
797 return LQR_OK;
798 }/*}}}*/
799
800 /*** PROGRESS INDICATOR ***/
801
802 /* set up the progress functions */
803 LqrRetVal
804 my_progress_init(const gchar *message)
805 {/*{{{*/
806 printf("%s -------------------- 0.00%% (00:00.00)", message);
807 fflush(stdout);
808 clock_start = clock();
809 return LQR_OK;
810 }/*}}}*/
811
812 LqrRetVal
813 my_progress_update(gdouble percentage)
814 {/*{{{*/
815 gint i;
816 gfloat p;
817 for (i = 0; i < 38; i++) {
818 printf("\b");
819 }
820 for (i = 0; i < 20 * percentage; i++) {
821 printf("*");
822 }
823 for (; i < 20; i++) {
824 printf("-");
825 }
826 p = 100 * percentage;
827 printf(" %s%.2f%%", p < 10 ? " " : "", p);
828
829 gfloat t;
830 clock_now = clock();
831 t = (gfloat) (clock_now - clock_start) / CLOCKS_PER_SEC;
832
833 i = (gint) t;
834
835 gint min, sec, cent;
836
837 min = i / 60;
838 sec = i % 60;
839 cent = (gint) ((t - i) * 100);
840 printf(" (%.2i:%.2i.%.2i)", min, sec, cent);
841 fflush(stdout);
842 return LQR_OK;
843 }/*}}}*/
844
845 LqrRetVal
846 my_progress_end(const gchar *message)
847 {/*{{{*/
848 gint i;
849 for (i = 0; i < 38; i++) {
850 printf("\b");
851 }
852 printf("******************** %s", message);
853
854 gfloat t;
855 clock_now = clock();
856 t = (gfloat) (clock_now - clock_start) / CLOCKS_PER_SEC;
857
858 i = (gint) t;
859
860 gint min, sec, cent;
861
862 min = i / 60;
863 sec = i % 60;
864 cent = (gint) ((t - i) * 100);
865 printf(" (%.2i:%.2i.%.2i)\n", min, sec, cent);
866
867 fflush(stdout);
868 return LQR_OK;
869 }/*}}}*/
870
871 /* setup the progress machinery */
872 void
873 init_progress(LqrProgress * progress)
874 {/*{{{*/
875 lqr_progress_set_init(progress, my_progress_init);
876 lqr_progress_set_update(progress, my_progress_update);
877 lqr_progress_set_end(progress, my_progress_end);
878 lqr_progress_set_init_width_message(progress, "Resizing width :");
879 lqr_progress_set_init_height_message(progress, "Resizing height :");
880 lqr_progress_set_end_width_message(progress, "done");
881 lqr_progress_set_end_height_message(progress, "done");
882 lqr_progress_set_update_step(progress, 0.01);
883 }/*}}}*/
884
885 /*** VISIBILTY MAPS ***/
886
887 /* convert a visibility map in binary and stores it in a file */
888 LqrRetVal
889 save_vmap_to_file(LqrVMap *vmap, gchar *name)
890 {/*{{{*/
891 FILE *sink;
892 gint *buffer;
893 gint width, height, depth, orientation;
894 gint y, x;
895 gint32 vs;
896
897 buffer = lqr_vmap_get_data(vmap);
898 width = lqr_vmap_get_width(vmap);
899 height = lqr_vmap_get_height(vmap);
900 depth = lqr_vmap_get_depth(vmap);
901 orientation = lqr_vmap_get_orientation(vmap);
902
903 /* open file */
904 if ((sink = fopen(name, "wb")) == NULL) {
905 cerr << "error opening outfile : " << name << endl;
906 return LQR_ERROR;
907 }
908
909 /* VMAP : filtype */
910 fprintf(sink, "VMAP[");
911
912 /* HEAD : the header */
913 fprintf(sink, "HEAD[");
914
915 fprintf(sink, "[width=%i]", width);
916 fprintf(sink, "[height=%i]", height);
917 fprintf(sink, "[orientation=%i]", orientation);
918 fprintf(sink, "[depth=%i]", depth);
919 fprintf(sink, "[comment=()]");
920
921 /* close HEAD */
922 fprintf(sink, "]");
923
924 /* BODY : the data */
925 fprintf(sink, "BODY[");
926
927 /* vmap is a buffer of gint's */
928 for (y = 0; y < height; y++) {
929 for (x = 0; x < width; x++) {
930 vs = buffer[y * width + x];
931 fprintf(sink, "%c%c%c%c", (vs >> 24) & 0xFF, ((vs >> 16) & 0xFF), ((vs >> 8) & 0xFF), vs & 0xFF);
932 }
933 }
934 /* close BODY */
935 fprintf(sink, "]");
936
937 /* close VMAP */
938 fprintf(sink, "]");
939
940 /* close file */
941 fclose(sink);
942
943 return LQR_OK;
944 }/*}}}*/
945
946 /* reads a binary file and converts it into a visibility map */
947 LqrVMap *
948 load_vmap_from_file(gchar *name)
949 {/*{{{*/
950 FILE *input;
951 gint x, y, z0;
952 gint w, h, depth, orientation;
953 gint c, i;
954 guchar i1, i2, i3, i4;
955 gint32 vs;
956 gboolean w_ok, h_ok, d_ok, o_ok;
957 gchar read_buffer[RBS];
958 gchar read_tag[RBS];
959 gint *buffer;
960 LqrVMap *vmap;
961
962 /* flags */
963 w_ok = h_ok = d_ok = o_ok = FALSE;
964
965 w = h = depth = orientation = 0;
966
967 /* open file */
968 CHECK_OR_N(((input = fopen(name, "rb")) != NULL), "can't open vmap file");
969
970 /* read filetype */
971 CHECK_OR_N(fscanf(input, "VMAP[") != EOF, "not a VMAP file");
972
973 /* read the header */
974 CHECK_OR_N(fscanf(input, "HEAD[") != EOF, "missing vmap header");
975
976 /* scan the header */
977 while (1) {
978 CHECK_OR_N((c = getc(input)) != EOF, "corrupted vmap header");
979 if (c == ']') {
980 /* the header has ended */
981 break;
982 }
983 ungetc(c, input);
984
985 /* start reading a tag */
986 CHECK_OR_N(fscanf(input, "[") != EOF, "corrupted vmap header");
987
988 /* start reading the tag name */
989 i = 0;
990 while (((c = getc(input)) != EOF) && (i < RBS)) {
991 if (c == '=') {
992 /* the tag name has ended */
993 break;
994 }
995 read_tag[i++] = c;
996 }
997 CHECK_OR_N(i < RBS, "vmap tag name too long");
998
999 read_tag[i] = '\0';
1000
1001 /* start reading the tag content */
1002 i = 0;
1003 while (((c = getc(input)) != EOF) && (i < RBS)) {
1004 if (c == ']') {
1005 /* the tag has ended */
1006 break;
1007 }
1008 read_buffer[i++] = c;
1009 }
1010 CHECK_OR_N(i < RBS, "tag content too long");
1011 read_buffer[i] = '\0';
1012
1013 /* set the corresponding variable and the flag */
1014 if (strncmp(read_tag, "width", RBS) == 0) {
1015 w = atoi(read_buffer);
1016 w_ok = TRUE;
1017 } else if (strncmp(read_tag, "height", RBS) == 0) {
1018 h = atoi(read_buffer);
1019 h_ok = TRUE;
1020 } else if (strncmp(read_tag, "depth", RBS) == 0) {
1021 depth = atoi(read_buffer);
1022 d_ok = TRUE;
1023 } else if (strncmp(read_tag, "orientation", RBS) == 0) {
1024 orientation = atoi(read_buffer);
1025 o_ok = TRUE;
1026 } else if (strncmp(read_tag, "comment", RBS) == 0) {
1027 /* discard comments */
1028 } else {
1029 cerr << "warning : unknown tag : " << read_tag << endl;
1030 }
1031 }
1032
1033 /* check if all the needed quantities are there */
1034 CHECK_OR_N(w_ok && h_ok && d_ok && o_ok, "missing vmap tags");
1035
1036 /* start reading the data */
1037 CHECK_OR_N(fscanf(input, "BODY[") != EOF, "missing vmap body");
1038
1039 /* allocate memory for the vmap buffer */
1040 buffer = g_try_new0(gint, w * h);
1041 if (buffer == NULL) {
1042 return NULL;
1043 }
1044
1045 /* scan the data */
1046 y = x = 0;
1047 while ((y < h) && ((c = getc(input)) != EOF)) {
1048 ungetc(c, input);
1049
1050 /* read a 32 bit integer */
1051 CHECK_OR_N(fscanf(input, "%c%c%c%c", &i1, &i2, &i3, &i4) == 4, "vmap data corrupted");
1052 vs = i4 + (i3 << 8) + (i2 << 16) + (i1 << 24);
1053
1054 /* set it in the buffer */
1055 z0 = y * w + x;
1056 buffer[z0] = vs;
1057
1058 /* update the coordinates */
1059 x++;
1060 if (x == w) {
1061 x = 0;
1062 y++;
1063 }
1064 }
1065
1066 /* test if the amount of data read is correct */
1067 CHECK_OR_N((x == 0) && (y == h), "vmap data corrupted");
1068
1069 /* end reading the data */
1070 CHECK_OR_N(fscanf(input, "]") != EOF, "unterminated vmap body");
1071
1072 /* end reading the vmap file */
1073 CHECK_OR_N(fscanf(input, "]") != EOF, "unterminated vmap file");
1074
1075 /* create the vmap object with all the data aquired */
1076 vmap = lqr_vmap_new(buffer, w, h, depth, orientation);
1077 if (vmap == NULL) {
1078 return NULL;
1079 }
1080
1081 return vmap;
1082 }/*}}}*/
1083
1084 /*** EXTRA ***/
1085
1086 void
1087 info_msg(const gchar *msg, const gchar *name)
1088 {/*{{{*/
1089 if (!quiet) {
1090 cout << " + " << msg << " " << name << endl << flush;
1091 }
1092 }/*}}}*/
1093
1094 void
1095 cancel_handler(int signum)
1096 {/*{{{*/
1097 printf("\n");
1098 fflush(stdout);
1099 /* We must cancel the computation
1100 * from a different thread */
1101 g_thread_create(cancel_thread, (gpointer) carver, FALSE, NULL);
1102 }/*}}}*/
1103
1104 gpointer
1105 cancel_thread(gpointer data)
1106 {/*{{{*/
1107 lqr_carver_cancel((LqrCarver *) data);
1108 return NULL;
1109 }/*}}}*/