home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / ImageLib / Image_Demo / Image_Demo.c < prev    next >
C/C++ Source or Header  |  1999-06-01  |  8KB  |  351 lines

  1. /* Image_Demo © Paweî Marciniak */
  2. /* Obrazek pochodzi z pîyty MACD 2 */
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <exec/execbase.h>
  6. #include <exec/types.h>
  7. #include <exec/memory.h>
  8. #include <intuition/intuition.h>
  9. #include <libraries/gadtools.h>
  10. #include <graphics/text.h>
  11. #include <graphics/gfxbase.h>
  12. #include <graphics/scale.h>
  13. #include <proto/dos.h>
  14. #include <proto/exec.h>
  15. #include <proto/intuition.h>
  16. #include <proto/graphics.h>
  17. #include <proto/gadtools.h>
  18. #include <proto/diskfont.h>
  19. #include <proto/dos.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22.  
  23. #include <proto/image.h>
  24. #include <libraries/image.h>
  25.  
  26. #include "APP_globals.h"
  27.  
  28. extern UBYTE home_pal[];
  29. extern UBYTE __far home_data[];
  30.  
  31. extern struct ExecBase *SysBase;
  32. struct Screen *Screen=NULL;
  33. struct Window *APP_Window=NULL;
  34.  
  35. STRPTR PubScreenName="Workbench";
  36. STRPTR APP_TitleWindow="Image.library Demo Window";
  37. STRPTR APP_TitleScreenWindow="Image.library Demo Screen";
  38.  
  39. APTR  VisualInfo = NULL;
  40.  
  41. /* Font */
  42.  
  43. struct TextAttr *Font, ScreenFont;
  44. struct TextFont    *APP_Font;
  45.  
  46. WORD FontX=NULL;
  47. WORD FontY=NULL;
  48. UWORD OffX, OffY;
  49.  
  50. ULONG            IClass;
  51. UWORD            Code, Qualifier;
  52. struct Gadget    *IObject;
  53.  
  54. /* Rozmiar Okna */
  55.  
  56.     WORD Width=320;
  57.     WORD Height=150;
  58.     WORD MinWidth=50;
  59.     WORD MinHeight=30;
  60.     WORD MaxWidth=1600;
  61.     WORD MaxHeight=1200;
  62.  
  63.     struct ChunkyImg homeimg;
  64.  
  65.  
  66. /* Tytaj zaczynajâ sië wszystkie poûyteczne funkcje */
  67.  
  68.  
  69. /* Funkcja otwiera okreôlony font */
  70.  
  71. int OpenFonts( void )
  72. {
  73.     if (!(APP_Font=OpenDiskFont( &ScreenFont )))
  74.         return( FALSE );
  75.     return( TRUE );
  76. }
  77.  
  78.  
  79. /* Funkcja zamyka otwarty font */
  80.  
  81. void CloseFonts( void )
  82. {
  83.     if ( APP_Font )    
  84.     {
  85.         CloseFont( APP_Font );
  86.         APP_Font=NULL;
  87.     }
  88. }
  89.  
  90.  
  91. UWORD ComputeX( UWORD value )
  92. {
  93.     return(( UWORD )((( FontX * value ) + 4 ) / 8 ));
  94. }
  95.  
  96.  
  97. UWORD ComputeY( UWORD value )
  98. {
  99.     return(( UWORD )((( FontY * value ) + 4 ) / 8 ));
  100. }
  101.  
  102.  
  103. void ComputeFont( void )
  104. {
  105.     Font = &ScreenFont;
  106.     Font->ta_Name = (STRPTR)Screen->RastPort.Font->tf_Message.mn_Node.ln_Name;
  107.     Font->ta_YSize = FontY = Screen->RastPort.Font->tf_YSize;
  108.     FontX = Screen->RastPort.Font->tf_XSize;
  109.  
  110.     OffX = Screen->WBorLeft;
  111.     OffY = Screen->RastPort.TxHeight + Screen->WBorTop + 1;
  112.     return;
  113. }
  114.  
  115.  
  116. /* Funkcja otwiera okno */
  117.  
  118. int OpenAPP_Display( WORD Left, WORD Top )
  119. {
  120.  
  121.     if(!(APP_Window=OpenWindowTags( 0,
  122.         WA_Left,                Left,
  123.         WA_Top,                    Top,
  124.         WA_Width,                (ComputeX(Width) + OffX + Screen->WBorRight),
  125.         WA_Height,            (ComputeY(Height) + OffY + Screen->WBorBottom),
  126.         WA_MinWidth,        (ComputeX(MinWidth) + OffX + Screen->WBorRight),
  127.         WA_MinHeight,        (ComputeX(MinHeight) + OffY + Screen->WBorBottom),
  128.         WA_MaxWidth,        (ComputeX(MaxWidth) + OffX + Screen->WBorRight),
  129.         WA_MaxHeight,        (ComputeY(MaxHeight) + OffY + Screen->WBorBottom),
  130.         WA_Title,                APP_TitleWindow,
  131.         WA_ScreenTitle,    APP_TitleScreenWindow,
  132.         WA_Flags,                WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_SIZEGADGET | WFLG_CLOSEGADGET | WFLG_SIMPLE_REFRESH,
  133.         WA_IDCMP,                IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW,
  134.         WA_Gadgets,            FALSE,
  135.         WA_AutoAdjust,    FALSE,
  136.         WA_PubScreen,        (struct Screen *)Screen,
  137.         TAG_DONE)))
  138.             return( FALSE );
  139.     GT_RefreshWindow( APP_Window, NULL );
  140.     return( TRUE );
  141. }
  142.  
  143.  
  144. /* Funkcja zamyka otwarte okno */
  145.  
  146. void CloseAPP_Display(void)
  147. {
  148.     if(APP_Window) 
  149.     {
  150.         CloseWindow((struct Window *)APP_Window);
  151.         APP_Window=NULL;
  152.     }
  153. }
  154.  
  155.  
  156. /* Funkcja odczytuje komunikat Intuition */
  157.  
  158. LONG ReadIMsg( struct Window *iwnd )
  159. {
  160.     struct IntuiMessage *imsg;
  161.  
  162.     if ( imsg = GT_GetIMsg( iwnd->UserPort ))
  163.         {
  164.         IClass      =    imsg->Class;
  165.         Qualifier    =    imsg->Qualifier;
  166.         Code        =    imsg->Code;
  167.         IObject     =    imsg->IAddress;
  168.  
  169.         GT_ReplyIMsg( imsg );
  170.  
  171.         return( TRUE );
  172.     }
  173.     return( FALSE );
  174. }
  175.  
  176.  
  177. /* Funkcja alokuje ekran */
  178.  
  179. int SetupScreen( void )
  180. {
  181.     if (!(Screen = LockPubScreen( PubScreenName )))
  182.         return( FALSE );
  183.  
  184.     ComputeFont();
  185.  
  186.     if(!(OpenFonts()))
  187.         return( FALSE );
  188.  
  189.  
  190.     if (!( VisualInfo = GetVisualInfo( Screen, TAG_DONE )))
  191.         return( FALSE );
  192.  
  193.     return( TRUE );
  194. }
  195.  
  196.  
  197. /* Funkcja dealokuje ekran */
  198.  
  199. void CloseDownScreen( void )
  200. {
  201.     if ( VisualInfo )
  202.     {
  203.         FreeVisualInfo( VisualInfo );
  204.         VisualInfo = NULL;
  205.     }
  206.  
  207.     CloseFonts();
  208.  
  209.     if ( Screen )
  210.     {
  211.         UnlockPubScreen( NULL, Screen );
  212.         Screen = NULL;
  213.     }
  214. }
  215.  
  216.  
  217. /* Funkcja otwiera wszystkie biblioteki */
  218.  
  219. LONG OpenLibraries( void )
  220. {
  221.     if ( !(DosBase = (struct DosLibrary *) OpenLibrary((UBYTE *) "dos.library", 36 )))
  222.         return( FALSE );
  223.     if ( !(IntuitionBase = (struct IntuitionBase *) OpenLibrary((UBYTE *) "intuition.library", 36 )))
  224.         return( FALSE );
  225.     if ( !(GadToolsBase = (struct Library *) OpenLibrary((UBYTE *) "gadtools.library", 36 )))
  226.         return( FALSE );
  227.     if ( !(GfxBase = (struct GfxBase *) OpenLibrary((UBYTE *) "graphics.library" , 36 )))
  228.         return( FALSE );
  229.     if ( !(DiskFontBase = (struct Library *) OpenLibrary((UBYTE *) "diskfont.library" , 36 )))
  230.         return( FALSE );
  231.     if ( !(ImageBase = (struct Library *) OpenLibrary((UBYTE *) "image.library" , 36 )))
  232.         return( FALSE );
  233.     return( TRUE );
  234. }
  235.  
  236.  
  237. /* Funkcja zamyka wszystkie biblioteki */
  238.  
  239. void CloseLibraries( void )
  240. {
  241.     if (ImageBase)            CloseLibrary( (struct Library *) ImageBase );
  242.     if (DiskFontBase)        CloseLibrary( (struct Library *) DiskFontBase );
  243.     if (GfxBase)                CloseLibrary( (struct Library *) GfxBase );
  244.     if (GadToolsBase)        CloseLibrary( (struct Library *) GadToolsBase );
  245.     if (IntuitionBase)    CloseLibrary( (struct Library *) IntuitionBase );
  246.     if (DosBase)                CloseLibrary( (struct Library *) DOSBase );
  247. }
  248.  
  249.  
  250.  
  251. /* Gîówna funkcja */
  252.  
  253. int main( int argc, char *argv[] )
  254. {
  255. BOOL running=TRUE;
  256. UBYTE text[50];
  257. struct BitMap *HomeBM, *ScaledBM;
  258.  
  259.     if(!(OpenLibraries()))
  260.     {
  261.         CloseLibraries();
  262.         return( FALSE );
  263.     }
  264.     
  265.     if(!(SetupScreen()))
  266.     {
  267.         CloseDownScreen();
  268.         CloseLibraries();
  269.         return( FALSE );
  270.     }
  271.  
  272.     if(!(OpenAPP_Display( 20, 20 )))
  273.     {
  274.         CloseDownScreen();
  275.         CloseLibraries();
  276.         return( FALSE );
  277.     }
  278.  
  279.     SetWindowTitles( APP_Window, "Converce c2p wait...", (UBYTE *)~0 );
  280.     homeimg.ci_Width=640;
  281.     homeimg.ci_Height=512;
  282.     homeimg.ci_NumColors=128;
  283.     homeimg.ci_Palette=home_pal;
  284.     homeimg.ci_ChunkyData=home_data;
  285.     if(!(HomeBM = ChunkyToBitMap( Screen, &homeimg, CTBM_Precision, PRECISION_EXACT, TAG_DONE)))
  286.     {
  287.         CloseAPP_Display();
  288.         CloseDownScreen();
  289.         CloseLibraries();
  290.         return( TRUE );
  291.     }
  292.     SetWindowTitles( APP_Window, "Scaling...", (UBYTE *)~0 );
  293.     if(ScaledBM = ScaleBitMap( HomeBM,
  294.                                         SBA_SrcWidth, 640,
  295.                                         SBA_SrcHeight, 512,
  296.                                         SBA_DestWidth, (APP_Window->Width - (OffX + APP_Window->BorderRight)),
  297.                                         SBA_DestHeight, (APP_Window->Height - (OffY + APP_Window->BorderBottom)),
  298.                                         TAG_DONE))
  299.     {
  300.         SetWindowTitles( APP_Window, "Drawing...", (UBYTE *)~0 );
  301.         DrawBitMap( ScaledBM, OffX, OffY, (APP_Window->Width - (OffX + APP_Window->BorderRight)), (APP_Window->Height - (OffY + APP_Window->BorderBottom)), APP_Window->RPort );
  302.         FreeBitMap( ScaledBM );
  303.         sprintf(text,"Image size: %dx%d", (APP_Window->Width - (OffX + APP_Window->BorderRight)), (APP_Window->Height - (OffY + APP_Window->BorderBottom)));
  304.         SetWindowTitles( APP_Window, text, (UBYTE *)~0 );
  305.     }
  306.     
  307.  
  308.     do
  309.     {
  310.         WaitPort( APP_Window->UserPort );
  311.  
  312.         while ( ReadIMsg( APP_Window ))
  313.         {
  314.             switch ( IClass )
  315.             {
  316.                 case    IDCMP_REFRESHWINDOW:
  317.                     GT_BeginRefresh( APP_Window );
  318.                     GT_EndRefresh( APP_Window, TRUE );
  319.                     SetWindowTitles( APP_Window, "Scaling...", (UBYTE *)~0 );
  320.                     if(ScaledBM = ScaleBitMap( HomeBM,
  321.                                         SBA_SrcWidth, 640,
  322.                                         SBA_SrcHeight, 512,
  323.                                         SBA_DestWidth, (APP_Window->Width - (OffX + APP_Window->BorderRight)),
  324.                                         SBA_DestHeight, (APP_Window->Height - (OffY + APP_Window->BorderBottom)),
  325.                                         TAG_DONE))
  326.                     {
  327.                         SetWindowTitles( APP_Window, "Drawing...", (UBYTE *)~0 );
  328.                         DrawBitMap( ScaledBM, OffX, OffY, (APP_Window->Width - (OffX + APP_Window->BorderRight)), (APP_Window->Height - (OffY + APP_Window->BorderBottom)), APP_Window->RPort );
  329.                         FreeBitMap( ScaledBM );
  330.                         sprintf(text,"Image size: %dx%d", (APP_Window->Width - (OffX + APP_Window->BorderRight)), (APP_Window->Height - (OffY + APP_Window->BorderBottom)));
  331.                         SetWindowTitles( APP_Window, text, (UBYTE *)~0 );
  332.                     }
  333.                 break;
  334.  
  335.                 case    IDCMP_CLOSEWINDOW:
  336.                     running=FALSE;
  337.                     FreeChunky( Screen, HomeBM );
  338.                     break;
  339.                         
  340.                 default:
  341.                 break;
  342.             }
  343.         }
  344.     } while ( running );
  345.  
  346.     CloseAPP_Display();
  347.     CloseDownScreen();
  348.     CloseLibraries();
  349.     return( TRUE );
  350. }
  351.