Giter Site home page Giter Site logo

zig-qoi's Introduction

zig-qoi

A implementation of the Quite OK Image Format for Zig. This implementation conforms to the Qoi specification.

API

Add src/qoi.zig to your Zig project as a package.

pub const DecodeError = error{ OutOfMemory, InvalidData, EndOfStream };
pub const EncodeError = error{ OutOfMemory };

// Simple API:

pub fn isQOI(bytes: []const u8) bool;
pub fn decodeBuffer(allocator: std.mem.Allocator, buffer: []const u8) DecodeError!Image;
pub fn decodeStream(allocator: std.mem.Allocator, reader: anytype) !Image;

pub fn encodeBuffer(allocator: std.mem.Allocator, image: ConstImage) EncodeError![]u8;
pub fn encodeStream(image: ConstImage, writer: anytype) !void;

// Streaming API:
pub fn encoder(writer: anytype) Encoder(@TypeOf(writer));
pub fn Encoder(comptime Writer: type) type {
   return struct {
      writer: Writer,
      pub fn reset(self: *Self) void;
      pub fn flush(self: *Self) (EncodeError || Writer.Error)!void;
      pub fn push(self: *Self, pixel: Color) (EncodeError || Writer.Error)!void;
   };
}

pub const ColorRun = struct {
   color: Color,
   length: usize,
};

pub fn decoder(reader: anytype) Decoder(@TypeOf(reader));
pub fn Decoder(comptime Reader: type) type {
   return struct {
      reader: Reader,
      pub fn fetch(self: *Self) Reader.Error!ColorRun;
   };
}

Implementation Status

Everything specified in phoboslab/qoi#37 is implemented and accessible via the API.

Performance

On my machine (AMD Ryzen 7 3700U), i did a small benchmark with decoding bench.zig, which will decode zero.qoi:

Build Mode QOI Bytes Raw Bytes Encoding Time Decoding Time
Debug 75.024 byte 1.048.576 byte 14.439ms 7.061ms
ReleaseSmall 75.024 byte 1.048.576 byte 1.888ms 1.499ms
ReleaseSafe 75.024 byte 1.048.576 byte 1.392ms 512.706us
ReleaseFast 75.024 byte 1.048.576 byte 1.186ms 456.762us

This means that this implementation is roughly able to decode ~2.1 GB/s raw texture data and is considered "fast enough" for now. If you find some performance improvements, feel free to PR it!

Running perf on the benchmark compiled with ReleaseFast showed that the implementation is quite optimal for the CPU, utilizing it to 100% and executing up to 3 instructions per cycle on my machine.

[felix@denkplatte-v2 zig-qoi]$ perf stat ./zig-out/bin/qoi-bench
Benchmark [4067/4096] Encoding time for 1048576 => 75024 bytes: 1.019ms
Benchmark [4067/4096] Decoding time for 75024 => 1048576 bytes: 419.223us

 Performance counter stats for './zig-out/bin/qoi-bench':

          9.665,11 msec task-clock:u              #    0,997 CPUs utilized
                 0      context-switches:u        #    0,000 K/sec
                 0      cpu-migrations:u          #    0,000 K/sec
            21.066      page-faults:u             #    0,002 M/sec
    29.757.225.002      cycles:u                  #    3,079 GHz                      (83,33%)
       317.453.390      stalled-cycles-frontend:u #    1,07% frontend cycles idle     (83,33%)
       515.819.113      stalled-cycles-backend:u  #    1,73% backend cycles idle      (83,32%)
    83.377.885.642      instructions:u            #    2,80  insn per cycle
                                                  #    0,01  stalled cycles per insn  (83,36%)
    18.947.655.057      branches:u                # 1960,417 M/sec                    (83,31%)
       193.594.708      branch-misses:u           #    1,02% of all branches          (83,35%)

       9,693303129 seconds time elapsed

       9,553127000 seconds user
       0,112001000 seconds sys

Also, running the benchmark dataset of the original author, it yielded the following data:

Number of total images:             1351
Average PNG Compression:              18.57%
Average QOI Compression:              22.70%
Average Compression Rate (MB/s):     438.31 MB/s
Minimal Compression Rate (MB/s):      27.06 MB/s
Maximum Compression Rate (MB/s):    1390.15 MB/s
Average Decompression Rate (MB/s):  1128.46 MB/s
Maximum Decompression Rate (MB/s):    39.77 MB/s
Maximum Deompression Rate (MB/s):  13307.20 MB/s

See also the original analysis on Google Docs

Contribution

Run the test suite like this:

[user@host zig-qoi]$ zig build test
All 5 tests passed.

Run the benchmark like this:

[user@host zig-qoi]$ zig build benchmark
Benchmark [4096/4096] Encoding time for 1048576 => 67076 bytes: 16.649ms
Benchmark [4095/4096] Decoding time for 67076 => 1048576 bytes: 5.681ms

To run the benchmark for batch files, run this:

[user@host zig-qoi]$ zig build install && ./zig-out/bin/qoi-bench-files $(folder_a) $(folder_b) ...
File Name       Width   Height  Total Raw Bytes  Total PNG Bytes PNG Compression  Total QOI Bytes  QOI Compression  QOI to PNG          Decode Time (ns)  Encode Time (ns)
data/zero.png   512     512     1048576          80591           0.08             67076            0.06             0.8323013782501221  5628360           14499346
total sum       0       0       1048576          80591           0.08             67076            0.06             0.8323013782501221  5628360           14499346

Pass as many folders you like to the benchmarking tool. It will render a CSV file on the stderr.

zig-qoi's People

Contributors

hazeycode avatar levydsa avatar samhattangady avatar steza1 avatar vexcess avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

zig-qoi's Issues

isQOI fails to compile

The isQOI function expects the Header type to contain a .size field, yet it doesn't.

I expect it to be fixed by replacing header.size with header.width * header.height at line 74, but I don't know for sure than this is how QOI works.

./zig-qoi/src/qoi.zig:74:46: error: no member named 'size' in struct '.qoi.Header'
    return (bytes.len >= Header.size + header.size);
                                             ^

Does this supports RGB buffer for input?

I'm newbie for zig so it may my misunderstanding, but it seems that this encoder implementation interprets the input buffer as RGBA packed pixel images regardless Header.format . If it's true, this implementation can't accept 3-channel images.

Does this supports RGB buffer for input?

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.