6 Star 52 Fork 9

xiaozhuai / imageinfo

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

imageinfo

Cross platform super fast single header c++ library to get image size and format without loading/decoding.

The imageinfo don't get image format by file ext name, but infer by file header bytes and character.

As few I/O times as possible! Read as few bytes as possible!

Supported formats

  • bmp
  • cur
  • dds
  • gif
  • hdr (pic)
  • heic (heif)
  • icns
  • ico
  • jp2
  • jpeg (jpg)
  • jpx
  • ktx
  • png
  • psd
  • svg
  • tga
  • tiff (tif)
  • webp
  • more coming...

Build & Test

Linux & MacOS

cmake -B build .
cmake --build build -- all
cmake --build build -- check

Windows

call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars32.bat"
cmake -G "NMake Makefiles" -B build .
cmake --build build -- all
cmake --build build -- check

Usage

Simplest Demo

ImageInfo<const std::string &, IIFilePathReader> imageInfo("images/valid/jpg/sample.jpg");
std::cout << "File: " << file << "\n";
std::cout << "  - Error    : " << imageInfo.getErrorMsg() << "\n";
std::cout << "  - Width    : " << imageInfo.getWidth() << "\n";
std::cout << "  - Height   : " << imageInfo.getHeight() << "\n";
std::cout << "  - Format   : " << imageInfo.getFormat() << "\n";
std::cout << "  - Ext      : " << imageInfo.getExt() << "\n";
std::cout << "  - Full Ext : " << imageInfo.getFullExt() << "\n";
std::cout << "  - Mimetype : " << imageInfo.getMimetype() << "\n\n";

You can pass a file path and use IIFilePathReader,

and there are some builtin reader IIFileReader, IIFileStreamReader, IIRawDataReader

FILE *file = fopen("images/valid/jpg/sample.jpg", "rb");
ImageInfo<FILE *, IIFileReader> imageInfo(file);
fclose(file);
std::ifstream file("images/valid/jpg/sample.jpg", std::ios::in);
ImageInfo<std::ifstream &, IIFileStreamReader> imageInfo(file);
file.close();
// Suppose we already got data and size
// void *data;
// size_t size;
ImageInfo<IIRawData, IIRawDataReader> imageInfo(IIRawData(data, size));

If you known the file is likely a JPEG, you can provide likely format parameter to improve performance;

ImageInfo<const std::string &, IIFilePathReader> imageInfo("images/valid/jpg/sample.jpg", II_FORMAT_JPEG);

Custom Reader

First, take a look at IIFileReader, all your need to do is define a class and implement size and read method. (not override)

class IIFileReader {
public:
    explicit IIFileReader(FILE *file) : m_file(file) {}

    inline size_t size() {
        if (m_file != nullptr) {
            fseek(m_file, 0, SEEK_END);
            return ftell(m_file);
        } else {
            return 0;
        }
    }

    inline void read(void *buf, off_t offset, size_t size) {
        fseek(m_file, offset, SEEK_SET);
        fread(buf, 1, size, m_file);
    }

private:
    FILE *m_file = nullptr;
};

Then, let's try to make a reader for Android assets file

class IIAndroidAssetFileReader {
public:
    explicit IIAndroidAssetFileReader(AAsset *file) : m_file(file) {}

    inline size_t size() {
        if (m_file != nullptr) {
            return AAsset_getLength(m_file);
        } else {
            return 0;
        }
    }

    inline void read(void *buf, off_t offset, size_t size) {
        AAsset_seek(m_file, offset, SEEK_SET);
        AAsset_read(m_file, buf, size);
    }

private:
    AAsset *m_file = nullptr;
};
// Suppose we have a AAssetManager
// AAssetManager *manager;
// Open with AASSET_MODE_RANDOM mode to seek forward and backward
AAsset *file = AAssetManager_open(manager, "test.png", AASSET_MODE_RANDOM);
ImageInfo<AAsset *, IIAndroidAssetFileReader> imageInfo(file);
AAsset_close(file);

Pretty easy?

Don't be stingy with your star : )

Comments ( 3 )

Sign in for post a comment

About

跨平台高性能的C++单个头文件库,在不加载/解码图片的情况下,获取图片文件类型和大小。支持格式 bmp, cur, dds, gif, hdr (pic), heic (heif), icns, ico, jp2, jpeg (jpg), jpx, ktx, png, psd, tga, tiff (tif), webp ... spread retract
C++ and 2 more languages
MIT
Cancel

Releases

No release

Gitee Metrics

Contributors

All

Activities

load more
can not load any more
C++
1
https://gitee.com/xiaozhuai/imageinfo.git
git@gitee.com:xiaozhuai/imageinfo.git
xiaozhuai
imageinfo
imageinfo
master

Search