![]() |
tinyimg 1.0.0
Lightweight image manipulation library for Cloudflare Workers
|
Math shims, lookup table builders, checksums and bit level IO. More...
#include <stddef.h>#include <stdint.h>Go to the source code of this file.
Classes | |
| struct | TinyWriter |
| A grow on demand byte sink. More... | |
| struct | TinyBitReader |
| Reads a bitstream out of a byte buffer. More... | |
| struct | TinyBitWriter |
| Writes a bitstream into a TinyWriter. More... | |
Functions | |
| float | tiny_fabsf (float x) |
| Absolute value of a float. | |
| float | tiny_sqrtf (float x) |
| Square root of a float. | |
| float | tiny_floorf (float x) |
| Largest integral value not greater than x. | |
| float | tiny_ceilf (float x) |
| Smallest integral value not less than x. | |
| float | tiny_truncf (float x) |
| Integral part of x, discarding the fraction. | |
| float | tiny_roundf (float x) |
| Nearest integral value, halfway cases rounded away from zero. | |
| float | tiny_fmodf (float x, float y) |
| Floating point remainder of x/y, with the sign of x. | |
| float | tiny_expf (float x) |
| Exponential of x. | |
| float | tiny_logf (float x) |
| Natural logarithm of x. | |
| float | tiny_powf (float x, float y) |
| x raised to the power y. | |
| float | tiny_sinf (float x) |
| Sine of x. | |
| float | tiny_cosf (float x) |
| Cosine of x. | |
| float | tiny_atan2f (float y, float x) |
| Angle of the vector (x, y) from the positive x axis. | |
| void | tiny_lut_identity (uint8_t *lut) |
| Fills a 256 entry table with the identity mapping. | |
| void | tiny_lut_gamma (uint8_t *lut, float gamma) |
| Fills a 256 entry table with a gamma curve. | |
| void | tiny_lut_srgb (uint8_t *lut, int encode) |
| Fills a 256 entry table with the sRGB transfer function. | |
| void | tiny_lut_compose (uint8_t *out, const uint8_t *first, const uint8_t *second) |
| Composes two 256 entry tables into one. | |
| uint32_t | tiny_crc32 (uint32_t crc, const uint8_t *data, size_t size) |
| Computes a running CRC-32 over a buffer. | |
| uint32_t | tiny_adler32 (uint32_t adler, const uint8_t *data, size_t size) |
| Computes a running Adler-32 over a buffer. | |
| size_t | tiny_strlen (const char *s) |
| Length of a NUL terminated string. | |
| int | tiny_strcmp (const char *a, const char *b) |
| Compares two NUL terminated strings. | |
| int | tiny_strcopy (char *dest, const char *src, size_t capacity) |
| Copies a string into a fixed buffer, always terminating it. | |
| int | tiny_huffman_lengths (const uint32_t *frequencies, uint32_t count, uint32_t limit, uint8_t *lengths) |
| Builds length limited Huffman code lengths from symbol frequencies. | |
| int | tiny_writer_init (TinyWriter *writer, size_t initial) |
| Prepares a writer, optionally reserving space up front. | |
| void | tiny_writer_free (TinyWriter *writer) |
| Releases a writer's buffer. | |
| uint8_t * | tiny_writer_detach (TinyWriter *writer, size_t *size) |
| Detaches a writer's buffer, handing ownership to the caller. | |
| int | tiny_writer_reserve (TinyWriter *writer, size_t extra) |
| Reserves capacity for at least extra more bytes. | |
| int | tiny_writer_write (TinyWriter *writer, const void *bytes, size_t size) |
| Appends raw bytes. | |
| int | tiny_writer_fill (TinyWriter *writer, uint8_t value, size_t count) |
| Appends the same byte repeatedly. | |
| int | tiny_writer_u8 (TinyWriter *writer, uint8_t value) |
| Appends one byte. | |
| int | tiny_writer_le16 (TinyWriter *writer, uint16_t value) |
| Appends a 16 bit value, little endian. | |
| int | tiny_writer_le32 (TinyWriter *writer, uint32_t value) |
| Appends a 32 bit value, little endian. | |
| int | tiny_writer_be16 (TinyWriter *writer, uint16_t value) |
| Appends a 16 bit value, big endian. | |
| int | tiny_writer_be32 (TinyWriter *writer, uint32_t value) |
| Appends a 32 bit value, big endian. | |
| uint32_t | tiny_writer_sizeof (void) |
| Size of a TinyWriter, for a host allocating one across the ABI. | |
| uint8_t * | tiny_writer_data (const TinyWriter *writer) |
| Pointer to a writer's bytes. | |
| uint32_t | tiny_writer_size (const TinyWriter *writer) |
| Number of bytes a writer holds. | |
| void | tiny_bits_init (TinyBitReader *reader, const uint8_t *data, size_t size) |
| Points a bit reader at a buffer. | |
| uint32_t | tiny_bits_msb (TinyBitReader *reader, uint32_t count) |
| Reads bits, most significant first. | |
| uint32_t | tiny_bits_peek_msb (TinyBitReader *reader, uint32_t count) |
| Reads bits without consuming them, most significant first. | |
| uint32_t | tiny_bits_lsb (TinyBitReader *reader, uint32_t count) |
| Reads bits, least significant first. | |
| uint32_t | tiny_bits_peek_lsb (TinyBitReader *reader, uint32_t count) |
| Reads bits without consuming them, least significant first. | |
| void | tiny_bits_skip_msb (TinyBitReader *reader, uint32_t count) |
| Discards bits already examined with a most significant first peek. | |
| void | tiny_bits_skip_lsb (TinyBitReader *reader, uint32_t count) |
| Discards bits already examined with a least significant first peek. | |
| void | tiny_bits_align_msb (TinyBitReader *reader) |
| Drops the partial byte at the read head of a most significant first stream. | |
| void | tiny_bits_align_lsb (TinyBitReader *reader) |
| Drops the partial byte at the read head of a least significant first stream. | |
| size_t | tiny_bits_remaining (const TinyBitReader *reader) |
| Bytes not yet consumed, counting only whole bytes past the head. | |
| void | tiny_bitwriter_init (TinyBitWriter *writer, TinyWriter *out) |
| Points a bit writer at a byte sink. | |
| void | tiny_bitwriter_msb (TinyBitWriter *writer, uint32_t value, uint32_t count) |
| Writes bits, most significant first. | |
| void | tiny_bitwriter_lsb (TinyBitWriter *writer, uint32_t value, uint32_t count) |
| Writes bits, least significant first. | |
| void | tiny_bitwriter_flush_msb (TinyBitWriter *writer) |
| Flushes a most significant bit first stream, padding with one bits. | |
| void | tiny_bitwriter_flush_lsb (TinyBitWriter *writer) |
| Flushes a least significant bit first stream, padding with zero bits. | |
Math shims, lookup table builders, checksums and bit level IO.
| uint32_t tiny_adler32 | ( | uint32_t | adler, |
| const uint8_t * | data, | ||
| size_t | size ) |
Computes a running Adler-32 over a buffer.
Uses the zlib calling convention, so passing 1 for the first call produces the value a zlib stream trailer carries.
O(n) time complexity, where n is size.
| adler | Running value, 1 to start a new checksum. |
| data | Bytes to fold in. May be NULL when size is 0. |
| size | Number of bytes. |
| float tiny_atan2f | ( | float | y, |
| float | x ) |
Angle of the vector (x, y) from the positive x axis.
| y | The vertical component. |
| x | The horizontal component. |
| void tiny_bits_align_lsb | ( | TinyBitReader * | reader | ) |
Drops the partial byte at the read head of a least significant first stream.
DEFLATE needs this before a stored block's length field.
| reader | The reader. |
| void tiny_bits_align_msb | ( | TinyBitReader * | reader | ) |
Drops the partial byte at the read head of a most significant first stream.
JPEG needs this after a restart marker. The two orders keep their unread bits at opposite ends of the accumulator, so aligning is not a shared operation.
| reader | The reader. |
| void tiny_bits_init | ( | TinyBitReader * | reader, |
| const uint8_t * | data, | ||
| size_t | size ) |
Points a bit reader at a buffer.
| reader | The reader. |
| data | The bytes. |
| size | Number of bytes. |
| uint32_t tiny_bits_lsb | ( | TinyBitReader * | reader, |
| uint32_t | count ) |
Reads bits, least significant first.
| reader | The reader. |
| count | How many bits to read. Must be 24 or fewer. |
| uint32_t tiny_bits_msb | ( | TinyBitReader * | reader, |
| uint32_t | count ) |
Reads bits, most significant first.
| reader | The reader. |
| count | How many bits to read. Must be 24 or fewer. |
| uint32_t tiny_bits_peek_lsb | ( | TinyBitReader * | reader, |
| uint32_t | count ) |
Reads bits without consuming them, least significant first.
| reader | The reader. |
| count | How many bits to peek. Must be 24 or fewer. |
| uint32_t tiny_bits_peek_msb | ( | TinyBitReader * | reader, |
| uint32_t | count ) |
Reads bits without consuming them, most significant first.
Used by Huffman decoders, which look at the longest possible code and then skip only the bits the matched symbol actually used.
| reader | The reader. |
| count | How many bits to peek. Must be 24 or fewer. |
| size_t tiny_bits_remaining | ( | const TinyBitReader * | reader | ) |
Bytes not yet consumed, counting only whole bytes past the head.
| reader | The reader. |
| void tiny_bits_skip_lsb | ( | TinyBitReader * | reader, |
| uint32_t | count ) |
Discards bits already examined with a least significant first peek.
| reader | The reader. |
| count | How many bits to drop. |
| void tiny_bits_skip_msb | ( | TinyBitReader * | reader, |
| uint32_t | count ) |
Discards bits already examined with a most significant first peek.
| reader | The reader. |
| count | How many bits to drop. |
| void tiny_bitwriter_flush_lsb | ( | TinyBitWriter * | writer | ) |
Flushes a least significant bit first stream, padding with zero bits.
| writer | The bit writer. |
| void tiny_bitwriter_flush_msb | ( | TinyBitWriter * | writer | ) |
Flushes a most significant bit first stream, padding with one bits.
One bits are the padding JPEG entropy coded segments use.
| writer | The bit writer. |
| void tiny_bitwriter_init | ( | TinyBitWriter * | writer, |
| TinyWriter * | out ) |
Points a bit writer at a byte sink.
| writer | The bit writer. |
| out | The sink, which must outlive the bit writer. |
| void tiny_bitwriter_lsb | ( | TinyBitWriter * | writer, |
| uint32_t | value, | ||
| uint32_t | count ) |
Writes bits, least significant first.
| writer | The bit writer. |
| value | The bits, right aligned. |
| count | How many bits to write. Must be 24 or fewer. |
| void tiny_bitwriter_msb | ( | TinyBitWriter * | writer, |
| uint32_t | value, | ||
| uint32_t | count ) |
Writes bits, most significant first.
| writer | The bit writer. |
| value | The bits, right aligned. |
| count | How many bits to write. Must be 24 or fewer. |
| float tiny_ceilf | ( | float | x | ) |
Smallest integral value not less than x.
| x | The value. |
| float tiny_cosf | ( | float | x | ) |
Cosine of x.
Worst measured absolute error is 9.1e-8 over -20 to 20. Argument reduction uses a two part pi/2, which holds to roughly |x| < 1e5 radians.
| x | The angle in radians. |
| uint32_t tiny_crc32 | ( | uint32_t | crc, |
| const uint8_t * | data, | ||
| size_t | size ) |
Computes a running CRC-32 over a buffer.
Uses the reflected 0xEDB88320 polynomial and the zlib calling convention, so passing 0 for the first call produces the value PNG chunks and gzip trailers carry. The 1 KiB table is generated on first use rather than shipped.
O(n) time complexity, where n is size.
| crc | Running value, 0 to start a new checksum. |
| data | Bytes to fold in. May be NULL when size is 0. |
| size | Number of bytes. |
| float tiny_expf | ( | float | x | ) |
Exponential of x.
Worst measured relative error is 2.2e-7 over -30 to 30, or two units in the last place. Saturates rather than producing subnormals: inputs above 87 return infinity and inputs below -87 return zero.
| x | The exponent. |
| float tiny_fabsf | ( | float | x | ) |
Absolute value of a float.
| x | The value. |
| float tiny_floorf | ( | float | x | ) |
Largest integral value not greater than x.
| x | The value. |
| float tiny_fmodf | ( | float | x, |
| float | y ) |
Floating point remainder of x/y, with the sign of x.
Computed as x - y * trunc(x / y), so the result loses precision once |x / y| grows past the mantissa. Angles and other small ratios are unaffected.
| x | The dividend. |
| y | The divisor. Zero produces NaN. |
| int tiny_huffman_lengths | ( | const uint32_t * | frequencies, |
| uint32_t | count, | ||
| uint32_t | limit, | ||
| uint8_t * | lengths ) |
Builds length limited Huffman code lengths from symbol frequencies.
Shared by every encoder that writes a prefix code, which is DEFLATE for its literals, its distances and its own code length alphabet, and WebP lossless for all five of its alphabets. They differ only in how many symbols they have and how deep a code they can express.
The lengths are canonical: the caller turns them into codes, and a symbol whose frequency is zero gets a length of zero and no code. An alphabet with one used symbol gets that symbol a length of one, which a canonical builder can express and which the two formats then treat differently.
O(n^2) time complexity in the alphabet size, because the two lowest weight nodes are found by scanning rather than through a heap. At the 536 symbols the larger caller reaches that is about 143k comparisons, against a heap's 5k, and it is a third of the code; past a few thousand symbols the trade stops being worth it, which is one reason WebP's color cache is capped where it is.
| frequencies | How often each symbol occurs, count entries. |
| count | Symbols in the alphabet. |
| limit | Longest code the format can express, in bits. |
| lengths | Receives count lengths, zero for an unused symbol. |
| float tiny_logf | ( | float | x | ) |
Natural logarithm of x.
Worst measured error over 1e-6 to 1e6 is 5.8e-7 absolute and 2.4e-7 relative. Relative error is unbounded as the result approaches zero at x = 1, which is a property of the function rather than of this implementation, so the absolute figure is the useful one. Subnormal inputs are handled by scaling.
| x | The value. Zero returns negative infinity and negatives return NaN. |
| void tiny_lut_compose | ( | uint8_t * | out, |
| const uint8_t * | first, | ||
| const uint8_t * | second ) |
Composes two 256 entry tables into one.
The result applies first and then second, which is what lets a chain of point operations collapse into a single table.
| out | Table to fill. May alias first, since each entry is read before it is written; aliasing second reads entries that have already been overwritten. |
| first | The table applied first. |
| second | The table applied second. |
| void tiny_lut_gamma | ( | uint8_t * | lut, |
| float | gamma ) |
Fills a 256 entry table with a gamma curve.
Values above 1 darken the midtones and values below 1 brighten them, matching the sense of tiny_image_gamma_correction.
| lut | Table to fill. |
| gamma | The exponent. Values at or below zero leave the table as the identity. |
| void tiny_lut_identity | ( | uint8_t * | lut | ) |
Fills a 256 entry table with the identity mapping.
| lut | Table to fill. |
| void tiny_lut_srgb | ( | uint8_t * | lut, |
| int | encode ) |
Fills a 256 entry table with the sRGB transfer function.
The piecewise curve from IEC 61966-2-1, linear near black and a power of 1/2.4 above it, rather than the plain 2.2 power it is often quoted as. The two differ by up to three levels in the shadows, which is where an ICC conversion is measured.
| lut | Table to fill. |
| encode | Non-zero for linear light to sRGB, zero for the inverse. |
| float tiny_powf | ( | float | x, |
| float | y ) |
x raised to the power y.
Evaluated as exp(y * log(x)), so the relative error grows with |y * log(x)|. Worst measured relative error is 1.3e-6 at an exponent of 2.2 over 0 to 1, which is the gamma case; that is a quarter of one step of an 8 bit channel. A negative base is only defined for integral exponents and returns NaN otherwise.
| x | The base. |
| y | The exponent. |
| float tiny_roundf | ( | float | x | ) |
Nearest integral value, halfway cases rounded away from zero.
| x | The value. |
| float tiny_sinf | ( | float | x | ) |
Sine of x.
Worst measured absolute error is 9.1e-8 over -20 to 20. Argument reduction uses a two part pi/2, which holds to roughly |x| < 1e5 radians.
| x | The angle in radians. |
| float tiny_sqrtf | ( | float | x | ) |
Square root of a float.
| x | The value. Negative inputs produce NaN. |
| int tiny_strcmp | ( | const char * | a, |
| const char * | b ) |
Compares two NUL terminated strings.
| a | First string. |
| b | Second string. |
| int tiny_strcopy | ( | char * | dest, |
| const char * | src, | ||
| size_t | capacity ) |
Copies a string into a fixed buffer, always terminating it.
Truncates rather than overrunning when the source does not fit.
| dest | Destination buffer. |
| src | Source string. |
| capacity | Size of dest in bytes, including the terminator. |
| size_t tiny_strlen | ( | const char * | s | ) |
Length of a NUL terminated string.
| s | The string. |
| float tiny_truncf | ( | float | x | ) |
Integral part of x, discarding the fraction.
| x | The value. |
| int tiny_writer_be16 | ( | TinyWriter * | writer, |
| uint16_t | value ) |
Appends a 16 bit value, big endian.
| writer | The writer. |
| value | The value. |
| int tiny_writer_be32 | ( | TinyWriter * | writer, |
| uint32_t | value ) |
Appends a 32 bit value, big endian.
| writer | The writer. |
| value | The value. |
| uint8_t * tiny_writer_data | ( | const TinyWriter * | writer | ) |
Pointer to a writer's bytes.
| writer | The writer. |
| uint8_t * tiny_writer_detach | ( | TinyWriter * | writer, |
| size_t * | size ) |
Detaches a writer's buffer, handing ownership to the caller.
The writer is left empty. The caller releases the buffer with tiny_free.
| writer | The writer. |
| size | Receives the byte length. |
| int tiny_writer_fill | ( | TinyWriter * | writer, |
| uint8_t | value, | ||
| size_t | count ) |
Appends the same byte repeatedly.
| writer | The writer. |
| value | The byte. |
| count | How many times to append it. |
| void tiny_writer_free | ( | TinyWriter * | writer | ) |
Releases a writer's buffer.
| writer | The writer. Safe to call twice. |
| int tiny_writer_init | ( | TinyWriter * | writer, |
| size_t | initial ) |
Prepares a writer, optionally reserving space up front.
| writer | The writer. |
| initial | Bytes to reserve. Zero defers the first allocation to the first write. |
| int tiny_writer_le16 | ( | TinyWriter * | writer, |
| uint16_t | value ) |
Appends a 16 bit value, little endian.
| writer | The writer. |
| value | The value. |
| int tiny_writer_le32 | ( | TinyWriter * | writer, |
| uint32_t | value ) |
Appends a 32 bit value, little endian.
| writer | The writer. |
| value | The value. |
| int tiny_writer_reserve | ( | TinyWriter * | writer, |
| size_t | extra ) |
Reserves capacity for at least extra more bytes.
| writer | The writer. |
| extra | Additional bytes needed. |
| uint32_t tiny_writer_size | ( | const TinyWriter * | writer | ) |
Number of bytes a writer holds.
| writer | The writer. |
| uint32_t tiny_writer_sizeof | ( | void | ) |
Size of a TinyWriter, for a host allocating one across the ABI.
| int tiny_writer_u8 | ( | TinyWriter * | writer, |
| uint8_t | value ) |
Appends one byte.
| writer | The writer. |
| value | The byte. |
| int tiny_writer_write | ( | TinyWriter * | writer, |
| const void * | bytes, | ||
| size_t | size ) |
Appends raw bytes.
| writer | The writer. |
| bytes | Source buffer. |
| size | Number of bytes. |