diff options
author | Carsten Haitzler (Rasterman) <raster@rasterman.com> | 2015-04-02 11:50:08 +0900 |
---|---|---|
committer | Carsten Haitzler (Rasterman) <raster@rasterman.com> | 2015-04-02 11:50:08 +0900 |
commit | fc07dc893f5e855240fca76fe54b01092139966a (patch) | |
tree | 8d392b4a48f00ec16d8760c5402a9edc61db0da0 /src/bin/ecore_evas/eetpack.c | |
parent | e663773d9e594dde9677e68a511a4d5563a215ce (diff) |
ecore_evas - eetpack utility for packing files (imgs) into eet files
this adds a new utility called "eetpack" that uses eet, ecore_evas,
eina and evas to stuff images with various compressions/encodings (as
well as raw data in a simple way) into eet files like edj files or any
eet archive. can be used in combination with the "eet" utility and
edje_cc generated edj files.
@feature
Diffstat (limited to '')
-rw-r--r-- | src/bin/ecore_evas/eetpack.c | 292 |
1 files changed, 292 insertions, 0 deletions
diff --git a/src/bin/ecore_evas/eetpack.c b/src/bin/ecore_evas/eetpack.c new file mode 100644 index 0000000000..7817fa9620 --- /dev/null +++ b/src/bin/ecore_evas/eetpack.c | |||
@@ -0,0 +1,292 @@ | |||
1 | /* | ||
2 | * A small binary utility for packing basic data and images into eet files. | ||
3 | * Can be used in combination with the "eet" utility and can even be used | ||
4 | * to stuff more data into edj files (which are eet). Run tool for help. | ||
5 | */ | ||
6 | #include <Eina.h> | ||
7 | #include <Eet.h> | ||
8 | #include <Evas.h> | ||
9 | #include <Ecore_Evas.h> | ||
10 | |||
11 | static const char *file = NULL; | ||
12 | static Eet_File *ef = NULL; | ||
13 | static Ecore_Evas *ee = NULL; | ||
14 | static Evas *evas = NULL; | ||
15 | static Evas_Object *im_obj = NULL; | ||
16 | |||
17 | static void | ||
18 | file_add(void) | ||
19 | { | ||
20 | if (ef) return; | ||
21 | ef = eet_open(file, EET_FILE_MODE_READ_WRITE); | ||
22 | if (!ef) ef = eet_open(file, EET_FILE_MODE_WRITE); | ||
23 | if (!ef) | ||
24 | { | ||
25 | printf("ERROR: Cannot open %s for reading or writing!\n", file); | ||
26 | exit(-1); | ||
27 | } | ||
28 | } | ||
29 | |||
30 | static void | ||
31 | file_del(void) | ||
32 | { | ||
33 | if (ef) return; | ||
34 | ef = eet_open(file, EET_FILE_MODE_READ_WRITE); | ||
35 | if (!ef) | ||
36 | { | ||
37 | printf("ERROR: Cannot open %s for writing!\n", file); | ||
38 | exit(-1); | ||
39 | } | ||
40 | } | ||
41 | |||
42 | static void | ||
43 | check_argc(int argc, int i) | ||
44 | { | ||
45 | if (i >= argc) | ||
46 | { | ||
47 | printf("ERROR: ran out of arguments at argument #%i\n", i); | ||
48 | exit(-1); | ||
49 | } | ||
50 | } | ||
51 | |||
52 | static void | ||
53 | commands(int start, int argc, char **argv) | ||
54 | { | ||
55 | int i; | ||
56 | const char *key, *file, *mode; | ||
57 | void *pixels, *data; | ||
58 | int w, h, alpha, compress, quality; | ||
59 | Eet_Image_Encoding lossy; | ||
60 | Eina_File *f; | ||
61 | size_t size; | ||
62 | |||
63 | for (i = start; i < argc;) | ||
64 | { | ||
65 | if (!strcmp(argv[i], "+")) | ||
66 | { | ||
67 | i++; check_argc(argc, i); | ||
68 | key = argv[i]; | ||
69 | i++; check_argc(argc, i); | ||
70 | file_add(); | ||
71 | if (!strcmp(argv[i], "im")) | ||
72 | { | ||
73 | i++; check_argc(argc, i); | ||
74 | file = argv[i]; | ||
75 | i++; check_argc(argc, i); | ||
76 | mode = argv[i]; | ||
77 | evas_object_image_file_set(im_obj, file, NULL); | ||
78 | if (evas_object_image_load_error_get(im_obj) != EVAS_LOAD_ERROR_NONE) | ||
79 | { | ||
80 | printf("ERROR: cannot load image file '%s'\n", file); | ||
81 | exit(-1); | ||
82 | } | ||
83 | pixels = evas_object_image_data_get(im_obj, EINA_FALSE); | ||
84 | if (!pixels) | ||
85 | { | ||
86 | printf("ERROR: cannot get pixel data for file '%s'\n", file); | ||
87 | exit(-1); | ||
88 | } | ||
89 | evas_object_image_size_get(im_obj, &w, &h); | ||
90 | if ((w <= 0) || (h <= 0)) | ||
91 | { | ||
92 | printf("ERROR: width or height <= 0 for file '%s'\n", file); | ||
93 | exit(-1); | ||
94 | } | ||
95 | alpha = evas_object_image_alpha_get(im_obj); | ||
96 | quality = 0; | ||
97 | compress = 0; | ||
98 | lossy = EET_IMAGE_LOSSLESS; | ||
99 | if (!strcmp(mode, "none")) | ||
100 | { | ||
101 | lossy = EET_IMAGE_LOSSLESS; | ||
102 | compress = EET_COMPRESSION_NONE; | ||
103 | } | ||
104 | else if (!strcmp(mode, "lo")) | ||
105 | { | ||
106 | lossy = EET_IMAGE_LOSSLESS; | ||
107 | compress = EET_COMPRESSION_LOW; | ||
108 | } | ||
109 | else if (!strcmp(mode, "med")) | ||
110 | { | ||
111 | lossy = EET_IMAGE_LOSSLESS; | ||
112 | compress = EET_COMPRESSION_MED; | ||
113 | } | ||
114 | else if (!strcmp(mode, "hi")) | ||
115 | { | ||
116 | lossy = EET_IMAGE_LOSSLESS; | ||
117 | compress = EET_COMPRESSION_HI; | ||
118 | } | ||
119 | else if (!strcmp(mode, "fast")) | ||
120 | { | ||
121 | lossy = EET_IMAGE_LOSSLESS; | ||
122 | compress = EET_COMPRESSION_VERYFAST; | ||
123 | } | ||
124 | else if (!strcmp(mode, "super")) | ||
125 | { | ||
126 | lossy = EET_IMAGE_LOSSLESS; | ||
127 | compress = EET_COMPRESSION_SUPERFAST; | ||
128 | } | ||
129 | else if (!strcmp(mode, "etc1")) | ||
130 | { | ||
131 | if (alpha) lossy = EET_IMAGE_ETC1_ALPHA; | ||
132 | else lossy = EET_IMAGE_ETC1; | ||
133 | compress = EET_COMPRESSION_SUPERFAST; | ||
134 | } | ||
135 | else if (!strcmp(mode, "etc2")) | ||
136 | { | ||
137 | if (alpha) lossy = EET_IMAGE_ETC2_RGBA; | ||
138 | else lossy = EET_IMAGE_ETC2_RGB; | ||
139 | compress = EET_COMPRESSION_SUPERFAST; | ||
140 | } | ||
141 | else | ||
142 | { | ||
143 | quality = atoi(mode); | ||
144 | lossy = EET_IMAGE_JPEG; | ||
145 | } | ||
146 | if (eet_data_image_write(ef, key, pixels, w, h, alpha, | ||
147 | compress, quality, lossy) <= 0) | ||
148 | { | ||
149 | printf("ERROR: cannot encode file '%s' in key '%s'\n", file, key); | ||
150 | exit(-1); | ||
151 | } | ||
152 | } | ||
153 | else if (!strcmp(argv[i], "data")) | ||
154 | { | ||
155 | i++; check_argc(argc, i); | ||
156 | file = argv[i]; | ||
157 | i++; check_argc(argc, i); | ||
158 | mode = argv[i]; | ||
159 | f = eina_file_open(file, EINA_FALSE); | ||
160 | if (!f) | ||
161 | { | ||
162 | printf("ERROR: cannot open file '%s'\n", file); | ||
163 | exit(-1); | ||
164 | } | ||
165 | size = eina_file_size_get(f); | ||
166 | if (size == 0) | ||
167 | { | ||
168 | printf("ERROR: file '%s' is zero sized\n", file); | ||
169 | exit(-1); | ||
170 | } | ||
171 | if (size >= 0x7f000000) | ||
172 | { | ||
173 | printf("ERROR: file '%s' is too big (a bit under 2GB max)\n", file); | ||
174 | exit(-1); | ||
175 | } | ||
176 | data = eina_file_map_all(f, EINA_FILE_POPULATE); | ||
177 | if (!data) | ||
178 | { | ||
179 | printf("ERROR: cannot mmap file '%s'\n", file); | ||
180 | exit(-1); | ||
181 | } | ||
182 | compress = 0; | ||
183 | if (!strcmp(mode, "none")) | ||
184 | { | ||
185 | compress = EET_COMPRESSION_NONE; | ||
186 | } | ||
187 | else if (!strcmp(mode, "lo")) | ||
188 | { | ||
189 | compress = EET_COMPRESSION_LOW; | ||
190 | } | ||
191 | else if (!strcmp(mode, "med")) | ||
192 | { | ||
193 | compress = EET_COMPRESSION_MED; | ||
194 | } | ||
195 | else if (!strcmp(mode, "hi")) | ||
196 | { | ||
197 | compress = EET_COMPRESSION_HI; | ||
198 | } | ||
199 | else if (!strcmp(mode, "fast")) | ||
200 | { | ||
201 | compress = EET_COMPRESSION_VERYFAST; | ||
202 | } | ||
203 | else if (!strcmp(mode, "super")) | ||
204 | { | ||
205 | compress = EET_COMPRESSION_SUPERFAST; | ||
206 | } | ||
207 | else | ||
208 | { | ||
209 | printf("ERROR: invalid compress mode '%s' for file '%s'\n", mode, file); | ||
210 | exit(-1); | ||
211 | } | ||
212 | if (eet_write(ef, key, data, size, compress) <= 0) | ||
213 | { | ||
214 | printf("ERROR: cannot encode file '%s' in key '%s'\n", file, key); | ||
215 | exit(-1); | ||
216 | } | ||
217 | eina_file_map_free(f, data); | ||
218 | eina_file_close(f); | ||
219 | } | ||
220 | i++; | ||
221 | } | ||
222 | else if (!strcmp(argv[i], "-")) | ||
223 | { | ||
224 | i++; check_argc(argc, i); | ||
225 | key = argv[i]; | ||
226 | file_del(); | ||
227 | eet_delete(ef, key); | ||
228 | i++; | ||
229 | } | ||
230 | else | ||
231 | { | ||
232 | printf("invalid argument #%i of '%s'\n", i, argv[i]); | ||
233 | exit(-1); | ||
234 | } | ||
235 | } | ||
236 | } | ||
237 | |||
238 | static void | ||
239 | scratch_canvas_init(void) | ||
240 | { | ||
241 | ee = ecore_evas_buffer_new(1, 1); | ||
242 | if (!ee) | ||
243 | { | ||
244 | printf("ERROR: cannot create buffer canvas!\n"); | ||
245 | exit(-1); | ||
246 | } | ||
247 | evas = ecore_evas_get(ee); | ||
248 | im_obj = evas_object_image_add(evas); | ||
249 | } | ||
250 | |||
251 | int | ||
252 | main(int argc, char **argv) | ||
253 | { | ||
254 | if (argc <= 1) | ||
255 | { | ||
256 | printf | ||
257 | ("USAGE: \n" | ||
258 | " eetpack outputfile.eet [commands]\n" | ||
259 | "WHERE commands are a series of one or more of:\n" | ||
260 | " + KEY im IMG-FILE [none|lo|med|hi|fast|super|etc1|etc2|0-100]\n" | ||
261 | " + KEY data DATA-FILE [none|lo|med|hi|fast|super]\n" | ||
262 | " - KEY\n" | ||
263 | "\n" | ||
264 | "e.g.\n" | ||
265 | "add 2 images and a text file to the archive:\n" | ||
266 | " eetpack archive.eet \\\n" | ||
267 | " + mykey1 im image.png fast \\\n" | ||
268 | " + mykey2 im logo.png 70 \\\n" | ||
269 | " + mydata1 data file.txt hi \\\n" | ||
270 | "\n" | ||
271 | "remove keys from an archive:\n" | ||
272 | " eetpack archive.eet - mykey2 - mydata1\n" | ||
273 | ); | ||
274 | |||
275 | return -1; | ||
276 | } | ||
277 | eina_init(); | ||
278 | eet_init(); | ||
279 | evas_init(); | ||
280 | ecore_evas_init(); | ||
281 | |||
282 | scratch_canvas_init(); | ||
283 | file = argv[1]; | ||
284 | commands(2, argc, argv); | ||
285 | if (ef) eet_close(ef); | ||
286 | |||
287 | ecore_evas_shutdown(); | ||
288 | evas_shutdown(); | ||
289 | eet_shutdown(); | ||
290 | eina_shutdown(); | ||
291 | return 0; | ||
292 | } | ||