1997-11-25 06:05:25 +08:00
|
|
|
/* The GIMP -- an 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 2 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, write to the Free Software
|
1998-04-13 13:44:11 +08:00
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
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"
|
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
#include <glib.h>
|
Check for mmap.
* configure.in: Check for mmap.
* app/makefile.msc: Depend on gimpi.lib.
* app/app_procs.c (app_init): Fix gccism: Allocate filenames (an
array with non-constant size) dynamically.
* app/{datafiles,fileops,general,install,module_db,temp_buf}.c:
Include glib.h before standard headers, because of certain obscure
details related to compiling with gcc on Win32.
(If you really want to know: glib.h defines he names of POSIXish
(but non-ANSI) functions as prefixed with underscore, because
that's how they are named in the msvcrt runtime C library we want
to use. However, defining stat as _stat causes some problems if
done after including the mingw32 <sys/stat.h>. So, it's easiest to
include <glib.h> early.)
* app/main.c: Use _stdcall and __argc, __argv with MSC, but
__attribute__((stdcall)) and _argc, _argv with gcc. Don't print
the "Passed serialization test" message on Win32. (It would open
up an otherwise unnecessary console window.)
* app/paint_funcs.c (gaussian_blur_region): Don't use variable sum
until initialized.
* app/{bezier_select,paths_dialog}.c: Include config.h and define
rint() if necessary.
* app/plug_in.c: Use _spawnv, not spawnv, on Win32 and OS/2.
1999-05-29 01:47:17 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
1999-02-21 07:20:54 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <unistd.h>
|
1999-02-21 07:20:54 +08:00
|
|
|
#endif
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <string.h>
|
1999-04-23 23:20:10 +08:00
|
|
|
#include <sys/types.h>
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <sys/stat.h>
|
1999-02-21 07:20:54 +08:00
|
|
|
|
1999-10-05 03:26:07 +08:00
|
|
|
#ifdef G_OS_WIN32
|
1999-02-21 07:20:54 +08:00
|
|
|
#include <process.h> /* For _getpid() */
|
|
|
|
#endif
|
2001-05-15 19:25:25 +08:00
|
|
|
|
2001-01-24 07:56:18 +08:00
|
|
|
#include "libgimpcolor/gimpcolor.h"
|
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "base-types.h"
|
2000-12-17 05:37:03 +08:00
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "base-config.h"
|
|
|
|
#include "pixel-region.h"
|
|
|
|
#include "temp-buf.h"
|
2001-04-07 23:58:26 +08:00
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "paint-funcs/paint-funcs.h"
|
2000-04-28 01:27:28 +08:00
|
|
|
|
2001-04-07 23:58:26 +08:00
|
|
|
|
2000-04-28 01:27:28 +08:00
|
|
|
static guchar * temp_buf_allocate (guint);
|
2001-01-03 02:17:16 +08:00
|
|
|
static void temp_buf_to_color (TempBuf *src_buf,
|
|
|
|
TempBuf *dest_buf);
|
|
|
|
static void temp_buf_to_gray (TempBuf *src_buf,
|
|
|
|
TempBuf *dest_buf);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Memory management */
|
|
|
|
|
2000-04-28 01:27:28 +08:00
|
|
|
static guchar *
|
|
|
|
temp_buf_allocate (guint size)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-04-28 01:27:28 +08:00
|
|
|
guchar *data;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-04-28 01:27:28 +08:00
|
|
|
data = g_new (guchar, size);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* The conversion routines */
|
|
|
|
|
|
|
|
static void
|
2001-02-05 06:10:54 +08:00
|
|
|
temp_buf_to_color (TempBuf *src_buf,
|
1999-07-20 03:46:05 +08:00
|
|
|
TempBuf *dest_buf)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-04-28 01:27:28 +08:00
|
|
|
guchar *src;
|
|
|
|
guchar *dest;
|
2001-01-03 02:17:16 +08:00
|
|
|
glong num_bytes;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
src = temp_buf_data (src_buf);
|
|
|
|
dest = temp_buf_data (dest_buf);
|
|
|
|
|
|
|
|
num_bytes = src_buf->width * src_buf->height;
|
|
|
|
|
|
|
|
while (num_bytes--)
|
|
|
|
{
|
2000-04-28 01:27:28 +08:00
|
|
|
guchar tmpch;
|
1997-11-25 06:05:25 +08:00
|
|
|
*dest++ = *src++; /* alpha channel */
|
1998-04-28 11:50:21 +08:00
|
|
|
*dest++ = tmpch = *src++;
|
|
|
|
*dest++ = tmpch;
|
|
|
|
*dest++ = tmpch;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2001-02-05 06:10:54 +08:00
|
|
|
temp_buf_to_gray (TempBuf *src_buf,
|
1999-07-20 03:46:05 +08:00
|
|
|
TempBuf *dest_buf)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-04-28 01:27:28 +08:00
|
|
|
guchar *src;
|
|
|
|
guchar *dest;
|
2001-01-03 02:17:16 +08:00
|
|
|
glong num_bytes;
|
|
|
|
gfloat pix;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
src = temp_buf_data (src_buf);
|
|
|
|
dest = temp_buf_data (dest_buf);
|
|
|
|
|
|
|
|
num_bytes = src_buf->width * src_buf->height;
|
|
|
|
|
|
|
|
while (num_bytes--)
|
|
|
|
{
|
2001-10-29 20:51:21 +08:00
|
|
|
*dest++ = src[0]; /* alpha channel */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-10-29 20:51:21 +08:00
|
|
|
pix = INTENSITY (src[1], src[2], src[3]);
|
2000-04-28 01:27:28 +08:00
|
|
|
*dest++ = (guchar) pix;
|
2001-10-29 20:51:21 +08:00
|
|
|
|
|
|
|
src += 4;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TempBuf *
|
2001-02-05 06:10:54 +08:00
|
|
|
temp_buf_new (gint width,
|
|
|
|
gint height,
|
|
|
|
gint bytes,
|
|
|
|
gint x,
|
|
|
|
gint y,
|
2000-04-28 01:27:28 +08:00
|
|
|
guchar *col)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-01-03 02:17:16 +08:00
|
|
|
glong i;
|
|
|
|
gint j;
|
2000-04-28 01:27:28 +08:00
|
|
|
guchar *data;
|
|
|
|
TempBuf *temp;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-11-13 11:31:47 +08:00
|
|
|
g_return_val_if_fail (width > 0 && height > 0, NULL);
|
|
|
|
|
2000-04-28 01:27:28 +08:00
|
|
|
temp = g_new (TempBuf, 1);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-01-03 02:17:16 +08:00
|
|
|
temp->width = width;
|
|
|
|
temp->height = height;
|
|
|
|
temp->bytes = bytes;
|
|
|
|
temp->x = x;
|
|
|
|
temp->y = y;
|
|
|
|
temp->swapped = FALSE;
|
1997-11-25 06:05:25 +08:00
|
|
|
temp->filename = NULL;
|
|
|
|
|
1998-04-28 11:50:21 +08:00
|
|
|
temp->data = data = temp_buf_allocate (width * height * bytes);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* initialize the data */
|
|
|
|
if (col)
|
|
|
|
{
|
1998-04-28 11:50:21 +08:00
|
|
|
/* First check if we can save a lot of work */
|
|
|
|
if (bytes == 1)
|
|
|
|
{
|
|
|
|
memset (data, *col, width * height);
|
|
|
|
}
|
|
|
|
else if ((bytes == 3) && (col[1] == *col) && (*col == col[2]))
|
|
|
|
{
|
|
|
|
memset (data, *col, width * height * 3);
|
|
|
|
}
|
2001-01-03 02:17:16 +08:00
|
|
|
else if ((bytes == 4) &&
|
|
|
|
(col[1] == *col) && (*col == col[2]) && (col[2] == col[3]))
|
1998-04-28 11:50:21 +08:00
|
|
|
{
|
|
|
|
memset (data, *col, (width * height) << 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No, we cannot */
|
2001-01-03 02:17:16 +08:00
|
|
|
guchar *dptr;
|
|
|
|
|
1998-04-28 11:50:21 +08:00
|
|
|
/* Fill the first row */
|
|
|
|
dptr = data;
|
2001-01-03 02:17:16 +08:00
|
|
|
|
1998-04-28 11:50:21 +08:00
|
|
|
for (i = width - 1; i >= 0; --i)
|
|
|
|
{
|
2001-01-03 02:17:16 +08:00
|
|
|
guchar *init;
|
|
|
|
|
|
|
|
j = bytes;
|
1998-04-28 11:50:21 +08:00
|
|
|
init = col;
|
2001-01-03 02:17:16 +08:00
|
|
|
|
1998-04-28 11:50:21 +08:00
|
|
|
while (j--)
|
|
|
|
*dptr++ = *init++;
|
|
|
|
}
|
2001-01-03 02:17:16 +08:00
|
|
|
|
1998-04-28 11:50:21 +08:00
|
|
|
/* Now copy from it (we set bytes to bytesperrow now) */
|
|
|
|
bytes *= width;
|
2001-01-03 02:17:16 +08:00
|
|
|
|
1998-04-28 11:50:21 +08:00
|
|
|
while (--height)
|
|
|
|
{
|
|
|
|
memcpy (dptr, data, bytes);
|
|
|
|
dptr += bytes;
|
|
|
|
}
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return temp;
|
|
|
|
}
|
2001-02-21 08:07:21 +08:00
|
|
|
|
|
|
|
/* This function simply renders a checkerboard with the given
|
|
|
|
parameters into a newly allocated tempbuf */
|
|
|
|
|
|
|
|
TempBuf *
|
|
|
|
temp_buf_new_check (gint width,
|
|
|
|
gint height,
|
|
|
|
GimpCheckType check_type,
|
|
|
|
GimpCheckSize check_size)
|
|
|
|
{
|
2001-11-13 11:31:47 +08:00
|
|
|
TempBuf *newbuf;
|
|
|
|
guchar *data;
|
|
|
|
guchar check_shift = 0;
|
|
|
|
guchar fg_color = 0;
|
|
|
|
guchar bg_color = 0;
|
|
|
|
gint i, j;
|
|
|
|
|
|
|
|
g_return_val_if_fail (width > 0 && height > 0, NULL);
|
2001-02-21 08:07:21 +08:00
|
|
|
|
|
|
|
switch (check_size)
|
|
|
|
{
|
|
|
|
case SMALL_CHECKS:
|
|
|
|
check_shift = 2;
|
|
|
|
break;
|
|
|
|
case MEDIUM_CHECKS:
|
|
|
|
check_shift = 4;
|
|
|
|
break;
|
|
|
|
case LARGE_CHECKS:
|
|
|
|
check_shift = 6;
|
2001-11-13 11:31:47 +08:00
|
|
|
break;
|
2001-02-21 08:07:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (check_type)
|
|
|
|
{
|
|
|
|
case LIGHT_CHECKS:
|
|
|
|
fg_color = 204;
|
|
|
|
bg_color = 255;
|
|
|
|
break;
|
|
|
|
case GRAY_CHECKS:
|
|
|
|
fg_color = 102;
|
|
|
|
bg_color = 153;
|
|
|
|
break;
|
|
|
|
case DARK_CHECKS:
|
|
|
|
fg_color = 0;
|
|
|
|
bg_color = 51;
|
|
|
|
break;
|
|
|
|
case WHITE_ONLY:
|
|
|
|
fg_color = 255;
|
|
|
|
bg_color = 255;
|
|
|
|
break;
|
|
|
|
case GRAY_ONLY:
|
|
|
|
fg_color = 127;
|
|
|
|
bg_color = 127;
|
|
|
|
break;
|
|
|
|
case BLACK_ONLY:
|
|
|
|
fg_color = 0;
|
|
|
|
bg_color = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
newbuf = temp_buf_new (width, height, 3, 0, 0, NULL);
|
|
|
|
data = temp_buf_data (newbuf);
|
|
|
|
|
2001-11-13 11:31:47 +08:00
|
|
|
for (i = 0, j = 1; i <= height; j++)
|
2001-02-21 08:07:21 +08:00
|
|
|
{
|
|
|
|
for (i = 1; i <= width; i++)
|
|
|
|
{
|
|
|
|
*(data + i - 1) = ((j & check_shift) && (i & check_shift))
|
|
|
|
? fg_color : bg_color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newbuf;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
TempBuf *
|
2001-02-05 06:10:54 +08:00
|
|
|
temp_buf_copy (TempBuf *src,
|
1999-07-20 03:46:05 +08:00
|
|
|
TempBuf *dest)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-01-03 02:17:16 +08:00
|
|
|
TempBuf *new;
|
|
|
|
glong length;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-11-13 11:31:47 +08:00
|
|
|
g_return_val_if_fail (src != NULL, NULL);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (!dest)
|
2001-01-03 02:17:16 +08:00
|
|
|
{
|
|
|
|
new = temp_buf_new (src->width, src->height, src->bytes, 0, 0, NULL);
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
new = dest;
|
|
|
|
if (dest->width != src->width || dest->height != src->height)
|
1998-05-28 17:03:57 +08:00
|
|
|
g_message ("In temp_buf_copy, the widths or heights don't match.");
|
1998-04-28 11:50:21 +08:00
|
|
|
/* The temp buf is smart, and can translate between color and gray */
|
|
|
|
/* (only necessary if not we allocated it */
|
|
|
|
if (src->bytes != new->bytes)
|
|
|
|
{
|
|
|
|
if (src->bytes == 4) /* RGB color */
|
|
|
|
temp_buf_to_gray (src, new);
|
|
|
|
else if (src->bytes == 2) /* grayscale */
|
|
|
|
temp_buf_to_color (src, new);
|
|
|
|
else
|
1999-12-28 18:30:50 +08:00
|
|
|
g_message ("Cannot convert from indexed color.");
|
2001-01-03 02:17:16 +08:00
|
|
|
|
1998-04-28 11:50:21 +08:00
|
|
|
return new;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
1998-04-28 11:50:21 +08:00
|
|
|
/* make the copy */
|
|
|
|
length = src->width * src->height * src->bytes;
|
|
|
|
memcpy (temp_buf_data (new), temp_buf_data (src), length);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
TempBuf *
|
2001-01-03 02:17:16 +08:00
|
|
|
temp_buf_resize (TempBuf *buf,
|
|
|
|
gint bytes,
|
|
|
|
gint x,
|
|
|
|
gint y,
|
|
|
|
gint width,
|
|
|
|
gint height)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-04-28 01:27:28 +08:00
|
|
|
gint size;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-11-13 11:31:47 +08:00
|
|
|
g_return_val_if_fail (width > 0 && height > 0, NULL);
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* calculate the requested size */
|
2001-01-03 02:17:16 +08:00
|
|
|
size = width * height * bytes;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* First, configure the canvas buffer */
|
|
|
|
if (!buf)
|
2001-01-03 02:17:16 +08:00
|
|
|
{
|
|
|
|
buf = temp_buf_new (width, height, bytes, x, y, NULL);
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (size != (buf->width * buf->height * buf->bytes))
|
|
|
|
{
|
|
|
|
/* Make sure the temp buf is unswapped */
|
|
|
|
temp_buf_unswap (buf);
|
|
|
|
|
|
|
|
/* Reallocate the data for it */
|
|
|
|
buf->data = g_realloc (buf->data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure the temp buf fields are valid */
|
2001-01-03 02:17:16 +08:00
|
|
|
buf->x = x;
|
|
|
|
buf->y = y;
|
|
|
|
buf->width = width;
|
|
|
|
buf->height = height;
|
|
|
|
buf->bytes = bytes;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2001-02-05 06:10:54 +08:00
|
|
|
TempBuf *
|
|
|
|
temp_buf_scale (TempBuf *src,
|
|
|
|
gint new_width,
|
|
|
|
gint new_height)
|
|
|
|
{
|
|
|
|
gint loop1;
|
|
|
|
gint loop2;
|
|
|
|
gdouble x_ratio;
|
|
|
|
gdouble y_ratio;
|
|
|
|
guchar *src_data;
|
|
|
|
guchar *dest_data;
|
|
|
|
TempBuf *dest;
|
|
|
|
|
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);
|
|
|
|
|
2001-02-05 06:10:54 +08:00
|
|
|
dest = temp_buf_new (new_width,
|
|
|
|
new_height,
|
|
|
|
src->bytes,
|
|
|
|
0, 0, NULL);
|
|
|
|
|
|
|
|
src_data = temp_buf_data (src);
|
|
|
|
dest_data = temp_buf_data (dest);
|
|
|
|
|
|
|
|
x_ratio = (gdouble) src->width / (gdouble) new_width;
|
|
|
|
y_ratio = (gdouble) src->height / (gdouble) new_height;
|
|
|
|
|
|
|
|
for (loop1 = 0 ; loop1 < new_height ; loop1++)
|
|
|
|
{
|
|
|
|
for (loop2 = 0 ; loop2 < new_width ; loop2++)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
guchar *src_pixel;
|
|
|
|
guchar *dest_pixel;
|
|
|
|
|
|
|
|
src_pixel = src_data +
|
|
|
|
(gint) (loop2 * x_ratio) * src->bytes +
|
|
|
|
(gint) (loop1 * y_ratio) * src->bytes * src->width;
|
|
|
|
|
|
|
|
dest_pixel = dest_data +
|
|
|
|
(loop2 + loop1 * new_width) * src->bytes;
|
|
|
|
|
|
|
|
for (i = 0 ; i < src->bytes; i++)
|
|
|
|
*dest_pixel++ = *src_pixel++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
TempBuf *
|
2001-01-03 02:17:16 +08:00
|
|
|
temp_buf_copy_area (TempBuf *src,
|
|
|
|
TempBuf *dest,
|
|
|
|
gint x,
|
|
|
|
gint y,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
2001-02-06 05:52:57 +08:00
|
|
|
gint dest_x,
|
|
|
|
gint dest_y)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-01-03 02:17:16 +08:00
|
|
|
TempBuf *new;
|
|
|
|
PixelRegion srcR, destR;
|
|
|
|
guchar empty[MAX_CHANNELS] = { 0, 0, 0, 0 };
|
|
|
|
gint x1, y1, x2, y2;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-02-06 05:52:57 +08:00
|
|
|
g_return_val_if_fail (src != NULL, dest);
|
|
|
|
g_return_val_if_fail (!dest || dest->bytes == src->bytes, dest);
|
|
|
|
|
|
|
|
g_return_val_if_fail (width + dest_x > 0, dest);
|
|
|
|
g_return_val_if_fail (height + dest_y > 0, dest);
|
|
|
|
|
|
|
|
g_return_val_if_fail (!dest || dest->width >= width + dest_x, dest);
|
|
|
|
g_return_val_if_fail (!dest || dest->height >= height + dest_y, dest);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* some bounds checking */
|
2001-02-06 05:52:57 +08:00
|
|
|
x1 = CLAMP (x, 0, src->width - 1);
|
|
|
|
y1 = CLAMP (y, 0, src->height - 1);
|
|
|
|
x2 = CLAMP (x + width - 1, 0, src->width - 1);
|
|
|
|
y2 = CLAMP (y + height - 1, 0, src->height - 1);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (!(x2 - x1) || !(y2 - y1))
|
|
|
|
return dest;
|
|
|
|
|
2001-02-06 05:52:57 +08:00
|
|
|
width = x2 - x1 + 1;
|
|
|
|
height = y2 - y1 + 1;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-02-06 05:52:57 +08:00
|
|
|
if (! dest)
|
2001-01-03 02:17:16 +08:00
|
|
|
{
|
2001-02-06 05:52:57 +08:00
|
|
|
new = temp_buf_new (width + dest_x,
|
|
|
|
height + dest_y,
|
|
|
|
src->bytes,
|
|
|
|
0, 0,
|
|
|
|
empty);
|
2001-01-03 02:17:16 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
new = dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the region */
|
2001-01-03 02:17:16 +08:00
|
|
|
srcR.bytes = src->bytes;
|
2001-02-06 05:52:57 +08:00
|
|
|
srcR.w = width;
|
|
|
|
srcR.h = height;
|
1997-11-25 06:05:25 +08:00
|
|
|
srcR.rowstride = src->bytes * src->width;
|
2001-02-06 05:52:57 +08:00
|
|
|
srcR.data = (temp_buf_data (src) +
|
|
|
|
y1 * srcR.rowstride +
|
|
|
|
x1 * srcR.bytes);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-02-06 05:52:57 +08:00
|
|
|
destR.bytes = dest->bytes;
|
1997-11-25 06:05:25 +08:00
|
|
|
destR.rowstride = new->bytes * new->width;
|
2001-02-06 05:52:57 +08:00
|
|
|
destR.data = (temp_buf_data (new) +
|
|
|
|
dest_y * destR.rowstride +
|
|
|
|
dest_x * destR.bytes);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
copy_region (&srcR, &destR);
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-07-20 03:46:05 +08:00
|
|
|
temp_buf_free (TempBuf *temp_buf)
|
2001-05-20 00:09:41 +08:00
|
|
|
{
|
2001-11-13 11:31:47 +08:00
|
|
|
g_return_if_fail (temp_buf != NULL);
|
|
|
|
|
2001-05-20 00:09:41 +08:00
|
|
|
if (temp_buf->data)
|
1997-11-25 06:05:25 +08:00
|
|
|
g_free (temp_buf->data);
|
|
|
|
|
|
|
|
if (temp_buf->swapped)
|
|
|
|
temp_buf_swap_free (temp_buf);
|
|
|
|
|
|
|
|
g_free (temp_buf);
|
|
|
|
}
|
|
|
|
|
2000-04-28 01:27:28 +08:00
|
|
|
guchar *
|
1999-07-20 03:46:05 +08:00
|
|
|
temp_buf_data (TempBuf *temp_buf)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
if (temp_buf->swapped)
|
|
|
|
temp_buf_unswap (temp_buf);
|
|
|
|
|
|
|
|
return temp_buf->data;
|
|
|
|
}
|
|
|
|
|
2001-01-03 01:01:30 +08:00
|
|
|
guchar *
|
|
|
|
temp_buf_data_clear (TempBuf *temp_buf)
|
|
|
|
{
|
|
|
|
if (temp_buf->swapped)
|
|
|
|
temp_buf_unswap (temp_buf);
|
|
|
|
|
2001-01-03 02:17:16 +08:00
|
|
|
memset (temp_buf->data, 0,
|
|
|
|
temp_buf->height * temp_buf->width * temp_buf->bytes);
|
|
|
|
|
2001-01-03 01:01:30 +08:00
|
|
|
return temp_buf->data;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* Mask buffer functions *
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
MaskBuf *
|
2000-04-28 01:27:28 +08:00
|
|
|
mask_buf_new (gint width,
|
|
|
|
gint height)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-04-28 01:27:28 +08:00
|
|
|
static guchar empty = 0;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-01-03 02:17:16 +08:00
|
|
|
return temp_buf_new (width, height, 1, 0, 0, &empty);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-07-20 03:46:05 +08:00
|
|
|
mask_buf_free (MaskBuf *mask)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
temp_buf_free ((TempBuf *) mask);
|
|
|
|
}
|
|
|
|
|
2000-04-28 01:27:28 +08:00
|
|
|
guchar *
|
1999-07-20 03:46:05 +08:00
|
|
|
mask_buf_data (MaskBuf *mask_buf)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-01-03 03:39:12 +08:00
|
|
|
return temp_buf_data ((TempBuf *) mask_buf);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2001-01-03 02:17:16 +08:00
|
|
|
guchar *
|
|
|
|
mask_buf_data_clear (MaskBuf *mask_buf)
|
|
|
|
{
|
|
|
|
return temp_buf_data_clear ((TempBuf *) mask_buf);
|
|
|
|
}
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* temp buffer disk caching functions *
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* NOTES:
|
|
|
|
* Disk caching is setup as follows:
|
|
|
|
* On a call to temp_buf_swap, the TempBuf parameter is stored
|
|
|
|
* in a temporary variable called cached_in_memory.
|
|
|
|
* On the next call to temp_buf_swap, if cached_in_memory is non-null,
|
|
|
|
* cached_in_memory is moved to disk, and the latest TempBuf parameter
|
|
|
|
* is stored in cached_in_memory. This method keeps the latest TempBuf
|
|
|
|
* structure in memory instead of moving it directly to disk as requested.
|
|
|
|
* On a call to temp_buf_unswap, if cached_in_memory is non-null, it is
|
|
|
|
* compared against the requested TempBuf. If they are the same, nothing
|
|
|
|
* must be moved in from disk since it still resides in memory. However,
|
|
|
|
* if the two pointers are different, the requested TempBuf is retrieved
|
|
|
|
* from disk. In the former case, cached_in_memory is set to NULL;
|
|
|
|
* in the latter case, cached_in_memory is left unchanged.
|
|
|
|
* If temp_buf_swap_free is called, cached_in_memory must be checked
|
1999-07-20 03:46:05 +08:00
|
|
|
* against the temp buf being freed. If they are the same, then
|
|
|
|
* cached_in_memory must be set to NULL;
|
1997-11-25 06:05:25 +08:00
|
|
|
*
|
|
|
|
* In the case where memory usage is set to "stingy":
|
|
|
|
* temp bufs are not cached in memory at all, they go right to disk.
|
|
|
|
*/
|
|
|
|
|
1999-07-20 03:46:05 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* a static counter for generating unique filenames */
|
2000-04-28 01:27:28 +08:00
|
|
|
static gint swap_index = 0;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* a static pointer which keeps track of the last request for a swapped buffer */
|
|
|
|
static TempBuf * cached_in_memory = NULL;
|
|
|
|
|
|
|
|
|
2000-04-28 01:27:28 +08:00
|
|
|
static gchar *
|
1997-11-25 06:05:25 +08:00
|
|
|
generate_unique_filename (void)
|
|
|
|
{
|
2001-10-24 23:56:33 +08:00
|
|
|
pid_t pid;
|
|
|
|
gchar *swapfile;
|
|
|
|
gchar *path;
|
2001-01-03 02:17:16 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
pid = getpid ();
|
2001-10-24 23:56:33 +08:00
|
|
|
|
|
|
|
swapfile = g_strdup_printf ("gimp%d.%d",
|
|
|
|
(gint) pid,
|
|
|
|
swap_index++);
|
|
|
|
|
|
|
|
path = g_build_filename (base_config->temp_path, swapfile, NULL);
|
|
|
|
|
|
|
|
g_free (swapfile);
|
|
|
|
|
|
|
|
return path;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-07-20 03:46:05 +08:00
|
|
|
temp_buf_swap (TempBuf *buf)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-01-03 02:17:16 +08:00
|
|
|
TempBuf *swap;
|
|
|
|
gchar *filename;
|
|
|
|
struct stat stat_buf;
|
|
|
|
gint err;
|
|
|
|
FILE *fp;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (!buf || buf->swapped)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Set the swapped flag */
|
|
|
|
buf->swapped = TRUE;
|
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
if (base_config->stingy_memory_use)
|
2001-10-24 23:56:33 +08:00
|
|
|
{
|
|
|
|
swap = buf;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
swap = cached_in_memory;
|
|
|
|
cached_in_memory = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For the case where there is no temp buf ready to be moved to disk, return */
|
|
|
|
if (!swap)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Get a unique filename for caching the data to a UNIX file */
|
|
|
|
filename = generate_unique_filename ();
|
|
|
|
|
|
|
|
/* Check if generated filename is valid */
|
|
|
|
err = stat (filename, &stat_buf);
|
|
|
|
if (!err)
|
|
|
|
{
|
|
|
|
if (stat_buf.st_mode & S_IFDIR)
|
|
|
|
{
|
1999-12-28 18:30:50 +08:00
|
|
|
g_message ("Error in temp buf caching: \"%s\" is a directory (cannot overwrite)", filename);
|
1997-11-25 06:05:25 +08:00
|
|
|
g_free (filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open file for overwrite */
|
1998-08-01 02:34:05 +08:00
|
|
|
if ((fp = fopen (filename, "wb")))
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
1998-10-10 15:49:59 +08:00
|
|
|
size_t blocks_written;
|
|
|
|
blocks_written = fwrite (swap->data, swap->width * swap->height * swap->bytes, 1, fp);
|
|
|
|
/* Check whether all bytes were written and fclose() was able to flush its buffers */
|
|
|
|
if ((0 != fclose (fp)) || (1 != blocks_written))
|
|
|
|
{
|
|
|
|
(void) unlink (filename);
|
|
|
|
perror ("Write error on temp buf");
|
1999-12-28 18:30:50 +08:00
|
|
|
g_message ("Cannot write \"%s\"", filename);
|
1998-10-10 15:49:59 +08:00
|
|
|
g_free (filename);
|
|
|
|
return;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-10-10 15:49:59 +08:00
|
|
|
(void) unlink (filename);
|
1997-11-25 06:05:25 +08:00
|
|
|
perror ("Error in temp buf caching");
|
1999-12-28 18:30:50 +08:00
|
|
|
g_message ("Cannot write \"%s\"", filename);
|
1997-11-25 06:05:25 +08:00
|
|
|
g_free (filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Finally, free the buffer's data */
|
|
|
|
g_free (swap->data);
|
|
|
|
swap->data = NULL;
|
|
|
|
|
|
|
|
swap->filename = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-07-20 03:46:05 +08:00
|
|
|
temp_buf_unswap (TempBuf *buf)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-01-03 02:17:16 +08:00
|
|
|
struct stat stat_buf;
|
|
|
|
FILE *fp;
|
|
|
|
gboolean succ = FALSE;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (!buf || !buf->swapped)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Set the swapped flag */
|
|
|
|
buf->swapped = FALSE;
|
|
|
|
|
|
|
|
/* If the requested temp buf is still in memory, simply return */
|
|
|
|
if (cached_in_memory == buf)
|
|
|
|
{
|
|
|
|
cached_in_memory = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate memory for the buffer's data */
|
|
|
|
buf->data = temp_buf_allocate (buf->width * buf->height * buf->bytes);
|
|
|
|
|
|
|
|
/* Find out if the filename of the swapped data is an existing file... */
|
1998-04-28 11:50:21 +08:00
|
|
|
/* (buf->filname HAS to be != 0 */
|
1997-11-25 06:05:25 +08:00
|
|
|
if (!stat (buf->filename, &stat_buf))
|
|
|
|
{
|
1998-08-01 02:34:05 +08:00
|
|
|
if ((fp = fopen (buf->filename, "rb")))
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
1998-10-10 15:49:59 +08:00
|
|
|
size_t blocks_read;
|
|
|
|
blocks_read = fread (buf->data, buf->width * buf->height * buf->bytes, 1, fp);
|
|
|
|
(void) fclose (fp);
|
|
|
|
if (blocks_read != 1)
|
|
|
|
perror ("Read error on temp buf");
|
1998-04-28 11:50:21 +08:00
|
|
|
else
|
|
|
|
succ = TRUE;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
perror ("Error in temp buf caching");
|
|
|
|
|
|
|
|
/* Delete the swap file */
|
|
|
|
unlink (buf->filename);
|
|
|
|
}
|
1998-04-28 11:50:21 +08:00
|
|
|
if (!succ)
|
1999-12-28 18:30:50 +08:00
|
|
|
g_message ("Error in temp buf caching: information swapped to disk was lost!");
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1998-04-28 11:50:21 +08:00
|
|
|
g_free (buf->filename); /* free filename */
|
1997-11-25 06:05:25 +08:00
|
|
|
buf->filename = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-07-20 03:46:05 +08:00
|
|
|
temp_buf_swap_free (TempBuf *buf)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
struct stat stat_buf;
|
|
|
|
|
|
|
|
if (!buf->swapped)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Set the swapped flag */
|
|
|
|
buf->swapped = FALSE;
|
|
|
|
|
|
|
|
/* If the requested temp buf is cached in memory... */
|
|
|
|
if (cached_in_memory == buf)
|
|
|
|
{
|
|
|
|
cached_in_memory = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find out if the filename of the swapped data is an existing file... */
|
|
|
|
if (!stat (buf->filename, &stat_buf))
|
|
|
|
{
|
|
|
|
/* Delete the swap file */
|
|
|
|
unlink (buf->filename);
|
|
|
|
}
|
|
|
|
else
|
1998-05-28 17:03:57 +08:00
|
|
|
g_message ("Error in temp buf disk swapping: information swapped to disk was lost!");
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (buf->filename)
|
|
|
|
g_free (buf->filename); /* free filename */
|
|
|
|
buf->filename = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
swapping_free (void)
|
|
|
|
{
|
|
|
|
if (cached_in_memory)
|
|
|
|
temp_buf_free (cached_in_memory);
|
|
|
|
}
|