/*	This example use of jpeg.library loads the jpeg file specified
		on the command line and shows any density information stored in
		the jpeg stream.
*/

#include <stdio.h>

#include <dos/dos.h>
#include <exec/types.h>

#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/cybergraphics_protos.h>
#include <clib/intuition_protos.h>

#include <pragmas/dos_pragmas.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/intuition_pragmas.h>

#include <jpeg/jpeg.h>
#include <jpeg/jpeg_protos.h>
#include <jpeg/jpeg_pragmas.h>

/* Function prototypes */
__saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata );

extern struct Library *DOSBase;
struct Library *JpegBase, *IntuitionBase;

char *version = "$VER: readdensity 1.0 (11.2.2000)";

void main( int argc, char **argv )
{
	if ( argv[1] )
	{
		JpegBase = OpenLibrary( "jpeg.library", 6 );
		IntuitionBase = OpenLibrary( "intuition.library", NULL );

		if ( IntuitionBase != NULL && JpegBase != NULL )
		{
			ULONG err;
			struct JPEGDecHandle *jph;
			BPTR fp;
			LONG xdensity, ydensity;
			BYTE densityunits;

			fp = Open( argv[1], MODE_OLDFILE );
			if ( fp != NULL )
			{
				err = AllocJPEGDecompress( &jph,
					JPG_SrcFile, fp,
					JPG_ReadMarker, M_ALL,
					TAG_DONE );
				if ( !err )
				{
					err = GetJPEGInfo( jph,
						JPG_DensityUnits, &densityunits,
						JPG_XDensity, &xdensity,
						JPG_YDensity, &ydensity,
						TAG_DONE );
					if ( !err )
					{
						if ( densityunits != DENSITYUNITS_UNSPECIFIED )
						{
							printf( "density units=%d\n", densityunits );
							printf( "x density=%d\n", xdensity );
							printf( "y density=%d\n", ydensity );
						}
						else printf( "no density information\n" );
					}
					else printf( "get jpeg info error:%d\n", err );

					FreeJPEGDecompress( jph );
				}
				else printf( "alloc jpeg error:%d\n", err );

				Close( fp );
			}
			else printf( "cant open file\n" );
		}

		if ( JpegBase != NULL ) CloseLibrary( JpegBase );
		else printf( "you need jpeg.library v6.0 minimum\n" );

		if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
	}
	else
	{
		printf( "no source file specified\n" );
	}
}
