View difference between Paste ID: AiUfzRer and Tvqjkd76
SHOW: | | - or go back to the newest paste.
1
	bool GetImageDimensionFromFile(const std::string& fileName, int& width, int& height)
2
	{
3
		std::ifstream in(fileName.c_str());
4
		std::uint32_t temp = 0;
5
		bool suc = false;
6
7-
		if (in.eof() || in.bad()) {
7+
		if (!in.is_open()) {
8
			return false;
9
		}
10
11
		width = height = 0;
12
		auto fmt = GetCxImageFormatFromFilename(fileName);
13
		switch (fmt) {
14
		case CXIMAGE_FORMAT_PNG:
15
			in.seekg(16);
16
			in.read(reinterpret_cast<char*>(&width), 4);
17
			in.read(reinterpret_cast<char*>(&height), 4);
18
			width = ntohl(width);
19
			height = ntohl(height);
20
			suc = true;
21
			break;
22
		case CXIMAGE_FORMAT_BMP:
23
			in.seekg(14);
24
			in.read(reinterpret_cast<char*>(&temp), 4);
25
			if (temp == 40) {
26
				// Windows Format
27
				in.read(reinterpret_cast<char*>(&width), 4);
28
				in.read(reinterpret_cast<char*>(&height), 4);
29
				suc = true;
30
			}
31
			else if (temp == 20) {
32
				// MAC Format
33
				in.read(reinterpret_cast<char*>(&width), 2);
34
				in.read(reinterpret_cast<char*>(&height), 2);
35
				suc = true;
36
			}
37
			break;
38
		case CXIMAGE_FORMAT_TGA:
39
			in.seekg(12);
40
			in.read(reinterpret_cast<char*>(&width), 2);
41
			in.read(reinterpret_cast<char*>(&height), 2);
42
			suc = true;
43
			break;
44
		}
45
46
		if (in.eof()) {
47
			return false;
48
		}
49
		return suc;