Skip to content

Commit 3ba514a

Browse files
committed
SixLabors 1.0 updates
1 parent 53c2be4 commit 3ba514a

File tree

4 files changed

+98
-105
lines changed

4 files changed

+98
-105
lines changed

src/Peachpie.Library.Graphics/FloodFillProcessor{TPixel}.cs

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -35,69 +35,73 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
3535
//var pixelSpan = source.GetPixelSpan();
3636
//int rowLength = source.Width;
3737

38-
var segmentQueue = new Queue<(Point point, int rightEdge)>();
39-
segmentQueue.Enqueue((_startPoint, _startPoint.X));
40-
41-
while (segmentQueue.Count > 0)
38+
source.ProcessPixelRows(accessor =>
4239
{
43-
var (currentPoint, rightEdge) = segmentQueue.Dequeue();
44-
var currentY = currentPoint.Y;
45-
var currentX = currentPoint.X;
46-
47-
var rowSpan = source.GetPixelRowSpan(currentY);
4840

49-
int leftEdge;
50-
leftEdge = currentX;
41+
var segmentQueue = new Queue<(Point point, int rightEdge)>();
42+
segmentQueue.Enqueue((_startPoint, _startPoint.X));
5143

52-
// Filling until reaching a border of specified color
53-
if (_toBorder)
44+
while (segmentQueue.Count > 0)
5445
{
55-
// Get the row segment to be colored
56-
while (rightEdge + 1 < source.Width)
57-
{
58-
var edgeColor = GetPixel(rowSpan, rightEdge + 1);
59-
if (edgeColor.Equals(_borderColor) || edgeColor.Equals(_fillColor))
60-
break;
46+
var (currentPoint, rightEdge) = segmentQueue.Dequeue();
47+
var currentY = currentPoint.Y;
48+
var currentX = currentPoint.X;
49+
50+
var rowSpan = accessor.GetRowSpan(currentY);
51+
int leftEdge;
52+
leftEdge = currentX;
6153

62-
rightEdge++;
54+
// Filling until reaching a border of specified color
55+
if (_toBorder)
56+
{
57+
// Get the row segment to be colored
58+
while (rightEdge + 1 < source.Width)
59+
{
60+
var edgeColor = GetPixel(rowSpan, rightEdge + 1);
61+
if (edgeColor.Equals(_borderColor) || edgeColor.Equals(_fillColor))
62+
break;
63+
64+
rightEdge++;
65+
}
66+
while (leftEdge - 1 < source.Width)
67+
{
68+
var edgeColor = GetPixel(rowSpan, leftEdge - 1);
69+
if (edgeColor.Equals(_borderColor) || edgeColor.Equals(_fillColor))
70+
break;
71+
72+
leftEdge--;
73+
}
74+
75+
// Actually color the row
76+
SetPixelRow(rowSpan, leftEdge, rightEdge, _fillColor);
77+
78+
// Add the segments to be filled above and below to the queue
79+
if (currentY > 0)
80+
AddFillingSegmentsToQueueWithBorder(floodFrom, accessor.GetRowSpan(currentY - 1), segmentQueue, leftEdge, rightEdge, currentY - 1);
81+
if (currentY + 1 < source.Height)
82+
AddFillingSegmentsToQueueWithBorder(floodFrom, accessor.GetRowSpan(currentY + 1), segmentQueue, leftEdge, rightEdge, currentY + 1);
6383
}
64-
while (leftEdge - 1 < source.Width)
84+
else
85+
// Filling whole region of same color
6586
{
66-
var edgeColor = GetPixel(rowSpan, leftEdge - 1);
67-
if (edgeColor.Equals(_borderColor) || edgeColor.Equals(_fillColor))
68-
break;
69-
70-
leftEdge--;
87+
// Get the row segment to be colored
88+
while (rightEdge + 1 < source.Width && GetPixel(rowSpan, rightEdge + 1).Equals(floodFrom))
89+
rightEdge++;
90+
while (leftEdge > 0 && GetPixel(rowSpan, leftEdge - 1).Equals(floodFrom))
91+
leftEdge--;
92+
93+
// Actually color the row
94+
SetPixelRow(rowSpan, leftEdge, rightEdge, _fillColor);
95+
96+
// Add the segments to be filled above and below to the queue
97+
if (currentY > 0)
98+
AddFillingSegmentsToQueue(floodFrom, accessor.GetRowSpan(currentY - 1), segmentQueue, leftEdge, rightEdge, currentY - 1);
99+
if (currentY + 1 < source.Height)
100+
AddFillingSegmentsToQueue(floodFrom, accessor.GetRowSpan(currentY + 1), segmentQueue, leftEdge, rightEdge, currentY + 1);
71101
}
72-
73-
// Actually color the row
74-
SetPixelRow(rowSpan, leftEdge, rightEdge, _fillColor);
75-
76-
// Add the segments to be filled above and below to the queue
77-
if (currentY > 0)
78-
AddFillingSegmentsToQueueWithBorder(floodFrom, source.GetPixelRowSpan(currentY - 1), segmentQueue, leftEdge, rightEdge, currentY - 1);
79-
if (currentY + 1 < source.Height)
80-
AddFillingSegmentsToQueueWithBorder(floodFrom, source.GetPixelRowSpan(currentY + 1), segmentQueue, leftEdge, rightEdge, currentY + 1);
81-
}
82-
else
83-
// Filling whole region of same color
84-
{
85-
// Get the row segment to be colored
86-
while (rightEdge + 1 < source.Width && GetPixel(rowSpan, rightEdge + 1).Equals(floodFrom))
87-
rightEdge++;
88-
while (leftEdge > 0 && GetPixel(rowSpan, leftEdge - 1).Equals(floodFrom))
89-
leftEdge--;
90-
91-
// Actually color the row
92-
SetPixelRow(rowSpan, leftEdge, rightEdge, _fillColor);
93-
94-
// Add the segments to be filled above and below to the queue
95-
if (currentY > 0)
96-
AddFillingSegmentsToQueue(floodFrom, source.GetPixelRowSpan(currentY - 1), segmentQueue, leftEdge, rightEdge, currentY - 1);
97-
if (currentY + 1 < source.Height)
98-
AddFillingSegmentsToQueue(floodFrom, source.GetPixelRowSpan(currentY + 1), segmentQueue, leftEdge, rightEdge, currentY + 1);
99102
}
100-
}
103+
104+
});
101105
}
102106

103107
private static void AddFillingSegmentsToQueue(TPixel floodFrom, Span<TPixel> rowSpan, Queue<(Point, int)> segmentQueue, int xStart, int xEnd, int y)

src/Peachpie.Library.Graphics/Peachpie.Library.Graphics.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
<Description>Peachpie PHP language library functions for image processing.</Description>
1010
</PropertyGroup>
1111
<ItemGroup>
12-
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0" />
13-
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0010" />
14-
<PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta0013" />
12+
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.8" />
13+
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0" />
14+
<PackageReference Include="SixLabors.Fonts" Version="1.0.1" />
1515
</ItemGroup>
1616

1717
<ItemGroup>

src/Peachpie.Library.Graphics/PhpGd2.cs

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
5+
using System.Linq;
56
using System.Numerics;
67
using Pchp.Core;
78
using Pchp.Library.Streams;
@@ -767,7 +768,7 @@ static Font CreateFontById(int fontInd)
767768
// TODO: cache statically
768769

769770
// Get the first available of specified sans serif system fonts
770-
var result = SystemFonts.TryFind("Consolas", out var fontFamily) || SystemFonts.TryFind("Lucida Console", out fontFamily) || SystemFonts.TryFind("Arial", out fontFamily) || SystemFonts.TryFind("Verdana", out fontFamily) || SystemFonts.TryFind("Tahoma", out fontFamily);
771+
var result = SystemFonts.TryGet("Consolas", out var fontFamily) || SystemFonts.TryGet("Lucida Console", out fontFamily) || SystemFonts.TryGet("Arial", out fontFamily) || SystemFonts.TryGet("Verdana", out fontFamily) || SystemFonts.TryGet("Tahoma", out fontFamily);
771772

772773
// Couldn't find the system font.
773774
if (!result)
@@ -777,7 +778,7 @@ static Font CreateFontById(int fontInd)
777778
var fontStyle = FontStyle.Regular;
778779
if (fontInd == 3 || fontInd >= 5)
779780
{
780-
if (fontFamily.IsStyleAvailable(FontStyle.Bold))
781+
if (fontFamily.TryGetMetrics(FontStyle.Bold, out _))
781782
{
782783
fontStyle = FontStyle.Bold;
783784
}
@@ -811,12 +812,7 @@ static Font CreateFontByFontFile(Context ctx, string font_file, double size)
811812

812813
try
813814
{
814-
family = new FontCollection().Install(font_stream.RawStream); // TODO: perf: global font collection cache
815-
816-
if (family == null)
817-
{
818-
throw new InvalidDataException();
819-
}
815+
family = new FontCollection().Add(font_stream.RawStream); // TODO: perf: global font collection cache
820816
}
821817
catch
822818
{
@@ -830,19 +826,19 @@ static Font CreateFontByFontFile(Context ctx, string font_file, double size)
830826

831827
FontStyle style;
832828

833-
if (family.IsStyleAvailable(FontStyle.Regular))
829+
if (family.TryGetMetrics(FontStyle.Regular, out _))
834830
{
835831
style = FontStyle.Regular;
836832
}
837-
else if (family.IsStyleAvailable(FontStyle.Bold))
833+
else if (family.TryGetMetrics(FontStyle.Bold, out _))
838834
{
839835
style = FontStyle.Bold;
840836
}
841-
else if (family.IsStyleAvailable(FontStyle.Italic))
837+
else if (family.TryGetMetrics(FontStyle.Italic, out _))
842838
{
843839
style = FontStyle.Italic;
844840
}
845-
else if (family.IsStyleAvailable(FontStyle.BoldItalic))
841+
else if (family.TryGetMetrics(FontStyle.BoldItalic, out _))
846842
{
847843
style = FontStyle.BoldItalic;
848844
}
@@ -1034,7 +1030,7 @@ public static bool imagerectangle(PhpResource im, int x1, int y1, int x2, int y2
10341030

10351031
var rect = new RectangleF(x1, y1, x2 - x1, y2 - y1);
10361032

1037-
var opt = new ShapeGraphicsOptions();
1033+
var opt = new DrawingOptions();
10381034
opt.GraphicsOptions.Antialias = img.AntiAlias;
10391035

10401036
img.Image.Mutate(o => o.Draw(opt, FromRGBA(col), 1.0f, rect));
@@ -1109,8 +1105,8 @@ public static bool imagesettile(PhpResource image, PhpResource tile)
11091105
return null;
11101106
}
11111107

1112-
var rendererOptions = new RendererOptions(font);
1113-
var textsize = TextMeasurer.Measure(text, rendererOptions);
1108+
var rendererOptions = new TextOptions(font);
1109+
var textsize = TextMeasurer.MeasureSize(text, rendererOptions);
11141110

11151111
// text transformation:
11161112
var matrix = (angle == 0.0) ? Matrix3x2.Identity : Matrix3x2.CreateRotation((float)(angle * -2.0 * Math.PI / 360.0f));
@@ -1213,9 +1209,11 @@ public static bool imageline(PhpResource im, int x1, int y1, int x2, int y2, int
12131209
var img = PhpGdImageResource.ValidImage(im);
12141210
if (img != null)
12151211
{
1216-
var opt = new ShapeGraphicsOptions();
1212+
var opt = new DrawingOptions();
12171213
opt.GraphicsOptions.Antialias = img.AntiAlias;
1218-
img.Image.Mutate(o => o.DrawLines(opt, GetAlphaColor(img, color), 1.0f, new PointF[] { new PointF(x1, y1), new PointF(x2, y2) }));
1214+
img.Image.Mutate(
1215+
o => o.DrawLine(opt, GetAlphaColor(img, color), 1.0f, new PointF[] { new PointF(x1, y1), new PointF(x2, y2) })
1216+
);
12191217

12201218
return true;
12211219
}
@@ -1518,7 +1516,7 @@ public static bool imageellipse(PhpResource im, int cx, int cy, int w, int h, lo
15181516

15191517
var ellipse = new EllipsePolygon(cx, cy, w, h);
15201518

1521-
var opt = new ShapeGraphicsOptions();
1519+
var opt = new DrawingOptions();
15221520
opt.GraphicsOptions.Antialias = img.AntiAlias;
15231521

15241522
img.Image.Mutate(o => o.Draw(opt, GetAlphaColor(img, col), 1.0f, ellipse));
@@ -1536,7 +1534,7 @@ public static bool imagefilledellipse(PhpResource im, int cx, int cy, int w, int
15361534
return false;
15371535

15381536
var ellipse = new EllipsePolygon(cx, cy, w, h);
1539-
var opt = new ShapeGraphicsOptions();
1537+
var opt = new DrawingOptions();
15401538
opt.GraphicsOptions.Antialias = img.AntiAlias;
15411539

15421540
if (img.tiled != null)
@@ -1674,17 +1672,19 @@ public static int imagecolorresolvealpha(PhpResource im, int red, int green, int
16741672

16751673
static bool DrawText(PhpResource im, int fontInd, int x, int y, string text, long col, bool up = false)
16761674
{
1677-
PhpGdImageResource img = PhpGdImageResource.ValidImage(im);
1675+
var img = PhpGdImageResource.ValidImage(im);
16781676
if (img == null)
1677+
{
16791678
return false;
1679+
}
16801680

16811681
if (x < 0 || y < 0) return true;
16821682
if (x > img.Image.Width || y > img.Image.Height) return true;
16831683

16841684
var font = CreateFontById(fontInd);
16851685
var color = FromRGBA(col);
16861686

1687-
var opt = new TextGraphicsOptions();
1687+
var opt = new DrawingOptions();
16881688
opt.GraphicsOptions.Antialias = img.AntiAlias;
16891689

16901690
if (up)
@@ -1732,7 +1732,7 @@ static FontRectangle imagefontsize(int fontInd)
17321732
var font = CreateFontById(fontInd);
17331733
if (font != null)
17341734
{
1735-
var size = TextMeasurer.Measure("X", new RendererOptions(font));
1735+
var size = TextMeasurer.MeasureSize("X", new TextOptions(font));
17361736

17371737
if (arr == null || arr.Length <= fontInd)
17381738
{
@@ -1823,9 +1823,10 @@ public static bool imagefilledarc(PhpResource im, long cx, long cy, long w, long
18231823
AdjustAnglesAndSize(ref w, ref h, ref s, ref e, ref range);
18241824

18251825
// Path Builder object to be used in all the branches
1826-
PathBuilder pathBuilder = new PathBuilder();
1826+
var pathBuilder = new PathBuilder();
18271827
var color = FromRGBA(col);
1828-
var pen = new Pen(color, 1);
1828+
1829+
var pen = new SolidPen(color, 1);
18291830

18301831
// edge points, used for both pie and chord
18311832
PointF startingPoint = new PointF(cx + (int)(Math.Cos(s * Math.PI / 180) * (w / 2.0)), cy + (int)(Math.Sin(s * Math.PI / 180) * (h / 2.0)));
@@ -2073,7 +2074,7 @@ public static bool imageopenpolygon(PhpResource image, PhpArray points, int num_
20732074

20742075
var pointsF = GetPointFsFromArray(points, num_points);
20752076

2076-
img.Image.Mutate(o => o.DrawLines(new Pen(FromRGBA(color), 1.0f), pointsF));
2077+
img.Image.Mutate(o => o.DrawLine(new SolidPen(FromRGBA(color), 1.0f), pointsF));
20772078

20782079
return true;
20792080
}
@@ -2107,29 +2108,19 @@ static bool Polygon(PhpResource im, PhpArray point, int num_points, long col, bo
21072108

21082109
if (filled)
21092110
{
2110-
IBrush brush;
2111-
2112-
switch (col)
2111+
var brush = col switch
21132112
{
2114-
case (long)ColorValues.TILED:
2115-
brush = img.tiled;
2116-
break;
2117-
case (long)ColorValues.STYLED:
2118-
brush = img.styled;
2119-
break;
2120-
case (long)ColorValues.BRUSHED:
2121-
brush = img.brushed;
2122-
break;
2123-
default:
2124-
brush = new SolidBrush(FromRGBA(col));
2125-
break;
2126-
}
2113+
(long)ColorValues.TILED => img.tiled,
2114+
(long)ColorValues.STYLED => img.styled,
2115+
(long)ColorValues.BRUSHED => img.brushed,
2116+
_ => new SolidBrush(FromRGBA(col))
2117+
};
21272118

21282119
img.Image.Mutate(o => o.FillPolygon(brush, points));
21292120
}
21302121
else
21312122
{
2132-
img.Image.Mutate(o => o.DrawPolygon(new Pen(FromRGBA(col), 1.0f), points));
2123+
img.Image.Mutate(o => o.DrawPolygon(new SolidPen(FromRGBA(col), 1.0f), points));
21332124
}
21342125

21352126
return true;

src/Peachpie.Library.Graphics/PhpGdImageResource.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4-
using System.Linq;
5-
using System.Threading.Tasks;
64
using SixLabors.ImageSharp;
75
using SixLabors.ImageSharp.Formats;
86
using Pchp.Core;
@@ -58,10 +56,10 @@ internal set
5856

5957
internal Rgba32 transparentColor;
6058
internal bool IsTransparentColSet = false;
61-
62-
internal IBrush styled = null;
63-
internal IBrush brushed = null;
64-
internal IBrush tiled = null;
59+
60+
internal Brush styled = null;
61+
internal Brush brushed = null;
62+
internal Brush tiled = null;
6563

6664
internal int LineThickness = 1;
6765

0 commit comments

Comments
 (0)