6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/sjpeg.rb', line 6
def self.compress(pixels, width, height, quality: 70)
in_pointer = FFI::MemoryPointer.new(:uint8, pixels.size)
in_pointer.write_array_of_uint8(pixels)
out_pointer = FFI::MemoryPointer.new(:pointer)
out_size_t = Unstable.SjpegCompress(
in_pointer,
width,
height,
quality,
out_pointer,
)
out_result = out_pointer.get_pointer(0)
result = out_result.read_array_of_uint8(out_size_t)
Unstable.SjpegFreeBuffer(out_result)
"data:image/jpeg;base64," + Base64.encode64(result.pack('C*')).split("\n").join
ensure
in_pointer.free if in_pointer
end
|