Browse code

Fix warnings when compiling in C++ mode

Robert Cranston authored on 27/02/2024 02:27:43
Showing 1 changed files
... ...
@@ -49,7 +49,7 @@ bool LoadTGATextureData(const char *filename, TextureData *texture)	// Loads A T
49 49
 	GLubyte *rowP;
50 50
 	int err;
51 51
 	GLubyte rle;
52
-	int b;
52
+	GLuint b;
53 53
 	long row, rowLimit;
54 54
 	GLubyte pixelData[4];	
55 55
 	
... ...
@@ -199,7 +199,7 @@ bool LoadTGATextureData(const char *filename, TextureData *texture)	// Loads A T
199 199
 	}
200 200
 
201 201
 	if (bytesPerPixel >= 3) // if not monochrome	
202
-	for (i = 0; i < (int)(imageSize); i += bytesPerPixel)	// Loop Through The Image Data
202
+	for (i = 0; i < imageSize; i += bytesPerPixel)	// Loop Through The Image Data
203 203
 	{		// Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
204 204
 		temp = texture->imageData[i];		// Temporarily Store The Value At Image Data 'i'
205 205
 		texture->imageData[i] = texture->imageData[i + 2];	// Set The 1st Byte To The Value Of The 3rd Byte
Browse code

Convert encoding from MacRoman to UTF-8

Swedish characters use a weird encoding. For instance, `file --mime`
just gives `charset=unknown-8bit` and `grep` just says `binary file
matches`.

Searching for them in the [Western Latin character sets comparison
table][] gives that this is the old [Mac OS Roman][] (MIME / IANA:
`macintosh`). Tooling support for this outside of Macs is very poor, as
can be seen above.

[Western Latin character sets comparison table]: https://en.wikipedia.org/wiki/Western_Latin_character_sets_(computing)#Comparison_table
[Mac OS Roman]: https://en.wikipedia.org/wiki/Mac_OS_Roman

To convert:

tmp_file="$(mktemp)"
trap 'rm "$tmp_file"' EXIT
find . -type f -exec file --mime {} + \
| while IFS=':' read file mime
do
if echo "$mime" | grep -qi 'charset=unknown-8bit'
then
iconv -f 'macintosh' -t 'utf-8' "$file" -o "$tmp_file"
mv "$tmp_file" "$file"
fi
done

Robert Cranston authored on 27/02/2024 02:27:29
Showing 1 changed files
... ...
@@ -53,7 +53,7 @@ bool LoadTGATextureData(const char *filename, TextureData *texture)	// Loads A T
53 53
 	long row, rowLimit;
54 54
 	GLubyte pixelData[4];	
55 55
 	
56
-	// Nytt f�r flipping-st�d 111114
56
+	// Nytt för flipping-stöd 111114
57 57
 	char flipped;
58 58
 	long step;
59 59
 	
Browse code

Remove executable bit on files

find . -type f -exec chmod -x {} +

Robert Cranston authored on 27/02/2024 02:27:27
Showing 1 changed files
1 1
old mode 100755
2 2
new mode 100644
Browse code

Add cpp-v2

Ingemar Ragnemalm authored on 21/02/2024 20:24:29 • Robert Cranston committed on 21/02/2024 20:24:29
Showing 1 changed files
... ...
@@ -12,6 +12,8 @@
12 12
 // 180216: calloc is better than malloc when you may have undefined data in parts of the texture!
13 13
 // 190416: Skipped testing the last four bytes of the 12-byte heading. That is the origin of the
14 14
 // image which is usually 0. It is now ignored.
15
+// 220218: Big change: Removed the power of 2. GPUs with that limitation are gone now.
16
+// This simplifies both the code and the interface.
15 17
 
16 18
 // NOTE: LoadTGA does NOT support all TGA variants! You may need to re-save your TGA
17 19
 // with different settings to find a suitable format.
... ...
@@ -43,7 +45,7 @@ bool LoadTGATextureData(const char *filename, TextureData *texture)	// Loads A T
43 45
 		imageSize,		// Used To Store The Image Size When Setting Aside Ram
44 46
 		temp;			// Temporary Variable
45 47
 	long rowSize, stepSize, bytesRead;
46
-	long w, h;
48
+//	long w, h;
47 49
 	GLubyte *rowP;
48 50
 	int err;
49 51
 	GLubyte rle;
... ...
@@ -109,20 +111,21 @@ bool LoadTGATextureData(const char *filename, TextureData *texture)	// Loads A T
109 111
 		return false;
110 112
 	}
111 113
 	flipped = (header[5] & 32) != 0; // Testa om flipped
112
-	
113
-	w = 1;
114
-	while (w < texture->width) w = w << 1;
115
-	h = 1;
116
-	while (h < texture->height) h = h << 1;
117
-	texture->texWidth = (GLfloat)texture->width / w;
118
-	texture->texHeight = (GLfloat)texture->height / h;
114
+
115
+// That old power of 2 is no longer needed!
116
+//	w = 1;
117
+//	while (w < texture->width) w = w << 1;
118
+//	h = 1;
119
+//	while (h < texture->height) h = h << 1;
120
+//	texture->texWidth = (GLfloat)texture->width / w;
121
+//	texture->texHeight = (GLfloat)texture->height / h;
119 122
 	
120 123
 	
121 124
 	texture->bpp = header[4];		// Grab The TGA's Bits Per Pixel (24 or 32)
122 125
 	bytesPerPixel = texture->bpp/8;		// Divide By 8 To Get The Bytes Per Pixel
123
-	imageSize = w * h * bytesPerPixel;	// Calculate The Memory Required For The TGA Data
126
+	imageSize = texture->width * texture->height * bytesPerPixel;	// Calculate The Memory Required For The TGA Data
124 127
 	rowSize	= texture->width * bytesPerPixel;	// Image memory per row
125
-	stepSize = w * bytesPerPixel;		// Memory per row
128
+	stepSize = texture->width * bytesPerPixel;		// Memory per row
126 129
 	texture->imageData = (GLubyte *)calloc(1, imageSize);	// Reserve Memory To Hold The TGA Data
127 130
 	if (texture->imageData == NULL)				// Does The Storage Memory Exist?
128 131
 	{
... ...
@@ -133,7 +136,7 @@ bool LoadTGATextureData(const char *filename, TextureData *texture)	// Loads A T
133 136
 	// Set rowP and step depending on direction
134 137
 	// Inverted this 120131, since a texture came out wrong.
135 138
 	// I am still not sure this is OK.
136
-	if (!flipped)
139
+	if (flipped)
137 140
 	{
138 141
 		step = -stepSize;
139 142
 		rowP = &texture->imageData[imageSize - stepSize];
... ...
@@ -204,8 +207,8 @@ bool LoadTGATextureData(const char *filename, TextureData *texture)	// Loads A T
204 207
 	}
205 208
 	fclose (file);
206 209
 
207
-texture->w = w;
208
-texture->h = h;
210
+//texture->w = w;
211
+//texture->h = h;
209 212
 
210 213
 	return true;				// Texture loading Went Ok, Return True
211 214
 }
... ...
@@ -232,7 +235,7 @@ bool LoadTGATexture(const char *filename, TextureData *texture)	// Loads A TGA F
232 235
 	{
233 236
 		type=GL_RGB;			// If So Set The 'type' To GL_RGB
234 237
 	}
235
-	glTexImage2D(GL_TEXTURE_2D, 0, type, texture->w, texture->h, 0, type, GL_UNSIGNED_BYTE, texture[0].imageData);
238
+	glTexImage2D(GL_TEXTURE_2D, 0, type, texture->width, texture->height, 0, type, GL_UNSIGNED_BYTE, texture[0].imageData);
236 239
 	
237 240
 	if (gMipmap)
238 241
 	{
... ...
@@ -324,7 +327,8 @@ int SaveDataToTGA(char			*filename,
324 327
 
325 328
 	fclose(file);
326 329
 // release the memory
327
-	free(imageData);
330
+// No, this is the responsability of the host!
331
+//	free(imageData);
328 332
 
329 333
 	return(TGA_OK);
330 334
 }
Browse code

Add cpp-v1

Ingemar Ragnemalm authored on 21/02/2024 20:24:28 • Robert Cranston committed on 21/02/2024 20:24:28
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,351 @@
1
+// TGA loader, by Ingemar 2009, based on "tga.c" and some other sources.
2
+// Updated 2013 to use modern mipmapping
3
+// 130423: Added LoadTGATextureData
4
+// 130905: Fixed a bug in LoadTGATextureSimple
5
+// 140212: Fixed a bug in loading of uncompressed images.
6
+// 140520: Supports grayscale images (loads to red channel).
7
+// 141007: Added SaveTGA. (Moved from the obsolete LoadTGA2.)
8
+// 160302: Uses fopen_s on Windows, as suggested by Jesper Post. Should reduce warnings a bit.
9
+// 170220: Changed fopen_s to "rb". This made it fail for some textures.
10
+// 170331: Cleaned up a bit to remove warnings.
11
+// 170419: Fixed a bug that prevented monochrome images from loading.
12
+// 180216: calloc is better than malloc when you may have undefined data in parts of the texture!
13
+// 190416: Skipped testing the last four bytes of the 12-byte heading. That is the origin of the
14
+// image which is usually 0. It is now ignored.
15
+
16
+// NOTE: LoadTGA does NOT support all TGA variants! You may need to re-save your TGA
17
+// with different settings to find a suitable format.
18
+
19
+#include "LoadTGA.h"
20
+
21
+static bool gMipmap = true;
22
+
23
+// Note that turning mimpapping on and off refers to the loading stage only.
24
+// If you want to turn off mipmapping later, use 
25
+// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
26
+
27
+void LoadTGASetMipmapping(bool active)
28
+{
29
+	gMipmap = active;
30
+}
31
+
32
+bool LoadTGATextureData(const char *filename, TextureData *texture)	// Loads A TGA File Into Memory
33
+{
34
+	GLuint i;
35
+	GLubyte
36
+		TGAuncompressedheader[12]={ 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},	// Uncompressed TGA Header
37
+		TGAcompressedheader[12]={ 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0},	// Compressed TGA Header
38
+		TGAuncompressedbwheader[12]={ 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0},	// Uncompressed grayscale TGA Header
39
+		TGAcompressedbwheader[12]={ 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0},	// Compressed grayscale TGA Header
40
+		actualHeader[12],	// Used To Compare TGA Header
41
+		header[6];		// First 6 Useful Bytes From The Header
42
+	GLuint bytesPerPixel,		// Holds Number Of Bytes Per Pixel Used In The TGA File
43
+		imageSize,		// Used To Store The Image Size When Setting Aside Ram
44
+		temp;			// Temporary Variable
45
+	long rowSize, stepSize, bytesRead;
46
+	long w, h;
47
+	GLubyte *rowP;
48
+	int err;
49
+	GLubyte rle;
50
+	int b;
51
+	long row, rowLimit;
52
+	GLubyte pixelData[4];	
53
+	
54
+	// Nytt f�r flipping-st�d 111114
55
+	char flipped;
56
+	long step;
57
+	
58
+	// It seems Windows/VS doesn't like fopen any more, but fopen_s is not on the others.
59
+	FILE *file;
60
+	#if defined(_WIN32)
61
+		fopen_s(&file, filename, "rb");
62
+	#else
63
+		file = fopen(filename, "rb"); // rw works everywhere except Windows?
64
+	#endif
65
+//	FILE *file = fopen(filename, "rb");			// Open The TGA File
66
+	err = 0;
67
+	if (file == NULL) err = 1;				// Does File Even Exist?
68
+	else if (fread(actualHeader, 1, sizeof(actualHeader), file) != sizeof(actualHeader)) err = 2; // Are There 12 Bytes To Read?
69
+	else if (
70
+	// Edit 2019: Ignore the origin!
71
+				(memcmp(TGAuncompressedheader, actualHeader, sizeof(TGAuncompressedheader)-4) != 0) &&
72
+				(memcmp(TGAcompressedheader, actualHeader, sizeof(TGAcompressedheader)-4) != 0) &&
73
+				(memcmp(TGAuncompressedbwheader, actualHeader, sizeof(TGAuncompressedheader)-4) != 0) &&
74
+				(memcmp(TGAcompressedbwheader, actualHeader, sizeof(TGAcompressedheader)-4) != 0)
75
+			)
76
+			{
77
+				err = 3; // Does The Header Match What We Want?
78
+				for (i = 0; i < 12; i++)
79
+					printf("%d ", actualHeader[i]);
80
+				printf("\n");
81
+			}
82
+	else if (fread(header, 1, sizeof(header), file) != sizeof(header)) err = 4; // If So Read Next 6 Header Bytes
83
+	
84
+	if (err != 0)
85
+	{
86
+		switch (err)
87
+		{
88
+			case 1: printf("could not open file %s\n", filename); break;
89
+			case 2: printf("could not read header of %s\n", filename); break;
90
+			case 3: printf("unsupported format in %s\n", filename); break;
91
+			case 4: printf("could not read file %s\n", filename); break;
92
+		}
93
+		
94
+		if(file == NULL)		// Did The File Even Exist? *Added Jim Strong*
95
+			return false;
96
+		else
97
+		{
98
+			fclose(file);		// If Anything Failed, Close The File
99
+			return false;
100
+		}
101
+	}
102
+	texture->width  = header[1] * 256 + header[0];	// Determine The TGA Width (highbyte*256+lowbyte)
103
+	texture->height = header[3] * 256 + header[2];	// Determine The TGA Height (highbyte*256+lowbyte)
104
+	if (texture->width <= 0 ||	// Is The Width Less Than Or Equal To Zero
105
+	texture->height <= 0 ||		// Is The Height Less Than Or Equal To Zero
106
+	(header[4] != 24 && header[4] != 32 && header[4] != 8))			// Is The TGA 24 or 32 Bit?
107
+	{
108
+		fclose(file);		// If Anything Failed, Close The File
109
+		return false;
110
+	}
111
+	flipped = (header[5] & 32) != 0; // Testa om flipped
112
+	
113
+	w = 1;
114
+	while (w < texture->width) w = w << 1;
115
+	h = 1;
116
+	while (h < texture->height) h = h << 1;
117
+	texture->texWidth = (GLfloat)texture->width / w;
118
+	texture->texHeight = (GLfloat)texture->height / h;
119
+	
120
+	
121
+	texture->bpp = header[4];		// Grab The TGA's Bits Per Pixel (24 or 32)
122
+	bytesPerPixel = texture->bpp/8;		// Divide By 8 To Get The Bytes Per Pixel
123
+	imageSize = w * h * bytesPerPixel;	// Calculate The Memory Required For The TGA Data
124
+	rowSize	= texture->width * bytesPerPixel;	// Image memory per row
125
+	stepSize = w * bytesPerPixel;		// Memory per row
126
+	texture->imageData = (GLubyte *)calloc(1, imageSize);	// Reserve Memory To Hold The TGA Data
127
+	if (texture->imageData == NULL)				// Does The Storage Memory Exist?
128
+	{
129
+		fclose(file);
130
+		return false;
131
+	}
132
+
133
+	// Set rowP and step depending on direction
134
+	// Inverted this 120131, since a texture came out wrong.
135
+	// I am still not sure this is OK.
136
+	if (!flipped)
137
+	{
138
+		step = -stepSize;
139
+		rowP = &texture->imageData[imageSize - stepSize];
140
+		row = 0 + (texture->height -1) * stepSize;
141
+	}
142
+	else
143
+	{
144
+		step = stepSize;
145
+		rowP = &texture->imageData[0];
146
+		row = 0;
147
+	}
148
+
149
+	if (actualHeader[2] == 2 || actualHeader[2] == 3) // uncompressed
150
+	{
151
+		for (i = 0; i < texture->height; i++)
152
+		{
153
+			bytesRead = fread(rowP, 1, rowSize, file);
154
+			rowP += step;
155
+			if (bytesRead != rowSize)
156
+			{
157
+				free(texture->imageData);	// If So, Release The Image Data
158
+				fclose(file);			// Close The File
159
+				return false;			// Return False
160
+			}
161
+		}
162
+	}
163
+	else
164
+	{ // compressed
165
+//		row = 0 + (texture->height -1) * stepSize;
166
+		i = row;
167
+		rowLimit = row + rowSize;
168
+		do
169
+		{
170
+			bytesRead = fread(&rle, 1, 1, file);
171
+			if (rle < 128)
172
+			{ // rle+1 raw pixels
173
+				bytesRead = fread(&texture->imageData[i], 1, (rle+1)*bytesPerPixel, file);
174
+				i += bytesRead;
175
+				if (bytesRead == 0)
176
+					i = imageSize;
177
+			}
178
+			else
179
+			{ // range of rle-127 pixels with a color that follows
180
+				bytesRead = fread(&pixelData, 1, bytesPerPixel, file);
181
+				do
182
+				{
183
+					for (b = 0; b < bytesPerPixel; b++)
184
+						texture->imageData[i+b] = pixelData[b];
185
+					i += bytesPerPixel;
186
+					rle = rle - 1;
187
+				} while (rle > 127);
188
+			}
189
+			if (i >= rowLimit)
190
+			{
191
+				row = row + step; // - stepSize;
192
+				rowLimit = row + rowSize;
193
+				i = row;
194
+			}
195
+		} while (i < imageSize);
196
+	}
197
+
198
+	if (bytesPerPixel >= 3) // if not monochrome	
199
+	for (i = 0; i < (int)(imageSize); i += bytesPerPixel)	// Loop Through The Image Data
200
+	{		// Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
201
+		temp = texture->imageData[i];		// Temporarily Store The Value At Image Data 'i'
202
+		texture->imageData[i] = texture->imageData[i + 2];	// Set The 1st Byte To The Value Of The 3rd Byte
203
+		texture->imageData[i + 2] = temp;	// Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
204
+	}
205
+	fclose (file);
206
+
207
+texture->w = w;
208
+texture->h = h;
209
+
210
+	return true;				// Texture loading Went Ok, Return True
211
+}
212
+
213
+bool LoadTGATexture(const char *filename, TextureData *texture)	// Loads A TGA File Into Memory and creates texture object
214
+{
215
+	char ok;
216
+	GLuint type = GL_RGBA;		// Set The Default GL Mode To RBGA (32 BPP)
217
+	
218
+	ok = LoadTGATextureData(filename, texture);	// Loads A TGA File Into Memory
219
+	if (!ok)
220
+		return false;
221
+
222
+	// Build A Texture From The Data
223
+	glGenTextures(1, &texture->texID);			// Generate OpenGL texture IDs
224
+	glBindTexture(GL_TEXTURE_2D, texture->texID);		// Bind Our Texture
225
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	// Linear Filtered
226
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	// Linear Filtered
227
+	if (texture->bpp == 8)						// Was The TGA 8 Bits? Should be grayscale then.
228
+	{
229
+		type=GL_RED;			// If So Set The 'type' To GL_RED
230
+	}
231
+	if (texture->bpp == 24)						// Was The TGA 24 Bits?
232
+	{
233
+		type=GL_RGB;			// If So Set The 'type' To GL_RGB
234
+	}
235
+	glTexImage2D(GL_TEXTURE_2D, 0, type, texture->w, texture->h, 0, type, GL_UNSIGNED_BYTE, texture[0].imageData);
236
+	
237
+	if (gMipmap)
238
+	{
239
+		glGenerateMipmap(GL_TEXTURE_2D);
240
+		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);	// Linear Filtered
241
+	}
242
+	
243
+	return true;				// Texture Building Went Ok, Return True
244
+}
245
+
246
+void LoadTGATextureSimple(const char *filename, GLuint *tex) // If you really only need the texture object.
247
+{
248
+	TextureData texture;
249
+	memset(&texture, 0, sizeof(texture)); // Bug fix 130905.
250
+	
251
+	if (LoadTGATexture(filename, &texture))
252
+	{
253
+		if(texture.imageData != NULL)
254
+			free(texture.imageData);
255
+		*tex = texture.texID;
256
+	}
257
+	else
258
+		*tex = 0;
259
+}
260
+
261
+
262
+// saves an array of pixels as a TGA image
263
+// Was tgaSave, found in some reusable code.
264
+// Limitation: Can NOT save with transparency! Only RGB, not RGBA!
265
+// But it should! Why not?
266
+int SaveDataToTGA(char			*filename, 
267
+			 short int		width, 
268
+			 short int		height, 
269
+			 unsigned char	pixelDepth,
270
+			 unsigned char	*imageData)
271
+{
272
+	unsigned char cGarbage = 0, mode,aux;
273
+	int i, w, ix;
274
+	FILE *file;
275
+	char /*GLubyte*/ TGAuncompressedheader[12]={ 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};	// Uncompressed TGA Header
276
+
277
+// open file and check for errors
278
+#if defined(_WIN32)
279
+	fopen_s(&file, filename, "w");
280
+#else
281
+	file = fopen(filename, "w"); // rw works everywhere except Windows?
282
+#endif
283
+//	file = fopen(filename, "w");
284
+	if (file == NULL)
285
+		return(TGA_ERROR_FILE_OPEN);
286
+
287
+// compute image type: 2 for RGB(A), 3 for greyscale
288
+	mode = pixelDepth / 8;
289
+// NOT YET IMPLEMENTED
290
+//	if ((pixelDepth == 24) || (pixelDepth == 32))
291
+//		type = 2;
292
+//	else
293
+//		type = 3;
294
+
295
+// write the header
296
+	fwrite(&TGAuncompressedheader, 12, 1, file);
297
+	fwrite(&width, sizeof(short int), 1, file);
298
+	fwrite(&height, sizeof(short int), 1, file);
299
+	fwrite(&pixelDepth, sizeof(unsigned char), 1, file);
300
+
301
+	fwrite(&cGarbage, sizeof(unsigned char), 1, file);
302
+
303
+// convert the image data from RGB(a) to BGR(A)
304
+	if (mode >= 3)
305
+	for (i=0; i < width * height * mode ; i+= mode)
306
+	{
307
+		aux = imageData[i];
308
+		imageData[i] = imageData[i+2];
309
+		imageData[i+2] = aux;
310
+	}
311
+
312
+// save the image data
313
+	w = 1;
314
+	while (w < width) w = w << 1;
315
+//	bytesPerPixel = pixelDepth/8;	
316
+//	row = width * bytesPerPixel;
317
+	
318
+// Write one row at a time
319
+	for (i = 0; i < height; i++)
320
+	{
321
+		ix = i * w * mode;
322
+		fwrite(&imageData[ix], sizeof(unsigned char), width * mode, file);
323
+	}
324
+
325
+	fclose(file);
326
+// release the memory
327
+	free(imageData);
328
+
329
+	return(TGA_OK);
330
+}
331
+
332
+// Save a TextureData
333
+// Problem: Saves upside down!
334
+void SaveTGA(TextureData *tex, char *filename)
335
+{
336
+	SaveDataToTGA(filename, tex->width, tex->height, 
337
+			tex->bpp, tex->imageData);
338
+}
339
+
340
+void SaveFramebufferToTGA(char *filename, GLint x, GLint y, GLint w, GLint h)
341
+{
342
+	int err;
343
+	void *buffer = malloc(h*w*3);
344
+	glReadPixels(x, y, w, h, GL_RGB, GL_UNSIGNED_BYTE, buffer);
345
+	err = SaveDataToTGA(filename, w, h, 
346
+			3*8, (unsigned char *)buffer);
347
+//	free(buffer); already done
348
+	printf("SaveDataToTGA returned %d\n", err);
349
+}
350
+
351
+