| 1 |
/* Libvisual-plugins - Standard plugins for libvisual |
| 2 |
* |
| 3 |
* Copyright (C) 2004, 2005, 2006 Dennis Smit <[email protected]> |
| 4 |
* |
| 5 |
* Authors: Dennis Smit <[email protected]> |
| 6 |
* |
| 7 |
* $Id: actor_gdkpixbuf.c,v 1.19 2006/01/27 20:19:15 synap Exp $ |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU Lesser General Public License as |
| 11 |
* published by the Free Software Foundation; either version 2.1 |
| 12 |
* of the License, or (at your option) any later version. |
| 13 |
* |
| 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 General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU Lesser General Public License |
| 20 |
* along with this program; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <config.h> |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <fcntl.h> |
| 30 |
#include <string.h> |
| 31 |
#include <gettext.h> |
| 32 |
|
| 33 |
#include <gdk/gdk.h> |
| 34 |
#include <libvisual/libvisual.h> |
| 35 |
|
| 36 |
typedef struct { |
| 37 |
GdkPixbuf *pixbuf; |
| 38 |
GdkPixbuf *scaled; |
| 39 |
VisVideo target; |
| 40 |
char *filename; |
| 41 |
int width; |
| 42 |
int height; |
| 43 |
|
| 44 |
/* Config flags, set through the param interface */ |
| 45 |
int set_scaled; |
| 46 |
int aspect; |
| 47 |
int center; |
| 48 |
int set_size; |
| 49 |
int set_width; |
| 50 |
int set_height; |
| 51 |
int x_offset; |
| 52 |
int y_offset; |
| 53 |
int interpolate; |
| 54 |
} PixbufPrivate; |
| 55 |
|
| 56 |
static int load_new_file (PixbufPrivate *priv, const char *filename); |
| 57 |
static int update_scaled_pixbuf (PixbufPrivate *priv); |
| 58 |
static int update_into_visvideo (PixbufPrivate *priv, GdkPixbuf *src); |
| 59 |
|
| 60 |
int act_gdkpixbuf_init (VisPluginData *plugin); |
| 61 |
int act_gdkpixbuf_cleanup (VisPluginData *plugin); |
| 62 |
int act_gdkpixbuf_requisition (VisPluginData *plugin, int *width, int *height); |
| 63 |
int act_gdkpixbuf_dimension (VisPluginData *plugin, VisVideo *video, int width, int height); |
| 64 |
int act_gdkpixbuf_events (VisPluginData *plugin, VisEventQueue *events); |
| 65 |
VisPalette *act_gdkpixbuf_palette (VisPluginData *plugin); |
| 66 |
int act_gdkpixbuf_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio); |
| 67 |
|
| 68 |
VISUAL_PLUGIN_API_VERSION_VALIDATOR |
| 69 |
|
| 70 |
const VisPluginInfo *get_plugin_info (int *count) |
| 71 |
{ |
| 72 |
static VisActorPlugin actor[] = {{ |
| 73 |
.requisition = act_gdkpixbuf_requisition, |
| 74 |
.palette = act_gdkpixbuf_palette, |
| 75 |
.render = act_gdkpixbuf_render, |
| 76 |
.vidoptions.depth = VISUAL_VIDEO_DEPTH_24BIT |
| 77 |
}}; |
| 78 |
|
| 79 |
static VisPluginInfo info[] = {{ |
| 80 |
.type = VISUAL_PLUGIN_TYPE_ACTOR, |
| 81 |
|
| 82 |
.plugname = "gdkpixbuf", |
| 83 |
.name = "GdkPixbuf image loader", |
| 84 |
.author = "Dennis Smit <[email protected]>", |
| 85 |
.version = "0.0.1", |
| 86 |
.about = N_("GdkPixbuf image loader for libvisual"), |
| 87 |
.help = N_("This plugin can be used to show images"), |
| 88 |
.license = VISUAL_PLUGIN_LICENSE_LGPL, |
| 89 |
|
| 90 |
.init = act_gdkpixbuf_init, |
| 91 |
.cleanup = act_gdkpixbuf_cleanup, |
| 92 |
.events = act_gdkpixbuf_events, |
| 93 |
|
| 94 |
.plugin = VISUAL_OBJECT (&actor[0]) |
| 95 |
}}; |
| 96 |
|
| 97 |
*count = sizeof (info) / sizeof (*info); |
| 98 |
|
| 99 |
return info; |
| 100 |
} |
| 101 |
|
| 102 |
int act_gdkpixbuf_init (VisPluginData *plugin) |
| 103 |
{ |
| 104 |
PixbufPrivate *priv; |
| 105 |
VisParamContainer *paramcontainer = visual_plugin_get_params (plugin);; |
| 106 |
|
| 107 |
static VisParamEntry params[] = { |
| 108 |
VISUAL_PARAM_LIST_ENTRY_STRING ("filename", ""), |
| 109 |
VISUAL_PARAM_LIST_ENTRY_INTEGER ("scaled", TRUE), |
| 110 |
VISUAL_PARAM_LIST_ENTRY_INTEGER ("aspect", FALSE), |
| 111 |
VISUAL_PARAM_LIST_ENTRY_INTEGER ("center", TRUE), |
| 112 |
VISUAL_PARAM_LIST_ENTRY_INTEGER ("set size", FALSE), |
| 113 |
VISUAL_PARAM_LIST_ENTRY_INTEGER ("width", 0), |
| 114 |
VISUAL_PARAM_LIST_ENTRY_INTEGER ("height", 0), |
| 115 |
VISUAL_PARAM_LIST_ENTRY_INTEGER ("x", 0), |
| 116 |
VISUAL_PARAM_LIST_ENTRY_INTEGER ("y", 0), |
| 117 |
VISUAL_PARAM_LIST_ENTRY_INTEGER ("interpolate", 0), |
| 118 |
VISUAL_PARAM_LIST_END |
| 119 |
}; |
| 120 |
|
| 121 |
#if ENABLE_NLS |
| 122 |
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 123 |
#endif |
| 124 |
|
| 125 |
priv = visual_mem_new0 (PixbufPrivate, 1); |
| 126 |
visual_object_set_private (VISUAL_OBJECT (plugin), priv); |
| 127 |
|
| 128 |
/* Initialize g_type, needed for GdkPixbuf */ |
| 129 |
g_type_init (); |
| 130 |
|
| 131 |
visual_param_container_add_many (paramcontainer, params); |
| 132 |
|
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
int act_gdkpixbuf_cleanup (VisPluginData *plugin) |
| 137 |
{ |
| 138 |
PixbufPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 139 |
|
| 140 |
if (priv->filename != NULL) |
| 141 |
free (priv->filename); |
| 142 |
|
| 143 |
if (priv->pixbuf != NULL) |
| 144 |
g_object_unref (priv->pixbuf); |
| 145 |
|
| 146 |
if (priv->scaled != NULL) |
| 147 |
g_object_unref (priv->scaled); |
| 148 |
|
| 149 |
if (visual_video_get_pixels (&priv->target) != NULL) |
| 150 |
visual_video_free_buffer (&priv->target); |
| 151 |
|
| 152 |
visual_mem_free (priv); |
| 153 |
|
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
int act_gdkpixbuf_requisition (VisPluginData *plugin, int *width, int *height) |
| 158 |
{ |
| 159 |
int reqw = *width; |
| 160 |
int reqh = *height; |
| 161 |
|
| 162 |
if (reqw < 1) |
| 163 |
reqw = 1; |
| 164 |
|
| 165 |
if (reqh < 1) |
| 166 |
reqh = 1; |
| 167 |
|
| 168 |
*width = reqw; |
| 169 |
*height = reqh; |
| 170 |
|
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
int act_gdkpixbuf_dimension (VisPluginData *plugin, VisVideo *video, int width, int height) |
| 175 |
{ |
| 176 |
PixbufPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 177 |
|
| 178 |
visual_video_set_dimension (video, width, height); |
| 179 |
|
| 180 |
priv->width = width; |
| 181 |
priv->height = height; |
| 182 |
|
| 183 |
if (priv->pixbuf != NULL) |
| 184 |
update_scaled_pixbuf (priv); |
| 185 |
else { |
| 186 |
/* If there is no image reset the VisVideo pixels, just to be sure */ |
| 187 |
if (visual_video_get_pixels (&priv->target) != NULL) |
| 188 |
visual_video_free_buffer (&priv->target); |
| 189 |
|
| 190 |
visual_video_set_buffer (&priv->target, NULL); |
| 191 |
} |
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
int act_gdkpixbuf_events (VisPluginData *plugin, VisEventQueue *events) |
| 196 |
{ |
| 197 |
PixbufPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 198 |
VisParamEntry *param; |
| 199 |
VisEvent ev; |
| 200 |
|
| 201 |
while (visual_event_queue_poll (events, &ev)) { |
| 202 |
switch (ev.type) { |
| 203 |
case VISUAL_EVENT_RESIZE: |
| 204 |
act_gdkpixbuf_dimension (plugin, ev.event.resize.video, |
| 205 |
ev.event.resize.width, ev.event.resize.height); |
| 206 |
break; |
| 207 |
|
| 208 |
case VISUAL_EVENT_PARAM: |
| 209 |
param = ev.event.param.param; |
| 210 |
|
| 211 |
if (visual_param_entry_is (param, "filename")) { |
| 212 |
visual_log (VISUAL_LOG_DEBUG, "New file to be loaded: %s\n", |
| 213 |
visual_param_entry_get_string (param)); |
| 214 |
|
| 215 |
load_new_file (priv, visual_param_entry_get_string (param)); |
| 216 |
|
| 217 |
} else if (visual_param_entry_is (param, "scaled")) { |
| 218 |
priv->set_scaled = visual_param_entry_get_integer (param); |
| 219 |
|
| 220 |
update_scaled_pixbuf (priv); |
| 221 |
|
| 222 |
} else if (visual_param_entry_is (param, "aspect")) { |
| 223 |
priv->aspect = visual_param_entry_get_integer (param); |
| 224 |
|
| 225 |
update_scaled_pixbuf (priv); |
| 226 |
|
| 227 |
} else if (visual_param_entry_is (param, "center")) { |
| 228 |
priv->center = visual_param_entry_get_integer (param); |
| 229 |
|
| 230 |
} else if (visual_param_entry_is (param, "set size")) { |
| 231 |
priv->set_size = visual_param_entry_get_integer (param); |
| 232 |
|
| 233 |
update_scaled_pixbuf (priv); |
| 234 |
|
| 235 |
} else if (visual_param_entry_is (param, "width")) { |
| 236 |
priv->set_width = visual_param_entry_get_integer (param); |
| 237 |
|
| 238 |
update_scaled_pixbuf (priv); |
| 239 |
|
| 240 |
} else if (visual_param_entry_is (param, "height")) { |
| 241 |
priv->set_height = visual_param_entry_get_integer (param); |
| 242 |
|
| 243 |
update_scaled_pixbuf (priv); |
| 244 |
|
| 245 |
} else if (visual_param_entry_is (param, "x")) { |
| 246 |
priv->x_offset = visual_param_entry_get_integer (param); |
| 247 |
|
| 248 |
} else if (visual_param_entry_is (param, "y")) { |
| 249 |
priv->y_offset = visual_param_entry_get_integer (param); |
| 250 |
|
| 251 |
} else if (visual_param_entry_is (param, "interpolate")) { |
| 252 |
priv->interpolate = visual_param_entry_get_integer (param); |
| 253 |
|
| 254 |
update_scaled_pixbuf (priv); |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
default: /* to avoid warnings */ |
| 259 |
break; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
VisPalette *act_gdkpixbuf_palette (VisPluginData *plugin) |
| 267 |
{ |
| 268 |
return NULL; |
| 269 |
} |
| 270 |
|
| 271 |
int act_gdkpixbuf_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio) |
| 272 |
{ |
| 273 |
PixbufPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 274 |
|
| 275 |
if (visual_video_get_pixels (&priv->target) != NULL) { |
| 276 |
if (priv->center == TRUE) { |
| 277 |
int xoff, yoff; |
| 278 |
|
| 279 |
xoff = (video->width - priv->target.width) / 2; |
| 280 |
yoff = (video->height - priv->target.height) / 2; |
| 281 |
|
| 282 |
visual_video_blit_overlay (video, &priv->target, xoff, yoff, FALSE); |
| 283 |
|
| 284 |
} else { |
| 285 |
visual_video_blit_overlay (video, &priv->target, priv->x_offset, priv->y_offset, FALSE); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
return 0; |
| 290 |
} |
| 291 |
|
| 292 |
static int load_new_file (PixbufPrivate *priv, const char *filename) |
| 293 |
{ |
| 294 |
if (priv->pixbuf != NULL) |
| 295 |
g_object_unref (priv->pixbuf); |
| 296 |
|
| 297 |
if (priv->scaled != NULL) |
| 298 |
g_object_unref (priv->scaled); |
| 299 |
|
| 300 |
if (priv->filename != NULL) |
| 301 |
free (priv->filename); |
| 302 |
|
| 303 |
priv->filename = strdup (filename); |
| 304 |
|
| 305 |
priv->pixbuf = gdk_pixbuf_new_from_file (filename, NULL); |
| 306 |
visual_log_return_val_if_fail (priv->pixbuf != NULL, -1); |
| 307 |
|
| 308 |
return update_scaled_pixbuf (priv); |
| 309 |
} |
| 310 |
|
| 311 |
static int update_scaled_pixbuf (PixbufPrivate *priv) |
| 312 |
{ |
| 313 |
GdkInterpType inter; |
| 314 |
|
| 315 |
if (priv->scaled != NULL) |
| 316 |
g_object_unref (priv->scaled); |
| 317 |
|
| 318 |
switch (priv->interpolate) { |
| 319 |
case 0: |
| 320 |
inter = GDK_INTERP_NEAREST; |
| 321 |
break; |
| 322 |
|
| 323 |
case 1: |
| 324 |
inter = GDK_INTERP_TILES; |
| 325 |
break; |
| 326 |
|
| 327 |
case 2: |
| 328 |
inter = GDK_INTERP_BILINEAR; |
| 329 |
break; |
| 330 |
|
| 331 |
case 3: |
| 332 |
inter = GDK_INTERP_HYPER; |
| 333 |
break; |
| 334 |
|
| 335 |
default: |
| 336 |
inter = GDK_INTERP_NEAREST; |
| 337 |
break; |
| 338 |
} |
| 339 |
|
| 340 |
if (priv->set_scaled == TRUE) { |
| 341 |
if (priv->set_size == TRUE) { |
| 342 |
/* We want to allow this, but gdk_pixbuf does spit warnings, so we catch this */ |
| 343 |
if (priv->set_width == 0 || priv->set_height == 0) { |
| 344 |
visual_video_set_buffer (&priv->target, NULL); |
| 345 |
|
| 346 |
return 0; |
| 347 |
} |
| 348 |
|
| 349 |
priv->scaled = gdk_pixbuf_scale_simple (priv->pixbuf, priv->set_width, priv->set_height, inter); |
| 350 |
|
| 351 |
} else if (priv->aspect == TRUE) { |
| 352 |
int as_w, as_h; |
| 353 |
int rw, rh; |
| 354 |
|
| 355 |
rw = gdk_pixbuf_get_width (priv->pixbuf); |
| 356 |
rh = gdk_pixbuf_get_height (priv->pixbuf); |
| 357 |
|
| 358 |
/* Determine which dimension we need to create aspect */ |
| 359 |
if ((priv->width - rw) > (priv->height - rh)) { |
| 360 |
as_h = priv->height; |
| 361 |
as_w = rw * ((float) as_h / rh); |
| 362 |
} else { |
| 363 |
as_w = priv->width; |
| 364 |
as_h = rh * ((float) as_w / rw); |
| 365 |
} |
| 366 |
|
| 367 |
priv->scaled = gdk_pixbuf_scale_simple (priv->pixbuf, as_w, as_h, inter); |
| 368 |
|
| 369 |
} else { |
| 370 |
priv->scaled = gdk_pixbuf_scale_simple (priv->pixbuf, priv->width, priv->height, inter); |
| 371 |
} |
| 372 |
|
| 373 |
visual_log_return_val_if_fail (priv->scaled != NULL, -1); |
| 374 |
|
| 375 |
update_into_visvideo (priv, priv->scaled); |
| 376 |
|
| 377 |
} else { |
| 378 |
visual_log_return_val_if_fail (priv->pixbuf != NULL, -1); |
| 379 |
|
| 380 |
update_into_visvideo (priv, priv->pixbuf); |
| 381 |
|
| 382 |
} |
| 383 |
|
| 384 |
return 0; |
| 385 |
} |
| 386 |
|
| 387 |
static int update_into_visvideo (PixbufPrivate *priv, GdkPixbuf *src) |
| 388 |
{ |
| 389 |
VisVideo *target; |
| 390 |
VisVideo bgr; |
| 391 |
|
| 392 |
target = &priv->target; |
| 393 |
|
| 394 |
/* Create a VisVideo from the pixbuf */ |
| 395 |
visual_video_set_depth (&bgr, |
| 396 |
visual_video_depth_enum_from_value (gdk_pixbuf_get_n_channels (src) * 8)); |
| 397 |
visual_video_set_dimension (&bgr, gdk_pixbuf_get_width (src), gdk_pixbuf_get_height (src)); |
| 398 |
visual_video_set_pitch (&bgr, gdk_pixbuf_get_rowstride (src)); |
| 399 |
visual_video_set_buffer (&bgr, gdk_pixbuf_get_pixels (src)); |
| 400 |
|
| 401 |
if (visual_video_get_pixels (target) != NULL) |
| 402 |
visual_video_free_buffer (target); |
| 403 |
|
| 404 |
visual_video_clone (target, &bgr); |
| 405 |
visual_video_allocate_buffer (target); |
| 406 |
|
| 407 |
/* Gdk uses a different color order than we do */ |
| 408 |
visual_video_color_bgr_to_rgb (target, &bgr); |
| 409 |
|
| 410 |
return 0; |
| 411 |
} |
| 412 |
|