mrg-ui.c 53.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* This file is part of GEGL editor -- an mrg frontend for GEGL
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
16
 * Copyright (C) 2015 Øyvind Kolås pippin@gimp.org
17 18
 */

19
/* The code in this file is an image viewer/editor written using microraptor
20
 * gui and GEGL. It renders the UI directly from GEGLs data structures. 
21 22
 */

23 24 25 26 27 28 29
#define _BSD_SOURCE
#define _DEFAULT_SOURCE

#include "config.h"

#if HAVE_MRG

30
#include <ctype.h>
31
#include <string.h>
32 33
#include <sys/types.h>
#include <sys/stat.h>
34 35 36 37 38 39 40 41
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <mrg.h>
#include <gegl.h>
#include <gexiv2/gexiv2.h>
#include <gegl-paramspecs.h>
42
#include <SDL.h>
43
#include <gegl-audio-fragment.h>
44

45 46 47 48 49 50 51 52 53
/* comment this out, and things render more correctly but much slower
 * for images larger than your screen/window resolution
 */
#define USE_MIPMAPS    1

/* set this to 1 to print the active gegl chain
 */
#define DEBUG_OP_LIST  0

54 55 56 57

static int audio_len   = 0;
static int audio_pos   = 0;
static int audio_post   = 0;
58
//static int audio_start = 0; /* which sample no is at the start of our circular buffer */
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

#define AUDIO_BUF_LEN 819200000

int16_t audio_data[AUDIO_BUF_LEN];

static void sdl_audio_cb(void *udata, Uint8 *stream, int len)
{
  int audio_remaining = audio_len - audio_pos;
  if (audio_remaining < 0)
    return;

  if (audio_remaining < len) len = audio_remaining;

  //SDL_MixAudio(stream, (uint8_t*)&audio_data[audio_pos/2], len, SDL_MIX_MAXVOLUME);
  memcpy (stream, (uint8_t*)&audio_data[audio_pos/2], len);
  audio_pos += len;
  audio_post += len;
  if (audio_pos >= AUDIO_BUF_LEN)
  {
    audio_pos = 0;
  }
}

static void sdl_add_audio_sample (int sample_pos, float left, float right)
{
84 85
   audio_data[audio_len/2 + 0] = left * 32767.0 * 0.46;
   audio_data[audio_len/2 + 1] = right * 32767.0 * 0.46;
86 87 88 89 90 91 92 93 94 95 96
   audio_len += 4;

   if (audio_len >= AUDIO_BUF_LEN)
   {
     audio_len = 0;
   }
}

static int audio_started = 0;


97 98 99
/*  this structure contains the full application state, and is what
 *  re-renderings of the UI is directly based on.
 */
100 101 102 103 104 105
typedef struct _State State;
struct _State {
  void      (*ui) (Mrg *mrg, void *state);
  Mrg        *mrg;

  char       *path;
106
  char       *save_path;
107

108 109
  GList      *paths;

110 111 112 113 114 115 116
  GeglBuffer *buffer;
  GeglNode   *gegl;
  GeglNode   *sink;
  GeglNode   *source;
  GeglNode   *load;
  GeglNode   *save;
  GeglNode   *active;
117 118

  GeglNode   *rotate;
119 120 121 122 123 124 125 126
  int         rev;
  float       u, v;
  float       scale;
  int         show_actions;
  int         show_controls;

  float       render_quality;
  float       preview_quality;
127 128

  int         controls_timeout;
129 130

  char      **ops; // the operations part of the commandline, if any
131 132 133 134

  float       slide_pause;
  int         slide_enabled;
  int         slide_timeout;
135 136 137 138 139 140 141

  GeglNode   *gegl_decode;
  GeglNode   *decode_load;
  GeglNode   *decode_store;

  int         is_video;
  int         frame_no;
142 143

  int         prev_frame_played;
144 145
};

146 147 148 149 150 151 152

typedef struct ActionData {
  const char *label;
  float priority; /* XXX: not yey used, should force scale/crop to happen early */
  const char *op_name;
} ActionData;

153
/* white-list of operations useful for improving/altering photos
154 155 156 157 158 159 160 161 162 163 164 165 166 167
 */
ActionData actions[]={
  {"rotate",            10,  "gegl:rotate"},
  {"crop",              20,  "gegl:crop"},
  {"color temperature", 50,  "gegl:color-temperature"},
  {"exposure",          60,  "gegl:exposure"},
  //{"levels",            60,  "gegl:levels"},
  //{"threshold",         70,  "gegl:threshold"},
  {NULL, 0, NULL}, /* sentinel */
};

static char *suffix = "-gegl";


168 169
void   gegl_meta_set (const char *path, const char *meta_data);
char * gegl_meta_get (const char *path);
170
GExiv2Orientation path_get_orientation (const char *path);
171

172 173
static char *suffix_path (const char *path);
static char *unsuffix_path (const char *path);
174
static int is_gegl_path (const char *path);
175

176
static void contrasty_stroke (cairo_t *cr);
177

178 179 180 181 182 183 184
static void mrg_gegl_blit (Mrg *mrg,
			   float x0, float y0,
			   float width, float height,
			   GeglNode *node,
			   float u, float v,
			   float scale,
                           float preview_multiplier);
185

186 187 188 189 190 191 192 193 194
gchar *get_thumb_path (const char *path);
gchar *get_thumb_path (const char *path)
{
  gchar *ret;
  gchar *uri = g_strdup_printf ("file://%s", path);
  gchar *hex = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1);
  int i;  
  for (i = 0; hex[i]; i++)
    hex[i] = tolower (hex[i]);
195 196 197 198 199 200
  ret = g_strdup_printf ("%s/.cache/thumbnails/large/%s.png", g_get_home_dir(), hex);
  if (access (ret, F_OK) == -1)
  {
    g_free (ret);
    ret = g_strdup_printf ("%s/.cache/thumbnails/normal/%s.png", g_get_home_dir(), hex);
  }
201 202 203 204 205 206
  g_free (uri);
  g_free (hex);
  return ret;
}


207
static void load_path (State *o);
208

209 210
static void go_next (State *o);
static void go_prev (State *o);
211
static void go_parent (State *o);
212

213
static void get_coords (State *o, float screen_x, float screen_y, float *gegl_x, float *gegl_y);
214 215 216 217 218
static void leave_editor (State *o);
static void drag_preview (MrgEvent *e);
static void load_into_buffer (State *o, const char *path);
static GeglNode *locate_node (State *o, const char *op_name);
static void zoom_to_fit (State *o);
219
static void center (State *o);
220
static void zoom_to_fit_buffer (State *o);
221

222 223 224 225 226
static void go_next_cb   (MrgEvent *event, void *data1, void *data2);
static void go_prev_cb   (MrgEvent *event, void *data1, void *data2);
static void go_parent_cb (MrgEvent *event, void *data1, void *data2);
static void zoom_fit_cb  (MrgEvent *e, void *data1, void *data2);
static void pan_left_cb  (MrgEvent *event, void *data1, void *data2);
227
static void pan_right_cb (MrgEvent *event, void *data1, void *data2);
228 229
static void pan_down_cb  (MrgEvent *event, void *data1, void *data2);
static void pan_up_cb    (MrgEvent *event, void *data1, void *data2);
230 231
static void preview_more_cb (MrgEvent *event, void *data1, void *data2);
static void preview_less_cb (MrgEvent *event, void *data1, void *data2);
232 233 234 235 236 237 238 239 240 241
static void zoom_1_cb               (MrgEvent *event, void *data1, void *data2);
static void zoom_in_cb              (MrgEvent *event, void *data1, void *data2);
static void zoom_out_cb             (MrgEvent *event, void *data1, void *data2);
static void toggle_actions_cb       (MrgEvent *event, void *data1, void *data2);
static void toggle_fullscreen_cb    (MrgEvent *event, void *data1, void *data2);
static void activate_op_cb          (MrgEvent *event, void *data1, void *data2);
static void disable_filter_cb       (MrgEvent *event, void *data1, void *data2);
static void apply_filter_cb         (MrgEvent *event, void *data1, void *data2);
static void discard_cb              (MrgEvent *event, void *data1, void *data2);
static void save_cb                 (MrgEvent *event, void *data1, void *data2);
242
static void toggle_show_controls_cb (MrgEvent *event, void *data1, void *data2);
243

244
static void gegl_ui       (Mrg *mrg, void *data);
245
int         mrg_ui_main   (int argc, char **argv, char **ops);
246 247
void        gegl_meta_set (const char *path, const char *meta_data);
char *      gegl_meta_get (const char *path); 
248

249

250
static void on_viewer_motion (MrgEvent *e, void *data1, void *data2);
251

252 253 254 255 256 257 258 259 260 261 262 263
static int str_has_image_suffix (char *path)
{
  return g_str_has_suffix (path, ".jpg") ||
         g_str_has_suffix (path, ".png") ||
         g_str_has_suffix (path, ".JPG") ||
         g_str_has_suffix (path, ".PNG") ||
         g_str_has_suffix (path, ".jpeg") ||
         g_str_has_suffix (path, ".JPEG") ||
         g_str_has_suffix (path, ".CR2") ||
         g_str_has_suffix (path, ".exr");
}

264 265 266 267 268
static int str_has_video_suffix (char *path)
{
  return g_str_has_suffix (path, ".avi") ||
         g_str_has_suffix (path, ".AVI") ||
         g_str_has_suffix (path, ".mp4") ||
269 270 271 272 273
         g_str_has_suffix (path, ".mp3") ||
         g_str_has_suffix (path, ".mpg") ||
         g_str_has_suffix (path, ".ogv") ||
         g_str_has_suffix (path, ".MPG") ||
         g_str_has_suffix (path, ".webm") ||
274 275
         g_str_has_suffix (path, ".MP4") ||
         g_str_has_suffix (path, ".mkv") ||
276 277
         g_str_has_suffix (path, ".gif") ||
         g_str_has_suffix (path, ".GIF") ||
278 279 280 281 282 283 284 285 286 287
         g_str_has_suffix (path, ".MKV") ||
         g_str_has_suffix (path, ".mov") ||
         g_str_has_suffix (path, ".ogg");
}

static int str_has_visual_suffix (char *path)
{
  return str_has_image_suffix (path) || str_has_video_suffix (path);
}

288
static void populate_path_list (State *o)
289 290 291 292 293 294 295 296 297
{
  struct dirent **namelist;
  int i;
  struct stat stat_buf;
  char *path = strdup (o->path);
  int n;
  while (o->paths)
    {
      char *freed = o->paths->data;
298
      o->paths = g_list_remove (o->paths, freed);
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
      g_free (freed);
    }

  lstat (o->path, &stat_buf);
  if (S_ISREG (stat_buf.st_mode))
  {
    char *lastslash = strrchr (path, '/');
    if (lastslash)
      {
	if (lastslash == path)
	  lastslash[1] = '\0';
	else
	  lastslash[0] = '\0';
      }
  }
  n = scandir (path, &namelist, NULL, alphasort);
  
  for (i = 0; i < n; i++)
  {
318
    if (namelist[i]->d_name[0] != '.' &&
319
        str_has_visual_suffix (namelist[i]->d_name))
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    {
      gchar *fpath = g_strdup_printf ("%s/%s", path, namelist[i]->d_name);

      lstat (fpath, &stat_buf);
      if (S_ISREG (stat_buf.st_mode))
      {
        if (is_gegl_path (fpath))
        {
          char *tmp = unsuffix_path (fpath);
          g_free (fpath);
          fpath = g_strdup (tmp);
          free (tmp);
        }
        if (!g_list_find_custom (o->paths, fpath, (void*)g_strcmp0))
        {
          o->paths = g_list_append (o->paths, fpath);
        }
      }
    }
  }
  free (namelist);
}

343
static State *hack_state = NULL;  // XXX: this shoudl be factored away
344 345 346

char **ops = NULL;

347

348
static void open_audio (int frequency)
349
{
350
  SDL_AudioSpec spec = {0};
351
  SDL_Init(SDL_INIT_AUDIO);
352 353 354
  spec.freq = frequency;
  spec.format = AUDIO_S16SYS;
  spec.channels = 2;
355
  spec.samples = 1024;
356 357 358 359
  spec.callback = sdl_audio_cb;
  SDL_OpenAudio(&spec, 0);

  if (spec.format != AUDIO_S16SYS)
360 361 362
   {
      fprintf (stderr, "not getting format we wanted\n");
   }
363
  if (spec.freq != frequency)
364
   {
365
      fprintf (stderr, "not getting desires samplerate(%i) we wanted got %i instead\n", frequency, spec.freq);
366 367 368 369 370 371 372 373
   }
}

static void end_audio (void)
{
  
}

374
int mrg_ui_main (int argc, char **argv, char **ops)
375
{
376 377
  Mrg *mrg = mrg_new (1024, 768, NULL);
  State o = {NULL,};
378

379
#ifdef USE_MIPMAPS
380 381 382 383
  /* to use this UI comfortably, mipmap rendering needs to be enabled, there are
   * still a few glitches, but for basic full frame un-panned, un-cropped use it
   * already works well.
   */
384 385
  g_setenv ("GEGL_MIPMAP_RENDERING", "1", TRUE);
#endif
386 387

/* we want to see the speed gotten if the fastest babl conversions we have were more accurate */
388
  g_setenv ("BABL_TOLERANCE", "0.1", TRUE);
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
  
  if(ops)
    o.ops = ops;
  else
  {
    int i;
    for (i = 0; argv[i]; i++)
    {
      if (!strcmp (argv[i], "--"))
      {
        o.ops = &argv[i];
        break;
      }
    }
  }
404

405
  gegl_init (&argc, &argv);
406
  o.gegl            = gegl_node_new (); // so that we have an object to unref
407 408 409 410
  o.mrg             = mrg;
  o.scale           = 1.0;
  o.render_quality  = 1.0;
  o.preview_quality = 2.0;
411 412
  o.slide_pause     = 5.0;
  o.slide_enabled   = 0;
413

414
  if (access (argv[1], F_OK) != -1)
415
    o.path = realpath (argv[1], NULL);
416 417 418 419 420
  else
    {
      printf ("usage: %s <full-path-to-image>\n", argv[0]);
      return -1;
    }
421

422

423 424 425
  load_path (&o);
  mrg_set_ui (mrg, gegl_ui, &o);
  hack_state = &o;  
426
  on_viewer_motion (NULL, &o, NULL);
427
  mrg_main (mrg);
428

429 430 431 432 433 434 435
  g_object_unref (o.gegl);
  if (o.buffer)
  {
    g_object_unref (o.buffer);
    o.buffer = NULL;
  }
  gegl_exit ();
436 437

  end_audio ();
438
  return 0;
439 440
}

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
static int hide_controls_cb (Mrg *mrg, void *data)
{
  State *o = data;
  o->controls_timeout = 0;
  o->show_controls = 0;
  mrg_queue_draw (o->mrg, NULL);
  return 0;
}

static void on_viewer_motion (MrgEvent *e, void *data1, void *data2)
{
  State *o = data1;
  {
    if (!o->show_controls)
    {
      o->show_controls = 1;
      mrg_queue_draw (o->mrg, NULL);
    }
    if (o->controls_timeout)
    {
      mrg_remove_idle (o->mrg, o->controls_timeout);
      o->controls_timeout = 0;
    }
    o->controls_timeout = mrg_add_timeout (o->mrg, 1000, hide_controls_cb, o);
  }
}

468
static void on_pan_drag (MrgEvent *e, void *data1, void *data2)
469
{
470 471 472 473 474 475 476 477 478
  State *o = data1;
  if (e->type == MRG_DRAG_MOTION)
  {
    o->u -= (e->delta_x );
    o->v -= (e->delta_y );
    mrg_queue_draw (e->mrg, NULL);
  }
  drag_preview (e);
}
479

480 481 482 483 484 485 486
static void prop_double_drag_cb (MrgEvent *e, void *data1, void *data2)
{
  GeglNode *node = data1;
  GParamSpec *pspec = data2;
  GeglParamSpecDouble *gspec = data2;
  gdouble value = 0.0;
  float range = gspec->ui_maximum - gspec->ui_minimum;
487

488 489 490 491 492
  value = e->x / mrg_width (e->mrg);
  value = value * range + gspec->ui_minimum;
  gegl_node_set (node, pspec->name, value, NULL);
   
  drag_preview (e);
493

494
  mrg_queue_draw (e->mrg, NULL);
495 496
}

497
static void draw_gegl_generic (State *state, Mrg *mrg, cairo_t *cr, GeglNode *node)
498
{
499 500 501 502 503
  const gchar* op_name = gegl_node_get_operation (node);
  mrg_set_edge_left (mrg, mrg_width (mrg) * 0.1);
  mrg_set_edge_top  (mrg, mrg_height (mrg) * 0.1);
  mrg_set_font_size (mrg, mrg_height (mrg) * 0.07);
  mrg_set_style (mrg, "color:white; background-color: transparent");
504

505 506 507 508 509
  cairo_save (cr);
  mrg_printf (mrg, "%s\n", op_name);
  {
    guint n_props;
    GParamSpec **pspecs = gegl_operation_list_properties (op_name, &n_props);
510

511 512 513 514
    if (pspecs)
    {
      int tot_pos = 0;
      int pos_no = 0;
515

516 517 518 519 520 521 522 523
      for (gint i = 0; i < n_props; i++)
      {
        if (g_type_is_a (pspecs[i]->value_type, G_TYPE_DOUBLE) ||
            g_type_is_a (pspecs[i]->value_type, G_TYPE_INT) ||
            g_type_is_a (pspecs[i]->value_type, G_TYPE_STRING) ||
            g_type_is_a (pspecs[i]->value_type, G_TYPE_BOOLEAN))
          tot_pos ++;
      }
524

525 526 527
      for (gint i = 0; i < n_props; i++)
      {
        mrg_set_xy (mrg, mrg_em(mrg), mrg_height (mrg) - mrg_em (mrg) * ((tot_pos-pos_no)));
528

529 530 531 532 533 534
        if (g_type_is_a (pspecs[i]->value_type, G_TYPE_DOUBLE))
        {
          float xpos;
          GeglParamSpecDouble *geglspec = (void*)pspecs[i];
          gdouble value;
          gegl_node_get (node, pspecs[i]->name, &value, NULL);
535

536 537 538 539
          cairo_rectangle (cr, 0,
             mrg_height (mrg) - mrg_em (mrg) * ((tot_pos-pos_no+1)),
             mrg_width (mrg), mrg_em(mrg));
          cairo_set_source_rgba (cr, 0,0,0, 0.5);
540

541
          mrg_listen (mrg, MRG_DRAG, prop_double_drag_cb, node,(void*)pspecs[i]);
542

543 544 545 546 547 548 549
          cairo_fill (cr);
          xpos = (value - geglspec->ui_minimum) / (geglspec->ui_maximum - geglspec->ui_minimum);
          cairo_rectangle (cr, xpos * mrg_width(mrg) - mrg_em(mrg)/4,
              mrg_height (mrg) - mrg_em (mrg) * ((tot_pos-pos_no+1)),
              mrg_em(mrg)/2, mrg_em(mrg));
          cairo_set_source_rgba (cr, 1,1,1, 0.5);
          cairo_fill (cr);
550

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
          mrg_printf (mrg, "%s:%f\n", pspecs[i]->name, value);
          pos_no ++;
        }
        else if (g_type_is_a (pspecs[i]->value_type, G_TYPE_INT))
        {
          gint value;
          gegl_node_get (node, pspecs[i]->name, &value, NULL);
          mrg_printf (mrg, "%s:%i\n", pspecs[i]->name, value);
          pos_no ++;
        }
        else if (g_type_is_a (pspecs[i]->value_type, G_TYPE_STRING))
        {
          char *value = NULL;
          gegl_node_get (node, pspecs[i]->name, &value, NULL);
          pos_no ++;
          mrg_printf (mrg, "%s:%s\n", pspecs[i]->name, value);
          if (value) g_free (value);
        }
        else if (g_type_is_a (pspecs[i]->value_type, G_TYPE_BOOLEAN))
        {
          gboolean value = FALSE;
          gegl_node_get (node, pspecs[i]->name, &value, NULL);
          pos_no ++;
          mrg_printf (mrg, "%s:%i\n", pspecs[i]->name, value);
        }
        
      }
      g_free (pspecs);
    }
  }
581

582 583
  cairo_restore (cr);
  mrg_set_style (mrg, "color:yellow; background-color: transparent");
584 585
}

586
static void crop_drag_ul (MrgEvent *e, void *data1, void *data2)
587
{
588 589 590 591 592
  GeglNode *node = data1;
  double x,y,width,height;
  double x0, y0, x1, y1;
  gegl_node_get (node, "x", &x, "y", &y, "width", &width, "height", &height, NULL);
  x0 = x; y0 = y; x1 = x0 + width; y1 = y + height;
593

594 595 596 597
  if (e->type == MRG_DRAG_MOTION)
  {
    x0 += e->delta_x;
    y0 += e->delta_y;
598

599 600 601 602 603 604 605 606 607 608
    x=x0;
    y=y0;
    width = x1 - x0;
    height = y1 - y0;
    gegl_node_set (node, "x", x, "y", y, "width", width, "height", height, NULL);

    mrg_queue_draw (e->mrg, NULL);
  }

  drag_preview (e);
609 610
}

611
static void crop_drag_lr (MrgEvent *e, void *data1, void *data2)
612
{
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
  GeglNode *node = data1;
  double x,y,width,height;
  double x0, y0, x1, y1;
  gegl_node_get (node, "x", &x, "y", &y, "width", &width, "height", &height, NULL);
  x0 = x; y0 = y; x1 = x0 + width; y1 = y + height;

  if (e->type == MRG_DRAG_MOTION)
  {
    x1 += e->delta_x;
    y1 += e->delta_y;

    x=x0;
    y=y0;
    width = x1 - x0;
    height = y1 - y0;
    gegl_node_set (node, "x", x, "y", y, "width", width, "height", height, NULL);

    mrg_queue_draw (e->mrg, NULL);
  }

  drag_preview (e);
634 635
}

636
static void crop_drag_rotate (MrgEvent *e, void *data1, void *data2)
637
{
638 639 640 641 642 643 644 645 646 647 648 649 650 651
  State *o = hack_state;
  double degrees;
  gegl_node_get (o->rotate, "degrees", &degrees, NULL);

  if (e->type == MRG_DRAG_MOTION)
  {
    degrees += e->delta_x / 100.0;

    gegl_node_set (o->rotate, "degrees", degrees, NULL);

    mrg_queue_draw (e->mrg, NULL);
  }

  drag_preview (e);
652 653
}

654
static void draw_gegl_crop (State *o, Mrg *mrg, cairo_t *cr, GeglNode *node)
655
{
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
  const gchar* op_name = gegl_node_get_operation (node);
  float dim = mrg_height (mrg) * 0.1 / o->scale;
  double x,y,width,height;
  double x0, y0, x1, y1;

  mrg_set_edge_left (mrg, mrg_width (mrg) * 0.1);
  mrg_set_edge_top  (mrg, mrg_height (mrg) * 0.1);
  mrg_set_font_size (mrg, mrg_height (mrg) * 0.07);
  mrg_set_style (mrg, "color:white; background-color: transparent");

  cairo_save (cr);
  mrg_printf (mrg, "%s\n", op_name);

  cairo_translate (cr, -o->u, -o->v);
  cairo_scale (cr, o->scale, o->scale);

  gegl_node_get (node, "x", &x, "y", &y, "width", &width, "height", &height, NULL);
  x0 = x; y0 = y; x1 = x0 + width; y1 = y + height;

  cairo_rectangle (cr, x0, y0, dim, dim);
  mrg_listen (mrg, MRG_DRAG, crop_drag_ul, node, NULL);
  contrasty_stroke (cr);

  cairo_rectangle (cr, x1-dim, y1-dim, dim, dim);
  mrg_listen (mrg, MRG_DRAG, crop_drag_lr, node, NULL);
  contrasty_stroke (cr);

  cairo_rectangle (cr, x0+dim, y0+dim, width-dim-dim, height-dim-dim);
  mrg_listen (mrg, MRG_DRAG, crop_drag_rotate, node, NULL);
  cairo_new_path (cr);

  cairo_restore (cr);
  mrg_set_style (mrg, "color:yellow; background-color: transparent");
689 690
}

691
static void ui_op_draw_apply_disable (State *o)
692
{
693 694 695 696 697 698 699 700 701 702
  Mrg *mrg = o->mrg;
  mrg_set_font_size (mrg, mrg_height (mrg) * 0.1);
  mrg_set_xy (mrg, 0, mrg_em(mrg));
  mrg_text_listen (mrg, MRG_PRESS, disable_filter_cb, o, NULL);
  mrg_printf (mrg, "X");
  mrg_text_listen_done (mrg);
  mrg_set_xy (mrg, mrg_width(mrg) - mrg_em (mrg), mrg_em(mrg));
  mrg_text_listen (mrg, MRG_PRESS, apply_filter_cb, o, NULL);
  mrg_printf (mrg, "O");
  mrg_text_listen_done (mrg);
703 704
}

705
static void ui_active_op (State *o)
706
{
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
  Mrg *mrg = o->mrg;
  cairo_t *cr = mrg_cr (mrg);
  char *opname = NULL;
  g_object_get (o->active, "operation", &opname, NULL);

  if (!strcmp (opname, "gegl:crop")) {
    zoom_to_fit_buffer (o);
    draw_gegl_crop (o, mrg, cr, o->active);
    ui_op_draw_apply_disable (o);
  }
  else
  {
    draw_gegl_generic (o, mrg, cr, o->active);
    ui_op_draw_apply_disable (o);
  }
722 723
}

724
#if DEBUG_OP_LIST
725
static void ui_debug_op_chain (State *o)
726
{
727 728 729 730
  Mrg *mrg = o->mrg;
  GeglNode *iter;
  mrg_set_edge_top  (mrg, mrg_height (mrg) * 0.2);
  iter = o->sink;
731 732 733 734
  while (iter)
   {
     char *opname = NULL;
     g_object_get (iter, "operation", &opname, NULL);
735 736 737 738 739 740
     if (iter == o->active)
       mrg_printf (mrg, "[%s]", opname);
     else
       mrg_printf (mrg, "%s", opname);
     mrg_printf (mrg, "\n");

741 742 743 744
     g_free (opname);
     iter = gegl_node_get_producer (iter, "input", NULL);
   }
}
745
#endif
746

747
static void ui_actions (State *o)
748
{
749 750 751 752 753 754
  Mrg *mrg = o->mrg;
  int i;
  cairo_t *cr = mrg_cr (mrg);
  mrg_set_edge_left (mrg, mrg_width (mrg) * 0.25);
  mrg_set_edge_top  (mrg, mrg_height (mrg) * 0.1);
  mrg_set_font_size (mrg, mrg_height (mrg) * 0.08);
755

756
  for (i = 0; actions[i].label; i ++)
757
    {
758 759 760 761 762
      mrg_text_listen (mrg, MRG_PRESS, activate_op_cb, o, &actions[i]);
      if (locate_node (o, actions[i].op_name))
        mrg_printf (mrg, "-");
      mrg_printf (mrg, "%s\n", actions[i].label);
      mrg_text_listen_done (mrg);
763
    }
764 765 766 767 768 769 770 771 772 773

  mrg_print (mrg, "\n");
  mrg_printf (mrg, "existing\n");
  mrg_text_listen (mrg, MRG_PRESS, discard_cb, o, NULL);
  mrg_printf (mrg, "discard\n");
  mrg_text_listen_done (mrg);
  cairo_scale (cr, mrg_width(mrg), mrg_height(mrg));
  cairo_new_path (cr);
  cairo_arc (cr, 0.9, 0.1, 0.1, 0.0, G_PI * 2);
  contrasty_stroke (cr);
774 775 776
  cairo_rectangle (cr, 0.8, 0.0, 0.2, 0.2); 
  mrg_listen (mrg, MRG_PRESS, toggle_actions_cb, o, NULL);
  cairo_new_path (cr);
777 778
}

779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
static void dir_pgdn_cb (MrgEvent *event, void *data1, void *data2)
{
  State *o = data1;
  o->u -= mrg_width (o->mrg) * 0.6; 
  mrg_queue_draw (o->mrg, NULL);
  mrg_event_stop_propagate (event);
}

static void dir_pgup_cb (MrgEvent *event, void *data1, void *data2)
{
  State *o = data1;
  o->u += mrg_width (o->mrg) * 0.6; 
  mrg_queue_draw (o->mrg, NULL);
  mrg_event_stop_propagate (event);
}

static void entry_pressed (MrgEvent *event, void *data1, void *data2)
{
797 798 799 800 801
  State *o = data1;
  free (o->path);
  o->path = strdup (data2);
  load_path (o);
  mrg_queue_draw (event->mrg, NULL);
802 803 804 805 806 807
}

static void ui_dir_viewer (State *o)
{
  Mrg *mrg = o->mrg;
  cairo_t *cr = mrg_cr (mrg);
808
  GList *iter;
809 810
  float x = 0;
  float y = 0;
811
  float dim = mrg_height (mrg) * 0.25;
812 813 814 815 816 817 818 819 820

  cairo_rectangle (cr, 0,0, mrg_width(mrg), mrg_height(mrg));
  mrg_listen (mrg, MRG_MOTION, on_viewer_motion, o, NULL);
  cairo_new_path (cr);

  mrg_set_edge_right (mrg, 4095);
  cairo_save (cr);
  cairo_translate (cr, o->u, 0);//o->v);

821
  for (iter = o->paths; iter; iter=iter->next)
822 823
  {
      int w, h;
824
      gchar *path = iter->data;
825 826 827 828 829 830 831 832 833 834 835
      char *lastslash = strrchr (path, '/');

      gchar *p2 = suffix_path (path);

      gchar *thumbpath = get_thumb_path (p2);
      free (p2);
      if (access (thumbpath, F_OK) == -1)
      {
        g_free (thumbpath);
        thumbpath = get_thumb_path (path);
      }
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
  
      if (
         access (thumbpath, F_OK) != -1 && //XXX: query image should suffice
         mrg_query_image (mrg, thumbpath, &w, &h))
      {
        float wdim = dim;
        float hdim = dim;
        if (w > h)
          hdim = dim / (1.0 * w / h);
        else
          wdim = dim * (1.0 * w / h);

        mrg_image (mrg, x + (dim-wdim)/2, y + (dim-hdim)/2, wdim, hdim, thumbpath);
      }
      g_free (thumbpath);

      mrg_set_xy (mrg, x, y + dim - mrg_em(mrg));
853
      mrg_printf (mrg, "%s\n", lastslash+1);
854 855
      cairo_new_path (mrg_cr(mrg));
      cairo_rectangle (mrg_cr(mrg), x, y, dim, dim);
856
      mrg_listen_full (mrg, MRG_CLICK, entry_pressed, o, path, NULL, NULL);
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
      cairo_new_path (mrg_cr(mrg));

      y += dim;
      if (y+dim > mrg_height (mrg))
      {
        y = 0;
        x += dim;
      }
  }
  cairo_restore (cr);

  cairo_scale (cr, mrg_width(mrg), mrg_height(mrg));
  cairo_new_path (cr);
  cairo_move_to (cr, 0.2, 0.8);
  cairo_line_to (cr, 0.2, 1.0);
  cairo_line_to (cr, 0.0, 0.9);
  cairo_close_path (cr);
  if (o->show_controls)
    contrasty_stroke (cr);
  else
    cairo_new_path (cr);
  cairo_rectangle (cr, 0.0, 0.8, 0.2, 0.2); 
  mrg_listen (mrg, MRG_PRESS, dir_pgup_cb, o, NULL);

  cairo_new_path (cr);

  cairo_move_to (cr, 0.8, 0.8);
  cairo_line_to (cr, 0.8, 1.0);
  cairo_line_to (cr, 1.0, 0.9);
  cairo_close_path (cr);

  if (o->show_controls)
    contrasty_stroke (cr);
  else
    cairo_new_path (cr);
  cairo_rectangle (cr, 0.8, 0.8, 0.2, 0.2); 
  mrg_listen (mrg, MRG_PRESS, dir_pgdn_cb, o, NULL);
  cairo_new_path (cr);

  mrg_add_binding (mrg, "left", NULL, NULL, dir_pgup_cb, o);
  mrg_add_binding (mrg, "right", NULL, NULL, dir_pgdn_cb, o);
}

900 901 902 903 904 905 906 907
static int slide_cb (Mrg *mrg, void *data)
{
  State *o = data;
  o->slide_timeout = 0;
  go_next (data);
  return 0;
}

908 909 910 911 912 913
static void ui_viewer (State *o)
{
  Mrg *mrg = o->mrg;
  cairo_t *cr = mrg_cr (mrg);
  cairo_rectangle (cr, 0,0, mrg_width(mrg), mrg_height(mrg));
  mrg_listen (mrg, MRG_DRAG, on_pan_drag, o, NULL);
914
  mrg_listen (mrg, MRG_MOTION, on_viewer_motion, o, NULL);
915
  cairo_scale (cr, mrg_width(mrg), mrg_height(mrg));
916 917 918 919 920 921 922 923 924 925 926 927 928 929
  cairo_new_path (cr);
  cairo_rectangle (cr, 0.05, 0.05, 0.05, 0.05);
  cairo_rectangle (cr, 0.15, 0.05, 0.05, 0.05);
  cairo_rectangle (cr, 0.05, 0.15, 0.05, 0.05);
  cairo_rectangle (cr, 0.15, 0.15, 0.05, 0.05);
  if (o->show_controls)
    contrasty_stroke (cr);
  else
    cairo_new_path (cr);
  cairo_rectangle (cr, 0.0, 0.0, 0.2, 0.2); 
  mrg_listen (mrg, MRG_PRESS, go_parent_cb, o, NULL);


  cairo_new_path (cr);
930 931 932 933 934 935
  cairo_move_to (cr, 0.2, 0.8);
  cairo_line_to (cr, 0.2, 1.0);
  cairo_line_to (cr, 0.0, 0.9);
  cairo_close_path (cr);
  if (o->show_controls)
    contrasty_stroke (cr);
936
  else
937
    cairo_new_path (cr);
938 939 940
  cairo_rectangle (cr, 0.0, 0.8, 0.2, 0.2); 
  mrg_listen (mrg, MRG_PRESS, go_prev_cb, o, NULL);
  cairo_new_path (cr);
941

942 943 944 945
  cairo_move_to (cr, 0.8, 0.8);
  cairo_line_to (cr, 0.8, 1.0);
  cairo_line_to (cr, 1.0, 0.9);
  cairo_close_path (cr);
946

947 948 949 950
  if (o->show_controls)
    contrasty_stroke (cr);
  else
    cairo_new_path (cr);
951 952 953
  cairo_rectangle (cr, 0.8, 0.8, 0.2, 0.2); 
  mrg_listen (mrg, MRG_PRESS, go_next_cb, o, NULL);
  cairo_new_path (cr);
954

955 956 957 958 959 960
  cairo_arc (cr, 0.9, 0.1, 0.1, 0.0, G_PI * 2);

  if (o->show_controls)
    contrasty_stroke (cr);
  else
    cairo_new_path (cr);
961 962 963
  cairo_rectangle (cr, 0.8, 0.0, 0.2, 0.2); 
  mrg_listen (mrg, MRG_PRESS, toggle_actions_cb, o, NULL);
  cairo_new_path (cr);
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980

  mrg_add_binding (mrg, "left", NULL, NULL,  pan_left_cb, o);
  mrg_add_binding (mrg, "right", NULL, NULL, pan_right_cb, o);
  mrg_add_binding (mrg, "up", NULL, NULL,    pan_up_cb, o);
  mrg_add_binding (mrg, "down", NULL, NULL,  pan_down_cb, o);
  mrg_add_binding (mrg, "+", NULL, NULL,     zoom_in_cb, o);
  mrg_add_binding (mrg, "=", NULL, NULL,     zoom_in_cb, o);
  mrg_add_binding (mrg, "-", NULL, NULL,     zoom_out_cb, o);
  mrg_add_binding (mrg, "1", NULL, NULL,     zoom_1_cb, o);
  mrg_add_binding (mrg, "m", NULL, NULL,     zoom_fit_cb, o);
  mrg_add_binding (mrg, "m", NULL, NULL,     zoom_fit_cb, o);
  mrg_add_binding (mrg, "x", NULL, NULL,     discard_cb, o);

  mrg_add_binding (mrg, "space", NULL, NULL,     go_next_cb , o);
  mrg_add_binding (mrg, "n", NULL, NULL,         go_next_cb, o);
  mrg_add_binding (mrg, "p", NULL, NULL,         go_prev_cb, o);
  mrg_add_binding (mrg, "backspace", NULL, NULL, go_prev_cb, o);
981 982 983 984 985 986

  if (o->slide_enabled && o->slide_timeout == 0)
  {
    o->slide_timeout = 
       mrg_add_timeout (o->mrg, o->slide_pause * 1000, slide_cb, o);
  }
987 988
}

989
static void toggle_show_controls_cb (MrgEvent *event, void *data1, void *data2)
990 991
{
  State *o = data1;
992
  o->show_controls = !o->show_controls;
993 994 995
  mrg_queue_draw (o->mrg, NULL);
}

996 997 998 999 1000 1001 1002 1003 1004 1005
static void toggle_slideshow_cb (MrgEvent *event, void *data1, void *data2)
{
  State *o = data1;
  o->slide_enabled = !o->slide_enabled;
  if (o->slide_timeout)
      mrg_remove_idle (o->mrg, o->slide_timeout);
  o->slide_timeout = 0;
  mrg_queue_draw (o->mrg, NULL);
}

1006
static void gegl_ui (Mrg *mrg, void *data)
1007
{
1008
  State *o = data;
1009 1010 1011 1012

  if (o->is_video)
   {
     o->frame_no++;
1013 1014 1015 1016 1017
     if (g_getenv ("GEGL_UI_DEBUG_SEEK"))
     {
       if ((o->frame_no / 200) % 2 == 1)
         o->frame_no+=600;
     }
1018
     fprintf (stderr, "\r%i", o->frame_no);
1019
     gegl_node_set (o->load, "frame", o->frame_no, NULL);
1020
     mrg_queue_draw (o->mrg, NULL);
1021 1022
   }

1023 1024 1025 1026 1027 1028 1029
  mrg_gegl_blit (mrg,
		 0, 0,
                 mrg_width (mrg), mrg_height (mrg),
		 o->sink,
		 o->u, o->v,
		 o->scale,
                 o->render_quality);
1030

1031
  if (o->is_video)
1032
  {
1033
    GeglAudioFragment *audio = NULL;
1034
    gdouble fps;
1035 1036
    gegl_node_get (o->load, "audio", &audio, "frame-rate", &fps, NULL);
    if (audio)
1037
    {
1038 1039
       int sample_count = gegl_audio_fragment_get_sample_count (audio);
       if (sample_count > 0)
1040
       {
1041 1042 1043
         int i;
         if (!audio_started)
         {
1044
  	   open_audio (gegl_audio_fragment_get_sample_rate (audio));
1045 1046 1047
           SDL_PauseAudio(0);
           audio_started = 1;
         }
1048
         for (i = 0; i < sample_count; i++)
1049
         {
1050
           sdl_add_audio_sample (0, audio->data[0][i], audio->data[1][i]);
1051 1052
         }

1053 1054
         while (audio_len > audio_pos + 5000)
           g_usleep (50);
1055
         o->prev_frame_played = o->frame_no;
1056
       }
1057
       g_object_unref (audio);
1058
    }
1059
  }
1060 1061
  
  if (o->show_controls)
1062
  {
1063 1064 1065 1066 1067
    mrg_printf (mrg, "%s\n", o->path);
#if DEBUG_OP_LIST
    ui_debug_op_chain (o);
#endif
  }
1068

1069 1070 1071 1072
  if (o->show_actions)
  {
    ui_actions (o);
    mrg_add_binding (mrg, "return", NULL, NULL, toggle_actions_cb, o);
1073
  }
1074 1075 1076 1077 1078 1079 1080 1081
  else if (o->active)
  {
    ui_active_op (o);
    mrg_add_binding (mrg, "return", NULL, NULL, apply_filter_cb, o);
    mrg_add_binding (mrg, "escape", NULL, NULL,     disable_filter_cb, o);
  }
  else
  {
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
    struct stat stat_buf;
    lstat (o->path, &stat_buf);
    if (S_ISREG (stat_buf.st_mode))
    {
      ui_viewer (o);

    }
    else if (S_ISDIR (stat_buf.st_mode))
    {
      ui_dir_viewer (o);
    }
    
    mrg_add_binding (mrg, "escape", NULL, NULL, go_parent_cb, o);
1095 1096 1097 1098 1099 1100 1101 1102
    mrg_add_binding (mrg, "return", NULL, NULL, toggle_actions_cb, o);
  }

  mrg_add_binding (mrg, "control-q", NULL, NULL, mrg_quit_cb, o);
  mrg_add_binding (mrg, "q", NULL, NULL,         mrg_quit_cb, o);
  mrg_add_binding (mrg, "f", NULL, NULL,         toggle_fullscreen_cb, o);
  mrg_add_binding (mrg, "F11", NULL, NULL,       toggle_fullscreen_cb, o);
  mrg_add_binding (mrg, "tab", NULL, NULL,       toggle_show_controls_cb, o);
1103
  mrg_add_binding (mrg, "s", NULL, NULL,       toggle_slideshow_cb, o);
1104 1105 1106

  mrg_add_binding (mrg, ",", NULL, NULL,         preview_less_cb, o);
  mrg_add_binding (mrg, ".", NULL, NULL,         preview_more_cb, o);
1107 1108
}

1109
/***********************************************/
1110

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
static char *get_path_parent (const char *path)
{
  char *ret = strdup (path);
  char *lastslash = strrchr (ret, '/');
  if (lastslash)
  {
    if (lastslash == ret)
      lastslash[1] = '\0';
    else
      lastslash[0] = '\0';
  }
  return ret;
}

1125 1126 1127
static char *suffix_path (const char *path)
{
  char *ret, *last_dot;
1128

1129 1130 1131 1132 1133 1134
  if (!path)
    return NULL;
  ret  = malloc (strlen (path) + strlen (suffix) + 3);
  strcpy (ret, path);
  last_dot = strrchr (ret, '.');
  if (last_dot)
1135
  {
1136 1137 1138
    char *extension = strdup (last_dot + 1);
    sprintf (last_dot, "%s.%s", suffix, extension);
   free (extension);
1139
  }
1140 1141 1142 1143 1144 1145
  else
  {
    sprintf (ret, "%s%s", path, suffix);
  }
  return ret;
}
1146

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
static char *unsuffix_path (const char *path)
{
  char *ret = NULL, *last_dot, *extension;
  char *suf;

  if (!path)
    return NULL;
  ret = malloc (strlen (path) + 4);
  strcpy (ret, path);
  last_dot = strrchr (ret, '.');
  extension = strdup (last_dot + 1);

  suf = strstr(ret, suffix);
  sprintf (suf, ".%s", extension);
  free (extension);
  return ret;
1163 1164
}

1165
static int is_gegl_path (const char *path)
1166
{
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
  if (strstr (path, suffix)) return 1;
  return 0;
}

static void contrasty_stroke (cairo_t *cr)
{
  double x0 = 6.0, y0 = 6.0;
  double x1 = 4.0, y1 = 4.0;

  cairo_device_to_user_distance (cr, &x0, &y0);
  cairo_device_to_user_distance (cr, &x1, &y1);
  cairo_set_source_rgba (cr, 0,0,0,0.5);
  cairo_set_line_width (cr, y0);
  cairo_stroke_preserve (cr);
  cairo_set_source_rgba (cr, 1,1,1,0.5);
  cairo_set_line_width (cr, y1);
  cairo_stroke (cr);
}

static unsigned char *copy_buf = NULL;
static int copy_buf_len = 0;

static void mrg_gegl_blit (Mrg *mrg,
			   float x0, float y0,
			   float width, float height,
			   GeglNode *node,
			   float u, float v,
			   float scale,
                           float preview_multiplier)
{
  float fake_factor = preview_multiplier;
  GeglRectangle bounds;

  cairo_t *cr = mrg_cr (mrg);
  cairo_surface_t *surface = NULL;

  if (!node)
    return;

  bounds = gegl_node_get_bounding_box (node);

  if (width == -1 && height == -1)
  {
    width  = bounds.width;
    height = bounds.height;
  }

  if (width == -1)
    width = bounds.width * height / bounds.height;
  if (height == -1)
    height = bounds.height * width / bounds.width;

  width /= fake_factor;
  height /= fake_factor;
  u /= fake_factor;
  v /= fake_factor;
 
  if (copy_buf_len < width * height * 4)
  {
    if (copy_buf)
      free (copy_buf);
    copy_buf_len = width * height * 4;
    copy_buf = malloc (copy_buf_len);
  }
  {
    static int foo = 0;
    unsigned char *buf = copy_buf;
    GeglRectangle roi = {u, v, width, height};
    static const Babl *fmt = NULL;

foo++;
    if (!fmt) fmt = babl_format ("cairo-RGB24");
    gegl_node_blit (node, scale / fake_factor, &roi, fmt, buf, width * 4, 
         GEGL_BLIT_DEFAULT);
  surface = cairo_image_surface_create_for_data (buf, CAIRO_FORMAT_RGB24, width, height, width * 4);
  }
1243 1244

  cairo_save (cr);
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
  cairo_surface_set_device_scale (surface, 1.0/fake_factor, 1.0/fake_factor);
  
  width *= fake_factor;
  height *= fake_factor;
  u *= fake_factor;
  v *= fake_factor;

  cairo_rectangle (cr, x0, y0, width, height);

  cairo_clip (cr);
  cairo_translate (cr, x0 * fake_factor, y0 * fake_factor);
  cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
  cairo_set_source_surface (cr, surface, 0, 0);
   
  cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
  cairo_paint (cr);
  cairo_surface_destroy (surface);
  cairo_restore (cr);
}

static void load_path (State *o)
{
  char *path;
  char *meta;
1269
  populate_path_list (o);
1270
  if (is_gegl_path (o->path))
1271
  {
1272 1273 1274 1275
    if (o->save_path)
      free (o->save_path);
    o->save_path = o->path;
    o->path = unsuffix_path (o->save_path);
1276 1277 1278
  }
  else
  {
1279 1280 1281
    if (o->save_path)
      free (o->save_path);
    o->save_path = suffix_path (o->path);
1282 1283
  }
  path  = o->path;
1284

1285 1286
  if (access (o->save_path, F_OK) != -1)
    path = o->save_path;
1287

1288 1289
  g_object_unref (o->gegl);
  o->gegl = NULL;
1290 1291 1292 1293 1294
  o->sink = NULL;
  o->source = NULL;
  o->rev = 0;
  o->u = 0;
  o->v = 0;
1295 1296
  o->is_video = 0;
  o->frame_no = 0;
1297
  o->prev_frame_played = 0;
1298

1299
  if (str_has_video_suffix (path))
1300
  {
1301
    o->is_video = 1;
1302 1303 1304 1305 1306 1307
    o->gegl = gegl_node_new ();
    o->sink = gegl_node_new_child (o->gegl,
                       "operation", "gegl:nop", NULL);
    o->source = gegl_node_new_child (o->gegl,
                       "operation", "gegl:nop", NULL);
    o->load = gegl_node_new_child (o->gegl,
1308
  			      "operation", "gegl:ff-load", "path", path, "frame", o->frame_no,
1309
			       NULL);
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
    gegl_node_link_many (o->load, o->source, o->sink, NULL);
  }
  else
  {
    meta = gegl_meta_get (path);
    if (meta)
    {
      GSList *nodes, *n;
      char *containing_path = get_path_parent (o->path);
      o->gegl = gegl_node_new_from_xml (meta, containing_path);
      free (containing_path);
      o->sink = gegl_node_new_child (o->gegl,
                       "operation", "gegl:nop", NULL);
      o->source = NULL;
      gegl_node_link_many (
        gegl_node_get_producer (o->gegl, "input", NULL), o->sink, NULL);
      nodes = gegl_node_get_children (o->gegl);
      for (n = nodes; n; n=n->next)
      {
        const char *op_name = gegl_node_get_operation (n->data);
        if (!strcmp (op_name, "gegl:load"))
        {
          GeglNode *load;
          gchar *path;
          gegl_node_get (n->data, "path", &path, NULL);
          load_into_buffer (o, path);
          gegl_node_set (n->data, "operation", "gegl:nop", NULL);
          o->source = n->data;
          load = gegl_node_new_child (o->gegl, "operation", "gegl:buffer-source",
                                               "buffer", o->buffer, NULL);
          gegl_node_link_many (load, o->source, NULL);
          g_free (path);
          break;
        }
      }
      o->save = gegl_node_new_child (o->gegl, "operation", "gegl:save",
                                              "path", path,
                                              NULL);
    }
    else
    {
      o->gegl = gegl_node_new ();
      o->sink = gegl_node_new_child (o->gegl,
                         "operation", "gegl:nop", NULL);
      o->source = gegl_node_new_child (o->gegl,
                         "operation", "gegl:nop", NULL);
      load_into_buffer (o, path);
      o->load = gegl_node_new_child (o->gegl,
                                     "operation", "gegl:buffer-source",
			             NULL);
      o->save = gegl_node_new_child (o->gegl,
                                     "operation", "gegl:save",
1362
  		     "path", o->save_path,
1363 1364 1365 1366
  		     NULL);
    gegl_node_link_many (o->load, o->source, o->sink, NULL);
    gegl_node_set (o->load, "buffer", o->buffer, NULL);
  }
1367
  }
1368 1369 1370 1371
  {
    struct stat stat_buf;
    lstat (o->path, &stat_buf);
    if (S_ISREG (stat_buf.st_mode))
1372 1373 1374 1375 1376 1377
    {
      if (o->is_video)
        center (o);
      else
        zoom_to_fit (o);
    }
1378
  }
1379

1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
  if (o->ops)
  {
    int i;
    for (i = 0; o->ops[i]; i++)
    {
      o->active = gegl_node_new_child (o->gegl,
	"operation", o->ops[i], NULL);

      gegl_node_link_many (gegl_node_get_producer (o->sink, "input", NULL),
				  o->active,
				  o->sink,
				  NULL);
    }
  }

1395 1396
  mrg_queue_draw (o->mrg, NULL);
}
1397

1398
static void go_parent (State *o)
1399 1400 1401 1402 1403 1404 1405 1406
{
  char *lastslash = strrchr (o->path, '/');
  if (lastslash)
  {
    if (lastslash == o->path)
      lastslash[1] = '\0';
    else
      lastslash[0] = '\0';
1407
    load_path (o);
1408
    mrg_queue_draw (o->mrg, NULL);
1409 1410 1411
  }
}

1412
static void go_next (State *o)
1413
{
1414 1415 1416
  GList *curr = g_list_find_custom (o->paths, o->path, (void*)g_strcmp0);

  if (curr && curr->next)
1417
  {
1418 1419
    free (o->path);
    o->path = strdup (curr->next->data);
1420 1421 1422 1423 1424
    load_path (o);
    mrg_queue_draw (o->mrg, NULL);
  }
}

1425
static void go_prev (State *o)
1426
{
1427
  GList *curr = g_list_find_custom (o->paths, o->path, (void*)g_strcmp0);
1428

1429 1430 1431 1432 1433 1434
  if (curr && curr->prev)
  {
    free (o->path);
    o->path = strdup (curr->prev->data);
    load_path (o);
    mrg_queue_draw (o->mrg, NULL);
1435 1436 1437
  }
}

1438
static void go_next_cb (MrgEvent *event, void *data1, void *data2)
1439
{
1440 1441 1442 1443 1444 1445 1446
  State *o = data1;
  if (o->rev)
    save_cb (event, data1, data2);
  go_next (data1);
  o->active = NULL;
  mrg_event_stop_propagate (event);
}
1447

1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
static void go_parent_cb (MrgEvent *event, void *data1, void *data2)
{
  State *o = data1;
  if (o->rev)
    save_cb (event, data1, data2);
  go_parent (data1);
  o->active = NULL;
  mrg_event_stop_propagate (event);
}

1458 1459 1460 1461 1462 1463 1464 1465 1466
static void go_prev_cb (MrgEvent *event, void *data1, void *data2)
{
  State *o = data1;
  if (o->rev)
    save_cb (event, data1, data2);
  go_prev (data1);
  o->active = NULL;
  mrg_event_stop_propagate (event);
}
1467

1468 1469 1470 1471 1472 1473
static void leave_editor (State *o)
{
  char *opname = NULL;
  g_object_get (o->active, "operation", &opname, NULL);
  if (!strcmp (opname, "gegl:crop"))
   {
1474
     zoom_to_fit (o);
1475 1476
   }
}
1477

1478 1479 1480 1481
static void drag_preview (MrgEvent *e)
{
  State *o = hack_state;
  static float old_factor = 1;
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
  switch (e->type)
  {
    case MRG_DRAG_PRESS:
      old_factor = o->render_quality;
      if (o->render_quality < o->preview_quality)
        o->render_quality = o->preview_quality;
      break;
    case MRG_DRAG_RELEASE:
      o->render_quality = old_factor;
      mrg_queue_draw (e->mrg, NULL);
      break;
    default:
    break;
  }
}

1498
static void load_into_buffer (State *o, const char *path)
1499
{
1500
  GeglNode *gegl, *load, *sink;
1501
  struct stat stat_buf;
1502

1503
  if (o->buffer)
1504
  {
1505 1506 1507
    g_object_unref (o->buffer);
    o->buffer = NULL;
  }
1508

1509 1510 1511 1512 1513
  lstat (path, &stat_buf);
  if (S_ISREG (stat_buf.st_mode))
  {


1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
  gegl = gegl_node_new ();
  load = gegl_node_new_child (gegl, "operation", "gegl:load",
                                    "path", path,
                                    NULL);
  sink = gegl_node_new_child (gegl, "operation", "gegl:buffer-sink",
                                    "buffer", &(o->buffer),
                                    NULL);
  gegl_node_link_many (load, sink, NULL);
  gegl_node_process (sink);
  g_object_unref (gegl);
1524

1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
  {
    GExiv2Orientation orientation = path_get_orientation (path);
    gboolean hflip = FALSE;
    gboolean vflip = FALSE;
    double degrees = 0.0;
    switch (orientation)
    {
      case GEXIV2_ORIENTATION_UNSPECIFIED:
      case GEXIV2_ORIENTATION_NORMAL:
        break;
      case GEXIV2_ORIENTATION_HFLIP: hflip=TRUE; break;
      case GEXIV2_ORIENTATION_VFLIP: vflip=TRUE; break;
      case GEXIV2_ORIENTATION_ROT_90: degrees = 90.0; break;
      case GEXIV2_ORIENTATION_ROT_90_HFLIP: degrees = 90.0; hflip=TRUE; break;
      case GEXIV2_ORIENTATION_ROT_90_VFLIP: degrees = 90.0; vflip=TRUE; break;
      case GEXIV2_ORIENTATION_ROT_180: degrees = 180.0; break;
      case GEXIV2_ORIENTATION_ROT_270: degrees = 270.0; break;
    }

    if (degrees != 0.0 || vflip || hflip)
     {
       /* XXX: deal with vflip/hflip */
       GeglBuffer *new_buffer = NULL;
       GeglNode *rotate;
       gegl = gegl_node_new ();
       load = gegl_node_new_child (gegl, "operation", "gegl:buffer-source",
                                   "buffer", o->buffer,
                                   NULL);
       sink = gegl_node_new_child (gegl, "operation", "gegl:buffer-sink",
                                   "buffer", &(new_buffer),
                                   NULL);
       rotate = gegl_node_new_child (gegl, "operation", "gegl:rotate",
                                   "degrees", -degrees,
                                   "sampler", GEGL_SAMPLER_NEAREST,
                                   NULL);
       gegl_node_link_many (load, rotate, sink, NULL);
       gegl_node_process (sink);
       g_object_unref (gegl);
       g_object_unref (o->buffer);
       o->buffer = new_buffer;
     }
  }

1568
#if 0 /* hack to see if having the data in some formats already is faster */
1569 1570
  {
  GeglBuffer *tempbuf;
1571 1572
  tempbuf = gegl_buffer_new (gegl_buffer_get_extent (o->buffer),
                                         babl_format ("RGBA float"));
1573

1574 1575 1576
  gegl_buffer_copy (o->buffer, NULL, GEGL_ABYSS_NONE, tempbuf, NULL);
  g_object_unref (o->buffer);
  o->buffer = tempbuf;
1577
  }
1578
#endif
1579 1580 1581 1582 1583 1584
    }
  else
    {
      GeglRectangle extent = {0,0,1,1}; /* segfaults with NULL / 0,0,0,0*/
      o->buffer = gegl_buffer_new (&extent, babl_format("R'G'B' u8"));
    }
1585 1586
}

1587
static GeglNode *locate_node (State *o, const char *op_name)
1588
{
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
  GeglNode *iter = o->sink;
  while (iter)
   {