/* wasp - Copyright 1991 by Steven Reiz
 * see COPYING and wasp.c for further info,
 * raw.c, 4/12/90 - 3/1/91, 5/5/91 - 23/6/91, 8/7/91,
 * 8/12/91, 27/12/91
 */

static char *sourcefile=__FILE__;

#include "wasp.h"

#define RAW_ID id('S', 'R', 'G', 'R')

read_srgr(void)
{
    short y;
    long t;

    cread(&t, 4);
    if (t!=RAW_ID) {
        cseek_in(0L, 0);
        return 0;
    }
    cread(&xsz, (int)sizeof(xsz));
    cread(&ysz, (int)sizeof(ysz));
    printe("SRGR input; %ld x %ld\n", xsz, ysz);
    if (!outfilename)
	exit(0);
    cread_type=CREAD_NONFATAL;
    rgb=Malloc(ysz*sizeof(u_short *));
    init_counter(0, (int)ysz, 20, "reading SRGR");
    for (y=0; y<ysz; ++y)
        rgb[y]=Calloc(xsz*sizeof(u_short));
    for (y=0; y<ysz; ++y) {
        counter();
        cread(rgb[y], (int)(xsz*sizeof(u_short)));
	if (cread_result!=xsz*sizeof(u_short))
	    break;
    }
    erase_counter(NULL);
    cread_type=CREAD_STRICT;
    return 1;
}


void
write_srgr(void)
{
    short y;

    printe("SRGR output; %ld x %ld\n", xsz, ysz);
    wrl((long)RAW_ID);
    wrl(xsz);
    wrl(ysz);
    init_counter(0, (int)ysz, 20, "writing SRGR");
    for (y=0; y<ysz; ++y) {
        counter();
        cwrite(rgb[y], xsz*sizeof(u_short));
    }
    erase_counter("SRGR file written, %ld bytes", cseek_out(0L, 1));
}
