-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.cpp
506 lines (441 loc) · 16.4 KB
/
image.cpp
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#include "image.h"
#include "imgui.h"
#include "debug.h"
GLuint shaderHandle = 0, vertHandle = 0, fragHandle = 0;
// this works with the TRIANGLE/RECTANGLE
const GLchar* vertex_shader =
"in vec3 aPos;\n" // attributes 0
"in vec2 aTexCoords;\n" // attributes 1
"out vec2 TexCoords;\n" // for fragment shader
"void main()\n"
"{\n"
" gl_Position = vec4(aPos, 1.0);\n"
" TexCoords = aTexCoords;\n"
"}\n";
// https://gamedev.stackexchange.com/questions/43294/creating-a-retro-style-palette-swapping-effect-in-opengl
const GLchar* fragment_shader =
"out vec4 FragColor;\n"
"in vec2 TexCoords;\n" // from vertex shader
"uniform sampler2D screenTexture;\n" // our texture (grayscale)
"uniform sampler2D ColorTable;\n" // our palette
"\n"
"void main()\n"
"{\n"
// pick up a pixel intensity from grayscale image
" vec4 index = texture2D(screenTexture, TexCoords);\n"
// generate x,y index into palette holding the color for this intensity
" float xp = mod(index.r, 256.0);\n"
" float yp = index.r / 256.0;\n"
// retrieve the actual color from the palette
" vec4 texel = texture2D(ColorTable, vec2(xp, yp));\n"
// output the color
" FragColor = texel;"
"}\n";
// If you get an error please report on github. You may try different GL context version or GLSL version. See GL<>GLSL version table at the top of this file.
static bool CheckShader(GLuint handle, const char* desc)
{
GLint status = 0, log_length = 0;
glGetShaderiv(handle, GL_COMPILE_STATUS, &status);
glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length);
if ((GLboolean)status == GL_FALSE)
E("failed to compile %s!\n", desc);
if (log_length > 1)
{
ImVector<char> buf;
buf.resize((int)(log_length + 1));
glGetShaderInfoLog(handle, log_length, NULL, (GLchar*)buf.begin());
E("%s\n", buf.begin());
}
return (GLboolean)status == GL_TRUE;
}
const char* g_glsl_version = "#version 130\n";
// If you get an error please report on GitHub. You may try different GL context version or GLSL version.
static bool CheckProgram(GLuint handle, const char* desc)
{
GLint status = 0, log_length = 0;
glGetProgramiv(handle, GL_LINK_STATUS, &status);
glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length);
if ((GLboolean)status == GL_FALSE)
E("failed to link %s! (with GLSL '%s')\n", desc, g_glsl_version);
if (log_length > 1)
{
ImVector<char> buf;
buf.resize((int)(log_length + 1));
glGetProgramInfoLog(handle, log_length, NULL, (GLchar*)buf.begin());
E("%s\n", buf.begin());
}
return (GLboolean)status == GL_TRUE;
}
#if 0
// from https://github.com/kbinani/colormap-shaders
// Matlab JET colormap
struct vec4
{
vec4(float a0, float a1, float a2, float a3)
: x(a0)
, y(a1)
, z(a2)
, w(a3)
{
}
union {
double r;
double x;
};
union {
double g;
double y;
};
union {
double b;
double z;
};
union {
double a;
double w;
};
};
float colormap_red(float x) {
if (x < 0.7) {
return 4.0 * x - 1.5;
} else {
return -4.0 * x + 4.5;
}
}
float colormap_green(float x) {
if (x < 0.5) {
return 4.0 * x - 0.5;
} else {
return -4.0 * x + 3.5;
}
}
float colormap_blue(float x) {
if (x < 0.3) {
return 4.0 * x + 0.5;
} else {
return -4.0 * x + 2.5;
}
}
float clamp(float v, float min, float max) {
if (v < min) {
return min;
} else if (max < v) {
return max;
} else {
return v;
}
}
vec4 colormap(float x) {
float r = clamp(colormap_red(x), 0.0, 1.0);
float g = clamp(colormap_green(x), 0.0, 1.0);
float b = clamp(colormap_blue(x), 0.0, 1.0);
return vec4(r, g, b, 1.0);
}
void Image::initColorMap(void) {
float v;
for (int i = 0; i < 256; i++) {
v = (float)i / 255.0;
// make JET colomap
colorMap[i][0] = (unsigned char)(clamp(colormap_red(v), 0.0, 1.0) * 255.0);
colorMap[i][1] = (unsigned char)(clamp(colormap_green(v), 0.0, 1.0) * 255.0);
colorMap[i][2] = (unsigned char)(clamp(colormap_blue(v), 0.0, 1.0) * 255.0);
colorMap[i][3] = 255;
// fprintf(stderr, "RGBA %3d %3d %3d %3d\n", colorMap[i][0], colorMap[i][1], colorMap[i][2], colorMap[i][3]);
}
}
// Matlab JET colormap
#endif
Image::Image() {
// default image properties
imageWidth = 512;
imageHeight = 512;
imageDepth = 8;
scaleWidth = 1.0f;
scaleHeight = 1.0f;
//RECTANGLE CREATION//
// half size
// float vertices0[] = {
// 0.5f, 0.5f, 0.0f, // top right
// 0.5f, -0.5f, 0.0f, // bottom right
// -0.5f, -0.5f, 0.0f, // bottom left
// -0.5f, 0.5f, 0.0f // top left
// };
// full size
float vertices[] = {
1.0f, 1.0f, 0.0f, // top right
1.0f, -1.0f, 0.0f, // bottom right
-1.0f, -1.0f, 0.0f, // bottom left
-1.0f, 1.0f, 0.0f // top left
};
unsigned int indices[] = { // note that we start from 0!
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
// texture
float Tvertices2[] = {
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f,
0.0f, 1.0f
};
unsigned int vbo2, vbo;
glGenBuffers(1, &ebo);
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glGenBuffers(1, &vbo2);
glBindBuffer(GL_ARRAY_BUFFER, vbo2);
glBufferData(GL_ARRAY_BUFFER, sizeof(Tvertices2), Tvertices2, GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Create shaders
const GLchar* vertex_shader_with_version[2] = { g_glsl_version, vertex_shader };
vertHandle = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertHandle, 2, vertex_shader_with_version, NULL);
glCompileShader(vertHandle);
CheckShader(vertHandle, "vertex shader");
const GLchar* fragment_shader_with_version[2] = { g_glsl_version, fragment_shader };
fragHandle = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragHandle, 2, fragment_shader_with_version, NULL);
glCompileShader(fragHandle);
CheckShader(fragHandle, "fragment shader");
shaderHandle = glCreateProgram();
glAttachShader(shaderHandle, vertHandle);
glAttachShader(shaderHandle, fragHandle);
glLinkProgram(shaderHandle);
CheckProgram(shaderHandle, "shader program");
// framebuffer configuration
// -------------------------
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// create a color attachment texture
glGenTextures(1, &colorTexture);
glBindTexture(GL_TEXTURE_2D, colorTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1280, 720, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0);
// now that we actually created the framebuffer and added all attachments we want to check if it is actually complete now
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
E("Framebuffer is not complete!");
}
// GLuint glError = glGetError();
// if (glError != GL_NO_ERROR) {
// E("glGetError() returned 0x%04X\n", glError);
// }
// assert(glError == 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// initColorMap();
// Create a OpenGL texture identifier for color map
glGenTextures(1, &paletteTexture);
glBindTexture(GL_TEXTURE_2D, paletteTexture);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Upload pixels into texture
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
// GL_RGBA8 = 8 bits got each color and alpha
// XXX: To change texels in an already existing 2d texture, use glTexSubImage2D
// https://www.khronos.org/opengl/wiki/Common_Mistakes#Updating_a_texture
// glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, colorMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// glError = glGetError();
// if (glError != GL_NO_ERROR) {
// E("glGetError() returned 0x%04X\n", glError);
// }
// assert(glError == 0);
// Create a OpenGL texture identifier for raw image
glGenTextures(1, &rawTexture);
glBindTexture(GL_TEXTURE_2D, rawTexture);
// Setup filtering parameters for display
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Upload pixels into texture
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
// glError = glGetError();
// if (glError != GL_NO_ERROR) {
// E("glGetError() returned 0x%04X\n", glError);
// }
// assert(glError == 0);
// we expect a R8 type of pixel data wo/ alpha
// GL_RED means single channels
// GL_R8 is for 8 bit indexed images
// XXX: make this work for 16 bit images as well
// XXX: To change texels in an already existing 2d texture, use glTexSubImage2D
// https://www.khronos.org/opengl/wiki/Common_Mistakes#Updating_a_texture
// glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 4096, 4096, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
// default to 8bit image
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, imageWidth, imageHeight, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
// glError = glGetError();
// if (glError != GL_NO_ERROR) {
// E("ERROR: glGetError() returned 0x%04X\n", glError);
// }
// assert(glError == 0);
glUseProgram(shaderHandle);
// set shader uniform index for indexed image: 0
glUniform1i(glGetUniformLocation(shaderHandle, "screenTexture"), 0);
// glError = glGetError();
// if (glError != GL_NO_ERROR) {
// E("glGetError() returned 0x%04X\n", glError);
// }
// assert(glError == 0);
// set shader uniform index for colomap array: 1
glUniform1i(glGetUniformLocation(shaderHandle, "ColorTable"), 1);
// glError = glGetError();
// if (glError != GL_NO_ERROR) {
// E("glGetError() returned 0x%04X\n", glError);
// }
// assert(glError == 0);
}
Image::~Image() {
}
void Image::updateImage(const unsigned int _width, const unsigned int _height, const unsigned int _depth, const void *_data) {
assert(rawTexture != 0);
// GLuint glError;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, rawTexture);
imageWidth = _width;
imageHeight = _height;
if (imageDepth != _depth) {
imageDepth = _depth;
D("changing depth to %d\n", imageDepth);
if (imageDepth == 8) {
// 8 bit image
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, imageWidth, imageHeight, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
} else if (imageDepth == 16) {
// 16 bit image
glTexImage2D(GL_TEXTURE_2D, 0, GL_R16, imageWidth, imageHeight, 0, GL_RED, GL_UNSIGNED_SHORT, NULL);
}
GLint format;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format);
D("rawTexture using internal format 0x%X\n", format);
}
GLenum format = -1;
if (imageDepth == 8) {
// 8 bit image
format = GL_UNSIGNED_BYTE;
} else if (imageDepth == 16) {
// 16 bit image
format = GL_UNSIGNED_SHORT;
}
// we expect a R8 type of pixel data wo/ alpha
// GL_RED means single channels
// GL_R8 is for 8 bit indexed images
// XXX: make this work for 16 bit images as well
// XXX: To change texels in an already existing 2d texture, use glTexSubImage2D
// https://www.khronos.org/opengl/wiki/Common_Mistakes#Updating_a_texture
//glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, _width, _height, 0, GL_RED, GL_UNSIGNED_BYTE, _data);
glTexSubImage2D(GL_TEXTURE_2D,
0,
0,
0,
imageWidth,
imageHeight,
GL_RED,
format,//GL_UNSIGNED_BYTE,
_data);
// glError = glGetError();
// if (glError != GL_NO_ERROR) {
// E("ERROR: glGetError() returned 0x%04X\n", glError);
// }
// assert(glError == 0);
// imageWidth = _width;
// imageHeight = _height;
// render to FBO
// bind to framebuffer and draw scene as we normally would to color texture
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// make sure we clear the framebuffer's content
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderHandle);
// // set shader uniform index for indexed image: 0
// glUniform1i(glGetUniformLocation(shaderHandle, "screenTexture"), 0);
// glError = glGetError();
// if (glError != GL_NO_ERROR) {
// E("glGetError() returned 0x%04X\n", glError);
// }
// assert(glError == 0);
// // set shader uniform index for colomap array: 1
// glUniform1i(glGetUniformLocation(shaderHandle, "ColorTable"), 1);
// glError = glGetError();
// if (glError != GL_NO_ERROR) {
// E("glGetError() returned 0x%04X\n", glError);
// }
// assert(glError == 0);
// rectangle vertex array
glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
// indexed image 2D texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, rawTexture);
// colormap 1D texture
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, paletteTexture);
// GL_RGBA8 = 8 bits got each color and alpha
// XXX: To change texels in an already existing 2d texture, use glTexSubImage2D
// https://www.khronos.org/opengl/wiki/Common_Mistakes#Updating_a_texture
//glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, colorMap);
// glError = glGetError();
// if (glError != GL_NO_ERROR) {
// E("glGetError() returned 0x%04X\n", glError);
// }
// assert(glError == 0);
// render the image to fbo
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// now bind back to default framebuffer (for ImGui)
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glActiveTexture(GL_TEXTURE0);
}
void Image::updateScale(const double _scaleWidth, const double _scaleHeight) {
assert(_scaleWidth > 0.0 && _scaleWidth <= 1.0);
assert(_scaleHeight > 0.0 && _scaleHeight <= 1.0);
scaleWidth = _scaleWidth;
scaleHeight = _scaleHeight;
}
void Image::updatePalette(const unsigned int _depth, const void *_data) {
assert(paletteTexture != 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, paletteTexture);
//glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, colorMap);
unsigned int width = 256;
unsigned int height = 0;
if (_depth == 8) {
height = 1;
} else if (_depth == 16) {
height = 256;
}
assert(height != 0);
glTexSubImage2D(GL_TEXTURE_2D,
0,
0,
0,
width,
height,
GL_RGBA,
GL_UNSIGNED_BYTE,
_data);
// GLuint glError = glGetError();
// if (glError != GL_NO_ERROR) {
// E("ERROR: glGetError() returned 0x%04X\n", glError);
// }
// assert(glError == 0);
glActiveTexture(GL_TEXTURE0);
}
void Image::render(void) {
assert(rawTexture != 0);
//ImGui::Image((void*)(intptr_t)rawTexture, ImVec2(imageWidth, imageHeight));
ImGui::Image((void*)(intptr_t)colorTexture, ImVec2(imageWidth * scaleWidth, imageHeight * scaleHeight), ImVec2(0.0f, 0.0f), ImVec2(1.0f, 1.0f));
}