pattern_generator.c 4.45 KB
#include <stdio.h>
#include <stdlib.h>
#ifdef __linux__
#include <getopt.h>
#endif

#define DEFAULT_WIDTH  640
#define DEFAULT_HEIGHT 480
#define DEFAULT_FORMAT 0

#define NUM_COLORBARS 8

#define BLACK_16_75    0x0000
#define BLUE_16_75     0x0038
#define GREEN_16_75    0x0700
#define RED_16_75      0xe000
#define CYAN_16_75     (BLUE_16_75 | GREEN_16_75)
#define MAGENTA_16_75  (BLUE_16_75 | RED_16_75)
#define YELLOW_16_75   (GREEN_16_75 | RED_16_75)
#define WHITE_16_75    (BLUE_16_75 | GREEN_16_75 | RED_16_75)

#define BLACK_16_100   0x0000
#define BLUE_16_100    0x003e
#define GREEN_16_100   0x07c0
#define RED_16_100     0xf800
#define CYAN_16_100    (BLUE_16_100 | GREEN_16_100)
#define MAGENTA_16_100 (BLUE_16_100 | RED_16_100)
#define YELLOW_16_100  (GREEN_16_100 | RED_16_100)
#define WHITE_16_100   (BLUE_16_100 | GREEN_16_100 | RED_16_100)

#define BLACK_32_75    0x00000000
#define BLUE_32_75     0x0000c000
#define GREEN_32_75    0x00c00000
#define RED_32_75      0xc0000000
#define CYAN_32_75     (BLUE_32_75 | GREEN_32_75)
#define MAGENTA_32_75  (BLUE_32_75 | RED_32_75)
#define YELLOW_32_75   (GREEN_32_75 | RED_32_75)
#define WHITE_32_75    (BLUE_32_75 | GREEN_32_75 | RED_32_75)

#define BLACK_32_100   0x00000000
#define BLUE_32_100    0x0000ff00
#define GREEN_32_100   0x00ff0000
#define RED_32_100     0xff000000
#define CYAN_32_100    (BLUE_32_100 | GREEN_32_100)
#define MAGENTA_32_100 (BLUE_32_100 | RED_32_100)
#define YELLOW_32_100  (GREEN_32_100 | RED_32_100)
#define WHITE_32_100   (BLUE_32_100 | GREEN_32_100 | RED_32_100)

const int Colorbar_Pattern_16_75[NUM_COLORBARS] = {
  WHITE_16_75,
  YELLOW_16_75,
  CYAN_16_75,
  GREEN_16_75,
  MAGENTA_16_75,
  RED_16_75,
  BLUE_16_75,
  BLACK_16_75,
};

const int Colorbar_Pattern_16_100[NUM_COLORBARS] = {
  WHITE_16_100,
  YELLOW_16_100,
  CYAN_16_100,
  GREEN_16_100,
  MAGENTA_16_100,
  RED_16_100,
  BLUE_16_100,
  BLACK_16_100,
};

const int Colorbar_Pattern_32_75[NUM_COLORBARS] = {
  WHITE_32_75,
  YELLOW_32_75,
  CYAN_32_75,
  GREEN_32_75,
  MAGENTA_32_75,
  RED_32_75,
  BLUE_32_75,
  BLACK_32_75,
};

const int Colorbar_Pattern_32_100[NUM_COLORBARS] = {
  WHITE_32_100,
  YELLOW_32_100,
  CYAN_32_100,
  GREEN_32_100,
  MAGENTA_32_100,
  RED_32_100,
  BLUE_32_100,
  BLACK_32_100,
};


void
main (int argc, char **argv) {

  FILE *file_p;
  int c;
  int scanline;
  int colorbar_width;

  int colorbar_saturation = 100;
  const int *colorbar_pattern = Colorbar_Pattern_16_100;
  int frame_width = DEFAULT_WIDTH;
  int frame_height = DEFAULT_HEIGHT;
  int frame_depth = 16;

  file_p = stdout;

  while ((c = getopt(argc, argv, "w:h:s:d:o:")) != EOF) {
    switch (c) {

    case 'w':
      frame_width = atoi(optarg);
      break;

    case 'h':
      frame_height = atoi(optarg);
      break;

    case 's':
      colorbar_saturation = atoi(optarg);
      break;

    case 'd':
      frame_depth = atoi(optarg);
      break;

    case 'o':
      if ((file_p = fopen(optarg, "w")) == NULL) {
	perror(argv[0]);
	exit(1);
      }
      break;

    default:
      fprintf(stderr, "usage: %s -w width -h height -s saturation -d depth -o file\n",
	      argv[0]);
      exit(1);
    }
  }

  if (colorbar_saturation == 75)
    if (frame_depth == 16)
      colorbar_pattern = Colorbar_Pattern_16_75;
    else if (frame_depth == 32)
      colorbar_pattern = Colorbar_Pattern_32_75;
    else {
      fprintf(stderr, "%s: depth must be either 16 or 32", argv[0]);
      exit(1);
    }
  else if (colorbar_saturation == 100)
    if (frame_depth == 16)
      colorbar_pattern = Colorbar_Pattern_16_100;
    else if (frame_depth == 32)
      colorbar_pattern = Colorbar_Pattern_32_100;
    else {
      fprintf(stderr, "%s: depth must be either 16 or 32", argv[0]);
      exit(1);
    }
  else {
    fprintf(stderr, "%s: saturation must be either 75 or 100", argv[0]);
    exit(1);
  }
  
  fprintf(file_p, "# width  %d\n", frame_width);
  fprintf(file_p, "# height %d\n", frame_height);
  fprintf(file_p, "# depth  %d\n", frame_depth);

  colorbar_width = frame_width / NUM_COLORBARS;
  for (scanline = 0; scanline < frame_height; scanline++) {
    int colorbar;
    int i;
    int unused_width = frame_width % NUM_COLORBARS;

    fprintf(file_p, "# scanline %d\n", scanline);
    for (colorbar = 0; colorbar < NUM_COLORBARS; colorbar++) {
      for (i = 0; i < colorbar_width; i++) {
	fprintf(file_p, "%x\n", colorbar_pattern[colorbar]);
      }
      if (unused_width) {
	fprintf(file_p, "%x\n", colorbar_pattern[colorbar]);
	unused_width--;
      }
    }
  }
  exit(0);
}