| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | #include <cstring>
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | #include <utility>
 | 
					
						
							| 
									
										
										
										
											2010-03-09 22:17:33 +00:00
										 |  |  | #include <SOIL/SOIL.h>
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "Common/CommonPaths.h"
 | 
					
						
							|  |  |  | #include "Common/FileSearch.h"
 | 
					
						
							|  |  |  | #include "Common/FileUtil.h"
 | 
					
						
							|  |  |  | #include "Common/StringUtil.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "VideoCommon/HiresTextures.h"
 | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace HiresTextures | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-08 15:58:25 +02:00
										 |  |  | static std::map<std::string, std::string> textureMap; | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | void Init(const std::string& gameCode) | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-05 10:08:06 +00:00
										 |  |  | 	textureMap.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 	CFileSearch::XStringVector Directories; | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 	std::string szDir = StringFromFormat("%s%s", File::GetUserPath(D_HIRESTEXTURES_IDX).c_str(), gameCode.c_str()); | 
					
						
							|  |  |  | 	Directories.push_back(szDir); | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (u32 i = 0; i < Directories.size(); i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		File::FSTEntry FST_Temp; | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		File::ScanDirectoryTree(Directories[i], FST_Temp); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 		for (auto& entry : FST_Temp.children) | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 			if (entry.isDirectory) | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				bool duplicate = false; | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 				for (auto& Directory : Directories) | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 					if (Directory == entry.physicalName) | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 					{ | 
					
						
							|  |  |  | 						duplicate = true; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 				if (!duplicate) | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 					Directories.push_back(entry.physicalName); | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 	CFileSearch::XStringVector Extensions = { | 
					
						
							|  |  |  | 		"*.png", | 
					
						
							|  |  |  | 		"*.bmp", | 
					
						
							|  |  |  | 		"*.tga", | 
					
						
							|  |  |  | 		"*.dds", | 
					
						
							|  |  |  | 		"*.jpg" // Why not? Could be useful for large photo-like textures
 | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	CFileSearch FileSearch(Extensions, Directories); | 
					
						
							|  |  |  | 	const CFileSearch::XStringVector& rFilenames = FileSearch.GetFileNames(); | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	const std::string code = StringFromFormat("%s_", gameCode.c_str()); | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (rFilenames.size() > 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 		for (auto& rFilename : rFilenames) | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			std::string FileName; | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 			SplitPath(rFilename, nullptr, &FileName, nullptr); | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 			if (FileName.substr(0, code.length()).compare(code) == 0 && textureMap.find(FileName) == textureMap.end()) | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 				textureMap.insert(std::map<std::string, std::string>::value_type(FileName, rFilename)); | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | bool HiresTexExists(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2012-05-12 14:31:38 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 	return textureMap.find(filename) != textureMap.end(); | 
					
						
							| 
									
										
										
										
											2012-05-12 14:31:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 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) | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 	if (textureMap.find(filename) == textureMap.end()) | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 		return PC_TEX_FMT_NONE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int width; | 
					
						
							|  |  |  | 	int height; | 
					
						
							|  |  |  | 	int channels; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 12:13:29 -04:00
										 |  |  | 	File::IOFile file; | 
					
						
							|  |  |  | 	file.Open(textureMap[filename], "rb"); | 
					
						
							|  |  |  | 	std::vector<u8> buffer(file.GetSize()); | 
					
						
							|  |  |  | 	file.ReadBytes(buffer.data(), file.GetSize()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	u8* temp = SOIL_load_image_from_memory(buffer.data(), (int)buffer.size(), &width, &height, &channels, SOIL_LOAD_RGBA); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	if (temp == nullptr) | 
					
						
							| 
									
										
										
										
											2009-07-26 09:52:35 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 		ERROR_LOG(VIDEO, "Custom texture %s failed to load", textureMap[filename].c_str()); | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 		return PC_TEX_FMT_NONE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-13 15:38:56 +02:00
										 |  |  | 	*pWidth = width; | 
					
						
							|  |  |  | 	*pHeight = height; | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-04 01:04:52 +02:00
										 |  |  | 	//int offset = 0;
 | 
					
						
							| 
									
										
										
										
											2012-05-13 15:38:56 +02:00
										 |  |  | 	PC_TexFormat returnTex = PC_TEX_FMT_NONE; | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-07 17:05:43 +01:00
										 |  |  | 	// TODO(neobrain): This function currently has no way to enforce RGBA32
 | 
					
						
							|  |  |  | 	// output, which however is required on some configurations to function
 | 
					
						
							|  |  |  | 	// properly. As a lazy workaround, we hence disable the optimized code
 | 
					
						
							|  |  |  | 	// path for now.
 | 
					
						
							|  |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2014-08-19 20:16:50 -07:00
										 |  |  | 	switch (texformat) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 	case GX_TF_I4: | 
					
						
							|  |  |  | 	case GX_TF_I8: | 
					
						
							|  |  |  | 	case GX_TF_IA4: | 
					
						
							|  |  |  | 	case GX_TF_IA8: | 
					
						
							| 
									
										
										
										
											2012-05-13 15:38:56 +02:00
										 |  |  | 		*required_size = width * height * 8; | 
					
						
							|  |  |  | 		if (data_size < *required_size) | 
					
						
							|  |  |  | 			goto cleanup; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 		for (int i = 0; i < width * height * 4; i += 4) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// Rather than use a luminosity function, just use the most intense color for luminance
 | 
					
						
							| 
									
										
										
										
											2012-05-13 15:38:56 +02:00
										 |  |  | 			// TODO(neobrain): Isn't this kind of.. stupid?
 | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 			data[offset++] = *std::max_element(temp+i, temp+i+3); | 
					
						
							|  |  |  | 			data[offset++] = temp[i+3]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		returnTex = PC_TEX_FMT_IA8; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2012-05-13 15:38:56 +02:00
										 |  |  | 		*required_size = width * height * 4; | 
					
						
							|  |  |  | 		if (data_size < *required_size) | 
					
						
							|  |  |  | 			goto cleanup; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 19:31:48 +00:00
										 |  |  | 		memcpy(data, temp, width * height * 4); | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 		returnTex = PC_TEX_FMT_RGBA32; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-19 20:16:50 -07:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 	*required_size = width * height * 4; | 
					
						
							|  |  |  | 	if (data_size < *required_size) | 
					
						
							|  |  |  | 		goto cleanup; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	memcpy(data, temp, width * height * 4); | 
					
						
							|  |  |  | 	returnTex = PC_TEX_FMT_RGBA32; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 	INFO_LOG(VIDEO, "Loading custom texture from %s", textureMap[filename].c_str()); | 
					
						
							| 
									
										
										
										
											2012-05-13 15:38:56 +02:00
										 |  |  | cleanup: | 
					
						
							|  |  |  | 	SOIL_free_image_data(temp); | 
					
						
							| 
									
										
										
										
											2009-07-06 02:10:26 +00:00
										 |  |  | 	return returnTex; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |