Initialize Tizen 2.3
[apps/home/mobileprint.git] / mobileprint / previewgen / util / main.c
1 /*
2 *  Mobileprint
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.1 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9
10 * http://floralicense.org/license/
11
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 #include <page_preview.h>
27 #include <paper_size.h>
28 #include <pgen_debug.h>
29 #include <preview_coords.h>
30
31
32 void show_help()
33 {
34         printf("Usage: previewgen-tool cmd ...\n"
35                 "Commands:"
36                 "\n"
37                 "help - show help\n"
38                 "\n"
39                 "init img - generate PDF for image\n"
40                 "Arguments:\n"
41                 "\tfname1 ... fnameN outfname_pdf ppdfname\n"
42                 "\tpaper_name paper_s_x paper_s_y\n"
43                 "\tav_s_x av_s_y orientation n_up\n"
44                 "\tscale_type scale_zoom scale_w scale_h\n"
45                 "\tis_grayscale\n"
46                 "\n"
47                 "init pdf - generate PDF for PDF\n"
48                 "Arguments:\n"
49                 "\tfname toutfname_pdf ppdfname\n"
50                 "\tpaper_name paper_s_x paper_s_y\n"
51                 "\torientation n_up\n"
52                 "\tscale_type scale_zoom scale_w scale_h\n"
53                 "\tis_grayscale\n"
54                 "\n"
55                 "empty_gen - generate empty page image\n"
56                 "Arguments:\n"
57                 "\toutfname_ppm paper_s_x paper_s_y av_s_x av_s_y\n"
58                 "\tshadow_x shadow_y is_rotate90\n"
59                 "\n"
60                 "pagegen - generate single page image for PDF\n"
61                 "\tfname page outfname_ppm\n"
62                 "\tpaper_s_x paper_s_y av_s_x av_s_y\n"
63                 "\tshadow_x shadow_y is_rotate90 is_grayscale\n"
64                 );
65 }
66
67
68 int parse_page_orientation(char *str, enum page_orientation *res)
69 {
70         if (NULL == str || NULL == res) {
71                 printf("ERROR in orientation string\n");
72                 return -1;
73         }
74
75         if (strcasecmp(str, "portrait") == 0) {
76                 *res = PAGE_ORIENTATION_PORTRAIT;
77                 return 0;
78         }
79         if (strcasecmp(str, "landscape") == 0) {
80                 *res = PAGE_ORIENTATION_LANDSCAPE;
81                 return 0;
82         }
83
84         return -1;
85 }
86
87
88 int parse_scale_type(char *str, enum page_scale_type *res)
89 {
90         if (NULL == str || NULL == res) {
91                 printf("ERROR in scale type string\n");
92                 return -1;
93         }
94
95         if (strcasecmp(str, "fit_to_paper") == 0) {
96                 *res = SCALE_FIT_TO_PAPER;
97                 return 0;
98         }
99         if (strcasecmp(str, "custom") == 0) {
100                 *res = SCALE_CUSTOM;
101                 return 0;
102         }
103         if (strcasecmp(str, "relative") == 0) {
104                 *res = SCALE_RELATIVE;
105                 return 0;
106         }
107         if (strcasecmp(str, "original") == 0) {
108                 *res = SCALE_ORIGINAL;
109                 return 0;
110         }
111
112         printf("ERROR in scale type string\n");
113         return -1;
114 }
115
116
117 void destroy_str_list(char **str_list, int list_len)
118 {
119         int i;
120         PGEN_RET_IF(NULL == str_list || list_len <= 0 || NULL == str_list[0],
121                         "Argument error");
122         for (i = 0; i < list_len; ++i) {
123                 PGEN_IF_FREE_MEM(str_list[i]);
124         }
125 }
126
127
128 void remove_tmp_files(char **const files, int files_count)
129 {
130         int i;
131         PGEN_RET_IF(NULL == files || files_count <= 0 || NULL == files[0],
132                         "Arguments error");
133         for (i = 0; i <= files_count; ++i)
134                 unlink(files[i]);
135 }
136
137
138 int process_init_img_files(int argc, char *argv[])
139 {
140         char **fnames;
141         int files_count;
142         char *outfname;
143         char *ppd_fname;
144         struct paper_size_pts paper_size;
145         struct size_px av_size;
146         enum page_orientation orientation;
147         int n_up;
148         struct page_scale scale;
149         int is_grayscale;
150
151         char **tmp_fnames;
152         int len;
153         char *tmp_str;
154
155         int res;
156         int is_err = 0;
157         int pages_count;
158         int i;
159
160         if (argc < 15) {
161                 printf("ERROR in init img arguments count\n");
162                 return -1;
163         }
164         PGEN_DEBUG("argc: %d", argc);
165
166         fnames = argv;
167         files_count = argc - 15;
168         i = argc - 15 - 1;
169         outfname = argv[i + 1];
170         ppd_fname = argv[i + 2];
171
172         PGEN_DEBUG("files_count: %d", files_count);
173         PGEN_DEBUG("outfname: %s", outfname);
174         PGEN_DEBUG("i: %d", i);
175
176         paper_size.name = argv[i + 3];
177         paper_size.s.x = atof(argv[i + 4]);
178         paper_size.s.y = atof(argv[i + 5]);
179
180         av_size.x = atoi(argv[i + 6]);
181         av_size.y = atoi(argv[i + 7]);
182
183         PGEN_DEBUG("orientation: %s", argv[i + 8]);
184         if (parse_page_orientation(argv[i + 8], &orientation) < 0)
185                 return -1;
186
187         n_up = atoi(argv[i + 9]);
188
189         PGEN_DEBUG("scale_type");
190         if (parse_scale_type(argv[i + 10], &(scale.type)) < 0)
191                 return -1;
192         scale.zoom = atoi(argv[i + 11]);
193         scale.w = atof(argv[i + 12]);
194         scale.h = atof(argv[i + 13]);
195
196         is_grayscale = atoi(argv[i + 14]);
197
198         if (files_count < 1 || files_count > 9999) {
199                 printf("Files count ERROR\n");
200                 return -1;
201         }
202
203         /* TODO: downscale each image file */
204         /* generate temporary names based on pid */
205         tmp_fnames = (char**)calloc(files_count + 1, sizeof(char*));
206         for (i = 0; i < files_count; ++i) {
207                 len = sizeof(PREVIEW_TEMP_DIR
208                                 "/convert_tmp_XXXXXXXX_XXXX.ppm");
209                 tmp_str = (char*)malloc(sizeof(char) * (len + 1));
210                 if (NULL == tmp_str) {
211                         is_err = 1;
212                         break;
213                 }
214                 tmp_str[len] = '\0';
215                 snprintf(tmp_str, len, PREVIEW_TEMP_DIR
216                                 "/convert_tmp_%08X_%04d.ppm", getpid(), i + 1);
217                 tmp_fnames[i] = tmp_str;
218         }
219         if (is_err) {
220                 destroy_str_list(tmp_fnames, files_count);
221                 PGEN_IF_FREE_MEM(tmp_fnames);
222                 printf("ERROR: out of memory\n");
223                 return -1;
224         }
225
226         for (i = 0; i < files_count; ++i) {
227                 res = process_image_downscale(
228                                 fnames[i], tmp_fnames[i], &av_size);
229                 if (res < 0) {
230                         printf("ERROR in image downscale\n");
231                         is_err = 1;
232                         break;
233                 }
234         }
235         if (is_err) {
236                 remove_tmp_files(tmp_fnames, files_count);
237                 destroy_str_list(tmp_fnames, files_count);
238                 PGEN_IF_FREE_MEM(tmp_fnames);
239                 return -1;
240         }
241
242         /* convert to pdf */
243         if (files_count > 1)
244                 res = img_files2pdf_preview_pages(tmp_fnames, files_count,
245                                 outfname, ppd_fname, &paper_size, &av_size,
246                                 orientation, n_up, &scale, is_grayscale);
247         else
248                 res = img2pdf_preview_pages(tmp_fnames[0], outfname, ppd_fname,
249                                 &paper_size, &av_size, orientation, n_up,
250                                 &scale, is_grayscale);
251
252         /* remove temporary files */
253         remove_tmp_files(tmp_fnames, files_count);
254         destroy_str_list(tmp_fnames, files_count);
255         PGEN_IF_FREE_MEM(tmp_fnames);
256
257         if (res < 0) {
258                 printf("ERROR in init img\n");
259                 return -1;
260         } else {
261                 PGEN_DEBUG("PDF generation OK");
262         }
263         pages_count = get_pdf_pages_count(outfname);
264         printf("init img: pages count: %d\n", pages_count);
265
266         return res;
267 }
268
269
270 int process_init_pdf(int argc, char *argv[])
271 {
272         char *fname;
273         char *outfname;
274         char *ppd_fname;
275         struct paper_size_pts paper_size;
276         enum page_orientation orientation;
277         int n_up;
278         struct page_scale scale;
279         int is_grayscale;
280
281         int res;
282         int pages_count;
283
284         if (argc < 13) {
285                 printf("ERROR in init pdf arguments count\n");
286                 return -1;
287         }
288
289         fname = argv[0];
290         outfname = argv[1];
291         ppd_fname = argv[2];
292
293         paper_size.name = argv[3];
294         paper_size.s.x = atof(argv[4]);
295         paper_size.s.y = atof(argv[5]);
296
297         if (parse_page_orientation(argv[6], &orientation) < 0)
298                 return -1;
299
300         n_up = atoi(argv[7]);
301
302         if (parse_scale_type(argv[8], &(scale.type)) < 0)
303                 return -1;
304         scale.zoom = atoi(argv[9]);
305         scale.w = atof(argv[10]);
306         scale.h = atof(argv[11]);
307
308         is_grayscale = atoi(argv[12]);
309
310
311         res = pdf2pdf_preview_pages(fname, outfname, ppd_fname,
312                         &paper_size, orientation, n_up,
313                         &scale, is_grayscale);
314         if (res < 0) {
315                 printf("ERROR in init pdf\n");
316                 return -1;
317         }
318         pages_count = get_pdf_pages_count(outfname);
319         printf("init pdf: pages count: %d\n", pages_count);
320         return 0;
321 }
322
323
324 int process_pagegen(int argc, char *argv[])
325 {
326         char *pdf_fname;
327         char *outfname_ppm;
328         int page;
329         struct size_pts paper_size;
330         struct size_px av_size;
331         struct size_px shadow_size;
332         int is_rotate90;
333         int is_grayscale;
334
335         struct preview_page_req settings_req;
336
337         int res;
338
339
340         if (argc < 11) {
341                 printf("ERROR in gen arguments count\n");
342                 return -1;
343         }
344
345         pdf_fname = argv[0];
346         page = atoi(argv[1]);
347         outfname_ppm = argv[2];
348
349         paper_size.x = atof(argv[3]);
350         paper_size.y = atof(argv[4]);
351
352         av_size.x = atoi(argv[5]);
353         av_size.y = atoi(argv[6]);
354
355         shadow_size.x = atoi(argv[7]);
356         shadow_size.y = atoi(argv[8]);
357
358         is_rotate90 = atoi(argv[9]);
359         is_grayscale = atoi(argv[10]);
360
361
362         settings_req.paper_size = paper_size;
363         settings_req.available_size_px = av_size;
364         settings_req.shadow_offset = shadow_size;
365         settings_req.is_rotate90 = is_rotate90;
366         settings_req.is_grayscale = is_grayscale;
367
368         /*if (get_preview_settings(&settings_req, &settings_px) < 0) {
369                 printf("ERROR in settings\n");
370                 return -1;
371         }*/
372
373         res = save_pdf_preview_page_image(pdf_fname, page,
374                         &settings_req, outfname_ppm);
375         if (res <= 0) {
376                 printf("ERROR in preview page image generation\n");
377                 return -1;
378         }
379         printf("OK\n");
380         return 0;
381 }
382
383
384 int process_empty_gen(int argc, char *argv[])
385 {
386         char *outfname_ppm;
387         struct size_pts paper_size;
388         struct size_px av_size;
389         struct size_px shadow_size;
390         int is_rotate90;
391
392         struct preview_page_req settings_req;
393
394         int res;
395
396
397         if (argc < 8) {
398                 printf("ERROR in empty_gen arguments count\n");
399                 return -1;
400         }
401
402         outfname_ppm = argv[0];
403
404         paper_size.x = atof(argv[1]);
405         paper_size.y = atof(argv[2]);
406
407         av_size.x = atoi(argv[3]);
408         av_size.y = atoi(argv[4]);
409
410         shadow_size.x = atoi(argv[5]);
411         shadow_size.y = atoi(argv[6]);
412
413         is_rotate90 = atoi(argv[7]);
414
415         settings_req.paper_size = paper_size;
416         settings_req.available_size_px = av_size;
417         settings_req.shadow_offset = shadow_size;
418         settings_req.is_rotate90 = is_rotate90;
419         settings_req.is_grayscale = 0;
420
421
422         res = save_empty_preview_page_image(&settings_req, outfname_ppm);
423         if (res <= 0) {
424                 printf("ERROR in preview page image generation\n");
425                 return -1;
426         }
427         printf("OK\n");
428
429         return 0;
430 }
431
432
433 int process_cmd(int argc, char *argv[])
434 {
435         if (strcasecmp("init", argv[1]) == 0) {
436                 if (argc <= 2) {
437                         printf("ERROR in init cmd usage\n");
438                         return -1;
439                 }
440                 if (strcasecmp("img", argv[2]) == 0)
441                         return process_init_img_files(argc - 2, &(argv[3]));
442                 if (strcasecmp("pdf", argv[2]) == 0)
443                         return process_init_pdf(argc - 2, &(argv[3]));
444                 printf("ERROR in init cmd usage\n");
445                 return -1;
446         }
447
448         if (strcasecmp("pagegen", argv[1]) == 0)
449                 return process_pagegen(argc - 1, &(argv[2]));
450
451         if (strcasecmp("empty_gen", argv[1]) == 0)
452                 return process_empty_gen(argc - 1, &(argv[2]));
453
454         return 0;
455 }
456
457
458 int main(int argc, char *argv[])
459 {
460         int res;
461         int ret;
462         int errsv;
463
464         if (argc <= 1) {
465                 printf("ERROR: no arguments, type help for usage\n");
466                 return 1;
467         }
468
469         if (strcasecmp("help", argv[1]) == 0) {
470                 show_help();
471                 return 0;
472         }
473
474         ret = mkdir(PREVIEW_TEMP_DIR, 0777);
475         errsv = errno;
476         if (ret < 0 && errsv != EEXIST) {
477                 printf("Failed to mkdir temporary directory " PREVIEW_TEMP_DIR
478                                 ", errno = %d\n", errsv);
479                 return 1;
480         }
481
482         evas_init();
483         res = process_cmd(argc, argv);
484         evas_shutdown();
485         if (res < 0) {
486                 printf("Arguments ERROR\n");
487                 return 1;
488         }
489
490         return 0;
491 }
492