home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / imdisp79.zip / BUFFER.C < prev    next >
C/C++ Source or Header  |  1993-02-15  |  19KB  |  686 lines

  1. /***  IMDISP module BUFFER.C
  2.  
  3.     BUFFER.C contains all the routines for manipulating images into
  4.     buffers.
  5.     Written by Ron Baalke - 06/15/91.
  6.  
  7. ***/
  8.  
  9. #define __MSC
  10.  
  11. /* * * * INCLUDE files * * * */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <fcntl.h>
  16. #include <io.h>
  17. #include <sys\types.h>
  18. #include <sys\stat.h>
  19. #include <math.h>
  20. #include "imdef.h"
  21. #include "imdisp.h"
  22. #include "dispio.h"
  23. #include "refresh.h"
  24. #include "textutil.h"
  25. #include "display.h"
  26. #include "keywutil.h"
  27. #include "mem.h"
  28.  
  29. /* * * * External functions * * * */
  30.  
  31. void DoCopy(void);
  32. void DoMerge(void);
  33. int AllocBuffer(int, int, int, int);
  34. int PutBuffer (int, unsigned char *, int, int, int);
  35. int GetBuffer (int, unsigned char *, int, int, int);
  36. void DeallocBuffer(int);
  37.  
  38. /* * * * Global Variables * * * */
  39.  
  40. Buffer Buffers[MAXBUFFERS];
  41. char   BufferFileName[40] = "C:\\";
  42. /*
  43. extern unsigned char buffer1[MAXDISPNS];
  44. extern unsigned char buffer2[MAXDISPNS];
  45. extern unsigned char outbuffer[MAXDISPNS];
  46. */
  47. extern unsigned char *buffer1;
  48. extern unsigned char *buffer2;
  49. extern unsigned char *outbuffer;
  50.  
  51. /*************************************************************************/
  52. /* DoCopy                                                                */
  53. /*                                                                       */
  54. /* Written by Ron Baalke - 06/16/91                                      */
  55. /*                                                                       */
  56. /* This routine will copy either an image, screen or another buffer      */
  57. /* to the specified buffer.                                              */
  58. /*************************************************************************/
  59.  
  60. void DoCopy(void)
  61. {
  62.    int i,j;
  63.    int source, destination;
  64.    int index;
  65.    char string1[40];
  66.    char string2[40];
  67.    char string3[40];
  68.    int  flag, toflag, fileflag;
  69.    unsigned char buffer[MAXDISPNS];
  70.    int  save;
  71.  
  72.    source = IMAGE;
  73.    destination = NOWHERE;
  74.  
  75.    strcat(CommandString," ");
  76.    GetKeywordString (CommandString, "COP", " ",string1, &flag);
  77.    GetKeywordString (CommandString, "TO" , " ",string2, &toflag);
  78.    GetKeywordString (CommandString, "FIL", " ",string3, &fileflag);
  79.    if (flag <= 0)
  80.    {
  81.       StatusLine(0,"Incomplete command");
  82.       return;
  83.    }
  84.  
  85.    /* Perform parameters logic */
  86.  
  87.    if (fileflag != -1)
  88.       fileflag = VIRTUAL_FILE;
  89.    else
  90.       fileflag = 0;
  91.  
  92.    if ((toflag)               &&
  93.        (strlen(string2) == 1) &&
  94.        (string2[0] >= 'A')    &&
  95.        (string2[0] <= 'Z'))
  96.       destination = string2[0] - 'A';
  97.  
  98.    if (strnicmp (string1, "SCR", 3) == 0)
  99.    {
  100.       source = SCREEN;
  101.       if (destination == NOWHERE)
  102.       {
  103.          strcat(string1," ");
  104.          GetKeywordString (CommandString, string1, " ",string2, &flag);
  105.          if ((flag)                 &&
  106.              (strlen(string2) == 1) &&
  107.              (string2[0] >= 'A')    &&
  108.              (string2[0] <= 'Z'))
  109.             destination = string2[0] - 'A';
  110.       }
  111.    }
  112.  
  113.    if ((strlen(string1) == 1) &&
  114.        (string1[0] >= 'A')    &&
  115.        (string1[0] <= 'Z'))
  116.    {
  117.       if (destination != NOWHERE)
  118.          source = string1[0] - 'A';
  119.       else
  120.       {
  121.          strcat(string1," ");
  122.          GetKeywordString (CommandString, string1, " ",string2, &flag);
  123.          if ((flag)                 &&
  124.              (strlen(string2) == 1) &&
  125.              (string2[0] >= 'A')    &&
  126.              (string2[0] <= 'Z'))
  127.          {
  128.             source      = string1[0] - 'A';
  129.             destination = string2[0] - 'A';
  130.          }
  131.          else
  132.             destination = string1[0] - 'A';
  133.       }
  134.    }
  135.  
  136.    /* Do error checks */
  137.  
  138.    if (destination == NOWHERE)
  139.    {
  140.       StatusLine(0,"Illegal destination");
  141.       return;
  142.    }
  143.  
  144.    if (source == destination)
  145.    {
  146.       StatusLine(0,"Source and destination can't be the same");
  147.       return;
  148.    }
  149.  
  150.    if ((source != SCREEN) &&
  151.        (source != IMAGE)  &&
  152.        (Buffers[source].location == NOWHERE))
  153.    {
  154.       strcpy(string1,"A");
  155.       string1[0] += source;
  156.       sprintf(buffer,"Buffer %s is empty",string1);
  157.       StatusLine(0,buffer);
  158.       return;
  159.    }
  160.  
  161.    /* Allocate destination buffer */
  162.  
  163.    if (Buffers[destination].location != NOWHERE)
  164.       DeallocBuffer(destination);
  165.  
  166.    if (source == IMAGE)
  167.       flag = AllocBuffer(destination,fileflag,Image_Length,Image_Height);
  168.    else if (source == SCREEN)
  169.       flag = AllocBuffer(destination,fileflag,dispns,dispnl);
  170.    else
  171.       flag = AllocBuffer(destination,fileflag,Buffers[source].dispns,
  172.                Buffers[source].dispnl);
  173.  
  174.  
  175.    if (Buffers[destination].location == NOWHERE)
  176.    {
  177.       StatusLine(0,"Not enough memory");
  178.       return;
  179.    }
  180.  
  181.    /* Do the actual copy */
  182.  
  183.    if (source == SCREEN)
  184.    {
  185.       /* Refresh the bottom of the screen to cover up the command prompt */
  186.  
  187.       if (RefreshLines > 0)
  188.       {
  189.          for (i = dispnl-2*TextHeight-15; i<=dispnl; i++)
  190.          {
  191.             GetRefresh(buffer,i,1,dispns);
  192.             DisplayLine(buffer,i,1,dispns);
  193.          }
  194.       }
  195.       save = RefreshLines;          /* temporarily disable GetLine() from using*/
  196.       RefreshLines = 0;             /* the refresh buffer */
  197.  
  198.       for (i=1; i<=dispnl; i++)
  199.       {
  200.          GetLine(buffer,i,1,dispns);
  201.          PutBuffer(destination,buffer,i,1,dispns);
  202.       }
  203.  
  204.       RefreshLines = save;          /* Restore */
  205.    }
  206.    else if (source == IMAGE)
  207.    {
  208.       for (i=Image_Line,j=1; i<Image_Line+Image_Height; i++,j++)
  209.       {
  210.          GetLine(buffer,i,Image_Sample,Image_Length);
  211.          PutBuffer(destination,buffer,j,1,Image_Length);
  212.       }
  213.    }
  214.    else
  215.    {
  216.       for (i=1; i<=dispnl; i++)
  217.       {
  218.          GetBuffer(source,buffer,i,1,dispns);
  219.          PutBuffer(destination,buffer,i,1,dispns);
  220.       }
  221.    }
  222.  
  223.  
  224. }
  225.  
  226. /*************************************************************************/
  227. /* DoMerge                                                               */
  228. /*                                                                       */
  229. /* Written by Ron Baalke - 06/16/91                                      */
  230. /*                                                                       */
  231. /* This routine will merge buffers together or with the screen and       */
  232. /* output the result to either another buffer or the screen.             */
  233. /*************************************************************************/
  234.  
  235.  
  236. void DoMerge(void)
  237. {
  238.    int i,j;
  239.    int source1, source2, destination;
  240.    int index;
  241.    char string1[40];
  242.    char string2[40];
  243.    char string3[40];
  244.    char string4[40];
  245.    int  flag, toflag, fileflag, withflag;
  246.    float scale1, scale2;
  247.    int  length, length1, length2;
  248.    int  height, height1, height2;
  249.    int  save;
  250.    int  iscale1, iscale2;
  251.    int  temp;
  252.  
  253.    source1 = NOWHERE;
  254.    source2 = IMAGE;
  255.    destination = SCREEN;
  256.    scale1 = 0.0;
  257.    scale2 = 0.0;
  258.  
  259.    strcat(CommandString," ");
  260.    GetKeywordString (CommandString, "MER", " ",string1, &flag);
  261.    GetKeywordString (CommandString, "TO" , " ",string2, &toflag);
  262.    GetKeywordString (CommandString, "FIL", " ",string3, &fileflag);
  263.    GetKeywordString (CommandString, "WIT", " ",string4, &withflag);
  264.    if (flag <= 0)
  265.    {
  266.       StatusLine(0,"Incomplete command");
  267.       return;
  268.    }
  269.  
  270.    /* Perform parameter logic */
  271.  
  272.    if (fileflag != -1)
  273.       fileflag = VIRTUAL_FILE;
  274.    else
  275.       fileflag = 0;
  276.  
  277.    if ((toflag)               &&
  278.        (strlen(string2) == 1) &&
  279.        (string2[0] >= 'A')    &&
  280.        (string2[0] <= 'Z'))
  281.       destination = string2[0] - 'A';
  282.  
  283.    if (withflag)
  284.    {
  285.       if ((strlen(string4) == 1) &&
  286.           (string4[0] >= 'A')    &&
  287.           (string4[0] <= 'Z'))
  288.       {
  289.          source2 = string4[0] - 'A';
  290.          source1 = IMAGE;
  291.       }
  292.       else if (strnicmp (string4, "SCR", 3) == 0)
  293.          source2 = SCREEN;
  294.    }
  295.  
  296.    if (flag)
  297.    {
  298.       if ((strlen(string1) == 1) &&
  299.           (string1[0] >= 'A')    &&
  300.           (string1[0] <= 'Z'))
  301.       {
  302.          source1 = string1[0] - 'A';
  303.       }
  304.       else if (strnicmp (string1, "SCR", 3) == 0)
  305.          source1 = SCREEN;
  306.    }
  307.  
  308.    /* Do some error checking */
  309.  
  310.    if (source1 == source2)
  311.    {
  312.       StatusLine(0,"Sources can't be the same");
  313.       return;
  314.    }
  315.  
  316.    if ((source1 == IMAGE && source2 == SCREEN) ||
  317.        (source2 == IMAGE && source1 == SCREEN))
  318.    {
  319.       StatusLine(0,"Sources can't be the same");
  320.       return;
  321.    }
  322.  
  323.    if (source1 == NOWHERE || source2 == NOWHERE)
  324.    {
  325.       StatusLine(0,"Source not specified");
  326.       return;
  327.    }
  328.  
  329.    if ((source1 >= 0) &&
  330.        (Buffers[source1].location == NOWHERE))
  331.    {
  332.       strcpy(string1,"A");
  333.       string1[0] += source1;
  334.       sprintf(buffer1,"Buffer %s is empty",string1);
  335.       StatusLine(0,buffer1);
  336.       return;
  337.    }
  338.    if ((source2 >= 0) &&
  339.        (Buffers[source2].location == NOWHERE))
  340.    {
  341.       strcpy(string1,"A");
  342.       string1[0] += source2;
  343.       sprintf(buffer1,"Buffer %s is empty",string1);
  344.       StatusLine(0,buffer1);
  345.       return;
  346.    }
  347.  
  348.    /* Retrieve scaling paramters */
  349.  
  350.    if (source1 == SCREEN)
  351.    {
  352.       GetKeywordString (CommandString, "SCR", " ",string2, &flag);
  353.       scale1 = atof(string2);
  354.    }
  355.    else if (source1 >= 0)
  356.    {
  357.       strcpy(string1,"A");
  358.       string1[0] += source1;
  359.       strcat(string1," ");
  360.       GetKeywordString (CommandString, string1, " ", string2, &flag);
  361.       scale1 = atof(string2);
  362.    }
  363.  
  364.    if (source2 == SCREEN)
  365.    {
  366.       GetKeywordString (CommandString, "SCR", " ",string2, &flag);
  367.       scale2 = atof(string2);
  368.    }
  369.    else if (source2 >= 0)
  370.    {
  371.       strcpy(string1,"A");
  372.       string1[0] += source2;
  373.       strcat(string1," ");
  374.       GetKeywordString (CommandString, string1, " ", string2, &flag);
  375.       scale2 = atof(string2);
  376.    }
  377.  
  378.    iscale1 = 100.0 * scale1 + 0.5;
  379.    iscale2 = 100.0 * scale2 + 0.5;
  380.  
  381.    if (iscale1 == 0 && iscale2 == 0)
  382.    {
  383.       iscale1 = 50;
  384.       iscale2 = 50;
  385.    }
  386.  
  387.    if (iscale1 == 0 && iscale2 > 0 && iscale2 < 100)
  388.       iscale1 = 100 - iscale2;
  389.    else if (iscale2 == 0 && iscale1 > 0 && iscale1 < 100)
  390.       iscale2 = 100 - iscale1;
  391.    else if (iscale1 == 0)
  392.       iscale1 == 100;
  393.    else if (iscale2 == 0)
  394.       iscale2 == 100;
  395.  
  396.  
  397.    if (source1 == IMAGE)
  398.    {
  399.       length1 = Image_Length;
  400.       height1 = Image_Height;
  401.    }
  402.    else if (source1 == SCREEN)
  403.    {
  404.       length1 = dispns;
  405.       height1 = dispnl;
  406.    }
  407.    else
  408.    {
  409.       length1 = Buffers[source1].dispns;
  410.       height1 = Buffers[source1].dispnl;
  411.    }
  412.  
  413.    if (source2 == IMAGE)
  414.    {
  415.       length2 = Image_Length;
  416.       height2 = Image_Height;
  417.    }
  418.    else if (source2 == SCREEN)
  419.    {
  420.       length2 = dispns;
  421.       height2 = dispnl;
  422.    }
  423.    else
  424.    {
  425.       length2 = Buffers[source2].dispns;
  426.       height2 = Buffers[source2].dispnl;
  427.    }
  428.  
  429.    length = length1;
  430.    if (length2 > length) length = length2;
  431.    height = height1;
  432.    if (height2 > height) height = height2;
  433.  
  434.    /* Allocate buffer if applicable */
  435.  
  436.    if (destination >= 0)
  437.    {
  438.       if (Buffers[destination].location != NOWHERE)
  439.          DeallocBuffer(destination);
  440.  
  441.       flag = AllocBuffer(destination,fileflag,length,height);
  442.  
  443.       if (Buffers[destination].location == NOWHERE)
  444.       {
  445.          StatusLine(0,"Not enough memory");
  446.          return;
  447.       }
  448.    }
  449.  
  450.    if ((source1 == SCREEN) || (source2 == SCREEN))
  451.    {
  452.       /* Refresh the bottom of the screen to cover up the command prompt */
  453.  
  454.       if (RefreshLines > 0)
  455.       {
  456.          for (i = dispnl-2*TextHeight-15; i<=dispnl; i++)
  457.          {
  458.             GetRefresh(buffer1,i,1,dispns);
  459.             DisplayLine(buffer1,i,1,dispns);
  460.          }
  461.       }
  462.    }
  463.  
  464.    /* Perform the merge here */
  465.  
  466.    for (i=1; i<=height; i++)
  467.    {
  468.       save = RefreshLines;          /* temporarily disable GetLine() from using*/
  469.       RefreshLines = 0;             /* the refresh buffer */
  470.       if (i <= height1)
  471.       {
  472.          if (source1 == SCREEN)
  473.             GetLine(buffer1,i,1,dispns);
  474.          else if (source1 == IMAGE)
  475.             GetLine(buffer1,Image_Line+i-1,Image_Sample,Image_Length);
  476.          else
  477.             GetBuffer(source1,buffer1,i,1,length1);
  478.       }
  479.       else
  480.           memset( buffer1, 0, length);
  481.  
  482.       if (i <= height2)
  483.       {
  484.          if (source2 == SCREEN)
  485.             GetLine(buffer2,i,1,dispns);
  486.          else if (source2 == IMAGE)
  487.             GetLine(buffer2,Image_Line+i-1,Image_Sample,Image_Length);
  488.          else
  489.             GetBuffer(source2,buffer2,i,1,length2);
  490.       }
  491.       else
  492.           memset( buffer2, 0, length);
  493.  
  494.       for (j=0; j<length; j++)
  495.       {
  496.          temp = 0;
  497.          if (buffer2[j] == 0)
  498.             temp = buffer1[j];
  499.          else if (buffer1[j] == 0)
  500.             temp = buffer2[j];
  501.          else
  502.          {
  503.             if ((j < length1) && (buffer2[j] > 0))
  504.                temp = iscale1 * buffer1[j];
  505.             if (j < length2)
  506.                temp += iscale2 * buffer2[j];
  507.             temp = temp/100;
  508.          }
  509.          if (temp < 0) temp = 0;
  510.          if (temp >= numDN) temp = numDN-1;
  511.          outbuffer[j] = temp;
  512.       }
  513.       RefreshLines = save;
  514.  
  515.       if (destination == SCREEN)
  516.       {
  517.          DisplayLine(outbuffer,i,1,length);
  518.          if (RefreshLines > 0)
  519.             PutRefresh(outbuffer,i,1,length);
  520.       }
  521.       else
  522.          PutBuffer(destination,outbuffer,i,1,length);
  523.  
  524.    }
  525.  
  526. }
  527.  
  528. int AllocBuffer(int index, int type, int length, int height)
  529. /***  Allocbuffer allocates memory for the buffer in either extended
  530.       memory or it creates a file and initializes it (if
  531.       it's newly created) to all NULLs.
  532.  
  533. ***/
  534. {
  535.     int i, n_bytes;
  536.     unsigned long lsize;
  537.     int size;
  538.     long offset;
  539.     char letter[2];
  540.  
  541.     Buffers[index].handle = 0;
  542.     Buffers[index].dispns = length;
  543.     Buffers[index].dispnl = height;
  544.     Buffers[index].location = NOWHERE;
  545.  
  546.     if (type == VIRTUAL_FILE)
  547.     {
  548.        strcpy(letter,"A");
  549.        letter[0] += index;
  550.        strcpy(Buffers[index].file,BufferFileName);
  551.        strcat(Buffers[index].file,"___BUF");
  552.        strcat(Buffers[index].file,letter);
  553.        strcat(Buffers[index].file,".TMP");
  554.        Buffers[index].handle = open( Buffers[index].file, O_CREAT|O_RDWR|O_BINARY, S_IREAD|S_IWRITE );
  555.        if (Buffers[index].handle == -1)
  556.        {
  557.        /* File must already exist - reopen w/o initialize */
  558.           Buffers[index].handle = open( Buffers[index].file, O_RDWR|O_BINARY, S_IREAD|S_IWRITE );
  559.           if (Buffers[index].handle == -1) /*  If open fails, just set #lines to 0 */
  560.              Buffers[index].dispnl = 0;
  561.        }
  562.        else
  563.        {
  564.        /* Initialize the file to all 0's, one line at a time */
  565.           memset( StageRefresh, 0, Buffers[index].dispns);
  566.           n_bytes = Buffers[index].dispns;
  567.           i = 0;
  568.           while ( (n_bytes == Buffers[index].dispns) &&
  569.                   (i < Buffers[index].dispnl) )
  570.           {
  571.               n_bytes = write( Buffers[index].handle, StageRefresh,
  572.                                Buffers[index].dispns);
  573.               if (n_bytes == Buffers[index].dispns)
  574.                   i++;
  575.           }
  576.           if (i == Buffers[index].dispnl)
  577.              Buffers[index].location = VIRTUAL_FILE;
  578.           else
  579.           {
  580.              close(Buffers[index].handle);
  581.              remove(Buffers[index].file);
  582.           }
  583.        }
  584.        return(0);
  585.     }
  586.  
  587.     /* Attempt extended memory */
  588.  
  589.     if (Buffers[index].location == NOWHERE)
  590.     {
  591.        lsize = (long)Buffers[index].dispns * (long)Buffers[index].dispnl;
  592.        if (Memory_Alloc(lsize,&Buffers[index].handle,&Buffers[index].location) == 0)
  593.        {
  594.           memset( StageRefresh, 0, Buffers[index].dispns);
  595.           size = Buffers[index].dispns;
  596.  
  597.           for (i=0; i<Buffers[index].dispnl; i++)
  598.           {
  599.              offset = (long) i * (long) Buffers[index].dispns;
  600.              Memory_PutLine(Buffers[index].handle, Buffers[index].location,
  601.                             StageRefresh, offset,size);
  602.           }
  603.        }
  604.     }
  605.  
  606.     return(0);
  607. }
  608.  
  609. int PutBuffer (int index, unsigned char * buffer, int line, int ss, int ns)
  610. /***  PutBuffer stores a line of pixels into the appropriate buffer.
  611.       Parameter   type       description
  612.         index    interger    The buffer number
  613.         buffer   char ptr    The array of pixel values
  614.         line     integer     The line coordinate of the first pixel
  615.         sample   integer     The sample coordinate of the first pixel
  616.         ns       integer     The number of pixels to store
  617. ***/
  618. {
  619.     int    n_bytes;
  620.     long   offset;
  621.     long   current;
  622.  
  623.     offset = (long)(line-1) * Buffers[index].dispns + ss - 1;
  624.  
  625.     if (Buffers[index].location != VIRTUAL_FILE)
  626.     {
  627.        Memory_PutLine(Buffers[index].handle, Buffers[index].location,
  628.                    buffer, offset, ns);
  629.        n_bytes = ns;
  630.     }
  631.     else
  632.     {
  633.        current = lseek( Buffers[index].handle, offset, SEEK_SET);
  634.        n_bytes = write( Buffers[index].handle, buffer, ns);
  635.     }
  636.  
  637.     return( n_bytes );
  638. }
  639.  
  640. int GetBuffer (int index, unsigned char * buffer, int line, int ss, int ns)
  641. /***  GetBuffer reads a line of pixels from the appropriate buffer.
  642.       Parameter   type       description
  643.         index    integer     The buffer number
  644.         buffer   char ptr    The receiving array of pixel values
  645.         line     integer     The line coordinate of the first pixel
  646.         sample   integer     The sample coordinate of the first pixel
  647.         ns       integer     The number of pixels to read
  648. ***/
  649. {
  650.     int    n_bytes;
  651.     long   offset;
  652.     long   current;
  653.  
  654.     offset = (long)(line-1) * Buffers[index].dispns + ss - 1;
  655.  
  656.     if (Buffers[index].location != VIRTUAL_FILE)
  657.     {
  658.        Memory_GetLine(Buffers[index].handle, Buffers[index].location,
  659.                       buffer, offset, ns);
  660.        n_bytes = ns;
  661.     }
  662.     else
  663.     {
  664.        current = lseek( Buffers[index].handle, offset, SEEK_SET);
  665.        n_bytes = read( Buffers[index].handle, buffer, ns);
  666.     }
  667.  
  668.     return( n_bytes );
  669. }
  670.  
  671. void DeallocBuffer(int index)
  672. /***  DeallocBuffer releases the buffer back to DOS.
  673. ***/
  674. {
  675.     if (Buffers[index].location != VIRTUAL_FILE)   /* Ron Baalke - 05/91 */
  676.        Memory_Free(Buffers[index].handle, Buffers[index].location);
  677.     else
  678.     {
  679.        close(Buffers[index].handle);
  680.        remove(Buffers[index].file);
  681.     }
  682.  
  683.     Buffers[index].location = NOWHERE;
  684. }
  685.  
  686.