FAQ
This code:
https://github.com/ajstarks/openvg/blob/master/openvg.go#l357-380 loads
image bytes to be passed to a C function.

What is the best way to optimize this for better performance? I suspect
reading a scanline at a time is one approach (used
by https://github.com/ajstarks/openvg/blob/master/util/raw2png.go).

Are there other methods?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Nigel Tao at Aug 29, 2013 at 12:11 am

    On Thu, Aug 29, 2013 at 10:07 AM, ajstarks wrote:
    What is the best way to optimize this for better performance?
    Have fast paths for the common image types (image.RGBA, image.NRGBA)
    that use their Pix field directly instead of going through At and Set.

    See the image/draw source code for examples.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nigel Tao at Aug 29, 2013 at 12:19 am

    On Thu, Aug 29, 2013 at 10:07 AM, ajstarks wrote:
    This code:
    https://github.com/ajstarks/openvg/blob/master/openvg.go#l357-380 loads
    image bytes to be passed to a C function.
    Your code says:

    var r, g, b, a uint32
    ...
    r, g, b, a = im.At(etc).RGBA()
    data[n] = C.VGubyte(r)

    This is incorrect. http://golang.org/pkg/image/color/#Color says that
    "Each value ranges within [0, 0xFFFF]". By truncating to a byte, you
    are taking the least significant 8 bits of the color value instead of
    the most significant 8 bits. Instead, you want
    data[n] = C.VGubyte(r >> 8)

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nigel Tao at Aug 29, 2013 at 12:40 am

    On Thu, Aug 29, 2013 at 10:07 AM, ajstarks wrote:
    I suspect
    reading a scanline at a time is one approach (used by
    https://github.com/ajstarks/openvg/blob/master/util/raw2png.go).
    I would rewrite


    scanline := make([]color.NRGBA, width)
    img := image.NewNRGBA(image.Rect(0, 0, width, height))
    // OpenVG has origin at lower left, y increasing up
    for y := height; y > 0; y-- {
         binary.Read(os.Stdin, binary.LittleEndian, &scanline)
         for x := 0; x < width; x++ {
             img.Set(x, y, scanline[x])
         }
    }


    as (off the top of my head, so untested, "haven't even compiled it" code):


    r := bufio.NewReader(os.Stdin)
    m := image.NewNRGBA(image.Rect(0, 0, width, height))
    // OpenVG has origin at lower left, y increasing up.
    for y := height - 1; y >= 0; y-- {
         i := m.PixOffset(0, y)
         if _, err := io.ReadFull(r, m.Pix[i:i+4*width]); err != nil {
             log.Fatal(err)
         }
    }


    I also think that your original raw2png.go code has an off-by-one error:


    for y := height; y > 0; y-- {
         etc
         img.Set(x, y, etc)
    }


    should be:


    for y := height-1; y >= 0; y-- {
         etc
         img.Set(x, y, etc)
    }


    otherwise the top row of the output remains transparent black and the
    first (bottom) row of the input is lost.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Anthony Starks at Aug 29, 2013 at 3:24 am
    adding these fixes decreased the runtime from 18.556s to 5.812s on the Raspberry Pi.

    Thanks!
    On Aug 28, 2013, at 8:39 PM, Nigel Tao wrote:
    On Thu, Aug 29, 2013 at 10:07 AM, ajstarks wrote:
    I suspect
    reading a scanline at a time is one approach (used by
    https://github.com/ajstarks/openvg/blob/master/util/raw2png.go).
    I would rewrite


    scanline := make([]color.NRGBA, width)
    img := image.NewNRGBA(image.Rect(0, 0, width, height))
    // OpenVG has origin at lower left, y increasing up
    for y := height; y > 0; y-- {
    binary.Read(os.Stdin, binary.LittleEndian, &scanline)
    for x := 0; x < width; x++ {
    img.Set(x, y, scanline[x])
    }
    }


    as (off the top of my head, so untested, "haven't even compiled it" code):


    r := bufio.NewReader(os.Stdin)
    m := image.NewNRGBA(image.Rect(0, 0, width, height))
    // OpenVG has origin at lower left, y increasing up.
    for y := height - 1; y >= 0; y-- {
    i := m.PixOffset(0, y)
    if _, err := io.ReadFull(r, m.Pix[i:i+4*width]); err != nil {
    log.Fatal(err)
    }
    }


    I also think that your original raw2png.go code has an off-by-one error:


    for y := height; y > 0; y-- {
    etc
    img.Set(x, y, etc)
    }


    should be:


    for y := height-1; y >= 0; y-- {
    etc
    img.Set(x, y, etc)
    }


    otherwise the top row of the output remains transparent black and the
    first (bottom) row of the input is lost.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 29, '13 at 12:07a
activeAug 29, '13 at 3:24a
posts5
users2
websitegolang.org

2 users in discussion

Nigel Tao: 3 posts Anthony Starks: 2 posts

People

Translate

site design / logo © 2021 Grokbase