iindex.c
资源名称:ilib [点击查看]
上传用户:changbiao
上传日期:2007-01-13
资源大小:141k
文件大小:9k
源码类别:

图片显示

开发平台:

C/C++

  1. /*
  2.  * Build an image that contains an index of a bunch of images.
  3.  * 
  4.  *
  5.  * Usage:
  6.  *    index [-wN] [-hN] [-f] [-s] [-h] outimage inimage1 ...
  7.  *
  8.  * -wN sets the icon width, Example: -w120
  9.  * -HN sets the icon height, Example: -h120
  10.  * -f go ahead and overwrite the output if it already exists
  11.  * -s silent
  12.  * -html f specify an output file to write an HTML client-side image map
  13.  *
  14.  * File format types will be determined by their filename
  15.  * extension (".jpg", ".gif", ".png", etc.)
  16.  *
  17.  * Note:
  18.  * You can use '-' for the outimage to write to standard output:
  19.  *   ./index - file1.jpg file2.jpg file3.jpg > index.jpg
  20.  * Note: you can only use JPEG for output this way.
  21.  *
  22.  * History:
  23.  * 09-Dec-1999 Craig Knudsen cknudsen@radix.net
  24.  * Display help if no arguments are given
  25.  * 28-Sep-1999 Craig Knudsen cknudsen@radix.net
  26.  * Fixed bug where cols was not init to 0
  27.  * 26-Jul-1999 Craig Knudsen cknudsen@radix.net
  28.  * Added 3D look
  29.  * Added -html option to write HTML image maps
  30.  * 23-Jul-1999 Craig Knudsen cknudsen@radix.net
  31.  * Created
  32.  *
  33.  **************************************************************************/
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <time.h>
  38. #include <ctype.h>
  39. #include <math.h>
  40. #include <values.h>
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #include <Ilib.h>
  44. #include "helvR08.h"
  45. #define TOP_SHADOW 232
  46. #define BOTTOM_SHADOW 112
  47. #define BACKGROUND 192
  48. static char *get_basename ( infile )
  49. char *infile;
  50. {
  51.   char *ptr;
  52.   for ( ptr = infile + strlen ( infile ) - 1;
  53.     *ptr != '/' && *ptr != '\' && ptr != infile; ptr-- )
  54.     ;
  55.   if ( *ptr == '/' || *ptr == '\' )
  56.     ptr++;
  57.   return ( ptr );
  58. }
  59. static void print_usage ()
  60. {
  61.   printf ( "Usage:n  index [options] outfile infile1 infile2 ...n" );
  62.   printf ( "nOptions:n" );
  63.   printf ( "t-h       show this help informationn" );
  64.   printf ( "t-f       overwrite outfile if it already existsn" );
  65.   printf ( "t-wN      use pixel width of N for iconsn" );
  66.   printf ( "t-HN      use pixel height of N for iconsn" );
  67.   printf ( "t-s       run silently (no output to stdout)n" );
  68.   printf ( "t-html f  specify an output file to write an HTML client-side image mapn" );
  69.   exit ( 0 );
  70. }
  71. /*
  72. ** Main
  73. */
  74. int main ( argc, argv )
  75. int argc;
  76. char *argv[];
  77. {
  78.   int loop;
  79.   int force = 0;
  80.   int silent = 0;
  81.   int iconW = 80;
  82.   int iconH = 60;
  83.   int thisW, thisH;
  84.   char *outfile = NULL;
  85.   char *infiles[1024];
  86.   char *filename;
  87.   int len;
  88.   unsigned int strw;
  89.   int ninfiles = 0;
  90.   FILE *fp, *in_fp, *map_fp = NULL;
  91.   IFileFormat format;
  92.   int border = 5;
  93.   int textspace = 12;
  94.   int rows, cols, row, col;
  95.   int w, h, x, y;
  96.   IImage image, in_image;
  97.   IFont font;
  98.   IGC gc;
  99.   IColor black, bg, ts, bs;
  100.   IError ret;
  101.   int scale;
  102.   struct stat buf;
  103.   for ( loop = 1; loop < argc; loop++ ) {
  104.     if ( strcmp ( argv[loop], "-h" ) == 0 ||
  105.       strcmp ( argv[loop], "-help" ) == 0 ||
  106.       strcmp ( argv[loop], "--help" ) == 0 ) {
  107.       print_usage ();
  108.     }
  109.     else if ( strcmp ( argv[loop], "-html" ) == 0 ) {
  110.       map_fp = fopen ( argv[++loop], "w" );
  111.       if ( map_fp == NULL ) {
  112.         fprintf ( stderr, "Error: could not write map file "%s"n",
  113.           argv[loop] );
  114.       }
  115.     }
  116.     else if ( strncmp ( argv[loop], "-w", 2 ) == 0 &&
  117.       strlen ( argv[loop] ) > 2 ) {
  118.       iconW = atoi ( argv[loop] + 2 );
  119.       if ( iconW <= 0 ) {
  120.         fprintf ( stderr, "Invalid width setting "%s"n", argv[loop] );
  121.         exit ( 1 );
  122.       }
  123.     }
  124.     else if ( strncmp ( argv[loop], "-h", 2 ) == 0 &&
  125.       strlen ( argv[loop] ) > 2 ) {
  126.       iconH = atoi ( argv[loop] + 2 );
  127.       if ( iconH <= 0 ) {
  128.         fprintf ( stderr, "Invalid height setting "%s"n", argv[loop] );
  129.         exit ( 1 );
  130.       }
  131.     }
  132.     else if ( strncmp ( argv[loop], "-f", 2 ) == 0 )
  133.       force = 1;
  134.     else if ( strncmp ( argv[loop], "-s", 2 ) == 0 )
  135.       silent = 1;
  136.     else if ( outfile == NULL ) {
  137.       outfile = argv[loop];
  138.       if ( strcmp ( outfile, "-" ) == 0 ) {
  139.         fp = stdin;
  140.       } else {
  141.         if ( stat ( outfile, &buf ) == 0 && ! force ) {
  142.           fprintf ( stderr, "Error: output file %s already exists.n",
  143.             outfile );
  144.           fprintf ( stderr, "Delete it or use the -f option.n" );
  145.           exit ( 1 );
  146.         }
  147.         fp = fopen ( outfile, "wb" );
  148.         if ( fp == NULL ) {
  149.           fprintf ( stderr, "Error writing to %sn", outfile );
  150.           exit ( 1 );
  151.         }
  152.       }
  153.     }
  154.     else {
  155.       infiles[ninfiles++] = argv[loop];
  156.     }
  157.   }
  158.   if ( outfile == NULL ) {
  159.     print_usage ();
  160.   }
  161.   /* load font */
  162.   if ( ( ret = ILoadFontFromData ( "helvR08", helvR08_font, &font ) ) ) {
  163.     fprintf ( stderr, "Error (%s) loading font: helvR08n",
  164.       IErrorString ( ret ) );
  165.     exit ( 1 );
  166.   }
  167.   gc = ICreateGC ();
  168.   /* determine size of output image */
  169.   cols = 0;
  170.   while ( cols * cols < ninfiles )
  171.     cols++;
  172.   rows = ninfiles / cols;
  173.   if ( ninfiles > ( rows * cols ) )
  174.     rows++;
  175.   w = border + cols * ( iconW + border );
  176.   h = border + rows * ( iconH + border + textspace );
  177.   image = ICreateImage ( w, h, IOPTION_NONE );
  178.   gc = ICreateGC ();
  179.   black = IAllocColor ( 0, 0, 0 );
  180.   bg = IAllocColor (  BACKGROUND, BACKGROUND, BACKGROUND );
  181.   ts = IAllocColor (  TOP_SHADOW, TOP_SHADOW, TOP_SHADOW );
  182.   bs = IAllocColor (  BOTTOM_SHADOW, BOTTOM_SHADOW, BOTTOM_SHADOW );
  183.   ISetForeground ( gc, bg );
  184.   IFillRectangle ( image, gc, 0, 0, w, h );
  185.   ISetForeground ( gc, black );
  186.   ISetFont ( gc, font );
  187.   if ( map_fp != NULL ) {
  188.     fprintf ( map_fp,
  189.       "<html><head><title>Image Index</title></head>n" );
  190.     fprintf ( map_fp, "<body bgcolor="#%02x%02x%02x"><h2>Image Index</h2>n",
  191.       BACKGROUND, BACKGROUND, BACKGROUND );
  192.     fprintf ( map_fp, "<map name="image_index">n" );
  193.   }
  194.   for ( row = col = loop = 0; loop < ninfiles; loop++ ) {
  195.     filename = get_basename ( infiles[loop] );
  196.     ret = IFileType ( infiles[loop], &format );
  197.     if ( ret ) {
  198.       fprintf ( stderr, "Input file error: %sn", IErrorString ( ret ) );
  199.       exit ( 1 );
  200.     }
  201.     in_fp = fopen ( infiles[loop], "rb" );
  202.     if ( in_fp == NULL ) {
  203.       fprintf ( stderr, "Error opening file %sn", infiles[loop] );
  204.       continue;
  205.     }
  206.     if ( ! silent )
  207.       printf ( "Reading %s.n", infiles[loop] );
  208.     if ( ( ret = IReadImageFile ( in_fp, format, IOPTION_NONE, &in_image ) ) ) {
  209.       fprintf ( stderr, "Error reading image %s: %sn", infiles[loop],
  210.         IErrorString ( ret ) );
  211.       exit ( 1 );
  212.     }
  213.     x = border + col * ( iconW + border );
  214.     y = border + row * ( iconH + border + textspace );
  215.     /* determine scaled size.  maintain proportions */
  216.     scale = 0;
  217.     do {
  218.       scale++;
  219.       thisW = IImageWidth ( in_image ) / scale;
  220.       thisH = IImageHeight ( in_image ) / scale;
  221.     } while ( thisW > iconW || thisH > iconH );
  222.     /*
  223.     if ( ! silent )
  224.       printf ( "tCopying to (%d,%d) as %dx%d (scale=1:%d)n",
  225.         x, y, thisW, thisH, scale );
  226.     */
  227.     ISetForeground ( gc, ts );
  228.     IDrawLine ( image, gc, x - 1, y - 1, x + iconW + 1, y - 1 );
  229.     IDrawLine ( image, gc, x - 1, y - 1, x - 1, y + iconH + 1 );
  230.     ISetForeground ( gc, bs );
  231.     IDrawLine ( image, gc, x - 1, y + iconH + 1, x + iconW + 1, y + iconH + 1 );
  232.     IDrawLine ( image, gc, x + iconW + 1, y - 1, x + iconW + 1, y + iconH + 1 );
  233.     ISetForeground ( gc, black );
  234.     ret = ICopyImageScaled ( in_image, image, gc,
  235.       0, 0, IImageWidth ( in_image ), IImageHeight ( in_image ),
  236.       x + ( iconW - thisW ) / 2, y + ( iconH - thisH ) / 2, thisW, thisH );
  237.     /* don't write more text than will fit under the image. */
  238.     len = strlen ( filename );
  239.     ret = ITextWidth ( gc, font, filename, len, &strw );
  240.     while ( strw > iconW && len > 0 ) {
  241.       ret = ITextWidth ( gc, font, filename, --len, &strw );
  242.     }
  243.     IDrawString ( image, gc, x, y + iconH + textspace - 2,
  244.       filename, len );
  245.     if ( map_fp != NULL )
  246.       fprintf ( map_fp,
  247.         "<area shape="rect" coords="%d,%d,%d,%d" href="%s">n",
  248.         x, y, x + iconW, y + iconH, infiles[loop] );
  249.     IFreeImage ( in_image );
  250.     col++;
  251.     if ( col >= cols ) {
  252.       col = 0;
  253.       row++;
  254.     }
  255.   }
  256.   if ( map_fp != NULL ) {
  257.     fprintf ( map_fp,
  258.       "<img src="%s" width="%d" height="%d" usemap="#image_index" border="0">n",
  259.       outfile, IImageWidth ( image ), IImageHeight ( image ) );
  260.     fprintf ( map_fp, "</map>n" );
  261.     fprintf ( map_fp,
  262.       "<p><font size="-1">Generated with <A HREF="%s">Ilib</A>.</font>n",
  263.       ILIB_URL );
  264.     fprintf ( map_fp, "</body></html>n" );
  265.   }
  266.   if ( strcmp ( outfile, "-" ) == 0 )
  267.     format = IFORMAT_JPEG;
  268.   else
  269.     ret = IFileType ( outfile, &format );
  270.   fp = fopen ( outfile, "wb" );
  271.   if ( ! fp ) {
  272.     fprintf ( stderr, "Error writing %s.n", outfile );
  273.     exit ( 1 );
  274.   }
  275.   if ( ! silent )
  276.     printf ( "Writing %s.n", outfile );
  277.   IWriteImageFile ( fp, image, format, IOPTION_NONE );
  278.   if ( strcmp ( outfile, "-" ) )
  279.     fclose ( fp );
  280.   /* exit */
  281.   return ( 0 );
  282. }