@amazon-devices/expo-image 2.0.1758683737

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (289) hide show
  1. package/.eslintrc.js +2 -0
  2. package/CHANGELOG.md +285 -0
  3. package/CMakeLists.txt +91 -0
  4. package/LICENSE +7 -0
  5. package/README.kepler.md +123 -0
  6. package/README.md +54 -0
  7. package/THIRD_PARTY_NOTICES.txt +24 -0
  8. package/android/build.gradle +124 -0
  9. package/android/proguard-rules.pro +28 -0
  10. package/android/src/main/AndroidManifest.xml +15 -0
  11. package/android/src/main/java/expo/modules/image/Exceptions.kt +9 -0
  12. package/android/src/main/java/expo/modules/image/ExpoImageAppGlideModule.kt +19 -0
  13. package/android/src/main/java/expo/modules/image/ExpoImageModule.kt +192 -0
  14. package/android/src/main/java/expo/modules/image/ExpoImageView.kt +220 -0
  15. package/android/src/main/java/expo/modules/image/ExpoImageViewWrapper.kt +652 -0
  16. package/android/src/main/java/expo/modules/image/GlideModel.kt +73 -0
  17. package/android/src/main/java/expo/modules/image/ImageUtils.kt +50 -0
  18. package/android/src/main/java/expo/modules/image/ImageViewWrapperTarget.kt +313 -0
  19. package/android/src/main/java/expo/modules/image/ResourceIdHelper.kt +47 -0
  20. package/android/src/main/java/expo/modules/image/ThumbnailRequestCoordinatorExtension.kt +21 -0
  21. package/android/src/main/java/expo/modules/image/YogaUtils.kt +20 -0
  22. package/android/src/main/java/expo/modules/image/blurhash/BlurHashFetcher.kt +32 -0
  23. package/android/src/main/java/expo/modules/image/blurhash/BlurhashDecoder.kt +189 -0
  24. package/android/src/main/java/expo/modules/image/blurhash/BlurhashModelLoader.kt +30 -0
  25. package/android/src/main/java/expo/modules/image/blurhash/BlurhashModelLoaderFactory.kt +14 -0
  26. package/android/src/main/java/expo/modules/image/blurhash/BlurhashModule.kt +17 -0
  27. package/android/src/main/java/expo/modules/image/dataurls/Base64DataFetcher.kt +27 -0
  28. package/android/src/main/java/expo/modules/image/dataurls/Base64ModelLoader.kt +24 -0
  29. package/android/src/main/java/expo/modules/image/dataurls/Base64ModelLoaderFactory.kt +12 -0
  30. package/android/src/main/java/expo/modules/image/dataurls/Base64Module.kt +16 -0
  31. package/android/src/main/java/expo/modules/image/drawing/OutlineProvider.kt +201 -0
  32. package/android/src/main/java/expo/modules/image/enums/ContentFit.kt +89 -0
  33. package/android/src/main/java/expo/modules/image/enums/ImageCacheType.kt +14 -0
  34. package/android/src/main/java/expo/modules/image/enums/Priority.kt +15 -0
  35. package/android/src/main/java/expo/modules/image/events/GlideRequestListener.kt +64 -0
  36. package/android/src/main/java/expo/modules/image/events/OkHttpProgressListener.kt +26 -0
  37. package/android/src/main/java/expo/modules/image/okhttp/ExpoImageOkHttpClientGlideModule.kt +80 -0
  38. package/android/src/main/java/expo/modules/image/okhttp/GlideUrlWrapperLoader.kt +55 -0
  39. package/android/src/main/java/expo/modules/image/records/CachePolicy.kt +10 -0
  40. package/android/src/main/java/expo/modules/image/records/ContentPosition.kt +89 -0
  41. package/android/src/main/java/expo/modules/image/records/ImageTransition.kt +8 -0
  42. package/android/src/main/java/expo/modules/image/records/SourceMap.kt +160 -0
  43. package/android/src/main/java/expo/modules/image/records/events.kt +25 -0
  44. package/android/src/main/java/expo/modules/image/svg/SVGDecoder.kt +33 -0
  45. package/android/src/main/java/expo/modules/image/svg/SVGDrawableTranscoder.kt +37 -0
  46. package/android/src/main/java/expo/modules/image/svg/SVGModule.kt +26 -0
  47. package/android/src/main/java/expo/modules/image/thumbhash/ThumbhashDecoder.kt +365 -0
  48. package/android/src/main/java/expo/modules/image/thumbhash/ThumbhashFetcher.kt +31 -0
  49. package/android/src/main/java/expo/modules/image/thumbhash/ThumbhashModelLoader.kt +26 -0
  50. package/android/src/main/java/expo/modules/image/thumbhash/ThumbhashModelLoaderFactory.kt +14 -0
  51. package/android/src/main/java/expo/modules/image/thumbhash/ThumbhashModule.kt +17 -0
  52. package/build/ExpoImage.d.ts +15 -0
  53. package/build/ExpoImage.d.ts.map +1 -0
  54. package/build/ExpoImage.js +75 -0
  55. package/build/ExpoImage.js.map +1 -0
  56. package/build/ExpoImage.kepler.d.ts +23 -0
  57. package/build/ExpoImage.kepler.d.ts.map +1 -0
  58. package/build/ExpoImage.kepler.js +71 -0
  59. package/build/ExpoImage.kepler.js.map +1 -0
  60. package/build/ExpoImage.web.d.ts +9 -0
  61. package/build/ExpoImage.web.d.ts.map +1 -0
  62. package/build/ExpoImage.web.js +90 -0
  63. package/build/ExpoImage.web.js.map +1 -0
  64. package/build/ExpoImageModules.d.ts +3 -0
  65. package/build/ExpoImageModules.d.ts.map +1 -0
  66. package/build/ExpoImageModules.js +4 -0
  67. package/build/ExpoImageModules.js.map +1 -0
  68. package/build/ExpoImageModules.kepler.d.ts +6 -0
  69. package/build/ExpoImageModules.kepler.d.ts.map +1 -0
  70. package/build/ExpoImageModules.kepler.js +5 -0
  71. package/build/ExpoImageModules.kepler.js.map +1 -0
  72. package/build/Image.d.ts +30 -0
  73. package/build/Image.d.ts.map +1 -0
  74. package/build/Image.js +49 -0
  75. package/build/Image.js.map +1 -0
  76. package/build/Image.types.d.ts +374 -0
  77. package/build/Image.types.d.ts.map +1 -0
  78. package/build/Image.types.js +2 -0
  79. package/build/Image.types.js.map +1 -0
  80. package/build/ImageBackground.d.ts +6 -0
  81. package/build/ImageBackground.d.ts.map +1 -0
  82. package/build/ImageBackground.js +13 -0
  83. package/build/ImageBackground.js.map +1 -0
  84. package/build/ImageViewNativeComponent.d.ts +20 -0
  85. package/build/ImageViewNativeComponent.d.ts.map +1 -0
  86. package/build/ImageViewNativeComponent.js +42 -0
  87. package/build/ImageViewNativeComponent.js.map +1 -0
  88. package/build/NativeImageLoaderKepler.d.ts +18 -0
  89. package/build/NativeImageLoaderKepler.d.ts.map +1 -0
  90. package/build/NativeImageLoaderKepler.js +3 -0
  91. package/build/NativeImageLoaderKepler.js.map +1 -0
  92. package/build/fabric/registerNativeComponent.d.ts +15 -0
  93. package/build/fabric/registerNativeComponent.d.ts.map +1 -0
  94. package/build/fabric/registerNativeComponent.js +47 -0
  95. package/build/fabric/registerNativeComponent.js.map +1 -0
  96. package/build/index.d.ts +4 -0
  97. package/build/index.d.ts.map +1 -0
  98. package/build/index.js +4 -0
  99. package/build/index.js.map +1 -0
  100. package/build/utils/AssetSourceResolver.web.d.ts +24 -0
  101. package/build/utils/AssetSourceResolver.web.d.ts.map +1 -0
  102. package/build/utils/AssetSourceResolver.web.js +67 -0
  103. package/build/utils/AssetSourceResolver.web.js.map +1 -0
  104. package/build/utils/blurhash/base83.d.ts +3 -0
  105. package/build/utils/blurhash/base83.d.ts.map +1 -0
  106. package/build/utils/blurhash/base83.js +103 -0
  107. package/build/utils/blurhash/base83.js.map +1 -0
  108. package/build/utils/blurhash/decode.d.ts +7 -0
  109. package/build/utils/blurhash/decode.d.ts.map +1 -0
  110. package/build/utils/blurhash/decode.js +92 -0
  111. package/build/utils/blurhash/decode.js.map +1 -0
  112. package/build/utils/blurhash/error.d.ts +4 -0
  113. package/build/utils/blurhash/error.d.ts.map +1 -0
  114. package/build/utils/blurhash/error.js +8 -0
  115. package/build/utils/blurhash/error.js.map +1 -0
  116. package/build/utils/blurhash/useBlurhash.d.ts +8 -0
  117. package/build/utils/blurhash/useBlurhash.d.ts.map +1 -0
  118. package/build/utils/blurhash/useBlurhash.js +64 -0
  119. package/build/utils/blurhash/useBlurhash.js.map +1 -0
  120. package/build/utils/blurhash/utils.d.ts +5 -0
  121. package/build/utils/blurhash/utils.d.ts.map +1 -0
  122. package/build/utils/blurhash/utils.js +21 -0
  123. package/build/utils/blurhash/utils.js.map +1 -0
  124. package/build/utils/resolveAssetSource.d.ts +3 -0
  125. package/build/utils/resolveAssetSource.d.ts.map +1 -0
  126. package/build/utils/resolveAssetSource.js +3 -0
  127. package/build/utils/resolveAssetSource.js.map +1 -0
  128. package/build/utils/resolveAssetSource.web.d.ts +9 -0
  129. package/build/utils/resolveAssetSource.web.d.ts.map +1 -0
  130. package/build/utils/resolveAssetSource.web.js +31 -0
  131. package/build/utils/resolveAssetSource.web.js.map +1 -0
  132. package/build/utils/resolveHashString.d.ts +14 -0
  133. package/build/utils/resolveHashString.d.ts.map +1 -0
  134. package/build/utils/resolveHashString.js +31 -0
  135. package/build/utils/resolveHashString.js.map +1 -0
  136. package/build/utils/resolveHashString.web.d.ts +20 -0
  137. package/build/utils/resolveHashString.web.d.ts.map +1 -0
  138. package/build/utils/resolveHashString.web.js +31 -0
  139. package/build/utils/resolveHashString.web.js.map +1 -0
  140. package/build/utils/resolveSources.d.ts +8 -0
  141. package/build/utils/resolveSources.d.ts.map +1 -0
  142. package/build/utils/resolveSources.js +45 -0
  143. package/build/utils/resolveSources.js.map +1 -0
  144. package/build/utils/thumbhash/thumbhash.d.ts +66 -0
  145. package/build/utils/thumbhash/thumbhash.d.ts.map +1 -0
  146. package/build/utils/thumbhash/thumbhash.js +338 -0
  147. package/build/utils/thumbhash/thumbhash.js.map +1 -0
  148. package/build/utils.d.ts +17 -0
  149. package/build/utils.d.ts.map +1 -0
  150. package/build/utils.js +85 -0
  151. package/build/utils.js.map +1 -0
  152. package/build/web/AnimationManager.d.ts +37 -0
  153. package/build/web/AnimationManager.d.ts.map +1 -0
  154. package/build/web/AnimationManager.js +155 -0
  155. package/build/web/AnimationManager.js.map +1 -0
  156. package/build/web/ColorTintFilter.d.ts +12 -0
  157. package/build/web/ColorTintFilter.d.ts.map +1 -0
  158. package/build/web/ColorTintFilter.js +30 -0
  159. package/build/web/ColorTintFilter.js.map +1 -0
  160. package/build/web/ImageWrapper.d.ts +5 -0
  161. package/build/web/ImageWrapper.d.ts.map +1 -0
  162. package/build/web/ImageWrapper.js +44 -0
  163. package/build/web/ImageWrapper.js.map +1 -0
  164. package/build/web/ImageWrapper.types.d.ts +29 -0
  165. package/build/web/ImageWrapper.types.d.ts.map +1 -0
  166. package/build/web/ImageWrapper.types.js +2 -0
  167. package/build/web/ImageWrapper.types.js.map +1 -0
  168. package/build/web/getImageWrapperEventHandler.d.ts +8 -0
  169. package/build/web/getImageWrapperEventHandler.d.ts.map +1 -0
  170. package/build/web/getImageWrapperEventHandler.js +25 -0
  171. package/build/web/getImageWrapperEventHandler.js.map +1 -0
  172. package/build/web/hooks.d.ts +13 -0
  173. package/build/web/hooks.d.ts.map +1 -0
  174. package/build/web/hooks.js +58 -0
  175. package/build/web/hooks.js.map +1 -0
  176. package/build/web/imageStyles.d.ts +2 -0
  177. package/build/web/imageStyles.d.ts.map +1 -0
  178. package/build/web/imageStyles.js +137 -0
  179. package/build/web/imageStyles.js.map +1 -0
  180. package/build/web/positioning.d.ts +11 -0
  181. package/build/web/positioning.d.ts.map +1 -0
  182. package/build/web/positioning.js +35 -0
  183. package/build/web/positioning.js.map +1 -0
  184. package/build/web/useSourceSelection.d.ts +14 -0
  185. package/build/web/useSourceSelection.d.ts.map +1 -0
  186. package/build/web/useSourceSelection.js +98 -0
  187. package/build/web/useSourceSelection.js.map +1 -0
  188. package/expo-module.config.json +9 -0
  189. package/ios/Blurhash.swift +326 -0
  190. package/ios/BlurhashLoader.swift +52 -0
  191. package/ios/ContentFit.swift +55 -0
  192. package/ios/ContentPosition.swift +108 -0
  193. package/ios/ExpoImage.podspec +30 -0
  194. package/ios/ImageCachePolicy.swift +24 -0
  195. package/ios/ImageCacheType.swift +21 -0
  196. package/ios/ImageModule.swift +127 -0
  197. package/ios/ImagePriority.swift +22 -0
  198. package/ios/ImageSource.swift +44 -0
  199. package/ios/ImageTransition.swift +67 -0
  200. package/ios/ImageUtils.swift +227 -0
  201. package/ios/ImageView.swift +469 -0
  202. package/ios/Loaders/PhotoLibraryAssetLoader.swift +146 -0
  203. package/ios/Thumbhash.swift +550 -0
  204. package/ios/ThumbhashLoader.swift +53 -0
  205. package/jest.config.js +3 -0
  206. package/kepler/ExpoImage.cpp +63 -0
  207. package/kepler/ExpoImage.h +29 -0
  208. package/kepler/ExpoImageCacheHandler.cpp +15 -0
  209. package/kepler/ExpoImageCacheHandler.h +28 -0
  210. package/kepler/ExpoImageComponent.cpp +579 -0
  211. package/kepler/ExpoImageComponent.h +199 -0
  212. package/kepler/ImageWrapper/BlurhashImageWrapper.cpp +37 -0
  213. package/kepler/ImageWrapper/BlurhashImageWrapper.h +23 -0
  214. package/kepler/ImageWrapper/CacheImageWrapper.cpp +80 -0
  215. package/kepler/ImageWrapper/CacheImageWrapper.h +41 -0
  216. package/kepler/ImageWrapper/HashImageWrapper.cpp +21 -0
  217. package/kepler/ImageWrapper/HashImageWrapper.h +22 -0
  218. package/kepler/ImageWrapper/ImageWrapper.cpp +24 -0
  219. package/kepler/ImageWrapper/ImageWrapper.h +33 -0
  220. package/kepler/ImageWrapper/LocalImageWrapper.cpp +61 -0
  221. package/kepler/ImageWrapper/LocalImageWrapper.h +30 -0
  222. package/kepler/ImageWrapper/ThumbhashImageWrapper.cpp +33 -0
  223. package/kepler/ImageWrapper/ThumbhashImageWrapper.h +20 -0
  224. package/kepler/KeplerImageViewComponent.h +64 -0
  225. package/kepler/KeplerInit.cpp +35 -0
  226. package/kepler/blurhash/common.h +32 -0
  227. package/kepler/blurhash/decode.cpp +187 -0
  228. package/kepler/blurhash/decode.h +58 -0
  229. package/kepler/contentposition/ImageContentPosition.cpp +86 -0
  230. package/kepler/contentposition/ImageContentPosition.h +48 -0
  231. package/kepler/enums/ImageContentFit.h +17 -0
  232. package/kepler/enums/ImagePriority.h +17 -0
  233. package/kepler/managers/ExpoImageManager.cpp +38 -0
  234. package/kepler/managers/ExpoImageManager.h +23 -0
  235. package/kepler/test/ExpoImage.cpp +73 -0
  236. package/kepler/test/ExpoImageComponent.cpp +290 -0
  237. package/kepler/test/blurhash/decode.cpp +39 -0
  238. package/kepler/test/contentposition/ImageContentPosition.cpp +100 -0
  239. package/kepler/test/managers/ExpoImageManager.cpp +39 -0
  240. package/kepler/test/thumbhash/decode.cpp +18 -0
  241. package/kepler/test/utils/LoaderDelegate.cpp +130 -0
  242. package/kepler/thumbhash/decode.cpp +166 -0
  243. package/kepler/thumbhash/decode.h +22 -0
  244. package/kepler/utils/PlaceholderImageLoaderDelegate.cpp +26 -0
  245. package/kepler/utils/PlaceholderImageLoaderDelegate.h +26 -0
  246. package/kepler/utils/PrefetchImageLoaderDelegate.cpp +55 -0
  247. package/kepler/utils/PrefetchImageLoaderDelegate.h +35 -0
  248. package/kepler/utils/SourceImageLoaderDelegate.cpp +47 -0
  249. package/kepler/utils/SourceImageLoaderDelegate.h +42 -0
  250. package/kepler/utils/SourceImageLoaderDelegateEmitter.cpp +15 -0
  251. package/kepler/utils/SourceImageLoaderDelegateEmitter.h +28 -0
  252. package/kepler-compatibility.json +8 -0
  253. package/package.json +1 -0
  254. package/react-native.config.js +22 -0
  255. package/src/ExpoImage.kepler.tsx +119 -0
  256. package/src/ExpoImage.tsx +119 -0
  257. package/src/ExpoImage.web.tsx +155 -0
  258. package/src/ExpoImageModules.kepler.ts +7 -0
  259. package/src/ExpoImageModules.ts +4 -0
  260. package/src/Image.tsx +82 -0
  261. package/src/Image.types.ts +450 -0
  262. package/src/ImageBackground.tsx +18 -0
  263. package/src/ImageViewNativeComponent.ts +59 -0
  264. package/src/NativeImageLoaderKepler.ts +22 -0
  265. package/src/fabric/registerNativeComponent.ts +70 -0
  266. package/src/index.ts +3 -0
  267. package/src/utils/AssetSourceResolver.web.ts +88 -0
  268. package/src/utils/blurhash/base83.ts +104 -0
  269. package/src/utils/blurhash/decode.ts +114 -0
  270. package/src/utils/blurhash/error.ts +7 -0
  271. package/src/utils/blurhash/useBlurhash.tsx +81 -0
  272. package/src/utils/blurhash/utils.ts +21 -0
  273. package/src/utils/resolveAssetSource.tsx +3 -0
  274. package/src/utils/resolveAssetSource.web.ts +40 -0
  275. package/src/utils/resolveHashString.tsx +36 -0
  276. package/src/utils/resolveHashString.web.tsx +33 -0
  277. package/src/utils/resolveSources.tsx +48 -0
  278. package/src/utils/thumbhash/thumbhash.ts +387 -0
  279. package/src/utils.ts +109 -0
  280. package/src/web/AnimationManager.tsx +224 -0
  281. package/src/web/ColorTintFilter.tsx +36 -0
  282. package/src/web/ImageWrapper.tsx +85 -0
  283. package/src/web/ImageWrapper.types.ts +36 -0
  284. package/src/web/getImageWrapperEventHandler.ts +29 -0
  285. package/src/web/hooks.ts +72 -0
  286. package/src/web/imageStyles.tsx +136 -0
  287. package/src/web/positioning.ts +48 -0
  288. package/src/web/useSourceSelection.ts +151 -0
  289. package/tsconfig.json +9 -0
@@ -0,0 +1,387 @@
1
+ // Code copied and slightly modified from https://github.com/evanw/thumbhash
2
+
3
+ /**
4
+ * Encodes an RGBA image to a ThumbHash. RGB should not be premultiplied by A.
5
+ *
6
+ * @param w The width of the input image. Must be ≤100px.
7
+ * @param h The height of the input image. Must be ≤100px.
8
+ * @param rgba The pixels in the input image, row-by-row. Must have w*h*4 elements.
9
+ * @returns The ThumbHash as a Uint8Array.
10
+ */
11
+
12
+ export function rgbaToThumbHash(w: number, h: number, rgba: Uint8Array) {
13
+ // Encoding an image larger than 100x100 is slow with no benefit
14
+ if (w > 100 || h > 100) throw new Error(`${w}x${h} doesn't fit in 100x100`);
15
+ const { PI, round, max, cos, abs } = Math;
16
+
17
+ // Determine the average color
18
+ let avg_r = 0,
19
+ avg_g = 0,
20
+ avg_b = 0,
21
+ avg_a = 0;
22
+ for (let i = 0, j = 0; i < w * h; i++, j += 4) {
23
+ const alpha = rgba[j + 3] / 255;
24
+ avg_r += (alpha / 255) * rgba[j];
25
+ avg_g += (alpha / 255) * rgba[j + 1];
26
+ avg_b += (alpha / 255) * rgba[j + 2];
27
+ avg_a += alpha;
28
+ }
29
+ if (avg_a) {
30
+ avg_r /= avg_a;
31
+ avg_g /= avg_a;
32
+ avg_b /= avg_a;
33
+ }
34
+
35
+ const hasAlpha = avg_a < w * h;
36
+ const l_limit = hasAlpha ? 5 : 7; // Use fewer luminance bits if there's alpha
37
+ const lx = max(1, round((l_limit * w) / max(w, h)));
38
+ const ly = max(1, round((l_limit * h) / max(w, h)));
39
+ const l: number[] = []; // luminance
40
+ const p: number[] = []; // yellow - blue
41
+ const q: number[] = []; // red - green
42
+ const a: number[] = []; // alpha
43
+
44
+ // Convert the image from RGBA to LPQA (composite atop the average color)
45
+ for (let i = 0, j = 0; i < w * h; i++, j += 4) {
46
+ const alpha = rgba[j + 3] / 255;
47
+ const r = avg_r * (1 - alpha) + (alpha / 255) * rgba[j];
48
+ const g = avg_g * (1 - alpha) + (alpha / 255) * rgba[j + 1];
49
+ const b = avg_b * (1 - alpha) + (alpha / 255) * rgba[j + 2];
50
+ l[i] = (r + g + b) / 3;
51
+ p[i] = (r + g) / 2 - b;
52
+ q[i] = r - g;
53
+ a[i] = alpha;
54
+ }
55
+
56
+ // Encode using the DCT into DC (constant) and normalized AC (varying) terms
57
+ const encodeChannel = (channel, nx, ny) => {
58
+ let dc = 0;
59
+ const ac: number[] = [];
60
+ let scale = 0;
61
+ const fx: number[] = [];
62
+ for (let cy = 0; cy < ny; cy++) {
63
+ for (let cx = 0; cx * ny < nx * (ny - cy); cx++) {
64
+ let f = 0;
65
+ for (let x = 0; x < w; x++) fx[x] = cos((PI / w) * cx * (x + 0.5));
66
+ for (let y = 0; y < h; y++)
67
+ for (let x = 0, fy = cos((PI / h) * cy * (y + 0.5)); x < w; x++)
68
+ f += channel[x + y * w] * fx[x] * fy;
69
+ f /= w * h;
70
+ if (cx || cy) {
71
+ ac.push(f);
72
+ scale = max(scale, abs(f));
73
+ } else {
74
+ dc = f;
75
+ }
76
+ }
77
+ }
78
+ if (scale) for (let i = 0; i < ac.length; i++) ac[i] = 0.5 + (0.5 / scale) * ac[i];
79
+ return [dc, ac, scale];
80
+ };
81
+ const [l_dc, l_ac, l_scale] = encodeChannel(l, max(3, lx), max(3, ly));
82
+ const [p_dc, p_ac, p_scale] = encodeChannel(p, 3, 3);
83
+ const [q_dc, q_ac, q_scale] = encodeChannel(q, 3, 3);
84
+ const [a_dc, a_ac, a_scale] = hasAlpha ? encodeChannel(a, 5, 5) : [];
85
+
86
+ // Write the constants
87
+ const isLandscape = w > h;
88
+ const header24 =
89
+ round(63 * (l_dc as number)) |
90
+ (round(31.5 + 31.5 * (p_dc as number)) << 6) |
91
+ (round(31.5 + 31.5 * (q_dc as number)) << 12) |
92
+ (round(31 * (l_scale as number)) << 18) |
93
+ ((hasAlpha ? 1 : 0) << 23);
94
+ const header16 =
95
+ (isLandscape ? ly : lx) |
96
+ (round(63 * (p_scale as number)) << 3) |
97
+ (round(63 * (q_scale as number)) << 9) |
98
+ ((isLandscape ? 1 : 0) << 15);
99
+ const hash = [
100
+ header24 & 255,
101
+ (header24 >> 8) & 255,
102
+ header24 >> 16,
103
+ header16 & 255,
104
+ header16 >> 8,
105
+ ];
106
+ const ac_start = hasAlpha ? 6 : 5;
107
+ let ac_index = 0;
108
+ if (hasAlpha) hash.push(round(15 * (a_dc as number)) | (round(15 * (a_scale as number)) << 4));
109
+
110
+ // Write the varying factors
111
+ for (const ac of hasAlpha ? [l_ac, p_ac, q_ac, a_ac] : [l_ac, p_ac, q_ac])
112
+ for (const f of ac as number[])
113
+ hash[ac_start + (ac_index >> 1)] |= round(15 * f) << ((ac_index++ & 1) << 2);
114
+ return new Uint8Array(hash);
115
+ }
116
+
117
+ /**
118
+ * Decodes a ThumbHash to an RGBA image. RGB is not be premultiplied by A.
119
+ *
120
+ * @param hash The bytes of the ThumbHash.
121
+ * @returns The width, height, and pixels of the rendered placeholder image.
122
+ */
123
+ export function thumbHashToRGBA(hash: Uint8Array) {
124
+ const { PI, min, max, cos, round } = Math;
125
+
126
+ // Read the constants
127
+ const header24 = hash[0] | (hash[1] << 8) | (hash[2] << 16);
128
+ const header16 = hash[3] | (hash[4] << 8);
129
+ const l_dc = (header24 & 63) / 63;
130
+ const p_dc = ((header24 >> 6) & 63) / 31.5 - 1;
131
+ const q_dc = ((header24 >> 12) & 63) / 31.5 - 1;
132
+ const l_scale = ((header24 >> 18) & 31) / 31;
133
+ const hasAlpha = header24 >> 23;
134
+ const p_scale = ((header16 >> 3) & 63) / 63;
135
+ const q_scale = ((header16 >> 9) & 63) / 63;
136
+ const isLandscape = header16 >> 15;
137
+ const lx = max(3, isLandscape ? (hasAlpha ? 5 : 7) : header16 & 7);
138
+ const ly = max(3, isLandscape ? header16 & 7 : hasAlpha ? 5 : 7);
139
+ const a_dc = hasAlpha ? (hash[5] & 15) / 15 : 1;
140
+ const a_scale = (hash[5] >> 4) / 15;
141
+
142
+ // Read the varying factors (boost saturation by 1.25x to compensate for quantization)
143
+ const ac_start = hasAlpha ? 6 : 5;
144
+ let ac_index = 0;
145
+ const decodeChannel = (nx, ny, scale) => {
146
+ const ac: number[] = [];
147
+ for (let cy = 0; cy < ny; cy++)
148
+ for (let cx = cy ? 0 : 1; cx * ny < nx * (ny - cy); cx++)
149
+ ac.push(
150
+ (((hash[ac_start + (ac_index >> 1)] >> ((ac_index++ & 1) << 2)) & 15) / 7.5 - 1) * scale
151
+ );
152
+ return ac;
153
+ };
154
+ const l_ac = decodeChannel(lx, ly, l_scale);
155
+ const p_ac = decodeChannel(3, 3, p_scale * 1.25);
156
+ const q_ac = decodeChannel(3, 3, q_scale * 1.25);
157
+ const a_ac = hasAlpha && decodeChannel(5, 5, a_scale);
158
+
159
+ // Decode using the DCT into RGB
160
+ const ratio = thumbHashToApproximateAspectRatio(hash);
161
+ const w = round(ratio > 1 ? 32 : 32 * ratio);
162
+ const h = round(ratio > 1 ? 32 / ratio : 32);
163
+ const rgba = new Uint8Array(w * h * 4),
164
+ fx: number[] = [],
165
+ fy: number[] = [];
166
+ for (let y = 0, i = 0; y < h; y++) {
167
+ for (let x = 0; x < w; x++, i += 4) {
168
+ let l = l_dc,
169
+ p = p_dc,
170
+ q = q_dc,
171
+ a = a_dc;
172
+
173
+ // Precompute the coefficients
174
+ for (let cx = 0, n = max(lx, hasAlpha ? 5 : 3); cx < n; cx++)
175
+ fx[cx] = cos((PI / w) * (x + 0.5) * cx);
176
+ for (let cy = 0, n = max(ly, hasAlpha ? 5 : 3); cy < n; cy++)
177
+ fy[cy] = cos((PI / h) * (y + 0.5) * cy);
178
+
179
+ // Decode L
180
+ for (let cy = 0, j = 0; cy < ly; cy++)
181
+ for (let cx = cy ? 0 : 1, fy2 = fy[cy] * 2; cx * ly < lx * (ly - cy); cx++, j++)
182
+ l += l_ac[j] * fx[cx] * fy2;
183
+
184
+ // Decode P and Q
185
+ for (let cy = 0, j = 0; cy < 3; cy++) {
186
+ for (let cx = cy ? 0 : 1, fy2 = fy[cy] * 2; cx < 3 - cy; cx++, j++) {
187
+ const f = fx[cx] * fy2;
188
+ p += p_ac[j] * f;
189
+ q += q_ac[j] * f;
190
+ }
191
+ }
192
+
193
+ // Decode A
194
+ if (hasAlpha)
195
+ for (let cy = 0, j = 0; cy < 5; cy++)
196
+ for (let cx = cy ? 0 : 1, fy2 = fy[cy] * 2; cx < 5 - cy; cx++, j++)
197
+ a += a_ac[j] * fx[cx] * fy2;
198
+
199
+ // Convert to RGB
200
+ const b = l - (2 / 3) * p;
201
+ const r = (3 * l - b + q) / 2;
202
+ const g = r - q;
203
+ rgba[i] = max(0, 255 * min(1, r));
204
+ rgba[i + 1] = max(0, 255 * min(1, g));
205
+ rgba[i + 2] = max(0, 255 * min(1, b));
206
+ rgba[i + 3] = max(0, 255 * min(1, a));
207
+ }
208
+ }
209
+ return { w, h, rgba };
210
+ }
211
+
212
+ /**
213
+ * Extracts the average color from a ThumbHash. RGB is not be premultiplied by A.
214
+ *
215
+ * @param hash The bytes of the ThumbHash.
216
+ * @returns The RGBA values for the average color. Each value ranges from 0 to 1.
217
+ */
218
+ export function thumbHashToAverageRGBA(hash: Uint8Array) {
219
+ const { min, max } = Math;
220
+ const header = hash[0] | (hash[1] << 8) | (hash[2] << 16);
221
+ const l = (header & 63) / 63;
222
+ const p = ((header >> 6) & 63) / 31.5 - 1;
223
+ const q = ((header >> 12) & 63) / 31.5 - 1;
224
+ const hasAlpha = header >> 23;
225
+ const a = hasAlpha ? (hash[5] & 15) / 15 : 1;
226
+ const b = l - (2 / 3) * p;
227
+ const r = (3 * l - b + q) / 2;
228
+ const g = r - q;
229
+ return {
230
+ r: max(0, min(1, r)),
231
+ g: max(0, min(1, g)),
232
+ b: max(0, min(1, b)),
233
+ a,
234
+ };
235
+ }
236
+
237
+ /**
238
+ * Extracts the approximate aspect ratio of the original image.
239
+ *
240
+ * @param hash The bytes of the ThumbHash.
241
+ * @returns The approximate aspect ratio (i.e. width / height).
242
+ */
243
+ export function thumbHashToApproximateAspectRatio(hash: Uint8Array) {
244
+ const header = hash[3];
245
+ const hasAlpha = hash[2] & 0x80;
246
+ const isLandscape = hash[4] & 0x80;
247
+ const lx = isLandscape ? (hasAlpha ? 5 : 7) : header & 7;
248
+ const ly = isLandscape ? header & 7 : hasAlpha ? 5 : 7;
249
+ return lx / ly;
250
+ }
251
+
252
+ /**
253
+ * Encodes an RGBA image to a PNG data URL. RGB should not be premultiplied by
254
+ * A. This is optimized for speed and simplicity and does not optimize for size
255
+ * at all. This doesn't do any compression (all values are stored uncompressed).
256
+ *
257
+ * @param w The width of the input image. Must be ≤100px.
258
+ * @param h The height of the input image. Must be ≤100px.
259
+ * @param rgba The pixels in the input image, row-by-row. Must have w*h*4 elements.
260
+ * @returns A data URL containing a PNG for the input image.
261
+ */
262
+ export function rgbaToDataURL(w: number, h: number, rgba: Uint8Array) {
263
+ const row = w * 4 + 1;
264
+ const idat = 6 + h * (5 + row);
265
+ const bytes = [
266
+ 137,
267
+ 80,
268
+ 78,
269
+ 71,
270
+ 13,
271
+ 10,
272
+ 26,
273
+ 10,
274
+ 0,
275
+ 0,
276
+ 0,
277
+ 13,
278
+ 73,
279
+ 72,
280
+ 68,
281
+ 82,
282
+ 0,
283
+ 0,
284
+ w >> 8,
285
+ w & 255,
286
+ 0,
287
+ 0,
288
+ h >> 8,
289
+ h & 255,
290
+ 8,
291
+ 6,
292
+ 0,
293
+ 0,
294
+ 0,
295
+ 0,
296
+ 0,
297
+ 0,
298
+ 0,
299
+ idat >>> 24,
300
+ (idat >> 16) & 255,
301
+ (idat >> 8) & 255,
302
+ idat & 255,
303
+ 73,
304
+ 68,
305
+ 65,
306
+ 84,
307
+ 120,
308
+ 1,
309
+ ];
310
+ const table = [
311
+ 0, 498536548, 997073096, 651767980, 1994146192, 1802195444, 1303535960, 1342533948, -306674912,
312
+ -267414716, -690576408, -882789492, -1687895376, -2032938284, -1609899400, -1111625188,
313
+ ];
314
+ let a = 1,
315
+ b = 0;
316
+ for (let y = 0, i = 0, end = row - 1; y < h; y++, end += row - 1) {
317
+ bytes.push(y + 1 < h ? 0 : 1, row & 255, row >> 8, ~row & 255, (row >> 8) ^ 255, 0);
318
+ for (b = (b + a) % 65521; i < end; i++) {
319
+ const u = rgba[i] & 255;
320
+ bytes.push(u);
321
+ a = (a + u) % 65521;
322
+ b = (b + a) % 65521;
323
+ }
324
+ }
325
+ bytes.push(
326
+ b >> 8,
327
+ b & 255,
328
+ a >> 8,
329
+ a & 255,
330
+ 0,
331
+ 0,
332
+ 0,
333
+ 0,
334
+ 0,
335
+ 0,
336
+ 0,
337
+ 0,
338
+ 73,
339
+ 69,
340
+ 78,
341
+ 68,
342
+ 174,
343
+ 66,
344
+ 96,
345
+ 130
346
+ );
347
+ for (let [start, end] of [
348
+ [12, 29],
349
+ [37, 41 + idat],
350
+ ]) {
351
+ let c = ~0;
352
+ for (let i = start; i < end; i++) {
353
+ c ^= bytes[i];
354
+ c = (c >>> 4) ^ table[c & 15];
355
+ c = (c >>> 4) ^ table[c & 15];
356
+ }
357
+ c = ~c;
358
+ bytes[end++] = c >>> 24;
359
+ bytes[end++] = (c >> 16) & 255;
360
+ bytes[end++] = (c >> 8) & 255;
361
+ bytes[end++] = c & 255;
362
+ }
363
+ return 'data:image/png;base64,' + btoa(String.fromCharCode(...bytes));
364
+ }
365
+
366
+ /**
367
+ * Decodes a ThumbHash to a PNG data URL. This is a convenience function that
368
+ * just calls "thumbHashToRGBA" followed by "rgbaToDataURL".
369
+ *
370
+ * @param hash The bytes of the ThumbHash.
371
+ * @returns A data URL containing a PNG for the rendered ThumbHash.
372
+ */
373
+ export function thumbHashToDataURL(hash: Uint8Array): string {
374
+ const image = thumbHashToRGBA(hash);
375
+ return rgbaToDataURL(image.w, image.h, image.rgba);
376
+ }
377
+
378
+ /**
379
+ * Convenience function added to the original thumbhash code, allows generating a thumbhash image directly from
380
+ * thumbhash string.
381
+ * @param thumbhash string from which thumbhashDataURL should be generated
382
+ * @returns A data URL containing a PNG for the rendered ThumbHash
383
+ */
384
+ export function thumbHashStringToDataURL(thumbhash: string): string {
385
+ const hash = Uint8Array.from(atob(thumbhash), (c) => c.charCodeAt(0));
386
+ return thumbHashToDataURL(hash);
387
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,109 @@
1
+ import {
2
+ ImageContentFit,
3
+ ImageContentPosition,
4
+ ImageContentPositionObject,
5
+ ImageContentPositionString,
6
+ ImageProps,
7
+ ImageTransition,
8
+ } from './Image.types';
9
+
10
+ let loggedResizeModeDeprecationWarning = false;
11
+ let loggedRepeatDeprecationWarning = false;
12
+ let loggedFadeDurationDeprecationWarning = false;
13
+
14
+ /**
15
+ * If the `contentFit` is not provided, it's resolved from the equivalent `resizeMode` prop
16
+ * that we support to provide compatibility with React Native Image.
17
+ */
18
+ export function resolveContentFit(
19
+ contentFit?: ImageContentFit,
20
+ resizeMode?: ImageProps['resizeMode']
21
+ ): ImageContentFit {
22
+ if (contentFit) {
23
+ return contentFit;
24
+ }
25
+ if (resizeMode) {
26
+ if (!loggedResizeModeDeprecationWarning) {
27
+ console.log('[expo-image]: Prop "resizeMode" is deprecated, use "contentFit" instead');
28
+ loggedResizeModeDeprecationWarning = true;
29
+ }
30
+
31
+ switch (resizeMode) {
32
+ case 'contain':
33
+ case 'cover':
34
+ return resizeMode;
35
+ case 'stretch':
36
+ return 'fill';
37
+ case 'center':
38
+ return 'scale-down';
39
+ case 'repeat':
40
+ if (!loggedRepeatDeprecationWarning) {
41
+ console.log('[expo-image]: Resize mode "repeat" is no longer supported');
42
+ loggedRepeatDeprecationWarning = true;
43
+ }
44
+ }
45
+ }
46
+ return 'cover';
47
+ }
48
+
49
+ /**
50
+ * It resolves a stringified form of the `contentPosition` prop to an object,
51
+ * which is the only form supported in the native code.
52
+ */
53
+ export function resolveContentPosition(
54
+ contentPosition?: ImageContentPosition
55
+ ): ImageContentPositionObject {
56
+ if (typeof contentPosition === 'string') {
57
+ const contentPositionStringMappings: Record<
58
+ ImageContentPositionString,
59
+ ImageContentPositionObject
60
+ > = {
61
+ center: { top: '50%', left: '50%' },
62
+ top: { top: 0, left: '50%' },
63
+ right: { top: '50%', right: 0 },
64
+ bottom: { bottom: 0, left: '50%' },
65
+ left: { top: '50%', left: 0 },
66
+ 'top center': { top: 0, left: '50%' },
67
+ 'top right': { top: 0, right: 0 },
68
+ 'top left': { top: 0, left: 0 },
69
+ 'right center': { top: '50%', right: 0 },
70
+ 'right top': { top: 0, right: 0 },
71
+ 'right bottom': { bottom: 0, right: 0 },
72
+ 'bottom center': { bottom: 0, left: '50%' },
73
+ 'bottom right': { bottom: 0, right: 0 },
74
+ 'bottom left': { bottom: 0, left: 0 },
75
+ 'left center': { top: '50%', left: 0 },
76
+ 'left top': { top: 0, left: 0 },
77
+ 'left bottom': { bottom: 0, left: 0 },
78
+ };
79
+ const contentPositionObject = contentPositionStringMappings[contentPosition];
80
+
81
+ if (!contentPositionObject) {
82
+ console.warn(`[expo-image]: Content position "${contentPosition}" is invalid`);
83
+ return contentPositionStringMappings.center;
84
+ }
85
+ return contentPositionObject;
86
+ }
87
+ return contentPosition ?? { top: '50%', left: '50%' };
88
+ }
89
+
90
+ /**
91
+ * If `transition` or `fadeDuration` is a number, it's resolved to a cross dissolve transition with the given duration.
92
+ * When `fadeDuration` is used, it logs an appropriate deprecation warning.
93
+ */
94
+ export function resolveTransition(
95
+ transition?: ImageProps['transition'],
96
+ fadeDuration?: ImageProps['fadeDuration']
97
+ ): ImageTransition | null {
98
+ if (typeof transition === 'number') {
99
+ return { duration: transition };
100
+ }
101
+ if (!transition && typeof fadeDuration === 'number') {
102
+ if (!loggedFadeDurationDeprecationWarning) {
103
+ console.warn('[expo-image]: Prop "fadeDuration" is deprecated, use "transition" instead');
104
+ loggedFadeDurationDeprecationWarning = true;
105
+ }
106
+ return { duration: fadeDuration };
107
+ }
108
+ return transition ?? null;
109
+ }