alien-map.c 6.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
 *
16 17 18 19
 * Original authors:
 *
 *      Daniel Cotting <cotting@mygale.org>
 *      Martin Weber   <martweb@gmx.net>
20 21 22 23 24 25
 */

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

26
#ifdef GEGL_PROPERTIES
27

28
enum_start (gegl_alien_map_color_model)
29 30
  enum_value (GEGL_ALIEN_MAP_COLOR_MODEL_RGB, "rgb", N_("RGB"))
  enum_value (GEGL_ALIEN_MAP_COLOR_MODEL_HSL, "hsl", N_("HSL"))
31 32 33 34 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
enum_end (GeglAlienMapColorModel)

property_enum (color_model, _("Color model"),
               GeglAlienMapColorModel, gegl_alien_map_color_model,
               GEGL_ALIEN_MAP_COLOR_MODEL_RGB)
  description (_("What color model used for the transformation"))

property_double (cpn_1_frequency, _("Component 1 frequency"), 1)
  value_range (0, 20)

property_double  (cpn_2_frequency, _("Component 2 frequency"), 1)
  value_range (0, 20)

property_double  (cpn_3_frequency, _("Component 3 frequency"), 1)
  value_range (0, 20)

property_double  (cpn_1_phaseshift, _("Component 1 phase shift"), 0)
  value_range (0, 360)
  ui_meta     ("unit", "degree")
property_double  (cpn_2_phaseshift, _("Component 2 phase shift"), 0)
  value_range (0, 360)
  ui_meta     ("unit", "degree")
property_double  (cpn_3_phaseshift, _("Component 3 phase shift"), 0)
  value_range (0, 360)
  ui_meta     ("unit", "degree")

property_boolean (cpn_1_keep, _("Keep component 1"), FALSE)
property_boolean (cpn_2_keep, _("Keep component 2"), FALSE)
property_boolean (cpn_3_keep, _("Keep component 3"), FALSE)
60 61 62

#else

63 64
#define GEGL_OP_POINT_FILTER
#define GEGL_OP_C_FILE "alien-map.c"
65

66
#include "gegl-op.h"
67 68 69 70

static void
prepare (GeglOperation *operation)
{
71
  GeglProperties *o = GEGL_PROPERTIES (operation);
72

73
  if (o->color_model == GEGL_ALIEN_MAP_COLOR_MODEL_RGB)
74
    {
75 76 77 78
      gegl_operation_set_format (operation, "input",
                                 babl_format ("R'G'B'A float"));
      gegl_operation_set_format (operation, "output",
                                 babl_format ("R'G'B'A float"));
79 80 81
    }
  else
    {
82 83 84 85
      gegl_operation_set_format (operation, "input",
                                 babl_format ("HSLA float"));
      gegl_operation_set_format (operation, "output",
                                 babl_format ("HSLA float"));
86 87 88 89 90 91 92 93 94 95 96
    }
}

static gboolean
process (GeglOperation       *op,
         void                *in_buf,
         void                *out_buf,
         glong                samples,
         const GeglRectangle *roi,
         gint                 level)
{
97
  GeglProperties *o = GEGL_PROPERTIES (op);
98 99 100 101 102
  gfloat     *in  = in_buf;
  gfloat     *out = out_buf;
  gfloat      freq[3];
  gfloat      phaseshift[3];
  gboolean    keep[3];
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

  freq[0] = o->cpn_1_frequency * G_PI;
  freq[1] = o->cpn_2_frequency * G_PI;
  freq[2] = o->cpn_3_frequency * G_PI;

  phaseshift[0] = G_PI * o->cpn_1_phaseshift / 180.0;
  phaseshift[1] = G_PI * o->cpn_2_phaseshift / 180.0;
  phaseshift[2] = G_PI * o->cpn_3_phaseshift / 180.0;

  keep[0] = o->cpn_1_keep;
  keep[1] = o->cpn_2_keep;
  keep[2] = o->cpn_3_keep;

  while (samples--)
    {
      gint i;
      for (i = 0; i < 3; i++)
        {
          out[i] = keep[i] ?
            in[i] :
            0.5 * (1.0 + sin ((2 * in[i] - 1.0) * freq[i] + phaseshift[i]));
        }

      out[3] = in[3];

      in  += 4;
      out += 4;
    }

  return TRUE;
}

135 136 137
#include "opencl/gegl-cl.h"
#include "opencl/alien-map.cl.h"

138
static GeglClRunData *cl_data = NULL;
139 140 141 142 143 144 145 146 147

static gboolean
cl_process (GeglOperation       *operation,
            cl_mem              in,
            cl_mem              out,
            size_t              global_worksize,
            const GeglRectangle *roi,
            gint                level)
{
148
  GeglProperties *o = GEGL_PROPERTIES (operation);
149 150 151
  cl_float3   freq;
  cl_float3   phaseshift;
  cl_int3     keep;
152
  cl_int      cl_err = 0;
153

154 155 156 157 158 159 160 161 162
  if (!cl_data)
    {
      const char *kernel_name[] = {"cl_alien_map",
                                   NULL};
      cl_data = gegl_cl_compile_and_build (alien_map_cl_source, kernel_name);
    }

  if (!cl_data)
    return TRUE;
163 164 165 166 167 168 169 170 171 172 173 174 175

  freq.s[0] = o->cpn_1_frequency * G_PI;
  freq.s[1] = o->cpn_2_frequency * G_PI;
  freq.s[2] = o->cpn_3_frequency * G_PI;

  phaseshift.s[0] = G_PI * o->cpn_1_phaseshift / 180.0;
  phaseshift.s[1] = G_PI * o->cpn_2_phaseshift / 180.0;
  phaseshift.s[2] = G_PI * o->cpn_3_phaseshift / 180.0;

  keep.s[0] = (cl_int)o->cpn_1_keep;
  keep.s[1] = (cl_int)o->cpn_2_keep;
  keep.s[2] = (cl_int)o->cpn_3_keep;

176 177 178 179 180 181 182
  cl_err = gegl_cl_set_kernel_args (cl_data->kernel[0],
                                    sizeof(cl_mem),    &in,
                                    sizeof(cl_mem),    &out,
                                    sizeof(cl_float3), &freq,
                                    sizeof(cl_float3), &phaseshift,
                                    sizeof(cl_int3),   &keep,
                                    NULL);
183 184 185 186 187 188
  CL_CHECK;

  cl_err = gegl_clEnqueueNDRangeKernel (gegl_cl_get_command_queue (),
                                        cl_data->kernel[0], 1,
                                        NULL, &global_worksize, NULL,
                                        0, NULL, NULL);
189 190 191 192 193 194 195 196
  CL_CHECK;

  return  FALSE;

error:
  return TRUE;
}

197
static void
198
gegl_op_class_init (GeglOpClass *klass)
199 200 201 202 203 204 205
{
  GeglOperationClass            *operation_class;
  GeglOperationPointFilterClass *point_filter_class;

  operation_class    = GEGL_OPERATION_CLASS (klass);
  point_filter_class = GEGL_OPERATION_POINT_FILTER_CLASS (klass);

206 207 208 209
  operation_class->prepare        = prepare;
  operation_class->opencl_support = TRUE;
  point_filter_class->process     = process;
  point_filter_class->cl_process  = cl_process;
210 211

  gegl_operation_class_set_keys (operation_class,
212
    "name",        "gegl:alien-map",
213
    "title",       _("Alien Map"),
214
    "categories",  "artistic",
215
    "description", _("Heavily distort images colors by applying trigonometric functions to map color values."),
216 217 218 219
    NULL);
}

#endif