gl_dev.c 5.22 KB
#include <stdio.h>
#include <string.h>
#include <bstring.h>
#include <gl/gl.h>
#include <gl/device.h>
#include <PRimage.h>

#include "test_image.h"

/*
 *  save_cvg -- save 32b RGBA image, pseudo color version of coverage (alpha) channel
 *
 *
 */
void
  save_cvg(char *name, Image *image)
{
	IMAGE		*img;
	unsigned short	*cb;
	unsigned char   *fb;
	int		x, xl, y, xsize, ysize;

	xsize = image->xsize;
        ysize = image->ysize;

	img = iopen(name, "w", RLE(1), 3, xsize, ysize, 3);
	if (img == (IMAGE *)0) {
		fprintf(stderr, "error in creating image %s\n", name);
		return;
	}

	cb = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
	fb = image->base;

	for (y = 0; y < ysize; y++, fb += image->lsize) {
		for (xl = 0, x = 3; xl < xsize; x += 4, xl++) {
                  if(fb[x] & 0x80)
		    cb[xl] = 0xff;
	          else 
		    cb[xl] = 0x00;
		}
		putrow(img, cb, y, 0);
		for (xl = 0, x = 3; xl < xsize; x += 4, xl++) {
                  if(fb[x] == 0x47)
		    cb[xl] = 0xff;
	          else 
		    cb[xl] = 0x00;
		}
		putrow(img, cb, y, 1);
		for (xl = 0, x = 3; xl < xsize; x += 4, xl++) {
                  if((fb[x] & 7) < 0x7 && (fb[x] & 0x40) && !(fb[x] & 0x80))
		    cb[xl] = 0xff;
	          else 
		    cb[xl] = 0x00;
		}
		putrow(img, cb, y, 2);
	}
	free(cb);
	iclose(img);
}

/*
 *  save_image -- save 32b RGBA frame buffer in 24b .rgb format
 *
 *
 */
void
  save_image(char *name, Image *image)
{
	IMAGE		*img;
	unsigned short	*cb;
	unsigned char   *fb;
	int		x, xl, y, xsize, ysize;

	xsize = image->xsize;
        ysize = image->ysize;

	img = iopen(name, "w", RLE(1), 3, xsize, ysize, 3);
	if (img == (IMAGE *)0) {
		fprintf(stderr, "error in creating image %s\n", name);
		return;
	}

	cb = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
	fb = image->base;

	for (y = 0; y < ysize; y++, fb += image->lsize) {
		for (xl = 0, x = 0; xl < xsize; x += 4, xl++) cb[xl] = fb[x];
		putrow(img, cb, y, 0);
		for (xl = 0, x = 1; xl < xsize; x += 4, xl++) cb[xl] = fb[x];
		putrow(img, cb, y, 1);
		for (xl = 0, x = 2; xl < xsize; x += 4, xl++) cb[xl] = fb[x];
		putrow(img, cb, y, 2);
	}
	free(cb);
	iclose(img);
}

/*
 *  dump_image_header
 *
 */

void
  dump_image_header(char *file)
{
	IMAGE		*img;

	if ((img = iopen(file, "r")) == (IMAGE *)NULL) {
		fprintf(stderr, "can't open texture file %s\n", file);
		exit(1);
	}

	printf("\n%s\n", file);
	printf("imagic = 0x%04x\n", img->imagic);
	printf("type   = 0x%04x\n", img->type);
	printf("dim    = 0x%04x\n", img->dim);
	printf("xsize  = %d\n", img->xsize);
	printf("ysize  = %d\n", img->ysize);
	printf("zsize  = %d\n", img->zsize);
	printf("min    = %d\n", img->min);
	printf("max    = %d\n", img->max);
	printf("waste  = %d\n", img->wastebytes);
	printf("name   = %s\n", img->name);
	printf("clrmap = 0x%08x\n", img->colormap);
	printf("\n");
}

/*
 *  load_image -- recognizes input data in 24/32b RGB/A SGI format
 *   or y/u/v data in separate SGI format files.  Imports these
 *   into either an RGBA image or UVYY image type with 8b per component.
 *   On first call make sure texture pointer is NULL.
 */
Image * 
  load_image(char *file, Image *texture)
{
	IMAGE		*img;
	int		x, xl, y;
	unsigned short	*cp;
	unsigned char   *sp;

	if ((img = iopen(file, "r")) == (IMAGE *)NULL) {
		fprintf(stderr, "can't open texture file %s\n", file);
		exit(1);
	}

	if(texture == (Image *)NULL) {
	  texture = (Image *)malloc(sizeof(Image));
	  texture->xsize = img->xsize;
	  texture->ysize = img->ysize;
	  texture->type  = 0;
	  texture->lsize = texture->xsize * 4;
	  texture->base = (unsigned char *)malloc(texture->ysize * texture->lsize);
	  bzero(texture->base, texture->lsize * texture->ysize);
	}

	cp = (unsigned short *)malloc(texture->xsize * sizeof(unsigned short));
	sp = texture->base;

	if((strstr(file, ".rgb")) != NULL)
	{
	  /*
	   * read in 24b or 32b pixels
	   */
	  for (y = 0; y < texture->ysize; y++, sp += texture->lsize) {
		getrow(img, cp, y, 0);
		for (xl = x = 0; xl < img->xsize; x += 4, xl++) sp[x] = cp[xl] & 0xff;
		getrow(img, cp, y, 1);
		for (xl = 0, x = 1; xl < img->xsize; x += 4, xl++) sp[x] = cp[xl] & 0xff;
		getrow(img, cp, y, 2);
		for (xl = 0, x = 2; xl < img->xsize; x += 4, xl++) sp[x] = cp[xl] & 0xff;
		if (img->zsize > 3) {
			getrow(img, cp, y, 3);
			for (xl = 0, x = 3; xl < img->xsize; x += 4, xl++) 
                          sp[x] = cp[xl] & 0xff;
		}
		else {
			for (xl = 0, x = 3; xl < img->xsize; x += 4, xl++)
				if (sp[x]) sp[x] = 0xff;
		}

	  }
	}
	else if((strstr(file, ".y")) != NULL)
	{
	  for (y = 0; y < texture->ysize; y++, sp += texture->lsize) 
	  {
		getrow(img, cp, y, 0);
		for (xl = 0, x = 2; xl < img->xsize; x += 4, xl++) 
		{
		  sp[x] = cp[xl] & 0xff;
		  sp[x+1] = cp[xl] & 0xff;
		}
	  }
	}
	else if((strstr(file,".u")) != NULL)
	{
	  for (y = 0; y < texture->ysize; y++, sp += texture->lsize) 
	  {
		getrow(img, cp, y, 0);
		for (xl = 0, x = 0; xl < img->xsize; x += 4, xl++) 
		  sp[x] = cp[xl] & 0xff;
	  }
	}
	else if((strstr(file,".v")) != NULL)
	{
	  for (y = 0; y < texture->ysize; y++, sp += texture->lsize) 
	  {
		getrow(img, cp, y, 0);
		for (xl = 0, x = 1; xl < img->xsize; x += 4, xl++) 
		  sp[x] = cp[xl] & 0xff;
	  }
	}
	else 
	{
	  fprintf(stderr,"Unrecognized file extension: %s\n", file);
	  exit(1);
	}

	free(cp);
	iclose(img);
	return(texture);
}