/* DgIFF.c */

/* Part of DgvCon by Jim Omura */

/* Converts one Imagewise line to 6 bit planes of IFF/ILBM.
 * An Imagewise line has 256 pixels.  That will convert to
 * 16 words (each with one bit from each of 16 pixels)
 * per bit plane.  The lowest order bit plane is stored
 * first.
 */

#include "dgvcon.h"

toiff()
{

    extern int      bright;             /* Brightness */
    extern int      contrast;
    extern int      inlen;              /* Input buffer length */
    extern int      planes;             /* Bit Planes */
    extern int      white;              /* White value */

    extern unsigned char inbuf[640];     /* Input File buffer */
    extern USHORT        outbuf[320];     /* Output File buffer */
    extern IFFBMHDR hdrstr;

    int             blkcntr;
    int             blklim;    /* Number of 16 Pixel blocks per line */
    int             plcntr;    /* Bit plane counter */
    int             ccntr;
    int             greyval;
    register USHORT iffword;
    char            *inbyte;   /* Input Byte pointer */
    int             skipcntr;  /* Unused bitplanes to be skipped */

/* Adjust Grey Scale: */

    if((contrast != 100) || (bright != 0))
    {

        ccntr = 0;
        inbyte = (char *)inbuf;
        for(;;)
        {
            if (ccntr == inlen)
            {
                break;
            }
            greyval = ((((unsigned) *inbyte) + bright) * contrast) / 100;

            if (greyval > white)  /* Boundary Checking */
            {
                greyval = white;
            }
            else
            {
                if (greyval < 0)
                {
                    greyval = 0;
                }
            }

            *inbyte = (char) greyval;
            ++inbyte;                 /* Next byte */
            ++ccntr;

        }   /* Endloop Adjust Brightness & Contrast */

    }   /* Endif not default brightness or contrast */

/* IFF Conversion */

/* Skip unused Bitplanes: */
    ccntr = 0;
    skipcntr = 8 - planes;
    if (skipcntr > 0)
    {
        for(;;)
        {
            if(ccntr == inlen)
            {
                break;
            }
            inbuf[ccntr] = inbuf[ccntr] >> skipcntr;
            ++ccntr;

        }   /* Endloop Adjust bytes */

    }   /* Endif Skip Bitplanes */

/* Convert line to IFF: */

    blklim = hdrstr.bm_w / 16; /* 320 line is 20, 640 line is 40 */
    plcntr = 0;
    for(;;)
    {
        if(plcntr == planes)
        {
            break;
        }   /* Endif */

        blkcntr = 0;
        inbyte = (char *)inbuf;
        for (;;)
        {

            if(blkcntr == blklim)
            {
                break;
            }   /* Endif */

            ccntr = 0;
            for(;;)
            {

                if (ccntr == 16)
                {
                    break;
                }   /* Endif */
                iffword = (iffword << 1);
                iffword = (iffword + (*inbyte & 1));
                (*inbyte) = (*inbyte >> 1);
                ++ccntr;
                ++inbyte;             /* Point to next Input byte */
            }   /* Endloop convert block */

            outbuf[(blklim * plcntr) + blkcntr] = iffword;
            ++blkcntr;

        }   /* Endloop convert one bit plane */

        ++plcntr;

    }   /* Endloop convert Imagewise line */

}   /* End of toiff() */

/* End of dgiff.c */
