streamlet-examples/exiv2
branchv_0
changeset 45 f466b4c7d9b1
parent 40 a41423ac9e94
child 49 ab48ad4ecb91
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/streamlet-examples/exiv2	Sat Jan 18 20:09:34 2020 +0100
@@ -0,0 +1,80 @@
+#!/bin/bash
+
+# Relational pipes
+# Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, version 3 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+# This streamlet provides EXIF metadata. It calls the tool exiv2.
+# With no options it returns "Image size", "Copyright" and "Exif comment" attributes.
+# Specific attributes can be selected using options – e.g. --option 'attribute' 'Image size'
+# List of available attributes can be obtained by directly calling the exiv2 command on a image file.
+# Two additional attributes are provided by this streamlet: "Image height" and "Image width" (they are extracted from "Image size").
+
+
+. "$(dirname $0)/streamlet-common.sh"
+
+processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA() {
+	streamletFields=()
+
+	for (( i=0; i<${#optionNames[@]}; i++)); do
+		if [[ "x${optionNames[$i]}" == "xattribute" ]]; then
+			streamletFields+=("${optionValues[$i]}");
+		elif [[ "x${optionNames[$i]}" == "xprefix" ]]; then
+			pdfPrefix="${optionValues[$i]}";
+		else
+			echo "Unsupported option: ${optionNames[$i]}" >&2
+		fi
+	done
+
+	if [[ -z "$streamletFields" ]]; then
+		streamletFields=( "Image size" "Copyright" "Exif comment" );
+	fi
+
+	for (( i=0; i<${#streamletFields[@]}; i++)); do
+		if [[ "x${streamletFields[$i]}" == "xImage height" ]] ||[[ "x${streamletFields[$i]}" == "xImage width" ]]; then local type="integer"; else local type="string"; fi
+		send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[$i]-$pdfPrefix${streamletFields[$i]}}"    "$type"
+	done
+
+	send WAITING_FOR_INPUT_ATTRIBUTES
+}
+
+processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES() {
+	local streamletInfo streamletValid value isNull;
+
+	[[ -d "$currentFile" ]] || streamletInfo="$(exiv2 "$currentFile")";
+	streamletValid="$?";
+
+	for (( i=0; i<${#streamletFields[@]}; i++)); do
+		value="$(echo "$streamletInfo" | grep -P "^\Q${streamletFields[$i]}\E\s*:" | sed -E 's/[^:]+:\s+(.*)/\1/g' | tr -d '\n';)"; # the field name must not contain "\E"
+
+		if   [[ -z "$value" ]] && [[ "x${streamletFields[$i]}" == "xImage width"  ]]; then value="$(echo "$streamletInfo" | grep -E 'Image size\s*:\s*[0-9]+ x [0-9]+' | sed -E 's/Image size\s*:\s*([0-9]+) x ([0-9]+)/\1/g')";
+		elif [[ -z "$value" ]] && [[ "x${streamletFields[$i]}" == "xImage height" ]]; then value="$(echo "$streamletInfo" | grep -E 'Image size\s*:\s*[0-9]+ x [0-9]+' | sed -E 's/Image size\s*:\s*([0-9]+) x ([0-9]+)/\2/g')";
+		fi
+
+		# n.b. if file has no exif data, exiv2 exits with error „No Exif data found in the file“ and thus $streamletValid != 0, but there still might be some value like „Image size“
+		if   [[ ! "x$streamletValid" == "x0" ]] && [[ "x$value" == "x" ]]; then value="";  isNull="true";
+		else                                                                               isNull="false";
+		fi
+
+		if ( [[ "x${streamletFields[$i]}" == "xImage height" ]] || [[ "x${streamletFields[$i]}" == "xImage width" ]] ) && [[ ! "$value" =~ ^[0-9]+$ ]]; then value="0"; isNull="true"; fi
+
+		send OUTPUT_ATTRIBUTE "$value"    "$isNull";
+	done
+	
+	send WAITING_FOR_INPUT_ATTRIBUTES;
+}
+
+initialize
+processMessages