-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcolor-convert.c
399 lines (311 loc) · 10.4 KB
/
color-convert.c
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <string.h>
#include <stdio.h>
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/glx.h>
#include <GL/glext.h>
#define WIDTH 1280
#define HEIGHT 720
#define FILENAME "sample.yuv420"
#define CANVAS_FILENAME "canvas.png"
#define FRAME_TIME_MS 1000 / 30
static uint8_t y_data[WIDTH*HEIGHT];
static uint8_t u_data[WIDTH * HEIGHT / 4];
static uint8_t v_data[WIDTH * HEIGHT / 4];
static GdkPixbuf *canvas_pixbuf;
static GRand *grand;
static float opacity = 0.5;
enum {
TEX_Y,
TEX_U,
TEX_V,
TEX_CANVAS,
N_TEX
};
GLuint textures[N_TEX];
static const char *program=
"uniform sampler2D Ytex;\n"
"uniform sampler2D Utex,Vtex;\n"
"uniform sampler2D canvas_tex;\n"
"uniform float opacity;\n"
"void main(void) {\n"
" float r,g,b,y,u,v;\n"
" float a;\n"
" vec4 txl,ux,vx;\n"
" vec2 nxy;\n"
" nxy = gl_TexCoord[0].xy;\n"
" y = texture2D(Ytex, nxy).r;\n"
" u = texture2D(Utex, nxy*0.5).r;\n"
" v = texture2D(Vtex, nxy*0.5).r;\n"
" y = 1.1643 * (y - 0.0625);\n"
" u = u - 0.5;\n"
" v = v - 0.5;\n"
" r = y + 1.5958*v;\n"
" g = y - 0.39173*u - 0.81290*v;\n"
" b = y + 2.017*u;\n"
" a = texture2D(canvas_tex, nxy).a * opacity;\n"
" gl_FragColor = (1 - a) * vec4(r, g, b, 1.0) + a * texture2D(canvas_tex, nxy);\n"
"}\n";
static void
draw_scene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
{
glTexCoord2i(0, 0);
glVertex2i(0, 0);
glTexCoord2i(1, 0);
glVertex2i(WIDTH, 0);
glTexCoord2i(1, 1);
glVertex2i(WIDTH, HEIGHT);
glTexCoord2i(0, 1);
glVertex2i(0, HEIGHT);
}
glEnd();
}
#define MINIMUM_REGION_WIDTH 100
#define MINIMUM_REGION_HEIGHT 100
static void
upload_data()
{
gboolean alpha;
glActiveTexture(GL_TEXTURE0 + TEX_Y);
glBindTexture(GL_TEXTURE_2D, textures[TEX_Y]);
glTexSubImage2D(GL_TEXTURE_2D,
0, 0, 0,
WIDTH, HEIGHT,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
y_data);
glActiveTexture(GL_TEXTURE0 + TEX_U);
glBindTexture(GL_TEXTURE_2D, textures[TEX_U]);
glTexSubImage2D(GL_TEXTURE_2D,
0, 0, 0,
WIDTH / 2, HEIGHT / 2,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
u_data);
glActiveTexture(GL_TEXTURE0 + TEX_V);
glBindTexture(GL_TEXTURE_2D, textures[TEX_V]);
glTexSubImage2D(GL_TEXTURE_2D,
0, 0, 0,
WIDTH / 2, HEIGHT / 2,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
v_data);
alpha = gdk_pixbuf_get_has_alpha(canvas_pixbuf);
glActiveTexture(GL_TEXTURE0 + TEX_CANVAS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, textures[TEX_CANVAS]);
{
int canvas_width = gdk_pixbuf_get_width(canvas_pixbuf);
int canvas_height = gdk_pixbuf_get_height(canvas_pixbuf);
int x = g_rand_int_range(grand, 0, canvas_width - MINIMUM_REGION_WIDTH - 1);
int y = g_rand_int_range(grand, 0, canvas_height - MINIMUM_REGION_HEIGHT - 1);
int width = g_rand_int_range(grand, MINIMUM_REGION_WIDTH, canvas_width - x - 1);
int height = g_rand_int_range(grand, MINIMUM_REGION_HEIGHT, canvas_height - y - 1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
glPixelStorei(GL_UNPACK_ROW_LENGTH, canvas_width);
glTexSubImage2D(GL_TEXTURE_2D,
0,
x,
y,
width,
height,
alpha?GL_RGBA:GL_RGB,
GL_UNSIGNED_BYTE,
gdk_pixbuf_get_pixels(canvas_pixbuf));
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
}
}
static void
load_data()
{
FILE *file;
size_t read_bytes;
file = fopen(FILENAME, "rb");
g_assert(file != NULL);
read_bytes = fread(y_data, sizeof(y_data), 1, file);
g_assert(read_bytes == 1);
read_bytes = fread(u_data, sizeof(u_data), 1, file);
g_assert(read_bytes == 1);
read_bytes = fread(v_data, sizeof(v_data), 1, file);
g_assert(read_bytes == 1);
//g_assert(feof(file));
fclose(file);
}
static void
setup_textures(GLuint program)
{
GLint location;
gboolean alpha;
glGenTextures(N_TEX, textures);
/* Select texture unit 0 as the active unit and bind the Y texture. */
glActiveTexture(GL_TEXTURE0 + TEX_Y);
location = glGetUniformLocation(program,"Ytex");
glUniform1i(location, TEX_Y); /* Bind Ytex to texture unit 0 */
glBindTexture(GL_TEXTURE_2D, textures[TEX_Y]);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_LUMINANCE,
WIDTH, HEIGHT,
0,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
y_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
/* Select texture unit 1 as the active unit and bind the U texture. */
glActiveTexture(GL_TEXTURE0 + TEX_U);
location = glGetUniformLocation(program, "Utex");
glUniform1i(location, TEX_U); /* Bind Utex to texture unit 1 */
glBindTexture(GL_TEXTURE_2D, textures[TEX_U]);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_LUMINANCE,
WIDTH / 2, HEIGHT / 2,
0,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
u_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
/* Select texture unit 2 as the active unit and bind the V texture. */
glActiveTexture(GL_TEXTURE0 + TEX_V);
location = glGetUniformLocation(program, "Vtex");
glUniform1i(location, TEX_V); /* Bind Vtext to texture unit 2 */
glBindTexture(GL_TEXTURE_2D, textures[TEX_V]);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_LUMINANCE,
WIDTH / 2, HEIGHT / 2,
0,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
v_data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
/* Select texture unit 3 as the active unit and bind the canvas texture. */
glActiveTexture(GL_TEXTURE0 + TEX_CANVAS);
location = glGetUniformLocation(program, "canvas_tex");
glUniform1i(location, TEX_CANVAS); /* Bind Vtext to texture unit 2 */
glBindTexture(GL_TEXTURE_2D, textures[TEX_CANVAS]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
alpha = gdk_pixbuf_get_has_alpha(canvas_pixbuf);
glTexImage2D(GL_TEXTURE_2D,
0,
alpha?GL_RGBA8:GL_RGB,
gdk_pixbuf_get_width(canvas_pixbuf), gdk_pixbuf_get_height(canvas_pixbuf),
0,
alpha?GL_RGBA:GL_RGB,
GL_UNSIGNED_BYTE,
gdk_pixbuf_get_pixels(canvas_pixbuf));
}
static GLuint
compile_program(void)
{
GLuint handle;
GLuint shader;
GLchar log[4096];
GLsizei log_size;
handle = glCreateProgram();
shader = glCreateShader(GL_FRAGMENT_SHADER);
/* Compile the shader. */
glShaderSource(shader, 1, &program,NULL);
glCompileShader(shader);
glGetShaderInfoLog(shader,
sizeof(log),
&log_size,
log);
g_assert(log_size >= 0);
if (log_size > 0) {
g_print("Shader compile log: %s\n", log);
}
/* Create a complete program object. */
glAttachShader(handle, shader);
glLinkProgram(handle);
glGetProgramInfoLog(handle,
sizeof(log),
&log_size,
log);
if (log_size > 0) {
g_print("Program link log: %s\n", log);
}
return handle;
}
#define OPACITY_INCREMENT 0.01
int
main (int argc, char **argv)
{
GTimer *timer;
int frame_count;
GLuint handle;
GLenum err;
GTimer *frame_timer;
double direction = OPACITY_INCREMENT;
g_type_init();
grand = g_rand_new();
frame_timer = g_timer_new();
g_timer_start(frame_timer);
load_data();
canvas_pixbuf = gdk_pixbuf_new_from_file(CANVAS_FILENAME, NULL);
g_assert(canvas_pixbuf);
glutInit(&argc, argv);
glutCreateWindow("color-convert");
glutFullScreen();
err = glewInit();
g_assert(err == GLEW_OK);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, (float) WIDTH, (float) HEIGHT, 0.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDepthRange(0.0f, 1.0f);
glViewport(0, 0, WIDTH, HEIGHT);
glRasterPos2f(0.0f, 0.0f);
glEnable(GL_TEXTURE_2D);
handle = compile_program();
glUseProgram(handle);
setup_textures(handle);
frame_count = 1;
timer = g_timer_new();
while (TRUE) {
double elapsed;
GLint location;
opacity += direction;
if (opacity >= 1.0) {
direction = -OPACITY_INCREMENT;
} else if (opacity <= 0.0) {
direction = OPACITY_INCREMENT;
}
location = glGetUniformLocation(handle, "opacity");
glUniform1f(location, opacity);
upload_data();
draw_scene();
glutSwapBuffers();
frame_count ++;
elapsed = g_timer_elapsed(frame_timer, NULL) * 1000;
if (elapsed < FRAME_TIME_MS) {
g_usleep(1000 * (FRAME_TIME_MS - elapsed));
}
g_timer_start(frame_timer);
if (frame_count % 1000 == 0) {
double fps;
fps = 1000 / g_timer_elapsed(timer, NULL);
g_timer_start(timer);
g_print("Framerate: %gfps\n", fps);
}
}
}