home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / flipscn.zip / flip.c < prev    next >
C/C++ Source or Header  |  1995-10-09  |  4KB  |  171 lines

  1. /* VIEW WITH TAB STOPS = 4 */
  2. #define INCL_DOS
  3. #define INCL_GPI
  4. #define INCL_WIN
  5. #include <os2.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <math.h>
  9. #define  _MEERROR_H_
  10. #include <mmioos2.h>
  11. #include <dive.h>
  12. #include <fourcc.h>
  13.  
  14. int main(int argc, char **argv)
  15. {
  16.     HAB             hab;
  17.     HDIVE            hdive;
  18.     DIVE_CAPS        caps;
  19.     LONG            banklines;
  20.     LONG            lines;
  21.     LONG            banklinesleft;
  22.     LONG            linesize;
  23.     LONG            banknum;
  24.     LONG            half;
  25.     LONG            currline;
  26.     LONG            copyline;
  27.     double            v;
  28.     double            shrink;
  29.     double            step;
  30.     LONG            steps;
  31.     unsigned char    *video;
  32.     unsigned char    *buffer;
  33.     unsigned char    *ptr;
  34.     RECTL            rcl;
  35.  
  36.     /* Number of frames
  37.      */
  38.     if(argc == 1 || (steps = atol(argv[1])) == 0)
  39.         steps = 50;
  40.  
  41.     hab = WinInitialize(0L);
  42.  
  43.     /* Query DIVE capabilities on this system
  44.      */
  45.     memset(&caps, 0, sizeof(caps));
  46.     caps.ulStructLen = sizeof(caps);
  47.     DiveQueryCaps(&caps, DIVE_BUFFER_SCREEN);
  48.  
  49.     if(! caps.fScreenDirect || DiveOpen(&hdive, FALSE, &video) != 0)
  50.         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  51.             "Cannot open DIVE", "Flip", 0, MB_OK | MB_MOVEABLE);
  52.     else
  53.     {
  54.         /* A few useful parameters
  55.          */
  56.         banklines = caps.fBankSwitched ?
  57.                         caps.ulApertureSize / caps.ulScanLineBytes :
  58.                         caps.ulVerticalResolution;
  59.         linesize = caps.ulHorizontalResolution * (caps.ulDepth / 8);
  60.  
  61.         if((buffer = malloc(caps.ulVerticalResolution * linesize)) != 0)
  62.         {
  63.             /* Copy screen content
  64.              */
  65.             rcl.yBottom = 0;
  66.             rcl.yTop = caps.ulVerticalResolution - 1;
  67.             rcl.xLeft = 0;
  68.             rcl.xRight = caps.ulHorizontalResolution - 1;
  69.  
  70.             /* Access screen memory
  71.              */
  72.             if(DiveAcquireFrameBuffer(hdive, &rcl) == 0)
  73.             {
  74.                 DiveSwitchBank(hdive, banknum = 0);
  75.                 ptr = video;
  76.                 banklinesleft = banklines;
  77.                 for(lines = 0; lines < caps.ulVerticalResolution; lines++)
  78.                 {
  79.                     /* The lines in the buffer are numbered
  80.                      * from 0 (top) to caps.ulVerticalResoultion - 1 (bottom)
  81.                      */
  82.                     memcpy(buffer + lines * linesize, ptr, linesize);
  83.  
  84.                     ptr += caps.ulScanLineBytes;
  85.  
  86.                     /* Do a bank switch when all the lines in the
  87.                      * current bank have been accessed.
  88.                      */
  89.                     if(--banklinesleft == 0)
  90.                     {
  91.                         banklinesleft = banklines;
  92.                         ptr = video;
  93.                         DiveSwitchBank(hdive, ++banknum);
  94.                     }
  95.                 }
  96.  
  97.                 /* Flip screen
  98.                  */
  99.                 half = caps.ulVerticalResolution / 2;
  100.                 step = 6.28 / (double)steps;
  101.                 for(v = 0; v < 6.28; v += step)
  102.                 {
  103.                     /* Calculate shrink factor, except for
  104.                      * last step to put everything back in place
  105.                      */
  106.                     shrink = (v + step < 6.28) ? (1. / cos(v)) : 1.;
  107.  
  108.                     DiveSwitchBank(hdive, banknum = 0);
  109.                     ptr = video;
  110.                     banklinesleft = banklines;
  111.                     currline = half;
  112.                     for(lines = 0; lines < caps.ulVerticalResolution; lines++)
  113.                     {
  114.                         /* The line to copy is obtained considering the
  115.                          * lines on the screen numbered from +half (top)
  116.                          * to -half (bottom), line 0 being the center one.
  117.                          * The currline variable does that. Multiplying
  118.                          * the current line by the shrink factor, the source
  119.                          * line is obtained in the same representation.
  120.                          * The subtraction puts it back in the 0 to
  121.                          * caps.ulVerticalResoultion - 1 scale.
  122.                          */
  123.                         copyline = half - ((double)currline * shrink);
  124.  
  125.                         /* If the source line is off screen the target
  126.                          * line is filled with zeros (hoping it's black
  127.                          * or the palette index for black).
  128.                          */
  129.                         if(copyline >= 0 && copyline < caps.ulVerticalResolution)
  130.                             memcpy(ptr, buffer + copyline * linesize, linesize);
  131.                         else
  132.                             memset(ptr, 0, linesize);
  133.                         currline--;
  134.  
  135.                         ptr += caps.ulScanLineBytes;
  136.  
  137.                         /* Do a bank switch when all the lines in the
  138.                          * current bank have been accessed.
  139.                          */
  140.                         if(--banklinesleft == 0)
  141.                         {
  142.                             banklinesleft = banklines;
  143.                             ptr = video;
  144.                             DiveSwitchBank(hdive, ++banknum);
  145.                         }
  146.                     }
  147.                 }
  148.  
  149.                 /* Restore screen memory access
  150.                  */
  151.                 DiveDeacquireFrameBuffer(hdive);
  152.             }
  153.             else
  154.                 WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  155.                     "Cannot acquire frame buffer", "Flip", 0, MB_OK | MB_MOVEABLE);
  156.  
  157.             free(buffer);
  158.         }
  159.         else
  160.             WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  161.                 "Cannot allocate memory for video image", "Flip", 0, MB_OK | MB_MOVEABLE);
  162.  
  163.         DiveClose(hdive);
  164.     }
  165.      
  166.     WinTerminate(hab);
  167.  
  168.     return 0;
  169. }
  170.  
  171.