home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / divesrc.zip / diveobj.cpp next >
Text File  |  1996-08-23  |  7KB  |  240 lines

  1. //****************************************************************************
  2. //* DIVE Class for Application use           - D.A.Braun - February 1996     *
  3. //****************************************************************************
  4.  
  5. #include "diveobj.h"
  6. #include <mem.h>
  7. #include <fourcc.h>
  8. #include <stdlib.h>
  9. #include <mmioos2.h>
  10.  
  11. //*****************************************************************************
  12. //* QueryDive function - used to check if the system supports DIVE
  13. int QueryDive ()
  14. {
  15. DIVE_CAPS DiveCaps;
  16. FOURCC    fccFormats[100] = {0};
  17.  
  18. DiveCaps.pFormatData = fccFormats;
  19. DiveCaps.ulFormatLength = 120;
  20. DiveCaps.ulStructLen = sizeof(DIVE_CAPS);
  21.  
  22. if ( DiveQueryCaps ( &DiveCaps, DIVE_BUFFER_SCREEN ))
  23.     return FALSE;
  24. if ( DiveCaps.ulDepth < 8 )
  25.     return FALSE;
  26.  
  27. return TRUE;
  28. }
  29.  
  30. //*************** CONSTRUCTOR : Set up DIVE and our Image buffer **************
  31. //* Loads an image and sets up DIVE with that image
  32. DIVEOBJ::DIVEOBJ (char *fname)
  33. {
  34. ULONG IMGHAND,IMGACT,IMGCOUNT;
  35. unsigned char TgaHeader [18];
  36. RGB2 *sourcepal;
  37. PBYTE pbBuffer;
  38. int BitDepth;
  39.  
  40. DosOpen (fname,        // Open up the image file specified
  41.     &IMGHAND,
  42.     &IMGACT,
  43.     0,
  44.     FILE_NORMAL,
  45.     OPEN_ACTION_OPEN_IF_EXISTS,
  46.     OPEN_SHARE_DENYWRITE | OPEN_ACCESS_READONLY |
  47.     OPEN_FLAGS_SEQUENTIAL,
  48.     0);
  49.  
  50. // Get image information from the targa header - This supports most
  51. // uncompressed targas, it's behavior is not predictable if given the
  52. // wrong type of file.
  53. DosRead (IMGHAND,TgaHeader,18,&IMGCOUNT);
  54.  
  55. Width = TgaHeader[12] + (TgaHeader[13] << 8);
  56. Height = TgaHeader[14] + (TgaHeader[15] << 8);
  57. BitDepth = TgaHeader[16];
  58.  
  59. // Now open up a dive instance with the necessary settings
  60. // Initialize DIVE
  61. DiveOpen (&H_Dive,FALSE,0);
  62.  
  63. // Setup information for the DIVE blitter
  64. D_BlitSetup.ulStructLen = sizeof ( SETUP_BLITTER );
  65. D_BlitSetup.ulSrcWidth = Width;
  66. D_BlitSetup.ulSrcHeight = Height;
  67. D_BlitSetup.ulSrcPosX = 0;
  68. D_BlitSetup.ulSrcPosY = 0;
  69. D_BlitSetup.fInvert = TRUE;
  70. D_BlitSetup.ulDitherType = 0;
  71. D_BlitSetup.fccDstColorFormat = FOURCC_SCRN;
  72.  
  73. // Color depth specific setup information and creation of buffer
  74.  
  75. if (BitDepth == 24) // Color Depth 24, BGR3 format.
  76.     {
  77.     D_BlitSetup.fccSrcColorFormat = FOURCC_BGR3;
  78.     DiveAllocImageBuffer (H_Dive,
  79.         &DH_Buffer,
  80.         FOURCC_BGR3,
  81.         D_BlitSetup.ulSrcWidth,
  82.         D_BlitSetup.ulSrcHeight,
  83.         0, 0);
  84.     }
  85. else if (BitDepth == 16) // Color Depth 16, R565 format.
  86.     {
  87.     D_BlitSetup.fccSrcColorFormat = FOURCC_R565;
  88.     DiveAllocImageBuffer (H_Dive,
  89.         &DH_Buffer,
  90.         FOURCC_R565,
  91.         D_BlitSetup.ulSrcWidth,
  92.         D_BlitSetup.ulSrcHeight,
  93.         0, 0);
  94.     }
  95. else    { // Color Depth 8, Palette format.
  96.     D_BlitSetup.fccSrcColorFormat = FOURCC_LUT8;
  97.     DiveAllocImageBuffer (H_Dive,
  98.         &DH_Buffer,
  99.         FOURCC_LUT8,
  100.         D_BlitSetup.ulSrcWidth,
  101.         D_BlitSetup.ulSrcHeight,
  102.         0, 0);
  103.     }
  104.  
  105. bDepth = BitDepth/8; // Byte Depth is BitDepth/8.
  106.  
  107. // If 8bit/palette mode then read in the image palette
  108. if (bDepth == 1)
  109.     {
  110.     sourcepal = (RGB2*) malloc (256 * sizeof(RGB2));
  111.     for (int cntr = 0;cntr < 256;cntr++)
  112.         {
  113.         DosRead (IMGHAND,&sourcepal[cntr],3,&IMGCOUNT);
  114.         sourcepal[cntr].fcOptions = 0;
  115.         }
  116.     DiveSetSourcePalette (H_Dive,0,256,(char*)sourcepal);
  117.     free (sourcepal);
  118.     }
  119.  
  120. DiveBeginImageBufferAccess    // Open access to the image buffer
  121.     (H_Dive,DH_Buffer,&pbBuffer,&Width,&Height);
  122. DosRead                // Read in the image data
  123.     (IMGHAND,pbBuffer,Width * Height * bDepth,&IMGCOUNT);
  124. DiveEndImageBufferAccess    // Close access to the image buffer
  125.     (H_Dive,DH_Buffer);
  126. DosClose (IMGHAND);        // Close the image file
  127. }
  128.  
  129. //******************* DESTRUCTOR : Close DIVE if it was opened ****************
  130. DIVEOBJ::~DIVEOBJ ()
  131. {
  132. // Free up the image buffer
  133. DiveFreeImageBuffer (H_Dive,DH_Buffer);
  134. // Close DIVE
  135. DiveClose (H_Dive);
  136. }
  137.  
  138. //******************** Handler for DIVE Realize Palette ***********************
  139. //* This tells DIVE that the physical palette may have changed.
  140. void DIVEOBJ::Realize_Palette  ()
  141. { DiveSetDestinationPalette ( H_Dive, 0, 256, 0 ); }
  142.  
  143. //********************** Handler for DIVE VRN Disable *************************
  144. //* This disables the DIVE blitter because the draw region is currently unknown
  145. void DIVEOBJ::VRN_Disable ()
  146. { DiveSetupBlitter ( H_Dive, 0 ); }
  147.  
  148. //********************** Handler for DIVE VRN Enable **************************
  149. //* Much of this function was taken from IBM's demo dive code - I made some
  150. //* changes to allow the image to be scaled to a percentage location in it's
  151. //* parent window - see DIVEOBJ::Location function below.
  152. void DIVEOBJ::VRN_Enable (HWND client,HWND frame)
  153. {
  154. POINTL    pointl;
  155. HPS    hps;
  156. HRGN    hrgn;
  157. RECTL    rcls [50];
  158. RGNRECT    rgnCtl;
  159. SWP    swp;
  160.  
  161. hps = WinGetPS ( client);
  162. hrgn = GpiCreateRegion ( hps, 0L, NULL );
  163. if ( hrgn )
  164.     {
  165.     // NOTE: If mp1 is zero, then this was just a move message.
  166.     // Illustrate the visible region on a WM_VRNENABLE.
  167.  
  168.     WinQueryVisibleRegion ( client, hrgn );
  169.     rgnCtl.ircStart     = 0;
  170.     rgnCtl.crc          = 50;
  171.     rgnCtl.ulDirection  = RECTDIR_LFRT_TOPBOT;
  172.  
  173.     // Get the all ORed rectangles
  174.     if ( GpiQueryRegionRects ( hps, hrgn, NULL, &rgnCtl, rcls) )
  175.         {
  176.         // Now find the window position and size, relative to parent.
  177.         WinQueryWindowPos (client, &swp );
  178.  
  179.         // Convert the point to offset from desktop lower left.
  180.         pointl.x = swp.x;
  181.         pointl.y = swp.y;
  182.         WinMapWindowPoints (frame,
  183.             HWND_DESKTOP, (POINTL*)&pointl, 1 );
  184.  
  185.         // Tell DIVE about the new settings.
  186.  
  187.         D_BlitSetup.ulDstWidth = swp.cx * W;
  188.         D_BlitSetup.ulDstHeight = swp.cy * H;
  189.         D_BlitSetup.lDstPosX = swp.cx * X;
  190.         D_BlitSetup.lDstPosY = swp.cy * Y;
  191.  
  192.         D_BlitSetup.lScreenPosX = pointl.x;
  193.         D_BlitSetup.lScreenPosY = pointl.y;
  194.         D_BlitSetup.ulNumDstRects = rgnCtl.crcReturned;
  195.         D_BlitSetup.pVisDstRects = rcls;
  196.         DiveSetupBlitter (H_Dive, &D_BlitSetup );
  197.         }
  198.     else DiveSetupBlitter (H_Dive, 0 );
  199.  
  200.     GpiDestroyRegion( hps, hrgn );
  201.     }
  202. WinReleasePS( hps );
  203. }
  204.  
  205. //************************ Set Location of DIVE region ************************
  206. //* Sets the location of this dive object to be at a certain place in the
  207. //* window - values are from 0 (left/bottom) to 1 (right/top) - DIVE wants
  208. //* left,bottom,width,height in screen coordinates, but I want to think of
  209. //* things as left,bottom,right,top percentages so I converted it here.
  210. void DIVEOBJ::Location (float xl,float yb,float xr,float yt)
  211. {
  212. X = xl;
  213. Y = yb;
  214. W = xr - xl;
  215. H = yt - yb;
  216. }
  217.  
  218. //************************* Open DIVE Buffer **********************************
  219. //* Opens the dive buffer and returns a pointer into the buffer - don't
  220. //* forget to close the buffer!
  221. char* DIVEOBJ::OpenBuffer ()
  222. {
  223. PBYTE pbBuffer;
  224.  
  225. DiveBeginImageBufferAccess (H_Dive,DH_Buffer,&pbBuffer,&Width,&Height);
  226. return pbBuffer;
  227. }
  228.  
  229. //************************ Close DIVE Buffer **********************************
  230. //* Closes the buffer - simple.
  231. void DIVEOBJ::CloseBuffer ()
  232. { DiveEndImageBufferAccess (H_Dive,DH_Buffer); }
  233.  
  234. //**************************** Blit the image *********************************
  235. //* Blits the image onto the screen - simple.
  236. int DIVEOBJ::Blit ()
  237. { return DiveBlitImage (H_Dive,DH_Buffer,DIVE_BUFFER_SCREEN); }
  238.  
  239. //********************************** END **************************************
  240.