enlarge.c 8.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* This file is an image processing operation for GEGL
 *
 * GEGL is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * GEGL 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
14
 * License along with GEGL; if not, see <https://www.gnu.org/licenses/>.
15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Copyright 2018 Øyvind Kolås <pippin@gimp.org>
 *
 */

#include <stdio.h>
#include "config.h"
#include <glib/gi18n-lib.h>

#ifdef GEGL_PROPERTIES

/* most of these should go away - here for ease of algorithm experimentation */

Øyvind "pippin" Kolås's avatar
Øyvind "pippin" Kolås committed
28
property_int (seek_distance, "seek radius", 128)
29 30
  value_range (4, 512)

Øyvind "pippin" Kolås's avatar
Øyvind "pippin" Kolås committed
31 32 33
property_int (max_k, "max k", 4)
  value_range (1, 4)

34
property_double (scale, "scale", 2.0)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  value_range (0.01, 16.0)

#else

#define GEGL_OP_FILTER
#define GEGL_OP_NAME      enlarge
#define GEGL_OP_C_SOURCE  enlarge.c

#include "gegl-op.h"
#include <stdio.h>
#include "pixel-duster.h"

static GeglRectangle
get_required_for_output (GeglOperation       *operation,
                         const gchar         *input_pad,
                         const GeglRectangle *roi)
{
  GeglRectangle result = *gegl_operation_source_get_bounding_box (operation, "input");
  if (gegl_rectangle_is_infinite_plane (&result))
    return *roi;
  return result;
}

static void
prepare (GeglOperation *operation)
{
  const Babl *format = babl_format ("RGBA float");

  gegl_operation_set_format (operation, "input",  format);
  gegl_operation_set_format (operation, "output", format);
}

67 68
static void scaled_copy (PixelDuster *duster,
                         GeglBuffer *in,
69
                         GeglBuffer *out,
70
                         gfloat      scale)
71 72 73 74 75 76 77 78 79
{
  GeglRectangle rect;
  const Babl *format = babl_format ("RGBA float");
  gint x, y;

  rect = *gegl_buffer_get_extent (out);
  for (y = 0; y < rect.height; y++)
    for (x = 0; x < rect.width; x++)
      {
80
        float rgba[4];
81
        GeglRectangle r = {x, y, 1, 1};
82 83
        gegl_sampler_get (duster->in_sampler_f, x / scale, y / scale, NULL,
                          &rgba[0], 0);
84 85 86 87 88 89 90 91 92 93
        gegl_buffer_set (out, &r, 0, format, &rgba[0], 0);
      }
}

static void improve (PixelDuster *duster,
                     GeglBuffer *in,
                     GeglBuffer *out,
                     gfloat      scale)
{
  GeglRectangle rect;
94
  const Babl *format = babl_format ("RGBA float");
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  gint x, y;

  rect = *gegl_buffer_get_extent (out);
  for (y = 0; y < rect.height; y++)
  {
    int xstart = 0;
    int xinc = 1;
    int xend = rect.width;
#if 0 /* consistent scanline direction produces more consistent results */
    if (y%2)
     {
       xstart = rect.width - 1;
       xinc = -1;
       xend = -1;
     }
#endif
    for (x = xstart; x != xend; x+=xinc)
      {
        Probe *probe;
        probe = add_probe (duster, x, y);
        if (probe_improve (duster, probe) == 0)
        {
117 118 119 120
#if PIXDUST_REL_DIGEST==0
          gfloat rgba[4*MAX_K];

          for (int j = 0; j < MAX_K; j++)
121 122
            gegl_sampler_get (duster->in_sampler_f, probe->source_x[j],
                              probe->source_y[j], NULL, &rgba[j*4], 0);
123 124 125 126 127 128 129 130 131 132 133 134


          for (int j = 1; j < probe->k; j++)
            for (int c = 0; c < 4; c++)
            {
              rgba[0+c] += rgba[j*4+c];
            }
          for (int c = 0; c < 4; c++)
            {
              rgba[c] /= probe->k;
            }

135
          if (rgba[3] <= 0.01)
136 137
            fprintf (stderr, "eek %i,%i %f %f %f %f\n", probe->source_x[MAX_K/2], probe->source_y[MAX_K/2], rgba[0], rgba[1], rgba[2], rgba[3]);

138
          gegl_buffer_set (duster->output, GEGL_RECTANGLE(probe->target_x, probe->target_y, 1, 1), 0, format, &rgba[0], 0);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
#else
          gfloat rgba[4];
          gfloat delta[4]={0,0,0,0};
          {
            int dx = 0, dy = 0;
            duster_idx_to_x_y (duster, 1, probe->hay[0][0], &dx, &dy);
            gegl_buffer_sample (duster->output, probe->target_x + dx, probe->target_y + dy,  NULL, &rgba[0], format, GEGL_SAMPLER_NEAREST, 0);
          }

          for (int k = 0; k < probe->k; k++)
          {
            for (int c = 0; c < 3; c ++)
              delta[c] += ((probe->hay[k][4+c]-127.0) / 128);
          }

          for (int c = 0; c < 3; c ++)
            rgba[c] = rgba[c] - delta[c] / probe->k;
          rgba[3]=1.0;

          gegl_buffer_set (duster->output, GEGL_RECTANGLE(probe->target_x, probe->target_y, 1, 1), 0, format, &rgba[0], 0);

160
#endif
161
        }
162
        g_hash_table_remove (duster->probes_ht, xy2offset(probe->target_x, probe->target_y));
163
      }
164 165 166
   fprintf (stderr, "\r%2.2f   ", y * 100.0 / rect.height);
  }
   fprintf (stderr, "\n");
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
}

static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
         GeglBuffer          *output,
         const GeglRectangle *result,
         gint                 level)
{
  GeglProperties *o      = GEGL_PROPERTIES (operation);
  GeglRectangle in_rect = *gegl_buffer_get_extent (input);
  GeglRectangle out_rect = *gegl_buffer_get_extent (output);
  PixelDuster    *duster;
  duster  = pixel_duster_new (input, output,
                              &in_rect, &out_rect,
                              o->seek_distance,
Øyvind "pippin" Kolås's avatar
Øyvind "pippin" Kolås committed
183
                              o->max_k,
184
                              1, 1, 1.0, 0.3,
185 186 187
                              o->scale,
                              o->scale,
                              NULL);
188
  scaled_copy (duster, input, output, o->scale);
189 190
  seed_db (duster);
  improve (duster, input, output, o->scale);
191 192 193 194 195 196 197 198 199
  pixel_duster_destroy (duster);

  return TRUE;
}

static GeglRectangle
get_bounding_box (GeglOperation *operation)
{
  GeglProperties *o      = GEGL_PROPERTIES (operation);
200 201 202 203
  GeglRectangle *res = gegl_operation_source_get_bounding_box (operation, "input");
  GeglRectangle result = {0,0,100,100};
  if (res)
    result = *res;
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  result.x = 0;
  result.y = 0;
  result.width  *= o->scale;
  result.height *= o->scale;

  return result;
}

static GeglRectangle
get_cached_region (GeglOperation       *operation,
                   const GeglRectangle *roi)
{
  GeglProperties *o      = GEGL_PROPERTIES (operation);
  GeglRectangle result = *gegl_operation_source_get_bounding_box (operation, "input");

  if (gegl_rectangle_is_infinite_plane (&result))
    return *roi;
  result.x = 0;
  result.y = 0;
  result.width  *= o->scale;
  result.height *= o->scale;

  return result;
}

static gboolean
operation_process (GeglOperation        *operation,
                   GeglOperationContext *context,
                   const gchar          *output_prop,
                   const GeglRectangle  *result,
                   gint                  level)
{
  GeglOperationClass  *operation_class;

  const GeglRectangle *in_rect =
    gegl_operation_source_get_bounding_box (operation, "input");

  operation_class = GEGL_OPERATION_CLASS (gegl_op_parent_class);

  if (in_rect && gegl_rectangle_is_infinite_plane (in_rect))
    {
      gpointer in = gegl_operation_context_get_object (context, "input");
      gegl_operation_context_take_object (context, "output",
                                          g_object_ref (G_OBJECT (in)));
      return TRUE;
    }

  return operation_class->process (operation, context, output_prop, result,
                                   gegl_operation_context_get_level (context));
}

static void
gegl_op_class_init (GeglOpClass *klass)
{
  GeglOperationClass       *operation_class;
  GeglOperationFilterClass *filter_class;

  operation_class = GEGL_OPERATION_CLASS (klass);
  filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);

  filter_class->process                    = process;
  operation_class->prepare                 = prepare;
  operation_class->process                 = operation_process;
  operation_class->get_bounding_box        = get_bounding_box;
  operation_class->get_required_for_output = get_required_for_output;
  operation_class->get_cached_region       = get_cached_region;
  operation_class->opencl_support          = FALSE;
  operation_class->threaded                = FALSE;

  gegl_operation_class_set_keys (operation_class,
      "name",        "gegl:enlarge",
      "title",       "Smart enlarge",
      "categories",  "heal",
      "description", "Enlarges an images based on pixel contents",
      NULL);
}

#endif