2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
1997-11-25 06:05:25 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
1997-11-25 06:05:25 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1997-11-25 06:05:25 +08:00
|
|
|
* (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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
2000-12-17 05:37:03 +08:00
|
|
|
|
1999-02-21 07:20:54 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2003-07-03 02:01:19 +08:00
|
|
|
#include <string.h>
|
1999-02-21 07:20:54 +08:00
|
|
|
|
2011-04-28 21:50:39 +08:00
|
|
|
#include <cairo.h>
|
2012-04-09 01:06:06 +08:00
|
|
|
#include <gegl.h>
|
2009-08-15 01:18:56 +08:00
|
|
|
#include <glib/gstdio.h>
|
2001-05-15 19:25:25 +08:00
|
|
|
|
2012-04-09 06:59:20 +08:00
|
|
|
#include "core-types.h"
|
2000-12-17 05:37:03 +08:00
|
|
|
|
2012-04-09 06:59:20 +08:00
|
|
|
#include "gimptempbuf.h"
|
2001-04-07 23:58:26 +08:00
|
|
|
|
2001-10-29 20:51:21 +08:00
|
|
|
|
2012-04-09 02:25:49 +08:00
|
|
|
GimpTempBuf *
|
2012-04-09 05:56:52 +08:00
|
|
|
gimp_temp_buf_new (gint width,
|
|
|
|
gint height,
|
|
|
|
const Babl *format)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2012-04-09 02:25:49 +08:00
|
|
|
GimpTempBuf *temp;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-12-03 21:44:59 +08:00
|
|
|
g_return_val_if_fail (width > 0 && height > 0, NULL);
|
2012-04-09 00:47:49 +08:00
|
|
|
g_return_val_if_fail (format != NULL, NULL);
|
2001-11-13 11:31:47 +08:00
|
|
|
|
2012-04-09 02:25:49 +08:00
|
|
|
temp = g_slice_new (GimpTempBuf);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2012-04-09 06:16:46 +08:00
|
|
|
temp->ref_count = 1;
|
|
|
|
temp->format = format;
|
|
|
|
temp->width = width;
|
|
|
|
temp->height = height;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2012-04-09 00:47:49 +08:00
|
|
|
temp->data = g_new (guchar,
|
|
|
|
width * height *
|
|
|
|
babl_format_get_bytes_per_pixel (format));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
return temp;
|
|
|
|
}
|
2001-02-21 08:07:21 +08:00
|
|
|
|
2012-04-09 02:25:49 +08:00
|
|
|
GimpTempBuf *
|
2012-04-10 21:51:33 +08:00
|
|
|
gimp_temp_buf_copy (const GimpTempBuf *src)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2012-04-09 02:25:49 +08:00
|
|
|
GimpTempBuf *dest;
|
2012-04-08 06:25:14 +08:00
|
|
|
|
2001-11-13 11:31:47 +08:00
|
|
|
g_return_val_if_fail (src != NULL, NULL);
|
2002-04-17 04:25:27 +08:00
|
|
|
|
2012-04-09 05:56:52 +08:00
|
|
|
dest = gimp_temp_buf_new (src->width, src->height, src->format);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2012-04-09 05:56:52 +08:00
|
|
|
memcpy (gimp_temp_buf_get_data (dest),
|
|
|
|
gimp_temp_buf_get_data (src),
|
|
|
|
gimp_temp_buf_get_data_size (src));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2002-04-17 04:25:27 +08:00
|
|
|
return dest;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2012-04-09 06:16:46 +08:00
|
|
|
GimpTempBuf *
|
|
|
|
gimp_temp_buf_ref (GimpTempBuf *buf)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (buf != NULL, NULL);
|
|
|
|
|
|
|
|
buf->ref_count++;
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2012-04-09 05:56:52 +08:00
|
|
|
void
|
2012-04-09 06:16:46 +08:00
|
|
|
gimp_temp_buf_unref (GimpTempBuf *buf)
|
2012-04-09 05:56:52 +08:00
|
|
|
{
|
|
|
|
g_return_if_fail (buf != NULL);
|
2012-04-09 06:16:46 +08:00
|
|
|
g_return_if_fail (buf->ref_count > 0);
|
|
|
|
|
|
|
|
buf->ref_count--;
|
2012-04-09 05:56:52 +08:00
|
|
|
|
2012-04-09 06:16:46 +08:00
|
|
|
if (buf->ref_count < 1)
|
|
|
|
{
|
|
|
|
if (buf->data)
|
|
|
|
g_free (buf->data);
|
2012-04-09 05:56:52 +08:00
|
|
|
|
2012-04-09 06:16:46 +08:00
|
|
|
g_slice_free (GimpTempBuf, buf);
|
|
|
|
}
|
2012-04-09 05:56:52 +08:00
|
|
|
}
|
|
|
|
|
2012-04-09 02:25:49 +08:00
|
|
|
GimpTempBuf *
|
2012-04-10 21:51:33 +08:00
|
|
|
gimp_temp_buf_scale (const GimpTempBuf *src,
|
|
|
|
gint new_width,
|
|
|
|
gint new_height)
|
2001-02-05 06:10:54 +08:00
|
|
|
{
|
2012-04-09 02:25:49 +08:00
|
|
|
GimpTempBuf *dest;
|
2007-12-14 01:27:07 +08:00
|
|
|
const guchar *src_data;
|
|
|
|
guchar *dest_data;
|
|
|
|
gdouble x_ratio;
|
|
|
|
gdouble y_ratio;
|
2012-04-09 00:47:49 +08:00
|
|
|
gint bytes;
|
2007-12-14 01:27:07 +08:00
|
|
|
gint loop1;
|
|
|
|
gint loop2;
|
2001-02-05 06:10:54 +08:00
|
|
|
|
2001-11-13 11:31:47 +08:00
|
|
|
g_return_val_if_fail (src != NULL, NULL);
|
|
|
|
g_return_val_if_fail (new_width > 0 && new_height > 0, NULL);
|
|
|
|
|
2012-04-10 21:51:33 +08:00
|
|
|
if (new_width == src->width && new_height == src->height)
|
|
|
|
return gimp_temp_buf_copy (src);
|
|
|
|
|
2012-04-09 05:56:52 +08:00
|
|
|
dest = gimp_temp_buf_new (new_width,
|
|
|
|
new_height,
|
|
|
|
src->format);
|
2001-02-05 06:10:54 +08:00
|
|
|
|
2012-04-09 05:56:52 +08:00
|
|
|
src_data = gimp_temp_buf_get_data (src);
|
|
|
|
dest_data = gimp_temp_buf_get_data (dest);
|
2001-02-05 06:10:54 +08:00
|
|
|
|
|
|
|
x_ratio = (gdouble) src->width / (gdouble) new_width;
|
|
|
|
y_ratio = (gdouble) src->height / (gdouble) new_height;
|
2003-08-25 19:35:16 +08:00
|
|
|
|
2012-04-09 00:47:49 +08:00
|
|
|
bytes = babl_format_get_bytes_per_pixel (src->format);
|
|
|
|
|
2001-02-05 06:10:54 +08:00
|
|
|
for (loop1 = 0 ; loop1 < new_height ; loop1++)
|
|
|
|
{
|
|
|
|
for (loop2 = 0 ; loop2 < new_width ; loop2++)
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
2007-12-14 01:27:07 +08:00
|
|
|
const guchar *src_pixel;
|
|
|
|
guchar *dest_pixel;
|
|
|
|
gint i;
|
2001-02-05 06:10:54 +08:00
|
|
|
|
2006-04-12 20:49:29 +08:00
|
|
|
src_pixel = src_data +
|
2012-04-09 00:47:49 +08:00
|
|
|
(gint) (loop2 * x_ratio) * bytes +
|
|
|
|
(gint) (loop1 * y_ratio) * bytes * src->width;
|
2001-02-05 06:10:54 +08:00
|
|
|
|
2006-04-12 20:49:29 +08:00
|
|
|
dest_pixel = dest_data +
|
2012-04-09 00:47:49 +08:00
|
|
|
(loop2 + loop1 * new_width) * bytes;
|
2001-02-05 06:10:54 +08:00
|
|
|
|
2012-04-09 00:47:49 +08:00
|
|
|
for (i = 0 ; i < bytes; i++)
|
2006-04-12 20:49:29 +08:00
|
|
|
*dest_pixel++ = *src_pixel++;
|
|
|
|
}
|
2001-02-05 06:10:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2000-04-28 01:27:28 +08:00
|
|
|
guchar *
|
2012-04-09 05:56:52 +08:00
|
|
|
gimp_temp_buf_get_data (const GimpTempBuf *buf)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2007-12-14 01:27:07 +08:00
|
|
|
return buf->data;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2008-12-13 18:51:16 +08:00
|
|
|
gsize
|
2012-04-10 21:51:33 +08:00
|
|
|
gimp_temp_buf_get_data_size (const GimpTempBuf *buf)
|
2008-12-13 18:51:16 +08:00
|
|
|
{
|
2012-04-09 00:47:49 +08:00
|
|
|
return babl_format_get_bytes_per_pixel (buf->format) * buf->width * buf->height;
|
2008-12-13 18:51:16 +08:00
|
|
|
}
|
|
|
|
|
2001-01-03 01:01:30 +08:00
|
|
|
guchar *
|
2012-04-09 05:56:52 +08:00
|
|
|
gimp_temp_buf_data_clear (GimpTempBuf *buf)
|
2001-01-03 01:01:30 +08:00
|
|
|
{
|
2012-04-09 05:56:52 +08:00
|
|
|
memset (buf->data, 0, gimp_temp_buf_get_data_size (buf));
|
2001-01-03 02:17:16 +08:00
|
|
|
|
2007-12-14 01:27:07 +08:00
|
|
|
return buf->data;
|
2001-01-03 01:01:30 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2002-01-31 00:14:26 +08:00
|
|
|
gsize
|
2012-04-10 21:51:33 +08:00
|
|
|
gimp_temp_buf_get_memsize (const GimpTempBuf *buf)
|
2002-01-31 00:14:26 +08:00
|
|
|
{
|
2007-12-14 01:27:07 +08:00
|
|
|
if (buf)
|
2012-04-09 05:56:52 +08:00
|
|
|
return (sizeof (GimpTempBuf) + gimp_temp_buf_get_data_size (buf));
|
2002-01-31 00:14:26 +08:00
|
|
|
|
2007-11-17 02:19:30 +08:00
|
|
|
return 0;
|
2002-01-31 00:14:26 +08:00
|
|
|
}
|
|
|
|
|
2012-04-09 01:06:06 +08:00
|
|
|
GeglBuffer *
|
2012-04-09 06:16:46 +08:00
|
|
|
gimp_temp_buf_create_buffer (GimpTempBuf *temp_buf)
|
2012-04-09 01:06:06 +08:00
|
|
|
{
|
|
|
|
GeglBuffer *buffer;
|
|
|
|
|
|
|
|
g_return_val_if_fail (temp_buf != NULL, NULL);
|
|
|
|
|
|
|
|
buffer =
|
2012-04-09 05:56:52 +08:00
|
|
|
gegl_buffer_linear_new_from_data (gimp_temp_buf_get_data (temp_buf),
|
2012-04-09 01:06:06 +08:00
|
|
|
temp_buf->format,
|
|
|
|
GEGL_RECTANGLE (0, 0,
|
|
|
|
temp_buf->width,
|
|
|
|
temp_buf->height),
|
|
|
|
GEGL_AUTO_ROWSTRIDE,
|
2012-04-09 06:16:46 +08:00
|
|
|
(GDestroyNotify) gimp_temp_buf_unref,
|
|
|
|
gimp_temp_buf_ref (temp_buf));
|
2012-04-09 01:06:06 +08:00
|
|
|
|
|
|
|
g_object_set_data (G_OBJECT (buffer), "gimp-temp-buf", temp_buf);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
2012-04-11 00:47:44 +08:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|