luajitos

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

image_parser.lua (1715B)


      1 -- image_parser.lua
      2 -- Wrapper that uses Image library for loading images
      3 
      4 local Image = require("Image")
      5 
      6 local image_parser = {}
      7 
      8 -- Load an image using Image library
      9 -- Returns: {width, height, image} or nil, error_message
     10 function image_parser.load_image(filepath, target_width, target_height)
     11     if not filepath then
     12         return nil, "No filepath provided"
     13     end
     14 
     15     -- Load using Image library
     16     local img, err = Image.open(filepath)
     17     if not img then
     18         return nil, err or ("Failed to load image: " .. filepath)
     19     end
     20 
     21     -- Get original dimensions
     22     local width, height = img:getSize()
     23 
     24     -- Calculate actual dimensions if target specified
     25     local actual_width = width
     26     local actual_height = height
     27 
     28     if target_width and target_height then
     29         actual_width = target_width
     30         actual_height = target_height
     31     elseif target_width then
     32         -- Maintain aspect ratio
     33         local aspect = height / width
     34         actual_width = target_width
     35         actual_height = math.floor(target_width * aspect + 0.5)
     36     elseif target_height then
     37         -- Maintain aspect ratio
     38         local aspect = width / height
     39         actual_height = target_height
     40         actual_width = math.floor(target_height * aspect + 0.5)
     41     end
     42 
     43     return {
     44         width = actual_width,
     45         height = actual_height,
     46         original_width = width,
     47         original_height = height,
     48         image = img,  -- Image object for drawing
     49         pixels = nil  -- Legacy compatibility
     50     }
     51 end
     52 
     53 -- Cleanup function
     54 function image_parser.destroy(imageData)
     55     -- Image objects don't need explicit cleanup in Lua
     56     if imageData then
     57         imageData.image = nil
     58     end
     59 end
     60 
     61 return image_parser