Skip to content

Instantly share code, notes, and snippets.

@tbonfort
Created July 13, 2021 13:22
Show Gist options
  • Save tbonfort/1e8630cf4b9bc82471c164e422c1cce7 to your computer and use it in GitHub Desktop.
Save tbonfort/1e8630cf4b9bc82471c164e422c1cce7 to your computer and use it in GitHub Desktop.
cogger c wrapper
package main
import "C"
import (
"fmt"
"os"
"unsafe"
"github.com/airbusgeo/cogger"
"github.com/google/tiff"
)
//export cogify
func cogify(out *C.char, in **C.char, nin C.int) C.int {
infiles := (*[1 << 28]unsafe.Pointer)(unsafe.Pointer(in))[:nin:nin]
readers := make([]tiff.ReadAtReadSeeker, len(infiles))
output := C.GoString(out)
for i, cinput := range infiles {
input := C.GoString((*C.char)(cinput))
topFile, err := os.Open(input)
if err != nil {
fmt.Fprintf(os.Stderr, "open %s: %v", input, err)
return 1
}
defer topFile.Close()
readers[i] = topFile
}
outf, err := os.Create(output)
if err != nil {
fmt.Fprintf(os.Stderr, "create %s: %v", output, err)
return 1
}
err = cogger.Rewrite(outf, readers...)
if err != nil {
fmt.Fprintf(os.Stderr, "mucog write: %v", err)
return 1
}
err = outf.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "close %s: %v", output, err)
return 1
}
return 0
}
func main() {}
extern int cogify(char* out, char** in, int nin);
int main(int argc, char **argv) {
char *infiles[] = {
"../testdata/exttest.tif",
"../testdata/exttest.tif.2",
};
cogify("output.tif", infiles,2);
}
build:
go build -o cogify.so -buildmode=c-shared .
gcc -o main main.c cogify.so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment