4 * Copyright 2012 Samsung Electronics Co., Ltd
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
10 * http://floralicense.org/license/
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.
23 #include <sys/types.h>
26 #include <page_preview.h>
27 #include <paper_size.h>
28 #include <pgen_debug.h>
29 #include <preview_coords.h>
34 printf("Usage: previewgen-tool cmd ...\n"
39 "init img - generate PDF for image\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"
47 "init pdf - generate PDF for PDF\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"
55 "empty_gen - generate empty page image\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"
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"
68 int parse_page_orientation(char *str, enum page_orientation *res)
70 if (NULL == str || NULL == res) {
71 printf("ERROR in orientation string\n");
75 if (strcasecmp(str, "portrait") == 0) {
76 *res = PAGE_ORIENTATION_PORTRAIT;
79 if (strcasecmp(str, "landscape") == 0) {
80 *res = PAGE_ORIENTATION_LANDSCAPE;
88 int parse_scale_type(char *str, enum page_scale_type *res)
90 if (NULL == str || NULL == res) {
91 printf("ERROR in scale type string\n");
95 if (strcasecmp(str, "fit_to_paper") == 0) {
96 *res = SCALE_FIT_TO_PAPER;
99 if (strcasecmp(str, "custom") == 0) {
103 if (strcasecmp(str, "relative") == 0) {
104 *res = SCALE_RELATIVE;
107 if (strcasecmp(str, "original") == 0) {
108 *res = SCALE_ORIGINAL;
112 printf("ERROR in scale type string\n");
117 void destroy_str_list(char **str_list, int list_len)
120 PGEN_RET_IF(NULL == str_list || list_len <= 0 || NULL == str_list[0],
122 for (i = 0; i < list_len; ++i) {
123 PGEN_IF_FREE_MEM(str_list[i]);
128 void remove_tmp_files(char **const files, int files_count)
131 PGEN_RET_IF(NULL == files || files_count <= 0 || NULL == files[0],
133 for (i = 0; i <= files_count; ++i)
138 int process_init_img_files(int argc, char *argv[])
144 struct paper_size_pts paper_size;
145 struct size_px av_size;
146 enum page_orientation orientation;
148 struct page_scale scale;
161 printf("ERROR in init img arguments count\n");
164 PGEN_DEBUG("argc: %d", argc);
167 files_count = argc - 15;
169 outfname = argv[i + 1];
170 ppd_fname = argv[i + 2];
172 PGEN_DEBUG("files_count: %d", files_count);
173 PGEN_DEBUG("outfname: %s", outfname);
174 PGEN_DEBUG("i: %d", i);
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]);
180 av_size.x = atoi(argv[i + 6]);
181 av_size.y = atoi(argv[i + 7]);
183 PGEN_DEBUG("orientation: %s", argv[i + 8]);
184 if (parse_page_orientation(argv[i + 8], &orientation) < 0)
187 n_up = atoi(argv[i + 9]);
189 PGEN_DEBUG("scale_type");
190 if (parse_scale_type(argv[i + 10], &(scale.type)) < 0)
192 scale.zoom = atoi(argv[i + 11]);
193 scale.w = atof(argv[i + 12]);
194 scale.h = atof(argv[i + 13]);
196 is_grayscale = atoi(argv[i + 14]);
198 if (files_count < 1 || files_count > 9999) {
199 printf("Files count ERROR\n");
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) {
215 snprintf(tmp_str, len, PREVIEW_TEMP_DIR
216 "/convert_tmp_%08X_%04d.ppm", getpid(), i + 1);
217 tmp_fnames[i] = tmp_str;
220 destroy_str_list(tmp_fnames, files_count);
221 PGEN_IF_FREE_MEM(tmp_fnames);
222 printf("ERROR: out of memory\n");
226 for (i = 0; i < files_count; ++i) {
227 res = process_image_downscale(
228 fnames[i], tmp_fnames[i], &av_size);
230 printf("ERROR in image downscale\n");
236 remove_tmp_files(tmp_fnames, files_count);
237 destroy_str_list(tmp_fnames, files_count);
238 PGEN_IF_FREE_MEM(tmp_fnames);
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);
248 res = img2pdf_preview_pages(tmp_fnames[0], outfname, ppd_fname,
249 &paper_size, &av_size, orientation, n_up,
250 &scale, is_grayscale);
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);
258 printf("ERROR in init img\n");
261 PGEN_DEBUG("PDF generation OK");
263 pages_count = get_pdf_pages_count(outfname);
264 printf("init img: pages count: %d\n", pages_count);
270 int process_init_pdf(int argc, char *argv[])
275 struct paper_size_pts paper_size;
276 enum page_orientation orientation;
278 struct page_scale scale;
285 printf("ERROR in init pdf arguments count\n");
293 paper_size.name = argv[3];
294 paper_size.s.x = atof(argv[4]);
295 paper_size.s.y = atof(argv[5]);
297 if (parse_page_orientation(argv[6], &orientation) < 0)
300 n_up = atoi(argv[7]);
302 if (parse_scale_type(argv[8], &(scale.type)) < 0)
304 scale.zoom = atoi(argv[9]);
305 scale.w = atof(argv[10]);
306 scale.h = atof(argv[11]);
308 is_grayscale = atoi(argv[12]);
311 res = pdf2pdf_preview_pages(fname, outfname, ppd_fname,
312 &paper_size, orientation, n_up,
313 &scale, is_grayscale);
315 printf("ERROR in init pdf\n");
318 pages_count = get_pdf_pages_count(outfname);
319 printf("init pdf: pages count: %d\n", pages_count);
324 int process_pagegen(int argc, char *argv[])
329 struct size_pts paper_size;
330 struct size_px av_size;
331 struct size_px shadow_size;
335 struct preview_page_req settings_req;
341 printf("ERROR in gen arguments count\n");
346 page = atoi(argv[1]);
347 outfname_ppm = argv[2];
349 paper_size.x = atof(argv[3]);
350 paper_size.y = atof(argv[4]);
352 av_size.x = atoi(argv[5]);
353 av_size.y = atoi(argv[6]);
355 shadow_size.x = atoi(argv[7]);
356 shadow_size.y = atoi(argv[8]);
358 is_rotate90 = atoi(argv[9]);
359 is_grayscale = atoi(argv[10]);
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;
368 /*if (get_preview_settings(&settings_req, &settings_px) < 0) {
369 printf("ERROR in settings\n");
373 res = save_pdf_preview_page_image(pdf_fname, page,
374 &settings_req, outfname_ppm);
376 printf("ERROR in preview page image generation\n");
384 int process_empty_gen(int argc, char *argv[])
387 struct size_pts paper_size;
388 struct size_px av_size;
389 struct size_px shadow_size;
392 struct preview_page_req settings_req;
398 printf("ERROR in empty_gen arguments count\n");
402 outfname_ppm = argv[0];
404 paper_size.x = atof(argv[1]);
405 paper_size.y = atof(argv[2]);
407 av_size.x = atoi(argv[3]);
408 av_size.y = atoi(argv[4]);
410 shadow_size.x = atoi(argv[5]);
411 shadow_size.y = atoi(argv[6]);
413 is_rotate90 = atoi(argv[7]);
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;
422 res = save_empty_preview_page_image(&settings_req, outfname_ppm);
424 printf("ERROR in preview page image generation\n");
433 int process_cmd(int argc, char *argv[])
435 if (strcasecmp("init", argv[1]) == 0) {
437 printf("ERROR in init cmd usage\n");
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");
448 if (strcasecmp("pagegen", argv[1]) == 0)
449 return process_pagegen(argc - 1, &(argv[2]));
451 if (strcasecmp("empty_gen", argv[1]) == 0)
452 return process_empty_gen(argc - 1, &(argv[2]));
458 int main(int argc, char *argv[])
465 printf("ERROR: no arguments, type help for usage\n");
469 if (strcasecmp("help", argv[1]) == 0) {
474 ret = mkdir(PREVIEW_TEMP_DIR, 0777);
476 if (ret < 0 && errsv != EEXIST) {
477 printf("Failed to mkdir temporary directory " PREVIEW_TEMP_DIR
478 ", errno = %d\n", errsv);
483 res = process_cmd(argc, argv);
486 printf("Arguments ERROR\n");