Commit a2952f2b authored by Michael Natterer's avatar Michael Natterer 😴 Committed by Michael Natterer
Browse files

temp_buf_data_clear(): the temp_buf's size is (width * height * bytes).

2001-01-02  Michael Natterer  <mitch@gimp.org>

	* app/temp_buf.[ch]: temp_buf_data_clear(): the temp_buf's size is
	(width * height * bytes). Cleanups.

	* app/fileops.c: removed a debugging g_print().
parent b4fc3293
...@@ -37,8 +37,6 @@ ...@@ -37,8 +37,6 @@
#include "apptypes.h" #include "apptypes.h"
#include "appenv.h" #include "appenv.h"
#include "drawable.h"
#include "gdisplay.h"
#include "gimpimage.h" #include "gimpimage.h"
#include "gimprc.h" #include "gimprc.h"
#include "paint_funcs.h" #include "paint_funcs.h"
...@@ -49,10 +47,10 @@ ...@@ -49,10 +47,10 @@
static guchar * temp_buf_allocate (guint); static guchar * temp_buf_allocate (guint);
static void temp_buf_to_color (TempBuf *, static void temp_buf_to_color (TempBuf *src_buf,
TempBuf *); TempBuf *dest_buf);
static void temp_buf_to_gray (TempBuf *, static void temp_buf_to_gray (TempBuf *src_buf,
TempBuf *); TempBuf *dest_buf);
/* Memory management */ /* Memory management */
...@@ -76,7 +74,7 @@ temp_buf_to_color (TempBuf *src_buf, ...@@ -76,7 +74,7 @@ temp_buf_to_color (TempBuf *src_buf,
{ {
guchar *src; guchar *src;
guchar *dest; guchar *dest;
long num_bytes; glong num_bytes;
src = temp_buf_data (src_buf); src = temp_buf_data (src_buf);
dest = temp_buf_data (dest_buf); dest = temp_buf_data (dest_buf);
...@@ -100,8 +98,8 @@ temp_buf_to_gray (TempBuf *src_buf, ...@@ -100,8 +98,8 @@ temp_buf_to_gray (TempBuf *src_buf,
{ {
guchar *src; guchar *src;
guchar *dest; guchar *dest;
long num_bytes; glong num_bytes;
float pix; gfloat pix;
src = temp_buf_data (src_buf); src = temp_buf_data (src_buf);
dest = temp_buf_data (dest_buf); dest = temp_buf_data (dest_buf);
...@@ -118,7 +116,6 @@ temp_buf_to_gray (TempBuf *src_buf, ...@@ -118,7 +116,6 @@ temp_buf_to_gray (TempBuf *src_buf,
} }
} }
TempBuf * TempBuf *
temp_buf_new (gint width, temp_buf_new (gint width,
gint height, gint height,
...@@ -127,19 +124,19 @@ temp_buf_new (gint width, ...@@ -127,19 +124,19 @@ temp_buf_new (gint width,
gint y, gint y,
guchar *col) guchar *col)
{ {
long i; glong i;
int j; gint j;
guchar *data; guchar *data;
TempBuf *temp; TempBuf *temp;
temp = g_new (TempBuf, 1); temp = g_new (TempBuf, 1);
temp->width = width; temp->width = width;
temp->height = height; temp->height = height;
temp->bytes = bytes; temp->bytes = bytes;
temp->x = x; temp->x = x;
temp->y = y; temp->y = y;
temp->swapped = FALSE; temp->swapped = FALSE;
temp->filename = NULL; temp->filename = NULL;
temp->data = data = temp_buf_allocate (width * height * bytes); temp->data = data = temp_buf_allocate (width * height * bytes);
...@@ -156,26 +153,33 @@ temp_buf_new (gint width, ...@@ -156,26 +153,33 @@ temp_buf_new (gint width,
{ {
memset (data, *col, width * height * 3); memset (data, *col, width * height * 3);
} }
else if ((bytes == 4) && (col[1] == *col) && (*col == col[2]) && (col[2] == col[3])) else if ((bytes == 4) &&
(col[1] == *col) && (*col == col[2]) && (col[2] == col[3]))
{ {
memset (data, *col, (width * height) << 2); memset (data, *col, (width * height) << 2);
} }
else else
{ {
/* No, we cannot */ /* No, we cannot */
guchar * dptr; guchar *dptr;
/* Fill the first row */ /* Fill the first row */
dptr = data; dptr = data;
for (i = width - 1; i >= 0; --i) for (i = width - 1; i >= 0; --i)
{ {
guchar * init; guchar *init;
j = bytes;
j = bytes;
init = col; init = col;
while (j--) while (j--)
*dptr++ = *init++; *dptr++ = *init++;
} }
/* Now copy from it (we set bytes to bytesperrow now) */ /* Now copy from it (we set bytes to bytesperrow now) */
bytes *= width; bytes *= width;
while (--height) while (--height)
{ {
memcpy (dptr, data, bytes); memcpy (dptr, data, bytes);
...@@ -187,13 +191,12 @@ temp_buf_new (gint width, ...@@ -187,13 +191,12 @@ temp_buf_new (gint width,
return temp; return temp;
} }
TempBuf * TempBuf *
temp_buf_copy (TempBuf *src, temp_buf_copy (TempBuf *src,
TempBuf *dest) TempBuf *dest)
{ {
TempBuf * new; TempBuf *new;
long length; glong length;
if (!src) if (!src)
{ {
...@@ -202,7 +205,9 @@ temp_buf_copy (TempBuf *src, ...@@ -202,7 +205,9 @@ temp_buf_copy (TempBuf *src,
} }
if (!dest) if (!dest)
new = temp_buf_new (src->width, src->height, src->bytes, 0, 0, NULL); {
new = temp_buf_new (src->width, src->height, src->bytes, 0, 0, NULL);
}
else else
{ {
new = dest; new = dest;
...@@ -218,6 +223,7 @@ temp_buf_copy (TempBuf *src, ...@@ -218,6 +223,7 @@ temp_buf_copy (TempBuf *src,
temp_buf_to_color (src, new); temp_buf_to_color (src, new);
else else
g_message ("Cannot convert from indexed color."); g_message ("Cannot convert from indexed color.");
return new; return new;
} }
} }
...@@ -229,23 +235,24 @@ temp_buf_copy (TempBuf *src, ...@@ -229,23 +235,24 @@ temp_buf_copy (TempBuf *src,
return new; return new;
} }
TempBuf * TempBuf *
temp_buf_resize (TempBuf *buf, temp_buf_resize (TempBuf *buf,
gint bytes, gint bytes,
gint x, gint x,
gint y, gint y,
gint w, gint width,
gint h) gint height)
{ {
gint size; gint size;
/* calculate the requested size */ /* calculate the requested size */
size = w * h * bytes; size = width * height * bytes;
/* First, configure the canvas buffer */ /* First, configure the canvas buffer */
if (!buf) if (!buf)
buf = temp_buf_new (w, h, bytes, x, y, NULL); {
buf = temp_buf_new (width, height, bytes, x, y, NULL);
}
else else
{ {
if (size != (buf->width * buf->height * buf->bytes)) if (size != (buf->width * buf->height * buf->bytes))
...@@ -258,30 +265,29 @@ temp_buf_resize (TempBuf *buf, ...@@ -258,30 +265,29 @@ temp_buf_resize (TempBuf *buf,
} }
/* Make sure the temp buf fields are valid */ /* Make sure the temp buf fields are valid */
buf->x = x; buf->x = x;
buf->y = y; buf->y = y;
buf->width = w; buf->width = width;
buf->height = h; buf->height = height;
buf->bytes = bytes; buf->bytes = bytes;
} }
return buf; return buf;
} }
TempBuf * TempBuf *
temp_buf_copy_area (TempBuf *src, temp_buf_copy_area (TempBuf *src,
TempBuf *dest, TempBuf *dest,
gint x, gint x,
gint y, gint y,
gint w, gint width,
gint h, gint height,
gint border) gint border)
{ {
TempBuf * new; TempBuf *new;
PixelRegion srcR, destR; PixelRegion srcR, destR;
guchar empty[MAX_CHANNELS] = { 0, 0, 0, 0 }; guchar empty[MAX_CHANNELS] = { 0, 0, 0, 0 };
gint x1, y1, x2, y2; gint x1, y1, x2, y2;
if (!src) if (!src)
{ {
...@@ -292,19 +298,21 @@ temp_buf_copy_area (TempBuf *src, ...@@ -292,19 +298,21 @@ temp_buf_copy_area (TempBuf *src,
/* some bounds checking */ /* some bounds checking */
x1 = CLAMP (x, 0, src->width); x1 = CLAMP (x, 0, src->width);
y1 = CLAMP (y, 0, src->height); y1 = CLAMP (y, 0, src->height);
x2 = CLAMP (x + w, 0, src->width); x2 = CLAMP (x + width, 0, src->width);
y2 = CLAMP (y + h, 0, src->height); y2 = CLAMP (y + height, 0, src->height);
if (!(x2 - x1) || !(y2 - y1)) if (!(x2 - x1) || !(y2 - y1))
return dest; return dest;
x = x1 - border; x = x1 - border;
y = y1 - border; y = y1 - border;
w = (x2 - x1) + border * 2; width = (x2 - x1) + border * 2;
h = (y2 - y1) + border * 2; height = (y2 - y1) + border * 2;
if (!dest) if (!dest)
new = temp_buf_new (w, h, src->bytes, x, y, empty); {
new = temp_buf_new (width, height, src->bytes, x, y, empty);
}
else else
{ {
new = dest; new = dest;
...@@ -317,21 +325,20 @@ temp_buf_copy_area (TempBuf *src, ...@@ -317,21 +325,20 @@ temp_buf_copy_area (TempBuf *src,
new->y = src->y + y; new->y = src->y + y;
/* Copy the region */ /* Copy the region */
srcR.bytes = src->bytes; srcR.bytes = src->bytes;
srcR.w = (x2 - x1); srcR.w = (x2 - x1);
srcR.h = (y2 - y1); srcR.h = (y2 - y1);
srcR.rowstride = src->bytes * src->width; srcR.rowstride = src->bytes * src->width;
srcR.data = temp_buf_data (src) + y1 * srcR.rowstride + x1 * srcR.bytes; srcR.data = temp_buf_data (src) + y1 * srcR.rowstride + x1 * srcR.bytes;
destR.rowstride = new->bytes * new->width; destR.rowstride = new->bytes * new->width;
destR.data = temp_buf_data (new) + (y1 - y) * destR.rowstride + (x1 - x) * srcR.bytes; destR.data = temp_buf_data (new) + (y1 - y) * destR.rowstride + (x1 - x) * srcR.bytes;
copy_region (&srcR, &destR); copy_region (&srcR, &destR);
return new; return new;
} }
void void
temp_buf_free (TempBuf *temp_buf) temp_buf_free (TempBuf *temp_buf)
{ {
...@@ -344,7 +351,6 @@ temp_buf_free (TempBuf *temp_buf) ...@@ -344,7 +351,6 @@ temp_buf_free (TempBuf *temp_buf)
g_free (temp_buf); g_free (temp_buf);
} }
guchar * guchar *
temp_buf_data (TempBuf *temp_buf) temp_buf_data (TempBuf *temp_buf)
{ {
...@@ -360,7 +366,9 @@ temp_buf_data_clear (TempBuf *temp_buf) ...@@ -360,7 +366,9 @@ temp_buf_data_clear (TempBuf *temp_buf)
if (temp_buf->swapped) if (temp_buf->swapped)
temp_buf_unswap (temp_buf); temp_buf_unswap (temp_buf);
memset (temp_buf->data, 0, temp_buf->height * temp_buf->width); memset (temp_buf->data, 0,
temp_buf->height * temp_buf->width * temp_buf->bytes);
return temp_buf->data; return temp_buf->data;
} }
...@@ -375,26 +383,33 @@ mask_buf_new (gint width, ...@@ -375,26 +383,33 @@ mask_buf_new (gint width,
{ {
static guchar empty = 0; static guchar empty = 0;
return (temp_buf_new (width, height, 1, 0, 0, &empty)); return temp_buf_new (width, height, 1, 0, 0, &empty);
} }
void void
mask_buf_free (MaskBuf *mask) mask_buf_free (MaskBuf *mask)
{ {
temp_buf_free ((TempBuf *) mask); temp_buf_free ((TempBuf *) mask);
} }
guchar * guchar *
mask_buf_data (MaskBuf *mask_buf) mask_buf_data (MaskBuf *mask_buf)
{ {
if (mask_buf->swapped) if (mask_buf->swapped)
temp_buf_unswap (mask_buf); temp_buf_unswap ((TempBuf *) mask_buf);
return mask_buf->data; return mask_buf->data;
} }
guchar *
mask_buf_data_clear (MaskBuf *mask_buf)
{
if (mask_buf->swapped)
temp_buf_unswap ((TempBuf *) mask_buf);
return temp_buf_data_clear ((TempBuf *) mask_buf);
}
/****************************************************************** /******************************************************************
* temp buffer disk caching functions * * temp buffer disk caching functions *
...@@ -434,20 +449,20 @@ static gchar * ...@@ -434,20 +449,20 @@ static gchar *
generate_unique_filename (void) generate_unique_filename (void)
{ {
pid_t pid; pid_t pid;
pid = getpid (); pid = getpid ();
return g_strdup_printf ("%s" G_DIR_SEPARATOR_S "gimp%d.%d", return g_strdup_printf ("%s" G_DIR_SEPARATOR_S "gimp%d.%d",
temp_path, (int) pid, swap_index++); temp_path, (int) pid, swap_index++);
} }
void void
temp_buf_swap (TempBuf *buf) temp_buf_swap (TempBuf *buf)
{ {
TempBuf * swap; TempBuf *swap;
gchar * filename; gchar *filename;
struct stat stat_buf; struct stat stat_buf;
gint err; gint err;
FILE * fp; FILE *fp;
if (!buf || buf->swapped) if (!buf || buf->swapped)
return; return;
...@@ -512,13 +527,12 @@ temp_buf_swap (TempBuf *buf) ...@@ -512,13 +527,12 @@ temp_buf_swap (TempBuf *buf)
swap->filename = filename; swap->filename = filename;
} }
void void
temp_buf_unswap (TempBuf *buf) temp_buf_unswap (TempBuf *buf)
{ {
struct stat stat_buf; struct stat stat_buf;
FILE * fp; FILE *fp;
gboolean succ = FALSE; gboolean succ = FALSE;
if (!buf || !buf->swapped) if (!buf || !buf->swapped)
return; return;
...@@ -563,7 +577,6 @@ temp_buf_unswap (TempBuf *buf) ...@@ -563,7 +577,6 @@ temp_buf_unswap (TempBuf *buf)
buf->filename = NULL; buf->filename = NULL;
} }
void void
temp_buf_swap_free (TempBuf *buf) temp_buf_swap_free (TempBuf *buf)
{ {
...@@ -596,7 +609,6 @@ temp_buf_swap_free (TempBuf *buf) ...@@ -596,7 +609,6 @@ temp_buf_swap_free (TempBuf *buf)
buf->filename = NULL; buf->filename = NULL;
} }
void void
swapping_free (void) swapping_free (void)
{ {
......
...@@ -36,43 +36,44 @@ struct _TempBuf ...@@ -36,43 +36,44 @@ struct _TempBuf
/* The temp buffer functions */ /* The temp buffer functions */
TempBuf * temp_buf_new (gint , TempBuf * temp_buf_new (gint width,
gint , gint height,
gint , gint bytes,
gint , gint x,
gint , gint y,
guchar *); guchar *col);
TempBuf * temp_buf_copy (TempBuf *, TempBuf * temp_buf_copy (TempBuf *src,
TempBuf *); TempBuf *dest);
TempBuf * temp_buf_resize (TempBuf *, TempBuf * temp_buf_resize (TempBuf *buf,
gint , gint bytes,
gint , gint x,
gint , gint y,
gint , gint width,
gint ); gint height);
TempBuf * temp_buf_copy_area (TempBuf *, TempBuf * temp_buf_copy_area (TempBuf *src,
TempBuf *, TempBuf *dest,
gint , gint x,
gint , gint y,
gint , gint width,
gint , gint height,
gint ); gint border);
void temp_buf_free (TempBuf *); void temp_buf_free (TempBuf *buf);
guchar * temp_buf_data (TempBuf *); guchar * temp_buf_data (TempBuf *buf);
guchar * temp_buf_data_clear (TempBuf *); guchar * temp_buf_data_clear (TempBuf *buf);
/* The mask buffer functions */ /* The mask buffer functions */
MaskBuf * mask_buf_new (gint , MaskBuf * mask_buf_new (gint width,
gint ); gint height);
void mask_buf_free (MaskBuf *); void mask_buf_free (MaskBuf *mask_buf);
guchar * mask_buf_data (MaskBuf *); guchar * mask_buf_data (MaskBuf *mask_buf);
guchar * mask_buf_data_clear (MaskBuf *mask_buf);
/* The disk caching functions */ /* The disk caching functions */
void temp_buf_swap (TempBuf *); void temp_buf_swap (TempBuf *buf);
void temp_buf_unswap (TempBuf *); void temp_buf_unswap (TempBuf *buf);