gimppreviewcache.c 7.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* 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.
 */

19
20
21
22
#include "config.h"

#include <glib.h>

23
24
#include "gimppreviewcache.h"

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#define MAX_CACHE_PREVIEWS 5

#undef  PREVIEW_CACHE_DEBUG


typedef struct _PreviewCache 
{
  TempBuf *preview;
  gint     width;
  gint     height;
} PreviewCache;

typedef struct _PreviewNearest 
{
  PreviewCache *pc;
  gint          width;
  gint          height;
} PreviewNearest;


46
static gint 
47
48
preview_cache_compare (gconstpointer  a,
		       gconstpointer  b)
49
{
50
51
  PreviewCache *pc1 = (PreviewCache *) a;
  PreviewCache *pc2 = (PreviewCache *) b;
52

53
  if (pc1->width > pc2->width && pc1->height > pc2->height)
54
55
56
57
58
59
    return -1;

  return 1;
}

static void
60
61
preview_cache_find_exact (gpointer data, 
			  gpointer udata)
62
{
63
64
  PreviewCache   *pc       = (PreviewCache *) data;
  PreviewNearest *pNearest = (PreviewNearest *) udata;
65

66
/*   g_print ("this value w,h [%d,%d]\n",pc->width,pc->height); */
67

68
69
70
/*   if (pNearest->pc) */
/*       g_print ("current nearest value w,h [%d,%d]\n",
                 pNearest->pc->width,pNearest->pc->height); */
71

72
  if (pNearest->pc)
73
74
75
76
77
78
79
80
81
    return;

  if(pc->width == pNearest->width &&
     pc->height == pNearest->height)
    {
      /* Ok we could make the preview out of this one...
       * If we already have it are these bigger dimensions? 
       */
      pNearest->pc = pc;
82

83
84
85
86
87
      return;
    }
}

static void
88
89
preview_cache_find_biggest (gpointer data, 
			    gpointer udata)
90
{
91
92
  PreviewCache   *pc       = (PreviewCache *) data;
  PreviewNearest *pNearest = (PreviewNearest *) udata;
93

94
/*   g_print ("this value w,h [%d,%d]\n",pc->width,pc->height); */
95

96
97
98
/*   if (pNearest->pc) */
/*       g_print ("current nearest value w,h [%d,%d]\n",
                 pNearest->pc->width,pNearest->pc->height); */
99
  
100
101
  if (pc->width >= pNearest->width &&
      pc->height >= pNearest->height)
102
103
104
105
    {
      /* Ok we could make the preview out of this one...
       * If we already have it are these bigger dimensions? 
       */
106
      if (pNearest->pc)
107
	{
108
109
	  if (pNearest->pc->width > pc->width &&
	      pNearest->pc->height > pc->height)
110
111
112
113
114
115
116
	    return;
	}
      pNearest->pc = pc;
    }
}

static void
117
preview_cache_remove_smallest (GSList **plist)
118
{
119
  GSList       *list;
120
121
  PreviewCache *smallest = NULL;
  
122
/*   g_print ("Removing smallest\n"); */
123

124
  for (list = *plist; list; list = g_slist_next (list))
125
    {
126
      if (!smallest)
127
	{
128
129
130
	  smallest = list->data;
/* 	  g_print ("init smallest  %d,%d\n",
	            smallest->width,smallest->height); */
131
132
133
	}
      else
	{
134
135
136
137
138
	  PreviewCache *pcthis = list->data;

/* 	  g_print ("Checking %d,%d\n",pcthis->width,pcthis->height); */
	  if ((smallest->height * smallest->width) >=
	      (pcthis->height * pcthis->width))
139
140
	    {
	      smallest = pcthis;
141
142
/* 	      g_print ("smallest now  %d,%d\n",
                      smallest->width,smallest->height); */
143
144
	    }
	}
145
    } 
146

147
148
149
150
  if (*plist && smallest)
    *plist = g_slist_remove (*plist, smallest);
/*   g_print ("removed %d,%d\n",smallest->width,smallest->height); */
/*   g_print ("removed smallest\n"); */
151
152
153
}

static void
154
155
preview_cache_invalidate (gpointer data, 
			  gpointer udata)
156
{
157
  PreviewCache *pc = (PreviewCache *) data;
158
159
160
161
162
163

  temp_buf_free (pc->preview);
  g_free(pc);
}

static void
164
preview_cache_print (GSList *plist)
165
{
166
167
168
169
170
171
172
#ifdef PREVIEW_CACHE_DEBUG  
  GSList       *list;
  PreviewCache *pc;

  g_print ("preview cache dump:\n");

  for (list = plist; list; list = g_slist_next (list))
173
    {
174
175
176
177
      pc = (PreviewCache *) list->data;

      g_print ("\tvalue w,h [%d,%d] => %p\n", 
	       pc->width, pc->height, pc->preview);
178
    }
179
#endif  /* PREVIEW_CACHE_DEBUG */
180
181
182
}

void
183
gimp_preview_cache_invalidate (GSList **plist)
184
{
185
186
/*   g_print ("gimp_preview_cache_invalidate\n"); */
  preview_cache_print (*plist);
187

188
  g_slist_foreach (*plist, preview_cache_invalidate, NULL);
189
190
191
192
  *plist = NULL;
}

void 
193
194
gimp_preview_cache_add (GSList  **plist,
			TempBuf  *buf)
195
196
197
{
  PreviewCache *pc;

198
199
/*   g_print ("gimp_preview_cache_add %d %d\n",buf->width,buf->height); */
  preview_cache_print (*plist);
200

201
  if (g_slist_length (*plist) > MAX_CACHE_PREVIEWS)
202
203
204
205
206
207
208
    {
      /* Remove the smallest */
      preview_cache_remove_smallest(plist);
    }

  pc = g_new0(PreviewCache,1);
  pc->preview = buf; 
209
210
211
212
  pc->width   = buf->width;
  pc->height  = buf->height;

  *plist = g_slist_insert_sorted (*plist, pc, preview_cache_compare);
213
214
215
}

TempBuf *
216
217
218
gimp_preview_cache_get (GSList **plist,
			gint     width,
			gint     height)
219
{
220
221
  PreviewNearest  pn;
  PreviewCache   *pc;
222

223
224
/*   g_print ("gimp_preview_cache_get %d %d\n",width,height); */
  preview_cache_print (*plist);
225

226
227
  pn.pc     = NULL;
  pn.width  = width;
228
229
  pn.height = height;

230
  g_slist_foreach (*plist, preview_cache_find_exact, &pn);
231

232
  if (pn.pc && pn.pc->preview)
233
    {
234
235
/*       g_print ("extact value w,h [%d,%d] => %p\n",
	           pn.pc->width,pn.pc->height,pn.pc->preview);  */
236
237
238
      return pn.pc->preview;
    }

239
  g_slist_foreach (*plist, preview_cache_find_biggest, &pn);
240

241
  if (pn.pc)
242
    {
243
244
245
246
247
248
249
250
251
252
253
      gint     pwidth;
      gint     pheight;
      gdouble  x_ratio;
      gdouble  y_ratio;
      guchar  *src_data;
      guchar  *dest_data;
      gint     loop1;
      gint     loop2;

/*       g_print ("nearest value w,h [%d,%d] => %p\n",
                   pn.pc->width,pn.pc->height,pn.pc->preview); */
254
255

/*       if(pn.pc->width == width && */
256
257
/* 	   pn.pc->height == height) */
/* 	 return pn.pc->preview; */
258

259
      if (!pn.pc->preview)
260
	{
261
	  g_error ("gimp_preview_cache_get:: Invalid cache item");
262
263
264
265
266
	  return NULL;
	}

      /* Make up new preview from the large one... */

267
      pwidth  = pn.pc->preview->width;
268
269
270
      pheight = pn.pc->preview->height;

      /* Now get the real one and add to cache */
271
272
273
274
/*       g_print ("Must create from large preview\n"); */
      pc = g_new0 (PreviewCache, 1);
      pc->preview = temp_buf_new (width, height, pn.pc->preview->bytes, 
				  0, 0, NULL);
275
      /* preview from nearest bigger one */
276
277

      if (width)
278
        x_ratio = (gdouble) pwidth / (gdouble) width;
279
280
      else
        x_ratio = 0.0;
281

282
      if (height)
283
        y_ratio = (gdouble) pheight / (gdouble) height;
284
285
      else
        y_ratio = 0.0;
286

287
288
289
      src_data = temp_buf_data(pn.pc->preview);
      dest_data = temp_buf_data(pc->preview);

290
/*       g_print ("x_ratio , y_ratio [%f,%f]\n",x_ratio,y_ratio); */
291

292
293
      for (loop1 = 0 ; loop1 < height ; loop1++)
	for (loop2 = 0 ; loop2 < width ; loop2++)
294
	  {
295
296
297
298
299
300
301
302
303
304
305
306
	    gint    i;
	    guchar *src_pixel;
	    guchar *dest_pixel;

	    src_pixel = src_data +
	      ((gint) (loop2 * x_ratio)) * pn.pc->preview->bytes +
	      ((gint) (loop1 * y_ratio)) * pwidth * pn.pc->preview->bytes;

	    dest_pixel = dest_data + 
	      (loop2 + loop1 * width) * pn.pc->preview->bytes;

	    for (i = 0; i < pn.pc->preview->bytes; i++)
307
308
309
	      *dest_pixel++ = *src_pixel++;
	  }

310
      pc->width  = width;
311
      pc->height = height;
312
313
314
      *plist = g_slist_insert_sorted (*plist, pc, preview_cache_compare); 
/*       g_print ("New preview created [%d,%d] => %p\n",
	           pc->width,pc->height,pc->preview);  */
315
316
317
318

      return pc->preview;
    }

319
/*   g_print ("gimp_preview_cache_get returning NULL\n");  */
320
321
  return NULL;
}