#include <stdio.h>

#define AY(a,y) ( ((a<<8) & 0xf000) | ((y<<4) & 0xf00) | (y & 0xf0) | ((y>>4) & 0xf) )


int main(int argc, char **argv) {
  FILE *fpina, *fpiny, *fpout;
  int A,Y,x,y;
  unsigned short o;
  
  if(argc<4) {
    printf("Syntax: %s <in.alpha> <in.lum> <out.16>\r\n", argv[0]);
    return 0;
  }
  
  fpina = fopen(argv[1], "rb");
  fpiny = fopen(argv[2], "rb");
  fpout = fopen(argv[3], "wb");

  if(!fpina)  { printf("%s: Unable to open '%s'\r\n", argv[0], argv[1]); return -1; } 
  if(!fpiny)  { printf("%s: Unable to open '%s'\r\n", argv[0], argv[2]); return -1; } 
  if(!fpout) { printf("%s: Unable to open '%s'\r\n", argv[0], argv[3]); return -1; } 
   
  for(y = 0; y<272; y++) {
    for(x = 0; x<480; x++) {
      A = fgetc(fpina);
      Y = fgetc(fpiny);
      o = AY(A,Y);
      fwrite(&o, 1, 2, fpout);
    }
  }
  
  fclose(fpina);
  fclose(fpiny);
  fclose(fpout);

  return 0;
}

