gegl-algorithms.c 17.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* This file is part of GEGL.
 *
 * This library 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.
 *
 * This library 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 this library; if not, see <http://www.gnu.org/licenses/>.
 *
16
 * Copyright 2006,2007,2015 Øyvind Kolås <pippin@gimp.org>
17 18 19 20 21 22 23 24 25 26 27 28
 *           2013 Daniel Sabo
 */

#include "config.h"

#include <string.h>

#include <glib-object.h>

#include <babl/babl.h>

#include "gegl-types.h"
29
#include "gegl-types-internal.h"
30
#include "gegl-utils.h"
31 32 33 34
#include "gegl-algorithms.h"

#include <math.h>

35 36 37 38 39 40 41 42 43
static void
gegl_downscale_2x2_generic (const Babl *format,
                            gint        src_width,
                            gint        src_height,
                            guchar     *src_data,
                            gint        src_rowstride,
                            guchar     *dst_data,
                            gint        dst_rowstride);

44

45
GeglDownscale2x2Fun gegl_downscale_2x2_get_fun (const Babl *format)
46 47
{
  const Babl *comp_type = babl_format_get_type (format, 0);
48
  const Babl *model     = babl_format_get_model (format);
49

50
  if (gegl_babl_model_is_linear (model))
51
  {
52 53
    if (comp_type == gegl_babl_float())
    {
54
      return gegl_downscale_2x2_float;
55 56 57
    }
    else if (comp_type == gegl_babl_u8())
    {
58
      return gegl_downscale_2x2_u8;
59 60 61
    }
    else if (comp_type == gegl_babl_u16())
    {
62
      return gegl_downscale_2x2_u16;
63 64 65
    }
    else if (comp_type == gegl_babl_u32())
    {
66
      return gegl_downscale_2x2_u32;
67 68 69
    }
    else if (comp_type == gegl_babl_double())
    {
70
      return gegl_downscale_2x2_double;
71
    }
72
  }
73 74
  return gegl_downscale_2x2_generic;
}
75

76 77 78 79 80 81 82 83 84 85
void gegl_downscale_2x2 (const Babl *format,
                         gint        src_width,
                         gint        src_height,
                         guchar     *src_data,
                         gint        src_rowstride,
                         guchar     *dst_data,
                         gint        dst_rowstride)
{
  gegl_downscale_2x2_get_fun (format)(format, src_width, src_height,
                                              src_data, src_rowstride,
86
                                              dst_data, dst_rowstride);;
87 88
}

89 90 91
#include <stdio.h>
#define ALLOCA_THRESHOLD  8192 * 4   /* maybe this needs to be reduced for win32? */

92 93 94 95 96 97 98 99 100 101
static void
gegl_downscale_2x2_generic (const Babl *format,
                            gint        src_width,
                            gint        src_height,
                            guchar     *src_data,
                            gint        src_rowstride,
                            guchar     *dst_data,
                            gint        dst_rowstride)
{
  const Babl *tmp_format = gegl_babl_rgbA_linear_float ();
102
  const Babl *from_fish  = babl_fish (format, tmp_format);
103
  const Babl *to_fish    = babl_fish (tmp_format, format);
104 105 106 107 108
  const gint tmp_bpp     = 4 * 4;
  gint dst_width         = src_width / 2;
  gint dst_height        = src_height / 2;
  gint in_tmp_rowstride  = src_width * tmp_bpp;
  gint out_tmp_rowstride = dst_width * tmp_bpp;
109 110 111 112
  gint do_free = 0;

  void *in_tmp;
  void *out_tmp;
113

114 115 116 117 118 119 120 121 122 123 124
  if (src_height * in_tmp_rowstride + dst_height * out_tmp_rowstride < ALLOCA_THRESHOLD)
  {
    in_tmp = alloca (src_height * in_tmp_rowstride);
    out_tmp = alloca (dst_height * out_tmp_rowstride);
  }
  else
  {
    in_tmp = gegl_malloc (src_height * in_tmp_rowstride);
    out_tmp = gegl_malloc (dst_height * out_tmp_rowstride);
    do_free = 1;
  }
125

126 127 128 129
  babl_process_rows (from_fish,
                     src_data, src_rowstride,
                     in_tmp,   in_tmp_rowstride,
                     src_width, src_height);
130
  gegl_downscale_2x2_float (tmp_format, src_width, src_height,
131
                            in_tmp,  in_tmp_rowstride,
132
                            out_tmp, out_tmp_rowstride);
133
  babl_process_rows (to_fish,
134 135
                     out_tmp,   out_tmp_rowstride,
                     dst_data,  dst_rowstride,
136
                     dst_width, dst_height);
137 138 139 140 141 142

  if (do_free)
   {
     gegl_free (in_tmp);
     gegl_free (out_tmp);
   }
143 144 145
}

void
146 147 148 149 150 151 152
gegl_downscale_2x2_nearest (const Babl *format,
                            gint        src_width,
                            gint        src_height,
                            guchar     *src_data,
                            gint        src_rowstride,
                            guchar     *dst_data,
                            gint        dst_rowstride)
153
{
154
  gint bpp = babl_format_get_bytes_per_pixel (format);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  gint y;

  for (y = 0; y < src_height / 2; y++)
    {
      gint x;
      guchar *src = src_data;
      guchar *dst = dst_data;

      for (x = 0; x < src_width / 2; x++)
        {
          memcpy (dst, src, bpp);
          dst += bpp;
          src += bpp * 2;
        }

      dst_data += dst_rowstride;
      src_data += src_rowstride * 2;
    }
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
static void
gegl_resample_boxfilter_generic (guchar       *dest_buf,
                                 const guchar *source_buf,
                                 const GeglRectangle *dst_rect,
                                 const GeglRectangle *src_rect,
                                 gint  s_rowstride,
                                 gdouble scale,
                                 const Babl *format,
                                 gint d_rowstride)
{
  const Babl *tmp_format = gegl_babl_rgbA_linear_float ();
  const Babl *from_fish  = babl_fish (format, tmp_format);
  const Babl *to_fish    = babl_fish (tmp_format, format);

  const gint tmp_bpp     = 4 * 4;
  gint in_tmp_rowstride  = src_rect->width * tmp_bpp;
  gint out_tmp_rowstride = dst_rect->width * tmp_bpp;
192 193 194
  gint do_free = 0;

  guchar *in_tmp, *out_tmp;
195

196 197 198 199 200 201 202 203 204 205 206
  if (src_rect->height * in_tmp_rowstride + dst_rect->height * out_tmp_rowstride < ALLOCA_THRESHOLD)
  {
    in_tmp = alloca (src_rect->height * in_tmp_rowstride);
    out_tmp = alloca (dst_rect->height * out_tmp_rowstride);
  }
  else
  {
    in_tmp  = gegl_malloc (src_rect->height * in_tmp_rowstride);
    out_tmp = gegl_malloc (dst_rect->height * out_tmp_rowstride);
    do_free = 1;
  }
207 208 209 210 211 212

  babl_process_rows (from_fish,
                     source_buf, s_rowstride,
                     in_tmp, in_tmp_rowstride,
                     src_rect->width, src_rect->height);

213
  gegl_resample_boxfilter_float (out_tmp, in_tmp, dst_rect, src_rect,
214 215 216 217 218 219
                                 in_tmp_rowstride, scale, tmp_bpp, out_tmp_rowstride);

  babl_process_rows (to_fish,
                     out_tmp,  out_tmp_rowstride,
                     dest_buf, d_rowstride,
                     dst_rect->width, dst_rect->height);
220 221 222 223 224 225

  if (do_free)
    {
      gegl_free (in_tmp);
      gegl_free (out_tmp);
    }
226 227
}

228 229 230 231 232 233 234 235 236
void gegl_resample_boxfilter (guchar              *dest_buf,
                              const guchar        *source_buf,
                              const GeglRectangle *dst_rect,
                              const GeglRectangle *src_rect,
                              gint                 s_rowstride,
                              gdouble              scale,
                              const Babl          *format,
                              gint                 d_rowstride)
{
237
  const Babl *model     = babl_format_get_model (format);
238

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  if (gegl_babl_model_is_linear (model))
  {
    const Babl *comp_type  = babl_format_get_type (format, 0);
    const gint bpp = babl_format_get_bytes_per_pixel (format);

    if (comp_type == gegl_babl_float())
      gegl_resample_boxfilter_float (dest_buf, source_buf, dst_rect, src_rect,
                                     s_rowstride, scale, bpp, d_rowstride);
    else if (comp_type == gegl_babl_u8())
      gegl_resample_boxfilter_u8 (dest_buf, source_buf, dst_rect, src_rect,
                                  s_rowstride, scale, bpp, d_rowstride);
    else if (comp_type == gegl_babl_u16())
      gegl_resample_boxfilter_u16 (dest_buf, source_buf, dst_rect, src_rect,
                                   s_rowstride, scale, bpp, d_rowstride);
    else if (comp_type == gegl_babl_u32())
      gegl_resample_boxfilter_u32 (dest_buf, source_buf, dst_rect, src_rect,
255
                                   s_rowstride, scale, bpp, d_rowstride);
256 257 258 259 260 261 262
    else if (comp_type == gegl_babl_double())
      gegl_resample_boxfilter_double (dest_buf, source_buf, dst_rect, src_rect,
                                      s_rowstride, scale, bpp, d_rowstride);
    else
      gegl_resample_nearest (dest_buf, source_buf, dst_rect, src_rect,
                             s_rowstride, scale, bpp, d_rowstride);
    }
263
  else
264 265 266 267
    {
      gegl_resample_boxfilter_generic (dest_buf, source_buf, dst_rect, src_rect,
                                       s_rowstride, scale, format, d_rowstride);
    }
268 269
}

270
static void
271 272
gegl_resample_bilinear_generic (guchar              *dest_buf,
                                const guchar        *source_buf,
273 274
                                const GeglRectangle *dst_rect,
                                const GeglRectangle *src_rect,
275 276 277 278
                                gint                 s_rowstride,
                                gdouble              scale,
                                const Babl          *format,
                                gint                 d_rowstride)
279 280 281 282 283 284 285 286 287
{
  const Babl *tmp_format = gegl_babl_rgbA_linear_float ();
  const Babl *from_fish  = babl_fish (format, tmp_format);
  const Babl *to_fish    = babl_fish (tmp_format, format);

  const gint tmp_bpp     = 4 * 4;
  gint in_tmp_rowstride  = src_rect->width * tmp_bpp;
  gint out_tmp_rowstride = dst_rect->width * tmp_bpp;

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  gint do_free = 0;

  guchar *in_tmp, *out_tmp;

  if (src_rect->height * in_tmp_rowstride + dst_rect->height * out_tmp_rowstride < ALLOCA_THRESHOLD)
  {
    in_tmp = alloca (src_rect->height * in_tmp_rowstride);
    out_tmp = alloca (dst_rect->height * out_tmp_rowstride);
  }
  else
  {
    in_tmp  = gegl_malloc (src_rect->height * in_tmp_rowstride);
    out_tmp = gegl_malloc (dst_rect->height * out_tmp_rowstride);
    do_free = 1;
  }
303 304 305 306 307 308

  babl_process_rows (from_fish,
                     source_buf, s_rowstride,
                     in_tmp, in_tmp_rowstride,
                     src_rect->width, src_rect->height);

309
  gegl_resample_bilinear_float (out_tmp, in_tmp, dst_rect, src_rect,
310 311 312 313 314 315
                                in_tmp_rowstride, scale, tmp_bpp, out_tmp_rowstride);

  babl_process_rows (to_fish,
                     out_tmp,  out_tmp_rowstride,
                     dest_buf, d_rowstride,
                     dst_rect->width, dst_rect->height);
316 317 318 319 320 321

  if (do_free)
    {
      gegl_free (in_tmp);
      gegl_free (out_tmp);
    }
322 323
}

324 325 326 327 328 329 330 331 332
void gegl_resample_bilinear (guchar              *dest_buf,
                             const guchar        *source_buf,
                             const GeglRectangle *dst_rect,
                             const GeglRectangle *src_rect,
                             gint                 s_rowstride,
                             gdouble              scale,
                             const Babl          *format,
                             gint                 d_rowstride)
{
333 334 335 336 337 338
  const Babl *model     = babl_format_get_model (format);

  if (gegl_babl_model_is_linear (model))
  {
    const Babl *comp_type  = babl_format_get_type (format, 0);
    const gint bpp = babl_format_get_bytes_per_pixel (format);
339

340 341 342 343 344 345 346 347
    if (comp_type == gegl_babl_float ())
      gegl_resample_bilinear_float (dest_buf, source_buf, dst_rect, src_rect,
                                    s_rowstride, scale, bpp, d_rowstride);
    else if (comp_type == gegl_babl_u8 ())
      gegl_resample_bilinear_u8 (dest_buf, source_buf, dst_rect, src_rect,
                                 s_rowstride, scale, bpp, d_rowstride);
    else if (comp_type == gegl_babl_u16 ())
      gegl_resample_bilinear_u16 (dest_buf, source_buf, dst_rect, src_rect,
348
                                  s_rowstride, scale, bpp, d_rowstride);
349 350 351 352 353 354 355 356 357 358
    else if (comp_type == gegl_babl_u32 ())
      gegl_resample_bilinear_u32 (dest_buf, source_buf, dst_rect, src_rect,
                                  s_rowstride, scale, bpp, d_rowstride);
    else if (comp_type == gegl_babl_double ())
      gegl_resample_bilinear_double (dest_buf, source_buf, dst_rect, src_rect,
                                     s_rowstride, scale, bpp, d_rowstride);
    else
      gegl_resample_nearest (dest_buf, source_buf, dst_rect, src_rect,
                             s_rowstride, scale, bpp, d_rowstride);
    }
359
  else
360 361 362 363
    {
      gegl_resample_bilinear_generic (dest_buf, source_buf, dst_rect, src_rect,
                                      s_rowstride, scale, format, d_rowstride);
    }
364 365
}

366 367 368 369 370 371
static inline int int_floorf (float x)
{
  int i = (int)x; /* truncate */
  return i - ( i > x ); /* convert trunc to floor */
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385
void
gegl_resample_nearest (guchar              *dst,
                       const guchar        *src,
                       const GeglRectangle *dst_rect,
                       const GeglRectangle *src_rect,
                       const gint           src_stride,
                       const gdouble        scale,
                       const gint           bpp,
                       const gint           dst_stride)
{
  int i, j;

  for (i = 0; i < dst_rect->height; i++)
    {
386
      const gfloat sy = (dst_rect->y + .5 + i) / scale - src_rect->y;
387
      const gint   ii = int_floorf (sy + GEGL_SCALE_EPSILON);
388 389 390

      for (j = 0; j < dst_rect->width; j++)
        {
391
          const gfloat sx = (dst_rect->x + .5 + j) / scale - src_rect->x;
392
          const gint   jj = int_floorf (sx + GEGL_SCALE_EPSILON);
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

          memcpy (&dst[i * dst_stride + j * bpp],
                  &src[ii * src_stride + jj * bpp],
                  bpp);
        }
    }
}

#define BOXFILTER_FUNCNAME   gegl_resample_boxfilter_double
#define BOXFILTER_TYPE       gdouble
#define BOXFILTER_ROUND(val) (val)
#include "gegl-algorithms-boxfilter.inc"
#undef BOXFILTER_FUNCNAME
#undef BOXFILTER_TYPE
#undef BOXFILTER_ROUND

#define BOXFILTER_FUNCNAME   gegl_resample_boxfilter_float
#define BOXFILTER_TYPE       gfloat
#define BOXFILTER_ROUND(val) (val)
#include "gegl-algorithms-boxfilter.inc"
#undef BOXFILTER_FUNCNAME
#undef BOXFILTER_TYPE
#undef BOXFILTER_ROUND

#define BOXFILTER_FUNCNAME   gegl_resample_boxfilter_u8
#define BOXFILTER_TYPE       guchar
419
#define BOXFILTER_ROUND(val) ((int)((val)+0.5))
420 421 422 423 424 425 426
#include "gegl-algorithms-boxfilter.inc"
#undef BOXFILTER_FUNCNAME
#undef BOXFILTER_TYPE
#undef BOXFILTER_ROUND

#define BOXFILTER_FUNCNAME   gegl_resample_boxfilter_u16
#define BOXFILTER_TYPE       guint16
427
#define BOXFILTER_ROUND(val) ((int)((val)+0.5))
428 429 430 431 432 433 434
#include "gegl-algorithms-boxfilter.inc"
#undef BOXFILTER_FUNCNAME
#undef BOXFILTER_TYPE
#undef BOXFILTER_ROUND

#define BOXFILTER_FUNCNAME   gegl_resample_boxfilter_u32
#define BOXFILTER_TYPE       guint32
435
#define BOXFILTER_ROUND(val) ((int)((val)+0.5))
436 437 438
#include "gegl-algorithms-boxfilter.inc"
#undef BOXFILTER_FUNCNAME
#undef BOXFILTER_TYPE
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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
#undef BOXFILTER_ROUND

#define DOWNSCALE_FUNCNAME gegl_downscale_2x2_double
#define DOWNSCALE_TYPE     gdouble
#define DOWNSCALE_SUM      gdouble
#define DOWNSCALE_DIVISOR  4.0
#include "gegl-algorithms-2x2-downscale.inc"
#undef DOWNSCALE_FUNCNAME
#undef DOWNSCALE_TYPE
#undef DOWNSCALE_SUM
#undef DOWNSCALE_DIVISOR

#define DOWNSCALE_FUNCNAME gegl_downscale_2x2_float
#define DOWNSCALE_TYPE     gfloat
#define DOWNSCALE_SUM      gfloat
#define DOWNSCALE_DIVISOR  4.0f
#include "gegl-algorithms-2x2-downscale.inc"
#undef DOWNSCALE_FUNCNAME
#undef DOWNSCALE_TYPE
#undef DOWNSCALE_SUM
#undef DOWNSCALE_DIVISOR

#define DOWNSCALE_FUNCNAME gegl_downscale_2x2_u32
#define DOWNSCALE_TYPE     guint32
#define DOWNSCALE_SUM      guint64
#define DOWNSCALE_DIVISOR  4
#include "gegl-algorithms-2x2-downscale.inc"
#undef DOWNSCALE_FUNCNAME
#undef DOWNSCALE_TYPE
#undef DOWNSCALE_SUM
#undef DOWNSCALE_DIVISOR

#define DOWNSCALE_FUNCNAME gegl_downscale_2x2_u16
#define DOWNSCALE_TYPE     guint16
#define DOWNSCALE_SUM      guint
#define DOWNSCALE_DIVISOR  4
#include "gegl-algorithms-2x2-downscale.inc"
#undef DOWNSCALE_FUNCNAME
#undef DOWNSCALE_TYPE
#undef DOWNSCALE_SUM
#undef DOWNSCALE_DIVISOR

#define DOWNSCALE_FUNCNAME gegl_downscale_2x2_u8
#define DOWNSCALE_TYPE     guint8
#define DOWNSCALE_SUM      guint
#define DOWNSCALE_DIVISOR  4
#include "gegl-algorithms-2x2-downscale.inc"
#undef DOWNSCALE_FUNCNAME
#undef DOWNSCALE_TYPE
#undef DOWNSCALE_SUM
#undef DOWNSCALE_DIVISOR
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531


#define BILINEAR_FUNCNAME   gegl_resample_bilinear_double
#define BILINEAR_TYPE       gdouble
#define BILINEAR_ROUND(val) (val)
#include "gegl-algorithms-bilinear.inc"
#undef BILINEAR_FUNCNAME
#undef BILINEAR_TYPE
#undef BILINEAR_ROUND

#define BILINEAR_FUNCNAME   gegl_resample_bilinear_float
#define BILINEAR_TYPE       gfloat
#define BILINEAR_ROUND(val) (val)
#include "gegl-algorithms-bilinear.inc"
#undef BILINEAR_FUNCNAME
#undef BILINEAR_TYPE
#undef BILINEAR_ROUND

#define BILINEAR_FUNCNAME   gegl_resample_bilinear_u8
#define BILINEAR_TYPE       guchar
#define BILINEAR_ROUND(val) ((int)((val)+0.5))
#include "gegl-algorithms-bilinear.inc"
#undef BILINEAR_FUNCNAME
#undef BILINEAR_TYPE
#undef BILINEAR_ROUND

#define BILINEAR_FUNCNAME   gegl_resample_bilinear_u16
#define BILINEAR_TYPE       guint16
#define BILINEAR_ROUND(val) ((int)((val)+0.5))
#include "gegl-algorithms-bilinear.inc"
#undef BILINEAR_FUNCNAME
#undef BILINEAR_TYPE
#undef BILINEAR_ROUND

#define BILINEAR_FUNCNAME   gegl_resample_bilinear_u32
#define BILINEAR_TYPE       guint32
#define BILINEAR_ROUND(val) ((int)((val)+0.5))
#include "gegl-algorithms-bilinear.inc"
#undef BILINEAR_FUNCNAME
#undef BILINEAR_TYPE
#undef BILINEAR_ROUND