|
@@ -13,29 +13,25 @@ class DistancePixelConversion;
|
|
|
template <>
|
|
|
class DistancePixelConversion<double> {
|
|
|
public:
|
|
|
- typedef float PixelType;
|
|
|
- inline static PixelType convert(double distance, double range) {
|
|
|
- return PixelType(distance/range+.5);
|
|
|
+ typedef BitmapRef<float, 1> BitmapRefType;
|
|
|
+ inline static void convert(float *pixels, double distance, double range) {
|
|
|
+ *pixels = float(distance/range+.5);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
template <>
|
|
|
class DistancePixelConversion<MultiDistance> {
|
|
|
public:
|
|
|
- typedef FloatRGB PixelType;
|
|
|
- inline static PixelType convert(const MultiDistance &distance, double range) {
|
|
|
- PixelType pixel;
|
|
|
- pixel.r = float(distance.r/range+.5);
|
|
|
- pixel.g = float(distance.g/range+.5);
|
|
|
- pixel.b = float(distance.b/range+.5);
|
|
|
- return pixel;
|
|
|
+ typedef BitmapRef<float, 3> BitmapRefType;
|
|
|
+ inline static void convert(float *pixels, const MultiDistance &distance, double range) {
|
|
|
+ pixels[0] = float(distance.r/range+.5);
|
|
|
+ pixels[1] = float(distance.g/range+.5);
|
|
|
+ pixels[2] = float(distance.b/range+.5);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
template <class ContourCombiner>
|
|
|
-void generateDistanceField(Bitmap<typename DistancePixelConversion<typename ContourCombiner::DistanceType>::PixelType> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
|
|
|
- int w = output.width(), h = output.height();
|
|
|
-
|
|
|
+void generateDistanceField(const typename DistancePixelConversion<typename ContourCombiner::DistanceType>::BitmapRefType &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
|
|
|
#ifdef MSDFGEN_USE_OPENMP
|
|
|
#pragma omp parallel
|
|
|
#endif
|
|
@@ -45,10 +41,10 @@ void generateDistanceField(Bitmap<typename DistancePixelConversion<typename Cont
|
|
|
#ifdef MSDFGEN_USE_OPENMP
|
|
|
#pragma omp for
|
|
|
#endif
|
|
|
- for (int y = 0; y < h; ++y) {
|
|
|
- int row = shape.inverseYAxis ? h-y-1 : y;
|
|
|
+ for (int y = 0; y < output.height; ++y) {
|
|
|
+ int row = shape.inverseYAxis ? output.height-y-1 : y;
|
|
|
p.y = (y+.5)/scale.y-translate.y;
|
|
|
- for (int x = 0; x < w; ++x) {
|
|
|
+ for (int x = 0; x < output.width; ++x) {
|
|
|
p.x = (x+.5)/scale.x-translate.x;
|
|
|
|
|
|
contourCombiner.reset(p);
|
|
@@ -66,32 +62,32 @@ void generateDistanceField(Bitmap<typename DistancePixelConversion<typename Cont
|
|
|
curEdge = nextEdge;
|
|
|
}
|
|
|
|
|
|
- contourCombiner.setContourEdge(int(contour-shape.contours.begin()), edgeSelector);
|
|
|
+ contourCombiner.setContourEdgeSelection(int(contour-shape.contours.begin()), edgeSelector);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
typename ContourCombiner::DistanceType distance = contourCombiner.distance();
|
|
|
- output(x, row) = DistancePixelConversion<typename ContourCombiner::DistanceType>::convert(distance, range);
|
|
|
+ DistancePixelConversion<typename ContourCombiner::DistanceType>::convert(output(x, row), distance, range);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void generateSDF(Bitmap<float> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport) {
|
|
|
+void generateSDF(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport) {
|
|
|
if (overlapSupport)
|
|
|
generateDistanceField<OverlappingContourCombiner<TrueDistanceSelector> >(output, shape, range, scale, translate);
|
|
|
else
|
|
|
generateDistanceField<SimpleContourCombiner<TrueDistanceSelector> >(output, shape, range, scale, translate);
|
|
|
}
|
|
|
|
|
|
-void generatePseudoSDF(Bitmap<float> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport) {
|
|
|
+void generatePseudoSDF(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport) {
|
|
|
if (overlapSupport)
|
|
|
generateDistanceField<OverlappingContourCombiner<PseudoDistanceSelector> >(output, shape, range, scale, translate);
|
|
|
else
|
|
|
generateDistanceField<SimpleContourCombiner<PseudoDistanceSelector> >(output, shape, range, scale, translate);
|
|
|
}
|
|
|
|
|
|
-void generateMSDF(Bitmap<FloatRGB> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold, bool overlapSupport) {
|
|
|
+void generateMSDF(const BitmapRef<float, 3> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold, bool overlapSupport) {
|
|
|
if (overlapSupport)
|
|
|
generateDistanceField<OverlappingContourCombiner<MultiDistanceSelector> >(output, shape, range, scale, translate);
|
|
|
else
|
|
@@ -100,10 +96,10 @@ void generateMSDF(Bitmap<FloatRGB> &output, const Shape &shape, double range, co
|
|
|
msdfErrorCorrection(output, edgeThreshold/(scale*range));
|
|
|
}
|
|
|
|
|
|
-inline static bool detectClash(const FloatRGB &a, const FloatRGB &b, double threshold) {
|
|
|
+inline static bool detectClash(const float *a, const float *b, double threshold) {
|
|
|
// Sort channels so that pairs (a0, b0), (a1, b1), (a2, b2) go from biggest to smallest absolute difference
|
|
|
- float a0 = a.r, a1 = a.g, a2 = a.b;
|
|
|
- float b0 = b.r, b1 = b.g, b2 = b.b;
|
|
|
+ float a0 = a[0], a1 = a[1], a2 = a[2];
|
|
|
+ float b0 = b[0], b1 = b[1], b2 = b[2];
|
|
|
float tmp;
|
|
|
if (fabsf(b0-a0) < fabsf(b1-a1)) {
|
|
|
tmp = a0, a0 = a1, a1 = tmp;
|
|
@@ -122,9 +118,9 @@ inline static bool detectClash(const FloatRGB &a, const FloatRGB &b, double thre
|
|
|
fabsf(a2-.5f) >= fabsf(b2-.5f); // Out of the pair, only flag the pixel farther from a shape edge
|
|
|
}
|
|
|
|
|
|
-void msdfErrorCorrection(Bitmap<FloatRGB> &output, const Vector2 &threshold) {
|
|
|
+void msdfErrorCorrection(const BitmapRef<float, 3> &output, const Vector2 &threshold) {
|
|
|
std::vector<std::pair<int, int> > clashes;
|
|
|
- int w = output.width(), h = output.height();
|
|
|
+ int w = output.width, h = output.height;
|
|
|
for (int y = 0; y < h; ++y)
|
|
|
for (int x = 0; x < w; ++x) {
|
|
|
if (
|
|
@@ -136,9 +132,9 @@ void msdfErrorCorrection(Bitmap<FloatRGB> &output, const Vector2 &threshold) {
|
|
|
clashes.push_back(std::make_pair(x, y));
|
|
|
}
|
|
|
for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
|
|
|
- FloatRGB &pixel = output(clash->first, clash->second);
|
|
|
- float med = median(pixel.r, pixel.g, pixel.b);
|
|
|
- pixel.r = med, pixel.g = med, pixel.b = med;
|
|
|
+ float *pixel = output(clash->first, clash->second);
|
|
|
+ float med = median(pixel[0], pixel[1], pixel[2]);
|
|
|
+ pixel[0] = med, pixel[1] = med, pixel[2] = med;
|
|
|
}
|
|
|
#ifndef MSDFGEN_NO_DIAGONAL_CLASH_DETECTION
|
|
|
clashes.clear();
|
|
@@ -153,23 +149,22 @@ void msdfErrorCorrection(Bitmap<FloatRGB> &output, const Vector2 &threshold) {
|
|
|
clashes.push_back(std::make_pair(x, y));
|
|
|
}
|
|
|
for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
|
|
|
- FloatRGB &pixel = output(clash->first, clash->second);
|
|
|
- float med = median(pixel.r, pixel.g, pixel.b);
|
|
|
- pixel.r = med, pixel.g = med, pixel.b = med;
|
|
|
+ float *pixel = output(clash->first, clash->second);
|
|
|
+ float med = median(pixel[0], pixel[1], pixel[2]);
|
|
|
+ pixel[0] = med, pixel[1] = med, pixel[2] = med;
|
|
|
}
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
// Legacy version
|
|
|
|
|
|
-void generateSDF_legacy(Bitmap<float> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
|
|
|
- int w = output.width(), h = output.height();
|
|
|
+void generateSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
|
|
|
#ifdef MSDFGEN_USE_OPENMP
|
|
|
#pragma omp parallel for
|
|
|
#endif
|
|
|
- for (int y = 0; y < h; ++y) {
|
|
|
- int row = shape.inverseYAxis ? h-y-1 : y;
|
|
|
- for (int x = 0; x < w; ++x) {
|
|
|
+ for (int y = 0; y < output.height; ++y) {
|
|
|
+ int row = shape.inverseYAxis ? output.height-y-1 : y;
|
|
|
+ for (int x = 0; x < output.width; ++x) {
|
|
|
double dummy;
|
|
|
Point2 p = Vector2(x+.5, y+.5)/scale-translate;
|
|
|
SignedDistance minDistance;
|
|
@@ -179,19 +174,18 @@ void generateSDF_legacy(Bitmap<float> &output, const Shape &shape, double range,
|
|
|
if (distance < minDistance)
|
|
|
minDistance = distance;
|
|
|
}
|
|
|
- output(x, row) = float(minDistance.distance/range+.5);
|
|
|
+ *output(x, row) = float(minDistance.distance/range+.5);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void generatePseudoSDF_legacy(Bitmap<float> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
|
|
|
- int w = output.width(), h = output.height();
|
|
|
+void generatePseudoSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
|
|
|
#ifdef MSDFGEN_USE_OPENMP
|
|
|
#pragma omp parallel for
|
|
|
#endif
|
|
|
- for (int y = 0; y < h; ++y) {
|
|
|
- int row = shape.inverseYAxis ? h-y-1 : y;
|
|
|
- for (int x = 0; x < w; ++x) {
|
|
|
+ for (int y = 0; y < output.height; ++y) {
|
|
|
+ int row = shape.inverseYAxis ? output.height-y-1 : y;
|
|
|
+ for (int x = 0; x < output.width; ++x) {
|
|
|
Point2 p = Vector2(x+.5, y+.5)/scale-translate;
|
|
|
SignedDistance minDistance;
|
|
|
const EdgeHolder *nearEdge = NULL;
|
|
@@ -208,19 +202,18 @@ void generatePseudoSDF_legacy(Bitmap<float> &output, const Shape &shape, double
|
|
|
}
|
|
|
if (nearEdge)
|
|
|
(*nearEdge)->distanceToPseudoDistance(minDistance, p, nearParam);
|
|
|
- output(x, row) = float(minDistance.distance/range+.5);
|
|
|
+ *output(x, row) = float(minDistance.distance/range+.5);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void generateMSDF_legacy(Bitmap<FloatRGB> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold) {
|
|
|
- int w = output.width(), h = output.height();
|
|
|
+void generateMSDF_legacy(const BitmapRef<float, 3> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold) {
|
|
|
#ifdef MSDFGEN_USE_OPENMP
|
|
|
#pragma omp parallel for
|
|
|
#endif
|
|
|
- for (int y = 0; y < h; ++y) {
|
|
|
- int row = shape.inverseYAxis ? h-y-1 : y;
|
|
|
- for (int x = 0; x < w; ++x) {
|
|
|
+ for (int y = 0; y < output.height; ++y) {
|
|
|
+ int row = shape.inverseYAxis ? output.height-y-1 : y;
|
|
|
+ for (int x = 0; x < output.width; ++x) {
|
|
|
Point2 p = Vector2(x+.5, y+.5)/scale-translate;
|
|
|
|
|
|
struct {
|
|
@@ -258,9 +251,9 @@ void generateMSDF_legacy(Bitmap<FloatRGB> &output, const Shape &shape, double ra
|
|
|
(*g.nearEdge)->distanceToPseudoDistance(g.minDistance, p, g.nearParam);
|
|
|
if (b.nearEdge)
|
|
|
(*b.nearEdge)->distanceToPseudoDistance(b.minDistance, p, b.nearParam);
|
|
|
- output(x, row).r = float(r.minDistance.distance/range+.5);
|
|
|
- output(x, row).g = float(g.minDistance.distance/range+.5);
|
|
|
- output(x, row).b = float(b.minDistance.distance/range+.5);
|
|
|
+ output(x, row)[0] = float(r.minDistance.distance/range+.5);
|
|
|
+ output(x, row)[1] = float(g.minDistance.distance/range+.5);
|
|
|
+ output(x, row)[2] = float(b.minDistance.distance/range+.5);
|
|
|
}
|
|
|
}
|
|
|
|