home *** CD-ROM | disk | FTP | other *** search
- /*
- ** MacWT -- a 3d game engine for the Macintosh
- ** © 1995, Bill Hayden and Nikol Software
- ** Free for non-commercial use - address questions to the e-mail address below
- **
- ** Mail: afn28988@freenet.ufl.edu (Bill Hayden)
- ** MacWT FTP site: ftp.circa.ufl.edu/pub/software/ufmug/mirrors/LocalSW/Hayden/
- ** WWW Page: http://grove.ufl.edu:80/~nikolsw
- **
- ** All of the above addresses are due to changes sometime in 1996, so stay tuned
- **
- ** based on wt, by Chris Laurel
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- */
-
- /* GIF87a code from Thomas Hamren (d0hampe@dtek.chalmers.se) */
-
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include "wt.h"
- #include "wtmem.h"
- #include "error.h"
- #include "color.h"
- #include "graphfile.h"
- #include "framebuf.h"
- #include "graphics.h"
- #include "texture.h"
- #include "MacWT.h"
-
-
- static short ilog2(short x);
- static void quantize_texture(Graphic_file *gfile, Texture *t);
-
-
-
-
- Texture *new_texture(short width, short height)
- {
- Texture *t;
-
- if (height <= 0 || width <= 0)
- fatal_error("new_texture: bad texture dimensions");
-
- t = (Texture *)wtmalloc(sizeof(Texture));
-
- if (gTrueColor)
- {
- t->texels16 = (Pixel16 *)wtmalloc(height * width * gSizeOfPixel);
- t->texels = NULL;
- }
- else
- {
- t->texels = (Pixel *)wtmalloc(height * width * gSizeOfPixel);
- t->texels16 = NULL;
- }
-
- t->width = width;
- t->height = height;
- t->log2height = ilog2(height);
-
- return t;
- }
-
-
-
- Texture *read_texture_file(char *filename)
- {
- Graphic_file *gfile;
- Texture *t;
-
-
- gfile = read_graphic_file(filename);
- if (gfile == NULL)
- fatal_error("Error reading texture %s.", filename);
-
- /* The height and width should be powers of two for efficient
- ** texture mapping. Here, we enforce this.
- */
- if (ilog2(gfile->height) == -1)
- fatal_error("Height of texture %s is not a power of two.", filename);
-
- t = new_texture(gfile->width, gfile->height);
- quantize_texture(gfile, t);
-
- free_graphic_file(gfile);
-
- return t;
- }
-
-
-
- /* Return the log base 2 of the argument, or -1 if the argument is not
- ** an integer power of 2.
- */
- static short ilog2(short x)
- {
- short i;
- unsigned short n;
-
- if (x <= 0)
- return -1;
- else
- n = (unsigned short) x;
-
- for (i = 0; (n & 0x1) == 0; i++, n >>= 1);
-
- if (n == 1)
- return i;
- else
- return -1;
- }
-
-
- static void quantize_texture(Graphic_file *gfile, Texture *t)
- {
- RGBcolor pixel; // Generic Color
- RGBColor macColor; // Macintosh Color (notice the case)
- short x, y;
-
-
- if (gTrueColor)
- {
- Pixel16 *dest_pixel = t->texels16;
-
- for (x = gfile->width - 1; x >= 0; x--)
- for (y = gfile->height - 1; y >= 0; y--)
- {
- if (!graphic_file_pixel(gfile, x, y, &pixel))
- *dest_pixel++ = (Pixel16) ~0;
- else
- *dest_pixel++ = (Pixel16)(( (pixel.red >> 3) << 10) +
- ( (pixel.green >> 3) << 5) +
- (pixel.blue >> 3));
- }
- }
- else
- {
- Pixel *dest_pixel = t->texels;
-
- for (x = gfile->width - 1; x >= 0; x--)
- for (y = gfile->height - 1; y >= 0; y--)
- {
- if (!graphic_file_pixel(gfile, x, y, &pixel))
- *dest_pixel++ = (Pixel) ~0;
- else
- {
- macColor.red = pixel.red << 8;
- macColor.green = pixel.green << 8;
- macColor.blue = pixel.blue << 8;
- *dest_pixel++ = (Pixel)Color2Index( &macColor );
- }
- }
- }
- }
-