From ae18379ca8ab5fad80bf6e26ccd2ab5568ff59ea Mon Sep 17 00:00:00 2001 From: IgorTimofeev Date: Sat, 4 Sep 2021 19:44:52 +0700 Subject: [PATCH] Added OCIF8 image format support & fixed HEX editor app --- Applications/App Market.app/Main.lua | 2 +- Applications/HEX.app/Main.lua | 72 +++++++++++++++----------- Applications/Picture Edit.app/Main.lua | 2 +- Installer/Files.cfg | 18 +++---- Libraries/Color.lua | 13 +++++ Libraries/GUI.lua | 16 +++++- Libraries/Image.lua | 53 +++++++++++-------- 7 files changed, 111 insertions(+), 65 deletions(-) diff --git a/Applications/App Market.app/Main.lua b/Applications/App Market.app/Main.lua index 19caa8a0..4e3ae519 100644 --- a/Applications/App Market.app/Main.lua +++ b/Applications/App Market.app/Main.lua @@ -241,7 +241,7 @@ local function checkImage(url, mneTolkoSprosit) if data:sub(1, 4) == "OCIF" then local encodingMethod = string.byte(data:sub(5, 5)) - if encodingMethod == 6 or encodingMethod == 7 then + if encodingMethod >= 6 or encodingMethod <= 8 then if string.byte(data:sub(6, 6)) == 8 and string.byte(data:sub(7, 7)) == 4 then if mneTolkoSprosit then handle:close() diff --git a/Applications/HEX.app/Main.lua b/Applications/HEX.app/Main.lua index f7a1810c..bb5ddd4d 100755 --- a/Applications/HEX.app/Main.lua +++ b/Applications/HEX.app/Main.lua @@ -1,13 +1,18 @@ local text = require("Text") -local number = require("Number") local filesystem = require("Filesystem") local GUI = require("GUI") local screen = require("Screen") local system = require("System") +local paths = require("Paths") ------------------------------------------------------------------------------------------------------------------ +local configPath = paths.user.applicationData .. "HEX Editor/Config.cfg" +local config = filesystem.exists(configPath) and filesystem.readTable(configPath) or { + recentPath = "/OS.lua" +} + local colors = { background = 0xF0F0F0, backgroundText = 0x555555, @@ -39,8 +44,13 @@ local scrollBar, titleTextBox local workspace, window = system.addWindow(GUI.filledWindow(1, 1, 98, 25, colors.background)) +window.maxWidth = window.width +window.showDesktopOnMaximize = true + window.backgroundPanel.localX, window.backgroundPanel.localY = 11, 5 -window.backgroundPanel.width, window.backgroundPanel.height = window.width - 10, window.height - 4 +window.backgroundPanel.width = window.width - 10 + +window.actionButtons.localY = 2 local function byteArrayToNumber(b) local n = 0 @@ -204,8 +214,8 @@ local function byteFieldEventHandler(workspace, object, e1, e2, e3, e4, e5) end end -local function newByteField(x, y, width, height, elementWidth, elementHeight, asChar) - local object = GUI.object(x, y, width, height) +local function newByteField(x, y, width, elementWidth, elementHeight, asChar) + local object = GUI.object(x, y, width, 1) object.elementWidth = elementWidth object.elementHeight = elementHeight @@ -221,20 +231,19 @@ end window:addChild(GUI.panel(1, 1, window.width, 3, 0x3C3C3C)):moveToBack() -local byteField = window:addChild(newByteField(13, 6, 64, 20, 4, 2, false)) -local charField = window:addChild(newByteField(byteField.localX + byteField.width + 3, 6, 16, 20, 1, 2, true)) -local separator = window:addChild(GUI.object(byteField.localX + byteField.width, 5, 1, 21)) +local byteField = window:addChild(newByteField(13, 6, 64, 4, 2, false)) +local charField = window:addChild(newByteField(byteField.localX + byteField.width + 3, 6, 16, 1, 2, true)) +local separator = window:addChild(GUI.object(byteField.localX + byteField.width, 5, 1, 1)) separator.draw = function(object) for i = object.y, object.y + object.height - 1 do screen.drawText(object.x, i, colors.separator, "│") end end - window:addChild(GUI.panel(11, 4, window.width - 10, 1, colors.panel)) -- Vertical -local verticalCounter = window:addChild(GUI.object(1, 4, 10, window.height - 3)) +local verticalCounter = window:addChild(GUI.object(1, 4, 10, 1)) verticalCounter.draw = function(object) screen.drawRectangle(object.x, object.y, object.width, object.height, colors.panel, colors.panelText, " ") @@ -277,12 +286,11 @@ window:addChild(GUI.object(13, 4, 62, 1)).draw = function(object) end end -scrollBar = window:addChild(GUI.scrollBar(window.width, 5, 1, window.height - 4, 0xC3C3C3, 0x393939, 0, 1, 1, 160, 1, true)) +scrollBar = window:addChild(GUI.scrollBar(window.width, 5, 1, 1, 0xC3C3C3, 0x393939, 0, 1, 1, 160, 1, true)) scrollBar.eventHandler = nil titleTextBox = window:addChild( - GUI.textBox( - 1, 1, math.floor(window.width * 0.35), 3, + GUI.textBox(1, 1, math.floor(window.width * 0.35), 3, colors.titleBackground, colors.titleText, { @@ -293,6 +301,7 @@ titleTextBox = window:addChild( 1, 1, 0 ) ) + titleTextBox.localX = math.floor(window.width / 2 - titleTextBox.width / 2) titleTextBox:setAlignment(GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_TOP) titleTextBox.eventHandler = nil @@ -307,6 +316,7 @@ local function load(path) if file then bytes = {} + local byte while true do byte = file:readBytes(1) @@ -318,9 +328,11 @@ local function load(path) end file:close() + offset = 0 selection.from, selection.to = 1, 1 scrollBar.value, scrollBar.maximumValue = 0, #bytes + status() else GUI.alert("Failed to open file for reading: " .. tostring(reason)) @@ -329,18 +341,26 @@ end openFileButton.onTouch = function() local filesystemDialog = GUI.addFilesystemDialog(workspace, true, 50, math.floor(workspace.height * 0.8), "Open", "Cancel", "File name", "/") + filesystemDialog:setMode(GUI.IO_MODE_OPEN, GUI.IO_MODE_FILE) filesystemDialog:show() + filesystemDialog.onSubmit = function(path) load(path) + + config.recentPath = path + filesystem.writeTable(configPath, config) + workspace:draw() end end saveFileButton.onTouch = function() local filesystemDialog = GUI.addFilesystemDialog(workspace, true, 50, math.floor(workspace.height * 0.8), "Save", "Cancel", "File name", "/") + filesystemDialog:setMode(GUI.IO_MODE_SAVE, GUI.IO_MODE_FILE) filesystemDialog:show() + filesystemDialog.onSubmit = function(path) local file = filesystem.open(path, "wb") if file then @@ -354,31 +374,21 @@ saveFileButton.onTouch = function() end end -window.actionButtons.localY = 2 -window.actionButtons.maximize.onTouch = function() - window.height = window.parent.height - byteField.height = window.height - 6 +window.onResize = function(width, height) + byteField.height = height - 6 charField.height = byteField.height scrollBar.height = byteField.height - window.backgroundPanel.height = window.height - 4 + window.backgroundPanel.height = height - 4 verticalCounter.height = window.backgroundPanel.height + 1 separator.height = byteField.height + 2 - - window.localY = 1 - - workspace:draw() end ------------------------------------------------------------------------------------------------------------------ -load("/OS.lua") +window.onResize(window.width, window.height) + +local args, options = system.parseArguments(...) + +load(((options.o or options.open) and args[1] and filesystem.exists(args[1])) and args[1] or config.recentPath) + workspace:draw() - - - - - - - - - diff --git a/Applications/Picture Edit.app/Main.lua b/Applications/Picture Edit.app/Main.lua index 8c25b5f2..c1df5d78 100644 --- a/Applications/Picture Edit.app/Main.lua +++ b/Applications/Picture Edit.app/Main.lua @@ -315,7 +315,7 @@ end local function save(path) if filesystem.extension(path) == ".pic" then - local result, reason = image.save(path, window.image.data, 7) + local result, reason = image.save(path, window.image.data, 8) if result then setSavePath(path) diff --git a/Installer/Files.cfg b/Installer/Files.cfg index a8f06898..73e22de8 100644 --- a/Installer/Files.cfg +++ b/Installer/Files.cfg @@ -182,13 +182,6 @@ "Applications/Settings.app/Localizations/Italian.lang", "Applications/Settings.app/Localizations/Japanese.lang", "Applications/Settings.app/Localizations/Dutch.lang", - -- Calendar - { path="Applications/Calendar.app/Main.lua", shortcut = true }, - "Applications/Calendar.app/Icon.pic", - "Applications/Calendar.app/Icons/ArrowLeft.pic", - "Applications/Calendar.app/Icons/ArrowRight.pic", - "Applications/Calendar.app/Localizations/English.lang", - "Applications/Calendar.app/Localizations/Russian.lang", -- 3D Print { path="Applications/3D Print.app/Main.lua", id=859, shortcut = true }, "Applications/3D Print.app/Icon.pic", @@ -250,8 +243,8 @@ { path="Applications/Graph.app/Main.lua", id=238, shortcut = true }, "Applications/Graph.app/Icon.pic", -- HEX - { path="Applications/HEX.app/Main.lua", id=248, shortcut = true }, - "Applications/HEX.app/Icon.pic", + { path="Applications/HEX Editor.app/Main.lua", id=248, shortcut = true }, + "Applications/HEX Editor.app/Icon.pic", -- Running string { path="Applications/Running String.app/Main.lua", id=410, shortcut = true }, "Applications/Running String.app/Icon.pic", @@ -291,6 +284,13 @@ "Applications/VK.app/Styles/Default.lua", "Applications/VK.app/Styles/Bright.lua", "Applications/VK.app/Styles/Dark.lua", + -- Calendar + { path="Applications/Calendar.app/Main.lua", shortcut = true }, + "Applications/Calendar.app/Icon.pic", + "Applications/Calendar.app/Icons/ArrowLeft.pic", + "Applications/Calendar.app/Icons/ArrowRight.pic", + "Applications/Calendar.app/Localizations/English.lang", + "Applications/Calendar.app/Localizations/Russian.lang", }, wallpapers = { "Pictures/AhsokaTano.pic", diff --git a/Libraries/Color.lua b/Libraries/Color.lua index 31c02008..9f38da65 100755 --- a/Libraries/Color.lua +++ b/Libraries/Color.lua @@ -23,6 +23,7 @@ if computer.getArchitecture and computer.getArchitecture() == "Lua 5.3" then function(color1, color2, transparency) local invertedTransparency = 1 - transparency + return ((color2 >> 16) * invertedTransparency + (color1 >> 16) * transparency) // 1 << 16 | ((color2 >> 8 & 0xFF) * invertedTransparency + (color1 >> 8 & 0xFF) * transparency) // 1 << 8 | @@ -31,6 +32,7 @@ if computer.getArchitecture and computer.getArchitecture() == "Lua 5.3" then function(color1, color2, position) local r1, g1, b1 = color1 >> 16, color1 >> 8 & 0xFF, color1 & 0xFF + return (r1 + ((color2 >> 16) - r1) * position) // 1 << 16 | (g1 + ((color2 >> 8 & 0xFF) - g1) * position) // 1 << 8 | @@ -49,6 +51,7 @@ if computer.getArchitecture and computer.getArchitecture() == "Lua 5.3" then paletteR, paletteG, paletteB = paletteColor >> 16, paletteColor >> 8 & 0xFF, paletteColor & 0xFF delta = (paletteR - r) ^ 2 + (paletteG - g) ^ 2 + (paletteB - b) ^ 2 + if delta < closestDelta then closestDelta, closestIndex = delta, i end @@ -66,6 +69,7 @@ else function(integerColor) local r = integerColor / 65536 r = r - r % 1 + local g = (integerColor - r * 65536) / 256 g = g - g % 1 @@ -81,11 +85,13 @@ else local r1 = color1 / 65536 r1 = r1 - r1 % 1 + local g1 = (color1 - r1 * 65536) / 256 g1 = g1 - g1 % 1 local r2 = color2 / 65536 r2 = r2 - r2 % 1 + local g2 = (color2 - r2 * 65536) / 256 g2 = g2 - g2 % 1 @@ -103,12 +109,15 @@ else function(color1, color2, position) local r1 = color1 / 65536 r1 = r1 - r1 % 1 + local g1 = (color1 - r1 * 65536) / 256 g1 = g1 - g1 % 1 + local b1 = color1 - r1 * 65536 - g1 * 256 local r2 = color2 / 65536 r2 = r2 - r2 % 1 + local g2 = (color2 - r2 * 65536) / 256 g2 = g2 - g2 % 1 @@ -128,12 +137,15 @@ else local r = color24Bit / 65536 r = r - r % 1 + local g = (color24Bit - r * 65536) / 256 g = g - g % 1 + local b = color24Bit - r * 65536 - g * 256 for index = 1, #palette do paletteColor = palette[index] + if color24Bit == paletteColor then return index - 1 else @@ -144,6 +156,7 @@ else paletteB = paletteColor - paletteR * 65536 - paletteG * 256 delta = (paletteR - r) ^ 2 + (paletteG - g) ^ 2 + (paletteB - b) ^ 2 + if delta < closestDelta then closestDelta, closestIndex = delta, index end diff --git a/Libraries/GUI.lua b/Libraries/GUI.lua index d38cd011..459b52db 100755 --- a/Libraries/GUI.lua +++ b/Libraries/GUI.lua @@ -4289,6 +4289,7 @@ local function windowCheck(window, x, y) return true elseif child.children then local result = windowCheck(child, x, y) + if result == true then return true elseif result == false then @@ -4313,6 +4314,7 @@ local function windowEventHandler(workspace, window, e1, e2, e3, e4, ...) end elseif e1 == "drag" and window.lastTouchX and not windowCheck(window, e3, e4) then local xOffset, yOffset = e3 - window.lastTouchX, e4 - window.lastTouchY + if xOffset ~= 0 or yOffset ~= 0 then window.localX, window.localY = window.localX + xOffset, window.localY + yOffset window.lastTouchX, window.lastTouchY = e3, e4 @@ -4344,10 +4346,22 @@ function GUI.windowMaximize(window, animationDisabled) if window.maximized then toX, toY, toWidth, toHeight = window.oldGeometryX, window.oldGeometryY, window.oldGeometryWidth, window.oldGeometryHeight + window.oldGeometryX, window.oldGeometryY, window.oldGeometryWidth, window.oldGeometryHeight = nil, nil, nil, nil window.maximized = nil else - toX, toY, toWidth, toHeight = 1, 1, window.parent.width, window.parent.height + toWidth, toHeight = window.parent.width, window.parent.height + + if window.maxWidth then + toWidth = math.min(toWidth, window.maxWidth) + end + + if window.maxHeight then + toHeight = math.min(toHeight, window.maxHeight) + end + + toX, toY = math.floor(1 + window.parent.width / 2 - toWidth / 2), math.floor(1 + window.parent.height / 2 - toHeight / 2) + window.oldGeometryX, window.oldGeometryY, window.oldGeometryWidth, window.oldGeometryHeight = window.localX, window.localY, window.width, window.height window.maximized = true end diff --git a/Libraries/Image.lua b/Libraries/Image.lua index 9b84094f..2a1215ac 100755 --- a/Libraries/Image.lua +++ b/Libraries/Image.lua @@ -78,32 +78,32 @@ encodingMethodsLoad[5] = function(file, picture) end end -local function loadOCIF67(file, picture, mode) - picture[1] = file:readBytes(1) - picture[2] = file:readBytes(1) +local function loadOCIF678(file, picture, is7, is8) + picture[1] = file:readBytes(1) + is8 + picture[2] = file:readBytes(1) + is8 local currentAlpha, currentSymbol, currentBackground, currentForeground, currentY - for alpha = 1, file:readBytes(1) + mode do + for alpha = 1, file:readBytes(1) + is7 do currentAlpha = file:readBytes(1) / 255 - for symbol = 1, file:readBytes(2) + mode do + for symbol = 1, file:readBytes(2) + is7 do currentSymbol = file:readUnicodeChar() - for background = 1, file:readBytes(1) + mode do + for background = 1, file:readBytes(1) + is7 do currentBackground = color.to24Bit(file:readBytes(1)) - for foreground = 1, file:readBytes(1) + mode do + for foreground = 1, file:readBytes(1) + is7 do currentForeground = color.to24Bit(file:readBytes(1)) - for y = 1, file:readBytes(1) + mode do + for y = 1, file:readBytes(1) + is7 do currentY = file:readBytes(1) - for x = 1, file:readBytes(1) + mode do + for x = 1, file:readBytes(1) + is7 do image.set( picture, - file:readBytes(1), - currentY, + file:readBytes(1) + is8, + currentY + is8, currentBackground, currentForeground, currentAlpha, @@ -117,9 +117,9 @@ local function loadOCIF67(file, picture, mode) end end -local function saveOCIF67(file, picture, mode) +local function saveOCIF678(file, picture, is7, is8) local function getGroupSize(t) - local size = mode == 1 and -1 or 0 + local size = -is7 for key in pairs(t) do size = size + 1 @@ -133,8 +133,8 @@ local function saveOCIF67(file, picture, mode) -- Writing 1 byte per image width and height file:writeBytes( - picture[1], - picture[2] + picture[1] - is8, + picture[2] - is8 ) -- Writing 1 byte for alphas array size @@ -178,13 +178,14 @@ local function saveOCIF67(file, picture, mode) for y in pairs(groupedPicture[alpha][symbol][background][foreground]) do file:writeBytes( -- Writing 1 byte for current y value - y, + y - is8, -- Writing 1 byte for x array size - #groupedPicture[alpha][symbol][background][foreground][y] - mode + #groupedPicture[alpha][symbol][background][foreground][y] - is7 ) for x = 1, #groupedPicture[alpha][symbol][background][foreground][y] do - file:writeBytes(groupedPicture[alpha][symbol][background][foreground][y][x]) + -- Wrting 1 byte for current x value + file:writeBytes(groupedPicture[alpha][symbol][background][foreground][y][x] - is8) end end end @@ -194,19 +195,27 @@ local function saveOCIF67(file, picture, mode) end encodingMethodsSave[6] = function(file, picture) - saveOCIF67(file, picture, 0) + saveOCIF678(file, picture, 0, 0) end encodingMethodsLoad[6] = function(file, picture) - loadOCIF67(file, picture, 0) + loadOCIF678(file, picture, 0, 0) end encodingMethodsSave[7] = function(file, picture) - saveOCIF67(file, picture, 1) + saveOCIF678(file, picture, 1, 0) end encodingMethodsLoad[7] = function(file, picture) - loadOCIF67(file, picture, 1) + loadOCIF678(file, picture, 1, 0) +end + +encodingMethodsSave[8] = function(file, picture) + saveOCIF678(file, picture, 1, 1) +end + +encodingMethodsLoad[8] = function(file, picture) + loadOCIF678(file, picture, 1, 1) end --------------------------------------------------------------------------------