Giter VIP home page Giter VIP logo

qoi-images's Introduction

Qoi images for C++

This library attempts to provide qoi format with a simplistic and modern interface. The code depends on the following:

  • GCC with C++20 support tested with GCC-11
  • CMake 2.26

Usage example

Two classes are provided Image and TransparentImage each containing RGB and RGBA pixels respectively.

Reading an image from a file using iterators.

auto file = std::ifstream{"image.qoi", std::ios_base::binary};
auto image = qoi::Image{std::istreambuf_iterator{file}, {}};

Similarly writing to a file using output iterators.

auto file = std::ofstream{"output_image.qoi", std::ios_base::binary};
image.write(std::ostreambuf_iterator{file});

Decoding an image from a vector using ranges.

auto byte_data = std::vector<char>{...};
auto image = qoi::TransparentImage{byte_data};

Encoding to a standard container using output iterator.

auto out_data = std::vector<char>{};
image.write(std::back_inserter{out_data});

CMake integration

Dropping the following into your cmake will make the header only target Qoi::qoi available.

###
### Automatic fetching of the qoi image encoder / decoder
### This cmake file provides one header only target Qoi::qoi
###

find_package(Qoi 1.0.0 QUIET)

if(Qoi_FOUND)
    message(STATUS "Using Qoi found at ${Qoi_CONFIG}")
else(Qoi_FOUND)
include(FetchContent)
    message(STATUS "Fetching Qoi from github")
    FetchContent_Declare(
        qoi
        GIT_REPOSITORY https://github.com/thorulf4/qoi-images.git
        GIT_TAG main
    )

    FetchContent_GetProperties(qoi)
    if(NOT qoi_POPULATED)
        FetchContent_Populate(qoi)
        add_subdirectory(${qoi_SOURCE_DIR} ${qoi_BINARY_DIR} EXCLUDE_FROM_ALL)
    endif()
endif(Qoi_FOUND)

Full example with openCV integration

The following example reads an qoi image and passes it into a OpenCV which saves it as a png.

#include <qoi/core.h>
#include <opencv2/opencv.hpp>

#include <fstream>
#include <vector>
#include <iterator>
#include <iostream>

cv::Vec3b to_cv_pixel(const qoi::RGB& pixel){
    return {pixel.b,pixel.g,pixel.r};
}

int main(int argc, const char** argv){
    auto file = std::ifstream{"image.qoi", std::ios_base::binary};
    auto image = qoi::Image{std::istreambuf_iterator{file}, {}};
    
    cv::Mat mat{};
    mat.create(image.header.height, image.header.width, CV_8UC3);
    std::transform(image.pixels.begin(), image.pixels.end(), mat.begin<cv::Vec3b>(), to_cv_pixel);
    cv::imwrite("image.png", mat);
}

qoi-images's People

Contributors

thorulf4 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.