/*	This example use of jpeg.library loads the jpeg file specified
		on the command line and shows all attached markers and sizes,
		and displays the contents of M_COM text comment markers, by
		looping manually through the linked list of markers.
*/

#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: loadmarkers 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;
			struct MinNode *work, *next;
			struct MinList *markers;
			struct JPEGMarker *marker;

			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_MarkerList, &markers,
						TAG_DONE );
					if ( !err )
					{
						work = markers->mlh_Head;

						while ( next = work->mln_Succ )
						{
							marker = (struct JPEGMarker *)work;

							if ( marker->mk_Exists )
							{
								printf( "marker=%x\n", marker->mk_ID );
								printf( "\tlength=%ld\n", marker->mk_Length );

								if ( marker->mk_ID == M_COM )
								{
									printf( "\tcomment=%s\n", (char *)marker->mk_Data );
								}
							}

							work = next;
						}
					}
					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" );
	}
}
