FAQ
Right now, seems like this is the only way:

http://play.golang.org/p/VAW8Ie3Rre

Would anyone know of a smarter/better/shorter way? As an aside, wouldn't an
image.Image.Clone() method be useful in the standard library?

A really-not-that-exotic use-case: wanting to keep the original image in
memory while modifying a copy of it -- without having to
load-from-disk-and-image.Decode() the same picture twice.

--
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

  • Philipp Schumann at Feb 14, 2013 at 9:43 am
    Apologies for a minor bug in that source -- the Pix slice is allocated
    a-new in the copy, but not populated with the original pixels.

    So yes then in addition to:

    copy.Pix = make([]uint8, len(pic.Pix))

    everywhere, I'd follow that by:

    copy.Pix[:] = pic.Pix[:]

    to ensure the contents of the original byte-slice is being copied over...

    Doesn't change my original question though!

    On Thursday, February 14, 2013 4:40:28 PM UTC+7, Philipp Schumann wrote:

    Right now, seems like this is the only way:

    http://play.golang.org/p/VAW8Ie3Rre

    Would anyone know of a smarter/better/shorter way? As an aside, wouldn't
    an image.Image.Clone() method be useful in the standard library?

    A really-not-that-exotic use-case: wanting to keep the original image in
    memory while modifying a copy of it -- without having to
    load-from-disk-and-image.Decode() the same picture twice.
    --
    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.
  • Philipp Schumann at Feb 14, 2013 at 9:46 am
    And of course... by cloned.Pix[:] = pic.Pix[:] I mean: copy(pic.Pix,
    cloned.Pix)
    ...
    On Thursday, February 14, 2013 4:43:17 PM UTC+7, Philipp Schumann wrote:

    Apologies for a minor bug in that source -- the Pix slice is allocated
    a-new in the copy, but not populated with the original pixels.

    So yes then in addition to:

    copy.Pix = make([]uint8, len(pic.Pix))

    everywhere, I'd follow that by:

    copy.Pix[:] = pic.Pix[:]

    to ensure the contents of the original byte-slice is being copied over...

    Doesn't change my original question though!

    On Thursday, February 14, 2013 4:40:28 PM UTC+7, Philipp Schumann wrote:

    Right now, seems like this is the only way:

    http://play.golang.org/p/VAW8Ie3Rre

    Would anyone know of a smarter/better/shorter way? As an aside, wouldn't
    an image.Image.Clone() method be useful in the standard library?

    A really-not-that-exotic use-case: wanting to keep the original image in
    memory while modifying a copy of it -- without having to
    load-from-disk-and-image.Decode() the same picture twice.
    --
    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.
  • Philipp Schumann at Feb 14, 2013 at 9:48 am
    Updated source for this question:

    http://play.golang.org/p/KJyuR9l8_n

    So, finally -- isn't there a smarter way for this functionality?

    --
    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 Feb 14, 2013 at 11:28 pm

    On Thu, Feb 14, 2013 at 8:40 PM, Philipp Schumann wrote:
    Would anyone know of a smarter/better/shorter way? As an aside, wouldn't an
    image.Image.Clone() method be useful in the standard library?
    When I want to work on a clone, I usually convert to RGBA at the same time:

    func CloneToRGBA(src image.Image) draw.Image {
    b := src.Bounds()
    dst := image.NewRGBA(b)
    draw.Draw(dst, b, src, b.Min, draw.Src)
    return dst
    }

    If you must keep the original image type, a type switch is the way to
    go. I would write it like http://play.golang.org/p/R7VPM30BBG

    P.S. you'll also need to switch on image.YCbCr if you want to clone
    the result of decoding JPEGs.

    --
    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.
  • Philipp Schumann at Feb 15, 2013 at 4:21 am
    Thanks! Yeah I'm already converting YCbCr and Paletted as the first
    pre-processing step (prior to this later processing stage where I need a
    clone and keep the original).

    But there I was getting-and-setting all pixels instead of using
    draw.Draw(), which certainly looks like a better idea :)

    On Friday, February 15, 2013 6:28:54 AM UTC+7, Nigel Tao wrote:

    On Thu, Feb 14, 2013 at 8:40 PM, Philipp Schumann
    <philipp....@gmail.com <javascript:>> wrote:
    Would anyone know of a smarter/better/shorter way? As an aside, wouldn't an
    image.Image.Clone() method be useful in the standard library?
    When I want to work on a clone, I usually convert to RGBA at the same
    time:

    func CloneToRGBA(src image.Image) draw.Image {
    b := src.Bounds()
    dst := image.NewRGBA(b)
    draw.Draw(dst, b, src, b.Min, draw.Src)
    return dst
    }

    If you must keep the original image type, a type switch is the way to
    go. I would write it like http://play.golang.org/p/R7VPM30BBG

    P.S. you'll also need to switch on image.YCbCr if you want to clone
    the result of decoding JPEGs.
    --
    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.
  • David DENG at Feb 15, 2013 at 6:56 am
    What's the operation you'll do to the image? I'd like to convert the loaded
    Image into data-structures of my own, process, put the result back to an
    Image and save to disk.

    The data-structures could look like this:

    type RGB [3]int
    type RGBData [][]RGB

    type GrayData [][]int


    David
    On Thursday, February 14, 2013 5:40:28 PM UTC+8, Philipp Schumann wrote:

    Right now, seems like this is the only way:

    http://play.golang.org/p/VAW8Ie3Rre

    Would anyone know of a smarter/better/shorter way? As an aside, wouldn't
    an image.Image.Clone() method be useful in the standard library?

    A really-not-that-exotic use-case: wanting to keep the original image in
    memory while modifying a copy of it -- without having to
    load-from-disk-and-image.Decode() the same picture twice.
    --
    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
postedFeb 14, '13 at 9:43a
activeFeb 15, '13 at 6:56a
posts7
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase