home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 106 / EnigmaAmiga106CD.iso / software / sviluppo / mcc_speedbar / developer / c / examples / mybrush.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-22  |  5.4 KB  |  218 lines

  1. /*
  2. ** $Id: mybrush.c,v 1.3 1999/08/10 21:53:03 wiz Exp $
  3. */
  4.  
  5. /// Includes
  6. #include <exec/types.h>                 // exec
  7. #include <exec/memory.h>
  8. #include <exec/libraries.h>
  9. #include <graphics/gfx.h>               // graphics
  10. #include <graphics/modeid.h>
  11. #include <graphics/gfxbase.h>
  12. #include <datatypes/datatypes.h>        // datatypes
  13. #include <datatypes/datatypesclass.h>
  14. #include <datatypes/pictureclass.h>
  15.  
  16. #include <proto/exec.h>
  17. #include <proto/graphics.h>
  18. #include <proto/datatypes.h>
  19.  
  20. #include "mybrush.h"
  21. ///
  22.  
  23. extern struct Library  *DataTypesBase;
  24. extern struct GfxBase  *GfxBase;
  25.  
  26. void FreeImg( struct MyImage * );
  27.  
  28. /// LoadImage
  29. struct MyImage *LoadImage( STRPTR File )
  30. {
  31.     struct MyImage *Img;
  32.     BOOL            ok = FALSE;
  33.  
  34.     if( DataTypesBase && ( Img = AllocMem( sizeof( struct MyImage ), MEMF_CLEAR ))) {
  35.         static ULONG tags[] = { DTA_GroupID, GID_PICTURE,
  36.                                 PDTA_Remap,  FALSE,
  37.                                 TAG_END };
  38.  
  39.         if( Img->Datatype = NewDTObjectA( File, ( struct TagItem * )tags )) {
  40.             struct BitMap          *b;
  41.             ULONG                   w, h;
  42.             struct gpLayout         gpl;
  43.  
  44.             gpl.MethodID    = DTM_PROCLAYOUT;
  45.             gpl.gpl_GInfo   = NULL;
  46.             gpl.gpl_Initial = 1;
  47.  
  48.             DoDTMethodA( Img->Datatype, NULL, NULL, (Msg) &gpl );
  49.  
  50.             GetDTAttrs( Img->Datatype,
  51.                         DTA_NominalVert,        &h,
  52.                         DTA_NominalHoriz,       &w,
  53.                         PDTA_BitMap,            &b,
  54.                         PDTA_ColorRegisters,    &Img->CReg,
  55.                         TAG_DONE );
  56.  
  57.             Img->Width       = w;
  58.             Img->Height      = h;
  59.             Img->Depth       = b->Depth;
  60.             Img->ImageData   = (UWORD *)b->Planes[0];
  61.             Img->BytesPerRow = b->BytesPerRow;
  62.  
  63.             for( w = 0; w < Img->Depth; w++ )
  64.                 Img->Planes[ w ] = b->Planes[ w ];
  65.  
  66.             ok = TRUE;
  67.         }
  68.  
  69.         if(!( ok )) {
  70.             FreeImg( Img );
  71.             Img = NULL;
  72.         }
  73.     }
  74.  
  75.     return( Img );
  76. }
  77. ///
  78. /// FreeImg
  79. void FreeImg( struct MyImage *Img )
  80. {
  81.     if( Img ) {
  82.  
  83.         if( Img->Datatype )
  84.             DisposeDTObject( Img->Datatype );
  85.  
  86.         FreeMem( Img, sizeof( struct MyImage ));
  87.     }
  88. }
  89. ///
  90.  
  91. /// CreateBitMap
  92. struct BitMap *CreateBitMap( ULONG width, ULONG height, ULONG depth, ULONG flags, struct BitMap *friend )
  93. {
  94.     struct BitMap *bm;
  95.  
  96.     if( GfxBase->LibNode.lib_Version >= 39 ) {
  97.         bm = AllocBitMap( width, height, depth, flags | BMF_CLEAR, friend );
  98.     } else {
  99.  
  100.         if( bm = AllocMem( sizeof( struct BitMap ), 0L )) {
  101.  
  102.             InitBitMap( bm, depth, width, height );
  103.  
  104.             if( bm->Planes[0] = (PLANEPTR) AllocVec( depth * RASSIZE( width, height ), MEMF_CHIP | MEMF_CLEAR )) {
  105.                 LONG i;
  106.  
  107.                 for( i = 1; i < depth; i++ )
  108.                     bm->Planes[ i ] = bm->Planes[ i - 1 ] + RASSIZE( width, height );
  109.  
  110.             } else {
  111.                 FreeMem( bm, sizeof( struct BitMap ));
  112.                 bm = NULL;
  113.             }
  114.         }
  115.     }
  116.  
  117.     return( bm );
  118. }
  119. ///
  120. /// DeleteBitMap
  121. void DeleteBitMap( struct BitMap *bm )
  122. {
  123.     if( bm ) {
  124.         if( GfxBase->LibNode.lib_Version >= 39 ) {
  125.             FreeBitMap( bm );
  126.         } else {
  127.             FreeVec( bm->Planes[0] );
  128.             FreeMem( bm, sizeof( struct BitMap ));
  129.         }
  130.     }
  131. }
  132. ///
  133.  
  134. /// LoadBrush
  135. struct MyBrush *LoadBrush( STRPTR File )
  136. {
  137.     struct MyImage *Img;
  138.     struct MyBrush *Brush = NULL;
  139.  
  140.     if( Img = LoadImage( File )) {
  141.  
  142.         if( Brush = AllocMem( sizeof( struct MyBrush ), MEMF_CLEAR )) {
  143.             BOOL    err = TRUE;
  144.  
  145.             Brush->Width  = Img->Width;
  146.             Brush->Height = Img->Height;
  147.  
  148.             if( Brush->BitMap = CreateBitMap( Brush->Width, Brush->Height, Img->Depth, BMF_DISPLAYABLE, NULL )) {
  149.                 ULONG   i, row;
  150.  
  151.                 row = ( Brush->Width + 7 ) >> 3;
  152.  
  153.                 for( i = 0; i < Img->Depth; i++ ) {
  154.                     UBYTE  *from, *to;
  155.                     ULONG   h;
  156.  
  157.                     from = Img->Planes[ i ];
  158.                     to   = Brush->BitMap->Planes[ i ];
  159.  
  160.                     for( h = 0; h < Img->Height; h++ ) {
  161.  
  162.                         CopyMem( from, to, row );
  163.  
  164.                         from += Img->BytesPerRow;
  165.                         to   += Brush->BitMap->BytesPerRow;
  166.                     }
  167.                 }
  168.  
  169.                 i = ( 1L << Img->Depth ) * 3;
  170.  
  171.                 if( Brush->Colors = AllocVec( i * sizeof( ULONG ), MEMF_ANY )) {
  172.                     UBYTE  *from;
  173.  
  174.                     if( from = Img->CReg ) {
  175.                         ULONG   n, *to;
  176.  
  177.                         to = Brush->Colors;
  178.  
  179.                         for( n = 0; n < i; n++ ) {
  180.                             UBYTE   c;
  181.  
  182.                             c = *from++;
  183.  
  184.                             *to++ = ( c << 24 ) | ( c << 16 ) | ( c << 8 ) | c;
  185.                         }
  186.                     }
  187.  
  188.                     err = FALSE;
  189.                 }
  190.             }
  191.  
  192.             if( err ) {
  193.                 FreeMem( Brush, sizeof( struct MyBrush ));
  194.                 Brush = NULL;
  195.             }
  196.         }
  197.  
  198.         FreeImg( Img );
  199.     }
  200.  
  201.     return( Brush );
  202. }
  203. ///
  204. /// FreeBrush
  205. void FreeBrush( struct MyBrush *Brush )
  206. {
  207.     if( Brush ) {
  208.  
  209.         DeleteBitMap( Brush->BitMap );
  210.  
  211.         FreeVec( Brush->Colors );
  212.  
  213.         FreeMem( Brush, sizeof( struct MyBrush ));
  214.     }
  215. }
  216. ///
  217.  
  218.