From patchwork Fri Aug 28 06:57:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9411 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 5CB26BDC71 for ; Fri, 28 Aug 2020 06:57:39 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2A28A62922; Fri, 28 Aug 2020 08:57:39 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="X3BuDKaU"; dkim-atps=neutral Received: from wrqvntvw.outbound-mail.sendgrid.net (wrqvntvw.outbound-mail.sendgrid.net [149.72.39.137]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 39F8D62919 for ; Fri, 28 Aug 2020 08:57:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uajain.com; h=from:subject:in-reply-to:references:mime-version:to:cc: content-transfer-encoding:content-type; s=s1; bh=lmu43f2D0fiDgcCnnKSc2BmAEZD100J3f2BT30jiXFE=; b=X3BuDKaUQ2ahpEb9eeJQTXOnZE8aTjlYp5kpVNs4ctLj/KQ5I/H4M5nQo3naJGG8D1/p ozpGFs5dB0/LTlgQWqsV9jGsPHL2pdZV8gmn+O1FEbJfDdLE8Hs29AMqZNPFZXNoJRArYW D9tE4sh+GQ8LF/ZTASmPGDIyBKr+86a48= Received: by filterdrecv-p3iad2-787bb5c9bc-jc7mb with SMTP id filterdrecv-p3iad2-787bb5c9bc-jc7mb-18-5F48AADE-46 2020-08-28 06:57:35.117886542 +0000 UTC m=+46666.866484751 Received: from mail.uajain.com (unknown) by ismtpd0002p1maa1.sendgrid.net (SG) with ESMTP id zyHjodtJT2GMLOj9e6paPg Fri, 28 Aug 2020 06:57:34.558 +0000 (UTC) From: Umang Jain Date: Fri, 28 Aug 2020 06:57:35 +0000 (UTC) Message-Id: <20200828065727.9909-2-email@uajain.com> In-Reply-To: <20200828065727.9909-1-email@uajain.com> References: <20200828065727.9909-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcytdNsKVIgBmp5SVZAuCwQdx1+49wwpvdvD0Pg/+j1if1YtEqeGdt73qOtY/plDjZ6h8nCTFQxr4k4iv7LMfhlFIAnDtu+e8uwbL2FtX0P1GBexKv3eTVVmLevgdznhHgk62BnkdxWTf/h2jegn78rjfT+BJG1Y6l0AGybKezlBTspSTHIOh+YVPPpwXzw/Rr4lLnO7ILwo42Gn7jY9xlmw== To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH v4 1/2] libcamera: android: Add EXIF infrastructure X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Kieran Bingham Provide helper classes to utilise the libexif interfaces and link against libexif to support tag additions when creating JPEG images. Signed-off-by: Kieran Bingham Signed-off-by: Umang Jain Reviewed-by: Kieran Bingham --- src/android/jpeg/exif.cpp | 172 ++++++++++++++++++++++++++++++++++++++ src/android/jpeg/exif.h | 44 ++++++++++ src/android/meson.build | 2 + 3 files changed, 218 insertions(+) create mode 100644 src/android/jpeg/exif.cpp create mode 100644 src/android/jpeg/exif.h diff --git a/src/android/jpeg/exif.cpp b/src/android/jpeg/exif.cpp new file mode 100644 index 0000000..b49b538 --- /dev/null +++ b/src/android/jpeg/exif.cpp @@ -0,0 +1,172 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * exif.cpp - EXIF tag creation using libexif + */ + +#include "exif.h" + +#include "libcamera/internal/log.h" + +using namespace libcamera; + +LOG_DEFINE_CATEGORY(EXIF) + +Exif::Exif() + : valid_(false), exifData_(0), size_(0) +{ + /* Create an ExifMem allocator to construct entries. */ + mem_ = exif_mem_new_default(); + if (!mem_) { + LOG(EXIF, Fatal) << "Failed to allocate ExifMem Allocator"; + return; + } + + data_ = exif_data_new_mem(mem_); + if (!data_) { + LOG(EXIF, Fatal) << "Failed to allocate an ExifData structure"; + return; + } + + valid_ = true; + + exif_data_set_option(data_, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION); + exif_data_set_data_type(data_, EXIF_DATA_TYPE_COMPRESSED); + + /* + * Big-Endian: EXIF_BYTE_ORDER_MOTOROLA + * Little Endian: EXIF_BYTE_ORDER_INTEL + */ + exif_data_set_byte_order(data_, EXIF_BYTE_ORDER_INTEL); + + /* Create the mandatory EXIF fields with default data. */ + exif_data_fix(data_); +} + +Exif::~Exif() +{ + if (exifData_) + free(exifData_); + + if (data_) + exif_data_unref(data_); + + if (mem_) + exif_mem_unref(mem_); +} + +ExifEntry *Exif::createEntry(ExifIfd ifd, ExifTag tag) +{ + ExifContent *content = data_->ifd[ifd]; + ExifEntry *entry = exif_content_get_entry(content, tag); + + if (entry) { + exif_entry_ref(entry); + return entry; + } + + entry = exif_entry_new_mem(mem_); + if (!entry) { + LOG(EXIF, Fatal) << "Failed to allocated new entry"; + return nullptr; + } + + entry->tag = tag; + + exif_content_add_entry(content, entry); + exif_entry_initialize(entry, tag); + + return entry; +} + +ExifEntry *Exif::createEntry(ExifIfd ifd, ExifTag tag, ExifFormat format, + uint64_t components, unsigned int size) +{ + ExifContent *content = data_->ifd[ifd]; + + /* Replace any existing entry with the same tag. */ + ExifEntry *existing = exif_content_get_entry(content, tag); + exif_content_remove_entry(content, existing); + + ExifEntry *entry = exif_entry_new_mem(mem_); + if (!entry) { + LOG(EXIF, Fatal) << "Failed to allocated new entry"; + return nullptr; + } + + void *buffer = exif_mem_alloc(mem_, size); + if (!buffer) { + LOG(EXIF, Fatal) << "Failed to allocate buffer for variable entry"; + exif_mem_unref(mem_); + return nullptr; + } + + entry->data = static_cast(buffer); + entry->components = components; + entry->format = format; + entry->size = size; + entry->tag = tag; + + exif_content_add_entry(content, entry); + + return entry; +} + +int Exif::setShort(ExifIfd ifd, ExifTag tag, uint16_t item) +{ + ExifEntry *entry = createEntry(ifd, tag); + + exif_set_short(entry->data, EXIF_BYTE_ORDER_INTEL, item); + exif_entry_unref(entry); + + return 0; +} + +int Exif::setLong(ExifIfd ifd, ExifTag tag, uint32_t item) +{ + ExifEntry *entry = createEntry(ifd, tag); + + exif_set_long(entry->data, EXIF_BYTE_ORDER_INTEL, item); + exif_entry_unref(entry); + + return 0; +} + +int Exif::setRational(ExifIfd ifd, ExifTag tag, uint32_t numerator, uint32_t denominator) +{ + ExifEntry *entry = createEntry(ifd, tag); + ExifRational item{ numerator, denominator }; + + exif_set_rational(entry->data, EXIF_BYTE_ORDER_INTEL, item); + exif_entry_unref(entry); + + return 0; +} + +int Exif::setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::string &item) +{ + /* Pad 1 extra byte for null-terminated string. */ + size_t length = item.length() + 1; + + ExifEntry *entry = createEntry(ifd, tag, format, length, length); + + memcpy(entry->data, item.c_str(), length); + exif_entry_unref(entry); + + return 0; +} + +int Exif::generate() +{ + if (exifData_) { + free(exifData_); + exifData_ = nullptr; + } + + exif_data_save_data(data_, &exifData_, &size_); + + LOG(EXIF, Debug) << "Created EXIF instance (" << size_ << " bytes)"; + + return 0; +} diff --git a/src/android/jpeg/exif.h b/src/android/jpeg/exif.h new file mode 100644 index 0000000..6c10f94 --- /dev/null +++ b/src/android/jpeg/exif.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * exif.h - EXIF tag creator using libexif + */ +#ifndef __ANDROID_JPEG_EXIF_H__ +#define __ANDROID_JPEG_EXIF_H__ + +#include + +#include + +#include + +class Exif +{ +public: + Exif(); + ~Exif(); + + libcamera::Span data() const { return { exifData_, size_ }; } + int generate(); + +private: + ExifEntry *createEntry(ExifIfd ifd, ExifTag tag); + ExifEntry *createEntry(ExifIfd ifd, ExifTag tag, ExifFormat format, + uint64_t components, unsigned int size); + + int setShort(ExifIfd ifd, ExifTag tag, uint16_t item); + int setLong(ExifIfd ifd, ExifTag tag, uint32_t item); + int setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::string &item); + int setRational(ExifIfd ifd, ExifTag tag, uint32_t numerator, uint32_t denominator); + + bool valid_; + + ExifData *data_; + ExifMem *mem_; + + unsigned char *exifData_; + unsigned int size_; +}; + +#endif /* __ANDROID_JPEG_EXIF_H__ */ diff --git a/src/android/meson.build b/src/android/meson.build index f7b81a4..ecb92f6 100644 --- a/src/android/meson.build +++ b/src/android/meson.build @@ -7,6 +7,7 @@ android_hal_sources = files([ 'camera_metadata.cpp', 'camera_ops.cpp', 'jpeg/encoder_libjpeg.cpp', + 'jpeg/exif.cpp', ]) android_camera_metadata_sources = files([ @@ -14,6 +15,7 @@ android_camera_metadata_sources = files([ ]) android_deps = [ + dependency('libexif'), dependency('libjpeg'), ]