1 Commits

Author SHA1 Message Date
ebf910165f imgmagick++ kinda works, but its still slow 2024-05-12 21:21:17 -04:00
7 changed files with 49 additions and 273 deletions

View File

@ -21,11 +21,11 @@ SOURCES += $(wildcard $(IMGUI_DIR)/*.cpp)
SOURCES += $(wildcard $(IMGUI_DIR)/backends/*.cpp)
OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
UNAME_S := $(shell uname -s)
LINUX_GL_LIBS = -lGL -lGLEW
LINUX_GL_LIBS = -lGL
CXXFLAGS = -std=c++20 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
CXXFLAGS += -g -DIMGUI_DEFINE_MATH_OPERATORS
LIBS =
CXXFLAGS += -g -DIMGUI_DEFINE_MATH_OPERATORS -Ofast -Xpreprocessor -fopenmp -DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16 -DMAGICKCORE_CHANNEL_MASK_DEPTH=32 -I/opt/homebrew/Cellar/imagemagick/7.1.1-32/include/ImageMagick-7
LIBS = -L/opt/homebrew/Cellar/imagemagick/7.1.1-32/lib -lMagick++-7.Q16HDRI -lMagickWand-7.Q16HDRI -lMagickCore-7.Q16HDRI
##---------------------------------------------------------------------
## OPENGL ES
@ -52,7 +52,7 @@ endif
ifeq ($(UNAME_S), Darwin) #APPLE
ECHO_MESSAGE = "Mac OS X"
LIBS += -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo `sdl2-config --libs` -lGLEW
LIBS += -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo `sdl2-config --libs`
LIBS += -L/usr/local/lib -L/opt/local/lib
CXXFLAGS += `sdl2-config --cflags`

View File

@ -32,7 +32,7 @@ EMS =
##---------------------------------------------------------------------
# ("EMS" options gets added to both CPPFLAGS and LDFLAGS, whereas some options are for linker only)
EMS += -s USE_SDL=2 -flto -O2 -s USE_WEBGL2=1
EMS += -s USE_SDL=2 -flto -O2
EMS += -s DISABLE_EXCEPTION_CATCHING=1
LDFLAGS += -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=0 -s ASSERTIONS=1 -sEXPORTED_FUNCTIONS=[_main,_malloc,_free] -sEXPORTED_RUNTIME_METHODS=[ccall]

View File

@ -1,58 +0,0 @@
// FrameBuffer.cpp
#include "FrameBuffer.h"
FrameBuffer::FrameBuffer(float width, float height) {
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
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,
texture, 0);
glGenRenderbuffers(1, &rbo);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
GL_RENDERBUFFER, rbo);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!"
<< std::endl;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}
FrameBuffer::~FrameBuffer() {
glDeleteFramebuffers(1, &fbo);
glDeleteTextures(1, &texture);
glDeleteRenderbuffers(1, &rbo);
}
unsigned int FrameBuffer::getFrameTexture() { return texture; }
void FrameBuffer::RescaleFrameBuffer(float width, float height) {
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
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,
texture, 0);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
GL_RENDERBUFFER, rbo);
}
void FrameBuffer::Bind() const { glBindFramebuffer(GL_FRAMEBUFFER, fbo); }
void FrameBuffer::Unbind() const { glBindFramebuffer(GL_FRAMEBUFFER, 0); }

View File

@ -1,38 +0,0 @@
// FrameBuffer.h
#ifdef __EMSCRIPTEN__
#else
#include "GL/glew.h"
#endif
#pragma once
#include <iostream>
#if defined(__APPLE__)
#include <SDL2/SDL.h>
#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <SDL2/SDL_opengles.h>
#else
#include <SDL2/SDL_opengl.h>
#endif
#else
#include <SDL.h>
#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <SDL_opengles2.h>
#else
#include <SDL_opengl.h>
#endif
#endif
class FrameBuffer {
public:
FrameBuffer(float width, float height);
~FrameBuffer();
unsigned int getFrameTexture();
void RescaleFrameBuffer(float width, float height);
void Bind() const;
void Unbind() const;
private:
unsigned int fbo;
unsigned int texture;
unsigned int rbo;
};

View File

@ -1,6 +1,6 @@
#include <cstdint>
#include "../ImNodeFlow.h"
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <ostream>
@ -20,11 +20,17 @@
#endif
#endif
#include "../ImNodeFlow.h"
#include "../image_model.h"
#include "../imfilebrowser.h"
#include "../stb_image.h"
#include "../uuid.h"
#include <cstdint>
#include <iostream>
#include <ImageMagick-7/Magick++.h>
#include <chrono>
using namespace std::chrono;
#ifdef __EMSCRIPTEN__
#include "../emscripten_browser_file.h"
@ -53,11 +59,7 @@ public:
});
}
~Exposure() {
if (returnBundle.loaded) {
stbi_image_free(returnBundle.image);
}
}
~Exposure() { stbi_image_free(returnBundle.image); }
void draw() override {
ImGui::SetNextItemWidth(100);
@ -161,11 +163,7 @@ public:
});
}
~Contrast() {
if (returnBundle.loaded) {
stbi_image_free(returnBundle.image);
}
}
~Contrast() { stbi_image_free(returnBundle.image); }
void draw() override {
ImGui::SetNextItemWidth(100);
@ -241,16 +239,34 @@ private:
return static_cast<int>(ret_val);
}
void adjustContrast(uint8_t *pixels, uint8_t *out, int width, int height,
double contrastValue) {
// Initialize Magick library
Magick::InitializeMagick(nullptr);
// Create a Magick::Image object from the uint8_t* data
Magick::Image image(width, height, "RGBA", Magick::CharPixel, pixels);
// Apply contrast adjustment
auto start = high_resolution_clock::now();
image.sigmoidalContrast(true, contrastValue); // Increase contrast
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
std::cout << duration.count() << std::endl;
start = high_resolution_clock::now();
Magick::Blob blob;
image.write(&blob);
memcpy(out, blob.data(), width * height * 4);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
std::cout << duration.count() << std::endl;
}
void compute(uint8_t *input, uint8_t *output, int size, int channels) {
int nBval = brightnessVal + 256;
for (int i = 0; i < size; i += channels) {
uint8_t r = std::clamp(formula(input[i], brightnessVal), 0, 255);
uint8_t g = std::clamp(formula(input[i + 1], brightnessVal), 0, 255);
uint8_t b = std::clamp(formula(input[i + 2], brightnessVal), 0, 255);
output[i] = r;
output[i + 1] = g;
output[i + 2] = b;
}
adjustContrast(input, output, returnBundle.width, returnBundle.height,
nBval);
}
int brightnessVal = 0;
@ -270,11 +286,7 @@ public:
});
}
~SimpleBrightness() {
if (returnBundle.loaded) {
stbi_image_free(returnBundle.image);
}
}
~SimpleBrightness() { stbi_image_free(returnBundle.image); }
void draw() override {
ImGui::SetNextItemWidth(100);
@ -478,8 +490,8 @@ public:
}
#else
if (bundle.fname == "") {
handle_upload_file_local(
"/Users/tanishqdubey/Downloads/Odd-eyed_cat_by_ihasb33r.jpg", this);
handle_upload_file_local("/Users/tanishqdubey/Pictures/DSC06956.JPG",
this);
}
#endif
if (bundle.fname != "") {

148
main.cpp
View File

@ -8,13 +8,6 @@
// - Documentation https://dearimgui.com/docs (same as your local docs/
// folder).
// - Introduction, links and more at the top of imgui.cpp
//
#include <cstdint>
#ifdef __EMSCRIPTEN__
#else
#include "GL/glew.h"
#endif
#include <cstddef>
#define IMGUI_DEFINE_MATH_OPERATORS
@ -50,138 +43,13 @@
#include "lib/emscripten/emscripten_mainloop_stub.h"
#endif
#include "lib/FrameBuffer.h"
#include "lib/ImNodeFlow.h"
#include <ImageMagick-7/Magick++.h>
static bool init = true;
GLuint initShader(const char *vShader, const char *fShader,
const char *outputAttributeName) {
struct Shader {
GLenum type;
const char *source;
} shaders[2] = {{GL_VERTEX_SHADER, vShader}, {GL_FRAGMENT_SHADER, fShader}};
GLuint program = glCreateProgram();
for (int i = 0; i < 2; ++i) {
Shader &s = shaders[i];
GLuint shader = glCreateShader(s.type);
glShaderSource(shader, 1, (const GLchar **)&s.source, NULL);
glCompileShader(shader);
GLint compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
std::cerr << " failed to compile:" << std::endl;
GLint logSize;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logSize);
char *logMsg = new char[logSize];
glGetShaderInfoLog(shader, logSize, NULL, logMsg);
std::cerr << logMsg << std::endl;
delete[] logMsg;
exit(EXIT_FAILURE);
}
glAttachShader(program, shader);
}
/* Link output */
#ifndef EMSCRIPTEN
glBindFragDataLocation(program, 0, outputAttributeName);
#endif
/* link and error check */
glLinkProgram(program);
GLint linked;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (!linked) {
std::cerr << "Shader program failed to link" << std::endl;
GLint logSize;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logSize);
char *logMsg = new char[logSize];
glGetProgramInfoLog(program, logSize, NULL, logMsg);
std::cerr << logMsg << std::endl;
delete[] logMsg;
exit(EXIT_FAILURE);
}
/* use program object */
glUseProgram(program);
return program;
}
uint8_t loadShader() {
#ifdef EMSCRIPTEN
const char *vert = R"(#version 100
uniform vec2 offset;
attribute vec4 position;
attribute vec2 uv;
varying vec2 vUV;
void main (void)
{
vUV = uv;
gl_Position = position + vec4(offset,0.0,0.0);
})";
#else
const char *vert = R"(#version 150
uniform vec2 offset;
in vec4 position;
in vec2 uv;
out vec2 vUV;
void main (void)
{
vUV = uv;
gl_Position = position + vec4(offset,0.0,0.0);
})";
#endif
#ifdef EMSCRIPTEN
const char *frag = R"(#version 100
precision mediump float;
varying vec2 vUV;
uniform sampler2D tex;
void main(void)
{
gl_FragColor = texture2D(tex,vUV);
})";
#else
const char *frag = R"(#version 150
in vec2 vUV;
out vec4 fragColor;
uniform sampler2D tex;
void main(void)
{
fragColor = texture(tex,vUV);
})";
#endif
std::cout << "Starting to init" << std::endl;
uint8_t shaderProgram = initShader(vert, frag, "fragColor");
std::cout << "done to init" << std::endl;
uint8_t uvAttribute = glGetAttribLocation(shaderProgram, "uv");
if (uvAttribute < 0) {
std::cerr << "Shader did not contain the 'color' attribute." << std::endl;
}
uint8_t positionAttribute = glGetAttribLocation(shaderProgram, "position");
if (positionAttribute < 0) {
std::cerr << "Shader did not contain the 'position' attribute."
<< std::endl;
}
return shaderProgram;
}
uint8_t setup() { return loadShader(); }
// Main code
int main(int, char **) {
int main(int, char **argv) {
// Setup SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) !=
0) {
@ -189,6 +57,8 @@ int main(int, char **) {
return -1;
}
Magick::InitializeMagick(*argv);
// Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
// GL ES 2.0 + GLSL 100
@ -272,9 +142,7 @@ int main(int, char **) {
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
ImFlow::ImNodeFlow editor = ImFlow::ImNodeFlow("Nodes");
uint8_t shader = 0;
// Main loop
bool done = false;
#ifdef __EMSCRIPTEN__
// For an Emscripten build we are disabling file-system access, so let's not
@ -336,8 +204,6 @@ int main(int, char **) {
ImGui::DockBuilderSetNodeSize(dockspace_id, viewport->Size);
ImGui::DockBuilderDockWindow("Editor", dockspace_id);
ImGui::DockBuilderFinish(dockspace_id);
// setup_shaders();
}
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("Add")) {
@ -367,12 +233,6 @@ int main(int, char **) {
ImGui::End();
ImGui::End();
ImGui::PopStyleVar();
if (shader != 0) {
ImGui::Begin("Shader test");
ImGui::Text("Shader ID: %d", shader);
ImGui::End();
}
}
// Rendering

BIN
ppdnode

Binary file not shown.