summaryrefslogtreecommitdiff
path: root/binary_data.cpp
blob: 27fc9fe86061f45813058d81a1dbf1c81db29fa6 (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
/* SPDX-License-Identifier: Apache-2.0 */
/*
 * Copyright (C) 2021, Google Inc.
 *
 * binary_data.cpp - AIQ Binary Data Wrapper
 */

#include "binary_data.h"

#include <libcamera/base/file.h>
#include <libcamera/base/log.h>

namespace libcamera::ipa::ipu3 {

LOG_DEFINE_CATEGORY(AIBD)

/**
 * \class BinaryData
 * \brief Binary Data wrapper
 *
 * Loads data from a file, and returns it as an ia_binary_data type.
 * Data is freed automatically when the object goes out of scope.
 */

BinaryData::BinaryData()
{
	iaBinaryData_.data = nullptr;
	iaBinaryData_.size = 0;
}

int BinaryData::load(const char *filename)
{
	File binary(filename);

	if (!binary.exists()) {
		LOG(AIBD, Error) << "Failed to find file: " << filename;
		return -ENOENT;
	}

	if (!binary.open(File::OpenModeFlag::ReadOnly)) {
		LOG(AIBD, Error) << "Failed to open: " << filename;
		return -EINVAL;
	}

	ssize_t fileSize = binary.size();
	if (fileSize < 0) {
		LOG(AIBD, Error) << "Failed to determine fileSize: " << filename;
		return -ENODATA;
	}

	data_.resize(fileSize);

	int bytesRead = binary.read(data_);
	if (bytesRead != fileSize) {
		LOG(AIBD, Error) << "Failed to read file: " << filename;
		return -EINVAL;
	}

	iaBinaryData_.data = data_.data();
	iaBinaryData_.size = fileSize;

	LOG(AIBD, Info) << "Successfully loaded: " << filename;

	return 0;
}

} /* namespace libcamera::ipa::ipu3 */