Skip to content
beetlebugorgPublic

About

An image library in Zig for ztk: decoders, encoders, metadata, color, and a fast pipeline of operations. No dependency.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

ztk-image

ztk-image is the image library of ztk: decoders, encoders, metadata, color conversion, and later resize. It is written in Zig. It has no dependency on ztk and no dependency on a library of another project.

ztk uses it for the image command. Its own Tcl commands are in the namespace ztk::image. Another program can use it alone.

The quality target

The library must be good enough to build an image service on it: a program such as go-dims that resizes, crops, and converts images on request, for many requests at one time, with input from the internet. That target decides the design:

  • A chain of commands, as a request of such a service has it (resize/400x300/crop/200x200+10+10/strip/true/format/webp/quality/80): the plan of the library is that ordered list, so a service of the kind of go-dims or mod_dims maps its URL to a plan with almost no code. docs/dev/design.md has the first set of commands and the geometry form.
  • A pipeline of operations, not only single commands: decode, orient, crop, resize, sharpen, composite (a watermark), convert the color, strip or keep metadata, and encode, as one plan. The library runs the plan with the least work: it decodes a JPEG at a smaller scale when the plan permits, and it reads only the rows that it needs.
  • Memory by rows or tiles, so a very large image does not need its full size in memory, and a limit on memory and on time for each plan.
  • Correct pixels: resize in linear light with premultiplied alpha, filters from nearest to Lanczos, correct handling of color profiles and of wide gamut, and the EXIF orientation applied.
  • Good encoders: quality and size that compare well with the usual tools at the same setting, with the options of each format (progressive JPEG, chroma sampling, PNG compression levels, lossless and lossy WebP).
  • Resize that is as fast and as small in memory as it can be made: a decode at a smaller scale where the format permits, two passes by rows with a ring of only the rows that the filter touches, a stream from the decoder to the encoder, fixed point where it is exact enough, and no allocation in a loop. docs/dev/design.md has the list and the benchmark rule.
  • Speed: SIMD code paths with a scalar path that gives the same pixels, and use of several threads for one large image and for many small ones.
  • Safety: each input is hostile. Limits before each allocation, fuzz targets for each parser, and no crash on any file.
  • Proof: a benchmark and a picture comparison for each operation, kept in the repository, so a claim of quality or speed has a number. A known tool is only a reference in such a comparison: it runs outside the library and is never a part of a build.

Scope

  • Read and write each modern image format. The plan and the format matrix are in the ztk issue "Images: each modern format on each platform, the whole image command, and metadata" (ztk 864).
  • Metadata: EXIF, XMP, IPTC, ICC profiles, the text chunks of PNG, and the facts of the header, with no decode of the pixels.
  • Color: the conversion of an embedded profile to sRGB.
  • Later: resize with a choice of filter, crop, rotate, and flip.

Rules

  • No dependency. The code is our own: each codec, each filter of resize, and each color conversion. The library links no libvips, no ImageMagick, and no codec library. Copy no code of another project, and credit each source of a format description in docs/references/.
  • Each decoder and each parser faces hostile input. It has a limit on pixels and on memory before it allocates, a fuzz target, and tests with files that are cut short and corrupt. A bad file gives an error and never a crash.
  • The same pixels on each platform and in a WebAssembly build.
  • A decoder of the platform or of the browser stays in ztk. This library has only portable code.
  • Each source file starts with a //! comment that explains its algorithm, and an SPDX line for MIT.

State

The project is new. It has a build and no decoder yet. The first code is the simple formats (PNM, BMP, QOI, ICO), the PNG and GIF code that ztk has, and the metadata parsers.

License

MIT. See LICENSE.

About

An image library in Zig for ztk: decoders, encoders, metadata, color, and a fast pipeline of operations. No dependency.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages