File: Graphfil.cpp

package info (click to toggle)
amphetamine 0.8.10-13
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 604 kB
  • ctags: 1,601
  • sloc: cpp: 9,020; makefile: 145; sh: 31
file content (170 lines) | stat: -rw-r--r-- 3,819 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

/*
**  MacWT -- a 3d game engine for the Macintosh
**   1995, Bill Hayden and Nikol Software
**
**  On the Internet:
**  bmoc1@aol.com (my personal address)
**  nikolsw@grove.ufl.edu (my school address)
**	MacWT anonymous FTP site: ftp.circa.ufl.edu/pub/software/ufmug/mirrors/LocalSW/Hayden/
**  http://grove.ufl.edu:80/~nikolsw (my WWW page, containing MacWT info)
**
**  based on wt, by Chris Laurel (claurel@mr.net)
**
**  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.
*/



#include <stdio.h>
#include <string.h>
//#include "wt.h"
//#include "gApplication->system->Error.h"
//#include "wtmem.h"
//#include "color.h"
#include "Graphfil.hpp"
#include "Appl.hpp"

extern CApplication *gApplication;
extern CSystem	*gSystem;

#define mfputc	fputc
#define mfgetc	fgetc
#define mfopen	fopen
#define mfclose fclose
#define mfread	fread
#define mfseek	fseek
#define mftell	ftell
#define	mrewind	rewind
#define	mfprintf fprintf

/* MAGIC_LENGTH must be the length of the longest MAGIC string. */
#define MAGIC_LENGTH 5
#define GIF_MAGIC "GIF87"
#define GIF89_MAGIC "GIF89"


extern Graphic_file *LoadGIF(FILE *fp, char *filename);

static Graphic_file_format check_format(FILE *fp, char *filename);


Graphic_file *new_graphic_file()
{
     Graphic_file *gf = (Graphic_file *)wtmalloc(sizeof(Graphic_file));

     gf->transparent_entry = -1;

     return gf;
}


void free_graphic_file(Graphic_file *gfile)
{
     if (gfile != NULL) {
	  if (gfile->palette != NULL)
	       wtfree(gfile->palette);
	  if (gfile->bitmap != NULL)
	       wtfree(gfile->bitmap);
     }
     wtfree(gfile);
}


/* Return 0 if the pixel is transparent, 1 if it is opaque.  In any
**   case, stuff dest with the pixel RGB value.
*/
short graphic_file_pixel(Graphic_file *gfile, int x, int y, RGBcolor *rgb)
{
	if (gfile->type == gfPaletted)
		{
		int index;

		index = gfile->bitmap[y * gfile->width + x];
		if (index == gfile->transparent_entry)
			return 0;
		else
			{
			*rgb = gfile->palette[index];
			return 1;
			}
		}
	else
		{
		unsigned char *pixel = gfile->bitmap + (y * gfile->width + x) * 3;

		rgb->red = *pixel++;
		rgb->green = *pixel++;
		rgb->blue = *pixel;

	  /* For now, let pure cyan be the transparent color for 1 color images. */
		if (rgb->red == 0 && rgb->green == 255 && rgb->blue == 255)
			return 0;
		else
			return 1;
		}
}

	  
Graphic_file *read_graphic_file(char *filename)
{
	FILE *fp;
	Graphic_file_format format;
	Graphic_file *gfile = 0L;


	if ((fp = mfopen(filename, "rb")) == NULL)
		gSystem->Error("Could not open texture", 0);

	format = check_format(fp, filename);
	mrewind(fp);

	switch (format)
		{
		case formatGIF89:
		case formatGIF87:
			gfile = LoadGIF(fp, filename);
			break;

		case formatUnknown:
			gSystem->Error("Unknown graphic file format.", 0);
			break;

		default:
			gSystem->Error("The graphic file reading code is really broken.", 0);
			break;
		}

	if (gfile == NULL)
		gSystem->Error("gApplication->system->Error reading texture", 0);

	mfclose(fp);

	return gfile;
}
	  

static Graphic_file_format check_format(FILE *fp, char *filename)
{
	char magic[MAGIC_LENGTH];

#if __MWERKS__ || __THINKC__
	// to read PICT or PICTR files, which are further along on the
	// evolutionary chain than to need MagicCookies or the like.
	mfread(magic, 1, MAGIC_LENGTH, fp);
#else
	if (mfread(magic, 1, MAGIC_LENGTH, fp) != MAGIC_LENGTH)
		gSystem->Error("gApplication->system->Error reading texture", 0);
#endif

	if (strncmp(magic, GIF_MAGIC, sizeof(GIF_MAGIC) - 1) == 0)
		return formatGIF87;
	else if (strncmp(magic, GIF89_MAGIC, sizeof(GIF89_MAGIC) - 1) == 0)
		return formatGIF89;
	else
		return formatPICTR;
		
	// return formatUnknown;
}