/* wasp - copyright Steven Reiz 1990, 1991
 * see wasp.c for further info,
 * formats/srgr.doc for a description of the SRGR format
 * raw.c, 4/12/90 - 3/1/91,
 * 5/5/91 - 23/6/91, 8/7/91
 */

#include "wasp.h"
#ifndef NOSH
#include "raw.sh"
#endif

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

#ifdef __STDC__
read_srgr(void)
#else
read_srgr()
#endif
{
    short y;
    long t;

    cread(&t, 4);
    if (t!=RAW_ID) {
        lseek(infd, 0L, 0);
        return 0;
    }
    cread(&xsz, sizeof(xsz));
    cread(&ysz, sizeof(ysz));
    printf("SRGR input; %ld x %ld\n", xsz, ysz);
    fflush(stdout);
    if (!outfilename)
	exit(0);
    rgb=Malloc(ysz*sizeof(u_short *));
    init_counter(0, (int)ysz, 20, NULL);
    for (y=0; y<ysz; ++y) {
        counter();
        rgb[y]=Malloc(xsz*sizeof(u_short));
        cread(rgb[y], xsz*sizeof(u_short));
    }
    erase_counter(NULL);
    return 1;
}


#ifdef __STDC__
write_srgr(void)
#else
write_srgr()
#endif
{
    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, NULL);
    for (y=0; y<ysz; ++y) {
        counter();
        cwrite(rgb[y], xsz*sizeof(u_short));
    }
    erase_counter("SRGR file written, %ld bytes", lseek(outfd, 0L, 1));
}
