| 1 |
/* |
| 2 |
Simple GO interface test program |
| 3 |
|
| 4 |
MIT License |
| 5 |
|
| 6 |
Copyright (c) 2018 Dirk Farin <[email protected]> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
of this software and associated documentation files (the "Software"), to deal |
| 10 |
in the Software without restriction, including without limitation the rights |
| 11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
copies of the Software, and to permit persons to whom the Software is |
| 13 |
furnished to do so, subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included in all |
| 16 |
copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 |
SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
package main |
| 28 |
|
| 29 |
import ( |
| 30 |
"bytes" |
| 31 |
"fmt" |
| 32 |
"image" |
| 33 |
_ "image/jpeg" |
| 34 |
"image/png" |
| 35 |
"io/ioutil" |
| 36 |
"os" |
| 37 |
"path/filepath" |
| 38 |
|
| 39 |
"github.com/strukturag/libheif/go/heif" |
| 40 |
) |
| 41 |
|
| 42 |
// ================================================== |
| 43 |
// TEST |
| 44 |
// ================================================== |
| 45 |
|
| 46 |
func savePNG(img image.Image, filename string) { |
| 47 |
var out bytes.Buffer |
| 48 |
if err := png.Encode(&out, img); err != nil { |
| 49 |
fmt.Printf("Could not encode image as PNG: %s\n", err) |
| 50 |
} else { |
| 51 |
if err := ioutil.WriteFile(filename, out.Bytes(), 0644); err != nil { |
| 52 |
fmt.Printf("Could not save PNG image as %s: %s\n", filename, err) |
| 53 |
} else { |
| 54 |
fmt.Printf("Written to %s\n", filename) |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func testHeifHighlevel(filename string) { |
| 60 |
fmt.Printf("Performing highlevel conversion of %s\n", filename) |
| 61 |
file, err := os.Open(filename) |
| 62 |
if err != nil { |
| 63 |
fmt.Printf("Could not read file %s: %s\n", filename, err) |
| 64 |
return |
| 65 |
} |
| 66 |
defer file.Close() |
| 67 |
|
| 68 |
img, magic, err := image.Decode(file) |
| 69 |
if err != nil { |
| 70 |
fmt.Printf("Could not decode image: %s\n", err) |
| 71 |
return |
| 72 |
} |
| 73 |
|
| 74 |
fmt.Printf("Decoded image of type %s: %s\n", magic, img.Bounds()) |
| 75 |
|
| 76 |
ext := filepath.Ext(filename) |
| 77 |
outFilename := filename[0:len(filename)-len(ext)] + "_highlevel.png" |
| 78 |
savePNG(img, outFilename) |
| 79 |
} |
| 80 |
|
| 81 |
func testHeifLowlevel(filename string) { |
| 82 |
fmt.Printf("Performing lowlevel conversion of %s\n", filename) |
| 83 |
c, err := heif.NewContext() |
| 84 |
if err != nil { |
| 85 |
fmt.Printf("Could not create context: %s\n", err) |
| 86 |
return |
| 87 |
} |
| 88 |
|
| 89 |
if err := c.ReadFromFile(filename); err != nil { |
| 90 |
fmt.Printf("Could not read file %s: %s\n", filename, err) |
| 91 |
return |
| 92 |
} |
| 93 |
|
| 94 |
nImages := c.GetNumberOfTopLevelImages() |
| 95 |
fmt.Printf("Number of top level images: %v\n", nImages) |
| 96 |
|
| 97 |
ids := c.GetListOfTopLevelImageIDs() |
| 98 |
fmt.Printf("List of top level image IDs: %#v\n", ids) |
| 99 |
|
| 100 |
if pID, err := c.GetPrimaryImageID(); err != nil { |
| 101 |
fmt.Printf("Could not get primary image id: %s\n", err) |
| 102 |
} else { |
| 103 |
fmt.Printf("Primary image: %v\n", pID) |
| 104 |
} |
| 105 |
|
| 106 |
handle, err := c.GetPrimaryImageHandle() |
| 107 |
if err != nil { |
| 108 |
fmt.Printf("Could not get primary image: %s\n", err) |
| 109 |
return |
| 110 |
} |
| 111 |
|
| 112 |
fmt.Printf("Image size: %v × %v\n", handle.GetWidth(), handle.GetHeight()) |
| 113 |
|
| 114 |
img, err := handle.DecodeImage(heif.ColorspaceUndefined, heif.ChromaUndefined, nil) |
| 115 |
if err != nil { |
| 116 |
fmt.Printf("Could not decode image: %s\n", err) |
| 117 |
} else if i, err := img.GetImage(); err != nil { |
| 118 |
fmt.Printf("Could not get image: %s\n", err) |
| 119 |
} else { |
| 120 |
fmt.Printf("Rectangle: %v\n", i.Bounds()) |
| 121 |
|
| 122 |
ext := filepath.Ext(filename) |
| 123 |
outFilename := filename[0:len(filename)-len(ext)] + "_lowlevel.png" |
| 124 |
savePNG(i, outFilename) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
func testHeifEncode(filename string) { |
| 129 |
file, err := os.Open(filename) |
| 130 |
if err != nil { |
| 131 |
fmt.Printf("failed to open file %v\n", file) |
| 132 |
return |
| 133 |
} |
| 134 |
defer file.Close() |
| 135 |
|
| 136 |
i, _, err := image.Decode(file) |
| 137 |
if err != nil { |
| 138 |
fmt.Printf("failed to decode image: %v\n", err) |
| 139 |
return |
| 140 |
} |
| 141 |
|
| 142 |
const quality = 100 |
| 143 |
ctx, err := heif.EncodeFromImage(i, heif.CompressionHEVC, quality, heif.LosslessModeEnabled, heif.LoggingLevelFull) |
| 144 |
if err != nil { |
| 145 |
fmt.Printf("failed to heif encode image: %v\n", err) |
| 146 |
return |
| 147 |
} |
| 148 |
|
| 149 |
ext := filepath.Ext(filename) |
| 150 |
out := filename[0:len(filename)-len(ext)] + "_encoded.heif" |
| 151 |
if err := ctx.WriteToFile(out); err != nil { |
| 152 |
fmt.Printf("failed to write to file: %v\n", err) |
| 153 |
return |
| 154 |
} |
| 155 |
fmt.Printf("Written to %s\n", out) |
| 156 |
} |
| 157 |
|
| 158 |
func main() { |
| 159 |
fmt.Printf("libheif version: %v\n", heif.GetVersion()) |
| 160 |
if len(os.Args) < 2 { |
| 161 |
fmt.Printf("USAGE: %s <filename>\n", os.Args[0]) |
| 162 |
return |
| 163 |
} |
| 164 |
|
| 165 |
filename := os.Args[1] |
| 166 |
testHeifLowlevel(filename) |
| 167 |
fmt.Println() |
| 168 |
testHeifHighlevel(filename) |
| 169 |
fmt.Println() |
| 170 |
testHeifEncode(filename) |
| 171 |
fmt.Println("Done.") |
| 172 |
} |