/*****************************************************************************

				ASCII_WRITE.C
				Writes GPK ASCII IMAGE format data files.

Copyright (C) 2002 Greg Kochanski, gpk@pribble.com .

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*****************************************************************************/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "gpk_std.h"
#include "gpklib.h"
#include "gpklib/image.h"
#include "rw_misc.h"
#include "rbfile.h"
#include "internals.h"



LOCAL void print_properties(FILE *out, C proplist& prop)
{
	C property *p = prop.first;
	for(; p; p=p->next)	{
		if(!*p->attr || !p->val)
			continue;

		/* Drop duplicates and 'covered' attributes. */
		if(prop.get(p->attr) != p)
			continue;

		print_avc(out, p->attr, p->val, p->comment);
		putc('\n', out);
	}
}


BOOL ascii_writefile(C struct image *p, FILE *out, C proplist& control)
{
 assert(p);
 assert(out);
 assert(p->prop);
 proplist q(*p->prop);
 normalize_proplist(&q, p);
 q += control;
 C int bitpix = q.geti("BITPIX", 0);
 double bscale, bzero;
 scale_image_for_bitpix(p, bitpix, &bscale, &bzero, 0, 0, &q, TRUE);

 q.add("_FILETYPE", (C char *)0);
 gpkio_date_fix(&q);

 print_properties(out, q);
 if(fprintf(out, "\n")<0 || ferror(out))
	return 0;

 return write_out_data(out, p, bitpix,
			NETWORK_ORDER,
			bscale, bzero);
}
