home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 4 / CD_Magazyn_EXEC_nr_4.iso / Recent / dev / c / GSys.lha / gsys / ggraphics / GBitMap.cpp next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  6.0 KB  |  329 lines

  1.  
  2. #ifndef GBITMAP_CPP
  3. #define GBITMAP_CPP
  4.  
  5. #include "ggraphics/GBitMap.h"
  6. #include "gsystem/GObject.cpp"
  7.  
  8. GBitMap::GBitMap(ULONG BMWidth, ULONG BMHeight, UWORD BMDepth)
  9. {
  10.     memset((void *)this, 0, sizeof (this));
  11.  
  12.     Width = BMWidth;
  13.     Height = BMHeight;
  14.     Depth = BMDepth;
  15.  
  16. #ifdef GAMIGA
  17.     if (AmigaBitMap = AllocBitMap(Width, Height, Depth, BMF_CLEAR | BMF_MINPLANES, NULL) )
  18.     {
  19.         Valid = TRUE;
  20.     }
  21. #endif
  22. #ifdef GDIRECTX
  23.     feil her
  24. #endif
  25.  
  26.     if ( (Depth == 8) && Valid )
  27.     {
  28.         Palette = new ULONG[256];
  29.         if (!Palette) Valid = FALSE;
  30.     }
  31.     if (Parent) Parent->InsertGBitMap(this);
  32. }
  33.  
  34. GBitMap::GBitMap(ULONG BMWidth, ULONG BMHeight, UWORD BMDepth, class GBuffer *GBuf)
  35. {
  36.     memset((void *)this, 0, sizeof (this));
  37.  
  38.     Width = BMWidth;
  39.     Height = BMHeight;
  40.     Depth = BMDepth;
  41.  
  42.  
  43. #ifdef GAMIGA
  44.     if ( AmigaBitMap = AllocBitMap(Width, Height, Depth, BMF_CLEAR | BMF_MINPLANES, NULL) )
  45.     {
  46.         Valid = TRUE;
  47.     }
  48. #endif
  49. #ifdef GDIRECTX
  50.     feil her
  51. #endif
  52.  
  53.     if (Valid)
  54.     {
  55.         APTR Src = GBuf->LockBuf();
  56.         APTR Dest = LockGBitMap();
  57.         ULONG Size = Width*Height*Depth/8;
  58.         memcpy(Dest, Src, Size);
  59.         UnLockGBitMap();
  60.         GBuf->UnLockBuf();
  61.  
  62.         if ( Depth == 8 )
  63.         {
  64.             Palette = new ULONG[256];
  65.             if (!Palette) Valid = FALSE;
  66.         }
  67.     }
  68.     if (Parent) Parent->InsertGBitMap(this);
  69. }
  70.  
  71. GBitMap::GBitMap(class GBuffer *GBuf)
  72. {
  73.     memset((void *)this, 0, sizeof (this));
  74.  
  75. /*
  76.     ULONG *Buf = (APTR) GBuf->LockBuf();
  77.     if ( Buf[0] == (ULONG) ((ULONG *)&"SIZE"[0]) )
  78.     {
  79.         Width = Buf[1];
  80.         Height = Buf[2];
  81.         Depth = Buf[3];
  82.         Buf+=16;
  83.  
  84. #ifdef GAMIGA
  85.         if ( AmigaBitMap = AllocBitMap(Width, Height, Depth, BMF_CLEAR | BMF_MINPLANES, NULL) )
  86.         {
  87.             Valid = TRUE;
  88.         }
  89. #endif
  90. #ifdef GDIRECTX
  91.         feil her
  92. #endif
  93.  
  94.         if ( Buf[0] == (ULONG) ((ULONG *)&"PALE"[0]) )
  95.         {
  96.             Palette = new ULONG[256];
  97.             SetPalette(&Buf[1], 0, 0, 256);
  98.             Buf+=257;    
  99.         }
  100.  
  101.         if ( Buf[0] == (ULONG) ((ULONG *)&"PALE"[0]) )
  102.         {
  103.             APTR Dest = LockGBitMap();            
  104.             memcpy(Dest, (APTR)Buf, Width*Height*Depth/8);
  105.             UnLockGBitMap();
  106.         }
  107.     }
  108.     GBuf->UnLockBuf();
  109. */
  110.  
  111.     if (Parent) Parent->InsertGBitMap(this);
  112. }
  113.  
  114.  
  115. GBitMap::~GBitMap()
  116. {
  117.     if (Parent)
  118.     {
  119. #ifdef GAMIGA
  120.         if (AmigaBitMap)
  121.         {
  122.             FreeBitMap(AmigaBitMap);
  123.         }
  124. #endif
  125.         if (Palette) delete Palette;
  126.         Parent->RemoveGBitMap(this);
  127.     }
  128. }
  129.  
  130. void PasteGBuffer(class GBuffer *GBuffer, ULONG SX, ULONG SY, UWORD SBPP, ULONG SBytesPerRow, ULONG DX, ULONG DY, ULONG Wid, ULONG Hei, ULONG *Pal)
  131. {
  132.     // unused atm
  133. }
  134.  
  135. #define PTGB_SIZE 1    // stores Width, Height(HE), Depth(DE) 16 bytes (SIZE)
  136. #define PTGB_PAL 2    // stores the Palette 256*4 bytes (PALE)
  137.  
  138. class GBuffer *PasteToGBuffer(ULONG Flags)
  139. {
  140. /*
  141.     ULONG Size = NULL;
  142.     BOOL SIZE= FALSE;
  143.     BOOL PAL = FALSE;
  144.     if (Flags & PTGB_SIZE)
  145.     {    
  146.         SIZE = TRUE;
  147.         Size += 16;
  148.     }
  149.     if (Flags & PTGB_PAL)
  150.     {
  151.         PAL = TRUE;
  152.         Size += 1028;
  153.     }
  154.     Size+= Width*Height*Depth/8+4;
  155.  
  156.     class GBuffer *DestBuf = new GBuffer(Size, FileName)
  157.  
  158.     ULONG *Buf = (ULONG *)DestBuf->LockBuf();
  159.     if (SIZE)
  160.     {
  161.         Buf[0] = ((ULONG *)&"SIZE"[0]);
  162.         Buf[1] = Width;
  163.         Buf[2] = Height;
  164.         Buf[3] = (ULONG) Depth;
  165.         Buf+=4;
  166.     }
  167.     if (PAL && Palette)
  168.     {
  169.         Buf[0] = ((ULONG *)&"PALE"[0]);
  170.         Buf+=1;
  171.         ULONG c;
  172.         for (c=0; c<256; c++)
  173.         {
  174.             Buf[i] = Palette[i];
  175.         }
  176.         Buf+=256;
  177.     }
  178.  
  179.     APTR Src = LockGBitMap();
  180.     if (Src)
  181.     {
  182.         Buf[0] = ((ULONG *)&"BMAP"[0]);
  183.         Buf+=1;
  184.         memcpy((APTR)Buf, Src, Width*Height*Depth/8 );
  185.     }
  186.     else
  187.     {
  188.         UnLockGBitMap();
  189.         GBuf->UnLockBuf();
  190.         delete DestBuf;
  191.         return FALSE;
  192.     }
  193.  
  194.     UnLockGBitMap();
  195.     DestBuf->UnLockBuf();
  196.     return DestBuf;
  197. */
  198.     return NULL;
  199. }
  200.     
  201. class GBitMap *GBitMap::ScaleBitMap(ULONG OffsetX, ULONG OffsetY, ULONG SrcWidth, ULONG SrcHeight, ULONG NewWidth, ULONG NewHeight)
  202. {
  203. #ifdef GAMIGA
  204.     if (Parent)
  205.     {
  206.         LastScaledGBitMap = new GBitMap(NewWidth, NewHeight, Depth);
  207.         if (LastScaledGBitMap)
  208.         {
  209.             Parent->BitScaleArgs.bsa_SrcX = OffsetX;
  210.             Parent->BitScaleArgs.bsa_SrcY = OffsetY;
  211.  
  212.             Parent->BitScaleArgs.bsa_SrcWidth = SrcWidth;
  213.             Parent->BitScaleArgs.bsa_SrcHeight = SrcHeight;
  214.  
  215.             Parent->BitScaleArgs.bsa_XSrcFactor = SrcWidth;
  216.             Parent->BitScaleArgs.bsa_YSrcFactor = SrcHeight;
  217.  
  218.             Parent->BitScaleArgs.bsa_DestX = 0;
  219.             Parent->BitScaleArgs.bsa_DestY = 0;
  220.  
  221.             Parent->BitScaleArgs.bsa_DestWidth = NewWidth;
  222.             Parent->BitScaleArgs.bsa_DestHeight = NewHeight;
  223.  
  224.             Parent->BitScaleArgs.bsa_XDestFactor = NewWidth;
  225.             Parent->BitScaleArgs.bsa_YDestFactor = NewHeight;
  226.  
  227.             Parent->BitScaleArgs.bsa_SrcBitMap = AmigaBitMap;
  228.             Parent->BitScaleArgs.bsa_DestBitMap = LastScaledGBitMap->AmigaBitMap;
  229.  
  230.             Parent->BitScaleArgs.bsa_Flags = NULL;
  231.  
  232.             BitMapScale(&Parent->BitScaleArgs);
  233.  
  234.             return LastScaledGBitMap;
  235.         }
  236.         else
  237.         {
  238.             #ifdef GDEBUG
  239.             printf("Attempt to open a GBitMap-object failed!\n");
  240.             #endif
  241.             return NULL;
  242.         }
  243.     }
  244.     else
  245.     {
  246.     #ifdef GDEBUG
  247.     printf("BitMapScale needs GSystem\n");
  248.     #endif
  249.     return NULL;
  250.     }
  251. #endif
  252. }
  253.  
  254. APTR GBitMap::LockGBitMap()
  255. {
  256. #ifdef GAMIGA
  257.     ULONG DDWidth = NULL;
  258.     ULONG DDHeight = NULL;
  259.  
  260.     struct TagItem LBMTags[] =
  261.     {
  262.         LBMI_WIDTH, (ULONG)&DDWidth,
  263.         LBMI_HEIGHT, (ULONG)&DDHeight,
  264.         LBMI_PIXFMT, (ULONG)&DDPxlFmt,
  265.         LBMI_BYTESPERPIX, (ULONG)&DDBytesPix,
  266.         LBMI_BYTESPERROW, (ULONG)&DDBytesRow,
  267.         LBMI_BASEADDRESS, (ULONG)&DDBuffer,
  268.         TAG_DONE,
  269.     };
  270.  
  271.     if (CyberGfxBase)
  272.     {
  273.         if ( GetCyberMapAttr(AmigaBitMap, CYBRMATTR_ISCYBERGFX ) )
  274.         {
  275.             Handle = LockBitMapTagList((APTR) AmigaBitMap, LBMTags);
  276.             if (Handle) return DDBuffer;
  277.         }
  278.     }
  279.     return NULL;
  280. #endif
  281. }
  282.  
  283. void GBitMap::UnLockGBitMap()
  284. {
  285. #ifdef GAMIGA
  286.     if (Handle)
  287.     {
  288.         UnLockBitMap(Handle);
  289.         Handle = NULL;
  290.     }
  291. #endif
  292. }
  293.  
  294. /*
  295. *  SetTrueColorPalette()
  296. *  Sets the palette to a 323 TrueColor palette, which is a bad-quality truecolor table
  297. */
  298.  
  299. void GBitMap::SetTrueColorPalette()
  300. {
  301.     ULONG color;
  302.     for (color=0; color<256; color++)
  303.     {
  304.         Palette[color] = ((color&0x1f)<<11) | ((color&0xe7)<<5) | (color&0x7);
  305.     }
  306. }
  307.  
  308. void GBitMap::SetPalette(ULONG *Pal, ULONG FirstSCol, ULONG FirstDCol, ULONG Colors)
  309. {
  310.     if (Palette)
  311.     {
  312.  
  313. //    if ((FirstCol+Colors) < 256)
  314. //    {
  315.         ULONG color;
  316.         ULONG *PalD = (ULONG *) &Palette[FirstDCol];
  317.         ULONG *PalS = (ULONG *) &Pal[FirstSCol];
  318.         for (color=0; color<Colors; color++)
  319.         {
  320.             PalD[color] = PalS[color];
  321.         }
  322.     }
  323.     else printf("No Palette!\n");
  324. }
  325.  
  326.  
  327.  
  328. #endif /* ifndef GBITMAP_CPP */
  329.