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

  1. /* This example use of jpeg.library loads the jpeg file specified
  2.         on the command line and viewsit halved in size on a cybergraphics
  3.         screen. It uses a DecompressHook to store the image data from
  4.         jpeg.library. This example uses memory based jpeg streams for the
  5.         source image.
  6.  
  7.         Supports RGB and Grayscale source jpeg images.
  8. */
  9.  
  10. #include <stdio.h>
  11.  
  12. #include <dos/dos.h>
  13. #include <exec/memory.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. #include <intuition/intuitionbase.h>
  21.  
  22. #include <pragmas/dos_pragmas.h>
  23. #include <pragmas/exec_pragmas.h>
  24. #include <pragmas/intuition_pragmas.h>
  25. #include <pragmas/cybergraphics_pragmas.h>
  26.  
  27. #include <cybergraphics/cybergraphics.h>
  28.  
  29. #include <jpeg/jpeg.h>
  30. #include <jpeg/jpeg_protos.h>
  31. #include <jpeg/jpeg_pragmas.h>
  32.  
  33. /* Function prototypes */
  34. __saveds __asm storeline( register __a0 void *scanline, register __d0 ULONG line, register __d1 ULONG bytes, register __a1 void *userdata );
  35.  
  36. extern struct Library *DOSBase;
  37. struct Library *JpegBase, *IntuitionBase;
  38. struct CyberGfxBase *CyberGfxBase;
  39.  
  40. void main( int argc, char **argv )
  41. {
  42.     JpegBase = OpenLibrary( "jpeg.library", NULL );
  43.     IntuitionBase = OpenLibrary( "intuition.library", NULL );
  44.     CyberGfxBase = (struct CyberGfxBase *)OpenLibrary( "cybergraphics.library", 0L );
  45.  
  46.     if ( IntuitionBase != NULL && CyberGfxBase != NULL && JpegBase != NULL )
  47.     {
  48.         ULONG err;
  49.         ULONG DisplayID;
  50.         struct Screen *scr;
  51.         struct Window *win;
  52.         struct Message *msg;
  53.         struct JPEGDecHandle *jph;
  54.         UBYTE *jstream;
  55.         ULONG jstreamsize;
  56.         UBYTE *buffer;
  57.         ULONG x, y;
  58.         BPTR fp, lock;
  59.         struct FileInfoBlock *fib;
  60.         UBYTE colourspace;
  61.         ULONG bpp;
  62.  
  63.         fib = AllocDosObject( DOS_FIB, TAG_DONE );
  64.         if ( fib != NULL )
  65.         {
  66.             lock = Lock( argv[1], ACCESS_READ );
  67.             if ( lock != NULL )
  68.             {
  69.                 if ( Examine( lock, fib ) )
  70.                 {
  71.                     jstreamsize = fib->fib_Size;
  72.  
  73.                     jstream = AllocVec( jstreamsize, MEMF_PUBLIC | MEMF_CLEAR );
  74.                     if ( jstream != NULL )
  75.                     {
  76.                         fp = OpenFromLock( lock );
  77.                         if ( fp != NULL )
  78.                         {
  79.                             lock = NULL;
  80.  
  81.                             /* Spool file into memory */
  82.                             Read( fp, jstream, jstreamsize );
  83.  
  84.                             Close( fp );
  85.  
  86.                             err = AllocJPEGDecompress( &jph,
  87.                                 JPG_SrcMemStream, jstream,
  88.                                 JPG_SrcMemStreamSize, jstreamsize,
  89.                                 TAG_DONE );
  90.                             if ( !err )
  91.                             {
  92.                                 err = GetJPEGInfo( jph,
  93.                                     JPG_Width, &x, JPG_Height, &y,
  94.                                     JPG_ColourSpace, &colourspace,
  95.                                     JPG_BytesPerPixel, &bpp,
  96.                                     JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  97.                                     TAG_DONE );
  98.                                 if ( !err )
  99.                                 {
  100.                                     printf( "colourspace=%d\n", colourspace );
  101.                                     printf( "bytes per pixel=%d\n", bpp );
  102.                                     printf( "width=%d\n", x );
  103.                                     printf( "height=%d\n", y );
  104.  
  105.                                     buffer = AllocRGBFromJPEG( jph,
  106.                                         JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  107.                                         TAG_DONE );
  108.                                     if ( buffer != NULL )
  109.                                     {
  110.                                         err = DecompressJPEG( jph, JPG_DecompressHook, storeline,
  111.                                             JPG_DecompressUserData, buffer,
  112.                                             JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  113.                                             TAG_DONE );
  114.                                         if ( !err )
  115.                                         {
  116.                                             DisplayID = BestCModeIDTags( CYBRBIDTG_NominalWidth, 640,
  117.                                                 CYBRBIDTG_NominalHeight, 480,
  118.                                                 CYBRBIDTG_Depth, 24,
  119.                                                 TAG_DONE );
  120.  
  121.                                             if ( DisplayID != INVALID_ID )
  122.                                             {
  123.                                                 scr = OpenScreenTags( NULL,
  124.                                                     SA_Title, "Proof",
  125.                                                     SA_DisplayID, DisplayID,
  126.                                                     SA_Depth, GetCyberIDAttr( CYBRIDATTR_DEPTH, DisplayID ),
  127.                                                     TAG_DONE );
  128.  
  129.                                                 if ( scr != NULL )
  130.                                                 {
  131.                                                     win = OpenWindowTags( NULL,
  132.                                                         WA_Title, "Proof",
  133.                                                         WA_Flags, WFLG_ACTIVATE | WFLG_SIMPLE_REFRESH |
  134.                                                             WFLG_SIZEGADGET | WFLG_RMBTRAP | WFLG_DRAGBAR |
  135.                                                             WFLG_DEPTHGADGET | WFLG_CLOSEGADGET,
  136.                                                         WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  137.                                                             IDCMP_SIZEVERIFY | IDCMP_NEWSIZE | IDCMP_RAWKEY,
  138.                                                         WA_Left, 16,
  139.                                                         WA_Top, scr->BarHeight+16,
  140.                                                         WA_Width, x,
  141.                                                         WA_Height, y,
  142.                                                         WA_CustomScreen, scr,
  143.                                                         TAG_DONE );
  144.  
  145.                                                     if ( win != NULL )
  146.                                                     {
  147.                                                         UBYTE format = RECTFMT_RGB;
  148.                                                         UWORD rowwidth = x * 3;
  149.  
  150.                                                         switch ( colourspace )
  151.                                                         {
  152.                                                             case JPCS_UNKNOWN:
  153.                                                             case JPCS_GRAYSCALE:
  154.                                                                 format = RECTFMT_GREY8;
  155.                                                                 rowwidth = x * bpp;
  156.                                                             break;
  157.                                                         }
  158.  
  159.                                                         WritePixelArray( buffer, 0, 0, rowwidth, win->RPort, 0, 0, x, y, format );
  160.  
  161.                                                         Wait( 1L << win->UserPort->mp_SigBit );
  162.                                                         while ( ( msg = GetMsg( win->UserPort ) ) != NULL ) ReplyMsg( msg );
  163.  
  164.                                                         CloseWindow( win );
  165.                                                     }
  166.                                                     else printf( "failed to open window\n" );
  167.  
  168.                                                     CloseScreen( scr );
  169.                                                 }
  170.                                                 else printf( "failed to open screen\n" );
  171.                                             }
  172.                                             else printf( "failed to get display id\n" );
  173.                                         }
  174.                                         else printf( "decompress jpeg error:%d\n", err );
  175.  
  176.                                         FreeJPEGRGBBuffer( buffer );
  177.                                     }
  178.                                     else printf( "cant allocate rgb buffer\n" );
  179.                                 }
  180.                                 else printf( "get jpeg info error:%d\n", err );
  181.  
  182.                                 FreeJPEGDecompress( jph );
  183.                             }
  184.                             else printf( "alloc jpeg error:%d\n", err );
  185.                         }
  186.                         else printf( "cant open from lock\n" );
  187.  
  188.                         FreeVec( jstream );
  189.                     }
  190.                     else printf( "cant allocate jstream buffer\n" );
  191.                 }
  192.                 else printf( "cant examine file\n" );
  193.  
  194.                 UnLock( lock );
  195.             }
  196.             else printf( "cant lock file\n" );
  197.  
  198.             FreeDosObject( DOS_FIB, fib );
  199.         }
  200.         else printf( "cant allocate fib\n" );
  201.     }
  202.  
  203.     if ( JpegBase != NULL ) CloseLibrary( JpegBase );
  204.     if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
  205.     if ( CyberGfxBase ) CloseLibrary ( (struct Library *)CyberGfxBase );
  206. }
  207.  
  208. __saveds __asm storeline( register __a0 void *scanline, register __d0 ULONG line, register __d1 ULONG bytes, register __a1 void *userdata )
  209. {
  210.     UBYTE *buffer = userdata;
  211.  
  212.     CopyMem( scanline, buffer + ( ( line - 1 ) * bytes ), bytes );
  213.  
  214.     return NULL;
  215. }
  216.