okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-5b adfcf9028 into f-own-int (round 16: the strata-image test program and its parts, DiT runner, sampler, noise, PNG writer; hooked into no build)e4309aaf3

main
966 B28 linesraw
1// src/image/vae/qi21_pixels.cpp - see include/strata/image/qi21_pixels.hpp.  Contraction off for the file: v * 255 + 0.5
2// must round twice, as sd.cpp's x86 build computes it, on every compiler and CPU.
3#if defined(__clang__)
4#pragma clang fp contract(off)
5#elif defined(__GNUC__)
6#pragma GCC optimize("fp-contract=off")
7#endif
8
9#include "strata/image/qi21_pixels.hpp"
10
11#include <cstddef>
12
13namespace strata::image {
14
15uint8_t qi21_float_to_u8(float v) {
16    if (v <= 0.0f) return 0;
17    if (v >= 1.0f) return 255;
18    volatile float m = v * 255.0f;   // its own rounding (volatile: no fused multiply-add on any compiler)
19    return static_cast<uint8_t>(m + 0.5f);
20}
21
22void qi21_picture_to_u8(const float* planar, int w, int h, int c, uint8_t* out) {
23    const size_t px = (size_t) w * (size_t) h;
24    for (size_t i = 0; i < px; ++i)
25        for (int k = 0; k < c; ++k) out[i * c + k] = qi21_float_to_u8(planar[(size_t) k * px + i]);
26}
27
28}  // namespace strata::image