home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / imageioproc.lib / dev / examples / doimage / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-17  |  5.6 KB  |  234 lines

  1. /* This example use of imageprocess.library loads the image file specified
  2.         on the command line and views it halved in size in a guigfx window.
  3. */
  4.  
  5. #include <stdio.h>
  6.  
  7. #include <dos/dos.h>
  8. #include <exec/memory.h>
  9. #include <exec/types.h>
  10.  
  11. #include <clib/dos_protos.h>
  12. #include <clib/exec_protos.h>
  13. #include <clib/intuition_protos.h>
  14.  
  15. #include <pragmas/dos_pragmas.h>
  16. #include <pragmas/exec_pragmas.h>
  17. #include <pragmas/intuition_pragmas.h>
  18.  
  19. #include <imageio/imageio.h>
  20. #include <imageio/imageio_protos.h>
  21. #include <imageio/imageio_pragmas.h>
  22.  
  23. #include <imageprocess/imageprocess.h>
  24. #include <imageprocess/imageprocess_protos.h>
  25. #include <imageprocess/imageprocess_pragmas.h>
  26.  
  27. #include <guigfx/guigfx.h>
  28. #include <pragmas/guigfx_pragmas.h>
  29. #include <guigfx/guigfx_protos.h>
  30.  
  31. /* Function prototypes */
  32. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata );
  33. void DisplayGuiGfx( UBYTE *buffer, UBYTE cs, ULONG rs, ULONG x, ULONG y );
  34.  
  35. extern struct Library *DOSBase;
  36. struct Library *ImageIOBase, *IntuitionBase, *ImageProcessBase;
  37.  
  38. void main( int argc, char **argv )
  39. {
  40.     if ( argv[1] )
  41.     {
  42.         ImageIOBase = OpenLibrary( "imageio.library", 2 );
  43.         ImageProcessBase = OpenLibrary( "imageprocess.library", 1 );
  44.         IntuitionBase = OpenLibrary( "intuition.library", NULL );
  45.         if ( IntuitionBase && ImageIOBase && ImageProcessBase )
  46.         {
  47.             struct ImageHandle *ih;
  48.             ULONG err;
  49.  
  50.             err = AllocImage( &ih,
  51.                 IMG_SrcFilename, argv[1],
  52.                 TAG_DONE );
  53.             if ( !err )
  54.             {
  55.                 ULONG num = 1, denom = 2;
  56.                 ULONG x, y, bpp;
  57.                 UBYTE *buffer, cs;
  58.                 int prevpercent;
  59.  
  60.                 err = GetImageAttrs( ih,
  61.                     IMG_Width, &x,
  62.                     IMG_Height,&y,
  63.                     IMG_BytesPerPixel, &bpp,
  64.                     IMG_ColourSpace, &cs,
  65.                     IMG_TestScaleNum, num,
  66.                     IMG_TestScaleDenom, denom,
  67.                     TAG_DONE );
  68.                 if ( !err )
  69.                 {
  70.                     printf( "width=%ld, height=%ld\n", x, y );
  71.                     printf( "bytes per pixel=%ld, colourspace=%d\n", bpp, cs );
  72.  
  73.                     prevpercent = 0;
  74.  
  75.                     err = ReadImage( ih,
  76.                         IMG_ScaleNum, num,
  77.                         IMG_ScaleDenom, denom,
  78.                         IMG_ImageBuffer, &buffer,
  79.                         IMG_ProgressHook, progressFunc,
  80.                         IMG_ProgressUserData, &prevpercent,
  81.                         TAG_DONE );
  82.                     if ( !err )
  83.                     {
  84.                         ULONG iperr;
  85.  
  86.                         printf("imageprocess.library\n");
  87.  
  88.                         iperr = DoImageProcess( IMP_Process_Scale,
  89.                             IMP_SourceImageIOHandle, ih,
  90.                             IMP_ScaleDenom, 2,
  91.                             IMP_ScaleType, IPSCALE_FAST,
  92.                             IMP_ScaleMaintainAspect, FALSE,
  93.                             TAG_DONE );
  94.                         if ( !iperr )
  95.                         {
  96.                             iperr = GetImageAttrs( ih,
  97.                                 IMG_Width, &x,
  98.                                 IMG_Height, &y,
  99.                                 IMG_ImageBuffer, &buffer,
  100.                                 TAG_DONE );
  101.                             if ( !iperr )
  102.                             {
  103.                                 printf("%ld x %ld\n",x,y);
  104.  
  105.                                 DisplayGuiGfx( buffer, cs, x * bpp, x, y );
  106.                             }
  107.                             else  printf( "imagegetattrs error:%ld\n", iperr );
  108.                         }
  109.                         else printf( "doimageprocess error:%ld\n", iperr );
  110.                     }
  111.                     else printf( "read image error:%d\n", err );
  112.                 }
  113.                 else printf( "get image attrs error:%d\n", err );
  114.  
  115.                 FreeImage( ih );
  116.             }
  117.             else printf( "alloc image error:%d\n", err );
  118.         }
  119.  
  120.         if ( ImageIOBase ) CloseLibrary( ImageIOBase );
  121.         if ( ImageProcessBase ) CloseLibrary( ImageProcessBase );
  122.         if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
  123.     }
  124. }
  125.  
  126. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata )
  127. {
  128.     int *prevpercent = (int *)userdata;
  129.  
  130.     int percent = ( curr * 100 ) / lines;
  131.  
  132.     if ( *prevpercent != percent )
  133.     {
  134.         if ( percent % 10 == 0 ) printf( "%d%%\n", percent );
  135.     }
  136.  
  137.     *prevpercent = percent;
  138.  
  139.     return NULL;
  140. }
  141.  
  142. void DisplayGuiGfx( UBYTE *buffer, UBYTE cs, ULONG rs, ULONG x, ULONG y )
  143. {
  144.     struct Library *GuiGFXBase;
  145.  
  146.     GuiGFXBase = OpenLibrary( "guigfx.library", NULL );
  147.     if ( GuiGFXBase )
  148.     {
  149.         struct Window *win;
  150.         APTR dh, pi;
  151.         UBYTE *argb;
  152.  
  153.         win = OpenWindowTags( NULL,
  154.             WA_Title, "Proof",
  155.             WA_Flags, WFLG_ACTIVATE | WFLG_SIMPLE_REFRESH |
  156.                 WFLG_SIZEGADGET | WFLG_RMBTRAP | WFLG_DRAGBAR |
  157.                 WFLG_DEPTHGADGET | WFLG_CLOSEGADGET,
  158.             WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  159.                 IDCMP_SIZEVERIFY | IDCMP_NEWSIZE | IDCMP_RAWKEY,
  160.             WA_Left, 16,
  161.             WA_Top, 16,
  162.             WA_Width, x,
  163.             WA_Height, y,
  164.             TAG_DONE );
  165.  
  166.         if ( win != NULL )
  167.         {
  168.             dh = ObtainDrawHandle( NULL, win->RPort, win->WScreen->ViewPort.ColorMap, TAG_DONE );
  169.  
  170.             if ( dh )
  171.             {
  172.                 argb = AllocVec( x * y * 4, MEMF_PUBLIC | MEMF_CLEAR );
  173.  
  174.                 if ( argb )
  175.                 {
  176.                     int i, count = 0;
  177.                     char **dest;
  178.  
  179.                     /* Convert a buffer to an ARGB buffer setting A to 0.*/
  180.                     dest = (char **)argb;
  181.  
  182.                     switch ( cs )
  183.                     {
  184.                         case IMCS_RGB:
  185.                             for ( i = 0; i < x * y * 3; i += 3 )
  186.                             {
  187.                                 dest[count++] = (char *)( ( (ULONG) * (char**)&buffer[i] ) >> 8 );
  188.                             }
  189.                         break;
  190.  
  191.                         case IMCS_GREY:
  192.                             for ( i = 0; i < x * y; i++ )
  193.                             {
  194.                                 argb[count++] = 0;
  195.                                 argb[count++] = buffer[i];
  196.                                 argb[count++] = buffer[i];
  197.                                 argb[count++] = buffer[i];
  198.                             }
  199.                         break;
  200.                     }
  201.  
  202.                     pi = MakePicture( argb, x, y, GGFX_PixelFormat, PIXFMT_0RGB_32, TAG_DONE );
  203.  
  204.                     if ( pi )
  205.                     {
  206.                         struct Message *msg;
  207.  
  208.                         DrawPicture( dh, pi, 0, 0, NULL );
  209.  
  210.                         Wait( 1L << win->UserPort->mp_SigBit );
  211.  
  212.                         while ( ( msg = GetMsg( win->UserPort ) ) != NULL ) ReplyMsg( msg );
  213.  
  214.                         DeletePicture( pi );
  215.                     }
  216.                     else printf( "failed to create picture\n" );
  217.  
  218.                     FreeVec( argb );
  219.                 }
  220.                 else printf( "failed to allocate argb buffer\n" );
  221.  
  222.                 ReleaseDrawHandle( dh );
  223.             }
  224.             else printf( "failed to get drawhandle\n" );
  225.  
  226.             CloseWindow( win );
  227.         }
  228.         else printf( "failed to open window\n" );
  229.     }
  230.     else printf( "failed to open guigfx.library\n" );
  231.  
  232.     if ( GuiGFXBase ) CloseLibrary( GuiGFXBase );
  233. }
  234.