From 0359ddf99f3940794d51c206a30d51eac4c2f1d2 Mon Sep 17 00:00:00 2001
From: Přemysl Eric Janouch
Date: Thu, 25 May 2023 03:55:25 +0200
Subject: Add a tool to extract LibRaw file information
---
meson.build | 6 ++-
tools/rawinfo.c | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 159 insertions(+), 2 deletions(-)
create mode 100644 tools/rawinfo.c
diff --git a/meson.build b/meson.build
index 8571101..7de6be2 100644
--- a/meson.build
+++ b/meson.build
@@ -164,10 +164,12 @@ jpegcrop = executable('fiv-jpegcrop', 'fiv-jpegcrop.c', rc, config,
if get_option('tools').enabled()
# libjq 1.6 lacks a pkg-config file, and there is no release in sight.
# libjq 1.6 is required.
- tools_dependencies = [cc.find_library('jq'), dependency('libpng')]
+ tools_dependencies = [
+ cc.find_library('jq'), dependency('libpng'), dependency('libraw')]
tools_c_args = cc.get_supported_arguments(
'-Wno-unused-function', '-Wno-unused-parameter')
- foreach tool : ['pnginfo', 'jpeginfo', 'tiffinfo', 'webpinfo', 'bmffinfo']
+ foreach tool : [
+ 'pnginfo', 'rawinfo', 'jpeginfo', 'tiffinfo', 'webpinfo', 'bmffinfo']
executable(tool, 'tools/' + tool + '.c', tiff_tables,
dependencies : tools_dependencies,
c_args: tools_c_args)
diff --git a/tools/rawinfo.c b/tools/rawinfo.c
new file mode 100644
index 0000000..fa36bba
--- /dev/null
+++ b/tools/rawinfo.c
@@ -0,0 +1,155 @@
+//
+// rawinfo.c: acquire information about raw image files in JSON format
+//
+// Copyright (c) 2023, Přemysl Eric Janouch
+//
+// 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
+#include
+
+#if LIBRAW_VERSION < LIBRAW_MAKE_VERSION(0, 21, 0)
+#error LibRaw 0.21.0 or newer is required.
+#endif
+
+#include
+#include
+#include
+#include
+
+// --- Raw image files ---------------------------------------------------------
+// This is in principle similar to LibRaw's `raw-identify -v`,
+// but the output is machine-processable.
+
+static jv
+parse_raw(jv o, const uint8_t *p, size_t len)
+{
+ libraw_data_t *iprc = libraw_init(LIBRAW_OPIONS_NO_DATAERR_CALLBACK);
+ if (!iprc)
+ return add_error(o, "failed to obtain a LibRaw handle");
+
+ int err = 0;
+ if ((err = libraw_open_buffer(iprc, p, len))) {
+ libraw_close(iprc);
+ return add_error(o, libraw_strerror(err));
+ }
+
+ o = jv_set(o, jv_string("width"), jv_number(iprc->sizes.width));
+ o = jv_set(o, jv_string("height"), jv_number(iprc->sizes.height));
+ o = jv_set(o, jv_string("flip"), jv_number(iprc->sizes.flip));
+
+ // -> iprc->rawparams.shot_select
+ o = jv_set(o, jv_string("count"), jv_number(iprc->idata.raw_count));
+
+ jv thumbnails = jv_array();
+ for (int i = 0; i < iprc->thumbs_list.thumbcount; i++) {
+ libraw_thumbnail_item_t *item = iprc->thumbs_list.thumblist + i;
+
+ const char *format = "?";
+ switch (item->tformat) {
+ case LIBRAW_INTERNAL_THUMBNAIL_UNKNOWN:
+ format = "unknown";
+ break;
+ case LIBRAW_INTERNAL_THUMBNAIL_KODAK_THUMB:
+ format = "Kodak thumbnail";
+ break;
+ case LIBRAW_INTERNAL_THUMBNAIL_KODAK_YCBCR:
+ format = "Kodak YCbCr";
+ break;
+ case LIBRAW_INTERNAL_THUMBNAIL_KODAK_RGB:
+ format = "Kodak RGB";
+ break;
+ case LIBRAW_INTERNAL_THUMBNAIL_JPEG:
+ format = "JPEG";
+ break;
+ case LIBRAW_INTERNAL_THUMBNAIL_LAYER:
+ format = "layer";
+ break;
+ case LIBRAW_INTERNAL_THUMBNAIL_ROLLEI:
+ format = "Rollei";
+ break;
+ case LIBRAW_INTERNAL_THUMBNAIL_PPM:
+ format = "PPM";
+ break;
+ case LIBRAW_INTERNAL_THUMBNAIL_PPM16:
+ format = "PPM16";
+ break;
+ case LIBRAW_INTERNAL_THUMBNAIL_X3F:
+ format = "X3F";
+ break;
+ }
+
+ thumbnails = jv_array_append(thumbnails,
+ JV_OBJECT(jv_string("width"), jv_number(item->twidth),
+ jv_string("height"), jv_number(item->theight),
+ jv_string("flip"), jv_number(item->tflip),
+ jv_string("format"), jv_string(format)));
+ }
+
+ libraw_close(iprc);
+ return jv_set(o, jv_string("thumbnails"), thumbnails);
+}
+
+// --- I/O ---------------------------------------------------------------------
+
+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_raw(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 ./rawinfo
+ 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;
+}
--
cgit v1.2.3-70-g09d2