summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon/HiresTextures.cpp
blob: 938e8cc5aa911553173e584644b60b10d25dbb1c (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.

#include <algorithm>
#include <cstring>
#include <string>
#include <utility>
#include <SOIL/SOIL.h>

#include "Common/CommonPaths.h"
#include "Common/FileSearch.h"
#include "Common/FileUtil.h"
#include "Common/StringUtil.h"

#include "VideoCommon/HiresTextures.h"

namespace HiresTextures
{

std::map<std::string, std::string> textureMap;

void Init(const std::string& gameCode)
{
	textureMap.clear();

	CFileSearch::XStringVector Directories;

	std::string szDir = StringFromFormat("%s%s", File::GetUserPath(D_HIRESTEXTURES_IDX).c_str(), gameCode.c_str());
	Directories.push_back(szDir);

	for (u32 i = 0; i < Directories.size(); i++)
	{
		File::FSTEntry FST_Temp;
		File::ScanDirectoryTree(Directories[i], FST_Temp);
		for (auto& entry : FST_Temp.children)
		{
			if (entry.isDirectory)
			{
				bool duplicate = false;

				for (auto& Directory : Directories)
				{
					if (Directory == entry.physicalName)
					{
						duplicate = true;
						break;
					}
				}

				if (!duplicate)
					Directories.push_back(entry.physicalName);
			}
		}
	}

	CFileSearch::XStringVector Extensions = {
		"*.png",
		"*.bmp",
		"*.tga",
		"*.dds",
		"*.jpg" // Why not? Could be useful for large photo-like textures
	};

	CFileSearch FileSearch(Extensions, Directories);
	const CFileSearch::XStringVector& rFilenames = FileSearch.GetFileNames();

	const std::string code = StringFromFormat("%s_", gameCode.c_str());

	if (rFilenames.size() > 0)
	{
		for (auto& rFilename : rFilenames)
		{
			std::string FileName;
			SplitPath(rFilename, nullptr, &FileName, nullptr);

			if (FileName.substr(0, code.length()).compare(code) == 0 && textureMap.find(FileName) == textureMap.end())
				textureMap.insert(std::map<std::string, std::string>::value_type(FileName, rFilename));
		}
	}
}

bool HiresTexExists(const std::string& filename)
{
	return textureMap.find(filename) != textureMap.end();
}

PC_TexFormat GetHiresTex(const std::string& filename, unsigned int* pWidth, unsigned int* pHeight, unsigned int* required_size, int texformat, unsigned int data_size, u8* data)
{
	if (textureMap.find(filename) == textureMap.end())
		return PC_TEX_FMT_NONE;

	int width;
	int height;
	int channels;

	u8 *temp = SOIL_load_image(textureMap[filename].c_str(), &width, &height, &channels, SOIL_LOAD_RGBA);
	if (temp == nullptr)
	{
		ERROR_LOG(VIDEO, "Custom texture %s failed to load", textureMap[filename].c_str());
		return PC_TEX_FMT_NONE;
	}

	*pWidth = width;
	*pHeight = height;

	int offset = 0;
	PC_TexFormat returnTex = PC_TEX_FMT_NONE;

	switch (texformat)
	{
	case GX_TF_I4:
	case GX_TF_I8:
	case GX_TF_IA4:
	case GX_TF_IA8:
		*required_size = width * height * 8;
		if (data_size < *required_size)
			goto cleanup;

		for (int i = 0; i < width * height * 4; i += 4)
		{
			// Rather than use a luminosity function, just use the most intense color for luminance
			// TODO(neobrain): Isn't this kind of.. stupid?
			data[offset++] = *std::max_element(temp+i, temp+i+3);
			data[offset++] = temp[i+3];
		}
		returnTex = PC_TEX_FMT_IA8;
		break;
	default:
		*required_size = width * height * 4;
		if (data_size < *required_size)
			goto cleanup;

		memcpy(data, temp, width * height * 4);
		returnTex = PC_TEX_FMT_RGBA32;
		break;
	}

	INFO_LOG(VIDEO, "Loading custom texture from %s", textureMap[filename].c_str());
cleanup:
	SOIL_free_image_data(temp);
	return returnTex;
}

}