From 9ad827c763b17f2d744ea4dbf4098f0c3b9e7768 Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Tue, 21 Feb 2023 11:00:43 +0100 Subject: [PATCH] add some helper functions --- src/ofxMsdfgen.cpp | 31 +++++++++++++++++++++++++++++++ src/ofxMsdfgen.h | 11 +++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/ofxMsdfgen.cpp create mode 100644 src/ofxMsdfgen.h diff --git a/src/ofxMsdfgen.cpp b/src/ofxMsdfgen.cpp new file mode 100644 index 0000000..8a06889 --- /dev/null +++ b/src/ofxMsdfgen.cpp @@ -0,0 +1,31 @@ +#include "ofxMsdfgen.h" +#include "ofPixels.h" + +void ofxMsdfgen::toOfImage(const msdfgen::Bitmap bitmap, + ofFloatImage & image){ + image.allocate(bitmap.width(), bitmap.height(), OF_IMAGE_COLOR); + memcpy(image.getPixels().getData(), bitmap.getPixels(), bitmap.width() * bitmap.height() * 3 * sizeof(float)); + image.update(); // float is slow here +} + +void ofxMsdfgen::toOfImage(const msdfgen::Bitmap bitmap, + ofImage & image){ + image.allocate(bitmap.width(), bitmap.height(), OF_IMAGE_COLOR); + ofPixels & pixels = image.getPixels(); + int w = bitmap.width(); + int h = bitmap.height(); + for(int y = 0; y < h; y++){ + for(int x = 0; x < w; x++){ + const float * rgb = bitmap(x, y); + int index = 3 * (x + y * w); + pixels.getData()[index] = (unsigned char)std::clamp(rgb[0] * 256.0, + 0.0, 255.0); + pixels.getData()[index + 1] = (unsigned char)std::clamp(rgb[1] * 256.0, + 0.0, 255.0); + pixels.getData()[index + 2] = (unsigned char)std::clamp(rgb[2] * 256.0, + 0.0, 255.0); + } + } + + image.update(); +} diff --git a/src/ofxMsdfgen.h b/src/ofxMsdfgen.h new file mode 100644 index 0000000..c1bba67 --- /dev/null +++ b/src/ofxMsdfgen.h @@ -0,0 +1,11 @@ +#pragma once + +#include "ofMain.h" +#include + +namespace ofxMsdfgen { +void toOfImage(const msdfgen::Bitmap bitmap, + ofFloatImage & image); +void toOfImage(const msdfgen::Bitmap bitmap, + ofImage & image); +}