func (img *Image) Resize(width int, height int) { wand := img.wand cur_width := float64(C.MagickGetImageWidth(wand)) cur_height := float64(C.MagickGetImageHeight(wand)) r_width := cur_width / float64(width) r_height := cur_height / float64(height) ratio := r_height if r_width < r_height { ratio = r_width } dest_width := cur_width / ratio dest_height := cur_height / ratio crop_x := int((dest_width - float64(width)) / 2) crop_y := int((dest_height - float64(height)) / 2) if r_width > 5 && r_height > 5 { C.MagickSampleImage(wand, C.size_t(dest_width*5), C.size_t(dest_height*5)) } C.MagickResizeImage(wand, C.size_t(dest_width), C.size_t(dest_height), C.LanczosFilter, 1) C.MagickCropImage(wand, C.size_t(width), C.size_t(height), C.ssize_t(crop_x), C.ssize_t(crop_y)) }
/* Extracts a region of the image. */ func (w *MagickWand) CropImage(width, height uint, x, y int) error { if C.MagickCropImage(w.wand, C.size_t(width), C.size_t(height), C.ssize_t(x), C.ssize_t(y)) == C.MagickFalse { eStr, eCode := w.Exception() return fmt.Errorf("CropImage() failed : [%d] %s", eStr, eCode) } return nil }
func (im *Image) setPixels(r *Rect, src *C.PixelPacket, ex *C.ExceptionInfo) bool { dst := C.GetAuthenticPixels(im.image, C.ssize_t(r.X), C.ssize_t(r.Y), C.size_t(r.Width), C.size_t(r.Height), ex) if dst == nil { return false } C.copy_pixel_packets(src, dst, C.int(r.Width*r.Height)) if C.SyncAuthenticPixels(im.image, ex) != C.MagickTrue { return false } return true }
// Private: center the current wand on top of the new wand, after that, we only // keep the new wand. // // Params: // - newWand: new wand, probably going to be result of blankWand // - x: x position on the current wand that is the center of the new wand // - y: y position on the current wand that is the center of the new wand // // Example: // newWand := blankWand("jpg", width, height) // done := m.compositeCenter(newWand, 10, 10) // // Return boolean result of the composition func (m *Mage) compositeCenter(newWand *C.MagickWand, x, y int) bool { success := C.MagickCompositeImage( newWand, m.wand, C.OverCompositeOp, C.ssize_t(x), C.ssize_t(y)) C.DestroyMagickWand(m.wand) m.wand = newWand return mBoolean(success) }
func (br *BodyReader) Read(p []byte) (n int, err error) { m := len(p) var rlen C.ssize_t = C.ssize_t(0) c_body := C.uwsgi_request_body_read(br.wsgi_req, C.ssize_t(m), &rlen) if c_body == C.uwsgi.empty { err = io.EOF return 0, err } else if c_body != nil { C.memcpy(unsafe.Pointer(&p[0]), unsafe.Pointer(c_body), C.size_t(rlen)) return int(rlen), err } err = io.ErrUnexpectedEOF rlen = 0 return int(rlen), err }
func (e *Enchant) Replace(word, wordNew string) { // does the dictionary need to be loaded here? cWord := C.CString(word) defer C.free(unsafe.Pointer(cWord)) cWordNew := C.CString(wordNew) defer C.free(unsafe.Pointer(cWordNew)) cWordLen := uintptr(len(word)) s := C.ssize_t(cWordLen) cWordLenNew := uintptr(len(wordNew)) sNew := C.ssize_t(cWordLenNew) C.enchant_dict_store_replacement(e.dict, cWord, s, cWordNew, sNew) }
// Puts a canvas on top of the current one. func (cv Canvas) AppendCanvas(source Canvas, x int, y int) bool { status := C.MagickCompositeImage(cv.wand, source.wand, C.OverCompositeOp, C.ssize_t(x), C.ssize_t(y)) if status == C.MagickFalse { return false } return true }
// Extracts a region from the canvas. func (cv Canvas) Crop(x int, y int, width uint, height uint) bool { status := C.MagickCropImage(cv.wand, C.size_t(width), C.size_t(height), C.ssize_t(x), C.ssize_t(y)) if status == C.MagickFalse { return false } return true }
/* Composite one image onto another at the specified offset. */ func (w *MagickWand) CompositeImage(srcWand *MagickWand, compose, x, y int) error { if C.MagickCompositeImage(w.wand, srcWand.wand, C.CompositeOperator(compose), C.ssize_t(x), C.ssize_t(y)) == C.MagickFalse { eStr, eCode := w.Exception() return fmt.Errorf("CompositeImage() failed : [%d] %s", eStr, eCode) } return nil }
// Extracts a region from the canvas. func (self *Canvas) Crop(x int, y int, width uint, height uint) error { success := C.MagickCropImage(self.wand, C.size_t(width), C.size_t(height), C.ssize_t(x), C.ssize_t(y)) if success == C.MagickFalse { return fmt.Errorf("Could not crop: %s", self.Error()) } return nil }
func (acl *ACL) CopyExt(buffer []byte) (int64, error) { p := unsafe.Pointer(&buffer[0]) l := C.ssize_t(len(buffer)) i, err := C.acl_copy_ext(p, acl.a, l) if i < 0 { return int64(i), err } return int64(i), nil }
// Puts a canvas on top of the current one. func (self *Canvas) AppendCanvas(source *Canvas, x int, y int) error { success := C.MagickCompositeImage(self.wand, source.wand, C.OverCompositeOp, C.ssize_t(x), C.ssize_t(y)) if success == C.MagickFalse { return fmt.Errorf("Could not append image: %s", self.Error()) } return nil }
// Check whether a given word is in the currently loaded dictionary. // This wraps enchant_dict_check. // It returns a boolean value: true if the word is in the dictionary, // false otherwise. func (d Dict) Check(word string) bool { if len(word) == 0 { return true } cWord := (*C.char)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&word)).Data)) size := C.ssize_t(uintptr(len(word))) return C.enchant_dict_check(d.dict, cWord, size) == 0 }
// Write writes len(b) bytes from b into the Fd // // Returns number of bytes written on success and error on failure func (fd *Fd) Write(b []byte) (int, error) { n, err := C.glfs_write(fd.fd, unsafe.Pointer(&b[0]), C.size_t(len(b)), 0) if n == C.ssize_t(len(b)) { // FIXME: errno is set to EINVAL even though write is successful. This // is probably a bug. Remove this workaround when that gets fixed. err = nil } return int(n), err }
// write to a file descriptor func fdWrite(fd C.int, d []byte) (n int, err error) { buff := make([]C.uchar, len(d)) for i, b := range d { buff[i] = C.uchar(b) } res := C.uchar_write(fd, &buff[0], C.size_t(len(d))) if res == C.ssize_t(-1) { err = errors.New("write returned -1") } else { n = int(res) } return }
//export gogpgme_writefunc func gogpgme_writefunc(handle, buffer unsafe.Pointer, size C.size_t) C.ssize_t { d := callbackLookup(uintptr(handle)).(*Data) if len(d.buf) < int(size) { d.buf = make([]byte, size) } C.memcpy(unsafe.Pointer(&d.buf[0]), buffer, C.size_t(size)) n, err := d.w.Write(d.buf[:size]) if err != nil && err != io.EOF { C.gpgme_err_set_errno(C.EIO) return -1 } return C.ssize_t(n) }
// read from a file descriptor func fdRead(fd C.int, d []byte) (n int, err error) { buff := make([]C.uchar, len(d)) res := C.uchar_read(fd, &buff[0], C.size_t(len(d))) if res == C.ssize_t(-1) { err = errors.New("read returned -1") } else { n = int(res) for i, b := range buff[:n] { d[i] = byte(b) } } return }
func readdelattr(filepath string) (bool, error) { val := C.char(0) err := C.getxattr( C.CString(filepath), C.CString("novm-deleted"), unsafe.Pointer(&val), C.size_t(1)) if err != C.ssize_t(1) { var stat syscall.Stat_t err := syscall.Stat( path.Join(filepath, ".deleted"), &stat) if err != nil { return false, err } return true, nil } return val == C.char(1), nil }
// Takes a serialized OSC packet and deserializes it into a // message. Parameters with an unsupported type will // be deserialized as ErrUnknown. func Deserialize(data []byte) (*Message, error) { cdata := unsafe.Pointer(&data[0]) cdatalen := C.size_t(len(data)) msg := C.lo_message_deserialise(cdata, cdatalen, (*C.int)(unsafe.Pointer(nil))) if msg == nil { // TODO: Obtain and parse actual error? return nil, errors.New("Deserialization failed") } result := &Message{ Path: C.GoString(C.lo_get_path(cdata, C.ssize_t(cdatalen))), Params: make([]interface{}, 0, 10), } argc := int(C.lo_message_get_argc(msg)) for i := 0; i < argc; i++ { result.Params = append(result.Params, extractArgument(msg, i)) } return result, nil }
func (im *Image) pixels(r *Rect, ex *C.ExceptionInfo) *C.PixelPacket { return C.GetVirtualPixels(im.image, C.ssize_t(r.X), C.ssize_t(r.Y), C.size_t(r.Width), C.size_t(r.Height), ex) }
// Sets the page geometry of the magick wand. func (mw *MagickWand) SetPage(width, height uint, x, y int) error { ok := C.MagickSetPage(mw.mw, C.size_t(width), C.size_t(height), C.ssize_t(x), C.ssize_t(y)) return mw.getLastErrorIfFailed(ok) }
func magickLong(v int) C.ssize_t { return C.ssize_t(v) }
// Sets the size and offset of the magick wand. Set it before you read a raw // image format such as RGB, GRAY, or CMYK. func (mw *MagickWand) SetSizeOffset(cols, rows uint, offset int) error { ok := C.MagickSetSizeOffset(mw.mw, C.size_t(cols), C.size_t(rows), C.ssize_t(offset)) return mw.getLastErrorIfFailed(ok) }
// Returns a new pixel iterator // // mw: the magick wand to iterate on // x, y, cols, rows: there values define the perimeter of a region of pixels // func (mw *MagickWand) NewPixelRegionIterator(x, y int, width, height uint) *PixelIterator { return &PixelIterator{C.NewPixelRegionIterator(mw.mw, C.ssize_t(x), C.ssize_t(y), C.size_t(width), C.size_t(height))} }
// Set the pixel iterator row. func (pi *PixelIterator) SetIteratorRow(row int) error { C.PixelSetIteratorRow(pi.pi, C.ssize_t(row)) return pi.GetLastError() }
// Set the pixel iterator row. func (pi *PixelIterator) SetIteratorRow(row int) error { ok := C.PixelSetIteratorRow(pi.pi, C.ssize_t(row)) return pi.getLastErrorIfFailed(ok) }
// Sets the size and offset of the magick wand. Set it before you read a raw // image format such as RGB, GRAY, or CMYK. func (mw *MagickWand) SetSizeOffset(cols, rows uint, offset int) error { C.MagickSetSizeOffset(mw.mw, C.size_t(cols), C.size_t(rows), C.ssize_t(offset)) return mw.GetLastError() }
// Sets the page geometry of the magick wand. func (mw *MagickWand) SetPage(width, height uint, x, y int) error { C.MagickSetPage(mw.mw, C.size_t(width), C.size_t(height), C.ssize_t(x), C.ssize_t(y)) return mw.GetLastError() }
// Sets the overall canvas size to be recorded with the drawing vector data. // Usually this will be specified using the same size as the canvas image. // When the vector data is saved to SVG or MVG formats, the viewbox is use to // specify the size of the canvas image that a viewer will render the vector // data on. // // x1: left x ordinate // // y1: top y ordinate // // x2: right x ordinate // // y2: bottom y ordinate func (dw *DrawingWand) SetViewbox(x1, y1, x2, y2 int) { C.DrawSetViewbox(dw.dw, C.ssize_t(x1), C.ssize_t(y1), C.ssize_t(x2), C.ssize_t(y2)) }
func (self *Canvas) PixelIterator(x, y int, width, height uint) *PixelIterator { return &PixelIterator{iterator: C.NewPixelRegionIterator(self.wand, C.ssize_t(x), C.ssize_t(y), C.size_t(width), C.size_t(height))} }