Skip to content

Commit

Permalink
Add a fast path for reading image.Gray images
Browse files Browse the repository at this point in the history
  • Loading branch information
jsummers committed Oct 21, 2012
1 parent 5639c3d commit cbb679b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
34 changes: 34 additions & 0 deletions fpconvert1.go
Expand Up @@ -48,6 +48,7 @@ type convertSrcWorkContext struct {
src_AsRGBA *image.RGBA
src_AsNRGBA *image.NRGBA
src_AsYCbCr *image.YCbCr
src_AsGray *image.Gray
cvtRowFn func(fp *FPObject, cctx *convertSrcWorkContext, j int)
}

Expand Down Expand Up @@ -122,6 +123,35 @@ func convertSrcRow_Any(fp *FPObject, wc *convertSrcWorkContext, j int) {
}
}

// Convert row j from wc.src_AsGray to wc.dst.
// This is an optimized version of convertSrcRow_Any().
func convertSrcRow_Gray(fp *FPObject, wc *convertSrcWorkContext, j int) {
for i := 0; i < fp.srcW; i++ {
srcPix := wc.src_AsGray.Pix[wc.src_AsGray.Stride*j+i]

// Identify the slice of samples representing this pixel in the
// converted image.
dstSam := wc.dst.Pix[j*wc.dst.Stride+i*4 : j*wc.dst.Stride+i*4+4]

// Do color correction, if necessary.
if fp.inputCCF != nil && wc.inputLUT_8to32 != nil {
// Convert to linear color, using a lookup table.
dstSam[0] = wc.inputLUT_8to32[srcPix]
} else {
// In all other cases, first copy the uncorrected sample to dstSam.
dstSam[0] = float32(srcPix) / 255.0

if fp.inputCCF != nil {
// Convert to linear color, without a lookup table.
fp.inputCCF(dstSam[0:1])
}
}
dstSam[1] = dstSam[0]
dstSam[2] = dstSam[0]
dstSam[3] = 1.0
}
}

// Convert row j from wc.src_AsNRGBA to wc.dst.
// This is an optimized version of convertSrcRow_Any().
func convertSrcRow_NRGBA(fp *FPObject, wc *convertSrcWorkContext, j int) {
Expand Down Expand Up @@ -314,6 +344,7 @@ func (fp *FPObject) convertSrc(src image.Image, dst *FPImage) error {
wc.src_AsRGBA, _ = wc.srcImage.(*image.RGBA)
wc.src_AsNRGBA, _ = wc.srcImage.(*image.NRGBA)
wc.src_AsYCbCr, _ = wc.srcImage.(*image.YCbCr)
wc.src_AsGray, _ = wc.srcImage.(*image.Gray)

// Select a conversion strategy.
if wc.src_AsNRGBA != nil {
Expand All @@ -325,6 +356,9 @@ func (fp *FPObject) convertSrc(src image.Image, dst *FPImage) error {
} else if wc.src_AsYCbCr != nil {
wc.cvtRowFn = convertSrcRow_YCbCr
wc.inputLUT_8to32 = fp.makeInputLUT_Xto32(256)
} else if wc.src_AsGray != nil {
wc.cvtRowFn = convertSrcRow_Gray
wc.inputLUT_8to32 = fp.makeInputLUT_Xto32(256)
} else {
wc.cvtRowFn = convertSrcRow_Any
wc.inputLUT_16to32 = fp.makeInputLUT_Xto32(65536)
Expand Down
7 changes: 7 additions & 0 deletions fpresize_test.go
Expand Up @@ -434,4 +434,11 @@ func TestMain(t *testing.T) {
opts.outfn = "test18.png"
opts.trnsTest1 = true
runDrawTest(t, opts)

resetOpts(opts)
opts.outfn = "test19.png"
opts.infn = "g8.png"
opts.bounds.Max.X = 18
opts.bounds.Max.Y = 18
runFileTest(t, opts)
}
Binary file added testdata/expected/test19.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/srcimg/g8.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cbb679b

Please sign in to comment.