Skip to content

Commit

Permalink
Docs updated
Browse files Browse the repository at this point in the history
  • Loading branch information
MaestroError committed Jun 5, 2023
1 parent 8add14c commit e36e44e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 62 deletions.
48 changes: 31 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# go-libheif
GoLang wrapper for the libheif library, providing easy-to-use APIs for HEIC to JPEG/PNG conversions and vice versa. (Also provides support for AVIF to JPEG/PNG conversions)
This package was developed to support
GoLang wrapper for the libheif library, providing easy-to-use APIs for HEIC to JPEG/PNG conversions and vice versa (Also provides support for AVIF to JPEG/PNG conversions).
This package was originally developed to support the [php-heic-to-jpg](https://github.com/MaestroError/php-heic-to-jpg) package, which had [problems](https://github.com/MaestroError/php-heic-to-jpg/issues/15) while converting some HEIF images.

### Pre-requisites
#### Implementation:
- For a swift start, consider browsing our [quickstart guide](https://medium.com/@revaz.gh/using-the-go-libheif-module-for-converting-images-between-the-heic-format-and-other-popular-formats-e7829165c368) on Medium.
- If you're primarily interested in a hassle-free image conversion tool, we have already integrated this module into a Command Line Interface (CLI) application and a docker image, available [here](https://github.com/MaestroError/heif-converter-image).

### Prerequisites
You need to install [libheif](https://github.com/strukturag/libheif) before using this module. You can check the [strukturag/libheif](https://github.com/strukturag/libheif) for installation instructions, but as I have found, the easiest way for me was to use [brew](https://brew.sh/):
```bash
brew install cmake make pkg-config x265 libde265 libjpeg libtool
Expand All @@ -15,6 +19,8 @@ You can find installation scripts in [heif-converter-image](https://github.com/M
- install-libheif-macos.sh
- install-libheif-windows.bat

*If the installation process seems a bit challenging, we recommend to consider using the [heif-converter-image](https://github.com/MaestroError/heif-converter-image) which offers a ready-to-use docker image.*

### Installation
```bash
go get github.com/MaestroError/go-libheif
Expand All @@ -34,12 +40,12 @@ import (
)

func main() {
err := go_libheif.HeifToJpeg("input.heic", "output.jpeg", 80)
err := libheif.HeifToJpeg("input.heic", "output.jpeg", 80)
if err != nil {
log.Fatal(err)
}

err = go_libheif.HeifToPng("input.heic", "output.png")
err = libheif.HeifToPng("input.heic", "output.png")
if err != nil {
log.Fatal(err)
}
Expand All @@ -58,7 +64,7 @@ import (
)

func main() {
err := go_libheif.ImageToHeif("input.jpeg", "output.heic")
err := libheif.ImageToHeif("input.jpeg", "output.heic")
if err != nil {
log.Fatal(err)
}
Expand All @@ -80,7 +86,7 @@ import (

func main() {
img := image.NewRGBA(image.Rect(0, 0, 100, 100))
err := go_libheif.SaveImageAsHeif(img, "png", "output.heic")
err := libheif.SaveImageAsHeif(img, "png", "output.heic")
if err != nil {
log.Fatal(err)
}
Expand All @@ -91,16 +97,24 @@ func main() {

Please consult the GoDoc [documentation](https://pkg.go.dev/github.com/MaestroError/go-libheif) for more detailed information about the provided functions and their usage.

### Contributions
Contributions to `go-libheif` are wholeheartedly encouraged! We believe in the power of the open source community to build the most robust and accessible projects. Whether you are a seasoned Go developer or just getting started, your perspective and efforts can help improve this project for everyone.

### Credits
Thanks to @strukturag and @farindk (Dirk Farin) for his work on the [libheif](https://github.com/strukturag/libheif) library 🙏
Here are a few ways you might contribute:

- Bug Fixes: If you encounter a problem with go-libheif, please open an issue in the GitHub repository. Even better, if you can solve the issue, we welcome pull requests.
- New Features: Interested in expanding the capabilities of go-libheif? Please open an issue to discuss your idea before starting on the code. Once we've agreed on an approach, you can submit a pull request with your new feature.
- Documentation: Clear documentation is vital to any project. If you can clarify our instructions or add helpful examples, we would appreciate your input.

### Implementation
If you are looking just for the easy way to convert images, this module is already implemented [here](https://github.com/MaestroError/heif-converter-image) as CLI app and docker image
Here are the steps for contributing code:

##### ToDo
- Write usage documentation +
- Write guide article for module
- Add contribution section in readme
- Implement the module in [php-heic-to-jpg](https://github.com/MaestroError/php-heic-to-jpg) +
- Add credits section in readme and update in php-heic-to-jpg +
- Fork the go-libheif repository and clone it to your local machine.
- Create a new branch for your changes.
- Make your changes and push them to your fork.
- Open a pull request from your fork's branch to the go-libheif main branch.

Please remember to be as detailed as possible in your pull request descriptions to help the maintainers understand your changes.
We look forward to seeing what you contribute! Together, we can make go-libheif even better.

### Credits
Thanks to @strukturag and @farindk (Dirk Farin) for his work on the [libheif](https://github.com/strukturag/libheif) library 🙏
99 changes: 54 additions & 45 deletions libheif.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import (
// The quality should be an integer between 1 and 100, inclusive, higher is better.
//
// Example:
// img := image.NewRGBA(image.Rect(0, 0, 100, 100))
// filename := "output.jpg"
// quality := 80
// if err := saveAsJpeg(img, filename, quality); err != nil {
// log.Fatal(err)
// }
//
// img := image.NewRGBA(image.Rect(0, 0, 100, 100))
// filename := "output.jpg"
// quality := 80
// if err := saveAsJpeg(img, filename, quality); err != nil {
// log.Fatal(err)
// }
func saveAsJpeg(img image.Image, filename string, quality int) error {
// Validate input
if img == nil {
Expand Down Expand Up @@ -54,11 +55,12 @@ func saveAsJpeg(img image.Image, filename string, quality int) error {
// saveAsPng encodes an image to PNG format and saves it to a file.
//
// Example:
// img := image.NewRGBA(image.Rect(0, 0, 100, 100))
// filename := "output.png"
// if err := saveAsPng(img, filename); err != nil {
// log.Fatal(err)
// }
//
// img := image.NewRGBA(image.Rect(0, 0, 100, 100))
// filename := "output.png"
// if err := saveAsPng(img, filename); err != nil {
// log.Fatal(err)
// }
func saveAsPng(img image.Image, filename string) error {
// Validate input
if img == nil {
Expand Down Expand Up @@ -90,12 +92,13 @@ func saveAsPng(img image.Image, filename string) error {
// If an error occurs during opening the file or decoding the image, it is returned.
//
// Example:
// filename := "image.heic"
// img, format, err := decodeHeifImage(filename)
// if err != nil {
// log.Fatal(err)
// }
// // img is now an image.Image object and format is the format of the image.
//
// filename := "image.heic"
// img, format, err := decodeHeifImage(filename)
// if err != nil {
// log.Fatal(err)
// }
// // img is now an image.Image object and format is the format of the image.
func decodeHeifImage(filename string) (image.Image, string, error) {
// Open the file
file, err := os.Open(filename)
Expand Down Expand Up @@ -125,11 +128,12 @@ func decodeHeifImage(filename string) (image.Image, string, error) {
// to the caller.
//
// Example:
// srcFile := "input.jpg"
// dstFile := "output.heif"
// if err := convertFileToHeif(srcFile, dstFile); err != nil {
// log.Fatal(err)
// }
//
// srcFile := "input.jpg"
// dstFile := "output.heif"
// if err := convertFileToHeif(srcFile, dstFile); err != nil {
// log.Fatal(err)
// }
func convertFileToHeif(filename string, newFileName string) error {
// Validate input
if filename == "" || newFileName == "" {
Expand Down Expand Up @@ -163,11 +167,12 @@ func convertFileToHeif(filename string, newFileName string) error {
// decoded or the decoded image is not in HEIF format, an error is returned.
//
// Example:
// filename := "image.heic"
// img, err := returnImageFromHeif(filename)
// if err != nil {
// log.Fatal(err)
// }
//
// filename := "image.heic"
// img, err := returnImageFromHeif(filename)
// if err != nil {
// log.Fatal(err)
// }
func returnImageFromHeif(filename string) (image.Image, error) {
// Validate input
if filename == "" {
Expand Down Expand Up @@ -195,10 +200,11 @@ func returnImageFromHeif(filename string) (image.Image, error) {
// the output PNG image file should be saved.
//
// Example:
// err := HeifToPng("input.heic", "output.png")
// if err != nil {
// log.Fatal(err)
// }
//
// err := HeifToPng("input.heic", "output.png")
// if err != nil {
// log.Fatal(err)
// }
func HeifToPng(heifImagePath string, newPngImagePath string) error {
// Convert the HEIF image to the internal image representation.
img, err := returnImageFromHeif(heifImagePath)
Expand All @@ -216,10 +222,11 @@ func HeifToPng(heifImagePath string, newPngImagePath string) error {
// the output JPEG image file should be saved, and the quality of the output JPEG image.
//
// Example:
// err := HeifToJpeg("input.heic", "output.jpeg", 80)
// if err != nil {
// log.Fatal(err)
// }
//
// err := HeifToJpeg("input.heic", "output.jpeg", 80)
// if err != nil {
// log.Fatal(err)
// }
func HeifToJpeg(heifImagePath string, newJpegImagePath string, quality int) error {
// Convert the HEIF image to the internal image representation.
img, err := returnImageFromHeif(heifImagePath)
Expand All @@ -237,10 +244,11 @@ func HeifToJpeg(heifImagePath string, newJpegImagePath string, quality int) erro
// the output HEIF image file should be saved.
//
// Example:
// err := ImageToHeif("input.jpeg", "output.heic")
// if err != nil {
// log.Fatal(err)
// }
//
// err := ImageToHeif("input.jpeg", "output.heic")
// if err != nil {
// log.Fatal(err)
// }
func ImageToHeif(jpegOrPngImagePath string, newHeifImagePath string) error {
// Convert the image to HEIF format and return the result.
return convertFileToHeif(jpegOrPngImagePath, newHeifImagePath)
Expand All @@ -252,12 +260,13 @@ func ImageToHeif(jpegOrPngImagePath string, newHeifImagePath string) error {
// original image format is printed to the console.
//
// Example:
// img := image.NewRGBA(image.Rect(0, 0, 100, 100))
// format := "png"
// newHeifImagePath := "output.heif"
// if err := SaveImageAsHeif(img, format, newHeifImagePath); err != nil {
// log.Fatal(err)
// }
//
// img := image.NewRGBA(image.Rect(0, 0, 100, 100))
// format := "png"
// newHeifImagePath := "output.heif"
// if err := SaveImageAsHeif(img, format, newHeifImagePath); err != nil {
// log.Fatal(err)
// }
func SaveImageAsHeif(i image.Image, format string, newHeifImagePath string) error {
// Validate input
if i == nil {
Expand Down

0 comments on commit e36e44e

Please sign in to comment.