<commit_before>// augmentation_widget.cpp

#include "augmentation_widget.hpp"

#include <QOpenGLExtraFunctions>
#include <QTemporaryFile>
#include <QVector2D>
#include <QVector3D>
#include <math.h>

augmentation_widget::augmentation_widget (QWidget* parent)
: QOpenGLWidget (parent)
, _scale_factor (1.0f)
, _x_pos (0.0f)
, _y_pos (0.0f)
, _opengl_mutex (QMutex::Recursive)
, _last_handle (0)
, _vertex_count (0) {
}

augmentation_widget::~augmentation_widget () {
    _opengl_mutex.lock ();
    glDeleteBuffers (1, &_object_vbo);
    glDeleteVertexArrays (1, &_object_vao);
    _opengl_mutex.unlock ();
}

QSize augmentation_widget::minimumSizeHint () const {
    return QSize (600, 350);
}

QSize augmentation_widget::sizeHint () const {
    return QSize (600, 350);
}

bool augmentation_widget::loadObject (const QString& resource_path) {
    _opengl_mutex.lock ();
    bool status = false;

    // extract model from resources into filesystem and parse it
    QFile resource_file (resource_path);
    if (resource_file.exists ()) {
        auto temp_file  = QTemporaryFile::createNativeFile (resource_file);
        QString fs_path = temp_file->fileName ();

        if (!fs_path.isEmpty ()) {
            std::vector<float> model_interleaved = _object.load (fs_path.toStdString ());

            glBindBuffer (GL_ARRAY_BUFFER, _object_vbo);
            glBufferData (GL_ARRAY_BUFFER, sizeof (float) * model_interleaved.size (),
            model_interleaved.data (), GL_STATIC_DRAW);
            glBindBuffer (GL_ARRAY_BUFFER, 0);
            _vertex_count = model_interleaved.size () / _object.data_per_vertex ();
            _object.release ();

            status = true;
        }
    }
    _opengl_mutex.unlock ();
    return status;
}

void augmentation_widget::downloadImage (image_t& image, GLuint handle) {
    _opengl_mutex.lock ();

    QOpenGLContext* ctx = QOpenGLContext::currentContext ();
    QOpenGLFunctions* f = ctx->functions ();
    GLuint fbo;
    f->glGenFramebuffers (1, &fbo);
    GLuint prevFbo;
    f->glGetIntegerv (GL_FRAMEBUFFER_BINDING, (GLint*)&prevFbo);
    f->glBindFramebuffer (GL_FRAMEBUFFER, fbo);
    f->glFramebufferTexture2D (
    GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, handle, 0);
    f->glReadPixels (
    0, 0, image.width, image.height, GL_RGBA, GL_UNSIGNED_BYTE, image.data);
    f->glBindFramebuffer (GL_FRAMEBUFFER, prevFbo);

    _opengl_mutex.unlock ();
}

void augmentation_widget::setBackground (GLuint tex) {
    // TODO: this might not be the "nicest" way to do this, since it simply
    // ignores the previously generated texture. That texture will be lost,
    // although this only happens once per runtime, so its not THAT bad.
    _opengl_mutex.lock ();
    _texture_background = tex;
    _opengl_mutex.unlock ();
}

void augmentation_widget::setBackground (image_t image) {
    bool status = true;
    GLint format_gl;

    _opengl_mutex.lock ();
    // create background texture
    glBindTexture (GL_TEXTURE_2D, _texture_background);

    switch (image.format) {
        case RGB24: {
            format_gl = GL_RGB;
            break;
        }
        case YUV:
        case GREY8:
        case BINARY8: {
            format_gl = GL_LUMINANCE;
            break;
        }
        case BGR32: {
            status = false;
            break;
        }
    }

    if (status) {
        glTexImage2D (GL_TEXTURE_2D, 0, format_gl, image.width, image.height, 0,
        format_gl, GL_UNSIGNED_BYTE, image.data);
    }
    glBindTexture (GL_TEXTURE_2D, 0);
    _opengl_mutex.unlock ();
}

void augmentation_widget::setScale (const float scale) {
    _opengl_mutex.lock ();
    _scale_factor = scale;
    _opengl_mutex.unlock ();
}

void augmentation_widget::setXPosition (const float location) {
    _opengl_mutex.lock ();
    _x_pos = location;
    _opengl_mutex.unlock ();<commit_msg>fix debug texture rendering on desktop GL<commit_after>// augmentation_widget.cpp

#include "augmentation_widget.hpp"

#include <QOpenGLExtraFunctions>
#include <QTemporaryFile>
#include <QVector2D>
#include <QVector3D>
#include <math.h>

augmentation_widget::augmentation_widget (QWidget* parent)
: QOpenGLWidget (parent)
, _scale_factor (1.0f)
, _x_pos (0.0f)
, _y_pos (0.0f)
, _opengl_mutex (QMutex::Recursive)
, _last_handle (0)
, _vertex_count (0) {
}

augmentation_widget::~augmentation_widget () {
    _opengl_mutex.lock ();
    glDeleteBuffers (1, &_object_vbo);
    glDeleteVertexArrays (1, &_object_vao);
    _opengl_mutex.unlock ();
}

QSize augmentation_widget::minimumSizeHint () const {
    return QSize (600, 350);
}

QSize augmentation_widget::sizeHint () const {
    return QSize (600, 350);
}

bool augmentation_widget::loadObject (const QString& resource_path) {
    _opengl_mutex.lock ();
    bool status = false;

    // extract model from resources into filesystem and parse it
    QFile resource_file (resource_path);
    if (resource_file.exists ()) {
        auto temp_file  = QTemporaryFile::createNativeFile (resource_file);
        QString fs_path = temp_file->fileName ();

        if (!fs_path.isEmpty ()) {
            std::vector<float> model_interleaved = _object.load (fs_path.toStdString ());

            glBindBuffer (GL_ARRAY_BUFFER, _object_vbo);
            glBufferData (GL_ARRAY_BUFFER, sizeof (float) * model_interleaved.size (),
            model_interleaved.data (), GL_STATIC_DRAW);
            glBindBuffer (GL_ARRAY_BUFFER, 0);
            _vertex_count = model_interleaved.size () / _object.data_per_vertex ();
            _object.release ();

            status = true;
        }
    }
    _opengl_mutex.unlock ();
    return status;
}

void augmentation_widget::downloadImage (image_t& image, GLuint handle) {
    _opengl_mutex.lock ();

    QOpenGLContext* ctx = QOpenGLContext::currentContext ();
    QOpenGLFunctions* f = ctx->functions ();
    GLuint fbo;
    f->glGenFramebuffers (1, &fbo);
    GLuint prevFbo;
    f->glGetIntegerv (GL_FRAMEBUFFER_BINDING, (GLint*)&prevFbo);
    f->glBindFramebuffer (GL_FRAMEBUFFER, fbo);
    f->glFramebufferTexture2D (
    GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, handle, 0);
    f->glReadPixels (
    0, 0, image.width, image.height, GL_RGBA, GL_UNSIGNED_BYTE, image.data);
    f->glBindFramebuffer (GL_FRAMEBUFFER, prevFbo);

    _opengl_mutex.unlock ();
}

void augmentation_widget::setBackground (GLuint tex) {
    // TODO: this might not be the "nicest" way to do this, since it simply
    // ignores the previously generated texture. That texture will be lost,
    // although this only happens once per runtime, so its not THAT bad.
    _opengl_mutex.lock ();
    _texture_background = tex;
    _opengl_mutex.unlock ();
}

void augmentation_widget::setBackground (image_t image) {
    bool status = true;
    GLint format_gl;

    _opengl_mutex.lock ();
    // create background texture
    glBindTexture (GL_TEXTURE_2D, _texture_background);

    switch (image.format) {
        case RGB24: {
            format_gl = GL_RGB;
            break;
        }
        case YUV:
        case GREY8:
        case BINARY8: {
            if (QOpenGLContext::currentContext ()->isOpenGLES ()) {
                format_gl = GL_LUMINANCE;
            } else {
                format_gl = GL_RED;
            }
            break;
        }
        case BGR32: {
            status = false;
            break;
        }
    }

    if (status) {
        glTexImage2D (GL_TEXTURE_2D, 0, format_gl, image.width, image.height, 0,
        format_gl, GL_UNSIGNED_BYTE, image.data);
    }
    glBindTexture (GL_TEXTURE_2D, 0);
    _opengl_mutex.unlock ();
}

void augmentation_widget::setScale (const float scale) {
    _opengl_mutex.lock ();
    _scale_factor = scale;
    _opengl_mutex.unlock ();
}

void augmentation_widget::setXPosition (const float location) {
    _opengl_mutex.lock ();
    _x_pos = location;
    _opengl_mutex.unlock ();
{'fraction_non_alphanumeric': 0.10811495125965312, 'fraction_numerical': 0.01139384732244588, 'mean_word_length': 3.093264248704663, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1851794', 'n_tokens_mistral': 2693, 'n_tokens_neox': 2594, 'n_words': 554}