ethumb: reduce number of generate.
[framework/uifw/ethumb.git] / src / bin / ethumb.c
1 /**
2  * @file
3  *
4  * Copyright (C) 2009 by ProFUSION embedded systems
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library;
18  * if not, see <http://www.gnu.org/licenses/>.
19  *
20  * @author Rafael Antognolli <antognolli@profusion.mobi>
21  */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <limits.h>
30
31 #include <Eina.h>
32 #include <Ecore.h>
33 #include <Ecore_Getopt.h>
34 #include <Ethumb.h>
35
36 const char *aspect_opt[] = { "keep", "ignore", "crop", NULL };
37 const char *format_opt[] = { "png", "jpg", "eet", NULL };
38 struct frame
39 {
40    const char *file;
41    const char *group;
42    const char *swallow;
43 };
44
45 static unsigned char
46 _ethumb_getopt_callback_frame_parse(const Ecore_Getopt *parser __UNUSED__, const Ecore_Getopt_Desc *desc __UNUSED__, const char *str, void *data __UNUSED__, Ecore_Getopt_Value *storage)
47 {
48    struct frame *f = (struct frame *)storage->ptrp;
49    const char *tfile, *tgroup, *tswallow, *base, *sep;
50
51    base = str;
52    sep = strchr(base, ':');
53    if (!sep)
54         goto error;
55    tfile = eina_stringshare_add_length(base, sep - base);
56    base = sep + 1;
57
58    sep = strchr(base, ':');
59    if (!sep)
60      {
61         eina_stringshare_del(tfile);
62         goto error;
63      }
64    tgroup = eina_stringshare_add_length(base, sep - base);
65    base = sep + 1;
66    if (base[0] == '\0')
67      {
68         eina_stringshare_del(tfile);
69         eina_stringshare_del(tgroup);
70         goto error;
71      }
72    tswallow = eina_stringshare_add(base);
73
74    f->file = tfile;
75    f->group = tgroup;
76    f->swallow = tswallow;
77    return 1;
78
79  error:
80    fprintf(stderr,
81            "ERROR: invalid theme, not in format "
82            "'file:group:swallow_part': '%s'\n",
83            str);
84    return 0;
85 }
86
87 const Ecore_Getopt optdesc = {
88   "ethumb",
89   NULL,
90   PACKAGE_VERSION,
91   "(C) 2009 - ProFUSION embedded systems",
92   "LGPL v3 - GNU Lesser General Public License",
93   "Thumbnails generator.\n"
94      "\n"
95      "This program uses ethumb to create thumbnails from pictures. "
96      "It's an example of use and a test for ethumb.\n",
97   0,
98   {
99      ECORE_GETOPT_CALLBACK_ARGS
100      ('s', "size", "thumbnail size expected.",
101       "WxH", ecore_getopt_callback_size_parse, NULL),
102      ECORE_GETOPT_CHOICE
103      ('f', "format", "file format to save.", format_opt),
104      ECORE_GETOPT_CHOICE
105      ('a', "aspect", "original image aspect ratio.", aspect_opt),
106      ECORE_GETOPT_STORE_STR
107      ('d', "directory", "directory to save thumbnails."),
108      ECORE_GETOPT_STORE_STR
109      ('c', "category", "thumbnails category."),
110      ECORE_GETOPT_CALLBACK_ARGS
111      ('t', "theme", "path to theme file, group and swallow part.",
112       "file:group:swallow_part", _ethumb_getopt_callback_frame_parse, NULL),
113      ECORE_GETOPT_STORE_STR
114      ('k', "key", "key inside eet file to read image from."),
115      ECORE_GETOPT_STORE_DOUBLE
116      ('v', "video_time", "time of video frame to use as thumbnail."),
117      ECORE_GETOPT_STORE_INT
118      ('p', "document_page", "document page to use as thumbnail."),
119      ECORE_GETOPT_LICENSE('L', "license"),
120      ECORE_GETOPT_COPYRIGHT('C', "copyright"),
121      ECORE_GETOPT_VERSION('V', "version"),
122      ECORE_GETOPT_HELP('h', "help"),
123      ECORE_GETOPT_SENTINEL
124   }
125 };
126
127 static void
128 _thumb_report(const char *mode, Ethumb *e)
129 {
130    const char *ap, *ak, *gp, *gk;
131    ethumb_file_get(e, &ap, &ak);
132    ethumb_thumb_path_get(e, &gp, &gk);
133    printf("%s '%s' '%s' => '%s' '%s'\n",
134           mode, ap, ak ? ak : "", gp, gk ? gk : "");
135 }
136
137 static void
138 _finished_thumb( void *data __UNUSED__, Ethumb *e, Eina_Bool success)
139 {
140    const char *mode = success ? "GENERATED" : "FAILED";
141    _thumb_report(mode, e);
142    ecore_main_loop_quit();
143 }
144
145 int
146 main(int argc, char *argv[])
147 {
148    Ethumb *e;
149    Eina_Bool quit_option = 0;
150    Eina_Rectangle geometry = {-1, -1, -1, -1};
151    unsigned int format = 0, aspect = 0;
152    char *format_str = NULL;
153    char *aspect_str = NULL;
154    char *directory = NULL;
155    char *category = NULL;
156    char *src_key = NULL;
157    struct frame frame = {NULL, NULL, NULL};
158    const char *thumb_path = NULL;
159    const char *thumb_key = NULL;
160    double video_time = 0;
161    int page = 0;
162    int arg_index;
163    int i;
164
165    int r = 1;
166
167    ethumb_init();
168    ecore_init();
169
170    Ecore_Getopt_Value values[] = {
171         ECORE_GETOPT_VALUE_PTR_CAST(geometry),
172         ECORE_GETOPT_VALUE_PTR_CAST(format_str),
173         ECORE_GETOPT_VALUE_PTR_CAST(aspect_str),
174         ECORE_GETOPT_VALUE_STR(directory),
175         ECORE_GETOPT_VALUE_STR(category),
176         ECORE_GETOPT_VALUE_PTR_CAST(frame),
177         ECORE_GETOPT_VALUE_STR(src_key),
178         ECORE_GETOPT_VALUE_DOUBLE(video_time),
179         ECORE_GETOPT_VALUE_INT(page),
180         ECORE_GETOPT_VALUE_BOOL(quit_option),
181         ECORE_GETOPT_VALUE_BOOL(quit_option),
182         ECORE_GETOPT_VALUE_BOOL(quit_option),
183         ECORE_GETOPT_VALUE_BOOL(quit_option),
184         ECORE_GETOPT_VALUE_NONE
185    };
186
187    arg_index = ecore_getopt_parse(&optdesc, values, argc, argv);
188    if (arg_index < 0)
189      {
190         fprintf(stderr, "Could not parse arguments.\n");
191         if (frame.file)
192           {
193              eina_stringshare_del(frame.file);
194              eina_stringshare_del(frame.group);
195              eina_stringshare_del(frame.swallow);
196           }
197         ecore_shutdown();
198         ethumb_shutdown();
199         return -1;
200      }
201
202    if (quit_option)
203      {
204         if (frame.file)
205           {
206              eina_stringshare_del(frame.file);
207              eina_stringshare_del(frame.group);
208              eina_stringshare_del(frame.swallow);
209           }
210         ecore_shutdown();
211         ethumb_shutdown();
212         return 0;
213      }
214
215    for (i = 0; i < 3; i++)
216      if (format_opt[i] == format_str)
217        {
218           format = i;
219           break;
220        }
221
222    for (i = 0; i < 3; i++)
223      if (aspect_opt[i] == aspect_str)
224        {
225           aspect = i;
226           break;
227        }
228
229    e = ethumb_new();
230
231    ethumb_thumb_format_set(e, format);
232    ethumb_thumb_aspect_set(e, aspect);
233    if (directory) ethumb_thumb_dir_path_set(e, directory);
234    if (category) ethumb_thumb_category_set(e, category);
235    if (geometry.w > 0 && geometry.h > 0)
236      ethumb_thumb_size_set(e, geometry.w, geometry.h);
237    if (frame.file)
238      {
239         ethumb_frame_set(e, frame.file, frame.group, frame.swallow);
240         eina_stringshare_del(frame.file);
241         eina_stringshare_del(frame.group);
242         eina_stringshare_del(frame.swallow);
243      }
244    if (video_time > 0)
245      ethumb_video_time_set(e, video_time);
246    if (page > 0)
247      ethumb_document_page_set(e, page);
248
249    if (r && arg_index < argc)
250      r = ethumb_file_set(e, argv[arg_index++], src_key);
251    else
252      r = 0;
253    if (r && arg_index < argc)
254      thumb_path = argv[arg_index++];
255    if (r && arg_index < argc)
256      thumb_key = argv[arg_index];
257
258    if (r)
259      {
260         ethumb_thumb_path_set(e, thumb_path, thumb_key);
261         if (ethumb_exists(e))
262           {
263              _thumb_report("EXISTS", e);
264              quit_option = 1;
265              r = 1;
266           }
267         else
268           r = ethumb_generate(e, _finished_thumb, NULL, NULL);
269      }
270
271    if (r && !quit_option)
272      ecore_main_loop_begin();
273
274    ethumb_file_free(e);
275    ethumb_free(e);
276
277    ecore_shutdown();
278    ethumb_shutdown();
279
280    return !r;
281 }