[{"id":12393,"web_url":"https://patchwork.libcamera.org/comment/12393/","msgid":"<20200910035037.GD4009@pendragon.ideasonboard.com>","date":"2020-09-10T03:50:37","subject":"Re: [libcamera-devel] [PATCH v7 1/2] android: jpeg: Add EXIF\n\tinfrastructure","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Umang,\n\nThank you for the patch.\n\nOn Wed, Sep 09, 2020 at 04:44:44PM +0530, Umang Jain wrote:\n> From: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> \n> Provide helper classes to utilise the libexif interfaces and link\n> against libexif to support tag additions when creating JPEG images.\n> \n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> Signed-off-by: Umang Jain <email@uajain.com>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n>  src/android/jpeg/exif.cpp | 188 ++++++++++++++++++++++++++++++++++++++\n>  src/android/jpeg/exif.h   |  46 ++++++++++\n>  src/android/meson.build   |   2 +\n>  3 files changed, 236 insertions(+)\n>  create mode 100644 src/android/jpeg/exif.cpp\n>  create mode 100644 src/android/jpeg/exif.h\n> \n> diff --git a/src/android/jpeg/exif.cpp b/src/android/jpeg/exif.cpp\n> new file mode 100644\n> index 0000000..41ddf48\n> --- /dev/null\n> +++ b/src/android/jpeg/exif.cpp\n> @@ -0,0 +1,188 @@\n> +/* SPDX-License-Identifier: GPL-2.0-or-later */\n> +/*\n> + * Copyright (C) 2020, Google Inc.\n> + *\n> + * exif.cpp - EXIF tag creation using libexif\n> + */\n> +\n> +#include \"exif.h\"\n> +\n> +#include \"libcamera/internal/log.h\"\n> +\n> +using namespace libcamera;\n> +\n> +LOG_DEFINE_CATEGORY(EXIF)\n> +\n> +/*\n> + * The Exif class should be instantiated and specific properties set\n> + * through the exposed public API.\n> + *\n> + * Once all desired properties have been set, the user shall call\n> + * generate() to process the entries and generate the Exif data.\n> + *\n> + * Calls to generate() must check the return code to determine if any error\n> + * occurred during the construction of the Exif data, and if successful the\n> + * data can be obtained using the data() method.\n> + */\n> +Exif::Exif()\n> +\t: valid_(false), data_(nullptr), exifData_(0), size_(0)\n> +{\n> +\t/* Create an ExifMem allocator to construct entries. */\n> +\tmem_ = exif_mem_new_default();\n> +\tif (!mem_) {\n> +\t\tLOG(EXIF, Error) << \"Failed to allocate ExifMem Allocator\";\n> +\t\treturn;\n> +\t}\n> +\n> +\tdata_ = exif_data_new_mem(mem_);\n> +\tif (!data_) {\n> +\t\tLOG(EXIF, Error) << \"Failed to allocate an ExifData structure\";\n> +\t\treturn;\n> +\t}\n> +\n> +\tvalid_ = true;\n> +\n> +\texif_data_set_option(data_, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);\n> +\texif_data_set_data_type(data_, EXIF_DATA_TYPE_COMPRESSED);\n> +\n> +\t/*\n> +\t * Big-Endian: EXIF_BYTE_ORDER_MOTOROLA\n> +\t * Little Endian: EXIF_BYTE_ORDER_INTEL\n> +\t */\n> +\texif_data_set_byte_order(data_, EXIF_BYTE_ORDER_INTEL);\n> +\n> +\t/* Create the mandatory EXIF fields with default data. */\n> +\texif_data_fix(data_);\n> +}\n> +\n> +Exif::~Exif()\n> +{\n> +\tif (exifData_)\n> +\t\tfree(exifData_);\n> +\n> +\tif (data_)\n> +\t\texif_data_unref(data_);\n> +\n> +\tif (mem_)\n> +\t\texif_mem_unref(mem_);\n> +}\n> +\n> +ExifEntry *Exif::createEntry(ExifIfd ifd, ExifTag tag)\n> +{\n> +\tExifContent *content = data_->ifd[ifd];\n> +\tExifEntry *entry = exif_content_get_entry(content, tag);\n> +\n> +\tif (entry) {\n> +\t\texif_entry_ref(entry);\n> +\t\treturn entry;\n> +\t}\n> +\n> +\tentry = exif_entry_new_mem(mem_);\n> +\tif (!entry) {\n> +\t\tLOG(EXIF, Error) << \"Failed to allocated new entry\";\n> +\t\tvalid_ = false;\n> +\t\treturn nullptr;\n> +\t}\n> +\n> +\texif_content_add_entry(content, entry);\n> +\texif_entry_initialize(entry, tag);\n> +\n> +\treturn entry;\n> +}\n> +\n> +ExifEntry *Exif::createEntry(ExifIfd ifd, ExifTag tag, ExifFormat format,\n> +\t\t\t      unsigned long components, unsigned int size)\n\nThere's an indentation issue here. I'll fix when applying.\n\n> +{\n> +\tExifContent *content = data_->ifd[ifd];\n> +\n> +\t/* Replace any existing entry with the same tag. */\n> +\tExifEntry *existing = exif_content_get_entry(content, tag);\n> +\texif_content_remove_entry(content, existing);\n> +\n> +\tExifEntry *entry = exif_entry_new_mem(mem_);\n> +\tif (!entry) {\n> +\t\tLOG(EXIF, Error) << \"Failed to allocated new entry\";\n> +\t\tvalid_ = false;\n> +\t\treturn nullptr;\n> +\t}\n> +\n> +\tvoid *buffer = exif_mem_alloc(mem_, size);\n> +\tif (!buffer) {\n> +\t\tLOG(EXIF, Error) << \"Failed to allocate buffer for variable entry\";\n> +\t\texif_mem_unref(mem_);\n> +\t\tvalid_ = false;\n> +\t\treturn nullptr;\n> +\t}\n> +\n> +\tentry->data = static_cast<unsigned char *>(buffer);\n> +\tentry->components = components;\n> +\tentry->format = format;\n> +\tentry->size = size;\n> +\tentry->tag = tag;\n> +\n> +\texif_content_add_entry(content, entry);\n> +\n> +\treturn entry;\n> +}\n> +\n> +void Exif::setShort(ExifIfd ifd, ExifTag tag, uint16_t item)\n> +{\n> +\tExifEntry *entry = createEntry(ifd, tag);\n> +\tif (!entry)\n> +\t\treturn;\n> +\n> +\texif_set_short(entry->data, EXIF_BYTE_ORDER_INTEL, item);\n> +\texif_entry_unref(entry);\n> +}\n> +\n> +void Exif::setLong(ExifIfd ifd, ExifTag tag, uint32_t item)\n> +{\n> +\tExifEntry *entry = createEntry(ifd, tag);\n> +\tif (!entry)\n> +\t\treturn;\n> +\n> +\texif_set_long(entry->data, EXIF_BYTE_ORDER_INTEL, item);\n> +\texif_entry_unref(entry);\n> +}\n> +\n> +void Exif::setRational(ExifIfd ifd, ExifTag tag, ExifRational item)\n> +{\n> +\tExifEntry *entry = createEntry(ifd, tag);\n> +\tif (!entry)\n> +\t\treturn;\n> +\n> +\texif_set_rational(entry->data, EXIF_BYTE_ORDER_INTEL, item);\n> +\texif_entry_unref(entry);\n> +}\n> +\n> +void Exif::setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::string &item)\n> +{\n> +\t/* Pad 1 extra byte for null-terminated string. */\n> +\tsize_t length = item.length() + 1;\n> +\n> +\tExifEntry *entry = createEntry(ifd, tag, format, length, length);\n> +\tif (!entry)\n> +\t\treturn;\n> +\n> +\tmemcpy(entry->data, item.c_str(), length);\n> +\texif_entry_unref(entry);\n> +}\n> +\n> +[[nodiscard]] int Exif::generate()\n> +{\n> +\tif (exifData_) {\n> +\t\tfree(exifData_);\n> +\t\texifData_ = nullptr;\n> +\t}\n> +\n> +\tif (!valid_) {\n> +\t\tLOG(EXIF, Error) << \"Generated EXIF data is invalid\";\n> +\t\treturn -1;\n> +\t}\n> +\n> +\texif_data_save_data(data_, &exifData_, &size_);\n> +\n> +\tLOG(EXIF, Debug) << \"Created EXIF instance (\" << size_ << \" bytes)\";\n> +\n> +\treturn 0;\n> +}\n> diff --git a/src/android/jpeg/exif.h b/src/android/jpeg/exif.h\n> new file mode 100644\n> index 0000000..8dfc324\n> --- /dev/null\n> +++ b/src/android/jpeg/exif.h\n> @@ -0,0 +1,46 @@\n> +/* SPDX-License-Identifier: GPL-2.0-or-later */\n> +/*\n> + * Copyright (C) 2020, Google Inc.\n> + *\n> + * exif.h - EXIF tag creator using libexif\n> + */\n> +#ifndef __ANDROID_JPEG_EXIF_H__\n> +#define __ANDROID_JPEG_EXIF_H__\n> +\n> +#include <ctime>\n> +#include <string>\n> +\n> +#include <libexif/exif-data.h>\n> +\n> +#include <libcamera/span.h>\n> +\n> +class Exif\n> +{\n> +public:\n> +\tExif();\n> +\t~Exif();\n> +\n> +\tlibcamera::Span<const uint8_t> data() const { return { exifData_, size_ }; }\n> +\t[[nodiscard]] int generate();\n> +\n> +private:\n> +\tExifEntry *createEntry(ExifIfd ifd, ExifTag tag);\n> +\tExifEntry *createEntry(ExifIfd ifd, ExifTag tag, ExifFormat format,\n> +\t\t\t       unsigned long components, unsigned int size);\n> +\n> +\tvoid setShort(ExifIfd ifd, ExifTag tag, uint16_t item);\n> +\tvoid setLong(ExifIfd ifd, ExifTag tag, uint32_t item);\n> +\tvoid setString(ExifIfd ifd, ExifTag tag, ExifFormat format,\n> +\t\t       const std::string &item);\n> +\tvoid setRational(ExifIfd ifd, ExifTag tag, ExifRational item);\n> +\n> +\tbool valid_;\n> +\n> +\tExifData *data_;\n> +\tExifMem *mem_;\n> +\n> +\tunsigned char *exifData_;\n> +\tunsigned int size_;\n> +};\n> +\n> +#endif /* __ANDROID_JPEG_EXIF_H__ */\n> diff --git a/src/android/meson.build b/src/android/meson.build\n> index f7b81a4..ecb92f6 100644\n> --- a/src/android/meson.build\n> +++ b/src/android/meson.build\n> @@ -7,6 +7,7 @@ android_hal_sources = files([\n>      'camera_metadata.cpp',\n>      'camera_ops.cpp',\n>      'jpeg/encoder_libjpeg.cpp',\n> +    'jpeg/exif.cpp',\n>  ])\n>  \n>  android_camera_metadata_sources = files([\n> @@ -14,6 +15,7 @@ android_camera_metadata_sources = files([\n>  ])\n>  \n>  android_deps = [\n> +    dependency('libexif'),\n>      dependency('libjpeg'),\n>  ]\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 22A25BDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 10 Sep 2020 03:51:07 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9C69962B90;\n\tThu, 10 Sep 2020 05:51:06 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8414F6037E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 10 Sep 2020 05:51:04 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id D6D9239;\n\tThu, 10 Sep 2020 05:51:03 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"J4rKYpiH\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1599709864;\n\tbh=5H8RdVwhvw/7BfFa5XRnuFKbdHCjvbR+Qrrdmpb5r4g=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=J4rKYpiHyn5BoUs0df39CS11OV8bdhkvtUtFXi10IPefZWwqkEj79ir6a6UM+7VPe\n\tcvWhhu0Hvu8AC5yiiXQq+VI186zsOtgcm+NDItwwc/FHe+0I/0yd49qc6N3IjZeDd2\n\trdYYRKxbUUbU4CXkweuOPQnE3dq4hAtXZnuuh4fs=","Date":"Thu, 10 Sep 2020 06:50:37 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Umang Jain <email@uajain.com>","Message-ID":"<20200910035037.GD4009@pendragon.ideasonboard.com>","References":"<20200909111445.2908-1-email@uajain.com>\n\t<20200909111445.2908-2-email@uajain.com>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<20200909111445.2908-2-email@uajain.com>","Subject":"Re: [libcamera-devel] [PATCH v7 1/2] android: jpeg: Add EXIF\n\tinfrastructure","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Cc":"libcamera-devel@lists.libcamera.org","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]