Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

sht-hash.md 3.6 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Spherical harmonic transform hash
  2. This document specifies a spherical harmonic transform (SHT) hash, which is
  3. intended to be a compact method of encoding a spherical panorama preview. It is
  4. based on the [BlurHash specification](https://github.com/woltapp/blurhash/blob/master/Algorithm.md)
  5. for DCT-based 2D image previews.
  6. There are three steps for creating a SHT hash:
  7. 1. Calculate real spherical harmonic transform coefficients
  8. 2. Encode with compact binary encoding
  9. 3. Encode binary data as Base-83-encoded string
  10. Spherical harmonics form an orthogonal basis for representing a function on the
  11. sphere. Combined with coefficients for each harmonic, they can be used to
  12. represent a frequency-space approximation of such a function, without boundary
  13. effects. There are multiple normalization conventions for spherical harmonics;
  14. the $4\pi$ convention is used here. Since JavaScript does not natively support
  15. complex numbers, real harmonics with separate real sine and cosine coefficients
  16. are used.
  17. Spherical harmonics, $Y_{\ell m}$, are defined for $\ell \in \mathbb{Z}^+$,
  18. with $|m| \leq \ell$. For each $Y_{\ell m}$, there is a corresponding
  19. $f_{\ell m}$ coefficient. When these coefficients are represented in a pair of
  20. matrices (one for the sine coefficient and one for the cosine coefficients)
  21. with rows indexed by $\ell$ and columns indexed by $m$, the upper triangle of
  22. the matrix is zero. Additionally, the $\ell = m = 1$ coefficient in the sine
  23. matrix is always 1 in the $4\pi$ normalization convention, which is why it is
  24. used here. Spherical harmonic coefficients are calculated separately for each
  25. color channel. Once calculated, the sine and cosine coefficients are stored in
  26. 1D arrays using row-first ordering with the upper triangle of the matrices
  27. excluded. The coefficient arrays also excludes the first row and column of
  28. the coefficient matrices since their contents are always zero, except for the
  29. $\ell = m = 1$ sine coefficient, which is always 1 (as previously mentioned).
  30. The 1D cosine coefficient array is appended to the 1D sine coefficient array,
  31. for each of the color channels.
  32. The maximum coefficient magnitude is then found across the color channels, and
  33. this value is used to normalize the coefficients in the range $[-1, 1]$. The
  34. normalized coefficients are then multiplied by 9 and converted to integers,
  35. thereby quantizing the coefficients as integer values. These signed integers in
  36. the range $[-9, 9]$ are then converted to unsigned integers in the range
  37. $[0, 18]$ by adding 9. For each coefficient, the color channel values are
  38. packed into a single number in the range $[0, 6859]$ using
  39. $R \cdot 19^2 + G \cdot 19 + B$. This number is then Base-83-encoded into a
  40. pair of characters. Color is encoded and decoded using gamma-compressed sRGB
  41. values, for simplicity.
  42. The final SHT hash string is constructed by combining the Base-83-encoded
  43. coefficients with a prefix. The first character in the prefix contains the max
  44. $\ell$ value for the coefficients, encoded as Base 83. For Pannellum, this is
  45. currently fixed at $\ell = 5$. The next character contains the maximum
  46. coefficient value, which was used in the normalization. The value is divided by
  47. 255 to normalize it to the range $[0, 1]$. This value is then multiplied by 82
  48. and quantized as an integer, before being Base-83 encoded. For $\ell = 5$ this
  49. string is 74 characters in length.
  50. ## Base 83
  51. A custom Base-83 encoding is used. Values are encoded individually, using one
  52. or two digits, and concatenated together. Multiple-digit values are encoded in
  53. big-endian order, with the most-significant digit first.
  54. The character set used is `0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~`.