Skip to content

Commit

Permalink
Optimize YCbCr Conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
disintegration committed Mar 19, 2015
1 parent 7db23ee commit 3ab6ec5
Showing 1 changed file with 53 additions and 14 deletions.
67 changes: 53 additions & 14 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,20 +318,59 @@ func Clone(img image.Image) *image.NRGBA {
parallel(dstH, func(partStart, partEnd int) {
for dstY := partStart; dstY < partEnd; dstY++ {
di := dst.PixOffset(0, dstY)
for dstX := 0; dstX < dstW; dstX++ {

srcX := srcMinX + dstX
srcY := srcMinY + dstY
siy := src.YOffset(srcX, srcY)
sic := src.COffset(srcX, srcY)
r, g, b := color.YCbCrToRGB(src.Y[siy], src.Cb[sic], src.Cr[sic])
dst.Pix[di+0] = r
dst.Pix[di+1] = g
dst.Pix[di+2] = b
dst.Pix[di+3] = 0xff

di += 4

switch src.SubsampleRatio {
case image.YCbCrSubsampleRatio422:
siy0 := dstY * src.YStride
sic0 := dstY * src.CStride
for dstX := 0; dstX < dstW; dstX = dstX + 1 {
siy := siy0 + dstX
sic := sic0 + ((srcMinX+dstX)/2 - srcMinX/2)
r, g, b := color.YCbCrToRGB(src.Y[siy], src.Cb[sic], src.Cr[sic])
dst.Pix[di+0] = r
dst.Pix[di+1] = g
dst.Pix[di+2] = b
dst.Pix[di+3] = 0xff
di += 4
}
case image.YCbCrSubsampleRatio420:
siy0 := dstY * src.YStride
sic0 := ((srcMinY+dstY)/2 - srcMinY/2) * src.CStride
for dstX := 0; dstX < dstW; dstX = dstX + 1 {
siy := siy0 + dstX
sic := sic0 + ((srcMinX+dstX)/2 - srcMinX/2)
r, g, b := color.YCbCrToRGB(src.Y[siy], src.Cb[sic], src.Cr[sic])
dst.Pix[di+0] = r
dst.Pix[di+1] = g
dst.Pix[di+2] = b
dst.Pix[di+3] = 0xff
di += 4
}
case image.YCbCrSubsampleRatio440:
siy0 := dstY * src.YStride
sic0 := ((srcMinY+dstY)/2 - srcMinY/2) * src.CStride
for dstX := 0; dstX < dstW; dstX = dstX + 1 {
siy := siy0 + dstX
sic := sic0 + dstX
r, g, b := color.YCbCrToRGB(src.Y[siy], src.Cb[sic], src.Cr[sic])
dst.Pix[di+0] = r
dst.Pix[di+1] = g
dst.Pix[di+2] = b
dst.Pix[di+3] = 0xff
di += 4
}
default:
siy0 := dstY * src.YStride
sic0 := dstY * src.CStride
for dstX := 0; dstX < dstW; dstX++ {
siy := siy0 + dstX
sic := sic0 + dstX
r, g, b := color.YCbCrToRGB(src.Y[siy], src.Cb[sic], src.Cr[sic])
dst.Pix[di+0] = r
dst.Pix[di+1] = g
dst.Pix[di+2] = b
dst.Pix[di+3] = 0xff
di += 4
}
}
}
})
Expand Down

0 comments on commit 3ab6ec5

Please sign in to comment.