File: gimppattern.c

package info (click to toggle)
gimp 3.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 210,076 kB
  • sloc: ansic: 842,287; lisp: 10,761; python: 10,318; cpp: 7,238; perl: 4,355; sh: 1,043; xml: 963; yacc: 609; lex: 348; javascript: 150; makefile: 43
file content (207 lines) | stat: -rw-r--r-- 6,516 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimppattern.c
 * Copyright (C) 2023 Jehan
 *
 * 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
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gimp.h"

#include "gimppattern.h"

struct _GimpPattern
{
  GimpResource  parent_instance;

  /* Native size buffer of the pattern contents. */
  GeglBuffer   *buffer;
};

static void         gimp_pattern_finalize        (GObject     *object);
static void         gimp_pattern_get_data        (GimpPattern *pattern);
static GeglBuffer * gimp_pattern_scale           (GeglBuffer  *buffer,
                                                  gint         max_width,
                                                  gint         max_height);


G_DEFINE_TYPE (GimpPattern, gimp_pattern, GIMP_TYPE_RESOURCE);


static void  gimp_pattern_class_init (GimpPatternClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gimp_pattern_finalize;
}

static void  gimp_pattern_init (GimpPattern *pattern)
{
  pattern->buffer = NULL;
}

static void
gimp_pattern_finalize (GObject *object)
{
  GimpPattern *pattern = GIMP_PATTERN (object);

  g_clear_object (&pattern->buffer);

  G_OBJECT_CLASS (gimp_pattern_parent_class)->finalize (object);
}


/**
 * gimp_pattern_get_buffer:
 * @pattern:    a [class@Pattern].
 * @max_width:  a maximum width for the returned buffer.
 * @max_height: a maximum height for the returned buffer.
 * @format:     an optional Babl format.
 *
 * Gets pixel data of the pattern within the bounding box specified by @max_width
 * and @max_height. The data will be scaled down so that it fits within this
 * size without changing its ratio. If the pattern is smaller than this size to
 * begin with, it will not be scaled up.
 *
 * If @max_width or @max_height are %NULL, the buffer is returned in the pattern's
 * native size.
 *
 * Make sure you called [func@Gegl.init] before calling any function using
 * `GEGL`.
 *
 * Returns: (transfer full): a [class@Gegl.Buffer].
*/
GeglBuffer *
gimp_pattern_get_buffer (GimpPattern *pattern,
                         gint         max_width,
                         gint         max_height,
                         const Babl  *format)
{
  gimp_pattern_get_data (pattern);

  g_return_val_if_fail (pattern->buffer != NULL, NULL);

  if (max_width == 0 || max_height == 0 ||
      (gegl_buffer_get_width (pattern->buffer) <= max_width &&
       gegl_buffer_get_height (pattern->buffer) <= max_height))
    return gegl_buffer_dup (pattern->buffer);

  return gimp_pattern_scale (pattern->buffer, max_width, max_height);
}

static void
gimp_pattern_get_data (GimpPattern *pattern)
{
  gint          width;
  gint          height;
  gint          bpp;
  GBytes       *bytes;
  const guchar *pixels;
  gsize         pixels_size;
  const Babl   *format;

  /*
   * This check assumes that the pattern contents doesn't change, which is not a
   * perfect assumption. We could maybe add a PDB call which would return
   * the new pattern data only if it changed since last call (which can be
   * verified with some kind of internal runtime version to pass from one call
   * to another). TODO
   */
  if (pattern->buffer != NULL)
    return;

  g_clear_object (&pattern->buffer);

  _gimp_pattern_get_pixels (pattern, &width, &height, &bpp, &bytes);
  pixels = g_bytes_unref_to_data (bytes, &pixels_size);

  /* It's an ugly way to determine the proper format but gimp_pattern_get_pixels()
   * doesn't give more info.
   */
  switch (bpp)
    {
    case 1:
      format = babl_format ("Y' u8");
      break;
    case 2:
      format = babl_format ("Y'A u8");
      break;
    case 3:
      format = babl_format ("R'G'B' u8");
      break;
    case 4:
      format = babl_format ("R'G'B'A u8");
      break;
    default:
      g_return_if_reached ();
    }

  pattern->buffer = gegl_buffer_linear_new_from_data ((const gpointer) pixels, format,
                                                      GEGL_RECTANGLE (0, 0, width, height),
                                                      0, g_free, NULL);
}

static GeglBuffer *
gimp_pattern_scale (GeglBuffer *buffer,
                    gint        max_width,
                    gint        max_height)
{
  GeglBuffer *scaled = NULL;
  GeglNode   *graph;
  GeglNode   *source;
  GeglNode   *op;
  GeglNode   *sink;
  gdouble     width;
  gdouble     height;
  gdouble     scale;

  height = (gdouble) max_height;
  width  = (gdouble) gegl_buffer_get_width (buffer) / gegl_buffer_get_height (buffer) * height;
  if (width > (gdouble) max_width)
    {
      width  = (gdouble) max_width;
      height = (gdouble) gegl_buffer_get_height (buffer) / gegl_buffer_get_width (buffer) * width;
    }
  scale = width / gegl_buffer_get_width (buffer);

  graph = gegl_node_new ();
  source = gegl_node_new_child (graph,
                                "operation", "gegl:buffer-source",
                                "buffer",    buffer,
                                NULL);
  op = gegl_node_new_child (graph,
                            "operation",   "gegl:scale-ratio",
                            "origin-x",     0.0,
                            "origin-y",     0.0,
                            "sampler",      GIMP_INTERPOLATION_LINEAR,
                            "abyss-policy", GEGL_ABYSS_CLAMP,
                            "x",            scale,
                            "y",            scale,
                            NULL);
  sink = gegl_node_new_child (graph,
                              "operation", "gegl:buffer-sink",
                              "buffer",    &scaled,
                              "format",    gegl_buffer_get_format (buffer),
                              NULL);
  gegl_node_link_many (source, op, sink, NULL);
  gegl_node_process (sink);

  g_object_unref (graph);

  return scaled;
}