home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games 1996 October / Amiga_Games_Extra_CD_10-96.bin / userbox / publicdomain / grabkey / source / gk_graphx.c < prev    next >
C/C++ Source or Header  |  1996-07-15  |  6KB  |  269 lines

  1. /*
  2. **    GrabKEY
  3. **
  4. **        © 1996 by Timo C. Nentwig
  5. **        All Rights Reserved !
  6. **
  7. **        Tcn@techbase.in-berlin.de
  8. **
  9. **
  10. */
  11.  
  12. /// #include
  13.  
  14. #include "gk_GST.h"
  15. #include "gk_Protos.h"
  16.  
  17. ///
  18. /// proto
  19.  
  20. static BOOL    GetCoords    (const ULONG What, struct Window *Window, struct Screen *Screen);
  21. proto  BOOL    GetImage     (Object **Image, const ULONG What);
  22.  
  23. ///
  24.  
  25. static struct
  26. {
  27.  
  28.     LONG    Left;
  29.     LONG    Top;
  30.     LONG    Width;
  31.     LONG    Height;
  32.  
  33. } GrabCoords;
  34.  
  35. /// GetImage ()
  36.  
  37.     /*
  38.      *    FUNCTION    Create a DT Object * referencing
  39.      *                to a window or screen.
  40.      *
  41.      *    NOTE        Values fur <What> in gk_GST.h.
  42.      *
  43.      *    EXAMPLE     GetImage (&Image, WINDOW);
  44.      *
  45.      */
  46.  
  47.  
  48. BOOL
  49. GetImage (Object **Image, const ULONG What)
  50. {
  51.  
  52.     BOOL               Result = FALSE;
  53.     ULONG              ILock  = LockIBase (NULL);
  54.  
  55.     struct   Window   *Window = IntuitionBase -> ActiveWindow;
  56.     struct   Screen   *Screen = IntuitionBase -> ActiveScreen;
  57.  
  58.     if (Window && Screen)
  59.     {
  60.  
  61.             // Get coordinates to grab
  62.  
  63.         if (GetCoords (What, Window, Screen))
  64.         {
  65.  
  66.             struct    BitMap   *BitMap;
  67.             LONG                Depth;
  68.  
  69.             Depth = GetBitMapAttr (Screen -> RastPort . BitMap, BMA_DEPTH);
  70.  
  71.                 // Alloc BitMap for our image
  72.  
  73.             if (BitMap = AllocBitMap (GrabCoords . Width, GrabCoords . Height, Depth, NULL, Screen -> RastPort . BitMap))
  74.             {
  75.  
  76.                 struct    RastPort __aligned    RPort;
  77.  
  78.                 InitRastPort (&RPort);
  79.  
  80.                 RPort . BitMap = BitMap;
  81.  
  82.                 if (What == WINDOW)
  83.                 {
  84.  
  85.                     ClipBlit (Window -> RPort, 0, 0, &RPort, 0, 0, GrabCoords . Width, GrabCoords . Height, 0xC0);
  86.  
  87.                 }
  88.                 else // if (What == SCREEN || What == RANGE)
  89.                 {
  90.  
  91.                     ClipBlit (&Screen -> RastPort, 0, 0, &RPort, 0, 0, GrabCoords . Width, GrabCoords . Height, 0xC0);
  92.  
  93.                 }
  94.  
  95.                     // Wait till blitted
  96.  
  97.                 WaitBlit();
  98.  
  99.                 {
  100.  
  101.                     LONG    NumColors = Screen -> ViewPort . ColorMap -> Count;
  102.                     LONG    ModeID    = BestModeID (BIDTAG_NominalWidth,     GrabCoords . Width,
  103.                                                     BIDTAG_NominalHeight,    GrabCoords . Height,
  104.                                                     BIDTAG_Depth,            Depth,
  105.                                                     TAG_END);
  106.  
  107.                     if (*Image = NewDTObject ("GrabImage", DTA_SourceType,    DTST_RAM,
  108.                                                            DTA_GroupID,       GID_PICTURE,
  109.                                                            PDTA_NumColors,    NumColors,
  110.                                                            PDTA_BitMap,       BitMap,
  111.                                                            PDTA_ModeID,       ModeID,
  112.                                                            TAG_END))
  113.                                                            {
  114.  
  115.                                                                struct    ColorRegister    *ColorMap;
  116.                                                                struct    BitMapHeader     *BitMapHeader;
  117.                                                                ULONG                      *Colors;
  118.  
  119.                                                                if (GetDTAttrs (*Image, PDTA_BitMapHeader,     &BitMapHeader,
  120.                                                                                        PDTA_ColorRegisters,   &ColorMap,
  121.                                                                                        PDTA_CRegs,            &Colors,
  122.                                                                                        TAG_END) == 3)
  123.                                                                                        {
  124.  
  125.                                                                                            LONG    i;
  126.  
  127.                                                                                            BitMapHeader -> bmh_Left       = GrabCoords . Left;
  128.                                                                                            BitMapHeader -> bmh_Top        = GrabCoords . Top;
  129.                                                                                            BitMapHeader -> bmh_Width      = GrabCoords . Width;
  130.                                                                                            BitMapHeader -> bmh_Height     = GrabCoords . Height;
  131.                                                                                            BitMapHeader -> bmh_PageWidth  = GrabCoords . Width;
  132.                                                                                            BitMapHeader -> bmh_PageHeight = GrabCoords . Height;
  133.                                                                                            BitMapHeader -> bmh_Depth      = Depth;
  134.  
  135.                                                                                            GetRGB32 (Screen -> ViewPort . ColorMap, 0, NumColors, Colors);
  136.  
  137.                                                                                            for (i = 0; i < NumColors; i++)
  138.                                                                                            {
  139.  
  140.                                                                                                ColorMap[i] . red   = (UBYTE) (Colors [i * 3 + 0] >> 24);
  141.                                                                                                ColorMap[i] . green = (UBYTE) (Colors [i * 3 + 1] >> 24);
  142.                                                                                                ColorMap[i] . blue  = (UBYTE) (Colors [i * 3 + 2] >> 24);
  143.  
  144.                                                                                            }
  145.  
  146.                                                                                            Result = TRUE;
  147.  
  148.                                                                                        }
  149.                                                                                        else
  150.                                                                                        {
  151.  
  152.                                                                                            DisposeDTObject(*Image);
  153.  
  154.                                                                                            *Image = NULL;
  155.  
  156.                                                                                        }
  157.  
  158.                                                            }
  159.                                                            else // "else" is VERY important !!
  160.                                                            {
  161.  
  162.                                                                    // Failed, free our BitMap
  163.  
  164.                                                                FreeBitMap (BitMap);
  165.  
  166.                                                            }
  167.  
  168.                 }
  169.  
  170.             }
  171.  
  172.         }
  173.  
  174.     }
  175.  
  176.         // Unlock IntuitionBase
  177.  
  178.     UnlockIBase (ILock);
  179.  
  180.         // Success ?
  181.  
  182.     return (Result);
  183.  
  184. }
  185.  
  186. ///
  187. /// GetCoords ()
  188.  
  189.     /*
  190.      *    FUNCTION    Get coordinates to grab
  191.      *                and write them to GrabCoords.
  192.      *
  193.      *    NOTE        MUST be called in state of LockIBase() !
  194.      *
  195.      *    EXAMPLE     GetCoords (RANGE);
  196.      *
  197.      */
  198.  
  199. static BOOL
  200. GetCoords (const ULONG What, struct Window *Window, struct Screen *Screen)
  201. {
  202.  
  203.     BOOL    Result = FALSE;
  204.  
  205.     if (What == WINDOW)
  206.     {
  207.  
  208.             // Get coordinates
  209.  
  210.         GrabCoords . Left   = Window -> LeftEdge;
  211.         GrabCoords . Top    = Window -> TopEdge;
  212.         GrabCoords . Width  = Window -> Width;
  213.         GrabCoords . Height = Window -> Height;
  214.  
  215.         if (Window -> Flags & WFLG_SIMPLE_REFRESH)
  216.         {
  217.  
  218.             struct    Rectangle    Visible;
  219.  
  220.             GetRPAttrs (Window -> RPort, RPTAG_DrawBounds,   &Visible,
  221.                                          TAG_END);
  222.  
  223.             if (Visible . MaxX < Visible . MinX)
  224.             {
  225.  
  226.                 return (FALSE);
  227.  
  228.             }
  229.             else
  230.             {
  231.  
  232.                 GrabCoords . Width  = Visible . MaxX - Visible . MinX + 1;
  233.                 GrabCoords . Height = Visible . MaxY - Visible . MinY + 1;
  234.  
  235.                 if (GrabCoords . Width < Window -> Width || GrabCoords . Height < Window -> Height)
  236.                 {
  237.  
  238.                     return (FALSE);
  239.  
  240.                 }
  241.  
  242.             }
  243.  
  244.         }
  245.  
  246.         Result = TRUE;
  247.  
  248.     }
  249.     else // if (What == SCREEN)
  250.     {
  251.  
  252.             // Get coordinates
  253.  
  254.         GrabCoords . Left   = Screen -> LeftEdge;
  255.         GrabCoords . Top    = Screen -> TopEdge;
  256.         GrabCoords . Width  = Screen -> Width;
  257.         GrabCoords . Height = Screen -> Height;
  258.  
  259.         Result = TRUE;
  260.  
  261.     }
  262.  
  263.     return (Result);
  264.  
  265. }
  266.  
  267. ///
  268.  
  269.