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

图片显示

开发平台:

C/C++

  1. /*
  2.  * sample.c
  3.  *
  4.  * Image library
  5.  *
  6.  * Description:
  7.  * Portable routines to manipulate raster images.
  8.  *
  9.  * Usage:
  10.  * sample [-width N] [-height N] [-text inputtext] [-out file]
  11.  *
  12.  * History:
  13.  * 28-Nov-99 Craig Knudsen cknudsen@radix.net
  14.  * Added use of IFillArc().
  15.  * 26-Aug-99 Craig Knudsen cknudsen@radix.net
  16.  * Added use of named colors.
  17.  * 23-Aug-99 Craig Knudsen cknudsen@radix.net
  18.  * Added use of text styles.
  19.  * 26-Jul-99 Craig Knudsen cknudsen@radix.net
  20.  * Misc. updates
  21.  * 12-Apr-99 Craig Knudsen cknudsen@radix.net
  22.  * Fixed to use real copyright ASCII code
  23.  * rather than escape.
  24.  * 17-May-98 Craig Knudsen cknudsen@radix.net
  25.  * Updated date to 1998
  26.  * Added call to ITextDimensions to center text.
  27.  * 20-May-96 Craig Knudsen cknudsen@radix.net
  28.  * Created
  29.  *
  30.  ****************************************************************************/
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <ctype.h>
  34. #include <Ilib.h>
  35. #define DEFAULT_FONTNAME_1 "helvR24"
  36. #define DEFAULT_FONT_1 helvR24_font
  37. #define DEFAULT_FONTNAME_2 "helvR08"
  38. #define DEFAULT_FONT_2 helvR08_font
  39. /* This will embed the font data within the application so that you can
  40. ** distribute the binary by itself.
  41. */
  42. #include "helvR24.h"
  43. #include "helvR08.h"
  44. int main ( argc, argv )
  45. int argc;
  46. char *argv[];
  47. {
  48.   IImage image;
  49.   IFont largefont, smallfont;
  50.   char *fontname1 = NULL, *fontname2 = NULL;
  51.   int loop;
  52.   unsigned int width = 500, height = 150, text_width = 0, text_height = 0,
  53.     font_height = 0, font_width, sample_width, sample_height;
  54.   int x, y;
  55.   char *outfile = "out.ppm";
  56.   char *sample_text = NULL;
  57.   char *copyright = "@Copyright 1999 Craig Knudsen";
  58.   IFileFormat output_format = IFORMAT_PPM;
  59.   char *infile = NULL;
  60.   FILE *fp;
  61.   IGC gc;
  62.   IError ret;
  63.   IColor topshadow, bottomshadow, textcolor, background, black, white;
  64.   for ( loop = 1; loop < argc; loop++ ) {
  65.     if ( strcmp ( argv[loop], "-w" ) == 0 ||
  66.       strcmp ( argv[loop], "-width" ) == 0 ) {
  67.       if ( ++loop >= argc ) {
  68.         fprintf ( stderr, "-width requires an argumentn" );
  69.         exit ( 1 );
  70.       }
  71.       width = atoi ( argv[loop] );
  72.     }
  73.     else if ( strcmp ( argv[loop], "-h" ) == 0 ||
  74.       strcmp ( argv[loop], "-height" ) == 0 ) {
  75.       if ( ++loop >= argc ) {
  76.         fprintf ( stderr, "-height requires an argumentn" );
  77.         exit ( 1 );
  78.       }
  79.       height = atoi ( argv[loop] );
  80.     }
  81.     else if ( strcmp ( argv[loop], "-font" ) == 0 ) {
  82.       if ( ++loop >= argc ) {
  83.         fprintf ( stderr, "-font requires an argumentn" );
  84.         exit ( 1 );
  85.       }
  86.       fontname1 = argv[loop];
  87.     }
  88.     else if ( strcmp ( argv[loop], "-t" ) == 0 ||
  89.       strcmp ( argv[loop], "-text" ) == 0 ) {
  90.       if ( ++loop >= argc ) {
  91.         fprintf ( stderr, "-text requires an argumentn" );
  92.         exit ( 1 );
  93.       }
  94.       sample_text = argv[loop];
  95.     }
  96.     else if ( strcmp ( argv[loop], "-o" ) == 0 ||
  97.       strcmp ( argv[loop], "-out" ) == 0 ) {
  98.       if ( ++loop >= argc ) {
  99.         fprintf ( stderr, "-out requires an argumentn" );
  100.         exit ( 1 );
  101.       }
  102.       outfile = argv[loop];
  103.     }
  104.     else if ( strcmp ( argv[loop], "-i" ) == 0 ||
  105.       strcmp ( argv[loop], "-infile" ) == 0 ) {
  106.       if ( ++loop >= argc ) {
  107.         fprintf ( stderr, "-infile requires an argumentn" );
  108.         exit ( 1 );
  109.       }
  110.       infile = argv[loop];
  111.     }
  112.   }
  113.   if ( sample_text == NULL ) {
  114.     sample_text = (char *) malloc ( 100 );
  115.     sprintf ( sample_text, "Ilib v%s (%s)n%s", ILIB_VERSION, ILIB_VERSION_DATE,
  116.       ILIB_URL );
  117.   }
  118.   
  119.   if ( infile ) {
  120.     fp = fopen ( infile, "r" );
  121.     if ( ! fp ) {
  122.       perror ( "Error opening input file:" );
  123.       exit ( 1 );
  124.     }
  125.     if ( ( ret = IReadImageFile ( fp, IFORMAT_PPM, IOPTION_NONE, &image ) ) ) {
  126.       fprintf ( stderr, "Error reading image: %dn", ret );
  127.       exit ( 1 );
  128.     }
  129.     fclose ( fp );
  130.   }
  131.   else {
  132.     image = ICreateImage ( width, height, IOPTION_NONE );
  133.   }
  134.   gc = ICreateGC ();
  135.   IAllocNamedColor ( "gray", &background );
  136.   ISetBackground ( gc, background );
  137.   IAllocNamedColor ( "lightgrey", &topshadow );
  138.   IAllocNamedColor ( "darkgrey", &bottomshadow );
  139.   IAllocNamedColor ( "yellow", &textcolor );
  140.   IAllocNamedColor ( "black", &black );
  141.   IAllocNamedColor ( "white", &white );
  142.   /* draw top shadow rectangle */
  143.   ISetForeground ( gc, topshadow );
  144.   IFillRectangle ( image, gc, 0, 0, width, height );
  145.   /* draw bottom shadow rectangle */
  146.   ISetForeground ( gc, bottomshadow );
  147.   IFillRectangle ( image, gc, 2, 2, width - 2, height - 2 );
  148.   /* draw background rectangle */
  149.   ISetForeground ( gc, background );
  150.   IFillRectangle ( image, gc, 2, 2, width - 4, height - 4 );
  151.   /* Now the fun part: draw some text */
  152.   if ( fontname1 ) {
  153.     if ( ( ret = ILoadFontFromFile ( fontname1, fontname1, &largefont ) ) ) {
  154.       fprintf ( stderr, "Error (%s) loading font: %sn",
  155.         IErrorString ( ret ), fontname1 );
  156.       exit ( 1 );
  157.     }
  158.   }
  159.   else {
  160.     fontname1 = DEFAULT_FONTNAME_1;
  161.     if ( ( ret = ILoadFontFromData ( fontname1, DEFAULT_FONT_1, &largefont ) ) ) {
  162.       fprintf ( stderr, "Error (%s) loading font: %sn", 
  163.         IErrorString ( ret ), fontname1 );
  164.       exit ( 1 );
  165.     }
  166.   }
  167.   fontname2 = DEFAULT_FONTNAME_2;
  168.   if ( ( ret = ILoadFontFromData ( fontname2, DEFAULT_FONT_2, &smallfont ) ) ) {
  169.     fprintf ( stderr, "Error (%s) loading font: %sn", IErrorString ( ret ),
  170.       fontname2 );
  171.     exit ( 1 );
  172.   }
  173.   ISetFont ( gc, largefont );
  174.   ret = ITextDimensions ( gc, largefont, sample_text, strlen ( sample_text ),
  175.     &text_width, &text_height );
  176.   ret = ITextDimensions ( gc, largefont, "X", 1, &font_width, &font_height );
  177.   x = ( (int)width - (int)text_width ) / 2;
  178.   y = ( ( (int)height - (int)text_height ) / 2 ) + (int)font_height;
  179.   /* draw arc */
  180.   ISetForeground ( gc, topshadow );
  181.   IFillArc ( image, gc, x - 2, y - 2, 20, 30, 90, 270 );
  182.   IFillArc ( image, gc, width - x - 2, y - 2, 20, 30, -90, 90 );
  183.   IFillRectangle ( image, gc, x - 2, y - 30 - 2, width - 2 * x, 60 );
  184.   ISetForeground ( gc, bottomshadow );
  185.   IFillArc ( image, gc, x + 2, y + 2, 20, 30, 90, 270 );
  186.   IFillArc ( image, gc, width - x + 2, y + 2, 20, 30, -90, 90 );
  187.   IFillRectangle ( image, gc, x + 2, y - 30 + 2, width - 2 * x, 60 );
  188.   ISetForeground ( gc, background );
  189.   IFillArc ( image, gc, x, y, 20, 30, 90, 270 );
  190.   IFillArc ( image, gc, width - x, y, 20, 30, -90, 90 );
  191.   IFillRectangle ( image, gc, x, y - 30, width - 2 * x, 60 );
  192.   /* draw text */
  193.   ISetForeground ( gc, textcolor );
  194.   ISetTextStyle ( gc, ITEXT_SHADOWED );
  195.   IDrawString ( image, gc, x, y, sample_text, strlen ( sample_text ) );
  196.   y += text_height;
  197.   /* draw "SAMPLE" from top to bottom on the left side */
  198.   ISetTextStyle ( gc, ITEXT_ETCHED_IN );
  199.   ret = ITextDimensions ( gc, largefont, "SAMPLE", strlen ( "SAMPLE" ),
  200.     &sample_width, &sample_height );
  201.   IDrawStringRotated ( image, gc, 8, ( height - sample_width ) / 2 + 1,
  202.     "SAMPLE", 6, ITEXT_TOP_TO_BOTTOM );
  203.   /* draw "SAMPLE" from bottom to top on the right side */
  204.   IDrawStringRotated ( image, gc,
  205.     width - 6, ( height + sample_width ) / 2 - 1,
  206.     "SAMPLE", strlen ( "SAMPLE" ), ITEXT_BOTTOM_TO_TOP );
  207.   /* draw copyright */
  208.   ISetTextStyle ( gc, ITEXT_SHADOWED );
  209.   ISetFont ( gc, smallfont );
  210.   ISetForeground ( gc, textcolor );
  211.   IDrawString ( image, gc, x, y, copyright, strlen ( copyright ) );
  212.   if ( outfile ) {
  213.     /* determine output file type (ppm/png/gif/etc.) by filename extension */
  214.     ret = IFileType ( outfile, &output_format );
  215.     if ( ret ) {
  216.       fprintf ( stderr, "Output file error: %sn", IErrorString ( ret ) );
  217.       exit ( 1 );
  218.     }
  219.     /* make sure to include "b" (for binary) for Win32 */
  220.     fp = fopen ( outfile, "wb" );
  221.     if ( ! fp ) {
  222.       perror ( "Cannot open output file: " );
  223.       exit ( 1 );
  224.     }
  225.   }
  226.   else
  227.     fp = stdout;
  228.   /* write output image file */
  229.   IWriteImageFile ( fp, image, output_format, IOPTION_INTERLACED );
  230.   fclose ( fp );
  231.   /* free up resources */
  232.   IFreeFont ( largefont );
  233.   IFreeFont ( smallfont );
  234.   IFreeImage ( image );
  235.   return ( 0 );
  236. }