def wrapper(self, *args, **kwargs): # Setup variables in_data_len = len(self._data) in_data_p = ffi.new("unsigned char[]", in_data_len) inbuf = ffi.buffer(in_data_p, in_data_len) inbuf[:] = self._data out_bufs = ffi.gc(ffi.new("unsigned char**"), _turbojpeg_cleanup) out_bufs[0] = ffi.NULL out_sizes = ffi.new("unsigned long*") tjhandle = ffi.gc(lib.tjInitTransform(), lib.tjDestroy) # Call the wrapped function with the transformoption struct transformoption = func(self, *args, **kwargs) # Execute transformation rv = lib.tjTransform(tjhandle, in_data_p, in_data_len, 1, out_bufs, out_sizes, transformoption, 0) if rv < 0: raise Exception("Transformation failed: {0}" .format(ffi.string(lib.tjGetErrorStr()))) _weak_keydict[out_bufs[0]] = out_bufs return bytearray(ffi.buffer(out_bufs[0], out_sizes[0])[:])
def wrapper(self, *args, **kwargs): # Setup variables in_data_len = len(self._data) in_data_p = ffi.new("unsigned char[]", in_data_len) inbuf = ffi.buffer(in_data_p, in_data_len) inbuf[:] = self._data out_bufs = ffi.gc(ffi.new("unsigned char**"), _turbojpeg_cleanup) out_bufs[0] = ffi.NULL out_sizes = ffi.new("unsigned long*") tjhandle = ffi.gc(lib.tjInitTransform(), lib.tjDestroy) # Call the wrapped function with the transformoption struct transformoption = func(self, *args, **kwargs) # Execute transformation rv = lib.tjTransform(tjhandle, in_data_p, in_data_len, 1, out_bufs, out_sizes, transformoption, 0) if rv < 0: raise Exception("Transformation failed: {0}".format( ffi.string(lib.tjGetErrorStr()))) _weak_keydict[out_bufs[0]] = out_bufs return bytearray(ffi.buffer(out_bufs[0], out_sizes[0])[:])
def scale(self, width, height, quality=75): in_data_len = len(self._data) in_data_p = ffi.new("unsigned char[]", in_data_len) inbuf = ffi.buffer(in_data_p, in_data_len) inbuf[:] = self._data img = ffi.gc(lib.epeg_memory_open(in_data_p, in_data_len), lib.epeg_close) lib.epeg_decode_size_set(img, width, height) lib.epeg_quality_set(img, quality) pdata = ffi.gc(ffi.new("unsigned char **"), _epeg_free_buffer) psize = ffi.new("int*") lib.epeg_memory_output_set(img, pdata, psize) lib.epeg_encode(img) _weak_keydict[pdata[0]] = pdata return bytearray(ffi.buffer(pdata[0], psize[0])[:])
def get_dimensions(self): width = ffi.new("int*") height = ffi.new("int*") in_data_len = len(self._data) in_data_p = ffi.new("unsigned char[]", in_data_len) inbuf = ffi.buffer(in_data_p, in_data_len) inbuf[:] = self._data img = ffi.gc(lib.epeg_memory_open(in_data_p, in_data_len), lib.epeg_close) lib.epeg_size_get(img, width, height) return (width[0], height[0])