Debian Codegraph @

examples/liquidrescale-basic.cpp

raw
1 /* LiquidRescaling Library EXAMPLE 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-basic.h"
22
23 using namespace std;
24
25 gchar *infile = NULL;
26 gchar *outfile = NULL;
27 gint new_width = 0;
28 gint new_height = 0;
29 gfloat rigidity = 0;
30 gint max_step = 1;
31
32 /*** MAIN ***/
33
34 int
35 main(int argc, char **argv)
36 {/*{{{*/
37
38 /*** read the command line ***/
39 TRAP(parse_command_line(argc, argv));
40
41 /*** open input and output files ***/
42 pngwriter png(1, 1, 0, outfile);
43 png.readfromfile(infile);
44
45 /*** old and new size ***/
46 gint old_width = png.getwidth();
47 gint old_height = png.getheight();
48
49 new_width = (new_width ? new_width : old_width);
50 new_height = (new_height ? new_height : old_height);
51
52 if (new_width < 2) {
53 cerr << "The width should be greater than 2" << endl;
54 exit(1);
55 }
56
57 if (new_height < 2) {
58 cerr << "The height should be greater than 2" << endl;
59 exit(1);
60 }
61
62 /*** print some information on screen ***/
63 cout << "resizing " << infile << " [" << old_width << "x" << old_height << "] into " <<
64 outfile << " [" << new_width << "x" << new_height << "]" << endl << flush;
65
66 /* convert the image into rgb buffers to use them with the library */
67
68 guchar *rgb_buffer;
69
70 TRAP_N(rgb_buffer = rgb_buffer_from_image(&png));
71
72 /**** (I) GENERATE THE MULTISIZE REPRESENTATION ****/
73
74 /* (I.1) swallow the buffer in a (minimal) LqrCarver object
75 * (arguments are width, height and number of colour channels) */
76 LqrCarver *carver;
77 TRAP_N(carver = lqr_carver_new(rgb_buffer, old_width, old_height, 3));
78
79 /* (I.2) initialize the carver (with default values),
80 * so that we can do the resizing */
81 TRAP(lqr_carver_init(carver, max_step, rigidity));
82
83 /**** (II) LIQUID RESCALE ****/
84
85 TRAP(lqr_carver_resize(carver, new_width, new_height));
86
87 /**** (III) READOUT THE MULTISIZE IMAGE ****/
88
89 TRAP(write_carver_to_image(carver, &png));
90
91 /**** (IV) DESTROY THE CARVER OBJECT ****/
92
93 lqr_carver_destroy(carver);
94
95 /*** close file (write the image on disk) ***/
96
97 png.close();
98
99 return 0;
100 }/*}}}*/
101
102 /*** PARSE COMMAND LINE ***/
103
104 LqrRetVal
105 parse_command_line(int argc, char **argv)
106 {/*{{{*/
107 int i;
108 int c;
109 struct option lopts[] = {
110 {"file", required_argument, NULL, 'f'},
111 {"out-file", required_argument, NULL, 'o'},
112 {"width", required_argument, NULL, 'w'},
113 {"height", required_argument, NULL, 'h'},
114 {"rigidity", required_argument, NULL, 'r'},
115 {"max-step", required_argument, NULL, 's'},
116 {"help", no_argument, NULL, '#'},
117 {NULL, 0, NULL, 0}
118 };
119
120 while ((c = getopt_long(argc, argv, "f:,o:,w:,h:,r:,s:", lopts, &i)) != EOF) {
121 switch (c) {
122 case 'f':
123 infile = optarg;
124 break;
125 case 'o':
126 outfile = optarg;
127 break;
128 case 'w':
129 new_width = atoi(optarg);
130 break;
131 case 'h':
132 new_height = atoi(optarg);
133 break;
134 case 'r':
135 rigidity = atof(optarg);
136 break;
137 case 's':
138 max_step = atoi(optarg);
139 break;
140 case '#':
141 help(argv[0]);
142 exit(0);
143 default:
144 cerr << "Error parsing command line. Use " << argv[0] << " --help for usage instructions." << endl;
145 return LQR_ERROR;
146 }
147 }
148
149 if (!infile) {
150 cerr << "Input file missing." << endl;
151 return LQR_ERROR;
152 }
153
154 if (!outfile) {
155 cerr << "Output file missing." << endl;
156 return LQR_ERROR;
157 }
158
159 if (!new_width && !new_height) {
160 cerr << "Either --width or --height has to be specified." << endl;
161 return LQR_ERROR;
162 }
163
164 if (rigidity < 0) {
165 cerr << "Rigidity cannot be negative." << endl;
166 return LQR_ERROR;
167 }
168
169 if (max_step < 0) {
170 cerr << "Max step cannot be negative." << endl;
171 return LQR_ERROR;
172 }
173
174 return LQR_OK;
175 }/*}}}*/
176
177 void
178 help(char *command)
179 {/*{{{*/
180 cout << "Usage: " << command << " -f <file> -o <out-file> [ -w <width> | -h <height> ] [ ... ]" << endl;
181 cout << " Options:" << endl;
182 cout << " -f <file> or --file <file>" << endl;
183 cout << " Specifies input file. Must be in PNG format, in RGB without alpha channel" << endl;
184 cout << " -o <out-file> or --out-file <out-file>" << endl;
185 cout << " Specifies the output file." << endl;
186 cout << " -w <width> or --width <width>" << endl;
187 cout << " The new width. It must be greater than 2." << endl;
188 cout << " If it is 0, or it is not given, the width is unchanged." << endl;
189 cout << " -h <height> or --height <height>" << endl;
190 cout << " Same as -w for the height." << endl;
191 cout << " -r <rigidity> or --rigidity < rigidity>" << endl;
192 cout << " Seams rigidity. Any non-negative value is allowed. Defaults to 0." << endl;
193 cout << " -s <max-step> or --max-step <max-step>" << endl;
194 cout << " Maximum seam transversal step. Default value is 1." << endl;
195 cout << " --help" << endl;
196 cout << " This help." << endl;
197 }/*}}}*/
198
199 /*** AUXILIARY I/O FUNCTIONS ***/
200
201 /* convert the image in the right format */
202 guchar *
203 rgb_buffer_from_image(pngwriter *png)
204 {/*{{{*/
205 gint x, y, k, channels;
206 gint w, h;
207 guchar *buffer;
208
209 /* get info from the image */
210 w = png->getwidth();
211 h = png->getheight();
212 channels = 3; // we assume an RGB image here
213
214 /* allocate memory to store w * h * channels unsigned chars */
215 buffer = g_try_new(guchar, channels * w * h);
216 g_assert(buffer != NULL);
217
218 /* start iteration (always y first, then x, then colours) */
219 for (y = 0; y < h; y++) {
220 for (x = 0; x < w; x++) {
221 for (k = 0; k < channels; k++) {
222 /* read the image channel k at position x,y */
223 buffer[(y * w + x) * channels + k] = (guchar) (png->dread(x + 1, y + 1, k + 1) * 255);
224 /* note : the x+1,y+1,k+1 on the right side are
225 * specific the pngwriter library */
226 }
227 }
228 }
229
230 return buffer;
231 }/*}}}*/
232
233 /* readout the multizie image */
234 LqrRetVal
235 write_carver_to_image(LqrCarver *r, pngwriter *png)
236 {/*{{{*/
237 gint x, y;
238 guchar *rgb;
239 gdouble red, green, blue;
240 gint w, h;
241
242 /* make sure the image is RGB */
243 LQR_CATCH_F(lqr_carver_get_channels(r) == 3);
244
245 /* resize the image canvas as needed to
246 * fit for the new size */
247 w = lqr_carver_get_width(r);
248 h = lqr_carver_get_height(r);
249 png->resize(w, h);
250
251 /* initialize image reading */
252 lqr_carver_scan_reset(r);
253
254 /* readout (no need to init rgb) */
255 while (lqr_carver_scan(r, &x, &y, &rgb)) {
256 /* convert the output into doubles */
257 red = (gdouble) rgb[0] / 255;
258 green = (gdouble) rgb[1] / 255;
259 blue = (gdouble) rgb[2] / 255;
260
261 /* plot (pngwriter's coordinates start from 1,1) */
262 png->plot(x + 1, y + 1, red, green, blue);
263 }
264
265 return LQR_OK;
266 }/*}}}*/