Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions src/pixie/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,24 @@ proc copy*(image: Image): Image {.raises: [].} =
template dataIndex*(image: Image, x, y: int): int =
image.width * y + x

proc mix*(a, b: uint8, t: float32): uint8 {.inline, raises: [].} =
## Linearly interpolate between a and b using t.
let t = round(t * 255).uint32
((a * (255 - t) + b * t) div 255).uint8

proc mix*(a, b: ColorRGBX, t: float32): ColorRGBX {.inline, raises: [].} =
## Linearly interpolate between a and b using t.
let x = round(t * 255).uint32
result.r = ((a.r.uint32 * (255 - x) + b.r.uint32 * x) div 255).uint8
result.g = ((a.g.uint32 * (255 - x) + b.g.uint32 * x) div 255).uint8
result.b = ((a.b.uint32 * (255 - x) + b.b.uint32 * x) div 255).uint8
result.a = ((a.a.uint32 * (255 - x) + b.a.uint32 * x) div 255).uint8
result.r = ((a.r.uint32 * (255 - x) + b.r.uint32 * x + 127) div 255).uint8
result.g = ((a.g.uint32 * (255 - x) + b.g.uint32 * x + 127) div 255).uint8
result.b = ((a.b.uint32 * (255 - x) + b.b.uint32 * x + 127) div 255).uint8
result.a = ((a.a.uint32 * (255 - x) + b.a.uint32 * x + 127) div 255).uint8

proc `*`*(color: ColorRGBX, opacity: float32): ColorRGBX {.raises: [].} =
if opacity == 0:
rgbx(0, 0, 0, 0)
else:
let
x = round(opacity * 255).uint32
r = ((color.r * x) div 255).uint8
g = ((color.g * x) div 255).uint8
b = ((color.b * x) div 255).uint8
a = ((color.a * x) div 255).uint8
r = ((color.r * x + 127) div 255).uint8
g = ((color.g * x + 127) div 255).uint8
b = ((color.b * x + 127) div 255).uint8
a = ((color.a * x + 127) div 255).uint8
rgbx(r, g, b, a)

proc snapToPixels*(rect: Rect): Rect {.raises: [].} =
Expand Down
8 changes: 4 additions & 4 deletions src/pixie/images.nim
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ proc minifyBy2*(
c = src.data[bottomRowStart + x * 2 + 1]
d = src.data[bottomRowStart + x * 2]
mixed = rgbx(
((a.r.uint32 + b.r + c.r + d.r) div 4).uint8,
((a.g.uint32 + b.g + c.g + d.g) div 4).uint8,
((a.b.uint32 + b.b + c.b + d.b) div 4).uint8,
((a.a.uint32 + b.a + c.a + d.a) div 4).uint8
((a.r.uint32 + b.r + c.r + d.r + 2) div 4).uint8,
((a.g.uint32 + b.g + c.g + d.g + 2) div 4).uint8,
((a.b.uint32 + b.b + c.b + d.b + 2) div 4).uint8,
((a.a.uint32 + b.a + c.a + d.a + 2) div 4).uint8
)
result.data[result.dataIndex(x, y)] = mixed

Expand Down
15 changes: 9 additions & 6 deletions src/pixie/simd/avx2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ proc minifyBy2Avx2*(image: Image, power = 1): Image {.simd.} =
)
let
oddMask = mm256_set1_epi16(0xff00)
vec2 = mm256_set1_epi16(2)
permuteControl = mm256_set_epi32(7, 7, 7, 7, 6, 4, 2, 0)
for y in 0 ..< resultEvenHeight:
let
Expand Down Expand Up @@ -323,8 +324,10 @@ proc minifyBy2Avx2*(image: Image, power = 1): Image {.simd.} =
bottomAddedOdd = mm256_add_epi16(bottomOdd, bottomShiftedOdd)
addedEven = mm256_add_epi16(topAddedEven, bottomAddedEven)
addedOdd = mm256_add_epi16(topAddedOdd, bottomAddedOdd)
addedEvenDiv4 = mm256_srli_epi16(addedEven, 2)
addedOddDiv4 = mm256_srli_epi16(addedOdd, 2)
addedEvenRounding = mm256_add_epi16(addedEven, vec2)
addedOddRounding = mm256_add_epi16(addedOdd, vec2)
addedEvenDiv4 = mm256_srli_epi16(addedEvenRounding, 2)
addedOddDiv4 = mm256_srli_epi16(addedOddRounding, 2)
merged = mm256_or_si256(addedEvenDiv4, mm256_slli_epi16(addedOddDiv4, 8))
# Merged has the correct values for the next two pixels at
# index 0, 2, 4, 6 so permute into position and store
Expand All @@ -342,10 +345,10 @@ proc minifyBy2Avx2*(image: Image, power = 1): Image {.simd.} =
c = src.data[bottomRowStart + x * 2 + 1]
d = src.data[bottomRowStart + x * 2]
mixed = rgbx(
((a.r.uint32 + b.r + c.r + d.r) div 4).uint8,
((a.g.uint32 + b.g + c.g + d.g) div 4).uint8,
((a.b.uint32 + b.b + c.b + d.b) div 4).uint8,
((a.a.uint32 + b.a + c.a + d.a) div 4).uint8
((a.r.uint32 + b.r + c.r + d.r + 2) div 4).uint8,
((a.g.uint32 + b.g + c.g + d.g + 2) div 4).uint8,
((a.b.uint32 + b.b + c.b + d.b + 2) div 4).uint8,
((a.a.uint32 + b.a + c.a + d.a + 2) div 4).uint8
)
result.data[result.dataIndex(x, y)] = mixed

Expand Down
48 changes: 30 additions & 18 deletions src/pixie/simd/sse2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,33 @@ proc minifyBy2Sse2*(image: Image, power = 1): Image {.simd.} =
if srcWidthIsOdd: resultEvenWidth + 1 else: resultEvenWidth,
if srcHeightIsOdd: resultEvenHeight + 1 else: resultEvenHeight
)
let oddMask = mm_set1_epi16(0xff00)
let
oddMask = mm_set1_epi16(0xff00)
loMask = mm_set_epi32(0, 0, uint32.high, uint32.high)
hiMask = mm_set_epi32(uint32.high, uint32.high, 0, 0)
vec2 = mm_set1_epi16(2)
for y in 0 ..< resultEvenHeight:
let
topRowStart = src.dataIndex(0, y * 2)
bottomRowStart = src.dataIndex(0, y * 2 + 1)

template loadEven(src: Image, idx: int): M128i =
var
a = mm_loadu_si128(src.data[idx].addr)
b = mm_loadu_si128(src.data[idx + 4].addr)
a = mm_shuffle_epi32(a, MM_SHUFFLE(3, 3, 2, 0))
b = mm_shuffle_epi32(b, MM_SHUFFLE(2, 0, 3, 3))
a = mm_and_si128(a, loMask)
b = mm_and_si128(b, hiMask)
mm_or_si128(a, b)

var x: int
while x <= resultEvenWidth - 4:
while x <= resultEvenWidth - 9:
let
top = mm_loadu_si128(src.data[topRowStart + x * 2].addr)
bottom = mm_loadu_si128(src.data[bottomRowStart + x * 2].addr)
topShifted = mm_srli_si128(top, 4)
bottomShifted = mm_srli_si128(bottom, 4)
top = loadEven(src, topRowStart + x * 2)
bottom = loadEven(src, bottomRowStart + x * 2)
topShifted = loadEven(src, topRowStart + x * 2 + 1)
bottomShifted = loadEven(src, bottomRowStart + x * 2 + 1)
topEven = mm_andnot_si128(oddMask, top)
topOdd = mm_srli_epi16(top, 8)
bottomEven = mm_andnot_si128(oddMask, bottom)
Expand All @@ -377,15 +391,13 @@ proc minifyBy2Sse2*(image: Image, power = 1): Image {.simd.} =
bottomAddedOdd = mm_add_epi16(bottomOdd, bottomShiftedOdd)
addedEven = mm_add_epi16(topAddedEven, bottomAddedEven)
addedOdd = mm_add_epi16(topAddedOdd, bottomAddedOdd)
addedEvenDiv4 = mm_srli_epi16(addedEven, 2)
addedOddDiv4 = mm_srli_epi16(addedOdd, 2)
addedEvenRounding = mm_add_epi16(addedEven, vec2)
addedOddRounding = mm_add_epi16(addedOdd, vec2)
addedEvenDiv4 = mm_srli_epi16(addedEvenRounding, 2)
addedOddDiv4 = mm_srli_epi16(addedOddRounding, 2)
merged = mm_or_si128(addedEvenDiv4, mm_slli_epi16(addedOddDiv4, 8))
# Merged has the correct values for the next two pixels at
# index 0 and 2 so shift 0 and 2 into position and store
shuffled = mm_shuffle_epi32(merged, MM_SHUFFLE(3, 3, 2, 0))
lower = mm_cvtsi128_si64(shuffled)
copyMem(result.data[result.dataIndex(x, y)].addr, lower.unsafeAddr, 8)
x += 2
mm_storeu_si128(result.data[result.dataIndex(x, y)].addr, merged)
x += 4

for x in x ..< resultEvenWidth:
let
Expand All @@ -394,10 +406,10 @@ proc minifyBy2Sse2*(image: Image, power = 1): Image {.simd.} =
c = src.data[bottomRowStart + x * 2 + 1]
d = src.data[bottomRowStart + x * 2]
mixed = rgbx(
((a.r.uint32 + b.r + c.r + d.r) div 4).uint8,
((a.g.uint32 + b.g + c.g + d.g) div 4).uint8,
((a.b.uint32 + b.b + c.b + d.b) div 4).uint8,
((a.a.uint32 + b.a + c.a + d.a) div 4).uint8
((a.r.uint32 + b.r + c.r + d.r + 2) div 4).uint8,
((a.g.uint32 + b.g + c.g + d.g + 2) div 4).uint8,
((a.b.uint32 + b.b + c.b + d.b + 2) div 4).uint8,
((a.a.uint32 + b.a + c.a + d.a + 2) div 4).uint8
)
result.data[result.dataIndex(x, y)] = mixed

Expand Down
Binary file modified tests/contexts/beginPath_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/bezierCurveTo_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/bezierCurveTo_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/blendmode_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/clearRect_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/clip_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/clip_1b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/clip_1c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/clip_1d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/clip_1e.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/clip_1f.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/clip_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/clip_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/clip_text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/closePath_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/draw_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/draw_image_rhino.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/draw_image_rhino2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/draw_image_scaled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/draw_image_self_scaled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/draw_image_translated.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/ellipse_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/fillText_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/fill_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/globalAlpha_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/contexts/moveTo_1.png
Binary file modified tests/contexts/paintSaveRestore.png
Binary file modified tests/contexts/quadracticCurveTo_1.png
Binary file modified tests/contexts/quadracticCurveTo_2.png
Binary file modified tests/contexts/resetTransform_1.png
Binary file modified tests/contexts/resetTransform_2.png
Binary file modified tests/contexts/rotate_1.png
Binary file modified tests/contexts/save_1.png
Binary file modified tests/contexts/scale_1.png
Binary file modified tests/contexts/setLineDash_1.png
Binary file modified tests/contexts/setTransform_1.png
Binary file modified tests/contexts/strokeRect_1.png
Binary file modified tests/contexts/strokeRect_2.png
Binary file modified tests/contexts/strokeText_1.png
Binary file modified tests/contexts/stroke_1.png
Binary file modified tests/contexts/stroke_2.png
Binary file modified tests/contexts/stroke_3.png
Binary file modified tests/contexts/textBaseline_1.png
Binary file modified tests/contexts/translate_1.png
Binary file modified tests/fileformats/gif/3x5.png
Binary file modified tests/fileformats/gif/audrey.png
Binary file modified tests/fileformats/gif/sunflower.png
Binary file modified tests/images/drawEllipse.png
Binary file modified tests/images/drawPolygon.png
Binary file modified tests/images/drawRect.png
Binary file modified tests/images/drawRoundedRect.png
Binary file modified tests/images/drawSegment.png
Binary file modified tests/images/fillOptimization.png
Binary file modified tests/images/fillOptimization2.png
Binary file modified tests/images/flipped1.png
Binary file modified tests/images/flipped2.png
Binary file modified tests/images/flipped3.png
Binary file modified tests/images/imageblur20.png
Binary file modified tests/images/imageblur20oob.png
Binary file modified tests/images/magnifiedBy2.png
Binary file modified tests/images/magnifiedBy4.png
Binary file modified tests/images/minifiedBy2.png
Binary file modified tests/images/minifiedBy4.png
Binary file modified tests/images/minifiedMandrill.png
Binary file modified tests/images/rotate180.png
Binary file modified tests/images/rotate270.png
Binary file modified tests/images/rotate360.png
Binary file modified tests/images/rotate90.png
Binary file modified tests/images/scaleHalf.png
Binary file modified tests/images/strokeEllipse.png
Binary file modified tests/images/strokePolygon.png
Binary file modified tests/images/strokeRect.png
Binary file modified tests/images/strokeRoundedRect.png
Binary file modified tests/images/superimage1.png
Binary file modified tests/images/superimage2.png
Binary file modified tests/images/superimage3.png
Binary file modified tests/images/superimage4.png
Binary file modified tests/images/superimage5.png
Binary file modified tests/images/superimage6.png
Binary file modified tests/paths/ButtCap.png
Binary file modified tests/paths/RoundCap.png
Binary file modified tests/paths/SquareCap.png
Binary file modified tests/paths/arc.png
Binary file modified tests/paths/arcTo1.png
Binary file modified tests/paths/arcTo2.png
Binary file modified tests/paths/arcTo3.png
Binary file modified tests/paths/boxBevel.png
Binary file modified tests/paths/boxMiter.png
Binary file modified tests/paths/boxRound.png
Binary file modified tests/paths/dashes.png
Binary file modified tests/paths/fillImagePaint.png
Binary file modified tests/paths/fillTiledImagePaint.png
Binary file modified tests/paths/gradientAngular.png
Binary file modified tests/paths/gradientAngularOpacity.png
Binary file modified tests/paths/gradientLinear.png
Binary file modified tests/paths/gradientLinear2.png
Binary file modified tests/paths/gradientRadial.png
Binary file modified tests/paths/miterLimit_10deg_2.00num.png
Binary file modified tests/paths/miterLimit_145deg_2.00num.png
Binary file modified tests/paths/miterLimit_145deg_3.32num.png
Binary file modified tests/paths/miterLimit_145deg_3.33num.png
Binary file modified tests/paths/miterLimit_155deg_2.00num.png
Binary file modified tests/paths/miterLimit_165deg_10.00num.png
Binary file modified tests/paths/miterLimit_165deg_2.00num.png
Binary file modified tests/paths/opacityFill.png
Binary file modified tests/paths/opacityStroke.png
Binary file modified tests/paths/paintImage.png
Binary file modified tests/paths/paintImageOpacity.png
Binary file modified tests/paths/paintImageTiled.png
Binary file modified tests/paths/paintImageTiledOpacity.png
Binary file modified tests/paths/paintSolid.png
Binary file modified tests/paths/path0pxCover.png
Binary file modified tests/paths/path1pxCover.png
Binary file modified tests/paths/pathBlackRectangle.png
Binary file modified tests/paths/pathBlackRectangleZ.png
Binary file modified tests/paths/pathBottomArc.png
Binary file modified tests/paths/pathCornerArc.png
Binary file modified tests/paths/pathHeart.png
Binary file modified tests/paths/pathInvertedCornerArc.png
Binary file modified tests/paths/pathRedRectangle.png
Binary file modified tests/paths/pathRotatedArc.png
Binary file modified tests/paths/pathStroke1.png
Binary file modified tests/paths/pathStroke1Big.png
Binary file modified tests/paths/pathStroke2.png
Binary file modified tests/paths/pathStroke3.png
Binary file modified tests/paths/pathSwish.png
Binary file modified tests/paths/pathYellowRectangle.png
Binary file modified tests/paths/pixelScale.png
Binary file modified tests/paths/rectExcludeMask.png
Binary file modified tests/paths/rectExcludeMaskAA.png
Binary file modified tests/paths/rectMask.png
Binary file modified tests/paths/rectMaskAA.png
Binary file modified tests/paths/rectMaskStroke.png
Binary file modified tests/paths/selfclosing.png