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

  1. /* This example use of jpeg.library loads the jpeg file specified
  2.         on the command line and saves a version halved in size to
  3.         'RAM:halved.jpg'. It uses RGB triplet buffer tags to store/supply the
  4.         image data to/from jpeg.library. This example uses AmigaDOS file
  5.         pointers for the source/destination image streams. It also demonstrates
  6.         the use of a progress indicator callback hook.
  7.  
  8.         Does NOT support greyscale jpegs (not implemented in this application
  9.         but jpeg.library supports them).
  10. */
  11.  
  12. #include <stdio.h>
  13.  
  14. #include <dos/dos.h>
  15. #include <exec/memory.h>
  16. #include <exec/types.h>
  17.  
  18. #include <clib/dos_protos.h>
  19. #include <clib/exec_protos.h>
  20.  
  21. #include <pragmas/dos_pragmas.h>
  22. #include <pragmas/exec_pragmas.h>
  23.  
  24. #include "jpeg/jpeg.h"
  25. #include "jpeg/jpeg_protos.h"
  26. #include "jpeg/jpeg_pragmas.h"
  27.  
  28. /* Function prototypes */
  29. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata );
  30.  
  31. extern struct Library *DOSBase;
  32. struct Library *JpegBase;
  33.  
  34. void main( int argc, char **argv )
  35. {
  36.     JpegBase = OpenLibrary( "jpeg.library", 2 );
  37.     if ( JpegBase != NULL )
  38.     {
  39.         ULONG err;
  40.         struct JPEGDecHandle *jph;
  41.         struct JPEGComHandle *jpc;
  42.         UBYTE *jstream, *cstream;
  43.         ULONG jstreamsize, cstreamsize;
  44.         UBYTE *buffer;
  45.         ULONG x, y;
  46.         BPTR fp, lock, out;
  47.         struct FileInfoBlock *fib;
  48.         UBYTE colourspace;
  49.  
  50.         fib = AllocDosObject( DOS_FIB, TAG_DONE );
  51.         if ( fib != NULL )
  52.         {
  53.             lock = Lock( argv[1], ACCESS_READ );
  54.             if ( lock != NULL )
  55.             {
  56.                 if ( Examine( lock, fib ) )
  57.                 {
  58.                     jstreamsize = fib->fib_Size;
  59.  
  60.                     jstream = AllocVec( jstreamsize, MEMF_PUBLIC | MEMF_CLEAR );
  61.                     if ( jstream != NULL )
  62.                     {
  63.                         fp = OpenFromLock( lock );
  64.                         if ( fp != NULL )
  65.                         {
  66.                             /* Spool file into memory */
  67.                             Read( fp, jstream, jstreamsize );
  68.  
  69.                             Close( fp );
  70.  
  71.                             err = AllocJPEGDecompress( &jph,
  72.                                 JPG_SrcMemStream, jstream,
  73.                                 JPG_SrcMemStreamSize, jstreamsize,
  74.                                 TAG_DONE );
  75.                             if ( !err )
  76.                             {
  77.                                 err = GetJPEGInfo( jph,
  78.                                     JPG_Width, &x, JPG_Height, &y,
  79.                                     JPG_ColourSpace, &colourspace,
  80.                                     JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  81.                                     TAG_DONE );
  82.                                 if ( !err )
  83.                                 {
  84.                                     printf( "colourspace=%d\n", colourspace );
  85.                                     printf( "scaled by half to %dx %d\n", x, y );
  86.  
  87.                                     buffer = AllocRGBFromJPEG( jph,
  88.                                         JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  89.                                         TAG_DONE );
  90.                                     if ( buffer != NULL )
  91.                                     {
  92.                                         err = DecompressJPEG( jph,
  93.                                             JPG_DestRGBBuffer, buffer,
  94.                                             JPG_ProgressHook, progressFunc,
  95.                                             JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  96.                                             TAG_DONE );
  97.                                         if ( !err )
  98.                                         {
  99.                                             err = AllocJPEGCompress( &jpc,
  100.                                                 JPG_DestMemStream, &cstream,
  101.                                                 JPG_DestMemStreamSize, &cstreamsize,
  102.                                                 TAG_DONE );
  103.                                             if ( !err )
  104.                                             {
  105.                                                 err = CompressJPEG( jpc,
  106.                                                     JPG_SrcRGBBuffer, buffer,
  107.                                                     JPG_Width, x, JPG_Height, y,
  108.                                                     TAG_DONE );
  109.                                                 if ( !err )
  110.                                                 {
  111.                                                     out = Open( "ram:halved.jpg", MODE_NEWFILE );
  112.                                                     if ( out != NULL )
  113.                                                     {
  114.                                                         printf( "jpeg saved ok\n" );
  115.  
  116.                                                         Write( out, cstream, cstreamsize );
  117.  
  118.                                                         Close( out );
  119.                                                     }
  120.                                                 }
  121.                                                 else printf( "comrpess jpeg error:%d\n", err );
  122.  
  123.                                                 if ( cstream != NULL ) FreeVec( cstream );
  124.  
  125.                                                 FreeJPEGCompress( jpc );
  126.                                             }
  127.                                             else printf( "alloc jpeg error:%d\n", err );
  128.                                         }
  129.                                         else printf( "descompress jpeg error:%d\n", err );
  130.  
  131.                                         FreeJPEGRGBBuffer( buffer );
  132.                                     }
  133.                                     else printf( "cant allocate rgb buffer\n" );
  134.                                 }
  135.                                 else printf( "get jpeg info error:%d\n", err );
  136.  
  137.                                 FreeJPEGDecompress( jph );
  138.                             }
  139.                             else printf( "alloc jpeg error:%d\n", err );
  140.                         }
  141.                         else printf( "cant open file from lock\n" );
  142.  
  143.                         FreeVec( jstream );
  144.                     }
  145.                     else printf( "cant allocate jstream buffer\n" );
  146.  
  147.                 }
  148.                 else printf( "cant examine file\n" );
  149.  
  150.                 UnLock( lock );
  151.             }
  152.             else printf( "cant open file\n" );
  153.  
  154.             FreeDosObject( DOS_FIB, fib );
  155.         }
  156.         else printf( "cant allocate fib\n" );
  157.     }
  158.  
  159.     if ( JpegBase != NULL ) CloseLibrary( JpegBase );
  160. }
  161.  
  162. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata )
  163. {
  164.     static int prevpercent = 0;
  165.  
  166.     int percent = ( curr * 100 ) / lines;
  167.  
  168.     if ( prevpercent != percent )
  169.     {
  170.         if ( percent % 10 == 0 ) printf( "%d%%\n", percent );
  171.     }
  172.  
  173.     prevpercent = percent;
  174.  
  175.     return NULL;
  176. }
  177.