luajitos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

viewer.lua (3604B)


      1 -- LuaJIT OS Lens Viewer - Image viewer application
      2 -- Displays images centered and scaled to fit the window
      3 
      4 -- Default window dimensions
      5 local defaultWidth = 512
      6 local defaultHeight = 512
      7 
      8 -- Create window with default size
      9 local window = app:newWindow("Lens Viewer", defaultWidth, defaultHeight)
     10 window.resizable = true
     11 
     12 -- Image data
     13 local imageData = nil
     14 local imageWidth = 0
     15 local imageHeight = 0
     16 local imagePath = ""
     17 
     18 -- Helper: Load image from file path
     19 local function loadImage(path)
     20     -- Try to load image using the system's image loading function
     21     -- This would need to be implemented in the C layer
     22     if VESALoadImage then
     23         local img = VESALoadImage(path)
     24         if img then
     25             imageData = img
     26             imageWidth = img.width or 0
     27             imageHeight = img.height or 0
     28             imagePath = path
     29             return true
     30         end
     31     end
     32     return false
     33 end
     34 
     35 -- Helper: Calculate scaled dimensions to fit window while maintaining aspect ratio
     36 local function calculateScaledDimensions(imgW, imgH, winW, winH)
     37     local scaleX = winW / imgW
     38     local scaleY = winH / imgH
     39     local scale = math.min(scaleX, scaleY)
     40     
     41     local scaledW = math.floor(imgW * scale)
     42     local scaledH = math.floor(imgH * scale)
     43     
     44     -- Center position
     45     local x = math.floor((winW - scaledW) / 2)
     46     local y = math.floor((winH - scaledH) / 2)
     47     
     48     return x, y, scaledW, scaledH
     49 end
     50 
     51 -- onOpened callback to resize window to fit image
     52 window.onOpened = function()
     53     if imageWidth > 0 and imageHeight > 0 then
     54         -- Resize window to match image dimensions
     55         if window.setSize then
     56             window:setSize(imageWidth, imageHeight)
     57         else
     58             -- Fallback: update window dimensions directly
     59             window.width = imageWidth
     60             window.height = imageHeight
     61             window.dirty = true
     62         end
     63     end
     64 end
     65 
     66 -- Draw callback
     67 window.onDraw = function(gfx)
     68     local winW = window:getWidth()
     69     local winH = window:getHeight()
     70 
     71     -- Fill background with black
     72     gfx:fillRect(0, 0, winW, winH, 0x000000)
     73 
     74     if imageData and imageWidth > 0 and imageHeight > 0 then
     75         -- Calculate scaled dimensions
     76         local x, y, w, h = calculateScaledDimensions(imageWidth, imageHeight, winW, winH)
     77 
     78         -- Draw the image (scaled to fit)
     79         if gfx.drawImage then
     80             gfx:drawImage(imageData, x, y, w, h)
     81         elseif VESADrawImage then
     82             -- Direct call to VESA function if gfx wrapper doesn't exist
     83             VESADrawImage(imageData, x, y, w, h)
     84         else
     85             -- Fallback: show error message
     86             gfx:drawText(10, 10, "Image loaded but cannot display", 0xFF0000)
     87             gfx:drawText(10, 30, "Size: " .. imageWidth .. "x" .. imageHeight, 0xFFFFFF)
     88         end
     89     else
     90         -- No image loaded - show instructions
     91         gfx:drawText(winW / 2 - 50, winH / 2 - 20, "No image loaded", 0x888888)
     92         gfx:drawText(winW / 2 - 80, winH / 2, "Use file explorer to open", 0x888888)
     93     end
     94 end
     95 
     96 -- Try to load image from command line argument
     97 if args and args[1] then
     98     local success = loadImage(args[1])
     99     if success then
    100         print("Loaded image: " .. args[1])
    101         print("Image dimensions: " .. imageWidth .. "x" .. imageHeight)
    102         
    103         -- Call onOpened to resize window
    104         if window.onOpened then
    105             window.onOpened()
    106         end
    107     else
    108         print("Failed to load image: " .. args[1])
    109     end
    110 else
    111     print("Lens Viewer started with no image")
    112     print("Usage: run com.luajitos.lensviewer <image_path>")
    113 end
    114 
    115 window:markDirty()