Removed trailing whitespaces
[liblqr.git] / lqr / lqr_vmap.c
blobfabe5e8af2493a7c2732a40ffa5507c314e1e4eb
1 /* LiquidRescaling Library
2 * Copyright (C) 2007-2009 Carlo Baldassi (the "Author") <carlobaldassi@gmail.com>.
3 * All Rights Reserved.
5 * This library implements the algorithm described in the paper
6 * "Seam Carving for Content-Aware Image Resizing"
7 * by Shai Avidan and Ariel Shamir
8 * which can be found at http://www.faculty.idc.ac.il/arik/imret.pdf
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; version 3 dated June, 2007.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>
23 #include <lqr/lqr_all.h>
25 #ifdef __LQR_DEBUG__
26 #include <assert.h>
27 #endif /* __LQR_DEBUG__ */
30 /**** SEAMS BUFFER FUNCTIONS ****/
32 LQR_PUBLIC
33 LqrVMap*
34 lqr_vmap_new (gint * buffer, gint width, gint height, gint depth, gint orientation)
36 LqrVMap * vmap;
38 TRY_N_N (vmap = g_try_new(LqrVMap, 1));
39 vmap->buffer = buffer;
40 vmap->width = width;
41 vmap->height = height;
42 vmap->orientation = orientation;
43 vmap->depth = depth;
44 return vmap;
47 LQR_PUBLIC
48 void
49 lqr_vmap_destroy (LqrVMap * vmap)
51 g_free (vmap->buffer);
52 g_free (vmap);
55 LQR_PUBLIC
56 gint *
57 lqr_vmap_get_data (LqrVMap *vmap)
59 return vmap->buffer;
62 LQR_PUBLIC
63 gint
64 lqr_vmap_get_width (LqrVMap *vmap)
66 return vmap->width;
69 LQR_PUBLIC
70 gint
71 lqr_vmap_get_height (LqrVMap *vmap)
73 return vmap->height;
76 LQR_PUBLIC
77 gint
78 lqr_vmap_get_depth (LqrVMap *vmap)
80 return vmap->depth;
83 LQR_PUBLIC
84 gint
85 lqr_vmap_get_orientation (LqrVMap *vmap)
87 return vmap->orientation;
90 /* dump the visibility level of the image */
91 LQR_PUBLIC
92 LqrVMap*
93 lqr_vmap_dump (LqrCarver * r)
95 LqrVMap * vmap;
96 gint w, h, w1, x, y, z0, vs;
97 gint * buffer;
98 gint depth;
100 /* save current size */
101 w1 = r->w;
103 /* temporarily set the size to the original */
104 lqr_carver_set_width (r, r->w_start);
106 w = lqr_carver_get_width (r);
107 h = lqr_carver_get_height (r);
108 depth = r->w0 - r->w_start;
110 TRY_N_N (buffer = g_try_new (gint, w * h));
112 lqr_cursor_reset (r->c);
113 for (y = 0; y < r->h; y++)
115 for (x = 0; x < r->w; x++)
117 vs = r->vs[r->c->now];
118 if (!r->transposed)
120 z0 = y * r->w + x;
122 else
124 z0 = x * r->h + y;
126 if (vs == 0)
128 buffer[z0] = 0;
130 else
132 buffer[z0] = vs - depth;
134 lqr_cursor_next (r->c);
138 /* recover size */
139 lqr_carver_set_width (r, w1);
140 lqr_cursor_reset (r->c);
142 TRY_N_N (vmap = lqr_vmap_new(buffer, w, h, depth, r->transposed));
144 return vmap;
149 /* dump the visibility level of the image */
150 LQR_PUBLIC
151 LqrRetVal
152 lqr_vmap_internal_dump (LqrCarver * r)
154 LqrVMap * vmap;
155 gint w, h, w1, x, y, z0, vs;
156 gint * buffer;
157 gint depth;
159 CATCH_CANC (r);
161 /* save current size */
162 w1 = r->w;
164 /* temporarily set the size to the original */
165 lqr_carver_set_width (r, r->w_start);
167 w = lqr_carver_get_width (r);
168 h = lqr_carver_get_height (r);
169 depth = r->w0 - r->w_start;
171 CATCH_MEM (buffer = g_try_new (gint, w * h));
173 lqr_cursor_reset (r->c);
174 for (y = 0; y < r->h; y++)
176 for (x = 0; x < r->w; x++)
178 vs = r->vs[r->c->now];
179 if (!r->transposed)
181 z0 = y * r->w + x;
183 else
185 z0 = x * r->h + y;
187 if (vs == 0)
189 buffer[z0] = 0;
191 else
193 buffer[z0] = vs - depth;
195 lqr_cursor_next (r->c);
199 /* recover size */
200 lqr_carver_set_width (r, w1);
201 lqr_cursor_reset (r->c);
203 CATCH_MEM (vmap = lqr_vmap_new(buffer, w, h, depth, r->transposed));
205 CATCH_MEM (r->flushed_vs = lqr_vmap_list_append(r->flushed_vs, vmap));
207 return LQR_OK;
211 LQR_PUBLIC
212 LqrRetVal
213 lqr_vmap_load (LqrCarver *r, LqrVMap *vmap)
215 gint w, h;
216 gint x, y, z0, z1;
218 w = vmap->width;
219 h = vmap->height;
221 CATCH_CANC (r);
222 CATCH_F (!r->active);
224 if (!r->transposed)
226 CATCH_F ((r->w_start == w ) && (r->h_start == h));
228 else
230 CATCH_F ((r->w_start == h ) && (r->h_start == w));
233 CATCH (lqr_carver_flatten(r));
235 if (vmap->orientation != r->transposed)
237 CATCH (lqr_carver_transpose (r));
240 for (y = 0; y < r->h; y++)
242 for (x = 0; x < r->w; x++)
244 if (!r->transposed)
246 z0 = y * r->w + x;
248 else
250 z0 = x * r->h + y;
252 z1 = y * r->w + x;
254 r->vs[z1] = vmap->buffer[z0];
258 CATCH (lqr_carver_inflate(r, vmap->depth));
260 lqr_cursor_reset (r->c);
262 lqr_carver_set_enl_step(r, 2.0);
264 return LQR_OK;