Skip to content
unXedDani edited this page Mar 5, 2016 · 17 revisions

Artal

A .PSD parsing library for LÖVE

Purpose is to expose the structure of .PSD files into LÖVE.

  • ImageData for the layers.
  • Names.
  • Blendmodes.
  • Clipping mode.
  • Structure, folder / image.

artal.lua

  • artal = require("artal") Load Library
  • psdTable = artal.newPSD(FileNameOrFileData) full structure with the layers loaded in as images.
  • psdTable = artal.newPSD(FileNameOrFileData, "info") full structure.
  • ImageDataOrNil, ox, oy = artal.newPSD(FileNameOrFileData, Layer number) ImageData, "Offset X" and "Offset Y" for the specified layer number, nil if the layer is empty / folder.
  • ImageData = artal.newPSD(FileNameOrFileData, "composed") ImageData of the composed image as it's stored in the psd file itself. (Note that Photoshop has an slightly erroneous implementation composing the alpha into the composed image. So images without a fully opaque background will be slightly blended with white.)

Sample code:

local artal = require("artal")

local simpleSample = artal.newPSD("SomeFile.psd")

function love.draw()
    for i = 1, #simpleSample do
        if simpleSample[i].type == "image" then
            love.graphics.draw(
                simpleSample[i].image,
                nil, -- Position X
                nil, -- Position Y
                nil, -- Rotation
                nil, -- Scale X
                nil, -- Scale Y
                simpleSample[i].ox, -- Offset X
                simpleSample[i].oy) -- Offset Y
        end
    end
end

Structure of the table returned from artal.newPSD()

This structure is generated from writetable.lua. And you can use that to visualize your own tables.

{
	-- Table with 5 indexes, and 2 string keys.
	-- Array values are all of type: "table".
	height = 600,
	width = 600,
	[1] = 
	{
		-- Table with 7 string keys.
		oy = 0,
		ox = 0,
		image = "Image: 0x7f9ca8c9a4f0",
		type = "image",
		blend = "norm",
		name = "Layer5",
		clip = "false",
	},
	[2] = 
	{
		-- Table with 7 string keys.
		oy = -126,
		ox = 0,
		image = "Image: 0x7f9ca8c9b9c0",
		type = "image",
		blend = "norm",
		name = "Layer2",
		clip = "false",
	},
	[3] = 
	{
		-- Table with 7 string keys.
		oy = -41,
		ox = -28,
		image = "Image: 0x7f9ca8d312d0",
		type = "image",
		blend = "norm",
		name = "Layer1",
		clip = "false",
	},
	[4] = 
	{
		-- Table with 7 string keys.
		oy = 0,
		ox = 0,
		image = "Image: 0x7f9caa9514d0",
		type = "image",
		blend = "norm",
		name = "Layer3",
		clip = "false",
	},
	[5] = 
	{
		-- Table with 7 string keys.
		oy = -42,
		ox = -112,
		image = "Image: 0x7f9caa952200",
		type = "image",
		blend = "norm",
		name = "Layer4",
		clip = "false",
	},
}

artalhelper.lua

Some helper functions for easily loading / sorting the tables.

  • artalHelper = require("artalhelper")
  • artalHelper.load(FileNameOrFileData,psdTable) Load in images on layers that are image layer.
  • psdTable = artalHelper.sortSimple(psdTable)
  • psdTable = artalHelper.sortRecursive(psdTable)

Rendering: --fix

Flattening: Clipping layers: Blendmodes implemented: Alpha, Multiply, Screen and Overlay.

psdshader.lua: --fix

Small library to generate shaders for blending and clipping layers.

  • psdShader = require("psdshader")
  • shaderString = psdShader.createShaderString(globalBlendmode, blendmodeBeingClipped, ...)
  • psdShader.setShader(shader)
  • psdShader.drawClip(drawOrderIndex,image,x,y,r,sx,sy,ox,oy,kx,ky) (Rotation and shearing not implemented.)
  • resultCanvas = psdShader.flatten(psdTableClipTo, psdTableBeingClipped, ...) ox and oy will remain the same as the psdTableClipTo image.

Simple blendmode sample code

psdShader = require("psdshader")

shaderString = psdShader.createShaderString("over") -- Overlay blendmode
--love.filesystem.write("ShaderInspect.glsl",shaderString) -- inspect the generated shader
blendShader = love.graphics.newShader(shaderString) -- Load the shader

someImage = love.graphics.newImage("someimagefile.png")

function love.draw()
    psdShader.setShader(blendShader)
    love.graphics.draw(someImage)
    love.graphics.setShader()
end

writetable.lua

Create a string from tables. So you can inspect tables created by artal.newPSD(). Or anything else for that matter.

  • tableAsString = writetable.createStringFromTable(table)
Clone this wiki locally