1999-06-24 07:01:14 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1999 Andy Thomas alt@gimp.org
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2000-12-11 11:33:25 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
#include <glib-object.h>
|
2000-12-29 23:22:01 +08:00
|
|
|
|
2002-05-03 20:45:22 +08:00
|
|
|
#include "core-types.h"
|
2001-05-15 19:25:25 +08:00
|
|
|
|
|
|
|
#include "base/temp-buf.h"
|
2000-12-11 11:33:25 +08:00
|
|
|
|
1999-06-24 07:01:14 +08:00
|
|
|
#include "gimppreviewcache.h"
|
|
|
|
|
2000-12-11 11:33:25 +08:00
|
|
|
|
|
|
|
#define MAX_CACHE_PREVIEWS 5
|
|
|
|
|
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
typedef struct
|
2000-12-11 11:33:25 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
TempBuf *buf;
|
2000-12-11 11:33:25 +08:00
|
|
|
gint width;
|
|
|
|
gint height;
|
|
|
|
} PreviewNearest;
|
|
|
|
|
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
static gint
|
2000-12-11 11:33:25 +08:00
|
|
|
preview_cache_compare (gconstpointer a,
|
|
|
|
gconstpointer b)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
TempBuf *buf1 = (TempBuf *) a;
|
|
|
|
TempBuf *buf2 = (TempBuf *) b;
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
if (buf1->width > buf2->width && buf1->height > buf2->height)
|
1999-06-24 07:01:14 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-09-06 00:51:51 +08:00
|
|
|
preview_cache_find_exact (gpointer data,
|
2000-12-11 11:33:25 +08:00
|
|
|
gpointer udata)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
TempBuf *buf = (TempBuf *) data;
|
|
|
|
PreviewNearest *nearest = (PreviewNearest *) udata;
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
if (nearest->buf)
|
1999-06-24 07:01:14 +08:00
|
|
|
return;
|
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
if (buf->width == nearest->width && buf->height == nearest->height)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
nearest->buf = buf;
|
1999-06-24 07:01:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-09-06 00:51:51 +08:00
|
|
|
preview_cache_find_biggest (gpointer data,
|
2000-12-11 11:33:25 +08:00
|
|
|
gpointer udata)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
TempBuf *buf = (TempBuf *) data;
|
|
|
|
PreviewNearest *nearest = (PreviewNearest *) udata;
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
if (buf->width >= nearest->width && buf->height >= nearest->height)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
|
|
|
/* Ok we could make the preview out of this one...
|
2003-09-06 00:51:51 +08:00
|
|
|
* If we already have it are these bigger dimensions?
|
1999-06-24 07:01:14 +08:00
|
|
|
*/
|
2003-09-06 00:51:51 +08:00
|
|
|
if (nearest->buf)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
if (nearest->buf->width > buf->width &&
|
|
|
|
nearest->buf->height > buf->height)
|
1999-06-24 07:01:14 +08:00
|
|
|
return;
|
|
|
|
}
|
2003-09-06 00:51:51 +08:00
|
|
|
|
|
|
|
nearest->buf = buf;
|
1999-06-24 07:01:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-12-11 11:33:25 +08:00
|
|
|
preview_cache_remove_smallest (GSList **plist)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
GSList *list;
|
|
|
|
TempBuf *smallest = NULL;
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
#ifdef PREVIEW_CACHE_DEBUG
|
|
|
|
g_print ("preview_cache_remove_smallest\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (list = *plist; list; list = list->next)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2000-12-11 11:33:25 +08:00
|
|
|
if (!smallest)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2000-12-11 11:33:25 +08:00
|
|
|
smallest = list->data;
|
1999-06-24 07:01:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
TempBuf *this = list->data;
|
2000-12-11 11:33:25 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
if ((smallest->height * smallest->width) >
|
|
|
|
(this->height * this->width))
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
smallest = this;
|
1999-06-24 07:01:14 +08:00
|
|
|
}
|
|
|
|
}
|
2003-09-06 00:51:51 +08:00
|
|
|
}
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
if (smallest)
|
|
|
|
{
|
|
|
|
*plist = g_slist_remove (*plist, smallest);
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
#ifdef PREVIEW_CACHE_DEBUG
|
|
|
|
g_print ("preview_cache_remove_smallest: removed %d x %d\n",
|
|
|
|
smallest->width, smallest->height);
|
|
|
|
#endif
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
temp_buf_free (smallest);
|
|
|
|
}
|
1999-06-24 07:01:14 +08:00
|
|
|
}
|
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
#ifdef PREVIEW_CACHE_DEBUG
|
1999-06-24 07:01:14 +08:00
|
|
|
static void
|
2000-12-11 11:33:25 +08:00
|
|
|
preview_cache_print (GSList *plist)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
GSList *list;
|
2000-12-11 11:33:25 +08:00
|
|
|
|
|
|
|
g_print ("preview cache dump:\n");
|
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
for (list = plist; list; list = list->next)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
TempBuf *buf = (TempBuf *) list->data;
|
2000-12-11 11:33:25 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
g_print ("\tvalue w,h [%d,%d]\n", buf->width, buf->height);
|
1999-06-24 07:01:14 +08:00
|
|
|
}
|
|
|
|
}
|
2003-09-06 00:51:51 +08:00
|
|
|
#endif /* PREVIEW_CACHE_DEBUG */
|
1999-06-24 07:01:14 +08:00
|
|
|
|
|
|
|
void
|
2000-12-11 11:33:25 +08:00
|
|
|
gimp_preview_cache_invalidate (GSList **plist)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
#ifdef PREVIEW_CACHE_DEBUG
|
|
|
|
g_print ("gimp_preview_cache_invalidate\n");
|
2000-12-11 11:33:25 +08:00
|
|
|
preview_cache_print (*plist);
|
2003-09-06 00:51:51 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
g_slist_foreach (*plist, (GFunc) temp_buf_free, NULL);
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
g_slist_free (*plist);
|
1999-06-24 07:01:14 +08:00
|
|
|
*plist = NULL;
|
|
|
|
}
|
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
void
|
2000-12-11 11:33:25 +08:00
|
|
|
gimp_preview_cache_add (GSList **plist,
|
|
|
|
TempBuf *buf)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
#ifdef PREVIEW_CACHE_DEBUG
|
|
|
|
g_print ("gimp_preview_cache_add: %d x %d\n", buf->width, buf->height);
|
2000-12-11 11:33:25 +08:00
|
|
|
preview_cache_print (*plist);
|
2003-09-06 00:51:51 +08:00
|
|
|
#endif
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
if (g_slist_length (*plist) >= MAX_CACHE_PREVIEWS)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
preview_cache_remove_smallest (plist);
|
1999-06-24 07:01:14 +08:00
|
|
|
}
|
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
*plist = g_slist_insert_sorted (*plist, buf, preview_cache_compare);
|
1999-06-24 07:01:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TempBuf *
|
2000-12-11 11:33:25 +08:00
|
|
|
gimp_preview_cache_get (GSList **plist,
|
|
|
|
gint width,
|
|
|
|
gint height)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
PreviewNearest pn;
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
#ifdef PREVIEW_CACHE_DEBUG
|
|
|
|
g_print ("gimp_preview_cache_get: %d x %d\n", width, height);
|
2000-12-11 11:33:25 +08:00
|
|
|
preview_cache_print (*plist);
|
2003-09-06 00:51:51 +08:00
|
|
|
#endif
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
pn.buf = NULL;
|
2000-12-11 11:33:25 +08:00
|
|
|
pn.width = width;
|
1999-06-24 07:01:14 +08:00
|
|
|
pn.height = height;
|
|
|
|
|
2000-12-11 11:33:25 +08:00
|
|
|
g_slist_foreach (*plist, preview_cache_find_exact, &pn);
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
if (pn.buf)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
#ifdef PREVIEW_CACHE_DEBUG
|
|
|
|
g_print ("gimp_preview_cache_get: found exact match %d x %d\n",
|
|
|
|
pn.buf->width, pn.buf->height);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return pn.buf;
|
1999-06-24 07:01:14 +08:00
|
|
|
}
|
|
|
|
|
2000-12-11 11:33:25 +08:00
|
|
|
g_slist_foreach (*plist, preview_cache_find_biggest, &pn);
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
if (pn.buf)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2003-09-06 00:51:51 +08:00
|
|
|
TempBuf *preview;
|
2000-12-11 11:33:25 +08:00
|
|
|
gint pwidth;
|
|
|
|
gint pheight;
|
|
|
|
gdouble x_ratio;
|
|
|
|
gdouble y_ratio;
|
|
|
|
guchar *src_data;
|
|
|
|
guchar *dest_data;
|
|
|
|
gint loop1;
|
|
|
|
gint loop2;
|
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
#ifdef PREVIEW_CACHE_DEBUG
|
|
|
|
g_print ("gimp_preview_cache_get: nearest value: %d x %d\n",
|
|
|
|
pn.buf->width, pn.buf->height);
|
|
|
|
#endif
|
1999-06-24 07:01:14 +08:00
|
|
|
|
|
|
|
/* Make up new preview from the large one... */
|
2003-09-06 00:51:51 +08:00
|
|
|
pwidth = pn.buf->width;
|
|
|
|
pheight = pn.buf->height;
|
1999-06-24 07:01:14 +08:00
|
|
|
|
|
|
|
/* Now get the real one and add to cache */
|
2003-09-06 00:51:51 +08:00
|
|
|
preview = temp_buf_new (width, height, pn.buf->bytes, 0, 0, NULL);
|
1999-11-25 07:58:46 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
/* preview from nearest bigger one */
|
1999-11-25 07:58:46 +08:00
|
|
|
if (width)
|
2000-12-11 11:33:25 +08:00
|
|
|
x_ratio = (gdouble) pwidth / (gdouble) width;
|
1999-11-25 07:58:46 +08:00
|
|
|
else
|
|
|
|
x_ratio = 0.0;
|
2000-12-11 11:33:25 +08:00
|
|
|
|
1999-11-25 07:58:46 +08:00
|
|
|
if (height)
|
2000-12-11 11:33:25 +08:00
|
|
|
y_ratio = (gdouble) pheight / (gdouble) height;
|
1999-11-25 07:58:46 +08:00
|
|
|
else
|
|
|
|
y_ratio = 0.0;
|
2000-12-11 11:33:25 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
src_data = temp_buf_data (pn.buf);
|
|
|
|
dest_data = temp_buf_data (preview);
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2000-12-11 11:33:25 +08:00
|
|
|
for (loop1 = 0 ; loop1 < height ; loop1++)
|
|
|
|
for (loop2 = 0 ; loop2 < width ; loop2++)
|
1999-06-24 07:01:14 +08:00
|
|
|
{
|
2000-12-11 11:33:25 +08:00
|
|
|
gint i;
|
|
|
|
guchar *src_pixel;
|
|
|
|
guchar *dest_pixel;
|
|
|
|
|
|
|
|
src_pixel = src_data +
|
2003-09-06 00:51:51 +08:00
|
|
|
((gint) (loop2 * x_ratio)) * preview->bytes +
|
|
|
|
((gint) (loop1 * y_ratio)) * pwidth * preview->bytes;
|
2000-12-11 11:33:25 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
dest_pixel = dest_data +
|
|
|
|
(loop2 + loop1 * width) * preview->bytes;
|
2000-12-11 11:33:25 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
for (i = 0; i < preview->bytes; i++)
|
1999-06-24 07:01:14 +08:00
|
|
|
*dest_pixel++ = *src_pixel++;
|
|
|
|
}
|
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
gimp_preview_cache_add (plist, preview);
|
1999-06-24 07:01:14 +08:00
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
return preview;
|
1999-06-24 07:01:14 +08:00
|
|
|
}
|
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
#ifdef PREVIEW_CACHE_DEBUG
|
|
|
|
g_print ("gimp_preview_cache_get returning NULL\n");
|
|
|
|
#endif
|
|
|
|
|
1999-06-24 07:01:14 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-01-31 00:14:26 +08:00
|
|
|
|
|
|
|
gsize
|
|
|
|
gimp_preview_cache_get_memsize (GSList *cache)
|
|
|
|
{
|
|
|
|
GSList *list;
|
|
|
|
gsize memsize = 0;
|
|
|
|
|
|
|
|
g_return_val_if_fail (cache != NULL, 0);
|
|
|
|
|
2003-09-06 00:51:51 +08:00
|
|
|
for (list = cache; list; list = list->next)
|
|
|
|
memsize += sizeof (GSList) + temp_buf_get_memsize ((TempBuf *) list->data);
|
2002-01-31 00:14:26 +08:00
|
|
|
|
|
|
|
return memsize;
|
|
|
|
}
|