/*->c.code */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>
#include <stdarg.h> 

#include "h.os"
#include "h.swis"


#include "h.buffer"


#include "h.tiff"


#include "h.code"


#include "h.g4"






static os_error * cputeop(codestr * cx)
{
 os_error * err;

 err=g4j_encode_EOFB(cx->bf);

 return(err);
}



static os_error * cputline(codestr * cx)
{
 os_error * err;

 err=g4j_encode_new_row(cx->bf); 
 cx->line++;

 return(err);
}


os_error * codeblank(codestr * cx)
{
 g4j_encode_white(cx->bf,cx->width);
 return(cputline(cx));
}



os_error * codefinit(codestr * cx)
{
 os_error * err;

 err=g4j_finit();

 return(NULL);

 cx=cx;
}


os_error * codeinit(codestr * cx)
{
 os_error * err;

 codefinit(cx);

 cx->line=0;

 err=g4j_initialize(cx->putbyte,cx->width);

 cx->putline=(putlinefn)cputline;
 cx->puteop=(puteopfn)cputeop;

 return(err);
}



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

void codebitstart(codestr * cx,int left)
{
 cx->colour=0;
 cx->column=left;
 cx->crun=left;
}


void codebitend(codestr * cx)
{
 if(cx->colour)    /* we are writing black */
 {
  g4j_encode_black(cx->bf,cx->crun);
  if(cx->column<cx->width) g4j_encode_white(cx->bf,cx->width-cx->column);
 }
 else              /* we are writing white */
 {
  if(cx->column<cx->width) cx->crun+=cx->width-cx->column;
  g4j_encode_white(cx->bf,cx->crun);
 }
}


void codebit(codestr * cx,int bit)
{
 if(cx->column<cx->width)
 {
  if(bit==cx->colour) cx->crun++;
  else
  {
   if(cx->colour) g4j_encode_black(cx->bf,cx->crun);
   else           g4j_encode_white(cx->bf,cx->crun);

   cx->crun=1;
   cx->colour=bit;
  }
  cx->column++;
 }
}

