Mode7multiplexer.c

From Uzebox Wiki
Jump to navigation Jump to search
#include <stdio.h>

FILE *soundFile;
FILE *pictureFile;
FILE *resultFile;
unsigned char currentBlock[512];

void clearCurrentBlock(){
  int i;
  for(i = 0; i < 512; i++){
    currentBlock[i] = 0;
  }
}


int main(int argc, char *argv[]){
  int fileNumber;
  int blockNumber = 0;
  int x, y;
  int currentPixel;
  int c;

  if(argc <= 3){
    printf("usage: %s resultFile soundfile frame1 [frame2 [...]]", argv[0]);
    return -1;
  }

  resultFile = fopen(argv[1], "w");
  if(resultFile == 0){
    printf("Couldn't write to file %s", argv[1]);
    return -1;
  }

  soundFile = fopen(argv[2], "r");
  if(soundFile == 0){
    printf("Couldn't read the file %s", argv[2]);
    fclose(resultFile);
    return -1;
  }
  //for wave files the header takes 44 bytes
  fseek(soundFile, 44, SEEK_SET);

  for(fileNumber = 3; fileNumber < argc; fileNumber++){
    clearCurrentBlock();
    fread(currentBlock, sizeof(unsigned char), 262, soundFile);
    fwrite(currentBlock, sizeof(unsigned char), 512, resultFile);

    pictureFile = fopen(argv[fileNumber], "r");
    if(pictureFile == 0){
      printf("Couldn't read the file %s", argv[fileNumber]);
      break;
    }
    //for the ppm files I had the header took 15 bytes
    fseek(pictureFile, 15, SEEK_SET);

    for(y = 0; y < 38; y++){
      clearCurrentBlock();
      for(x = 0; x < 510; x++){
        c = fgetc(pictureFile);
        currentPixel = (c >> 5) & 0x07;
        c = fgetc(pictureFile);
        currentPixel |= (c >> 2) & 0x38;
        c = fgetc(pictureFile);
        currentPixel |= c & 0xC0;
        currentBlock[x] = (unsigned char)currentPixel;
      }
      
      fwrite(currentBlock, sizeof(unsigned char), 512, resultFile);
    }

    fclose(pictureFile);
  }

  fclose(resultFile);
  fclose(soundFile);
  return 0;
}