Skip to content

raducrisan1/goheif

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoHeif - A go gettable decoder/converter for HEIC based on libde265

Intel and ARM supported

Install

go get github.com/raducrisan1/goheif

  • Code Sample
func main() {
	flag.Parse()
	...
  
	fin, fout := flag.Arg(0), flag.Arg(1)
	fi, err := os.Open(fin)
	if err != nil {
		log.Fatal(err)
	}
	defer fi.Close()

	exif, err := goheif.ExtractExif(fi)
	if err != nil {
		log.Printf("Warning: no EXIF from %s: %v\n", fin, err)
	}

	img, err := goheif.DecodeImage(fi)
	if err != nil {
		log.Fatalf("Failed to parse %s: %v\n", fin, err)
	}

	fo, err := os.OpenFile(fout, os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		log.Fatalf("Failed to create output file %s: %v\n", fout, err)
	}
	defer fo.Close()

	w, _ := newWriterExif(fo, exif)
	err = jpeg.Encode(w, img, nil)
	if err != nil {
		log.Fatalf("Failed to encode %s: %v\n", fout, err)
	}

	log.Printf("Convert %s to %s successfully\n", fin, fout)
}

What is done

  • Changes make to @bradfitz's (https://github.com/bradfitz) golang heif parser

    • Some minor bugfixes
    • A few new box parsers, noteably 'iref' and 'hvcC'
  • Include libde265's source code (SSE by default enabled) and a simple golang binding

  • A Utility heic2jpg to illustrate the usage.

Security notes

This decoder parses untrusted-by-default container data and runs the bundled libde265 C++ decoder. The Go layer enforces hard limits to bound resource use and prevent integer-overflow / memory-aliasing classes of bugs:

  • The total input stream is capped at 200 MiB (maxInputBytes).
  • Grid output dimensions and tile counts are capped (maxGridDim, maxGridTiles); 32-bit grid dims are parsed via binary.BigEndian.Uint32 to avoid signed-shift behavior on 32-bit builds.
  • libde265 pixel buffers are always copied via C.GoBytes before returning; no Go slice aliases C-owned memory.
  • BMFF parser allocations are bounded per box body (maxBoxBodyBytes, maxHevcNalBytesPerBox, maxIrefCount, etc.). See heif/bmff/bmff.go.

For hardened deployments that parse attacker-controlled HEIC input, run the decoder in a sandboxed process. The library has no non-stdlib runtime dependencies; consumers needing full EXIF parsing should pass the bytes returned by ExtractExif to the EXIF library of their choice.

License

  • heif and libde265 are in their own licenses

  • goheif.go, libde265 golang binding and the heic2jpg utility are in MIT license

Credits

TODO

  • Upstream the changes to heif?

About

go gettable decoder/converter for HEIF/HEIC based on libde265

Resources

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages

  • Go 100.0%