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
|
/* GIMP - The GNU 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 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 <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <glib-object.h>
#include "core-types.h"
#include "base/temp-buf.h"
#include "gimppreviewcache.h"
#define MAX_CACHE_PREVIEWS 5
typedef struct
{
TempBuf *buf;
gint width;
gint height;
} PreviewNearest;
static gint
preview_cache_compare (gconstpointer a,
gconstpointer b)
{
const TempBuf *buf1 = a;
const TempBuf *buf2 = b;
if (buf1->width > buf2->width && buf1->height > buf2->height)
return -1;
return 1;
}
static void
preview_cache_find_exact (gpointer data,
gpointer udata)
{
TempBuf *buf = data;
PreviewNearest *nearest = udata;
if (nearest->buf)
return;
if (buf->width == nearest->width && buf->height == nearest->height)
{
nearest->buf = buf;
return;
}
}
static void
preview_cache_find_biggest (gpointer data,
gpointer udata)
{
TempBuf *buf = data;
PreviewNearest *nearest = udata;
if (buf->width >= nearest->width && buf->height >= nearest->height)
{
/* Ok we could make the preview out of this one...
* If we already have it are these bigger dimensions?
*/
if (nearest->buf)
{
if (nearest->buf->width > buf->width &&
nearest->buf->height > buf->height)
return;
}
nearest->buf = buf;
}
}
static void
preview_cache_remove_smallest (GSList **plist)
{
GSList *list;
TempBuf *smallest = NULL;
#ifdef PREVIEW_CACHE_DEBUG
g_print ("preview_cache_remove_smallest\n");
#endif
for (list = *plist; list; list = list->next)
{
if (!smallest)
{
smallest = list->data;
}
else
{
TempBuf *this = list->data;
if (smallest->height * smallest->width > this->height * this->width)
{
smallest = this;
}
}
}
if (smallest)
{
*plist = g_slist_remove (*plist, smallest);
#ifdef PREVIEW_CACHE_DEBUG
g_print ("preview_cache_remove_smallest: removed %d x %d\n",
smallest->width, smallest->height);
#endif
temp_buf_free (smallest);
}
}
#ifdef PREVIEW_CACHE_DEBUG
static void
preview_cache_print (GSList *plist)
{
GSList *list;
g_print ("preview cache dump:\n");
for (list = plist; list; list = list->next)
{
TempBuf *buf = (TempBuf *) list->data;
g_print ("\tvalue w,h [%d,%d]\n", buf->width, buf->height);
}
}
#endif /* PREVIEW_CACHE_DEBUG */
void
gimp_preview_cache_invalidate (GSList **plist)
{
#ifdef PREVIEW_CACHE_DEBUG
g_print ("gimp_preview_cache_invalidate\n");
preview_cache_print (*plist);
#endif
g_slist_free_full (*plist, (GDestroyNotify) temp_buf_free);
*plist = NULL;
}
void
gimp_preview_cache_add (GSList **plist,
TempBuf *buf)
{
#ifdef PREVIEW_CACHE_DEBUG
g_print ("gimp_preview_cache_add: %d x %d\n", buf->width, buf->height);
preview_cache_print (*plist);
#endif
if (g_slist_length (*plist) >= MAX_CACHE_PREVIEWS)
{
preview_cache_remove_smallest (plist);
}
*plist = g_slist_insert_sorted (*plist, buf, preview_cache_compare);
}
TempBuf *
gimp_preview_cache_get (GSList **plist,
gint width,
gint height)
{
PreviewNearest pn;
#ifdef PREVIEW_CACHE_DEBUG
g_print ("gimp_preview_cache_get: %d x %d\n", width, height);
preview_cache_print (*plist);
#endif
pn.buf = NULL;
pn.width = width;
pn.height = height;
g_slist_foreach (*plist, preview_cache_find_exact, &pn);
if (pn.buf)
{
#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;
}
g_slist_foreach (*plist, preview_cache_find_biggest, &pn);
if (pn.buf)
{
TempBuf *preview;
gint pwidth;
gint pheight;
gdouble x_ratio;
gdouble y_ratio;
guchar *src_data;
guchar *dest_data;
gint loop1;
gint loop2;
#ifdef PREVIEW_CACHE_DEBUG
g_print ("gimp_preview_cache_get: nearest value: %d x %d\n",
pn.buf->width, pn.buf->height);
#endif
/* Make up new preview from the large one... */
pwidth = pn.buf->width;
pheight = pn.buf->height;
/* Now get the real one and add to cache */
preview = temp_buf_new (width, height, pn.buf->bytes, 0, 0, NULL);
/* preview from nearest bigger one */
if (width)
x_ratio = (gdouble) pwidth / (gdouble) width;
else
x_ratio = 0.0;
if (height)
y_ratio = (gdouble) pheight / (gdouble) height;
else
y_ratio = 0.0;
src_data = temp_buf_get_data (pn.buf);
dest_data = temp_buf_get_data (preview);
for (loop1 = 0 ; loop1 < height ; loop1++)
for (loop2 = 0 ; loop2 < width ; loop2++)
{
gint i;
guchar *src_pixel;
guchar *dest_pixel;
src_pixel = src_data +
((gint) (loop2 * x_ratio)) * preview->bytes +
((gint) (loop1 * y_ratio)) * pwidth * preview->bytes;
dest_pixel = dest_data +
(loop2 + loop1 * width) * preview->bytes;
for (i = 0; i < preview->bytes; i++)
*dest_pixel++ = *src_pixel++;
}
gimp_preview_cache_add (plist, preview);
return preview;
}
#ifdef PREVIEW_CACHE_DEBUG
g_print ("gimp_preview_cache_get returning NULL\n");
#endif
return NULL;
}
gsize
gimp_preview_cache_get_memsize (GSList *cache)
{
GSList *list;
gsize memsize = 0;
if (! cache)
return 0;
for (list = cache; list; list = list->next)
memsize += sizeof (GSList) + temp_buf_get_memsize ((TempBuf *) list->data);
return memsize;
}
|