forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_format.cpp
More file actions
141 lines (106 loc) · 3.46 KB
/
detect_format.cpp
File metadata and controls
141 lines (106 loc) · 3.46 KB
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
// Aseprite Document IO Library
// Copyright (c) 2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "docio/detect_format.h"
#include "base/file_handle.h"
#include "base/fs.h"
#include "base/string.h"
#include "flic/flic_details.h"
#include <cstring>
#define ASE_MAGIC_NUMBER 0xA5E0
#define BMP_MAGIC_NUMBER 0x4D42 // "BM"
#define JPG_MAGIC_NUMBER 0xD8FF
#define GIF_87_STAMP "GIF87a"
#define GIF_89_STAMP "GIF89a"
#define PNG_MAGIC_DWORD1 0x474E5089
#define PNG_MAGIC_DWORD2 0x0A1A0A0D
namespace docio {
FileFormat detect_format(const std::string& filename)
{
FileFormat ff = detect_format_by_file_content(filename);
if (ff == FileFormat::UNKNOWN)
ff = detect_format_by_file_extension(filename);
return ff;
}
FileFormat detect_format_by_file_content(const std::string& filename)
{
#define IS_MAGIC_WORD(offset, word) \
((buf[offset+0] == (word & 0xff)) && \
(buf[offset+1] == ((word & 0xff00) >> 8)))
#define IS_MAGIC_DWORD(offset, dword) \
((buf[offset+0] == (dword & 0xff)) && \
(buf[offset+1] == ((dword & 0xff00) >> 8)) && \
(buf[offset+2] == ((dword & 0xff0000) >> 16)) && \
(buf[offset+3] == ((dword & 0xff000000) >> 24)))
base::FileHandle handle(base::open_file(filename.c_str(), "rb"));
if (!handle)
return FileFormat::ERROR;
FILE* f = handle.get();
unsigned char buf[8];
int count = fread(buf, 1, 8, f);
if (count >= 2) {
if (IS_MAGIC_WORD(0, BMP_MAGIC_NUMBER))
return FileFormat::BMP_IMAGE;
if (IS_MAGIC_WORD(0, JPG_MAGIC_NUMBER))
return FileFormat::JPEG_IMAGE;
if (count >= 6) {
if (std::strncmp((const char*)buf, GIF_87_STAMP, 6) == 0 ||
std::strncmp((const char*)buf, GIF_89_STAMP, 6) == 0)
return FileFormat::GIF_ANIMATION;
if (IS_MAGIC_WORD(4, ASE_MAGIC_NUMBER))
return FileFormat::ASE_ANIMATION;
if (IS_MAGIC_WORD(4, FLI_MAGIC_NUMBER) ||
IS_MAGIC_WORD(4, FLC_MAGIC_NUMBER))
return FileFormat::FLIC_ANIMATION;
if (count >= 8) {
if (IS_MAGIC_DWORD(0, PNG_MAGIC_DWORD1) &&
IS_MAGIC_DWORD(4, PNG_MAGIC_DWORD2))
return FileFormat::PNG_IMAGE;
}
}
}
return FileFormat::UNKNOWN;
}
FileFormat detect_format_by_file_extension(const std::string& filename)
{
// By extension
const std::string ext = base::string_to_lower(base::get_file_extension(filename));
if (ext == "ase" ||
ext == "aseprite")
return FileFormat::ASE_ANIMATION;
if (ext == "bmp")
return FileFormat::BMP_IMAGE;
if (ext == "col")
return FileFormat::COL_PALETTE;
if (ext == "flc" ||
ext == "fli")
return FileFormat::FLIC_ANIMATION;
if (ext == "gif")
return FileFormat::GIF_ANIMATION;
if (ext == "gpl")
return FileFormat::GPL_PALETTE;
if (ext == "hex")
return FileFormat::HEX_PALETTE;
if (ext == "ico")
return FileFormat::ICO_IMAGES;
if (ext == "jpg" ||
ext == "jpeg")
return FileFormat::JPEG_IMAGE;
if (ext == "pal")
return FileFormat::PAL_PALETTE;
if (ext == "pcx" ||
ext == "pcc")
return FileFormat::PCX_IMAGE;
if (ext == "anim")
return FileFormat::PIXLY_ANIMATION;
if (ext == "png")
return FileFormat::PNG_IMAGE;
if (ext == "tga")
return FileFormat::TARGA_IMAGE;
if (ext == "webp")
return FileFormat::WEBP_ANIMATION;
return FileFormat::UNKNOWN;
}
} // namespace docio