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

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