summaryrefslogtreecommitdiff
path: root/src/android/jpeg/exif.h
blob: b0d614027ede351278bb5e03a498c643a6ce3443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* SPDX-License-Identifier: LGPL-2.1-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 <chrono>
#include <string>
#include <time.h>

#include <libexif/exif-data.h>

#include <libcamera/geometry.h>
#include <libcamera/span.h>

class Exif
{
public:
	Exif();
	~Exif();

	enum Compression {
		None = 1,
		JPEG = 6,
	};

	enum Flash {
		/* bit 0 */
		Fired = 0x01,
		/* bits 1 and 2 */
		StrobeDetected = 0x04,
		StrobeNotDetected = 0x06,
		/* bits 3 and 4 */
		ModeCompulsoryFiring = 0x08,
		ModeCompulsorySuppression = 0x10,
		ModeAuto = 0x18,
		/* bit 5 */
		FlashNotPresent = 0x20,
		/* bit 6 */
		RedEye = 0x40,
	};

	enum WhiteBalance {
		Auto = 0,
		Manual = 1,
	};

	enum StringEncoding {
		NoEncoding = 0,
		ASCII = 1,
		Unicode = 2,
	};

	void setMake(const std::string &make);
	void setModel(const std::string &model);

	void setOrientation(int orientation);
	void setSize(const libcamera::Size &size);
	void setThumbnail(libcamera::Span<const unsigned char> thumbnail,
			  Compression compression);
	void setTimestamp(time_t timestamp, std::chrono::milliseconds msec);

	void setGPSDateTimestamp(time_t timestamp);
	void setGPSLocation(const double *coords);
	void setGPSMethod(const std::string &method);

	void setFocalLength(float length);
	void setExposureTime(uint64_t nsec);
	void setAperture(float size);
	void setISO(uint16_t iso);
	void setFlash(Flash flash);
	void setWhiteBalance(WhiteBalance wb);

	libcamera::Span<const uint8_t> data() const { return { exifData_, size_ }; }
	[[nodiscard]] int generate();

private:
	ExifEntry *createEntry(ExifIfd ifd, ExifTag tag);
	ExifEntry *createEntry(ExifIfd ifd, ExifTag tag, ExifFormat format,
			       unsigned long components, unsigned int size);

	void setByte(ExifIfd ifd, ExifTag tag, uint8_t item);
	void setShort(ExifIfd ifd, ExifTag tag, uint16_t item);
	void setLong(ExifIfd ifd, ExifTag tag, uint32_t item);
	void setString(ExifIfd ifd, ExifTag tag, ExifFormat format,
		       const std::string &item,
		       StringEncoding encoding = NoEncoding);
	void setRational(ExifIfd ifd, ExifTag tag, ExifRational item);

	std::tuple<int, int, int> degreesToDMS(double decimalDegrees);
	void setGPSDMS(ExifIfd ifd, ExifTag tag, int deg, int min, int sec);

	std::u16string utf8ToUtf16(const std::string &str);

	bool valid_;

	ExifData *data_;
	ExifMem *mem_;
	ExifByteOrder order_;

	unsigned char *exifData_;
	unsigned int size_;
};

#endif /* __ANDROID_JPEG_EXIF_H__ */
n> ct_ccm.ct = p.second.get<double>("ct"); ct_ccm.ccm.Read(p.second.get_child("ccm")); if (!config_.ccms.empty() && ct_ccm.ct <= config_.ccms.back().ct) throw std::runtime_error( "Ccm: CCM not in increasing colour temperature order"); config_.ccms.push_back(std::move(ct_ccm)); } if (config_.ccms.empty()) throw std::runtime_error("Ccm: no CCMs specified"); } void Ccm::SetSaturation(double saturation) { saturation_ = saturation; } void Ccm::Initialise() {} template<typename T> static bool get_locked(Metadata *metadata, std::string const &tag, T &value) { T *ptr = metadata->GetLocked<T>(tag); if (ptr == nullptr) return false; value = *ptr; return true; } Matrix calculate_ccm(std::vector<CtCcm> const &ccms, double ct) { if (ct <= ccms.front().ct) return ccms.front().ccm; else if (ct >= ccms.back().ct) return ccms.back().ccm; else { int i = 0; for (; ct > ccms[i].ct; i++) ; double lambda = (ct - ccms[i - 1].ct) / (ccms[i].ct - ccms[i - 1].ct); return lambda * ccms[i].ccm + (1.0 - lambda) * ccms[i - 1].ccm; } } Matrix apply_saturation(Matrix const &ccm, double saturation) { Matrix RGB2Y(0.299, 0.587, 0.114, -0.169, -0.331, 0.500, 0.500, -0.419, -0.081); Matrix Y2RGB(1.000, 0.000, 1.402, 1.000, -0.345, -0.714, 1.000, 1.771, 0.000); Matrix S(1, 0, 0, 0, saturation, 0, 0, 0, saturation); return Y2RGB * S * RGB2Y * ccm; } void Ccm::Prepare(Metadata *image_metadata) { bool awb_ok = false, lux_ok = false; struct AwbStatus awb = {}; awb.temperature_K = 4000; // in case no metadata struct LuxStatus lux = {}; lux.lux = 400; // in case no metadata { // grab mutex just once to get everything std::lock_guard<Metadata> lock(*image_metadata); awb_ok = get_locked(image_metadata, "awb.status", awb); lux_ok = get_locked(image_metadata, "lux.status", lux); } if (!awb_ok) RPI_WARN("Ccm: no colour temperature found"); if (!lux_ok) RPI_WARN("Ccm: no lux value found"); Matrix ccm = calculate_ccm(config_.ccms, awb.temperature_K); double saturation = saturation_; struct CcmStatus ccm_status; ccm_status.saturation = saturation; if (!config_.saturation.Empty()) saturation *= config_.saturation.Eval( config_.saturation.Domain().Clip(lux.lux)); ccm = apply_saturation(ccm, saturation); for (int j = 0; j < 3; j++) for (int i = 0; i < 3; i++) ccm_status.matrix[j * 3 + i] = std::max(-8.0, std::min(7.9999, ccm.m[j][i])); RPI_LOG("CCM: colour temperature " << awb.temperature_K << "K"); RPI_LOG("CCM: " << ccm_status.matrix[0] << " " << ccm_status.matrix[1] << " " << ccm_status.matrix[2] << " " << ccm_status.matrix[3] << " " << ccm_status.matrix[4] << " " << ccm_status.matrix[5] << " " << ccm_status.matrix[6] << " " << ccm_status.matrix[7] << " " << ccm_status.matrix[8]); image_metadata->Set("ccm.status", ccm_status); } // Register algorithm with the system. static Algorithm *Create(Controller *controller) { return (Algorithm *)new Ccm(controller); ; } static RegisterAlgorithm reg(NAME, &Create);