diff --git a/lua/image/backends/kitty/helpers.lua b/lua/image/backends/kitty/helpers.lua index 15fe748..c989a4e 100644 --- a/lua/image/backends/kitty/helpers.lua +++ b/lua/image/backends/kitty/helpers.lua @@ -2,7 +2,7 @@ local codes = require("image/backends/kitty/codes") local utils = require("image/utils") local uv = vim.uv - -- Allow for loop to be used on older versions +-- Allow for loop to be used on older versions if not uv then uv = vim.loop end local stdout = vim.loop.new_tty(1, false) @@ -98,7 +98,7 @@ local write_graphics = function(config, data) local file = io.open(data, "rb") data = file:read("*all") end - data = utils.base64.encode(data):gsub("%-", "/") + data = vim.base64.encode(data):gsub("%-", "/") local chunks = get_chunked(data) local m = #chunks > 1 and 1 or 0 control_payload = control_payload .. ",m=" .. m diff --git a/lua/image/image.lua b/lua/image/image.lua index 28313f1..bb58b8c 100644 --- a/lua/image/image.lua +++ b/lua/image/image.lua @@ -63,7 +63,7 @@ function Image:render(geometry) local format = self.global_state.processor.get_format(self.original_path) if format ~= "png" then - local converted_path = self.global_state.tmp_dir .. "/" .. utils.base64.encode(self.id) .. "-source.png" + local converted_path = self.global_state.tmp_dir .. "/" .. vim.base64.encode(self.id) .. "-source.png" self.path = self.global_state.processor.convert_to_png(self.original_path, converted_path) end @@ -189,7 +189,7 @@ end ---@param brightness number function Image:brightness(brightness) - local altered_path = self.global_state.tmp_dir .. "/" .. utils.base64.encode(self.id) .. "-source.png" + local altered_path = self.global_state.tmp_dir .. "/" .. vim.base64.encode(self.id) .. "-source.png" self.path = self.global_state.processor.brightness(self.path, brightness, altered_path) self.cropped_path = self.path self.resize_hash = nil @@ -203,7 +203,7 @@ end ---@param saturation number function Image:saturation(saturation) - local altered_path = self.global_state.tmp_dir .. "/" .. utils.base64.encode(self.id) .. "-source.png" + local altered_path = self.global_state.tmp_dir .. "/" .. vim.base64.encode(self.id) .. "-source.png" self.path = self.global_state.processor.saturation(self.path, saturation, altered_path) self.cropped_path = self.path self.resize_hash = nil @@ -217,7 +217,7 @@ end ---@param hue number function Image:hue(hue) - local altered_path = self.global_state.tmp_dir .. "/" .. utils.base64.encode(self.id) .. "-source.png" + local altered_path = self.global_state.tmp_dir .. "/" .. vim.base64.encode(self.id) .. "-source.png" self.path = self.global_state.processor.hue(self.path, hue, altered_path) self.cropped_path = self.path self.resize_hash = nil @@ -294,7 +294,7 @@ local from_file = function(path, options, state) -- convert non-png images to png and read the dimensions local source_path = absolute_original_path - local converted_path = state.tmp_dir .. "/" .. utils.base64.encode(id) .. "-source.png" + local converted_path = state.tmp_dir .. "/" .. vim.base64.encode(id) .. "-source.png" -- case 1: non-png, already converted if diff --git a/lua/image/renderer.lua b/lua/image/renderer.lua index d018a86..b2935ef 100644 --- a/lua/image/renderer.lua +++ b/lua/image/renderer.lua @@ -380,7 +380,7 @@ local render = function(image) image.resize_hash = resize_hash else -- perform resize - local tmp_path = state.tmp_dir .. "/" .. utils.base64.encode(image.id) .. "-resized-" .. resize_hash .. ".png" + local tmp_path = state.tmp_dir .. "/" .. vim.base64.encode(image.id) .. "-resized-" .. resize_hash .. ".png" image.resized_path = state.processor.resize(image.path, pixel_width, pixel_height, tmp_path) image.resize_hash = resize_hash image_cache.resized[resize_hash] = image.resized_path @@ -404,7 +404,7 @@ local render = function(image) image.crop_hash = crop_hash else -- perform crop - local tmp_path = state.tmp_dir .. "/" .. utils.base64.encode(image.id) .. "-cropped-" .. crop_hash .. ".png" + local tmp_path = state.tmp_dir .. "/" .. vim.base64.encode(image.id) .. "-cropped-" .. crop_hash .. ".png" image.cropped_path = state.processor.crop( image.resized_path or image.path, 0, diff --git a/lua/image/utils/base64.lua b/lua/image/utils/base64.lua deleted file mode 100644 index a0f26b7..0000000 --- a/lua/image/utils/base64.lua +++ /dev/null @@ -1,67 +0,0 @@ --- LuaJIT FFI Base64 encoder/decoder --- Copyright (c) 2022 Edward Lufadeju (edward@ncade.com) --- See end of file for license information - -local ffi = require("ffi") -local base64 = {} - -local b64 = ffi.new("unsigned const char[65]", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-") - -function base64.encode(str) - ---@diagnostic disable-next-line: undefined-global - local band, bor, lsh, rsh = bit.band, bit.bor, bit.lshift, bit.rshift - local len = #str - local enc_len = 4 * math.ceil(len / 3) -- (len + 2) // 3 * 4 after Lua 5.3 - - local src = ffi.new("unsigned const char[?]", len + 1, str) - local enc = ffi.new("unsigned char[?]", enc_len + 1) - - local i, j = 0, 0 - while i < len - 2 do - enc[j] = b64[band(rsh(src[i], 2), 0x3F)] - enc[j + 1] = b64[bor(lsh(band(src[i], 0x3), 4), rsh(band(src[i + 1], 0xF0), 4))] - enc[j + 2] = b64[bor(lsh(band(src[i + 1], 0xF), 2), rsh(band(src[i + 2], 0xC0), 6))] - enc[j + 3] = b64[band(src[i + 2], 0x3F)] - i, j = i + 3, j + 4 - end - - if i < len then - enc[j] = b64[band(rsh(src[i], 2), 0x3F)] - if i == len - 1 then - enc[j + 1] = b64[lsh(band(src[i], 0x3), 4)] - enc[j + 2] = 0x3D - else - enc[j + 1] = b64[bor(lsh(band(src[i], 0x3), 4), rsh(band(src[i + 1], 0xF0), 4))] - enc[j + 2] = b64[lsh(band(src[i + 1], 0xF), 2)] - end - enc[j + 3] = 0x3D - end - - return ffi.string(enc, enc_len) -end - -return base64 - ---[[ -MIT License - -Copyright (c) 2022 Edward Lufadeju - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. ---]] diff --git a/lua/image/utils/init.lua b/lua/image/utils/init.lua index 6b6e863..6409187 100644 --- a/lua/image/utils/init.lua +++ b/lua/image/utils/init.lua @@ -1,4 +1,3 @@ -local base64 = require("image/utils/base64") local hash = require("image/utils/hash") local logger = require("image/utils/logger") local magic = require("image/utils/magic") @@ -14,7 +13,6 @@ return { log = logger.log, throw = logger.throw, debug = logger.debug, - base64 = base64, random = random, window = window, term = term,