# HEIC decoder source and replacement instructions

The website serves `heic-to.mjs` as a separate module. It is exactly the
`dist/csp/heic-to.js` file from the official `heic-to@1.6.7` npm package;
only the filename has changed. No decoder modifications were made.

- Runtime file: `heic-to.mjs` (3,224,697 bytes).
- Runtime SHA-256: `e0a515585872c319c601c33551925c9d5fee31746caf0311291ed7e6364c592c`.
- heic-to commit: `78345a3fcdb4a1d70d513fce2646aeff0127f7ba`.
- Embedded decoder: libheif 1.23.7 and libde265 1.0.16.
- Source archive: `corresponding-source.tar.gz` in this same directory.
- License and copyright notices: `NOTICE.txt`, `LGPL-3.0.txt`,
  `GPL-3.0.txt`, `libheif-COPYING.txt`, and `libde265-COPYING.txt`.

## Source contents

The source archive contains the original heic-to source files, its build
script, its dependency lock files, and its original license and README.
Upstream generated `dist` files and demonstration pages are omitted:
they are not required to build the runtime module. The generated
libheif modules used by the original heic-to build remain included.

The archive also contains complete, byte-for-byte upstream source-release
archives for libheif 1.23.7 and libde265 1.0.16 in `upstream/`, including
their original source copyright notices and build scripts. The compiler
and general-purpose build tools are available separately from their
respective upstream projects. `provenance.json` records the original
download locations and artifact hashes.

## Build the JavaScript package

Extract `corresponding-source.tar.gz`. With Node.js and npm installed,
run the following in its `heic-to-1.6.7` directory:

```sh
npm ci
npm run build
```

The CSP entry point is then `dist/csp/heic-to.js`. The build script uses
the included generated decoder modules, so this step does not require
recompiling the C/C++ decoder. The upstream `.tool-versions` file and
package lock identify the original JavaScript tool versions.

## Modify the underlying decoder

Extract the two source-release archives in `upstream/`. The libheif
release contains `build-emscripten.sh`; the upstream heic-to README
documents its build procedure. Install and activate the Emscripten SDK,
CMake, Make, and the platform build tools required by those projects.

Create a `buildjs` directory within the extracted libheif source. Copy
the supplied `libde265-1.0.16.tar.gz` into that directory, so the upstream
build script can use the provided source rather than download it again.
From the `buildjs` directory, the CSP-compatible configuration is:

```sh
LIBDE265_VERSION=1.0.16 USE_UNSAFE_EVAL=0 USE_WASM=0 USE_ES6=1 ../build-emscripten.sh ..
```

The resulting ES module must default-export the libheif factory, as
expected by the original `src/worker.js` import. Use this module as
`heic-to-1.6.7/src/lib/libheif-without-unsafe-eval.js`, then rebuild the
JavaScript package. The upstream README describes a platform-specific
`llvm-nm` path adjustment for Homebrew; an activated standard Emscripten
SDK uses the path already present in the source-release build script.

Compiler versions may affect generated bytes; these instructions enable
modification and rebuilding rather than promise a byte-identical C/C++
rebuild with every toolchain. The original generated module is included
unchanged for rebuilding the original JavaScript wrapper.

## Replace the separately loaded library

In a local copy of this website, replace this directory's `heic-to.mjs`
with the rebuilt `dist/csp/heic-to.js`. Retain its ES-module exports and
compatible API. Rebuild or serve the website normally. The library is
not merged into the application bundle or protected by a runtime hash
check. Users may modify the library and debug those modifications under
the applicable LGPL terms.

The public API used by the website is:

```js
const { heicTo } = await import('./heic-to.mjs');
const bitmap = await heicTo({ blob: file, type: 'bitmap' });
// Draw the ImageBitmap as needed, then call bitmap.close().
```

`isHeic(file)` is also exported. The library decodes on the local device
using a lazy Blob worker. It does not provide an AbortSignal, a public
worker termination API, or an external conversion service. An application
may ignore and release a late result after cancellation; the underlying
in-progress conversion still completes. Concurrent conversions should
be limited on memory-constrained devices.

## Content Security Policy

This CSP build contains an embedded JavaScript decoder. It does not need
`unsafe-eval`, a native WebAssembly allowance, an external CDN, or a remote
worker file. The host must allow its same-origin module and the Blob
worker, for example `script-src 'self'` and `worker-src 'self' blob:`.
Keep the website's existing necessary image and other directives.

This distribution's license, source, and notices must remain available
alongside the served library. Link to `index.html` from the website's
software notices or credits.
