Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2020 Neil Pankey <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,90 @@
Basic reading of [JPEG File Interchange Format (JFIF)][wiki-jfif] segments

[wiki-jfif]: https://en.wikipedia.org/wiki/JPEG_File_Interchange_Format#File_format_structure

## Install

Use `go get` to install the module

```
go get neilpa.me/go-jfif
```

Or use it and rely on go modules to "do the right thing".

## Usage

Reading JFIF segments from an existing JPEG.

```go
package main

import (
"fmt"
"log"
"os"

"neilpa.me/go-jfif"
)

func main() {
f, err := os.Open("path/to/file.jpg")
if err != nil {
log.Fatal(err)
}

// See also jfif.ScanSegments which doesn't read the segment payload.
// This is used to detect the segment "signatures" for some APPn segments.
segs, err := jfif.DecodeSegments(f)
if err != nil {
log.Fatal(err)
}

for _, s := range segs {
sig, _, _ := s.AppPayload()
sig = jfif.CleanSig(sig)
fmt.Printf("%d\t%s\t%s\n", s.Length, s.Marker, sig)
}
}
```

Appending new segments to a JPEG. (Under the hood this edits a copy of the file before renaming the copy
to finalize the updates).

```go
package main

import (
"log"

"neilpa.me/go-jfif"
)

func main() {
err := jfif.Add("path/to/file.jpg", jfif.COM, []byte("adding a comment to this file"))
if err != nil {
log.Fatal(err)
}
}
```

## Tools

There are a few simple CLI tools include for manipulating JPEG/JFIF files that exercise functionality
exposed by this module.

### jfifcom

Append free-form comment (`COM`) segments to an existing JPEG file.

### jfifstat

Prints segment markers, sizes, and optional `APPN` signatures from a JPEG until the image stream.

### xmpdump

Extracts and prints [XMP](https://www.adobe.com/products/xmp.html) data from `APP1` segments from JPEG file(s).

## License

[MIT](/LICENSE)
1 change: 1 addition & 0 deletions cmd/jfifcom/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jfifcom
63 changes: 63 additions & 0 deletions cmd/jfifcom/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// jfifcom embeds a new comment segment with the data from stdin
// before the start of stream (SOS) segment.
package main

import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"

"neilpa.me/go-jfif"
)

func main() {
os.Exit(realMain(os.Args[1:], os.Stdin))
}

func realMain(args []string, stdin io.Reader) int {
flag.Usage = printUsage
flag.CommandLine.Parse(args)

if flag.NArg() == 0 {
return usageError("no files specified")
}

buf, err := ioutil.ReadAll(stdin)
if err != nil {
return fatal(err.Error())
}

for _, arg := range flag.Args() {
//fmt.Println("embeddding", arg, "buf", buf)

err = jfif.Add(arg, jfif.COM, buf)
if err != nil {
return fatal(err.Error()) // todo: continue writing the other files?
}
}
return 0
}

func fatal(format string, args ...interface{}) int {
format = os.Args[0] + ": " + format + "\n"
fmt.Fprintf(os.Stderr, format, args...)
return 1
}

func usageError(msg string) int {
fmt.Fprintln(os.Stderr, msg)
printUsage()
return 2
}

func printUsage() {
fmt.Fprintf(os.Stderr, `Usage: %s jpeg [jpeg...] < ...

jfifcomment embeds a new comment segment with the data from stdin
before the start of stream (SOS) segment.
`, os.Args[0])
flag.PrintDefaults()
fmt.Fprintln(os.Stderr)
}
71 changes: 71 additions & 0 deletions cmd/jfifcom/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)

func TestMain(t *testing.T) {
root := filepath.Join("..", "..", "testdata")

tests := []struct {
in string
golden string
}{
{
"min.jpg",
"min.jfifcom.jpg",
},
}

for _, tt := range tests {
t.Run(tt.golden, func(t *testing.T) {
temp, err := ioutil.TempFile(os.TempDir(), "jfifcom-test-main-"+tt.in)
if err != nil {
t.Fatal(err)
}
path := temp.Name()
defer os.Remove(path)
defer temp.Close()

src, err := os.Open(filepath.Join(root, tt.in))
if err != nil {
t.Fatal(err)
}
defer src.Close()

_, err = io.Copy(temp, src)
if err != nil {
t.Fatal(err)
}
temp.Close()
src.Close()

exit := realMain([]string{path}, strings.NewReader("hello"))
if exit != 0 {
t.Fatalf("invalid exit %d", exit)
}

compareFiles(t, path, filepath.Join(root, tt.golden))
})
}
}

func compareFiles(t *testing.T, path, golden string) {
want, err := ioutil.ReadFile(golden)
if err != nil {
t.Fatal(err)
}
got, err := ioutil.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, want) {
t.Errorf("bytes don't match\ngot: % x\nwant: % x", got, want) // TODO Better diff
}
}
Binary file added docs/itu-t81.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module neilpa.me/go-jfif

go 1.14

require neilpa.me/go-x v0.2.0
require neilpa.me/go-x v0.2.1
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
neilpa.me/go-x v0.2.0 h1:GbLRDtAZ9MgVrwrIe3jWnHF2W40LCFA9Ng/aDbd9GVs=
neilpa.me/go-x v0.2.0/go.mod h1:aIemU+pQYLLV3dygXotHKF7SantXe5HzZR6VIjzY/4g=
neilpa.me/go-x v0.2.1 h1:9eb+b9Hj3iVIqw8F84Kq9GlAIjNsMH6V0A3AwkZuhA4=
neilpa.me/go-x v0.2.1/go.mod h1:aIemU+pQYLLV3dygXotHKF7SantXe5HzZR6VIjzY/4g=
Loading