summaryrefslogtreecommitdiffstats
path: root/app/core/gimptempbuf.c
blob: 2879be9241bcd769838d57d852fca26c429ffc24 (plain)
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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#include <cairo.h>
#include <gegl.h>
#include <gegl-utils.h>
#include <glib/gstdio.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "core-types.h"

#include "libgimpcolor/gimpcolor.h"

#include "gimptempbuf.h"


#define LOCK_DATA_ALIGNMENT 16


struct _GimpTempBuf
{
  gint        ref_count;
  gint        width;
  gint        height;
  const Babl *format;
  guchar     *data;
};

typedef struct
{
  const Babl     *format;
  GeglAccessMode  access_mode;
} LockData;

G_STATIC_ASSERT (sizeof (LockData) <= LOCK_DATA_ALIGNMENT);


/*  local variables  */

static guintptr gimp_temp_buf_total_memsize = 0;


/*  public functions  */

GimpTempBuf *
gimp_temp_buf_new (gint        width,
                   gint        height,
                   const Babl *format)
{
  GimpTempBuf *temp;
  gint         bpp;

  g_return_val_if_fail (format != NULL, NULL);

  bpp = babl_format_get_bytes_per_pixel (format);

  g_return_val_if_fail (width > 0 && height > 0 && bpp > 0, NULL);
  g_return_val_if_fail (G_MAXSIZE / width / height / bpp > 0, NULL);

  temp = g_slice_new (GimpTempBuf);

  temp->ref_count = 1;
  temp->width     = width;
  temp->height    = height;
  temp->format    = format;
  temp->data      = gegl_malloc ((gsize) width * height * bpp);

  g_atomic_pointer_add (&gimp_temp_buf_total_memsize,
                        +gimp_temp_buf_get_memsize (temp));

  return temp;
}

GimpTempBuf *
gimp_temp_buf_new_from_pixbuf (GdkPixbuf  *pixbuf,
                               const Babl *f_or_null)
{
  const Babl   *format = f_or_null;
  const Babl   *fish   = NULL;
  GimpTempBuf  *temp_buf;
  const guchar *pixels;
  gint          width;
  gint          height;
  gint          rowstride;
  gint          bpp;
  guchar       *data;
  gint          i;

  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);

  if (! format)
    format = gimp_pixbuf_get_format (pixbuf);

  pixels    = gdk_pixbuf_get_pixels (pixbuf);
  width     = gdk_pixbuf_get_width (pixbuf);
  height    = gdk_pixbuf_get_height (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);

  temp_buf  = gimp_temp_buf_new (width, height, format);
  data      = gimp_temp_buf_get_data (temp_buf);

  bpp       = babl_format_get_bytes_per_pixel (format);

  if (gimp_pixbuf_get_format (pixbuf) != format)
    fish = babl_fish (gimp_pixbuf_get_format (pixbuf), format);

  for (i = 0; i < height; i++)
    {
      if (fish)
        babl_process (fish, pixels, data, width);
      else
        memcpy (data, pixels, width * bpp);

      data   += width * bpp;
      pixels += rowstride;
    }

  return temp_buf;
}

GimpTempBuf *
gimp_temp_buf_copy (const GimpTempBuf *src)
{
  GimpTempBuf *dest;

  g_return_val_if_fail (src != NULL, NULL);

  dest = gimp_temp_buf_new (src->width, src->height, src->format);

  memcpy (gimp_temp_buf_get_data (dest),
          gimp_temp_buf_get_data (src),
          gimp_temp_buf_get_data_size (src));

  return dest;
}

GimpTempBuf *
gimp_temp_buf_ref (const GimpTempBuf *buf)
{
  g_return_val_if_fail (buf != NULL, NULL);

  g_atomic_int_inc ((gint *) &buf->ref_count);

  return (GimpTempBuf *) buf;
}

void
gimp_temp_buf_unref (const GimpTempBuf *buf)
{
  g_return_if_fail (buf != NULL);
  g_return_if_fail (buf->ref_count > 0);

  if (g_atomic_int_dec_and_test ((gint *) &buf->ref_count))
    {
      g_atomic_pointer_add (&gimp_temp_buf_total_memsize,
                            -gimp_temp_buf_get_memsize (buf));


      if (buf->data)
        gegl_free (buf->data);

      g_slice_free (GimpTempBuf, (GimpTempBuf *) buf);
    }
}

GimpTempBuf *
gimp_temp_buf_scale (const GimpTempBuf *src,
                     gint               new_width,
                     gint               new_height)
{
  GimpTempBuf  *dest;
  const guchar *src_data;
  guchar       *dest_data;
  gdouble       x_ratio;
  gdouble       y_ratio;
  gint          bytes;
  gint          loop1;
  gint          loop2;

  g_return_val_if_fail (src != NULL, NULL);
  g_return_val_if_fail (new_width > 0 && new_height > 0, NULL);

  if (new_width == src->width && new_height == src->height)
    return gimp_temp_buf_copy (src);

  dest = gimp_temp_buf_new (new_width,
                            new_height,
                            src->format);

  src_data  = gimp_temp_buf_get_data (src);
  dest_data = gimp_temp_buf_get_data (dest);

  x_ratio = (gdouble) src->width  / (gdouble) new_width;
  y_ratio = (gdouble) src->height / (gdouble) new_height;

  bytes = babl_format_get_bytes_per_pixel (src->format);

  for (loop1 = 0 ; loop1 < new_height ; loop1++)
    {
      for (loop2 = 0 ; loop2 < new_width ; loop2++)
        {
          const guchar *src_pixel;
          guchar       *dest_pixel;
          gint          i;

          src_pixel = src_data +
            (gint) (loop2 * x_ratio) * bytes +
            (gint) (loop1 * y_ratio) * bytes * src->width;

          dest_pixel = dest_data +
            (loop2 + loop1 * new_width) * bytes;

          for (i = 0 ; i < bytes; i++)
            *dest_pixel++ = *src_pixel++;
        }
    }

  return dest;
}

gint
gimp_temp_buf_get_width (const GimpTempBuf *buf)
{
  return buf->width;
}

gint
gimp_temp_buf_get_height (const GimpTempBuf *buf)
{
  return buf->height;
}

const Babl *
gimp_temp_buf_get_format (const GimpTempBuf *buf)
{
  return buf->format;
}

void
gimp_temp_buf_set_format (GimpTempBuf *buf,
                          const Babl  *format)
{
  g_return_if_fail (babl_format_get_bytes_per_pixel (buf->format) ==
                    babl_format_get_bytes_per_pixel (format));

  buf->format = format;
}

guchar *
gimp_temp_buf_get_data (const GimpTempBuf *buf)
{
  return buf->data;
}

gsize
gimp_temp_buf_get_data_size (const GimpTempBuf *buf)
{
  return (gsize) babl_format_get_bytes_per_pixel (buf->format) *
                 buf->width * buf->height;
}

guchar *
gimp_temp_buf_data_clear (GimpTempBuf *buf)
{
  memset (buf->data, 0, gimp_temp_buf_get_data_size (buf));

  return buf->data;
}

gpointer
gimp_temp_buf_lock (const GimpTempBuf *buf,
                    const Babl        *format,
                    GeglAccessMode     access_mode)
{
  guchar   *data;
  LockData *lock_data;
  gint      n_pixels;
  gint      bpp;

  g_return_val_if_fail (buf != NULL, NULL);

  if (! format || format == buf->format)
    return gimp_temp_buf_get_data (buf);

  n_pixels = buf->width * buf->height;
  bpp      = babl_format_get_bytes_per_pixel (format);

  data = gegl_scratch_alloc (LOCK_DATA_ALIGNMENT + n_pixels * bpp);

  if ((guintptr) data % LOCK_DATA_ALIGNMENT)
    {
      g_free (data);

      g_return_val_if_reached (NULL);
    }

  lock_data              = (LockData *) data;
  lock_data->format      = format;
  lock_data->access_mode = access_mode;

  data += LOCK_DATA_ALIGNMENT;

  if (access_mode & GEGL_ACCESS_READ)
    {
      babl_process (babl_fish (buf->format, format),
                    gimp_temp_buf_get_data (buf),
                    data,
                    n_pixels);
    }

  return data;
}

void
gimp_temp_buf_unlock (const GimpTempBuf *buf,
                      gconstpointer      data)
{
  LockData *lock_data;

  g_return_if_fail (buf != NULL);
  g_return_if_fail (data != NULL);

  if (data == buf->data)
    return;

  lock_data = (LockData *) ((const guint8 *) data - LOCK_DATA_ALIGNMENT);

  if (lock_data->access_mode & GEGL_ACCESS_WRITE)
    {
      babl_process (babl_fish (lock_data->format, buf->format),
                    data,
                    gimp_temp_buf_get_data (buf),
                    buf->width * buf->height);
    }

  gegl_scratch_free (lock_data);
}

gsize
gimp_temp_buf_get_memsize (const GimpTempBuf *buf)
{
  if (buf)
    return (sizeof (GimpTempBuf) + gimp_temp_buf_get_data_size (buf));

  return 0;
}

GeglBuffer *
gimp_temp_buf_create_buffer (const GimpTempBuf *temp_buf)
{
  GeglBuffer *buffer;

  g_return_val_if_fail (temp_buf != NULL, NULL);

  buffer =
    gegl_buffer_linear_new_from_data (gimp_temp_buf_get_data (temp_buf),
                                      temp_buf->format,
                                      GEGL_RECTANGLE (0, 0,
                                                      temp_buf->width,
                                                      temp_buf->height),
                                      GEGL_AUTO_ROWSTRIDE,
                                      (GDestroyNotify) gimp_temp_buf_unref,
                                      gimp_temp_buf_ref (temp_buf));

  g_object_set_data (G_OBJECT (buffer),
                     "gimp-temp-buf", (GimpTempBuf *) temp_buf);

  return buffer;
}

GdkPixbuf *
gimp_temp_buf_create_pixbuf (const GimpTempBuf *temp_buf)
{
  GdkPixbuf    *pixbuf;
  const Babl   *format;
  const Babl   *fish = NULL;
  const guchar *data;
  gint          width;
  gint          height;
  gint          bpp;
  guchar       *pixels;
  gint          rowstride;
  gint          i;

  g_return_val_if_fail (temp_buf != NULL, NULL);

  data      = gimp_temp_buf_get_data (temp_buf);
  format    = gimp_temp_buf_get_format (temp_buf);
  width     = gimp_temp_buf_get_width (temp_buf);
  height    = gimp_temp_buf_get_height (temp_buf);
  bpp       = babl_format_get_bytes_per_pixel (format);

  pixbuf    = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
                              babl_format_has_alpha (format),
                              8, width, height);

  pixels    = gdk_pixbuf_get_pixels (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);

  if (format != gimp_pixbuf_get_format (pixbuf))
    fish = babl_fish (format, gimp_pixbuf_get_format (pixbuf));

  for (i = 0; i < height; i++)
    {
      if (fish)
        babl_process (fish, data, pixels, width);
      else
        memcpy (pixels, data, width * bpp);

      data   += width * bpp;
      pixels += rowstride;
    }

  return pixbuf;
}

GimpTempBuf *
gimp_gegl_buffer_get_temp_buf (GeglBuffer *buffer)
{
  g_return_val_if_fail (GEGL_IS_BUFFER (buffer), NULL);

  return g_object_get_data (G_OBJECT (buffer), "gimp-temp-buf");
}


/*  public functions (stats)  */

guint64
gimp_temp_buf_get_total_memsize (void)
{
  return gimp_temp_buf_total_memsize;
}