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