rgb2c.c 3.83 KB


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "readtex.h"

void usage (void)
{
	fprintf (stderr,
"\nusage: rgb2c [options] image.rgb\n"
"\n"
"options:  -m name    Name the texture (defaults to \"texture\").\n"
"          -Q         Quadricate lookup table for color index texture\n"
"          -P         Toggle Padding of texel rows for use in Load Block (default=on)\n"
"          -F         Flip Image Vertically\n"
"          -X         Xtra padding to make all declared objects be 32x bytes long\n"
"          -t citype  Color Index type (C, I)\n"
"          -f fmt     Format of texel (RGBA, YUV, CI, IA, I, A)\n"
"			(defaults to best type for image).\n"
"          -o output  Format of output text (`C' for .c output, `RAW'\n"
"                        for raw ascii format, 'MIP' for MipMapping (.c)\n"
"          -s bits    Size of each texel in bits (4, 8, 16, or 32)\n"
"			(defaults to best type for image).\n"
"          -l r,g,b   \ Specify the low and high colors to interpolate\n"
"          -h r,g,b   / between when making RGB from intensity.\n\n");

	exit (1);
}

int main (int argc, char *argv[])
{
	int		c, lr, lg, lb, hr, hg, hb;
	extern int	optind;
	extern char	*optarg;
	int		fmt, siz, output, ColorIndexType;
	int		flags;
	struct texture	tex;


	strcpy (tex.name, "texture");
	lr = lg = lb = 0;
	hr = hg = hb = 255;
	flags = PAD_FLAG | FLIP_FLAG;	/* Default is ON to correct for SGI format */
	fmt = -1;
	siz = -1;
	output = C;
	ColorIndexType = COLOR;

	while ((c = getopt (argc, argv, "l:h:m:f:s:o:t:FPXQ")) != EOF) {
		switch (c) {
			case 'Q':
				flags ^= QUAD_FLAG;
				break;
			case 'P':
				flags ^= PAD_FLAG;
				break;
			case 'F':
				flags ^= FLIP_FLAG;
				break;
			case 'X':
				flags ^= XTRA_FLAG;
				break;
			case 'm':
				strcpy (tex.name, optarg);
				break;
			case 'f':
				if (strcasecmp (optarg, "RGBA") == 0) {
					fmt = RGBA;
				} else if (strcasecmp (optarg, "IA") == 0) {
					fmt = IA;
				} else if (strcasecmp (optarg, "I") == 0) {
					fmt = I;
				} else if (strcasecmp (optarg, "A") == 0) {
					fmt = A;					
				} else if (strcasecmp (optarg, "CI") == 0) {
					fmt = CI;
				} else if (strcasecmp (optarg, "YUV") == 0) {
					fmt = YUV;
				} else {
					fprintf (stderr, "Unknown format "
						"type: %s.\n", optarg);
					exit (1);
				}
				break;
			case 'o':
				if (strcasecmp (optarg, "C") == 0) {
				        output = C;
                                } else if (strcasecmp (optarg, "RAW") == 0) {
				        output = RAW;
                                } else if (strcasecmp (optarg, "MIP") == 0) {
				        output = MIPMAP;
				} else {
				        fprintf (stderr, "Unknown format "
                                                "type: %s. \n", optarg);
                                        exit(1);
				}
                                break;
			case 't':
				if (strcasecmp(optarg, "C") == 0) {
				       ColorIndexType = COLOR;
				} else if (strcasecmp(optarg, "I") == 0) {
                                       ColorIndexType = INTENSITY;
				} else {
				        fprintf (stderr, "Unknown color index type "
                                                 ": %s. \n", optarg);
				}  
		                break;				
			case 's':
				siz = atoi (optarg);
				break;
			case 'l':
				sscanf (optarg, "%d,%d,%d", &lr, &lg, &lb);
				break;
			case 'h':
				sscanf (optarg, "%d,%d,%d", &hr, &hg, &hb);
				break;
			case '?':
				usage ();
				break;
		}
	}

	if (optind == argc) {
		usage ();
	}

	fprintf (stderr, "Making texture %s\n", tex.name);

	if ((output == C) || (output == MIPMAP) )
	  {
	    printf ("/*\n");
	    printf (" * Do not edit this file.  It was automatically generated\n");
	    printf (" * by \"rgb2c\" from the file \"%s\".\n", argv[optind]);
	    printf (" *\n");
	  }

	readtex (argv[optind], &tex, fmt, siz, 0, lr, lg, lb, hr, hg, hb, output, flags);
}