Path: blob/master/thirdparty/msdfgen/core/rasterization.cpp
21026 views
1#include "rasterization.h"23#include <vector>4#include "arithmetics.hpp"56namespace msdfgen {78void rasterize(BitmapSection<float, 1> output, const Shape &shape, const Projection &projection, FillRule fillRule) {9output.reorient(shape.getYAxisOrientation());10Scanline scanline;11for (int y = 0; y < output.height; ++y) {12shape.scanline(scanline, projection.unprojectY(y+.5));13for (int x = 0; x < output.width; ++x)14*output(x, y) = (float) scanline.filled(projection.unprojectX(x+.5), fillRule);15}16}1718void distanceSignCorrection(BitmapSection<float, 1> sdf, const Shape &shape, const Projection &projection, float sdfZeroValue, FillRule fillRule) {19sdf.reorient(shape.getYAxisOrientation());20float doubleSdfZeroValue = sdfZeroValue+sdfZeroValue;21Scanline scanline;22for (int y = 0; y < sdf.height; ++y) {23shape.scanline(scanline, projection.unprojectY(y+.5));24for (int x = 0; x < sdf.width; ++x) {25bool fill = scanline.filled(projection.unprojectX(x+.5), fillRule);26float &sd = *sdf(x, y);27if ((sd > sdfZeroValue) != fill)28sd = doubleSdfZeroValue-sd;29}30}31}3233template <int N>34static void multiDistanceSignCorrection(BitmapSection<float, N> sdf, const Shape &shape, const Projection &projection, float sdfZeroValue, FillRule fillRule) {35int w = sdf.width, h = sdf.height;36if (!(w && h))37return;38sdf.reorient(shape.getYAxisOrientation());39float doubleSdfZeroValue = sdfZeroValue+sdfZeroValue;40Scanline scanline;41bool ambiguous = false;42std::vector<char> matchMap;43matchMap.resize(w*h);44char *match = &matchMap[0];45for (int y = 0; y < h; ++y) {46shape.scanline(scanline, projection.unprojectY(y+.5));47for (int x = 0; x < w; ++x) {48bool fill = scanline.filled(projection.unprojectX(x+.5), fillRule);49float *msd = sdf(x, y);50float sd = median(msd[0], msd[1], msd[2]);51if (sd == sdfZeroValue)52ambiguous = true;53else if ((sd > sdfZeroValue) != fill) {54msd[0] = doubleSdfZeroValue-msd[0];55msd[1] = doubleSdfZeroValue-msd[1];56msd[2] = doubleSdfZeroValue-msd[2];57*match = -1;58} else59*match = 1;60if (N >= 4 && (msd[3] > sdfZeroValue) != fill)61msd[3] = doubleSdfZeroValue-msd[3];62++match;63}64}65// This step is necessary to avoid artifacts when whole shape is inverted66if (ambiguous) {67match = &matchMap[0];68for (int y = 0; y < h; ++y) {69for (int x = 0; x < w; ++x) {70if (!*match) {71int neighborMatch = 0;72if (x > 0) neighborMatch += *(match-1);73if (x < w-1) neighborMatch += *(match+1);74if (y > 0) neighborMatch += *(match-w);75if (y < h-1) neighborMatch += *(match+w);76if (neighborMatch < 0) {77float *msd = sdf(x, y);78msd[0] = doubleSdfZeroValue-msd[0];79msd[1] = doubleSdfZeroValue-msd[1];80msd[2] = doubleSdfZeroValue-msd[2];81}82}83++match;84}85}86}87}8889void distanceSignCorrection(BitmapSection<float, 3> sdf, const Shape &shape, const Projection &projection, float sdfZeroValue, FillRule fillRule) {90multiDistanceSignCorrection(sdf, shape, projection, sdfZeroValue, fillRule);91}9293void distanceSignCorrection(BitmapSection<float, 4> sdf, const Shape &shape, const Projection &projection, float sdfZeroValue, FillRule fillRule) {94multiDistanceSignCorrection(sdf, shape, projection, sdfZeroValue, fillRule);95}9697// Legacy API9899void rasterize(const BitmapSection<float, 1> &output, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {100rasterize(output, shape, Projection(scale, translate), fillRule);101}102103void distanceSignCorrection(BitmapSection<float, 1> sdf, const Shape &shape, const Projection &projection, FillRule fillRule) {104distanceSignCorrection(sdf, shape, projection, .5f, fillRule);105}106107void distanceSignCorrection(BitmapSection<float, 3> sdf, const Shape &shape, const Projection &projection, FillRule fillRule) {108distanceSignCorrection(sdf, shape, projection, .5f, fillRule);109}110111void distanceSignCorrection(BitmapSection<float, 4> sdf, const Shape &shape, const Projection &projection, FillRule fillRule) {112distanceSignCorrection(sdf, shape, projection, .5f, fillRule);113}114115void distanceSignCorrection(const BitmapSection<float, 1> &sdf, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {116distanceSignCorrection(sdf, shape, Projection(scale, translate), fillRule);117}118119void distanceSignCorrection(const BitmapSection<float, 3> &sdf, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {120distanceSignCorrection(sdf, shape, Projection(scale, translate), fillRule);121}122123void distanceSignCorrection(const BitmapSection<float, 4> &sdf, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {124distanceSignCorrection(sdf, shape, Projection(scale, translate), fillRule);125}126127}128129130