home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / IMGPROC.ZIP / C11.ZIP / FRTEST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-06  |  8.8 KB  |  313 lines

  1. /*  
  2. Copyright 1990 by John Wiley & Sons, Inc.
  3.           All Rights Reserved.
  4. */
  5. /****************************************/
  6. /*        Image Processing Code         */
  7. /*       Frame Process Functions        */
  8. /*             Demo Program             */
  9. /*        written in Turbo C 2.0        */
  10. /*                 by                   */
  11. /*          Craig A. Lindley            */
  12. /*                                      */
  13. /*   Vers: 1.0  Last Update: 12/26/89   */
  14. /****************************************/
  15.  
  16. #include <stdio.h>
  17. #include <conio.h>
  18. #include <dos.h>
  19. #include <alloc.h>
  20. #include <process.h>
  21. #include <graphics.h>
  22. #include "misc.h"
  23. #include "pcx.h"
  24. #include "vga.h"
  25. #include "imagesup.h"
  26. #include "ptprocess.h"
  27. #include "frprocess.h"
  28.  
  29. /* main frame process demonstration program */
  30.  
  31. void main(void)
  32. {
  33.    char *InFileName1 = "p2c11i1a";
  34.    char *InFileName2 = "p2c11i1b";
  35.    char *InFileName3 = "p2c11i2a";
  36.    char *InFileName4 = "p2c11i2b";
  37.    char *InFileName5 = "p2c11i3a";
  38.    char *InFileName6 = "p2c11i4a";
  39.    char *InFileName7 = "p2c11i4b";
  40.    char *InFileName8 = "p2c11i5a";
  41.  
  42.    BYTE huge *InImage1;
  43.    BYTE huge *InImage2;
  44.    BYTE huge *Buffer;
  45.  
  46.    unsigned GenPCXFiles = FALSE;   /* controls output file generation */
  47.    BYTE LUT[MAXQUANTLEVELS];
  48.    unsigned Index;
  49.  
  50.  
  51.    /* Initialize graphic subsystem */
  52.    InitGraphics();
  53.  
  54.    printf("Frame Transform Example Program Program\n\n");
  55.    printf("Reading the Image PCX Files into memory\n");
  56.  
  57.    /*
  58.    Allocate enough memory to save destination image
  59.    as it will be written over during each transform.
  60.    */
  61.  
  62.    Buffer = (BYTE huge *) farcalloc(RASTERSIZE,sizeof(BYTE));
  63.    if (Buffer == NULL)
  64.    {
  65.       printf("FRTEST Error: No memory for buffer\n");
  66.       exit(ENoMemory);
  67.    }
  68.  
  69.    /*
  70.    Image Sequence One - Background substraction
  71.    */
  72.  
  73.    /*
  74.    Read source and destination images into memory. The
  75.    image read into InImage2 is considered the destination
  76.    image.
  77.    */
  78.    if (ReadPCXFileToBuf (InFileName1,&InImage1) != NoError)
  79.       exit(1);
  80.    DisplayImageInBuf(InImage1,INITVGALOADPALETTE,WAITFORKEY);
  81.  
  82.    if (ReadPCXFileToBuf (InFileName2,&InImage2) != NoError)
  83.       exit(1);
  84.    DisplayImageInBuf(InImage2,NOVGAINIT,WAITFORKEY);
  85.    /*
  86.    Combine source image with destination image by the subtraction
  87.    process. The destination image is overwritten.
  88.    */
  89.    CombineImages(InImage2,MINCOLNUM,MINROWNUM,MAXCOLS,MAXROWS,
  90.          InImage1,MINCOLNUM,MINROWNUM,Sub,0);
  91.  
  92.    /*
  93.    Display image with background removed
  94.    */
  95.    DisplayImageInBuf(InImage1,NOVGAINIT,WAITFORKEY);
  96.    if (GenPCXFiles)
  97.       WritePCXFile("p2c11i1c",8,320,200,1,320);
  98.    /*
  99.    Return memory taken up by the image buffers
  100.    */
  101.    farfree((BYTE far *) InImage1);
  102.    farfree((BYTE far *) InImage2);
  103.  
  104.    /*
  105.    Image Sequence Two - Circuit board inspection
  106.    */
  107.  
  108.    /*
  109.    Read source and destination images into memory. The
  110.    image read into InImage2 is considered the destination
  111.    image.
  112.    */
  113.    if (ReadPCXFileToBuf (InFileName3,&InImage1) != NoError)
  114.       exit(1);
  115.    DisplayImageInBuf(InImage1,NOVGAINIT,WAITFORKEY);
  116.  
  117.    if (ReadPCXFileToBuf (InFileName4,&InImage2) != NoError)
  118.       exit(1);
  119.    DisplayImageInBuf(InImage2,NOVGAINIT,WAITFORKEY);
  120.  
  121.    /*
  122.    Combine source image with destination image by the subtraction
  123.    process. The destination image is overwritten.
  124.    */
  125.    CombineImages(InImage2,MINCOLNUM,MINROWNUM,MAXCOLS,MAXROWS,
  126.          InImage1,MINCOLNUM,MINROWNUM,Sub,0);
  127.  
  128.    /*
  129.    Display the differences between the circuit board images
  130.    */
  131.    DisplayImageInBuf(InImage1,NOVGAINIT,WAITFORKEY);
  132.    if (GenPCXFiles)
  133.       WritePCXFile("p2c11i2c",8,320,200,1,320);
  134.  
  135.    /*
  136.    Image Sequence Three - Image Masking
  137.    */
  138.  
  139.    /* Read in the image to be masked */
  140.    if (ReadPCXFileToBuf (InFileName5,&InImage1) != NoError)
  141.       exit(1);
  142.    DisplayImageInBuf(InImage1,NOVGAINIT,WAITFORKEY);
  143.  
  144.    /* Prepare the image mask */
  145.    ClearImageArea(Buffer,MINCOLNUM,MINROWNUM,MAXCOLS,MAXROWS,WHITE);
  146.    ClearImageArea(Buffer,200,10,80,180,BLACK);
  147.  
  148.    /* Display mask */
  149.    DisplayImageInBuf(Buffer,NOVGAINIT,WAITFORKEY);
  150.    if (GenPCXFiles)
  151.       WritePCXFile("p2c11i3b",8,320,200,1,320);
  152.  
  153.    /* Preform masking operation */
  154.    CombineImages(Buffer,MINCOLNUM,MINROWNUM,MAXCOLS,MAXROWS,
  155.          InImage1,MINCOLNUM,MINROWNUM,And,0);
  156.    DisplayImageInBuf(InImage1,NOVGAINIT,WAITFORKEY);
  157.    if (GenPCXFiles)
  158.       WritePCXFile("p2c11i3c",8,320,200,1,320);
  159.  
  160.    /*
  161.    Return memory taken up by the image buffers
  162.    */
  163.    farfree((BYTE far *) InImage1);
  164.    farfree((BYTE far *) InImage2);
  165.  
  166.    /*
  167.    Image Sequence Four - Various frame processes
  168.    */
  169.  
  170.    /*
  171.    Read source and destination images into memory. The
  172.    image read into InImage2 is considered the destination
  173.    image.
  174.    */
  175.  
  176.    if (ReadPCXFileToBuf (InFileName6,&InImage1) != NoError)
  177.       exit(1);
  178.    DisplayImageInBuf(InImage1,NOVGAINIT,WAITFORKEY);
  179.  
  180.    if (ReadPCXFileToBuf (InFileName7,&InImage2) != NoError)
  181.       exit(1);
  182.    DisplayImageInBuf(InImage2,NOVGAINIT,WAITFORKEY);
  183.  
  184.    /*
  185.    Make a copy of the destination image so it can be
  186.    restored after each frame process without having
  187.    to reload it from disk.
  188.    */
  189.    CopyImage(InImage2,Buffer);
  190.  
  191.    /*
  192.    Combine source image with destination image by addition
  193.    process. The destination image is overwritten.
  194.    */
  195.    CombineImages(InImage1,32,20,256,160,
  196.          InImage2,32,20,Add,1);
  197.  
  198.    DisplayImageInBuf(InImage2,NOVGAINIT,WAITFORKEY);
  199.    if (GenPCXFiles)
  200.       WritePCXFile("p2c11i4c",8,320,200,1,320);
  201.  
  202.    /*
  203.    Restore InImage2 the destination buffer for use with the
  204.    next frame process transformation. Now we are back in our
  205.    original state with the source image in InImage1 and the
  206.    destination image in InImage2.
  207.    */
  208.    CopyImage(Buffer,InImage2);
  209.  
  210.    /*
  211.    Combine source image with destination image by bitwise OR
  212.    process. The destination image is overwritten.
  213.    */
  214.    CombineImages(InImage1,32,20,256,160,
  215.          InImage2,32,20,Or,0);
  216.  
  217.    DisplayImageInBuf(InImage2,NOVGAINIT,WAITFORKEY);
  218.    if (GenPCXFiles)
  219.       WritePCXFile("p2c11i4d",8,320,200,1,320);
  220.  
  221.    CopyImage(Buffer,InImage2);
  222.  
  223.    /*
  224.    Combine source image with destination image by average
  225.    process. The destination image is overwritten.
  226.    */
  227.    CombineImages(InImage1,32,20,256,160,
  228.          InImage2,32,20,Ave,0);
  229.  
  230.    DisplayImageInBuf(InImage2,NOVGAINIT,WAITFORKEY);
  231.    if (GenPCXFiles)
  232.       WritePCXFile("p2c11i4e",8,320,200,1,320);
  233.  
  234.    CopyImage(Buffer,InImage2);
  235.  
  236.    /*
  237.    Combine source image with destination image by maximum
  238.    process. The destination image is overwritten.
  239.    */
  240.    CombineImages(InImage1,32,20,256,160,
  241.          InImage2,32,20,Max,0);
  242.  
  243.    DisplayImageInBuf(InImage2,NOVGAINIT,WAITFORKEY);
  244.    if (GenPCXFiles)
  245.       WritePCXFile("p2c11i4f",8,320,200,1,320);
  246.  
  247.    CopyImage(Buffer,InImage2);
  248.  
  249.    /*
  250.    Combine source image with destination image by minimum
  251.    process. The destination image is overwritten.
  252.    */
  253.    CombineImages(InImage1,32,20,256,160,
  254.          InImage2,32,20,Min,0);
  255.  
  256.    DisplayImageInBuf(InImage2,NOVGAINIT,WAITFORKEY);
  257.    if (GenPCXFiles)
  258.       WritePCXFile("p2c11i4g",8,320,200,1,320);
  259.  
  260.    CopyImage(Buffer,InImage2);
  261.    /*
  262.    Return memory taken up by the image buffers
  263.    */
  264.    farfree((BYTE far *) InImage1);
  265.    farfree((BYTE far *) InImage2);
  266.  
  267.    /*
  268.    Image Sequence Five - Determining Image Pixel Intensities
  269.    */
  270.    if (ReadPCXFileToBuf (InFileName8,&InImage1) != NoError)
  271.       exit(1);
  272.    DisplayImageInBuf(InImage1,NOVGAINIT,WAITFORKEY);
  273.    /*
  274.    Set all bytes of InImage2 buffer to a value of 32
  275.    */
  276.    ClearImageArea(Buffer,MINCOLNUM,MINROWNUM,MAXCOLS,MAXROWS,32);
  277.    DisplayImageInBuf(Buffer,NOVGAINIT,WAITFORKEY);
  278.    if (GenPCXFiles)
  279.       WritePCXFile("p2c11i5b",8,320,200,1,320);
  280.  
  281.    /*
  282.    Combine source image with destination image using the Xor
  283.    process. The destination image is overwritten.
  284.    */
  285.    CombineImages(InImage1,MINCOLNUM,MINROWNUM,MAXCOLS,MAXROWS,
  286.          Buffer,MINCOLNUM,MINROWNUM,Xor,0);
  287.  
  288.    /*
  289.    After Xor operation all pixels which had the value of 32 black
  290.    and all other pixels are some other color. We will now use a
  291.    point process to convert black pixels to white and all others
  292.    to black.
  293.    */
  294.    for (Index=0; Index < MAXQUANTLEVELS; Index++)
  295.       LUT[Index] = 0;
  296.  
  297.    LUT[0] = MAXSAMPLEVAL;
  298.  
  299.    PtTransform(Buffer,MINCOLNUM,MINROWNUM,MAXCOLS,MAXROWS,LUT);
  300.    DisplayImageInBuf(Buffer,NOVGAINIT,WAITFORKEY);
  301.    if (GenPCXFiles)
  302.       WritePCXFile("p2c11i5c",8,320,200,1,320);
  303.  
  304.    /*
  305.    Free all allocated memory
  306.    */
  307.    farfree((BYTE far *) InImage1);
  308.    farfree((BYTE far *) Buffer);
  309.  
  310.    restorecrtmode();
  311.    closegraph();
  312. }
  313.