Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added a new method ExifUtils.readMetaDataAsTable which return a lua
table with all exif keys and values
Adapted ExfiUtils.findFirstMatchingValue accordingly
  • Loading branch information
rderimay authored and rderimay committed Jan 9, 2017
1 parent 50758d3 commit 9791996
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 56 deletions.
2 changes: 1 addition & 1 deletion focuspoints.lrdevplugin/DefaultDelegates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function DefaultDelegates.getShotOrientation(photo, metaData)
local dimens = photo:getFormattedMetadata("dimensions")
local orgPhotoW, orgPhotoH = parseDimens(dimens) -- original dimension before any cropping

local metaOrientation = ExifUtils.findValue(metaData, "Orientation")
local metaOrientation = ExifUtils.findFirstMatchingValue(metaData, { "Orientation" })
if string.match(metaOrientation, "90 CCW") and orgPhotoW < orgPhotoH then
return 90 -- 90 CCW => 90 trigo
elseif string.match(metaOrientation, "270 CCW") and orgPhotoW < orgPhotoH then
Expand Down
2 changes: 1 addition & 1 deletion focuspoints.lrdevplugin/DefaultPointRenderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ DefaultPointRenderer.funcGetShotOrientation = nil
--]]
function DefaultPointRenderer.createView(photo, photoDisplayWidth, photoDisplayHeight)
local developSettings = photo:getDevelopSettings()
local metaData = ExifUtils.readMetaData(photo)
local metaData = ExifUtils.readMetaDataAsTable(photo)

local originalWidth, originalHeight = parseDimens(photo:getFormattedMetadata("dimensions"))
local croppedWidth, croppedHeight = parseDimens(photo:getFormattedMetadata("croppedDimensions"))
Expand Down
90 changes: 36 additions & 54 deletions focuspoints.lrdevplugin/ExifUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,44 @@ function ExifUtils.readMetaData(targetPhoto)
return fileInfo
end

function ExifUtils.filterInput(str)
--local result = string.gsub(str, "[^a-zA-Z0-9 ,\\./;'\\<>\\?:\\\"\\{\\}\\|!@#\\$%\\^\\&\\*\\(\\)_\\+\\=-\\[\\]~`]", "?");
-- FIXME: doesn't strip - or ] correctly
local result = string.gsub(str, "[^a-zA-Z0-9 ,\\./;'\\<>\\?:\\\"\\{\\}\\|!@#\\$%\\^\\&\\*\\(\\)_\\+\\=\\-\\[\\\n\\\t~`-]", "?");
return result
end
--[[
-- Transforms the output of ExifUtils.readMetaData and returns a key/value lua Table
-- targetPhoto - LrPhoto to extract the Exif from
--]]
function ExifUtils.readMetaDataAsTable(targetPhoto)
local metaData = ExifUtils.readMetaData(targetPhoto)
if metaData == nil then
return nil
end

function ExifUtils.findValue(metaData, key)
local parts = ExifUtils.createParts(metaData)
local labels = ""
local values = ""
for k in pairs(parts) do
local l = parts[k].key
local v = parts[k].value
if (l == nill) then l = "" end
if (v == nill) then v = "" end
l = LrStringUtils.trimWhitespace(l)
v = LrStringUtils.trimWhitespace(v)
if (key == l) then
return v
end
local parsedTable = {}

for keyword, value in string.gmatch(metaData, "([^\:]+)\:([^\r\n]*)\r?\n") do
keyword = LrStringUtils.trimWhitespace(keyword)
value = LrStringUtils.trimWhitespace(value)
parsedTable[keyword] = value
log("EXIF | Parsed '" .. keyword .. "' = '" .. value .. "'")
end

return nil
return parsedTable
end

function ExifUtils.findFirstMatchingValue(metaData, keys)
--[[
-- Returns the first value of "keys" that could be found within the metaDataTable table
-- Ignores nil and "(none)" as valid values
-- metaDataTable - the medaData key/value table
-- keys - the keys to be search for in order of importance
--]]
function ExifUtils.findFirstMatchingValue(metaDataTable, keys)
local value = nil

for key,keyword in pairs(keys) do
value = ExifUtils.findValue(metaData, keyword)
for key, keyword in pairs(keys) do
value = metaDataTable[keyword]
if value == nil then
log("EXIF | Searching for " .. keyword .. " [" .. string.gsub(keyword, "%s+", "") .. "] -> NIL")
else
log("EXIF | Searching for " .. keyword .. " [" .. string.gsub(keyword, "%s+", "") .. "] -> " .. value)
end

if value ~= nil and value ~= "(none)" then
return value
Expand All @@ -82,34 +89,9 @@ function ExifUtils.findFirstMatchingValue(metaData, keys)
return nil
end

function ExifUtils.splitForColumns(metaData)
local parts = ExifUtils.createParts(metaData)
local labels = ""
local values = ""
for k in pairs(parts) do
local l = parts[k].key
local v = parts[k].value
if (l == nill) then l = "" end
if (v == nill) then v = "" end
l = LrStringUtils.trimWhitespace(l)
v = LrStringUtils.trimWhitespace(v)

labels = labels .. l .. "\r"
values = values .. v .. "\r"
end
return labels, values

end

function ExifUtils.createParts(metaData)
local parts = {}
local num = 0;
for i in string.gmatch(metaData, "[^\\\n]+") do
p = splitToKeyValue(i, ":")
if (p ~= nill) then
parts[num] = p
num = num+1
end
end
return parts
function ExifUtils.filterInput(str)
--local result = string.gsub(str, "[^a-zA-Z0-9 ,\\./;'\\<>\\?:\\\"\\{\\}\\|!@#\\$%\\^\\&\\*\\(\\)_\\+\\=-\\[\\]~`]", "?");
-- FIXME: doesn't strip - or ] correctly
local result = string.gsub(str, "[^a-zA-Z0-9 ,\\./;'\\<>\\?:\\\"\\{\\}\\|!@#\\$%\\^\\&\\*\\(\\)_\\+\\=\\-\\[\\\n\\\t~`-]", "?");
return result
end

0 comments on commit 9791996

Please sign in to comment.