npd_gegl.c 3.5 KB
Newer Older
1
/*
2
 * This file is part of N-point image deformation library.
3
 *
4 5 6 7 8
 * N-point image deformation 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.
9
 *
10 11 12 13
 * N-point image deformation 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.
14
 *
15 16 17
 * You should have received a copy of the GNU Lesser General Public
 * License along with N-point image deformation library.
 * If not, see <http://www.gnu.org/licenses/>.
18 19 20 21
 *
 * Copyright (C) 2013 Marek Dvoroznak <dvoromar@gmail.com>
 */

22
#include "npd_gegl.h"
23
#include <glib.h>
24
#include <gegl.h>
25 26 27 28 29 30 31 32 33 34 35

void
npd_compute_affinity (NPDPoint  *p11,
                      NPDPoint  *p21,
                      NPDPoint  *p31,
                      NPDPoint  *p12,
                      NPDPoint  *p22,
                      NPDPoint  *p32,
                      NPDMatrix *T)
{
  GeglMatrix3 Y, X;
36

37 38 39
  Y.coeff[0][0] = p12->x; Y.coeff[1][0] = p12->y; Y.coeff[2][0] = 1;
  Y.coeff[0][1] = p22->x; Y.coeff[1][1] = p22->y; Y.coeff[2][1] = 1;
  Y.coeff[0][2] = p32->x; Y.coeff[1][2] = p32->y; Y.coeff[2][2] = 1;
40

41 42 43
  X.coeff[0][0] = p11->x; X.coeff[1][0] = p11->y; X.coeff[2][0] = 1;
  X.coeff[0][1] = p21->x; X.coeff[1][1] = p21->y; X.coeff[2][1] = 1;
  X.coeff[0][2] = p31->x; X.coeff[1][2] = p31->y; X.coeff[2][2] = 1;
44

45
  gegl_matrix3_invert (&X);
46
  gegl_matrix3_multiply (&Y, &X, T);
47 48 49 50 51 52 53 54
}

void
npd_apply_transformation (NPDMatrix *T,
                          NPDPoint  *src,
                          NPDPoint  *dest)
{
  gdouble x = src->x, y = src->y;
55
  gegl_matrix3_transform_point (T, &x, &y);
56 57
  dest->x = x; dest->y = y;
}
58 59 60 61 62 63 64 65 66 67

void
npd_gegl_set_pixel_color (NPDImage *image,
                          gint      x,
                          gint      y,
                          NPDColor *color)
{
  if (x > -1 && x < image->width &&
      y > -1 && y < image->height)
    {
68
      gint position = y * image->rowstride + 4 * x;
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

      image->buffer[position + 0] = color->r;
      image->buffer[position + 1] = color->g;
      image->buffer[position + 2] = color->b;
      image->buffer[position + 3] = color->a;
    }
}

void
npd_gegl_get_pixel_color (NPDImage *image,
                          gint      x,
                          gint      y,
                          NPDColor *color)
{
  if (x > -1 && x < image->width &&
      y > -1 && y < image->height)
    {
86
      gint position = y * image->rowstride + 4 * x;
87 88 89 90 91 92 93 94 95 96 97 98 99

      color->r = image->buffer[position + 0];
      color->g = image->buffer[position + 1];
      color->b = image->buffer[position + 2];
      color->a = image->buffer[position + 3];
    }
  else
    {
      color->r = color->g = color->b = color->a = 0;
    }
}

void
100
npd_gegl_open_buffer (NPDImage *image)
101
{
102 103
  image->buffer = (guchar*) gegl_buffer_linear_open (image->gegl_buffer, NULL, &image->rowstride, image->format);
}
104

105 106 107 108
void
npd_gegl_close_buffer (NPDImage *image)
{
  gegl_buffer_linear_close (image->gegl_buffer, image->buffer);
109 110 111
}

void
112 113 114
npd_gegl_init_image (NPDImage   *image,
                     GeglBuffer *gegl_buffer,
                     const Babl *format)
115
{
116 117 118 119
  image->gegl_buffer = gegl_buffer;
  image->width = gegl_buffer_get_width (gegl_buffer);
  image->height = gegl_buffer_get_height (gegl_buffer);
  image->format = format;
120
}