Path: blob/master/thirdparty/msdfgen/core/msdf-error-correction.cpp
9902 views
1#include "msdf-error-correction.h"23#include <vector>4#include "arithmetics.hpp"5#include "Bitmap.h"6#include "contour-combiners.h"7#include "MSDFErrorCorrection.h"89namespace msdfgen {1011template <int N>12static void msdfErrorCorrectionInner(const BitmapRef<float, N> &sdf, const Shape &shape, const SDFTransformation &transformation, const MSDFGeneratorConfig &config) {13if (config.errorCorrection.mode == ErrorCorrectionConfig::DISABLED)14return;15Bitmap<byte, 1> stencilBuffer;16if (!config.errorCorrection.buffer)17stencilBuffer = Bitmap<byte, 1>(sdf.width, sdf.height);18BitmapRef<byte, 1> stencil;19stencil.pixels = config.errorCorrection.buffer ? config.errorCorrection.buffer : (byte *) stencilBuffer;20stencil.width = sdf.width, stencil.height = sdf.height;21MSDFErrorCorrection ec(stencil, transformation);22ec.setMinDeviationRatio(config.errorCorrection.minDeviationRatio);23ec.setMinImproveRatio(config.errorCorrection.minImproveRatio);24switch (config.errorCorrection.mode) {25case ErrorCorrectionConfig::DISABLED:26case ErrorCorrectionConfig::INDISCRIMINATE:27break;28case ErrorCorrectionConfig::EDGE_PRIORITY:29ec.protectCorners(shape);30ec.protectEdges<N>(sdf);31break;32case ErrorCorrectionConfig::EDGE_ONLY:33ec.protectAll();34break;35}36if (config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::DO_NOT_CHECK_DISTANCE || (config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::CHECK_DISTANCE_AT_EDGE && config.errorCorrection.mode != ErrorCorrectionConfig::EDGE_ONLY)) {37ec.findErrors<N>(sdf);38if (config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::CHECK_DISTANCE_AT_EDGE)39ec.protectAll();40}41if (config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::ALWAYS_CHECK_DISTANCE || config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::CHECK_DISTANCE_AT_EDGE) {42if (config.overlapSupport)43ec.findErrors<OverlappingContourCombiner, N>(sdf, shape);44else45ec.findErrors<SimpleContourCombiner, N>(sdf, shape);46}47ec.apply(sdf);48}4950template <int N>51static void msdfErrorCorrectionShapeless(const BitmapRef<float, N> &sdf, const SDFTransformation &transformation, double minDeviationRatio, bool protectAll) {52Bitmap<byte, 1> stencilBuffer(sdf.width, sdf.height);53MSDFErrorCorrection ec(stencilBuffer, transformation);54ec.setMinDeviationRatio(minDeviationRatio);55if (protectAll)56ec.protectAll();57ec.findErrors<N>(sdf);58ec.apply(sdf);59}6061void msdfErrorCorrection(const BitmapRef<float, 3> &sdf, const Shape &shape, const SDFTransformation &transformation, const MSDFGeneratorConfig &config) {62msdfErrorCorrectionInner(sdf, shape, transformation, config);63}64void msdfErrorCorrection(const BitmapRef<float, 4> &sdf, const Shape &shape, const SDFTransformation &transformation, const MSDFGeneratorConfig &config) {65msdfErrorCorrectionInner(sdf, shape, transformation, config);66}67void msdfErrorCorrection(const BitmapRef<float, 3> &sdf, const Shape &shape, const Projection &projection, Range range, const MSDFGeneratorConfig &config) {68msdfErrorCorrectionInner(sdf, shape, SDFTransformation(projection, range), config);69}70void msdfErrorCorrection(const BitmapRef<float, 4> &sdf, const Shape &shape, const Projection &projection, Range range, const MSDFGeneratorConfig &config) {71msdfErrorCorrectionInner(sdf, shape, SDFTransformation(projection, range), config);72}7374void msdfFastDistanceErrorCorrection(const BitmapRef<float, 3> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {75msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, false);76}77void msdfFastDistanceErrorCorrection(const BitmapRef<float, 4> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {78msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, false);79}80void msdfFastDistanceErrorCorrection(const BitmapRef<float, 3> &sdf, const Projection &projection, Range range, double minDeviationRatio) {81msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, false);82}83void msdfFastDistanceErrorCorrection(const BitmapRef<float, 4> &sdf, const Projection &projection, Range range, double minDeviationRatio) {84msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, false);85}86void msdfFastDistanceErrorCorrection(const BitmapRef<float, 3> &sdf, Range pxRange, double minDeviationRatio) {87msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, false);88}89void msdfFastDistanceErrorCorrection(const BitmapRef<float, 4> &sdf, Range pxRange, double minDeviationRatio) {90msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, false);91}9293void msdfFastEdgeErrorCorrection(const BitmapRef<float, 3> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {94msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, true);95}96void msdfFastEdgeErrorCorrection(const BitmapRef<float, 4> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {97msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, true);98}99void msdfFastEdgeErrorCorrection(const BitmapRef<float, 3> &sdf, const Projection &projection, Range range, double minDeviationRatio) {100msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, true);101}102void msdfFastEdgeErrorCorrection(const BitmapRef<float, 4> &sdf, const Projection &projection, Range range, double minDeviationRatio) {103msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, true);104}105void msdfFastEdgeErrorCorrection(const BitmapRef<float, 3> &sdf, Range pxRange, double minDeviationRatio) {106msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, true);107}108void msdfFastEdgeErrorCorrection(const BitmapRef<float, 4> &sdf, Range pxRange, double minDeviationRatio) {109msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, true);110}111112113// Legacy version114115inline static bool detectClash(const float *a, const float *b, double threshold) {116// Sort channels so that pairs (a0, b0), (a1, b1), (a2, b2) go from biggest to smallest absolute difference117float a0 = a[0], a1 = a[1], a2 = a[2];118float b0 = b[0], b1 = b[1], b2 = b[2];119float tmp;120if (fabsf(b0-a0) < fabsf(b1-a1)) {121tmp = a0, a0 = a1, a1 = tmp;122tmp = b0, b0 = b1, b1 = tmp;123}124if (fabsf(b1-a1) < fabsf(b2-a2)) {125tmp = a1, a1 = a2, a2 = tmp;126tmp = b1, b1 = b2, b2 = tmp;127if (fabsf(b0-a0) < fabsf(b1-a1)) {128tmp = a0, a0 = a1, a1 = tmp;129tmp = b0, b0 = b1, b1 = tmp;130}131}132return (fabsf(b1-a1) >= threshold) &&133!(b0 == b1 && b0 == b2) && // Ignore if other pixel has been equalized134fabsf(a2-.5f) >= fabsf(b2-.5f); // Out of the pair, only flag the pixel farther from a shape edge135}136137template <int N>138static void msdfErrorCorrectionInner_legacy(const BitmapRef<float, N> &output, const Vector2 &threshold) {139std::vector<std::pair<int, int> > clashes;140int w = output.width, h = output.height;141for (int y = 0; y < h; ++y)142for (int x = 0; x < w; ++x) {143if (144(x > 0 && detectClash(output(x, y), output(x-1, y), threshold.x)) ||145(x < w-1 && detectClash(output(x, y), output(x+1, y), threshold.x)) ||146(y > 0 && detectClash(output(x, y), output(x, y-1), threshold.y)) ||147(y < h-1 && detectClash(output(x, y), output(x, y+1), threshold.y))148)149clashes.push_back(std::make_pair(x, y));150}151for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {152float *pixel = output(clash->first, clash->second);153float med = median(pixel[0], pixel[1], pixel[2]);154pixel[0] = med, pixel[1] = med, pixel[2] = med;155}156#ifndef MSDFGEN_NO_DIAGONAL_CLASH_DETECTION157clashes.clear();158for (int y = 0; y < h; ++y)159for (int x = 0; x < w; ++x) {160if (161(x > 0 && y > 0 && detectClash(output(x, y), output(x-1, y-1), threshold.x+threshold.y)) ||162(x < w-1 && y > 0 && detectClash(output(x, y), output(x+1, y-1), threshold.x+threshold.y)) ||163(x > 0 && y < h-1 && detectClash(output(x, y), output(x-1, y+1), threshold.x+threshold.y)) ||164(x < w-1 && y < h-1 && detectClash(output(x, y), output(x+1, y+1), threshold.x+threshold.y))165)166clashes.push_back(std::make_pair(x, y));167}168for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {169float *pixel = output(clash->first, clash->second);170float med = median(pixel[0], pixel[1], pixel[2]);171pixel[0] = med, pixel[1] = med, pixel[2] = med;172}173#endif174}175176void msdfErrorCorrection_legacy(const BitmapRef<float, 3> &output, const Vector2 &threshold) {177msdfErrorCorrectionInner_legacy(output, threshold);178}179void msdfErrorCorrection_legacy(const BitmapRef<float, 4> &output, const Vector2 &threshold) {180msdfErrorCorrectionInner_legacy(output, threshold);181}182183}184185186