/*****************************************************************************
 *           Copyright (C) 1986, =Robert J. Mical=
 *                Placed in the Public Domain
 ****************************************************************************/
#include "iffwriter.h"

extern struct   RastPort      *rp;
extern struct   ViewPort      *vp;
 
extern struct   Window  *w;
extern struct   Screen  *screen;
 
extern struct NewScreen ns;

LONG PictModes[3] =
    {
    CAMG,
    4,
    NULL,
    };

LONG PictBody[2] =
    {
    BODY,
    40000,
    };


struct PaintingHeader PaintingHeader =
    {
    FORM,               /* IFFID */
    (40156),            /* file length */
    ILBM,               
    BMHD,               
    sizeof(struct BitMapHeader),
        {               /* start of the BitMapHeader structure */
        320, 200,
        0, 0,
        5,
        0, 
        0,
        0,
        0,
        10, 11,
        320, 200,       /* end of BitMapHead */
        },
    CMAP,               
    96,                 /* this value is followed by 32 color registers */
};

BOOL SavePicture()
{
    SHORT i, j, offset;
    ULONG length, actual_length, file;
    UBYTE *p, component, rgbbyte;
    USHORT rgb;
    BOOL written;
    char filename[31],newname[31];
    int error,n;

    written = FALSE;
    strcpy(filename,"temp.ilbm");

    /* Open the data file */
    file = Open(filename,MODE_NEWFILE);
    if (file == NULL)
        /* Couldn't create temp file */
        goto EXIT_ERROR;

    /***********************************************************************
     * to write the buffer to the disk, we need to write:
     *      "FORM"
     *      file length
     *      "ILBM"
     *      "BMHD"
     *      BMHD length
     *      BitMapHeader structure
     *      "CMAP"
     *      CMAP length
     *      colors
     *      pad byte if needed
     *      "BODY"
     *      BODY length
     *      body data
     **********************************************************************/

     /* First, write out the header */
    length = sizeof(struct PaintingHeader);
    actual_length = Write(file, &PaintingHeader, length);
    if (actual_length != length)
        goto WRITE_ERROR;

    /* also, enough bytes for each color register */
    for (i = 0; i < (1 << ns.Depth); i++)
        {
        for (component = 0; component < 3; component++)
            {
            rgb = (GetRGB4(vp->ColorMap, i));
            rgbbyte = (rgb >> (4 * (2 - component)));
            rgbbyte <<= 4;
            actual_length = Write(file, &rgbbyte, 1);
            if (actual_length != 1)
                goto WRITE_ERROR;
            }
        }
    actual_length = Write(file,  &PictBody[0], 8);
    if (actual_length != 8)
        goto WRITE_ERROR;

    /* Finally, the data (uncompressed for now). */
    /* First, get the bytelength per line */
    length = (((ns.Width + 15) >> 4) << 1);

    /* For every line ... */
    for (j = 0; j < ns.Height; j++)
        {
        offset = length * j;
        /* For every bit plane ... */
        for (i = 0; i < ns.Depth; i++)
            {
            /* p is set to the start of the line in this bit plane */
            p = (screen->BitMap.Planes[i]) + offset;
            actual_length = Write(file, p, length);
            if (actual_length != length) goto WRITE_ERROR;
            }
        }

    PictModes[2] = vp->Modes;
    actual_length = Write(file,  &PictModes[0], 12);
    if (actual_length != 12)
        goto WRITE_ERROR;
    written = TRUE;

WRITE_ERROR:
    Close(file);

EXIT_ERROR:
    if (NOT written)
        {
        Cleanup();
        printf("*** COULDN'T WRITE YOUR FILE! ***\n");
        exit();
        }
    Cleanup();
    printf("\nEnter name for file to be stored under?");
    scanf("%s",newname);
    error = rename(filename,newname);
    if (error==-1){
      printf("\nCouldn't rename temp file\n");
      exit();
    }
    for (n=1;n<=500000;n++)
    ;
    return(written);
}


