aboutsummaryrefslogtreecommitdiff
path: root/tools/tiffinfo.c
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2021-12-11 22:51:52 +0100
committerPřemysl Eric Janouch <p@janouch.name>2021-12-12 00:22:20 +0100
commit121c63e35e667a841380f4d405cc47ce0f50b2e9 (patch)
tree76d97392c8c6c88445c689313c026ce0817d6e75 /tools/tiffinfo.c
parent1bd5cb02e74fafa5f4f87be39cf919efab03fe25 (diff)
downloadfiv-121c63e35e667a841380f4d405cc47ce0f50b2e9.tar.gz
fiv-121c63e35e667a841380f4d405cc47ce0f50b2e9.tar.xz
fiv-121c63e35e667a841380f4d405cc47ce0f50b2e9.zip
Add a basic tiffinfo utility
Also fix a few TIFF-related issues.
Diffstat (limited to 'tools/tiffinfo.c')
-rw-r--r--tools/tiffinfo.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/tools/tiffinfo.c b/tools/tiffinfo.c
new file mode 100644
index 0000000..da629c6
--- /dev/null
+++ b/tools/tiffinfo.c
@@ -0,0 +1,79 @@
+//
+// tiffinfo.c: acquire information about TIFF files in JSON format
+//
+// Copyright (c) 2021, Přemysl Eric Janouch <p@janouch.name>
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+//
+
+#include "info.h"
+
+#include <jv.h>
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+
+// This is essentially the same as jpeginfo.c, but we only have an Exif segment.
+// TODO(p): Photoshop data and ICC profiles also have their tag,
+// they're not currently processed.
+
+static jv
+do_file(const char *filename, jv o)
+{
+ const char *err = NULL;
+ FILE *fp = fopen(filename, "rb");
+ if (!fp) {
+ err = strerror(errno);
+ goto error;
+ }
+
+ uint8_t *data = NULL, buf[256 << 10];
+ size_t n, len = 0;
+ while ((n = fread(buf, sizeof *buf, sizeof buf / sizeof *buf, fp))) {
+ data = realloc(data, len + n);
+ memcpy(data + len, buf, n);
+ len += n;
+ }
+ if (ferror(fp)) {
+ err = strerror(errno);
+ goto error_read;
+ }
+
+ o = parse_exif(o, data, len);
+
+error_read:
+ fclose(fp);
+ free(data);
+error:
+ if (err)
+ o = add_error(o, err);
+ return o;
+}
+
+int
+main(int argc, char *argv[])
+{
+ // XXX: Can't use `xargs -P0`, there's a risk of non-atomic writes.
+ // Usage: find . -iname *.png -print0 | xargs -0 ./pnginfo
+ for (int i = 1; i < argc; i++) {
+ const char *filename = argv[i];
+
+ jv o = jv_object();
+ o = jv_object_set(o, jv_string("filename"), jv_string(filename));
+ o = do_file(filename, o);
+ jv_dumpf(o, stdout, 0 /* Might consider JV_PRINT_SORTED. */);
+ fputc('\n', stdout);
+ }
+ return 0;
+}