pngembed

package module
v0.0.0-...-149afe9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2018 License: MIT Imports: 10 Imported by: 3

README

PNG Embed

Build Status Coverage Status Go Report Card

Embed key-value data into png images.

Install

go get github.com/sabhiram/png-embed

Usage

This library exposes two APIs:

Embed(data []byte, key string, value interface{}) ([]byte, error)
package main

import (
    "io/ioutil"
    pngembed "github.com/sabhiram/png-embed"
)

func main() {
    bs, _ := ioutil.ReadFile("sample.png")

    // Encode the key "FOO" with the value "BAR" (string).
    data, _ := pngembed.Embed(bs, "FOO", "BAR")
    ioutil.WriteFile("sample.png", data, 755)
}
EmbedFile(filePath, key string, value interface{}) ([]byte, error)

EmbedFile is exactly like Embed but it accepts a path to a file instead of the PNG file data.

package main

import (
    "io/ioutil"
    pngembed "github.com/sabhiram/png-embed"
)

func main() {
    // Encode the key "FOO" with the value "BAR" (string).
    data, _ := pngembed.EmbedFile("sample.png", "FOO", "BAR")
    ioutil.WriteFile("sample.png", data, 755)
}
Embedding JSON

Since the Embed method accepts an interface and assumes that the interface adheres to JSON encoding, we can pass arbitrary structs or map[interface{}]interface{}s to it.

package main

import (
    "io/ioutil"
    pngembed "github.com/sabhiram/png-embed"
)

func main() {
    s := struct {
        Foo string `json:"Foo"`
        Bar string `json:"Bar"`
    }{
        Foo: "FooValue",
        Bar: "BarValue",
    }

    data, _ := pngembed.EmbedFile("sample.png", "FOO", s)
    ioutil.WriteFile("sample.png", data, 755)
}
Extract(data []byte) (map[string][]byte, error)

TODO

Sample application

There is a sample application in the example directory which takes a source image and attempts to encode data into it. You can run this using go run example/main.go with the following arguments:

  -input string
        input file name for the png [required]
  -key string
        key name for the data to inject [default="TEST_KEY"]
  -value string
        value for the data to inject [default="TEST_VALUE"]
  -output string
        output file name for the png [default="out.png"]

To inject in.png with the key value pair "fruit": "apple" and generate out.png:

$ go run example/main.go -input in.png -key fruit -value apple -output out.png

You can then use something like pngcheck to verify that we did the right thing:

$ go run example/main.go -input ~/Desktop/test.png -key fruit -value apple -output out.png
$ pngcheck -t out.png
File: out.png (10785 bytes)
fruit:
    apple
OK: out.png (225x225, 8-bit palette, non-interlaced, 78.7%).

Documentation

Overview

Package pngembed embeds key-value data into a png image.

Index

Constants

View Source
const (
	Major = 0
	Minor = 1
	Patch = 1

	Version = "0.1.1"
)

WARNING: Auto generated version file. Do not edit this file by hand. WARNING: go get github.com/sabhiram/gover to manage this file. Version: 0.1.1

Variables

This section is empty.

Functions

func Embed

func Embed(data []byte, k string, v interface{}) ([]byte, error)

Embed processes a stream of raw PNG data, and encodes the specified key-value pair into a `tEXt` chunk. The resultant PNG byte-stream is returned, or an error. The interface `v` is serialized to known types and then to JSON if all else fails.

func EmbedFile

func EmbedFile(fp, k string, v interface{}) ([]byte, error)

EmbedFile is like `Embed` but accepts the path to a PNG file.

func Extract

func Extract(data []byte) (map[string][]byte, error)

Extract processes a stream of raw PNG data, and returns a map of `tEXt` records encoded by this library.

func ExtractFile

func ExtractFile(fp string) (map[string][]byte, error)

ExtractFile is like `Extract` but accepts the path to a PNG file.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL