Skip to content

Instantly share code, notes, and snippets.

@scottleedavis
Created September 7, 2019 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottleedavis/25f5e7d1b97dcb8964b74712cfbc6a29 to your computer and use it in GitHub Desktop.
Save scottleedavis/25f5e7d1b97dcb8964b74712cfbc6a29 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"bytes"
"fmt"
"gopkg.in/auyer/steganography.v2"
"image"
"os"
)
func main() {
inFile, err := os.Open("./assets/pre_test.png") // opening file
if err != nil {
fmt.Println("unable to open file")
}
defer inFile.Close()
reader := bufio.NewReader(inFile) // buffer reader
img, _, err := image.Decode(reader)
if err != nil {
fmt.Println("ERROR: original image is corrupt " + err.Error())
return
}
w := &bytes.Buffer{}
err = steganography.Encode(w, img, []byte("this is a watermark"))
if err != nil {
fmt.Println(err.Error())
}
reader = bufio.NewReader(w)
img, _, err = image.Decode(reader)
if err != nil {
fmt.Println(err.Error())
}
sizeOfMessage := steganography.GetMessageSizeFromImage(img)
msg := steganography.Decode(sizeOfMessage, img)
fmt.Println("**** " + string(msg))
}
@auyer
Copy link

auyer commented Sep 7, 2019

Ran this with both images in your assets folder and it worked fine.

go run t.go 
**** this is a watermark

In the case of the .jpg image, it will work if:

  • 1
    Using the "image/jpeg" module to decode img, err := jpeg.Decode(reader)
  • 2
    Usage of a init import paired with the default decoding:
    _ "image/jpeg"
    ...
    img, _, err := image.Decode(reader)

@scottleedavis
Copy link
Author

@auyer I tested with test.png and it worked, however, I have uploaded pre_test.png (a png without the watermark already) and it doesn't. Does using pre_test.png correctly work on your system?

@scottleedavis
Copy link
Author

perhaps something is corrupted in the pre_test.png image?

@scottleedavis
Copy link
Author

scottleedavis commented Sep 7, 2019

I will try out the jpg approach now. 👍

@scottleedavis
Copy link
Author

jpg test with decode.go, with above approach has this output currently.

$ go run decode.go ./assets/test.jpg 
panic: runtime error: slice bounds out of range

goroutine 1 [running]:
gopkg.in/auyer/steganography%2ev2.decodeRGBA(0xffffffff00000004, 0xc0000b6080, 0xc0000b6080, 0x4, 0x8)
        /Users/scottd/go/pkg/mod/gopkg.in/auyer/steganography.v2@v2.0.0-20190427051610-2664780d40a3/steganography.go:180 +0x5a8
gopkg.in/auyer/steganography%2ev2.decode(0xffffffff00000004, 0x1113040, 0xc00014e080, 0xc0000aa000, 0x1113040, 0xc00014e080)
        /Users/scottd/go/pkg/mod/gopkg.in/auyer/steganography.v2@v2.0.0-20190427051610-2664780d40a3/steganography.go:203 +0x53
gopkg.in/auyer/steganography%2ev2.Decode(...)
        /Users/scottd/go/pkg/mod/gopkg.in/auyer/steganography.v2@v2.0.0-20190427051610-2664780d40a3/steganography.go:217
main.main()
        /Users/scottd/workspace/mattermost-plugin-watermark/decode.go:44 +0x296
exit status 2

@scottleedavis
Copy link
Author

it must be the image (for the png issue)... it is working with a new png.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment