主要根据二进制的前几个数据来判断
osgEarth/ImageUtils.cpp
osgDB::ReaderWriter*
ImageUtils::getReaderWriterForStream(std::istream& stream) {
// Modified from https://oroboro.com/image-format-magic-bytes/
// Get the length of the stream
stream.seekg(0, std::ios::end);
unsigned int len = stream.tellg();
stream.seekg(0, std::ios::beg);
if (len < 16) return 0;
//const char* data = input.c_str();
// Read a 16 byte header
char data[16];
stream.read(data, 16);
// Reset reading
stream.seekg(0, std::ios::beg);
// .jpg: FF D8 FF
// .png: 89 50 4E 47 0D 0A 1A 0A
// .gif: GIF87a
// GIF89a
// .tiff: 49 49 2A 00
// 4D 4D 00 2A
// .bmp: BM
// .webp: RIFF ???? WEBP
// .ico 00 00 01 00
// 00 00 02 00 ( cursor files )
switch (data[0])
{
case '\xFF':
return (!strncmp((const char*)data, "\xFF\xD8\xFF&