home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / pdflib / pdflib-4.0.1.sit / pdflib-4.0.1 / clients / pdfimage.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  7.2 KB  |  249 lines  |  [TEXT/CWIE]

  1. /*---------------------------------------------------------------------------*
  2.  |              PDFlib - A library for generating PDF on the fly             |
  3.  +---------------------------------------------------------------------------+
  4.  | Copyright (c) 1997-2001 PDFlib GmbH and Thomas Merz. All rights reserved. |
  5.  +---------------------------------------------------------------------------+
  6.  |    This software is NOT in the public domain.  It can be used under two   |
  7.  |    substantially different licensing terms:                               |
  8.  |                                                                           |
  9.  |    The commercial license is available for a fee, and allows you to       |
  10.  |    - ship a commercial product based on PDFlib                            |
  11.  |    - implement commercial Web services with PDFlib                        |
  12.  |    - distribute (free or commercial) software when the source code is     |
  13.  |      not made available                                                   |
  14.  |    Details can be found in the file PDFlib-license.pdf.                   |
  15.  |                                                                           |
  16.  |    The "Aladdin Free Public License" doesn't require any license fee,     |
  17.  |    and allows you to                                                      |
  18.  |    - develop and distribute PDFlib-based software for which the complete  |
  19.  |      source code is made available                                        |
  20.  |    - redistribute PDFlib non-commercially under certain conditions        |
  21.  |    - redistribute PDFlib on digital media for a fee if the complete       |
  22.  |      contents of the media are freely redistributable                     |
  23.  |    Details can be found in the file aladdin-license.pdf.                  |
  24.  |                                                                           |
  25.  |    These conditions extend to ports to other programming languages.       |
  26.  |    PDFlib is distributed with no warranty of any kind. Commercial users,  |
  27.  |    however, will receive warranty and support statements in writing.      |
  28.  *---------------------------------------------------------------------------*/
  29.  
  30. /* $Id: pdfimage.c,v 1.12 2001/04/04 12:55:57 tm Exp $
  31.  *
  32.  * Convert PNG/TIFF/GIF/JPEG images to PDF
  33.  *
  34.  */
  35.  
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <time.h>
  40.  
  41. #if defined(__CYGWIN32__)
  42. #include <getopt.h>
  43. #elif defined(WIN32)
  44. int getopt(int argc, char * const argv[], const char *optstring);
  45. extern char *optarg;
  46. extern int optind;
  47. #elif !defined(WIN32) && !defined(MAC)
  48. #include <unistd.h>
  49. #endif
  50.  
  51. #ifdef WIN32
  52. #include <process.h>
  53. #endif
  54.  
  55. #ifdef NeXT
  56. #include <libc.h>    /* for getopt(), optind, optarg */
  57. #endif
  58.  
  59. #ifdef __CYGWIN32__
  60. #include <getopt.h>    /* for getopt(), optind, optarg */
  61. #endif
  62.  
  63. #include "pdflib.h"
  64.  
  65. #if defined WIN32 || defined __DJGPP__ || \
  66.     defined __OS2__ || defined __IBMC__ || defined __IBMCPP__ || \
  67.     defined __POWERPC__ || defined __CFM68K__ || defined __MC68K__
  68.  
  69. #define READMODE    "rb"
  70.  
  71. #else
  72.  
  73. #define READMODE    "r"
  74.  
  75. #endif    /* Mac, Windows, and OS/2 platforms */
  76.  
  77. static void
  78. usage(void)
  79. {
  80.     fprintf(stderr, "pdfimage: convert images to PDF.\n");
  81.     fprintf(stderr, "(C) PDFlib GmbH and Thomas Merz 1997-2001\n");
  82.     fprintf(stderr, "usage: imagepdf [options] imagefile(s)\n");
  83.     fprintf(stderr, "Available options:\n");
  84.     fprintf(stderr, "-r <res>   force resolution overriding image settings\n");
  85.     fprintf(stderr, "-o <file>  output file\n");
  86.     fprintf(stderr, "-p <num>   bookmark page numbering starting from num\n");
  87.     fprintf(stderr, "-w         print details for damaged image files\n");
  88.  
  89.     exit(1);
  90. }
  91.  
  92. /* Several magic numbers for image file formats */
  93.  
  94. #define    GIF_MAGIC    "GIF"
  95. #define TIFF_MAGIC_M    "MM"
  96. #define TIFF_MAGIC_I    "II"
  97. #define PNG_MAGIC    "\x89PNG"
  98. #define JPEG_MAGIC    "\xFF\xD8"
  99.  
  100. #define MAGIC_LEN_MAX    10        /* maximum length of magic strings */
  101.  
  102. int
  103. main(int argc, char *argv[])
  104. {
  105.     char    *pdffilename = NULL;
  106.     FILE    *imagefile;
  107.     PDF        *p;
  108.     int        image;
  109.     int        opt;
  110.     int        resolution = 0;
  111.     int        page_numbering = 0;
  112.     int        current_page = 1;
  113.     int        warn = 0;
  114.     float    scale_x, scale_y, dpi_x, dpi_y;
  115.     char    header[MAGIC_LEN_MAX];
  116.     
  117.     while ((opt = getopt(argc, argv, "r:o:p:w")) != -1)
  118.     switch (opt) {
  119.         case 'o':
  120.         pdffilename = optarg;
  121.         break;
  122.  
  123.         case 'p':
  124.         page_numbering = 1;
  125.         if (optarg) {
  126.             current_page = atoi(optarg);
  127.         }
  128.         break;
  129.  
  130.         case 'r':
  131.         if (!optarg || (resolution = atoi(optarg)) <= 0) {
  132.             fprintf(stderr, "Error: non-positive resolution.\n");
  133.             usage();
  134.         }
  135.  
  136.         case 'w':
  137.         warn = 1;
  138.         break;
  139.  
  140.         case '?':
  141.         default:
  142.         usage();
  143.     }
  144.  
  145.     if (optind == argc) {
  146.     fprintf(stderr, "Error: no image files given.\n");
  147.     usage();
  148.     }
  149.  
  150.     if (pdffilename == NULL) {
  151.     fprintf(stderr, "Error: no output file given.\n");
  152.     usage();
  153.     }
  154.  
  155.     p = PDF_new();
  156.  
  157.     if (warn)
  158.     PDF_set_parameter(p, "imagewarning", "true");
  159.  
  160.     if (PDF_open_file(p, pdffilename) == -1) {
  161.     fprintf(stderr, "Error: cannot open output file %s.\n", pdffilename);
  162.     exit(1);
  163.     }
  164.  
  165.     PDF_set_info(p, "Creator", "pdfimage");
  166.  
  167.     while (optind++ < argc) {
  168.     fprintf(stderr, "Processing image file '%s'...\n", argv[optind-1]);
  169.  
  170.     if ((imagefile = fopen(argv[optind-1], READMODE)) == NULL) {
  171.        fprintf(stderr, "Error: Couldn't open image file %s - skipped.\n",
  172.         argv[optind-1]);
  173.        continue;
  174.     }
  175.  
  176.     if (fread(header, MAGIC_LEN_MAX, 1, imagefile) != 1)  {
  177.        fprintf(stderr,
  178.            "Error: Couldn't read from image file %s - skipped.\n",
  179.         argv[optind-1]);
  180.        fclose(imagefile);
  181.        continue;
  182.     }
  183.     fclose(imagefile);
  184.  
  185.     if (!strncmp(header, GIF_MAGIC, strlen(GIF_MAGIC)))
  186.        image = PDF_open_image_file(p, "gif", argv[optind-1], "", 0);
  187.  
  188.     else if (!strncmp(header, TIFF_MAGIC_I, strlen(TIFF_MAGIC_I)) ||
  189.              !strncmp(header, TIFF_MAGIC_M, strlen(TIFF_MAGIC_M)))
  190.        image = PDF_open_image_file(p, "tiff", argv[optind-1], "", 0);
  191.  
  192.     else if (!strncmp(header, PNG_MAGIC, strlen(PNG_MAGIC)))
  193.        image = PDF_open_image_file(p, "png", argv[optind-1], "", 0);
  194.  
  195.     else if (!strncmp(header, JPEG_MAGIC, strlen(JPEG_MAGIC)))
  196.        image = PDF_open_image_file(p, "jpeg", argv[optind-1], "", 0);
  197.  
  198.     else
  199.        image = -1;    /* unknown file type */
  200.  
  201.     if (image == -1) {
  202.         fprintf(stderr,"Error: Couldn't analyze image %s - skipped.\n",
  203.             argv[optind-1]);
  204.         continue;
  205.     }
  206.  
  207.     if (resolution) {
  208.         dpi_x = (float) resolution;
  209.         dpi_y = (float) resolution;
  210.     } else {
  211.         dpi_x = PDF_get_value(p, "resx", (float) image);
  212.         dpi_y = PDF_get_value(p, "resy", (float) image);
  213.     }
  214.  
  215.     if (dpi_x > 0 && dpi_y > 0) {
  216.         scale_x = ((float) 72.0) / dpi_x;
  217.         scale_y = ((float) 72.0) / dpi_y;
  218.     } else if (dpi_x < 0 && dpi_y < 0) {
  219.         scale_x = (float) 1.0;
  220.         scale_y = dpi_y / dpi_x;
  221.     } else {
  222.         scale_x = (float) 1.0;
  223.         scale_y = (float) 1.0;
  224.     }
  225.  
  226.     PDF_begin_page(p,
  227.         PDF_get_value(p, "imagewidth", (float) image) * scale_x, 
  228.         PDF_get_value(p, "imageheight", (float) image) * scale_y);
  229.     
  230.     /* define outline with filename or page number */
  231.     if (page_numbering) {
  232.         char buf[32];
  233.         sprintf(buf, "Page %d", current_page++);
  234.         PDF_add_bookmark(p, buf, 0, 0);
  235.     } else {
  236.         PDF_add_bookmark(p, argv[optind-1], 0, 0);
  237.     }
  238.  
  239.     PDF_scale(p, scale_x, scale_y);
  240.     PDF_place_image(p, image, 0.0, 0.0, 1.0);
  241.  
  242.     PDF_end_page(p);
  243.     }
  244.  
  245.     PDF_close(p);
  246.     PDF_delete(p);
  247.     exit(0);
  248. }
  249.