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

图片显示

开发平台:

C/C++

  1. /*
  2.  * ICopyImg.c
  3.  *
  4.  * Image library
  5.  *
  6.  * Description:
  7.  * Copy an area of an image to another image.
  8.  *
  9.  * History:
  10.  * 23-Jul-99 Craig Knudsen   cknudsen@radix.net
  11.  * Added ICopyImageScaled
  12.  * 11-Nov-98 Craig Knudsen cknudsen@radix.net
  13.  * Allow transparent values to not be copied.
  14.  * 20-May-96 Craig Knudsen cknudsen@radix.net
  15.  * Created
  16.  *
  17.  ****************************************************************************/
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21. #include <memory.h>
  22. #include "Ilib.h"
  23. #include "IlibP.h"
  24. IError ICopyImage ( source, dest, gc, src_x, src_y, width, height,
  25.   dest_x, dest_y )
  26. IImage source, dest;
  27. IGC gc;
  28. int src_x, src_y;
  29. unsigned int width, height;
  30. int dest_x, dest_y;
  31. {
  32.   IImageP *i1 = (IImageP *) source;
  33.   IImageP *i2 = (IImageP *) dest;
  34.   IGCP *gcp = (IGCP *) gc;
  35.   int row, col, x, y;
  36.   IColor color;
  37.   IColorP *colorp;
  38.   IColorP *save;
  39.   unsigned char *ptr;
  40.   if ( i1 ) {
  41.     if ( i1->magic != IMAGIC_IMAGE )
  42.       return ( IInvalidImage );
  43.   }
  44.   else
  45.     return ( IInvalidImage );
  46.   if ( i2 ) {
  47.     if ( i2->magic != IMAGIC_IMAGE )
  48.       return ( IInvalidImage );
  49.   }
  50.   else
  51.     return ( IInvalidImage );
  52.   if ( gcp ) {
  53.     if ( gcp->magic != IMAGIC_GC )
  54.       return ( IInvalidGC );
  55.   }
  56.   else
  57.     return ( IInvalidGC );
  58.   save = gcp->foreground;
  59.   color = IAllocColor ( 0, 0, 0 );
  60.   colorp = _IGetColor ( color );
  61.   ISetForeground ( gc, color );
  62.   for ( row = src_y, y = dest_y; row < src_y + height; row++, y++ ) {
  63.     for ( col = src_x, x = dest_x; col < src_x + width; col++, x++ ) {
  64.       if ( i1->greyscale ) {
  65.         ptr = i1->data + ( row * i1->width ) + col;
  66.         colorp->red = colorp->green = colorp->blue = *(ptr);
  67.         _ISetPoint ( i2, gcp, x, y );
  68.       }
  69.       else {
  70.         ptr = i1->data + ( row * i1->width * 3 ) + ( col * 3 );
  71.         /* check for transparent color */
  72.         if ( i1->transparent == NULL 
  73.           || i1->transparent->red != *(ptr)
  74.           || i1->transparent->green != *(ptr + 1)
  75.           || i1->transparent->blue != (*ptr + 2) ) {
  76.           colorp->red = *(ptr);
  77.           colorp->green = *(ptr + 1);
  78.           colorp->blue = *(ptr + 2);
  79.           _ISetPoint ( i2, gcp, x, y );
  80.         }
  81.       }
  82.     }
  83.   }
  84.   IFreeColor ( color );
  85.   gcp->foreground = save;
  86.   return ( INoError );
  87. }
  88. /*
  89. ** This allows the user to scale up or down the source image onto
  90. ** the destination image.
  91. */
  92. IError ICopyImageScaled ( source, dest, gc,
  93.   src_x, src_y, src_width, src_height,
  94.   dest_x, dest_y, dest_width, dest_height )
  95. IImage source, dest;
  96. IGC gc;
  97. int src_x, src_y;
  98. unsigned int src_width, src_height;
  99. int dest_x, dest_y;
  100. unsigned int dest_width, dest_height;
  101. {
  102.   IImageP *i1 = (IImageP *) source;
  103.   IImageP *i2 = (IImageP *) dest;
  104.   IGCP *gcp = (IGCP *) gc;
  105.   int x, y, x2, y2;
  106.   IColor color;
  107.   IColorP *colorp;
  108.   IColorP *save;
  109.   unsigned char *ptr;
  110.   double scalex, scaley;
  111.   double tempx, tempy;
  112.   if ( i1 ) {
  113.     if ( i1->magic != IMAGIC_IMAGE )
  114.       return ( IInvalidImage );
  115.   }
  116.   else
  117.     return ( IInvalidImage );
  118.   if ( i2 ) {
  119.     if ( i2->magic != IMAGIC_IMAGE )
  120.       return ( IInvalidImage );
  121.   }
  122.   else
  123.     return ( IInvalidImage );
  124.   if ( gcp ) {
  125.     if ( gcp->magic != IMAGIC_GC )
  126.       return ( IInvalidGC );
  127.   }
  128.   else
  129.     return ( IInvalidGC );
  130.   save = gcp->foreground;
  131.   color = IAllocColor ( 0, 0, 0 );
  132.   colorp = _IGetColor ( color );
  133.   ISetForeground ( gc, color );
  134.   /*
  135.   ** When scaling down, we might want to add an algorithm for averaging
  136.   ** a series of source pixels into the destination pixel.  For now,
  137.   ** we just grab one color.
  138.   */
  139.   scalex = (double) dest_width / (double) src_width;
  140.   scaley = (double) dest_height / (double) src_height;
  141.   for ( y = dest_y; y < dest_y + dest_height; y++ ) {
  142.     for ( x = dest_x; x < dest_x + dest_width; x++ ) {
  143.       /* get location from source image for this location
  144.       ** x2,y2 is location in source image.
  145.       */
  146.       tempx = (double) src_x + (double) ( x - dest_x ) / scalex;
  147.       x2 = (int) tempx;
  148.       tempy = (double) src_y + (double) ( y - dest_y ) / scaley;
  149.       y2 = (int) tempy;
  150.       if ( i1->greyscale ) {
  151.         ptr = i1->data + ( y2 * i1->width ) + x2;
  152.         colorp->red = colorp->green = colorp->blue = *(ptr);
  153.         _ISetPoint ( i2, gcp, x, y );
  154.       }
  155.       else {
  156.         ptr = i1->data + ( y2 * i1->width * 3 ) + ( x2 * 3 );
  157.         /* check for transparent color */
  158.         if ( i1->transparent == NULL 
  159.           || i1->transparent->red != *(ptr)
  160.           || i1->transparent->green != *(ptr + 1)
  161.           || i1->transparent->blue != (*ptr + 2) ) {
  162.           colorp->red = *(ptr);
  163.           colorp->green = *(ptr + 1);
  164.           colorp->blue = *(ptr + 2);
  165.           _ISetPoint ( i2, gcp, x, y );
  166.         }
  167.       }
  168.     }
  169.   }
  170.   IFreeColor ( color );
  171.   gcp->foreground = save;
  172.   return ( INoError );
  173. }