home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / IMGPROC.ZIP / C8.ZIP / IMAGESUP.C next >
Encoding:
C/C++ Source or Header  |  1990-04-06  |  5.4 KB  |  201 lines

  1. /*  
  2. Copyright 1990 by John Wiley & Sons, Inc.
  3.           All Rights Reserved.
  4. */
  5. /****************************************/
  6. /* Image Processing Support Functions   */
  7. /*       written in Turbo C 2.0         */
  8. /*                by                    */
  9. /*         Craig A. Lindley             */
  10. /*                                      */
  11. /*   Vers: 1.0  Last Update: 11/14/89   */
  12. /****************************************/
  13.  
  14. #include <stdio.h>
  15. #include <process.h>
  16. #include <conio.h>
  17. #include <dos.h>
  18. #include <alloc.h>
  19. #include <mem.h>
  20. #include <graphics.h>
  21. #include "misc.h"
  22. #include "pcx.h"
  23. #include "vga.h"
  24. #include "imagesup.h"
  25.  
  26.  
  27. extern struct PCX_File PCXData;
  28. extern unsigned ImageWidth;
  29. extern unsigned ImageHeight;
  30.  
  31. /*
  32. Image Processing Support Functions - See text for details.
  33. */
  34.  
  35. /*
  36. Copy a complete image from source buffer to destination buffer
  37. */
  38.  
  39. void CopyImage(BYTE huge *SourceBuf, BYTE huge *DestBuf)
  40. {
  41.    movedata(FP_SEG(SourceBuf),FP_OFF(SourceBuf),
  42.         FP_SEG(DestBuf),FP_OFF(DestBuf),
  43.         (unsigned) RASTERSIZE);
  44. }
  45.  
  46.  
  47. /*
  48. NOTE: to index into the image memory like an array, the index
  49. value must be a long variable type, NOT just cast to long.
  50. */
  51.  
  52. BYTE GetPixelFromImage(BYTE huge *Image, unsigned Col, unsigned Row)
  53. {
  54.   unsigned long PixelBufOffset;
  55.  
  56.   if((Col < ImageWidth) && (Row < ImageHeight))
  57.   {
  58.      PixelBufOffset  = Row;            /* done to prevent overflow */
  59.      PixelBufOffset *= ImageWidth;
  60.      PixelBufOffset += Col;
  61.      return(Image[PixelBufOffset]);
  62.   }
  63.   printf("GetPixelFromImage Error: Coordinate out of range\n");
  64.   printf("  Col = %d  Row = %d\n",Col,Row);
  65.   return(FALSE);
  66. }
  67.  
  68. CompletionCode PutPixelInImage(BYTE huge *Image, unsigned Col,
  69.                    unsigned Row, unsigned Color)
  70. {
  71.   unsigned long PixelBufOffset;
  72.  
  73.   if((Col < ImageWidth) && (Row < ImageHeight))
  74.   {
  75.      PixelBufOffset  = Row;            /* done to prevent overflow */
  76.      PixelBufOffset *= ImageWidth;
  77.      PixelBufOffset += Col;
  78.      Image[PixelBufOffset] = Color;
  79.      return(TRUE);
  80.   }
  81.   else
  82.   {
  83.      printf("PutPixelInImage Error: Coordinate out of range\n");
  84.      printf("  Col = %d  Row = %d\n",Col,Row);
  85.      return(FALSE);
  86.   }
  87. }
  88.  
  89. /*
  90. NOTE: A length of 0 is one pixel on. A length of 1 is two pixels
  91. on. That is why length is incremented before being used.
  92. */
  93.  
  94. CompletionCode DrawHLine(BYTE huge *Image, unsigned Col, unsigned Row,
  95.              unsigned Length, unsigned Color)
  96. {
  97.    if ((Col < ImageWidth) && ((Col+Length) <= ImageWidth) &&
  98.        (Row < ImageHeight))
  99.    {
  100.       Length++;
  101.       while(Length--)
  102.     PutPixelInImage(Image,Col++,Row,Color);
  103.       return(TRUE);
  104.    }
  105.    else
  106.    {
  107.       printf("DrawHLine Error: Coordinate out of range\n");
  108.       printf("  Col = %d  Row = %d  Length = %d\n",Col,Row,Length);
  109.       return(FALSE);
  110.    }
  111. }
  112.  
  113.  
  114. CompletionCode DrawVLine(BYTE huge *Image, unsigned Col, unsigned Row,
  115.              unsigned Length, unsigned Color)
  116. {
  117.    if ((Row < ImageHeight) && ((Row+Length) <= ImageHeight) &&
  118.        (Col < ImageWidth))
  119.    {
  120.       Length++;
  121.       while(Length--)
  122.     PutPixelInImage(Image,Col,Row++,Color);
  123.       return(TRUE);
  124.    }
  125.    else
  126.    {
  127.       printf("DrawVLine Error: Coordinate out of range\n");
  128.       printf("  Col = %d  Row = %d  Length = %d\n",Col,Row,Length);
  129.       return(FALSE);
  130.    }
  131. }
  132.  
  133. void ReadImageAreaToBuf (BYTE huge *Image, unsigned Col, unsigned Row,
  134.              unsigned Width, unsigned Height, BYTE huge *Buffer)
  135. {
  136.    unsigned long PixelBufOffset = 0L;
  137.    register unsigned ImageCol, ImageRow;
  138.  
  139.    for (ImageRow=Row; ImageRow < Row+Height; ImageRow++)
  140.       for (ImageCol=Col; ImageCol < Col+Width; ImageCol++)
  141.          Buffer[PixelBufOffset++] =
  142.           GetPixelFromImage(Image,ImageCol,ImageRow);
  143. }
  144.  
  145. void WriteImageAreaFromBuf (BYTE huge *Buffer, unsigned BufWidth,
  146.                 unsigned BufHeight, BYTE huge *Image,
  147.                 unsigned ImageCol, unsigned ImageRow)
  148. {
  149.    unsigned long PixelBufOffset;
  150.    register unsigned BufCol, BufRow, CurrentImageCol;
  151.  
  152.    for (BufRow = 0; BufRow < BufHeight; BufRow++)
  153.    {
  154.       CurrentImageCol = ImageCol;
  155.       for (BufCol = 0; BufCol < BufWidth; BufCol++)
  156.       {
  157.      PixelBufOffset = (unsigned long)BufRow*BufWidth+BufCol;
  158.      PutPixelInImage(Image,CurrentImageCol,ImageRow,Buffer[PixelBufOffset]);
  159.      CurrentImageCol++;
  160.       }
  161.       ImageRow++;
  162.    }
  163. }
  164.  
  165.  
  166. void ClearImageArea(BYTE huge *Image,unsigned Col, unsigned Row,
  167.             unsigned Width, unsigned Height,
  168.             unsigned PixelValue)
  169. {
  170.    register unsigned BufCol, BufRow;
  171.  
  172.    for (BufRow = 0; BufRow < Height; BufRow++)
  173.       for (BufCol = 0; BufCol < Width; BufCol++)
  174.      PutPixelInImage(Image,BufCol+Col,BufRow+Row,PixelValue);
  175. }
  176.  
  177.  
  178. /*
  179. This function checks to make sure the parameters passed to
  180. the image processing functions are all within range. If so
  181. a TRUE is returned. If not, an error message is output and
  182. the calling program is terminated.
  183. */
  184.  
  185. CompletionCode ParameterCheckOK(unsigned Col, unsigned Row,
  186.                 unsigned ColExtent, unsigned RowExtent,
  187.                 char *FunctionName)
  188. {
  189.    if ((Col > MAXCOLNUM) || (Row > MAXROWNUM) ||
  190.        (ColExtent > MAXCOLS) || (RowExtent > MAXROWS))
  191.    {
  192.       restorecrtmode();
  193.       printf("Parameter(s) out of range in function: %s\n",FunctionName);
  194.       printf("  Col = %d  Row = %d  ColExtent = %d  RowExtent = %d\n",
  195.         Col, Row, ColExtent, RowExtent);
  196.  
  197.       exit(EBadParms);
  198.    }
  199.    return(TRUE);
  200. }
  201.