~ubuntu-branches/ubuntu/lucid/graphviz/lucid-updates

« back to all changes in this revision

Viewing changes to lib/utilities/glTexFontTGA.c

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2008-06-19 20:23:23 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080619202323-ls23h96ntj9ny94m
Tags: 2.18-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Build depend on liblualib50-dev instead of liblua5.1-0-dev.
  - Drop libttf-dev (libttf-dev is in universe) (LP: #174749).
  - Replace gs-common with ghostscript.
  - Build-depend on python-dev instead of python2.4-dev or python2.5-dev.
  - Mention the correct python version for the python bindings in the
    package description.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 1999 Nate Miller
 
3
    
 
4
    Notice: Usage of any code in this file is subject to the rules
 
5
    described in the LICENSE.TXT file included in this directory.
 
6
    Reading, compiling, or otherwise using this code constitutes
 
7
    automatic acceptance of the rules in said text file.
 
8
 
 
9
    File        -- glTexFontTGA.c
 
10
    Date        -- 5/30/99
 
11
    Author      -- Nate 'm|d' Miller
 
12
    Contact     -- vandals1@home.com
 
13
    Web         -- http://members.home.com/vandals1
 
14
 
 
15
    Change Log
 
16
    **********
 
17
    6/11/99 - added support for 8bit images, changed commenting
 
18
    5/30/99 - original file
 
19
*/
 
20
#include "glTexFont.h"
 
21
#include "glTexFontTGA.h"
 
22
#include "glTexFontInclude.h"
 
23
 
 
24
GLenum texFormat;
 
25
 
 
26
/*
 
27
=============
 
28
fontCheckSize
 
29
 
 
30
Make sure its a power of 2.
 
31
=============
 
32
*/
 
33
int fontCheckSize (int x)
 
34
{
 
35
        if (x == 2       || x == 4 || 
 
36
                x == 8   || x == 16 || 
 
37
                x == 32  || x == 64 ||
 
38
                x == 128 || x == 256 || x == 512)
 
39
                return 1;
 
40
        else return 0;
 
41
}
 
42
/*
 
43
=============
 
44
fontGetRGBA
 
45
 
 
46
Reads in RGBA data for a 32bit image. 
 
47
=============
 
48
*/
 
49
unsigned char *fontGetRGBA (FILE *s, int size)
 
50
{
 
51
        unsigned char *rgba;
 
52
        unsigned char temp;
 
53
        int bread;
 
54
        int i;
 
55
 
 
56
        rgba = malloc (size * 4); 
 
57
 
 
58
        if (rgba == NULL)
 
59
                return 0;
 
60
 
 
61
        bread = fread (rgba, sizeof (unsigned char), size * 4, s); 
 
62
        
 
63
    /* TGA is stored in BGRA, make it RGBA */
 
64
        if (bread != size * 4)
 
65
        {
 
66
                free (rgba);
 
67
                return 0;
 
68
        }
 
69
 
 
70
        for (i = 0; i < size * 4; i += 4 )
 
71
        {
 
72
                temp = rgba[i];
 
73
                rgba[i] = rgba[i + 2];
 
74
                rgba[i + 2] = temp;
 
75
        }
 
76
 
 
77
        texFormat = GL_RGBA;
 
78
        return rgba;
 
79
}
 
80
/*
 
81
=============
 
82
fontGetRGB
 
83
 
 
84
Reads in RGB data for a 24bit image. 
 
85
=============
 
86
*/
 
87
unsigned char *fontGetRGB (FILE *s, int size)
 
88
{
 
89
        unsigned char *rgb;
 
90
        unsigned char temp;
 
91
        int bread;
 
92
        int i;
 
93
 
 
94
        rgb = malloc (size * 3); 
 
95
        
 
96
        if (rgb == NULL)
 
97
                return 0;
 
98
 
 
99
        bread = fread (rgb, sizeof (unsigned char), size * 3, s);
 
100
 
 
101
        if (bread != size * 3)
 
102
        {
 
103
                free (rgb);
 
104
                return 0;
 
105
        }
 
106
 
 
107
    /* TGA is stored in BGR, make it RGB */
 
108
        for (i = 0; i < size * 3; i += 3)
 
109
        {
 
110
                temp = rgb[i];
 
111
                rgb[i] = rgb[i + 2];
 
112
                rgb[i + 2] = temp;
 
113
        }
 
114
        
 
115
        texFormat = GL_RGB;
 
116
 
 
117
        return rgb;
 
118
}
 
119
/*
 
120
=============
 
121
fontGetGray
 
122
 
 
123
Reads a gray scale image. 
 
124
=============
 
125
*/
 
126
unsigned char *fontGetGray (FILE *s, int size)
 
127
{
 
128
        unsigned char *grayData;
 
129
        int bread;
 
130
 
 
131
        grayData = malloc (size);
 
132
 
 
133
        if (grayData == NULL)
 
134
                return 0;
 
135
 
 
136
        bread = fread (grayData, sizeof (unsigned char), size, s);
 
137
 
 
138
        if (bread != size)
 
139
        {
 
140
                free (grayData);
 
141
                return 0;
 
142
        }
 
143
        
 
144
        texFormat = GL_ALPHA;
 
145
 
 
146
        return grayData;
 
147
}
 
148
/*
 
149
=============
 
150
fontGetData
 
151
 
 
152
Gets the image data for the specified bit depth.
 
153
=============
 
154
*/
 
155
unsigned char *fontGetData (FILE *s, int sz, int iBits)
 
156
{
 
157
        if (iBits == 32)
 
158
                return fontGetRGBA (s, sz);
 
159
        else if (iBits == 24)
 
160
                return fontGetRGB (s, sz);      
 
161
    else if (iBits == 8)
 
162
        return fontGetGray (s, sz);
 
163
    else
 
164
        return 0;
 
165
}
 
166
/*
 
167
=============
 
168
fontLoadTGA
 
169
 
 
170
Loads up a targa file.  Supported types are 8,24 and 32 uncompressed images.  
 
171
=============
 
172
*/
 
173
int fontLoadTGA (char *name, int id)
 
174
{
 
175
        unsigned char type[4];
 
176
        unsigned char info[7];
 
177
        unsigned char *imageData = NULL;
 
178
        int imageWidth, imageHeight;
 
179
        int imageBits, size;
 
180
        FILE *s;
 
181
        
 
182
#ifdef _WIN32
 
183
        if (!(s = fopen (name, "r+bt")))
 
184
#else
 
185
// FIXME - the file exists but I still error on this fopen() call
 
186
fprintf(stderr,"font: %s\n", name);
 
187
        if (!(s = fopen (name, "rb")))
 
188
#endif
 
189
                return FONT_FILE_NOT_FOUND;
 
190
 
 
191
        fread (&type, sizeof (char), 3, s); // read in colormap info and image type, byte 0 ignored
 
192
        fseek (s, 12, SEEK_SET);                        // seek past the header and useless info
 
193
        fread (&info, sizeof (char), 6, s);
 
194
 
 
195
        if (type[1] != 0 || (type[2] != 2 && type[2] != 3))
 
196
                return FONT_BAD_IMAGE_TYPE;
 
197
    
 
198
    imageWidth = info[0] + info[1] * 256; 
 
199
        imageHeight = info[2] + info[3] * 256;
 
200
        imageBits =     info[4]; 
 
201
 
 
202
        size = imageWidth * imageHeight; 
 
203
 
 
204
        /* make sure dimension is a power of 2 */
 
205
        if (!fontCheckSize (imageWidth) || !fontCheckSize (imageHeight))
 
206
                return FONT_BAD_DIMENSION;
 
207
 
 
208
        /* make sure we are loading a supported type */
 
209
        if (imageBits != 32 && imageBits != 24 && imageBits != 8)
 
210
                return FONT_BAD_BITS;
 
211
 
 
212
        imageData = fontGetData (s, size, imageBits);
 
213
        
 
214
        fclose (s);
 
215
 
 
216
        /* no image data */
 
217
        if (imageData == NULL)
 
218
                return FONT_BAD_DATA;
 
219
 
 
220
        glBindTexture (GL_TEXTURE_2D, id);
 
221
        glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
 
222
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 
223
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
224
        /* glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); */
 
225
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
226
        /* glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); */
 
227
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
228
        glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
229
        glTexImage2D (GL_TEXTURE_2D, 0, texFormat, imageWidth, imageHeight, 0, texFormat, GL_UNSIGNED_BYTE, imageData);
 
230
 
 
231
        /* release data, its been uploaded */
 
232
        free (imageData);
 
233
 
 
234
        return 1;
 
235
}
 
236