image.C 6.23 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:     image.C
* CLASS:        Image 
* DESCRIPTION:  
* AUTHOR:       Mike Goslin
* CREATED:      3/29/95
* REVISIONS:
*
*******************************************************************************/
#include <stdlib.h>
#include <iostream.h>
#include <math.h>
#include <string.h>
#include <unistd.h>

#include "image.H"

/*******************************************************************************
*     Function: allocate
*        Class: Image 
*       Access: Private
*  Description: Allocates memory for an image of size x by y of type type 
*        Input:
*       Output: A void pointer to the matrix in memory
*******************************************************************************/
uint Image::allocate(void)
{
    if ((data = (txtrData **)calloc(ysize*sizeof(txtrData), type)) == NULL)
    {
	fprintf(stderr, "Couldn't allocate memory for texture map\n");
	data = NULL;
	return (0);
    }
    for (int i=0; i<ysize; i++)
    {
	if ((data[i] = (txtrData *)calloc(xsize*sizeof(txtrData), type))== NULL)
	{
	    fprintf(stderr, "Couldn't allocate memory for texture map\n");
	    data = NULL;
	    return (0);
	}
    }
    return (1);
}

/*******************************************************************************
*     Function: Constructor
*        Class: Image 
*       Access: Public
*  Description: Creates a default image of size 1x1 of txtrData 
*        Input:
*       Output:
*******************************************************************************/
Image::Image(void)
{
    xsize = 1;
    ysize = 1;
    type = IM_DATA;

    allocate();
}

/*******************************************************************************
*     Function: Constructor
*        Class: Image 
*       Access: Public
*  Description: 
*        Input:
*       Output:
*******************************************************************************/
Image::Image(uint size)
{
    xsize = size;
    ysize = size;
    type = IM_DATA;

    allocate();
}

/*******************************************************************************
*     Function: Constructor
*        Class: Image 
*       Access: Public
*  Description: 
*        Input:
*       Output:
*******************************************************************************/
Image::Image(uint size, uint _type)
{
    xsize = size;
    ysize = size;
    type = _type;

    allocate();
}

/*******************************************************************************
*     Function: Destructor
*        Class: Image 
*       Access: Public
*  Description: 
*        Input:
*       Output:
*******************************************************************************/
Image::~Image()
{
    for (int i=0; i<ysize; i++)
	free(data[i]);
    free(data);
}

/*******************************************************************************
*     Function: set 
*        Class: Image 
*       Access: Public
*  Description: 
*        Input:
*       Output:
*******************************************************************************/
void Image::set(uint col, uint row, txtrData value)
{
    VEC3COPY(data[row][col], value);
}

/*******************************************************************************
*     Function: set 
*        Class: Image 
*       Access: Public
*  Description: 
*        Input:
*       Output:
*******************************************************************************/
void Image::set(txtrData value)
{
    for (int y=0; y<ysize; y++)
	for (int x=0; x<xsize; x++)
	    set(y, x, value);
}

/*******************************************************************************
*     Function: operator() 
*        Class: Image 
*       Access: Public
*  Description: 
*        Input:
*       Output:
*******************************************************************************/
txtrData& Image::operator()(uint row, uint col)
{
    return (data[row][col]);
}

/*******************************************************************************
*     Function: Write 
*        Class: Image 
*       Access: Public
*  Description: 
*        Input:
*       Output:
*******************************************************************************/
uint Image::Write(FILE *file)
{
    int x, y;

    // write all red, then all green, then all blue
    for (y=0; y<ysize; y++)
  	for (x=0; x<xsize; x++)
	    fwrite(&data[y][x][0], sizeof(unsigned char), 
								1, file); 
    for (y=0; y<ysize; y++)
  	for (x=0; x<xsize; x++)
	    fwrite(&data[y][x][1], sizeof(unsigned char), 
								1, file); 
    for (y=0; y<ysize; y++)
  	for (x=0; x<xsize; x++)
	    fwrite(&data[y][x][2], sizeof(unsigned char), 
								1, file); 
    return (1);
}