File: ImageLoader.cpp

package info (click to toggle)
zxing-cpp 1.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,860 kB
  • sloc: cpp: 49,035; python: 150; javascript: 31; makefile: 11
file content (65 lines) | stat: -rw-r--r-- 1,448 bytes parent folder | download
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
/*
* Copyright 2016 Nu-book Inc.
* Copyright 2019 Axel Waggersauser.
*/
// SPDX-License-Identifier: Apache-2.0

#include "ImageLoader.h"

#include "BinaryBitmap.h"
#include "ImageView.h"

#include <map>
#include <memory>
#include <stdexcept>

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

namespace ZXing::Test {

class STBImage : public ImageView
{
	std::unique_ptr<stbi_uc[], void (*)(void*)> _memory;

public:
	STBImage() : ImageView(nullptr, 0, 0, ImageFormat::None), _memory(nullptr, stbi_image_free) {}

	void load(const fs::path& imgPath)
	{
		int width, height, colors;
		_memory.reset(stbi_load(imgPath.string().c_str(), &width, &height, &colors, 3));
		if (_memory == nullptr)
			throw std::runtime_error("Failed to read image");
#if 1
		auto* img = _memory.get();
		for (int i = 0; i < width * height; ++i )
			img[i] = RGBToLum(img[3 * i + 0], img[3 * i + 1], img[3 * i + 2]);
		ImageView::operator=({_memory.get(), width, height, ImageFormat::Lum});
#else
		ImageView::operator=({_memory.get(), width, height, ImageFormat::RGB});
#endif
	}

	operator bool() const { return _data; }
};

std::map<fs::path, STBImage> cache;

void ImageLoader::clearCache()
{
	cache.clear();
}

const ImageView& ImageLoader::load(const fs::path& imgPath)
{
	thread_local std::unique_ptr<BinaryBitmap> localAverage, threshold;

	auto& binImg = cache[imgPath];
	if (!binImg)
		binImg.load(imgPath);

	return binImg;
}

} // namespace ZXing::Test