txtrWriter.C 6.61 KB
/******************************************************************************\  Copyright 1995 The University of North Carolina at Chapel Hill.
  All Rights Reserved.

  Permission to use, copy, modify and distribute this software and its
  documentation for educational, research and non-profit purposes, without
  fee, and without a written agreement is hereby granted, provided that the
  above copyright notice and the following three paragraphs appear in all
  copies.

  IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA
  AT CHAPEL HILL BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
  INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
  OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE
  UNIVERSITY OF NORTH CAROLINA HAS BEEN ADVISED OF THE POSSIBILITY OF
  SUCH DAMAGES.

  THE UNIVERSITY OF NORTH CAROLINA SPECIFICALLY DISCLAIM ANY WARRANTIES,
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HERUNDER IS
  ON AN "AS IS" BASIS, AND THE UNIVERSITY OF NORTH CAROLINA HAS NO OBLIGATIONS
  TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

  The author may be contacted via:

  US Mail:             Mike Goslin
                       Department of Computer Science
                       Sitterson Hall, CB #3175
                       University of N. Carolina
                       Chapel Hill, NC 27599-3175

  Phone:               (919)962-1719

  EMail:               goslin@cs.unc.edu

*******************************************************************************/
/*******************************************************************************
*
* FILENAME:     txtrWriter.C
* CLASS:        TxtrWriter
* DESCRIPTION:  SGI Texture map file (.rgb) writer class 
* AUTHOR:       Mike Goslin
* CREATED:      3/24/95
* REVISIONS:
*
*******************************************************************************/
#include <stdlib.h>
#include <iostream.h>
#include <math.h>
#include <string.h>

#include "fileWriter.H"

/*******************************************************************************
*     Function: Constructor
*        Class: TxtrWriter
*       Access: Public
*  Description: Default constructor
*        Input:
*       Output:
*******************************************************************************/
TxtrWriter::TxtrWriter(void)
{
    file = NULL;
    strcpy(fname, "DEFAULT");
    image = NULL;
}

/*******************************************************************************
*     Function: Constructor
*        Class: TxtrWriter
*       Access: Public
*  Description:
*        Input: single size for both u and v dimensions
*       Output:
*******************************************************************************/
TxtrWriter::TxtrWriter(uint size)
{
    file = NULL;
    strcpy(fname, "DEFAULT");
 
    if (size > MAX_XSIZE)
    {
	fprintf(stderr, "Texture size requested is larger than MAX_SIZE\n");
	exit(0);
    }

    image = new Image(size);
}

/*******************************************************************************
*     Function: Destructor
*        Class: TxtrWriter
*       Access: Public
*  Description:
*        Input:
*       Output:
*******************************************************************************/
TxtrWriter::~TxtrWriter()
{
    delete image;
}

/*******************************************************************************
*     Function: putbyte                      
*        Class: TxtrWriter
*       Access: Public
*  Description:
*        Input: 
*       Output:
*******************************************************************************/
int TxtrWriter::putbyte(unsigned char val)
{
    unsigned char buf[1];

    buf[0] = val;
    return fwrite(buf,1,1,file);
}

/*******************************************************************************
*     Function: putshort                      
*        Class: TxtrWriter
*       Access: Public
*  Description:
*        Input: 
*       Output:
*******************************************************************************/
int TxtrWriter::putshort(unsigned short val)
{
    unsigned char buf[2];
   
    buf[0] = (uchar)(val>>8);
    buf[1] = (uchar)(val>>0);
    return fwrite(buf,2,1,file);
}

/*******************************************************************************
*     Function: putlong                      
*        Class: TxtrWriter
*       Access: Public
*  Description:
*        Input: 
*       Output:
*******************************************************************************/
int TxtrWriter::putlong(unsigned long val)
{
    unsigned char buf[4];
   
    buf[0] = (unsigned char)(val>>24);
    buf[1] = (unsigned char)(val>>16);
    buf[2] = (unsigned char)(val>>8);
    buf[3] = (unsigned char)(val>>0);
    return fwrite(buf,4,1,file);
}

/*******************************************************************************
*     Function: Write                      
*        Class: TxtrWriter
*       Access: Public
*  Description:
*        Input: 
*       Output:
*******************************************************************************/
uint TxtrWriter::Write(uint x, uint y, txtrData value)
{
    image->set(x, y, value);
    return (1);
}

/*******************************************************************************
*     Function: Write                      
*        Class: TxtrWriter
*       Access: Public
*  Description:
*        Input: 
*       Output:
*******************************************************************************/
uint TxtrWriter::Write(char *_fname)
{
    char iname[80];
    int i;

    strcpy(fname, _fname);
    if (openFile() == 0)
    {
        fprintf(stderr, "openFile: Could not open file < %s >\n", fname);
        return (0);
    }
 
    /* data is 3*ysize pointers to xsize chars */

    putshort(474);       	/* MAGIC                    */
    putbyte(0);          	/* STORAGE is VERBATIM      */
    putbyte(1);          	/* BPC is 1                 */
    putshort(3);         	/* DIMENSION is 2           */
    putshort((short)image->sizex());  /* XSIZE                      */
    putshort((short)image->sizey());  /* YSIZE                      */
    putshort(3);         	/* ZSIZE                    */
    putlong(0);          	/* PIXMIN is 0              */
    putlong(255);        	/* PIXMAX is 255            */
    for(i=0; i<4; i++)      	/* DUMMY 4 bytes    */
        putbyte(0);
    strcpy(iname,fname);
    fwrite(iname,80,1,file);  	/* IMAGENAME                */
    putlong(0);          	/* COLORMAP is 0    */
    for(i=0; i<404; i++)    	/* DUMMY 404 bytes  */
        putbyte(0);

    image->Write(file);

    fclose(file);
    return (1);
}