From patchwork Mon Aug 24 20:46:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9376 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 C7AB3BD87C for ; Mon, 24 Aug 2020 20:46:57 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0A06862859; Mon, 24 Aug 2020 22:46:57 +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="fJF0oMkU"; dkim-atps=neutral Received: from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C3EB8616B1 for ; Mon, 24 Aug 2020 22:46:54 +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=OhuNKkqUEvX9FzUuIh7SR0Wks/BFouW4fm9gZcOJWg4=; b=fJF0oMkU8jUEDfIhODpVxbsMIiETmZm+yvBPg3RwnKxCmfbPRVy47/dZhhhye1u97L6w VVrnTGXhh+iHQH7a+noP+eG0X1NLJlvML2Tw7SaEoKf4oCxwCe9MGavt+Rnu8NocX4POVM DQL3p3cnb8Meqs5HYxyYFtzNs0KlAV7PM= Received: by filterdrecv-p3iad2-56dbc8bd54-vz96m with SMTP id filterdrecv-p3iad2-56dbc8bd54-vz96m-19-5F44273D-2 2020-08-24 20:46:53.177347258 +0000 UTC m=+17347.236853780 Received: from mail.uajain.com (unknown) by ismtpd0003p1hnd1.sendgrid.net (SG) with ESMTP id yWu89oa_TNqqlxtbQOIj1Q Mon, 24 Aug 2020 20:46:52.677 +0000 (UTC) From: Umang Jain Date: Mon, 24 Aug 2020 20:46:53 +0000 (UTC) Message-Id: <20200824204646.16866-2-email@uajain.com> In-Reply-To: <20200824204646.16866-1-email@uajain.com> References: <20200824204646.16866-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcOiinPhbAFGEz1ue/PLGCvHGErdCoYBmNq7elRDFh2kwCyjvYTbXH2m3OTx74SXnQ8kkuna6D3j92brEJSYfNdo8ZiXhbFkTVpIcE4pj3z1mPlMxDnsGQ3pm6igqfyZGb6A4XBT9Lu6x7uShdf3nFsT2nNF+v9Yp9Ws8SgLxx6JNH/7jn48cBb5nnguxmxDHAogLWYuyri8tx/j800gbqOw== To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH 1/3] 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 --- src/android/jpeg/exif.cpp | 176 ++++++++++++++++++++++++++++++++++++++ src/android/jpeg/exif.h | 45 ++++++++++ src/android/meson.build | 2 + 3 files changed, 223 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..f6a9f5c --- /dev/null +++ b/src/android/jpeg/exif.cpp @@ -0,0 +1,176 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * exif.cpp - EXIF tag creation and parser using libexif + */ + +#include "exif.h" + +#include "libcamera/internal/log.h" + +using namespace libcamera; + +LOG_DEFINE_CATEGORY(EXIF) + +Exif::Exif() + : valid_(false), exif_data_(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 (exif_data_) + free(exif_data_); + + 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) +{ + size_t length = item.length(); + + ExifEntry *entry = createEntry(ifd, tag, format, length, length); + if (!entry) { + LOG(EXIF, Error) << "Failed to add tag: " << tag; + return -ENOMEM; + } + + memcpy(entry->data, item.c_str(), length); + exif_entry_unref(entry); + + return 0; +} + +Span Exif::generate() +{ + if (exif_data_) { + free(exif_data_); + exif_data_ = nullptr; + } + + exif_data_save_data(data_, &exif_data_, &size_); + + LOG(EXIF, Debug) << "Created EXIF instance (" << size_ << " bytes)"; + + return { exif_data_, size_ }; +} + diff --git a/src/android/jpeg/exif.h b/src/android/jpeg/exif.h new file mode 100644 index 0000000..7df83c7 --- /dev/null +++ b/src/android/jpeg/exif.h @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * exif.h - EXIF tag creator and parser using libexif + */ +#ifndef __LIBCAMERA_EXIF_H__ +#define __LIBCAMERA_EXIF_H__ + +#include + +#include + +#include + +class Exif +{ +public: + Exif(); + ~Exif(); + + 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); + + libcamera::Span generate(); + unsigned char *data() const { return exif_data_; } + unsigned int size() const { return size_; } + +private: + ExifEntry *createEntry(ExifIfd ifd, ExifTag tag); + ExifEntry *createEntry(ExifIfd ifd, ExifTag tag, ExifFormat format, + uint64_t components, unsigned int size); + + bool valid_; + + ExifData *data_; + ExifMem *mem_; + + unsigned char *exif_data_; + unsigned int size_; +}; + +#endif /* __LIBCAMERA_EXIF_H__ */ diff --git a/src/android/meson.build b/src/android/meson.build index f7b81a4..0f49b25 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([ @@ -15,6 +16,7 @@ android_camera_metadata_sources = files([ android_deps = [ dependency('libjpeg'), + dependency('libexif'), ] android_camera_metadata = static_library('camera_metadata', From patchwork Mon Aug 24 20:46:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9377 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 BF529BD87C for ; Mon, 24 Aug 2020 20:46:58 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id BC44462849; Mon, 24 Aug 2020 22:46:57 +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="N5gJSM//"; dkim-atps=neutral Received: from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 54E8E62113 for ; Mon, 24 Aug 2020 22:46:55 +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=8/qcuugyt4BWpSoJ4t9q0NrplB2eQ4owPbxugcT20d8=; b=N5gJSM//BuKaLrKJ+3n8WpuYRlD4cP7G7m3PjRC1+jXeizUfsbmM0Fzma5JqCwEtXhJT RczbrHpdSyySxRznNFXNYRjmO/1RRI9Q01tYGDJvwiTOIdtJVUC0hdq42020/nyLNpPjZO NU8wRnZVozzOEskDvL2S9+hczDB7hRz28= Received: by filterdrecv-p3iad2-canary-d97c86fd5-k8f6p with SMTP id filterdrecv-p3iad2-canary-d97c86fd5-k8f6p-18-5F44273D-4F 2020-08-24 20:46:54.080920535 +0000 UTC m=+18539.274374053 Received: from mail.uajain.com (unknown) by ismtpd0001p1hnd1.sendgrid.net (SG) with ESMTP id 3W2yccn9TLiNS34kIun2Dg for ; Mon, 24 Aug 2020 20:46:53.758 +0000 (UTC) From: Umang Jain Date: Mon, 24 Aug 2020 20:46:54 +0000 (UTC) Message-Id: <20200824204646.16866-3-email@uajain.com> In-Reply-To: <20200824204646.16866-1-email@uajain.com> References: <20200824204646.16866-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcROc560Os82e1tQwAlD2FoKZ1gYInNEr2A12ZKoe3r+iLhS1vvkgcNXhQkusThCRA0/CDIz+Nxjf7yVPmFVbgnN6ROcO+aepCj5mVNhi51K3eJTIkQCsNjKnZQ71h1rMjJdj/OZgLJouyTRyrXMEIi6cgBwj4rEgGgbMStbw75m5Q7ioun+LDUZN/48uVUuP8/xdm4bmW3M+PRg14XRQGR/DCC7K0VBY2g6q0E6W5ps4= To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH 2/3] android: jpeg: Pass a Exif object while encoding 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" Create a Exif object when the jpeg encoder is about to start encoding the stream in CameraDevice::requestComplete(). This is done so that the CameraDevice's metadata (like Orientation) can be populated inside the Exif object and passed on to the encoder (which will populate FrameBuffer associated metadata). Signed-off-by: Umang Jain --- src/android/camera_device.cpp | 4 +++- src/android/jpeg/encoder.h | 5 ++++- src/android/jpeg/encoder_libjpeg.cpp | 3 ++- src/android/jpeg/encoder_libjpeg.h | 3 ++- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index de6f86f..bc5690e 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1434,7 +1434,9 @@ void CameraDevice::requestComplete(Request *request) continue; } - int jpeg_size = encoder->encode(buffer, mapped.maps()[0]); + Exif exif; + + int jpeg_size = encoder->encode(buffer, mapped.maps()[0], &exif); if (jpeg_size < 0) { LOG(HAL, Error) << "Failed to encode stream image"; status = CAMERA3_BUFFER_STATUS_ERROR; diff --git a/src/android/jpeg/encoder.h b/src/android/jpeg/encoder.h index f9eb88e..806cf4f 100644 --- a/src/android/jpeg/encoder.h +++ b/src/android/jpeg/encoder.h @@ -7,6 +7,8 @@ #ifndef __ANDROID_JPEG_ENCODER_H__ #define __ANDROID_JPEG_ENCODER_H__ +#include "exif.h" + #include #include #include @@ -18,7 +20,8 @@ public: virtual int configure(const libcamera::StreamConfiguration &cfg) = 0; virtual int encode(const libcamera::FrameBuffer *source, - const libcamera::Span &destination) = 0; + const libcamera::Span &destination, + Exif *exif) = 0; }; #endif /* __ANDROID_JPEG_ENCODER_H__ */ diff --git a/src/android/jpeg/encoder_libjpeg.cpp b/src/android/jpeg/encoder_libjpeg.cpp index 977538c..0cd93b6 100644 --- a/src/android/jpeg/encoder_libjpeg.cpp +++ b/src/android/jpeg/encoder_libjpeg.cpp @@ -180,7 +180,8 @@ void EncoderLibJpeg::compressNV(const libcamera::MappedBuffer *frame) } int EncoderLibJpeg::encode(const FrameBuffer *source, - const libcamera::Span &dest) + const libcamera::Span &dest, + Exif *exif) { MappedFrameBuffer frame(source, PROT_READ); if (!frame.isValid()) { diff --git a/src/android/jpeg/encoder_libjpeg.h b/src/android/jpeg/encoder_libjpeg.h index aed081a..7e0757a 100644 --- a/src/android/jpeg/encoder_libjpeg.h +++ b/src/android/jpeg/encoder_libjpeg.h @@ -22,7 +22,8 @@ public: int configure(const libcamera::StreamConfiguration &cfg) override; int encode(const libcamera::FrameBuffer *source, - const libcamera::Span &destination) override; + const libcamera::Span &destination, + Exif *exif) override; private: void compressRGB(const libcamera::MappedBuffer *frame); From patchwork Mon Aug 24 20:46:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9379 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 C961FBD87C for ; Mon, 24 Aug 2020 20:46:59 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 96C76616B1; Mon, 24 Aug 2020 22:46:59 +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="Jxmu8Zua"; dkim-atps=neutral Received: from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BF8036170D for ; Mon, 24 Aug 2020 22:46:56 +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=YE4ixvv5qRUxZRpkzwRMQjRGzw1v2wLVdDrVfoglVxU=; b=Jxmu8ZuahkLGINPfdBtt59EJd4FiXmBKz0IvNmEF331Is+Bvb9uwfFjKGjYXaiEdoqVJ icKVJ9H++77+NjBPnbGE48S6JgKQ2U/SyypHAOGvg8NCuhAoJwbbyBjbY7ZcMbpfh7j4S1 sdB6Au7EHTPhXgIYQUeMSgPsb1jT1v97M= Received: by filterdrecv-p3iad2-56dbc8bd54-l59qx with SMTP id filterdrecv-p3iad2-56dbc8bd54-l59qx-19-5F44273F-13 2020-08-24 20:46:55.37874524 +0000 UTC m=+17350.726504245 Received: from mail.uajain.com (unknown) by ismtpd0007p1hnd1.sendgrid.net (SG) with ESMTP id 8QH9Ia16Sci7ACwjKjgQcQ for ; Mon, 24 Aug 2020 20:46:55.011 +0000 (UTC) From: Umang Jain Date: Mon, 24 Aug 2020 20:46:55 +0000 (UTC) Message-Id: <20200824204646.16866-4-email@uajain.com> In-Reply-To: <20200824204646.16866-1-email@uajain.com> References: <20200824204646.16866-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcZj5z9XHfwy1Ge6f/0y1LYyVXr4TmHaUM1xCQSxitCYfrtK45qesLCVSSmeChF10FbpdTiZRXDISHkwM5Ty/eOSQHFDEHh2zvkwNkeclaO6aO/tSQy6EZ/e1UnnUWa2cgNyPSFnvUzWTDMOMBqZr5pRhmahjoirHSXYJgZORMmLZVQcHJ9vikeaeC9hgLIPMe8kHhHHEFCDiVm4MQTH7z3g== To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH 3/3] android: Support initial set of EXIF metadata tags 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" Support a initial set of EXIF metadata tags namely: Make, Model, Height, Width, Orientation and Timestamp. Each tag is set by a convenient high level helper API defined in exif.h. Signed-off-by: Umang Jain --- src/android/camera_device.cpp | 8 +++++++ src/android/jpeg/encoder_libjpeg.cpp | 12 ++++++++++ src/android/jpeg/exif.cpp | 34 ++++++++++++++++++++++++++++ src/android/jpeg/exif.h | 8 +++++++ 4 files changed, 62 insertions(+) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index bc5690e..fcf378a 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1435,6 +1435,14 @@ void CameraDevice::requestComplete(Request *request) } Exif exif; + /* + * \todo Discuss setMake String, maybe vendor ID? + * KB suggested to leave it to "libcamera" for now. + * setModel should use the 'model' property of the camera. + */ + exif.setMake("Libcamera"); + exif.setModel("cameraModel"); + exif.setOrientation(orientation_); int jpeg_size = encoder->encode(buffer, mapped.maps()[0], &exif); if (jpeg_size < 0) { diff --git a/src/android/jpeg/encoder_libjpeg.cpp b/src/android/jpeg/encoder_libjpeg.cpp index 0cd93b6..f84def9 100644 --- a/src/android/jpeg/encoder_libjpeg.cpp +++ b/src/android/jpeg/encoder_libjpeg.cpp @@ -213,7 +213,19 @@ int EncoderLibJpeg::encode(const FrameBuffer *source, else compressRGB(&frame); + exif->setWidth(compress_.image_width); + exif->setHeight(compress_.image_height); + exif->setTimestamp(source->metadata().timestamp); + + Span exif_data = exif->generate(); + jpeg_finish_compress(&compress_); + if (exif->size()) + /* Store Exif data in the JPEG_APP1 data block. */ + jpeg_write_marker(&compress_, JPEG_APP0 + 1, + static_cast(exif_data.data()), + exif_data.size()); + return size; } diff --git a/src/android/jpeg/exif.cpp b/src/android/jpeg/exif.cpp index f6a9f5c..b7591d5 100644 --- a/src/android/jpeg/exif.cpp +++ b/src/android/jpeg/exif.cpp @@ -160,6 +160,40 @@ int Exif::setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::stri return 0; } +int Exif::setHeight(uint16_t height) +{ + setShort(EXIF_IFD_0, EXIF_TAG_IMAGE_LENGTH, height); + setLong(EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION, height); + + return 0; +} + +int Exif::setWidth(uint16_t width) +{ + setShort(EXIF_IFD_0, EXIF_TAG_IMAGE_WIDTH, width); + setLong(EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION, width); + + return 0; +} + +int Exif::setTimestamp(const time_t timestamp) +{ + std::string ts(std::ctime(×tamp)); + int ret = setString(EXIF_IFD_0, EXIF_TAG_DATE_TIME, EXIF_FORMAT_ASCII, ts); + if(ret < 0) + return ret; + + ret = setString(EXIF_IFD_EXIF, EXIF_TAG_DATE_TIME_ORIGINAL, EXIF_FORMAT_ASCII, ts); + if(ret < 0) + return ret; + + ret = setString(EXIF_IFD_EXIF, EXIF_TAG_DATE_TIME_DIGITIZED, EXIF_FORMAT_ASCII, ts); + if(ret < 0) + return ret; + + return ret; +} + Span Exif::generate() { if (exif_data_) { diff --git a/src/android/jpeg/exif.h b/src/android/jpeg/exif.h index 7df83c7..6a8f5f5 100644 --- a/src/android/jpeg/exif.h +++ b/src/android/jpeg/exif.h @@ -11,6 +11,7 @@ #include +#include #include class Exif @@ -24,6 +25,13 @@ public: int setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::string &item); int setRational(ExifIfd ifd, ExifTag tag, uint32_t numerator, uint32_t denominator); + int setHeight(uint16_t height); + int setMake(const std::string &make) { return setString(EXIF_IFD_0, EXIF_TAG_MAKE, EXIF_FORMAT_ASCII, make); } + int setModel(const std::string &model) { return setString(EXIF_IFD_0, EXIF_TAG_MODEL, EXIF_FORMAT_ASCII, model); } + int setOrientation(int orientation) { return setShort(EXIF_IFD_0, EXIF_TAG_ORIENTATION, orientation); } + int setTimestamp(const time_t timestamp); + int setWidth(uint16_t width); + libcamera::Span generate(); unsigned char *data() const { return exif_data_; } unsigned int size() const { return size_; }