home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / ImageLib / Image_lib / lib_source / ChunkyToBM.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-02  |  6.5 KB  |  244 lines

  1. /* ChunkyToBM.c © Paweî Marciniak <pmarciniak@lodz.home.pl>*/
  2. #include <exec/execbase.h>
  3. #include <exec/types.h>
  4. #include <exec/memory.h>
  5. #include <graphics/rastport.h>
  6. #include <graphics/gfxbase.h>
  7. #include <graphics/view.h>
  8. #include <intuition/screens.h>
  9. #include <utility/tagitem.h>
  10. #include <proto/exec.h>
  11. #include <proto/graphics.h>
  12. #include <proto/utility.h>
  13.  
  14. #include "/include/libraries/image.h"
  15. #include "chunkytobm.h"
  16.  
  17. #define DEBUG
  18. VOID __stdargs CleanUp( struct NewBitMap *, struct Screen * );
  19. struct NewBitMap *MyAllocBitMap( UWORD, UWORD, BYTE );
  20.  
  21. /* ChunkyToBitMapA() (c) 1997 Paweî Marciniak
  22.   Modify date  |  Version  |  Comment
  23. ---------------+-----------+------------------------------
  24. 30-05-97 18:00 |  1.0      |
  25. 1-06-97  8:42  |  1.1      |
  26. 1-06-97 15:48  |  1.2      |
  27. 1-06-97 16:56  |  1.21     | 
  28. 2-06-97 15:44  |  1.22     |
  29. 8-06-97 12:23  |  1.23     |
  30. 12-08-97 18:47 |  1.3      |
  31. 14-08-97 12:12 |  1.4      |
  32. 12-09-97 18:03 |  1.5      |  Zamiana WritePixel() na WritePixelLine8()
  33. 13-09-97 18:25 |  1.6      |  Usuniëcie pëtli ponownego remapowania
  34. 11-10-97 19:33 |  1.7      |  Dodane tagi
  35. 13-01-98 19:51 |  1.71     |  Dodanie sprawdzenia czy AllocVec() zwraca NULL
  36. 13-01-98 20:04 |  1.72     |
  37. */
  38. struct BitMap * __saveds __asm ChunkyToBitMapA( register __a0 struct Screen *Pal_Screen_reg, register __a1 struct ChunkyImg *CI_reg, register __a2 struct TagItem *Tags  )
  39. {
  40.   UBYTE R, G, B;
  41.   register UBYTE *chunkyPtr, *PalettePtr, *arraymem, *arrayPtr;
  42.   register ULONG i, n, m;
  43.   ULONG Error;
  44.   LONG chunkysize, precision = PRECISION_GUI;
  45.   struct RastPort temprp, *ChunkyRPort;
  46.   struct NewBitMap *NBM;
  47.  
  48.   struct Screen *Pal_Screen = Pal_Screen_reg;
  49.   struct ChunkyImg *CI = CI_reg;
  50.   struct TagItem *ti, *TagsTmp = Tags;
  51.   chunkyPtr = CI->ci_ChunkyData;
  52.   PalettePtr = CI->ci_Palette;
  53.   chunkysize=CI->ci_Width*CI->ci_Height;
  54.  
  55. /* Tagi */
  56.   while ( ti = NextTagItem( &TagsTmp ) )
  57.   {
  58.     switch ( ti->ti_Tag )
  59.     {
  60.       case CTBM_Precision:
  61.         precision = ti->ti_Data;
  62.       break;
  63.  
  64.       default:
  65.       break;
  66.     }
  67.   }
  68.  
  69. /* sprawdzam iloôê kolorów jeôli > 256 wyjôcie */
  70.   if(CI->ci_NumColors > 256)
  71.   {
  72.     Error=1000;
  73.     return( 0 );
  74.   }
  75.  
  76.  
  77.  
  78.   if(!(ChunkyRPort=AllocVec( sizeof(struct RastPort),MEMF_ANY|MEMF_CLEAR )))
  79.   {
  80.     Error=1002;
  81.     return( 0 );
  82.   }
  83.  
  84.   InitRastPort( ChunkyRPort );
  85.   
  86.   if(!( NBM = (struct NewBitMap*)MyAllocBitMap( CI->ci_Width,
  87.                                                 CI->ci_Height,
  88.                                                 Pal_Screen->RastPort.BitMap->Depth)))
  89.   {
  90.     FreeVec( ChunkyRPort );
  91.     Error=1003;
  92.     return( 0 );
  93.   }
  94.   NBM->Width = CI->ci_Width;
  95.   NBM->Height = CI->ci_Height;
  96.   NBM->NumColors = CI->ci_NumColors;
  97.   ChunkyRPort->BitMap = (struct BitMap *)NBM;
  98.  
  99. /* wypeîniamy tabelë wartoôciami -1 */
  100.   for( i=0; i<CI->ci_NumColors; i++ )
  101.     NBM->ColorTab[i] = -1;
  102.  
  103. /* zamieniam wartoôci RGB na numer koloru i wpisujë do tablicy */
  104.   for( i=0; i<(CI->ci_NumColors); i++ )
  105.   {
  106.     R=*PalettePtr;
  107.     PalettePtr++;
  108.     G=*PalettePtr;
  109.     PalettePtr++;
  110.     B=*PalettePtr;
  111.     PalettePtr++;
  112.     NBM->ColorTab[i]=ObtainBestPen( Pal_Screen->ViewPort.ColorMap,
  113.                                   (ULONG)((R) << 24),
  114.                                   (ULONG)((G) << 24),
  115.                                   (ULONG)((B) << 24),
  116.                                    OBP_Precision, precision, TAG_DONE );
  117.     if(NBM->ColorTab[i] ==-1)
  118.     {
  119.       Error=1001;
  120.       CleanUp( NBM, Pal_Screen );
  121.       return( 0 );
  122.     }
  123.   }
  124.  
  125. /* Iniciacja temprp */
  126.   InitRastPort( &temprp );
  127.   if(!(temprp.BitMap = AllocBitMap( CI->ci_Width, 1,
  128.                   Pal_Screen->RastPort.BitMap->Depth, BMF_CLEAR, 0 )))
  129.   {
  130.     FreeVec( ChunkyRPort );
  131.     Error=1003;
  132.     CleanUp( NBM, Pal_Screen );
  133.     return( 0 );
  134.   }
  135.  
  136.  
  137.   if(!(arraymem=(UBYTE *)AllocVec((CI->ci_Width + 16), MEMF_ANY | MEMF_CLEAR )))
  138.   {
  139.     FreeVec( ChunkyRPort );
  140.     FreeBitMap( temprp.BitMap );
  141.     Error=1003;
  142.     CleanUp( NBM, Pal_Screen );
  143.     return( 0 );
  144.   }
  145.  
  146. /* konwersja chunky na bitmapë i remaping */
  147.   for( n = 0; n < CI->ci_Height; n++ )
  148.   {
  149.     arrayPtr=arraymem;
  150.     CopyMem( chunkyPtr, arrayPtr, CI->ci_Width );
  151.     for( m = 0; m < CI->ci_Width; m++ )
  152.     {
  153.       *arrayPtr=NBM->ColorTab[*arrayPtr]; /* remaping */
  154.       arrayPtr++;
  155.     }
  156.     chunkyPtr+=CI->ci_Width;
  157.     WritePixelLine8( ChunkyRPort, 0, n, CI->ci_Width, arraymem, &temprp );
  158.   }
  159.   FreeBitMap( temprp.BitMap );
  160.   FreeVec( arraymem );
  161.   FreeVec( ChunkyRPort );
  162.   return( (struct BitMap *)NBM );
  163. }
  164.  
  165. /**FreeChunky***********************************************************/
  166.  
  167.  
  168. /* FreeChunky() (c) 1997 Paweî Marciniak
  169.   Modify date  |  Version  |  Comment
  170. ---------------+-----------+------------------------------
  171. 30-05-97 18:00 |  1.0      |
  172. 8-06-97 12:23  |  1.1      |
  173. 12-08-97 18:53 |  2.0      |
  174. 14-08-97 12:15 |  3.0      |
  175. */
  176. VOID __saveds __asm FreeChunky( register __a0 struct Screen *Pal_Screen, register __a1 struct BitMap *BM )
  177. {
  178.   CleanUp( (struct NewBitMap *)BM, Pal_Screen );
  179. }
  180.  
  181.  
  182. /**CleanUp**************************************************************/
  183.  
  184.  
  185. /* CleanUp() (c) 1997 Paweî Marciniak
  186.   Modify date  |  Version  |  Comment
  187. ---------------+-----------+------------------------------
  188. 12-08-97 12:10 |  1.0      |
  189. */
  190. VOID CleanUp( struct NewBitMap *NewBM, struct Screen *Pal_Screen )
  191. {
  192.   ULONG i;
  193.  
  194.   if( NewBM )
  195.   {
  196.   /* Zwalniam pîaty bitowe */
  197.     FreeVec( NewBM->Planes[0] );
  198.  
  199.   /* Zwalniam wszystkie kolory */
  200.       for( i=0; i<NewBM->NumColors; i++ )
  201.     {
  202.       if( NewBM->ColorTab[i] > -1)
  203.       {
  204.          ReleasePen( Pal_Screen->ViewPort.ColorMap,NewBM->ColorTab[i] );
  205.         NewBM->ColorTab[i] = -1;
  206.       }
  207.     }
  208.     FreeVec( NewBM);
  209.     NewBM = NULL;
  210.   }
  211. }
  212.  
  213. /**MyAllocBitMap********************************************************/
  214.  
  215.  
  216. /* MyAllocBitMap() (c) 1997 Paweî Marciniak
  217.   Modify date  |  Version  |  Comment
  218. ---------------+-----------+------------------------------
  219. 14-08-97 12:10 |  1.0      |
  220. 13-01-98 20:03 |  1.01     |
  221. 26-09-98 17:44 |  1.02     | Change in first AllocVec MEMF_CHIP on MEMF_ANY
  222. */
  223. struct NewBitMap *MyAllocBitMap( UWORD Width, UWORD Height, BYTE Depth )
  224. {
  225.   int i;
  226.   struct NewBitMap *NBM;
  227.   PLANEPTR Data;
  228.  
  229.   if(!(NBM = AllocVec( sizeof(struct NewBitMap), MEMF_ANY)))
  230.     return( 0 );
  231.   InitBitMap((struct BitMap *)NBM,Depth, Width, Height );
  232.  
  233.   if(!(Data = (PLANEPTR)AllocVec( RASSIZE( Width, Height)*Depth, MEMF_CHIP )))
  234.   {
  235.     FreeVec( NBM );
  236.     return( 0 );
  237.   }
  238.   for(i = 0 ; i < Depth ; i++, Data += RASSIZE( Width, Height) )
  239.     NBM->Planes[i] = Data;
  240.   return( NBM);
  241. }
  242.  
  243. /***********************************************************************/
  244.