File: thumbnail.cpp

package info (click to toggle)
sludge 2.2.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,852 kB
  • sloc: cpp: 32,432; sh: 1,237; makefile: 634; xml: 284
file content (231 lines) | stat: -rw-r--r-- 6,769 bytes parent folder | download | duplicates (4)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <stdint.h>

#include "allfiles.h"
#include "errors.h"
#include "moreio.h"
#include "version.h"
#include "sludger.h"
#include "colours.h"
#include "backdrop.h"
#include "graphics.h"
#include "newfatal.h"

bool freeze ();
void unfreeze (bool);	// Because FREEZE.H needs a load of other includes

int thumbWidth = 0, thumbHeight = 0;

extern GLuint backdropTextureName;


bool saveThumbnail (FILE * fp) {
	GLuint thumbnailTextureName = 0;

	put4bytes (thumbWidth, fp);
	put4bytes (thumbHeight, fp);

	if (thumbWidth && thumbHeight) {
		if (! freeze ()) return false;


		setPixelCoords (true);
		glUseProgram(shader.texture);
		setPMVMatrix(shader.texture);

		glGenTextures (1, &thumbnailTextureName);
		glBindTexture(GL_TEXTURE_2D, thumbnailTextureName);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		texImage2D (GL_TEXTURE_2D, 0, GL_RGBA, thumbWidth, thumbHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, thumbnailTextureName);

		// Render the backdrop (scaled)
		//glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
		glBindTexture (GL_TEXTURE_2D, backdropTextureName);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

		const GLfloat vertices[] = { 
			0., 0., 0., 
			thumbWidth-1.f, 0., 0., 
			0., thumbHeight-1.f, 0.,
			thumbWidth-1.f, thumbHeight-1.f, 0.
		};

		const GLfloat texCoords[] = { 
			0.0f, 0.0f,
			backdropTexW, 0.0f,
			0.0f, backdropTexH,
			backdropTexW, backdropTexH
		}; 

		drawQuad(shader.texture, vertices, 1, texCoords);

		if (gameSettings.antiAlias < 0) {
			glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		} else {
			glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		}
		deleteTextures (1, &thumbnailTextureName);
		thumbnailTextureName = 0;
		
		// Save Our ViewPort
		#ifdef HAVE_GLES2
		GLushort* image = new GLushort [thumbWidth*thumbHeight];
		GLuint* tmp = new GLuint [thumbWidth*thumbHeight];
		if (! checkNew (image)) return false;
		glReadPixels(viewportOffsetX, viewportOffsetY, thumbWidth, thumbHeight, GL_RGBA, GL_UNSIGNED_BYTE, tmp);
		for (int y = 0; y < thumbHeight; y ++) {
			for (int x = 0; x < thumbWidth; x ++) {
				const GLuint a = tmp[y*thumbWidth+x];
				image[y*thumbWidth+x] =  ((a&0x00f80000)>>(16+3)) | ((a&0x0000fc00)>>(8+2-5)) | ((a&0x000000f8)<<(11-3));
			}
		}
		delete[] tmp;
		#else
		GLushort* image = new GLushort [thumbWidth*thumbHeight];
		if (! checkNew (image)) return false;
		glReadPixels(viewportOffsetX, viewportOffsetY, thumbWidth, thumbHeight, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, image);
		#endif

		glUseProgram(0);
		setPixelCoords (false);

		for (int y = 0; y < thumbHeight; y ++) {
			for (int x = 0; x < thumbWidth; x ++) {
				put2bytes ((*(image +y*thumbWidth+x)), fp);
			}
		}
		delete[] image;
		image = NULL;
		unfreeze (true);
	}
	fputc ('!', fp);
	return true;
}

void showThumbnail (char * filename, int atX, int atY) {
	GLubyte * thumbnailTexture = NULL;
	GLuint thumbnailTextureName = 0;

	GLfloat texCoordW = 1.0;
	GLfloat texCoordH = 1.0;	
	
	int ssgVersion;
	FILE * fp = openAndVerify (filename, 'S', 'A', ERROR_GAME_LOAD_NO, ssgVersion);
	if (ssgVersion >= VERSION(1,4)) {
		if (fp == NULL) return;
		int fileWidth = get4bytes (fp);
		int fileHeight = get4bytes (fp);

		int picWidth = fileWidth;
		int picHeight = fileHeight;
		if (! NPOT_textures) {
			picWidth = getNextPOT(picWidth);
			picHeight = getNextPOT(picHeight);
			texCoordW = ((double)fileWidth) / picWidth;
			texCoordH = ((double)fileHeight) / picHeight;
			
		}

		thumbnailTexture = new GLubyte [picHeight*picWidth*4];
		if (thumbnailTexture == NULL) return;

		int t1, t2;
		unsigned short c;
		GLubyte * target;
		for (t2 = 0; t2 < fileHeight; t2 ++) {
			t1 = 0;
			while (t1 < fileWidth) {
				c = (unsigned short) get2bytes (fp);
				target = thumbnailTexture + 4*picWidth*t2 + t1*4;
				target[0] = (GLubyte) redValue(c);
				target[1] = (GLubyte) greenValue(c);
				target[2] = (GLubyte) blueValue(c);
				target[3] = (GLubyte) 255;
				t1++;
			}
		}

		fclose (fp);

		glGenTextures (1, &thumbnailTextureName);
		glBindTexture(GL_TEXTURE_2D, thumbnailTextureName);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		texImage2D (GL_TEXTURE_2D, 0, GL_RGBA, picWidth, picHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, thumbnailTexture, thumbnailTextureName);

		delete thumbnailTexture;
		thumbnailTexture = NULL;

		if (atX<0){
			fileWidth+= atX;
			atX=0;
		}
		if (atY<0){
			fileHeight+= atY;
			atY=0;
		}
		if (fileWidth + atX > sceneWidth) fileWidth = sceneWidth-atX;
		if (fileHeight + atY > sceneHeight) fileHeight = sceneHeight-atY;

		setPixelCoords (true);

		glUseProgram(shader.texture);
		setPMVMatrix(shader.texture);

		int xoffset = 0;
		while (xoffset < fileWidth) {
			int w = (fileWidth-xoffset < viewportWidth) ? fileWidth-xoffset : viewportWidth;

			int yoffset = 0;
			while (yoffset < fileHeight) {
				int h = (fileHeight-yoffset < viewportHeight) ? fileHeight-yoffset : viewportHeight;

				glBindTexture (GL_TEXTURE_2D, thumbnailTextureName);

				const GLfloat vertices[] = { 
					(GLfloat)fileWidth-1.f-xoffset, (GLfloat)-yoffset, 0., 
					(GLfloat)-xoffset, (GLfloat)-yoffset, 0., 
					(GLfloat)fileWidth-1.f-xoffset, (GLfloat)fileHeight-1.f-yoffset, 0.,
					(GLfloat)-xoffset, (GLfloat)fileHeight-1.f-yoffset, 0.
				};

				const GLfloat texCoords[] = { 
					texCoordW, 0.0f,
					0.0f, 0.0f,
					texCoordW, texCoordH,
					0.0f, texCoordH
				}; 
	
				drawQuad(shader.texture, vertices, 1, texCoords);

				glDisable(GL_BLEND);

				// Copy Our ViewPort To The Texture
				copyTexSubImage2D(GL_TEXTURE_2D, 0, atX+xoffset, atY+yoffset, viewportOffsetX, viewportOffsetY, w, h, backdropTextureName);

				yoffset += viewportHeight;
			}
			xoffset += viewportWidth;
		}
		glUseProgram(0);
		setPixelCoords (false);
		deleteTextures (1, &thumbnailTextureName);
		thumbnailTextureName = 0;
	}
}

bool skipThumbnail (FILE * fp) {
	thumbWidth = get4bytes (fp);
	thumbHeight = get4bytes (fp);
	uint32_t skippy = thumbWidth;
	skippy *= thumbHeight << 1;
	fseek (fp, skippy, 1);
	return (fgetc (fp) == '!');
}