Exemplo n.º 1
0
func webpSetMetadata(data, metadata []byte, format string) (newData []byte, err error) {
	if len(data) == 0 || len(metadata) == 0 {
		err = errors.New("webpSetMetadata: bad arguments")
		return
	}
	isCBuf := false
	cData := cgoSafePtr(data, isCBuf)
	defer cgoFreePtr(cData, isCBuf)
	cMetadata := cgoSafePtr(metadata, isCBuf)
	defer cgoFreePtr(cMetadata, isCBuf)

	switch format {
	case "EXIF":
		rv := C.cgoWebpSetEXIF(
			(*C.uint8_t)(cData), C.size_t(len(data)),
			(*C.char)(cMetadata), C.size_t(len(metadata)),
		)
		if rv.ok != 1 {
			err = errors.New("webpSetMetadata: failed")
			return
		}
		newData = C.GoBytes(unsafe.Pointer(rv.ptr), C.int(rv.size))
		C.webpFree(unsafe.Pointer(rv.ptr))
		return
	case "ICCP":
		rv := C.cgoWebpSetICCP(
			(*C.uint8_t)(cData), C.size_t(len(data)),
			(*C.char)(cMetadata), C.size_t(len(metadata)),
		)
		if rv.ok != 1 {
			err = errors.New("webpSetMetadata: failed")
			return
		}
		newData = C.GoBytes(unsafe.Pointer(rv.ptr), C.int(rv.size))
		C.webpFree(unsafe.Pointer(rv.ptr))
		return
	case "XMP":
		rv := C.cgoWebpSetXMP(
			(*C.uint8_t)(cData), C.size_t(len(data)),
			(*C.char)(cMetadata), C.size_t(len(metadata)),
		)
		if rv.ok != 1 {
			err = errors.New("webpSetMetadata: failed")
			return
		}
		newData = C.GoBytes(unsafe.Pointer(rv.ptr), C.int(rv.size))
		C.webpFree(unsafe.Pointer(rv.ptr))
		return
	default:
		err = errors.New("webpSetMetadata: unknown format")
		return
	}
}
Exemplo n.º 2
0
func (p *innerCBuffer) Close() error {
	if p.cptr != nil {
		C.webpFree(p.cptr)
	}
	*p = innerCBuffer{}

	// no need for a finalizer anymore
	runtime.SetFinalizer(p, nil)
	return nil
}
Exemplo n.º 3
0
func webpGetXMP(data []byte) (metadata []byte, err error) {
	if len(data) == 0 {
		err = errors.New("webpGetXMP: bad arguments")
		return
	}
	isCBuf := false
	cData := cgoSafePtr(data, isCBuf)
	defer cgoFreePtr(cData, isCBuf)

	rv := C.cgoWebpGetXMP((*C.uint8_t)(cData), C.size_t(len(data)))
	if rv.ok != 1 {
		err = errors.New("webpGetXMP: failed")
		return
	}
	metadata = C.GoBytes(unsafe.Pointer(rv.ptr), C.int(rv.size))
	C.webpFree(unsafe.Pointer(rv.ptr))
	return
}
Exemplo n.º 4
0
func (p *innerCBuffer) Resize(size int) error {
	if size < 0 {
		return errors.New("webp: cBuffer.Resize, bad size!")
	}
	if p.dontResize {
		return errors.New("webp: cBuffer.Resize, donot resize!")
	}
	if n := len(p.data); n > 0 && n != size {
		C.webpFree(p.cptr)
		p.cptr = nil
		p.data = nil
	}
	p.Close()
	if size > 0 {
		p.cptr = C.webpMalloc(C.size_t(size))
		p.data = (*[1 << 30]byte)(p.cptr)[0:size:size]
	}
	return nil
}
Exemplo n.º 5
0
func webpSetEXIF(data, metadata []byte) (newData []byte, err error) {
	if len(data) == 0 || len(metadata) == 0 {
		err = errors.New("webpSetEXIF: bad arguments")
		return
	}
	isCBuf := false
	cData := cgoSafePtr(data, isCBuf)
	defer cgoFreePtr(cData, isCBuf)
	cMetadata := cgoSafePtr(metadata, isCBuf)
	defer cgoFreePtr(cMetadata, isCBuf)

	rv := C.cgoWebpSetEXIF(
		(*C.uint8_t)(cData), C.size_t(len(data)),
		(*C.char)(cMetadata), C.size_t(len(metadata)),
	)
	if rv.ok != 1 {
		err = errors.New("webpSetEXIF: failed")
		return
	}
	newData = C.GoBytes(unsafe.Pointer(rv.ptr), C.int(rv.size))
	C.webpFree(unsafe.Pointer(rv.ptr))
	return
}