/* $Id: wshot.c,v 1.2 1995/10/29 09:41:56 aris Exp aris $
*/
#include <intuition/IntuitionBase.h>
#include <graphics/gfxbase.h>
#include <datatypes/pictureclass.h>
#include <exec/memory.h>
#include <dos/dos.h>

#include <clib/datatypes_protos.h>
#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>
#include <clib/alib_protos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/macros.h>

extern struct IntuitionBase *IntuitionBase;
extern struct Library *DataTypesBase;

char *MYLIBNAME="datatypes.library";

VOID
SavePicture(Object *Picture,const STRPTR FileName)
{
        BPTR FileHandle;

        if(FileHandle = Open(FileName,MODE_NEWFILE))
        {
                if(DoMethod(Picture,DTM_WRITE,NULL,FileHandle,DTWM_IFF,NULL))
                        Close(FileHandle);
                else
                {
                        Close(FileHandle);

                        DeleteFile(FileName);
                }
        }

        DisposeDTObject(Picture);
}

Object *
GetPicture(BOOL scr)
{
        struct RastPort *RPort;

        if(RPort = (struct RastPort *)AllocVec(sizeof(struct RastPort),MEMF_ANY))
        {
                struct BitMap   *BitMap;
                ULONG                    IntuiLock;
                struct Window   *Window;
                LONG                     Left,Top,
                                         Width,Height,
                                         PageWidth,PageHeight,
                                         Depth;
                BOOL                     Locked = TRUE;
                struct Screen *Screen;

                InitRastPort(RPort);

                IntuiLock = LockIBase(NULL);

                if(!scr) {

                    Window          = IntuitionBase -> ActiveWindow;

                    Left            = Window -> LeftEdge;
                    Top             = Window -> TopEdge;
                    Width           = Window -> Width;
                    Height          = Window -> Height;
                    PageWidth       = Window -> WScreen -> Width;
                    PageHeight      = Window -> WScreen -> Height;
                    Depth           = GetBitMapAttr(Window -> RPort -> BitMap,BMA_DEPTH);
                    }
                else {

                    Screen          = IntuitionBase -> ActiveScreen;

                    Left            = Screen -> LeftEdge;
                    Top             = Screen -> TopEdge ;
                    Width           = Screen -> Width;
                    Height          = Screen -> Height;
                    PageWidth       = Width;
                    PageHeight      = Height;
                    Depth           = GetBitMapAttr(Screen -> RastPort . BitMap,BMA_DEPTH);
                    }

                if(BitMap = AllocBitMap(Width,Height,Depth,BMF_CLEAR,
                        ((scr)?(Screen -> RastPort . BitMap):(Window -> RPort -> BitMap))))
                {
                        ULONG   *ColourTable,
                                         ModeID;
                        LONG     NumColours;

                        NumColours = ((scr)?(Screen -> ViewPort . ColorMap -> Count):(Window -> WScreen -> ViewPort . ColorMap -> Count));

                        ModeID = GetVPModeID(((scr)?(&Screen -> ViewPort):(&Window -> WScreen -> ViewPort)));

                        RPort -> BitMap = BitMap;

                        ClipBlit(((scr)?(&Screen -> RastPort):(Window -> RPort)),0,0,RPort,0,0,Width,Height,0xC0);

                        WaitBlit();

                        if(ColourTable = (ULONG *)AllocVec(sizeof(ULONG) * 3 * NumColours,
                                MEMF_ANY))
                        {
                                Object *Picture;

                                GetRGB32(((scr)?(Screen -> ViewPort . ColorMap):(Window -> WScreen -> ViewPort . ColorMap)),0,NumColours,
                                        ColourTable);

                                UnlockIBase(IntuiLock);

                                Locked = FALSE;

                                if(Picture = NewDTObject("ActiveWindow",
                                        DTA_SourceType, DTST_RAM,
                                        DTA_GroupID,    GID_PICTURE,
                                        PDTA_NumColors, NumColours,
                                        PDTA_BitMap,    BitMap,
                                        PDTA_ModeID,    ModeID,
                                TAG_DONE))
                                {
                                        struct ColorRegister    *ColourMap;
                                        struct BitMapHeader             *BitMapHeader;
                                        ULONG                                   *Colours;

                                        if(GetDTAttrs(Picture,
                                                PDTA_BitMapHeader,      &BitMapHeader,
                                                PDTA_ColorRegisters,&ColourMap,
                                                PDTA_CRegs,                     &Colours,
                                        TAG_DONE) == 3)
                                        {
                                                BitMapHeader -> bmh_Left                = Left;
                                                BitMapHeader -> bmh_Top                 = Top;
                                                BitMapHeader -> bmh_Width               = Width;
                                                BitMapHeader -> bmh_Height              = Height;
                                                BitMapHeader -> bmh_Depth               = Depth;
                                                BitMapHeader -> bmh_PageWidth           = PageWidth;
                                                BitMapHeader -> bmh_PageHeight          = PageHeight;

                                                CopyMem(ColourTable,Colours,
                                                        3 * sizeof(ULONG) * NumColours);

                                                while(NumColours--)
                                                {
                                                        ColourMap -> red        = (UBYTE)((*Colours++) >> 24);
                                                        ColourMap -> green      = (UBYTE)((*Colours++) >> 24);
                                                        ColourMap -> blue       = (UBYTE)((*Colours++) >> 24);

                                                        ColourMap++;
                                                }

                                                FreeVec(ColourTable);

                                                FreeVec(RPort);

                                                return(Picture);
                                        }

                                        DisposeDTObject(Picture);

                                        BitMap = NULL;
                                }

                                FreeVec(ColourTable);
                        }

                        FreeBitMap(BitMap);
                }

                if(Locked)
                        UnlockIBase(IntuiLock);

                FreeVec(RPort);
        }

        return(NULL);
}
