Path: blob/master/thirdparty/msdfgen/core/msdf-error-correction.cpp
20931 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 BitmapSection<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);18BitmapSection<byte, 1> stencil(NULL, sdf.width, sdf.height);19stencil.pixels = config.errorCorrection.buffer ? config.errorCorrection.buffer : (byte *) stencilBuffer;20MSDFErrorCorrection ec(stencil, transformation);21ec.setMinDeviationRatio(config.errorCorrection.minDeviationRatio);22ec.setMinImproveRatio(config.errorCorrection.minImproveRatio);23switch (config.errorCorrection.mode) {24case ErrorCorrectionConfig::DISABLED:25case ErrorCorrectionConfig::INDISCRIMINATE:26break;27case ErrorCorrectionConfig::EDGE_PRIORITY:28ec.protectCorners(shape);29ec.protectEdges<N>(sdf);30break;31case ErrorCorrectionConfig::EDGE_ONLY:32ec.protectAll();33break;34}35if (config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::DO_NOT_CHECK_DISTANCE || (config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::CHECK_DISTANCE_AT_EDGE && config.errorCorrection.mode != ErrorCorrectionConfig::EDGE_ONLY)) {36ec.findErrors<N>(sdf);37if (config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::CHECK_DISTANCE_AT_EDGE)38ec.protectAll();39}40if (config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::ALWAYS_CHECK_DISTANCE || config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::CHECK_DISTANCE_AT_EDGE) {41if (config.overlapSupport)42ec.findErrors<OverlappingContourCombiner, N>(sdf, shape);43else44ec.findErrors<SimpleContourCombiner, N>(sdf, shape);45}46ec.apply(sdf);47}4849template <int N>50static void msdfErrorCorrectionShapeless(const BitmapSection<float, N> &sdf, const SDFTransformation &transformation, double minDeviationRatio, bool protectAll) {51Bitmap<byte, 1> stencilBuffer(sdf.width, sdf.height);52MSDFErrorCorrection ec(stencilBuffer, transformation);53ec.setMinDeviationRatio(minDeviationRatio);54if (protectAll)55ec.protectAll();56ec.findErrors<N>(sdf);57ec.apply(sdf);58}5960void msdfErrorCorrection(const BitmapSection<float, 3> &sdf, const Shape &shape, const SDFTransformation &transformation, const MSDFGeneratorConfig &config) {61msdfErrorCorrectionInner(sdf, shape, transformation, config);62}63void msdfErrorCorrection(const BitmapSection<float, 4> &sdf, const Shape &shape, const SDFTransformation &transformation, const MSDFGeneratorConfig &config) {64msdfErrorCorrectionInner(sdf, shape, transformation, config);65}66void msdfErrorCorrection(const BitmapSection<float, 3> &sdf, const Shape &shape, const Projection &projection, Range range, const MSDFGeneratorConfig &config) {67msdfErrorCorrectionInner(sdf, shape, SDFTransformation(projection, range), config);68}69void msdfErrorCorrection(const BitmapSection<float, 4> &sdf, const Shape &shape, const Projection &projection, Range range, const MSDFGeneratorConfig &config) {70msdfErrorCorrectionInner(sdf, shape, SDFTransformation(projection, range), config);71}7273void msdfFastDistanceErrorCorrection(const BitmapSection<float, 3> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {74msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, false);75}76void msdfFastDistanceErrorCorrection(const BitmapSection<float, 4> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {77msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, false);78}79void msdfFastDistanceErrorCorrection(const BitmapSection<float, 3> &sdf, const Projection &projection, Range range, double minDeviationRatio) {80msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, false);81}82void msdfFastDistanceErrorCorrection(const BitmapSection<float, 4> &sdf, const Projection &projection, Range range, double minDeviationRatio) {83msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, false);84}85void msdfFastDistanceErrorCorrection(const BitmapSection<float, 3> &sdf, Range pxRange, double minDeviationRatio) {86msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, false);87}88void msdfFastDistanceErrorCorrection(const BitmapSection<float, 4> &sdf, Range pxRange, double minDeviationRatio) {89msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, false);90}9192void msdfFastEdgeErrorCorrection(const BitmapSection<float, 3> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {93msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, true);94}95void msdfFastEdgeErrorCorrection(const BitmapSection<float, 4> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {96msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, true);97}98void msdfFastEdgeErrorCorrection(const BitmapSection<float, 3> &sdf, const Projection &projection, Range range, double minDeviationRatio) {99msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, true);100}101void msdfFastEdgeErrorCorrection(const BitmapSection<float, 4> &sdf, const Projection &projection, Range range, double minDeviationRatio) {102msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, true);103}104void msdfFastEdgeErrorCorrection(const BitmapSection<float, 3> &sdf, Range pxRange, double minDeviationRatio) {105msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, true);106}107void msdfFastEdgeErrorCorrection(const BitmapSection<float, 4> &sdf, Range pxRange, double minDeviationRatio) {108msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, true);109}110111112// Legacy version113114inline static bool detectClash(const float *a, const float *b, double threshold) {115// Sort channels so that pairs (a0, b0), (a1, b1), (a2, b2) go from biggest to smallest absolute difference116float a0 = a[0], a1 = a[1], a2 = a[2];117float b0 = b[0], b1 = b[1], b2 = b[2];118float tmp;119if (fabsf(b0-a0) < fabsf(b1-a1)) {120tmp = a0, a0 = a1, a1 = tmp;121tmp = b0, b0 = b1, b1 = tmp;122}123if (fabsf(b1-a1) < fabsf(b2-a2)) {124tmp = a1, a1 = a2, a2 = tmp;125tmp = b1, b1 = b2, b2 = tmp;126if (fabsf(b0-a0) < fabsf(b1-a1)) {127tmp = a0, a0 = a1, a1 = tmp;128tmp = b0, b0 = b1, b1 = tmp;129}130}131return (fabsf(b1-a1) >= threshold) &&132!(b0 == b1 && b0 == b2) && // Ignore if other pixel has been equalized133fabsf(a2-.5f) >= fabsf(b2-.5f); // Out of the pair, only flag the pixel farther from a shape edge134}135136template <int N>137static void msdfErrorCorrectionInner_legacy(const BitmapSection<float, N> &output, const Vector2 &threshold) {138std::vector<std::pair<int, int> > clashes;139int w = output.width, h = output.height;140for (int y = 0; y < h; ++y)141for (int x = 0; x < w; ++x) {142if (143(x > 0 && detectClash(output(x, y), output(x-1, y), threshold.x)) ||144(x < w-1 && detectClash(output(x, y), output(x+1, y), threshold.x)) ||145(y > 0 && detectClash(output(x, y), output(x, y-1), threshold.y)) ||146(y < h-1 && detectClash(output(x, y), output(x, y+1), threshold.y))147)148clashes.push_back(std::make_pair(x, y));149}150for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {151float *pixel = output(clash->first, clash->second);152float med = median(pixel[0], pixel[1], pixel[2]);153pixel[0] = med, pixel[1] = med, pixel[2] = med;154}155#ifndef MSDFGEN_NO_DIAGONAL_CLASH_DETECTION156clashes.clear();157for (int y = 0; y < h; ++y)158for (int x = 0; x < w; ++x) {159if (160(x > 0 && y > 0 && detectClash(output(x, y), output(x-1, y-1), threshold.x+threshold.y)) ||161(x < w-1 && y > 0 && detectClash(output(x, y), output(x+1, y-1), threshold.x+threshold.y)) ||162(x > 0 && y < h-1 && detectClash(output(x, y), output(x-1, y+1), threshold.x+threshold.y)) ||163(x < w-1 && y < h-1 && detectClash(output(x, y), output(x+1, y+1), threshold.x+threshold.y))164)165clashes.push_back(std::make_pair(x, y));166}167for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {168float *pixel = output(clash->first, clash->second);169float med = median(pixel[0], pixel[1], pixel[2]);170pixel[0] = med, pixel[1] = med, pixel[2] = med;171}172#endif173}174175void msdfErrorCorrection_legacy(const BitmapSection<float, 3> &output, const Vector2 &threshold) {176msdfErrorCorrectionInner_legacy(output, threshold);177}178void msdfErrorCorrection_legacy(const BitmapSection<float, 4> &output, const Vector2 &threshold) {179msdfErrorCorrectionInner_legacy(output, threshold);180}181182}183184185