File: ee.c

package info (click to toggle)
gimageview 0.2.27-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, sarge
  • size: 7,880 kB
  • ctags: 8,193
  • sloc: ansic: 80,389; sh: 8,625; perl: 1,167; makefile: 914; yacc: 318
file content (279 lines) | stat: -rw-r--r-- 7,330 bytes parent folder | download
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
/* -*- Mode: C; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3 -*- */

/*
 * Electric Eyes thumbnail support plugin for GImageView
 * Copyright (C) 2001 Takuro Ashie
 *
 * 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.
 *
 * $Id: ee.c,v 1.18 2003/07/13 13:20:18 makeinu Exp $
 */

#include <string.h>
#include <gtk/gtk.h>
#include <gmodule.h>

#include "fileutil.h"
#include "gfileutil.h"
#include "intl.h"
#include "gimv_plugin.h"
#include "gimv_thumb_cache.h"


#ifndef BUF_SIZE
#define BUF_SIZE 4096
#endif

#ifndef MAX_PATH_LEN
#define MAX_PATH_LEN 1024
#endif

/* under image directory */
#define EE_THUMNAIL_DIRECTORY ".ee"


typedef struct EEThumbPrefs_Tag
{
   gint   width;
   gint   height;
   gchar *dir;
} EEThumbPrefs;


static GimvImage *load_thumb (const gchar *filename,
                              const gchar *cache_type,
                              GimvImageInfo *info);
static GimvImage *save_thumb (const gchar *filename,
                              const gchar *cache_type,
                              GimvImage   *image,
                              GimvImageInfo *info);
static gchar     *get_path   (const gchar *filename,
                              const gchar *cache_type);
static gboolean   get_size   (gint         width,
                              gint         height,
                              const gchar *cache_type,
                              gint        *width_ret,
                              gint        *height_ret);


static EEThumbPrefs ee_thumb_prefs [] =
{
   {180, 180, "large"},
   {256, 64,  "med"},
   {256, 24,  "small"},
};
static gint ee_thumb_prefs_num
= sizeof (ee_thumb_prefs) / sizeof (ee_thumb_prefs[0]);

static GimvThumbCacheLoader plugin_impl[] =
{
   {GIMV_THUMB_CACHE_LOADER_IF_VERSION,
    N_("Electric Eyes (Preview)"),
    load_thumb, save_thumb,
    get_path, get_size,
    NULL, NULL, NULL, NULL, NULL},

   {GIMV_THUMB_CACHE_LOADER_IF_VERSION,
    N_("Electric Eyes (Icon)"),
    load_thumb, save_thumb,
    get_path, get_size,
    NULL, NULL, NULL, NULL, NULL},

   {GIMV_THUMB_CACHE_LOADER_IF_VERSION,
    N_("Electric Eyes (Mini)"),
    load_thumb, save_thumb,
    get_path, get_size,
    NULL, NULL, NULL, NULL, NULL},
};
static gint plugin_impl_num = sizeof (plugin_impl) / sizeof (plugin_impl[0]);

GIMV_PLUGIN_GET_IMPL(plugin_impl, GIMV_PLUGIN_THUMB_CACHE)

GimvPluginInfo gimv_plugin_info =
{
   if_version:    GIMV_PLUGIN_IF_VERSION,
   name:          N_("Electric Eyes thumbnail support"),
   version:       "0.5.0",
   author:        N_("Takuro Ashie"),
   description:   NULL,
   get_implement: gimv_plugin_get_impl,
   get_mime_type: NULL,
   get_prefs_ui:  NULL,
};


gchar *
g_module_check_init (GModule *module)
{
   return NULL;
}


void
g_module_unload (GModule *module)
{
   return;
}


static GimvImage *
load_thumb (const gchar *filename, const gchar *cache_type,
            GimvImageInfo *info)
{
   GimvImage *image;
   gchar *thumb_file;

   g_return_val_if_fail (filename, NULL);
   g_return_val_if_fail (cache_type, NULL);

   thumb_file = get_path (filename, cache_type);

   g_return_val_if_fail (thumb_file, NULL);

   image = gimv_image_load_file (thumb_file, FALSE);

   g_free (thumb_file);

   return image;
}


static GimvImage *
save_thumb (const gchar *filename, const gchar *cache_type,
            GimvImage *image, GimvImageInfo *info)
{
   GimvImage *imcache;
   gchar *thumb_file;
   gint im_width = -1, im_height = -1, width = -1, height = -1;
   gboolean success;

   /* check args */
   g_return_val_if_fail (filename, FALSE);
   g_return_val_if_fail (image, FALSE);
   g_return_val_if_fail (cache_type, FALSE);

   thumb_file = get_path (filename, cache_type);
   g_return_val_if_fail (thumb_file, FALSE);

   /* get image width & height */
   gimv_image_get_size (image, &im_width, &im_height);
   if (im_width < 1 || im_height < 1) return NULL;

   /* get thumnail width & height */
   success = get_size (im_width, im_height, cache_type, &width, &height);
   if (!success || width < 1 || height < 1) return NULL;

   /* create cache directory if not found */
   success = mkdirs (thumb_file);
   if (!success) return NULL;

   /* scale image */
   imcache = gimv_image_scale (image, width, height);

   /* save cache to disk */
   if (imcache) {
      g_print (N_("save cache: %s\n"), thumb_file);
      gimv_image_save_file (imcache, thumb_file, "ppm");
   }

   g_free (thumb_file);

   return imcache;
}


static gchar *
get_path (const gchar *filename, const gchar *cache_type)
{
   gchar *abspath;
   gchar buf[MAX_PATH_LEN];
   gchar *size = NULL;
   gint i;

   g_return_val_if_fail (filename, NULL);
   g_return_val_if_fail (cache_type, NULL);

   for (i = 0; (i < plugin_impl_num) && (i < ee_thumb_prefs_num); i++) {
      if (!strcmp (cache_type, plugin_impl[i].label)) {
         size = ee_thumb_prefs[i].dir;
         break;
      }
   }

   g_return_val_if_fail (size, NULL);

   abspath = relpath2abs (filename);
   g_return_val_if_fail (abspath, NULL);

   g_snprintf (buf, MAX_PATH_LEN, "%s/%s/%s%s",
               g_getenv("HOME"), EE_THUMNAIL_DIRECTORY, size, filename);

   g_free (abspath);

   return g_strdup (buf);
}


static gboolean
get_size (gint width, gint height, const gchar *cache_type,
          gint *width_ret, gint *height_ret)
{
   gint max_width = -1, max_height = -1;
   gint i;
   gboolean use_large = FALSE;

   /* check args */
   g_return_val_if_fail (width_ret && height_ret, FALSE);
   *width_ret = *height_ret = -1;

   g_return_val_if_fail (cache_type, FALSE);

   if (width < 1 || height < 1)
      return FALSE;

   /* get max thumbnail size */
   for (i = 0; (i < plugin_impl_num) && (i < ee_thumb_prefs_num); i++) {
      if (!strcmp (cache_type, plugin_impl[i].label)) {
         max_width =  ee_thumb_prefs[i].width;
         max_height = ee_thumb_prefs[i].height;
         if (i == 0)
            use_large = TRUE;
         break;
      }
   }
   g_return_val_if_fail (max_width > 0 && max_height > 0, FALSE);

   /* no need to scale */
   if (width < max_width && height < max_height) {
      *width_ret  = width;
      *height_ret = height;
      return TRUE;
   }

   /* calculate width & height of thumbnail */
   if (width < height || !use_large) {
      *width_ret = (gfloat) width * (gfloat) max_height / (gfloat) height;
      *height_ret = max_height;
      if (!use_large && width > max_width) {
         *width_ret = max_width;
         *height_ret = (gfloat) height * (gfloat) max_width / (gfloat) width;
      }
   } else {
      *width_ret = max_width;
      *height_ret = (gfloat) height * (gfloat) max_width / (gfloat) width;
   }

   return TRUE;
}