beautiful-image 0.1.0

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.
package/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Beautiful Image
2
+
3
+ A Rust library for optimizing images in the browser using WebAssembly.
4
+
5
+ ## What it does
6
+
7
+ Optimizes images by resizing and compressing them to JPEG format. Perfect for reducing image file sizes before uploading to your website.
8
+
9
+ ## Features
10
+
11
+ - Resize images to a specific width (maintains aspect ratio)
12
+ - Compress to JPEG with adjustable quality
13
+ - Two resize modes:
14
+ - **Standard**: Fast resizing (Triangle filter)
15
+ - **HighQuality**: Better quality (Lanczos3 filter)
16
+ - Supports JPEG, PNG, and WebP input formats
17
+ - Runs in the browser via WebAssembly
18
+
19
+ ## Installation
20
+
21
+ First, install [wasm-pack](https://rustwasm.github.io/wasm-pack/):
22
+
23
+ ```bash
24
+ cargo install wasm-pack
25
+ ```
26
+
27
+ Then build the project:
28
+
29
+ ```bash
30
+ wasm-pack build --target web
31
+ ```
32
+
33
+ This will generate a `pkg/` folder with the necessary files.
34
+
35
+ ## Usage in JavaScript
36
+
37
+ ```html
38
+ <script type="module">
39
+ import init, { optimize_image, ResizeMode } from "./pkg/beautiful_image.js";
40
+
41
+ async function optimizeImage() {
42
+ // Initialize WASM
43
+ await init();
44
+
45
+ // Get image bytes (e.g., from a file input)
46
+ const file = document.getElementById('upload').files[0];
47
+ const arrayBuffer = await file.arrayBuffer();
48
+ const bytes = new Uint8Array(arrayBuffer);
49
+
50
+ // Optimize: resize to 800px width with quality 85
51
+ const result = optimize_image(
52
+ bytes, // Image bytes
53
+ 800, // New width in pixels
54
+ 85, // Quality (0-100)
55
+ ResizeMode.HighQuality // Resize mode
56
+ );
57
+
58
+ // Create blob to display/download
59
+ const blob = new Blob([result], { type: "image/jpeg" });
60
+ const url = URL.createObjectURL(blob);
61
+ }
62
+
63
+ optimizeImage();
64
+ </script>
65
+ ```
66
+
67
+ ## License
68
+
69
+ MIT
@@ -0,0 +1,11 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export enum ResizeMode {
5
+ Standard = 0,
6
+ HighQuality = 1,
7
+ }
8
+
9
+ export function main_js(): void;
10
+
11
+ export function optimize_image(datos_entrada: Uint8Array, new_width: number, quality: number, resize_mode?: ResizeMode | null): Uint8Array;
@@ -0,0 +1,5 @@
1
+ import * as wasm from "./beautiful_image_bg.wasm";
2
+ export * from "./beautiful_image_bg.js";
3
+ import { __wbg_set_wasm } from "./beautiful_image_bg.js";
4
+ __wbg_set_wasm(wasm);
5
+ wasm.__wbindgen_start();
@@ -0,0 +1,193 @@
1
+ let wasm;
2
+ export function __wbg_set_wasm(val) {
3
+ wasm = val;
4
+ }
5
+
6
+ function getArrayU8FromWasm0(ptr, len) {
7
+ ptr = ptr >>> 0;
8
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
9
+ }
10
+
11
+ let cachedDataViewMemory0 = null;
12
+ function getDataViewMemory0() {
13
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
14
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
15
+ }
16
+ return cachedDataViewMemory0;
17
+ }
18
+
19
+ function getStringFromWasm0(ptr, len) {
20
+ ptr = ptr >>> 0;
21
+ return decodeText(ptr, len);
22
+ }
23
+
24
+ let cachedUint8ArrayMemory0 = null;
25
+ function getUint8ArrayMemory0() {
26
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
27
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
28
+ }
29
+ return cachedUint8ArrayMemory0;
30
+ }
31
+
32
+ function isLikeNone(x) {
33
+ return x === undefined || x === null;
34
+ }
35
+
36
+ function passArray8ToWasm0(arg, malloc) {
37
+ const ptr = malloc(arg.length * 1, 1) >>> 0;
38
+ getUint8ArrayMemory0().set(arg, ptr / 1);
39
+ WASM_VECTOR_LEN = arg.length;
40
+ return ptr;
41
+ }
42
+
43
+ function passStringToWasm0(arg, malloc, realloc) {
44
+ if (realloc === undefined) {
45
+ const buf = cachedTextEncoder.encode(arg);
46
+ const ptr = malloc(buf.length, 1) >>> 0;
47
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
48
+ WASM_VECTOR_LEN = buf.length;
49
+ return ptr;
50
+ }
51
+
52
+ let len = arg.length;
53
+ let ptr = malloc(len, 1) >>> 0;
54
+
55
+ const mem = getUint8ArrayMemory0();
56
+
57
+ let offset = 0;
58
+
59
+ for (; offset < len; offset++) {
60
+ const code = arg.charCodeAt(offset);
61
+ if (code > 0x7F) break;
62
+ mem[ptr + offset] = code;
63
+ }
64
+ if (offset !== len) {
65
+ if (offset !== 0) {
66
+ arg = arg.slice(offset);
67
+ }
68
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
69
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
70
+ const ret = cachedTextEncoder.encodeInto(arg, view);
71
+
72
+ offset += ret.written;
73
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
74
+ }
75
+
76
+ WASM_VECTOR_LEN = offset;
77
+ return ptr;
78
+ }
79
+
80
+ function takeFromExternrefTable0(idx) {
81
+ const value = wasm.__wbindgen_externrefs.get(idx);
82
+ wasm.__externref_table_dealloc(idx);
83
+ return value;
84
+ }
85
+
86
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
87
+ cachedTextDecoder.decode();
88
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
89
+ let numBytesDecoded = 0;
90
+ function decodeText(ptr, len) {
91
+ numBytesDecoded += len;
92
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
93
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
94
+ cachedTextDecoder.decode();
95
+ numBytesDecoded = len;
96
+ }
97
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
98
+ }
99
+
100
+ const cachedTextEncoder = new TextEncoder();
101
+
102
+ if (!('encodeInto' in cachedTextEncoder)) {
103
+ cachedTextEncoder.encodeInto = function (arg, view) {
104
+ const buf = cachedTextEncoder.encode(arg);
105
+ view.set(buf);
106
+ return {
107
+ read: arg.length,
108
+ written: buf.length
109
+ };
110
+ }
111
+ }
112
+
113
+ let WASM_VECTOR_LEN = 0;
114
+
115
+ /**
116
+ * @enum {0 | 1}
117
+ */
118
+ export const ResizeMode = Object.freeze({
119
+ Standard: 0, "0": "Standard",
120
+ HighQuality: 1, "1": "HighQuality",
121
+ });
122
+
123
+ export function main_js() {
124
+ wasm.main_js();
125
+ }
126
+
127
+ /**
128
+ * @param {Uint8Array} datos_entrada
129
+ * @param {number} new_width
130
+ * @param {number} quality
131
+ * @param {ResizeMode | null} [resize_mode]
132
+ * @returns {Uint8Array}
133
+ */
134
+ export function optimize_image(datos_entrada, new_width, quality, resize_mode) {
135
+ const ptr0 = passArray8ToWasm0(datos_entrada, wasm.__wbindgen_malloc);
136
+ const len0 = WASM_VECTOR_LEN;
137
+ const ret = wasm.optimize_image(ptr0, len0, new_width, quality, isLikeNone(resize_mode) ? 2 : resize_mode);
138
+ if (ret[3]) {
139
+ throw takeFromExternrefTable0(ret[2]);
140
+ }
141
+ var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
142
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
143
+ return v2;
144
+ }
145
+
146
+ export function __wbg___wbindgen_throw_dd24417ed36fc46e(arg0, arg1) {
147
+ throw new Error(getStringFromWasm0(arg0, arg1));
148
+ };
149
+
150
+ export function __wbg_error_7534b8e9a36f1ab4(arg0, arg1) {
151
+ let deferred0_0;
152
+ let deferred0_1;
153
+ try {
154
+ deferred0_0 = arg0;
155
+ deferred0_1 = arg1;
156
+ console.error(getStringFromWasm0(arg0, arg1));
157
+ } finally {
158
+ wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
159
+ }
160
+ };
161
+
162
+ export function __wbg_log_2638f9d948a0513e(arg0, arg1) {
163
+ console.log(getStringFromWasm0(arg0, arg1));
164
+ };
165
+
166
+ export function __wbg_new_8a6f238a6ece86ea() {
167
+ const ret = new Error();
168
+ return ret;
169
+ };
170
+
171
+ export function __wbg_stack_0ed75d68575b0f3c(arg0, arg1) {
172
+ const ret = arg1.stack;
173
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
174
+ const len1 = WASM_VECTOR_LEN;
175
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
176
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
177
+ };
178
+
179
+ export function __wbindgen_cast_2241b6af4c4b2941(arg0, arg1) {
180
+ // Cast intrinsic for `Ref(String) -> Externref`.
181
+ const ret = getStringFromWasm0(arg0, arg1);
182
+ return ret;
183
+ };
184
+
185
+ export function __wbindgen_init_externref_table() {
186
+ const table = wasm.__wbindgen_externrefs;
187
+ const offset = table.grow(4);
188
+ table.set(0, undefined);
189
+ table.set(offset + 0, undefined);
190
+ table.set(offset + 1, null);
191
+ table.set(offset + 2, true);
192
+ table.set(offset + 3, false);
193
+ };
Binary file
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "beautiful-image",
3
+ "type": "module",
4
+ "description": "A Rust library for optimizing images for the web",
5
+ "version": "0.1.0",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/devcastech/beautiful-image"
10
+ },
11
+ "files": [
12
+ "beautiful_image_bg.wasm",
13
+ "beautiful_image.js",
14
+ "beautiful_image_bg.js",
15
+ "beautiful_image.d.ts"
16
+ ],
17
+ "main": "beautiful_image.js",
18
+ "types": "beautiful_image.d.ts",
19
+ "sideEffects": [
20
+ "./beautiful_image.js",
21
+ "./snippets/*"
22
+ ]
23
+ }