/* This example use of imageio.library loads the image file specified
		on the command line and views its attributes
*/

#include <stdio.h>

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

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

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

#include <imageio/imageio.h>
#include <imageio/imageio_protos.h>
#include <imageio/imageio_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 *ImageIOBase, *IntuitionBase;

void main( int argc, char **argv )
{
	ImageIOBase = OpenLibrary( "imageio.library", 2 );
	IntuitionBase = OpenLibrary( "intuition.library", NULL );
	if ( IntuitionBase && ImageIOBase )
	{
		struct ImageHandle *ih;
		ULONG err;
		BPTR fp;

		fp = Open( argv[1], MODE_OLDFILE );
		if ( fp != NULL )
		{
			err = AllocImage( &ih,
				IMG_SrcFile, fp,
				TAG_DONE );
			if ( !err )
			{
				ULONG num = 1, denom = 1;
				ULONG x, y, bpp, rs, attrs[8];
				UBYTE cs, it, depth;
				BOOL encoded;

				err = GetImageAttrs( ih,
					IMG_ImageType, &it,
					TAG_DONE );
				if ( !err )
				{
					printf("Image type=%d\n",it);
				}

				err = GetImageAttrs( ih,
					IMG_Width, &x,
					IMG_Height,&y,
					IMG_BytesPerPixel, &bpp,
					IMG_ColourSpace, &cs,
					IMG_RowSize, &rs,
					IMG_TestScaleNum, num,
					IMG_TestScaleDenom, denom,
					IMG_IsDecoded, &encoded,
					IMG_ImagePlanar, &attrs[0],
					IMG_ImageChunky,&attrs[1],
					IMG_ImageHAM6, &attrs[2],
					IMG_ImageHalfBrite, &attrs[3],
					IMG_ImageHAM8, &attrs[4],
					IMG_Image24Bit, &attrs[5],
					IMG_ImagePalette, &attrs[6],
					IMG_ImageGrey, &attrs[7],
					IMG_ImageDepth, &depth,
					TAG_DONE );
				if ( !err )
				{
					printf( "width=%ld, height=%ld\n", x, y );
					printf( "bytes per pixel=%ld, colourspace=%d\n", bpp, cs );
					printf( "row size=%ld\n", rs );
					printf( "image is decoded %s\n", encoded == TRUE ? "yes" : "no" );

					if ( attrs[0] ) printf( "planar, " );
					if ( attrs[1] ) printf( "chunky, " );
					if ( attrs[2] ) printf( "HAM6 " );
					if ( attrs[3] ) printf( "HALFBRITE " );
					if ( attrs[4] ) printf( "HAM8 " );
					if ( attrs[5] ) printf( "24bit " );
					if ( attrs[6] ) printf( "palette " );
					if ( attrs[7] ) printf( "grey" );
					printf( "\n" );

					printf( "depth=%d\n", depth );
				}
				else printf( "get image attrs error:%d\n", err );

				FreeImage( ih );
			}
			else printf( "alloc image error:%d\n", err );

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

	if ( ImageIOBase ) CloseLibrary( ImageIOBase );
	if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
}

__saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata )
{
	static int prevpercent = 0;

	int percent = ( curr * 100 ) / lines;

	if ( prevpercent != percent )
	{
		if ( percent % 10 == 0 ) printf( "%d%%\n", percent );
	}

	prevpercent = percent;

	return NULL;
}
