Skip to content

fencl/whale

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Whale

Simple VP8L bit stream decoder written in both C (or C++, both valid) and Zig. VP8L is a image format used by the WebP format in lossless mode. WebP decoders usually support both lossless and lossy mode, which makes them significantly bigger than necessary if you only want lossless. So Whale does only the absolute minimum to parse VP8L bit stream and output RGBA pixel array.

Usage

Use the function

void *whale_decode(
    void *user_data,

    // interface
    unsigned long (*stream) (unsigned char n, void *user_data),
    void*         (*alloc)  (unsigned long n, void *user_data),
    void          (*free)   (void     *block, void *user_data),

    // output
    unsigned *width,
    unsigned *height
);

to decode VP8L bit stream.

Stream function takes argument n and returns an integer with n least-significant bits set to n bits from bit stream. For example, if n is 5 and next bits in the stream are -> 1 1 1 0 1 ... the function will return 0b00...0010111

Function will return an array of pixels in scan-line order, each pixel represented by 4 bytes first being red, second green, third blue and forth alpha component. So the result is RGBARGBARGBA... No padding or alignment is added between pixels or lines. This array is allocated using interface alloc function and the caller is responsible for freeing it.

Compilation

There is no need for build system and so no build system or script is provided. Just add whale.c in your sources and whale.h in your include directories.

Example

example.c uses Whale as header-only library (see below). To compile the example, run

gcc example.c -o webp2tga

or whatever equivalent command your favorite c/c++ compiler use. To compile in visual studio, just add the example.c file to a project and compile.

Header-only

whale.c can be used as a header-only library.

#include "whale.c"

Whale is written to be valid c and c++ code so it can be included into both. However whale.c can only be included in single source file as no guards or IMPLEMENTATION macros are present and this would result in multiple definitions of whale_decode symbol.

All symbols in whale.c are prefixed using vp8l_ prefix and are static (except whale_decode).

Configuration

If you know for sure that you are only decoding specific set of images which doesn't use one or more of four transforms used by VP8L, you can disable the transform by defining one of 4 macros:

WHALE_DISABLE_PREDICTOR_TRANSFORM WHALE_DISABLE_COLOR_TRANSFORM WHALE_DISABLE_SUBTRACT_GREEN_TRANSFORM WHALE_DISABLE_INDEX_TRANSFORM

Zig

Repository contains whale.zig file which contains whale decoder rewritten in Zig. This version works more-less the same as the C version, just more Zig-ified. See comments in the source code.

The code is currently compatible with 0.13-dev version of the Zig compiler. Let's see how long it is going to take before some breaking change comes.

Remarks

This library has no error codes, asserts or any runtime checks whatsoever. The input stream has to be correct otherwise your program will output garbage or more likely crash.

License

Copyright (c) 2023 Matej Fencl
Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.