15 Star 121 Fork 28

xiaozhuai / imageinfo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

imageinfo

跨平台高性能的C++单个头文件库,在不加载/解码图片的情况下,获取图片文件类型和大小。

imageinfo 并不是通过扩展名来识别图片格式,而是通过文件头和文件格式特征来判断图片格式。

尽可能少的I/O次数!读取尽可能少的字节数!

部分测试图片文件来源于 image-size ,感谢 @netroy

Rust 版本: imageinfo-rs

ci

imageinfo 已经重构,如果你使用的是旧版本,请查看 v1 分支。

支持格式

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

Vcpkg

imageinfo 可以通过 vcpkg 安装

vcpgk install imageinfo

构建 & 测试

Linux & MacOS

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

Windows

打开 Visual Studio Command Prompt 并执行下面的命令

cmake -G "NMake Makefiles" -B build .
cmake --build build -- all
cmake --build build -- check

用法

最简DEMO代码

const char *file = "images/valid/jpg/sample.jpg";
auto info = imageinfo::parse<imageinfo::FilePathReader>(file);
std::cout << "File: " << file << "\n";
std::cout << "  - Error    : " << info.error_msg() << "\n";
std::cout << "  - Width    : " << info.size().width << "\n";
std::cout << "  - Height   : " << info.size().height << "\n";
std::cout << "  - Format   : " << info.format() << "\n";
std::cout << "  - Ext      : " << info.ext() << "\n";
std::cout << "  - Full Ext : " << info.full_ext() << "\n";
std::cout << "  - Mimetype : " << info.mimetype() << "\n\n";

可以使用 imageinfo::FilePathReader 然后直接传入文件路径,

不同类型可以使用不同的 Reader, 如 imageinfo::FileReader, imageinfo::FileStreamReader, imageinfo::RawDataReader

FILE *file = fopen("images/valid/jpg/sample.jpg", "rb");
auto info = imageinfo::parse<imageinfo::FileReader>(file);
fclose(file);
std::ifstream file("images/valid/jpg/sample.jpg", std::ios::in | std::ios::binary);
auto info = imageinfo::parse<imageinfo::FileStreamReader>(file);
file.close();
// 假设已经得到了 data 和 size
// void *data;
// size_t size;
auto info = imageinfo::parse<imageinfo::RawDataReader>(imageinfo::RawData(data, size));

如果你事先知道一个文件大概率是JPEG格式, 你可以提供额外的 likely_formats 参数来提升性能;

auto imageInfo = imageinfo::parse<imageinfo::FilePathReader>("images/valid/jpg/sample.jpg", {II_FORMAT_JPEG});

自定义Reader

首先,来看一下 imageinfo::FileReader, 要做的只是定义一个类,然后实现 sizeread 方法。(非override)

class FileReader {
public:
    explicit FileReader(FILE *file) : file_(file) {}

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

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

private:
    FILE *file_ = nullptr;
};

然后,让我们来尝试实现一个Android assets文件的Reader

class AndroidAssetFileReader {
public:
    explicit AndroidAssetFileReader(AAsset *file) : file_(file) {}

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

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

private:
    AAsset *file_ = nullptr;
};
// 假设已经得到了 AAssetManager
// AAssetManager *manager;
// 以 AASSET_MODE_RANDOM 模式打开以支持双向seek
AAsset *file = AAssetManager_open(manager, "test.png", AASSET_MODE_RANDOM);
auto imageInfo = imageinfo::parse<imageinfo::AndroidAssetFileReader>(file);
AAsset_close(file);

很简单不是吗?

请不要吝啬你的Star : )

MIT License Copyright (c) 2021 xiaozhuai Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

跨平台高性能的C++单个头文件库,在不加载/解码图片的情况下,获取图片文件类型和大小。支持格式 avif, bmp, cur, dds, gif, hdr (pic), heic (heif), icns, ico, jp2, jpeg (jpg), jpx, ktx, png, psd, qoi, tga, tiff (tif), webp ... 展开 收起
C++ 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C++
1
https://gitee.com/xiaozhuai/imageinfo.git
git@gitee.com:xiaozhuai/imageinfo.git
xiaozhuai
imageinfo
imageinfo
master

搜索帮助

14c37bed 8189591 565d56ea 8189591