From 3686cdda925dcd401934840fdcbc972cf5e7db1e Mon Sep 17 00:00:00 2001 From: Neil Pankey Date: Thu, 7 May 2020 18:30:14 -0700 Subject: [PATCH] readme: Usage notes --- README.md | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d96617e..666dc00 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,71 @@ 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 + +Simply use `go get` to install the module + +``` +go get neilpa.me/go-jfif +``` + +Or just use it and rely on go modules to "do the right thing". + ## Usage -TODO +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