home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 117 / af117sub.adf / jpeglibrary.lzx / jpeglibrary / examples / load / load_file.c next >
C/C++ Source or Header  |  1998-09-22  |  5KB  |  185 lines

  1. /* This example use of jpeg.library loads the jpeg file specified
  2.         on the command line and views it halved in size on a cybergraphics
  3.         screen. It uses an RGB triplet buffer tag to store the image data
  4.         from jpeg.library. This example uses an AmigaDOS file pointer for the
  5.         source stream. It also demonstrates the use of a progress indicator
  6.         callback hook.
  7.  
  8.         Supports RGB and Grayscale source jpeg images.
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. #include <dos/dos.h>
  14. #include <exec/types.h>
  15.  
  16. #include <clib/dos_protos.h>
  17. #include <clib/exec_protos.h>
  18. #include <clib/cybergraphics_protos.h>
  19. #include <clib/intuition_protos.h>
  20.  
  21. #include <pragmas/dos_pragmas.h>
  22. #include <pragmas/exec_pragmas.h>
  23. #include <pragmas/intuition_pragmas.h>
  24. #include <pragmas/cybergraphics_pragmas.h>
  25.  
  26. #include <cybergraphics/cybergraphics.h>
  27.  
  28. #include <jpeg/jpeg.h>
  29. #include <jpeg/jpeg_protos.h>
  30. #include <jpeg/jpeg_pragmas.h>
  31.  
  32. /* Function prototypes */
  33. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata );
  34.  
  35. extern struct Library *DOSBase;
  36. struct Library *JpegBase, *IntuitionBase;
  37. struct CyberGfxBase *CyberGfxBase = NULL;
  38.  
  39. void main( int argc, char **argv )
  40. {
  41.     JpegBase = OpenLibrary( "jpeg.library", 2 );
  42.     IntuitionBase = OpenLibrary( "intuition.library", NULL );
  43.     CyberGfxBase = (struct CyberGfxBase *)OpenLibrary( "cybergraphics.library", 0L );
  44.  
  45.     if ( IntuitionBase != NULL && CyberGfxBase != NULL && JpegBase != NULL )
  46.     {
  47.         ULONG err;
  48.         ULONG DisplayID;
  49.         struct Screen *scr;
  50.         struct Window *win;
  51.         struct Message *msg;
  52.         struct JPEGDecHandle *jph;
  53.         UBYTE *buffer;
  54.         ULONG x, y;
  55.         BPTR fp;
  56.         UBYTE colourspace;
  57.         ULONG bpp;
  58.  
  59.         fp = Open( argv[1], MODE_OLDFILE );
  60.         if ( fp != NULL )
  61.         {
  62.             err = AllocJPEGDecompress( &jph, JPG_SrcFile, fp, TAG_DONE );
  63.             if ( !err )
  64.             {
  65.                 err = GetJPEGInfo( jph, JPG_Width, &x, JPG_Height, &y,
  66.                     JPG_ColourSpace, &colourspace,
  67.                     JPG_BytesPerPixel, &bpp,
  68.                     JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  69.                     TAG_DONE );
  70.                 if ( !err )
  71.                 {
  72.                     printf( "colourspace=%d\n", colourspace );
  73.                     printf( "bytes per pixel=%d\n", bpp );
  74.                     printf( "width=%d\n", x );
  75.                     printf( "height=%d\n", y );
  76.  
  77.                     buffer = AllocRGBFromJPEG( jph,
  78.                         JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  79.                         TAG_DONE );
  80.                     if ( buffer != NULL )
  81.                     {
  82.                         err = DecompressJPEG( jph,
  83.                             JPG_DestRGBBuffer, buffer,
  84.                             JPG_ProgressHook, progressFunc,
  85.                             JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  86.                             JPG_DCTMethod, DCT_FLOAT,
  87.                             TAG_DONE );
  88.                         if ( !err )
  89.                         {
  90.                             DisplayID = BestCModeIDTags( CYBRBIDTG_NominalWidth, 640,
  91.                                 CYBRBIDTG_NominalHeight, 480,
  92.                                 CYBRBIDTG_Depth, 24,
  93.                                 TAG_DONE );
  94.  
  95.                             if ( DisplayID != INVALID_ID )
  96.                             {
  97.                                 scr = OpenScreenTags( NULL,
  98.                                     SA_Title, "Proof",
  99.                                     SA_DisplayID, DisplayID,
  100.                                     SA_Depth, GetCyberIDAttr( CYBRIDATTR_DEPTH, DisplayID ),
  101.                                     TAG_DONE );
  102.  
  103.                                 if ( scr != NULL )
  104.                                 {
  105.                                     win = OpenWindowTags( NULL,
  106.                                         WA_Title, "Proof",
  107.                                         WA_Flags, WFLG_ACTIVATE | WFLG_SIMPLE_REFRESH |
  108.                                             WFLG_SIZEGADGET | WFLG_RMBTRAP | WFLG_DRAGBAR |
  109.                                             WFLG_DEPTHGADGET | WFLG_CLOSEGADGET,
  110.                                         WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  111.                                             IDCMP_SIZEVERIFY | IDCMP_NEWSIZE | IDCMP_RAWKEY,
  112.                                         WA_Left, 16,
  113.                                         WA_Top, scr->BarHeight+16,
  114.                                         WA_Width, x,
  115.                                         WA_Height, y,
  116.                                         WA_CustomScreen, scr,
  117.                                         TAG_DONE );
  118.  
  119.                                     if ( win != NULL )
  120.                                     {
  121.                                         UBYTE format = RECTFMT_RGB;
  122.                                         UWORD rowwidth = x * 3;
  123.  
  124.                                         switch ( colourspace )
  125.                                         {
  126.                                             case JPCS_UNKNOWN:
  127.                                             case JPCS_GRAYSCALE:
  128.                                                 format = RECTFMT_GREY8;
  129.                                                 rowwidth = x * bpp;
  130.                                             break;
  131.                                         }
  132.  
  133.                                         WritePixelArray( buffer, 0, 0, rowwidth, win->RPort, 0, 0, x, y, format );
  134.  
  135.                                         Wait( 1L << win->UserPort->mp_SigBit );
  136.                                         while ( ( msg = GetMsg( win->UserPort ) ) != NULL ) ReplyMsg( msg );
  137.  
  138.                                         CloseWindow( win );
  139.                                     }
  140.                                     else printf( "failed to open window\n" );
  141.  
  142.                                     CloseScreen( scr );
  143.                                 }
  144.                                 else printf( "failed to open screen\n" );
  145.                             }
  146.                             else printf( "failed to get display id\n" );
  147.                         }
  148.                         else printf( "decompress jpeg error:%d\n", err );
  149.  
  150.                         FreeJPEGRGBBuffer( buffer );
  151.                     }
  152.                     else printf( "cant allocate rgb buffer\n" );
  153.                 }
  154.                 else printf( "get jpeg info error:%d\n", err );
  155.  
  156.                 FreeJPEGDecompress( jph );
  157.             }
  158.             else printf( "alloc jpeg error:%d\n", err );
  159.  
  160.             Close( fp );
  161.         }
  162.         else printf( "cant open file\n" );
  163.     }
  164.  
  165.     if ( JpegBase != NULL ) CloseLibrary( JpegBase );
  166.     if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
  167.     if ( CyberGfxBase ) CloseLibrary ( (struct Library *)CyberGfxBase );
  168. }
  169.  
  170. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata )
  171. {
  172.     static int prevpercent = 0;
  173.  
  174.     int percent = ( curr * 100 ) / lines;
  175.  
  176.     if ( prevpercent != percent )
  177.     {
  178.         if ( percent % 10 == 0 ) printf( "%d%%\n", percent );
  179.     }
  180.  
  181.     prevpercent = percent;
  182.  
  183.     return NULL;
  184. }
  185.