home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 117 / af117sub.adf / jpeglibrary.lzx / jpeglibrary / examples / save / save_mem.c < prev    next >
C/C++ Source or Header  |  1998-09-22  |  4KB  |  130 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 de/compress hooks to store/supply the image
  4.         data to/from jpeg.library. This example uses memory based jpeg
  5.         streams for the source/destination images.
  6.  
  7.         Does NOT support greyscale jpegs (not implemented in this application
  8.         but jpeg.library supports them).
  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.  
  19. #include <pragmas/dos_pragmas.h>
  20. #include <pragmas/exec_pragmas.h>
  21.  
  22. #include "jpeg/jpeg.h"
  23. #include "jpeg/jpeg_protos.h"
  24. #include "jpeg/jpeg_pragmas.h"
  25.  
  26. /* Function prototypes */
  27. __saveds __asm storeline( register __a0 void *scanline, register __d0 ULONG line, register __d1 ULONG bytes, register __a1 void *userdata );
  28. __saveds __asm getline( register __a0 UBYTE **scanline, register __d0 ULONG line, register __d1 ULONG width, register __a1 void *userdata );
  29.  
  30. extern struct Library *DOSBase;
  31. struct Library *JpegBase;
  32.  
  33. void main( int argc, char **argv )
  34. {
  35.     JpegBase = OpenLibrary( "jpeg.library", NULL );
  36.     if ( JpegBase != NULL )
  37.     {
  38.         ULONG err;
  39.         struct JPEGDecHandle *jph;
  40.         struct JPEGComHandle *jpc;
  41.         UBYTE *buffer;
  42.         ULONG x, y;
  43.         BPTR fp, out;
  44.         UBYTE colourspace;
  45.  
  46.         fp = Open( argv[1], MODE_OLDFILE );
  47.         out = Open( "ram:halved.jpg", MODE_NEWFILE );
  48.         if ( fp != NULL && out != NULL )
  49.         {
  50.             err = AllocJPEGDecompress( &jph, JPG_SrcFile, fp, TAG_DONE );
  51.             if ( !err )
  52.             {
  53.                 err = GetJPEGInfo( jph,
  54.                     JPG_Width, &x, JPG_Height, &y,
  55.                     JPG_ColourSpace, &colourspace,
  56.                     JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  57.                     TAG_DONE );
  58.                 if ( !err )
  59.                 {
  60.                     printf( "colourspace=%d\n", colourspace );
  61.                     printf( "scaled by half to %dx %d\n", x, y );
  62.  
  63.                     buffer = AllocRGBFromJPEG( jph,
  64.                         JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  65.                         TAG_DONE );
  66.                     if ( buffer != NULL )
  67.                     {
  68.                         err = DecompressJPEG( jph, JPG_DecompressHook, storeline,
  69.                             JPG_DecompressUserData, buffer,
  70.                             JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  71.                             TAG_DONE );
  72.                         if ( !err )
  73.                         {
  74.                             err = AllocJPEGCompress( &jpc, JPG_DestFile, out, TAG_DONE );
  75.                             if ( !err )
  76.                             {
  77.                                 err = CompressJPEG( jpc,
  78.                                     JPG_CompressHook, getline,
  79.                                     JPG_CompressUserData, buffer,
  80.                                     JPG_Width, x, JPG_Height, y,
  81.                                     TAG_DONE );
  82.                                 if ( !err )
  83.                                 {
  84.                                     printf( "jpeg saved ok\n" );
  85.                                 }
  86.                                 else printf( "compress jpeg error:%d\n", err );
  87.  
  88.                                 FreeJPEGCompress( jpc );
  89.                             }
  90.                             else printf( "alloc jpeg error:%d\n", err );
  91.                         }
  92.                         else printf( "decompress jpeg error:%d\n", err );
  93.  
  94.                         FreeJPEGRGBBuffer( buffer );
  95.                     }
  96.                     else printf( "cant allocate rgb buffer\n" );
  97.                 }
  98.                 else printf( "get jpeg info error:%d\n", err );
  99.  
  100.                 FreeJPEGDecompress( jph );
  101.             }
  102.             else printf( "alloc jpeg error:%d\n", err );
  103.         }
  104.         else printf( "cant open source or target file\n" );
  105.  
  106.         if ( fp != NULL ) Close( fp );
  107.         if ( out != NULL ) Close( out );
  108.     }
  109.  
  110.     if ( JpegBase != NULL ) CloseLibrary( JpegBase );
  111. }
  112.  
  113. __saveds __asm storeline( register __a0 void *scanline, register __d0 ULONG line, register __d1 ULONG bytes, register __a1 void *userdata )
  114. {
  115.     UBYTE *buffer = userdata;
  116.  
  117.     CopyMem( scanline, buffer + ( ( line - 1 ) * bytes ), bytes );
  118.  
  119.     return NULL;
  120. }
  121.  
  122. __saveds __asm getline( register __a0 UBYTE **scanline, register __d0 ULONG line, register __d1 ULONG width, register __a1 void *userdata )
  123. {
  124.     UBYTE *buffer = userdata;
  125.  
  126.     *scanline = &buffer[ width * 3 * line ];
  127.  
  128.     return NULL;
  129. }
  130.