GithubHelp home page GithubHelp logo

aftersol / simplified-qoi-codec Goto Github PK

View Code? Open in Web Editor NEW
13.0 3.0 0.0 72 KB

An QOI codec that doesn't requires any other dependencies

License: MIT License

CMake 0.57% C 99.43%
c cpp embedded compression compression-implementations decoder encoder embedded-c embedded-systems header-only

simplified-qoi-codec's Introduction

Simplified QOI Codec Library

A one header file library for encoding and decoding QOI files.

Features

How to Install Library

Place the qoi.h file into your project folder preferably in your project's include folder

Define the following below before you include qoi.h library in one of your source code file

#define  SIMPLIFIED_QOI_IMPLEMENTATION
#include "sQOI.h"

Minimal implementation

Encoder

/* 	
	Assume you open the file and
	allocated enough memory for the encoder
	before this code
*/

qoi_desc_t desc;
qoi_enc_t enc;
uint8_t* pixel_seek;

qoi_desc_init(&desc);
qoi_set_dimensions(&desc, width, height);
qoi_set_channels(&desc, channels);
qoi_set_colorspace(&desc, colorspace);

write_qoi_header(&desc, qoi_file);  

pixel_seek = file_buffer;
qoi_enc_init(&desc, &enc, qoi_file);

while(!qoi_enc_done(&enc))
{
	qoi_encode_chunk(&desc, &enc, pixel_seek);
	pixel_seek += desc.channels;
}

/* 
	Write the QOI file after encoding
	or do something else after encoding
*/

Decoder

/* After reading a QOI file and placed in buffer */

qoi_desc_t desc;
qoi_dec_t dec;
qoi_pixel_t px;

uint8_t *bytes;
size_t rawImageLength, seek;

qoi_desc_init(&desc);

if (!read_qoi_header(&desc, qoi_bytes))
{
	printf("The file you opened is not a QOIF file\n");
	return;
}

raw_image_length = (size_t)desc.width * (size_t)desc.height * (size_t)desc.channels;

seek = 0;
if (raw_image_length == 0)
	return;

qoi_dec_init(&desc, &dec, qoi_bytes, buffer_size);
/* Creates a blank image for the decoder to work on */
bytes = (unsigned char*)malloc(raw_image_length * sizeof(unsigned char) + 4);

if (!bytes)
	return;

/* Keep decoding the pixels until
all pixels are done decompressing */
while (!qoi_dec_done(&dec))
{
	px = qoi_decode_chunk(&dec);
	/* Do something with the pixel values below */
	bytes[seek] = px.red;
	bytes[seek + 1] = px.green;
	bytes[seek + 2] = px.blue;
	
	if (desc.channels > 3) 
		bytes[seek + 3] = px.alpha;
	
	seek += desc.channels;
}

/* Use the pixels however you want after this code */

How to Run Example Programs

Encoder

qoi_enc <input file> <width> <height> <channels> <colorspace> <output file>

Input file must be raw RGB or RGBA file

Decoder

This program only outputs raw RGB or RGBA files depending on the amount of channels in a QOI file

qoi_dec <input file> <output file>

Software Requirements

References

Thank you to the authors of the source code being used for inspiration for this source code

Official QOI References

Other QOI Programs

Footnotes

  1. You must provide a pointer to an existing array to run this codec โ†ฉ

simplified-qoi-codec's People

Contributors

aftersol avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

simplified-qoi-codec's Issues

Error in byte swapping

Hi,

While checking your code, I have found that sQOI.h#L198 is shifting value twice with >> 24:

((value >> 24) & 0xFF | (value << 8) & 0xFF0000 | (value >> 8) & 0xFF00 | (value >> 24) & 0xFF000000);

The last bit shifting should be << 24 instead. Finally, for clarity reasons, it would be great if the 2nd and 3rd expressions between | were swapped. This would be like the following:

((value >> 24) & 0xFF | (value >> 8) & 0xFF00 | (value << 8) & 0xFF0000 | (value << 24) & 0xFF000000);

Licensing

Is this code provided with a license? I was unable to locate one in the repo.

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.