File: TextureLoaderQoi.cpp

package info (click to toggle)
jazz2-native 3.5.0-1
  • links: PTS, VCS
  • area: contrib
  • in suites:
  • size: 16,836 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (51 lines) | stat: -rw-r--r-- 1,122 bytes parent folder | download | duplicates (3)
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
#include "TextureLoaderQoi.h"

#if defined(WITH_QOI)

#define QOI_IMPLEMENTATION
#define QOI_DECODE_ONLY
#define QOI_NO_STDIO
#include <qoi.h>

using namespace Death::IO;

namespace nCine
{
	TextureLoaderQoi::TextureLoaderQoi(std::unique_ptr<Stream> fileHandle)
		: ITextureLoader(std::move(fileHandle))
	{
		if (!fileHandle_->IsValid()) {
			return;
		}

		auto fileSize = fileHandle_->GetSize();
		if (fileSize < QOI_HEADER_SIZE || fileSize > 64 * 1024 * 1024) {
			// 64 MB file size limit, files are usually smaller than 1MB
			return;
		}

		auto buffer = std::make_unique<char[]>(fileSize);
		fileHandle_->Read(buffer.get(), fileSize);

		qoi_desc desc = { };
		void* data = qoi_decode(buffer.get(), fileSize, &desc, 4);
		if (data == nullptr) {
			return;
		}

		int imageSize = desc.width * desc.height * desc.channels;
		pixels_ = std::make_unique<GLubyte[]>(imageSize);
		// TODO: remove this additional copy
		memcpy(pixels_.get(), data, imageSize);
		QOI_FREE(data);

		width_ = desc.width;
		height_ = desc.height;
		mipMapCount_ = 1;
		texFormat_ = TextureFormat(GL_RGBA8);

		hasLoaded_ = true;
	}
}

#endif