@su-engineering/heic 0.1.1 → 0.2.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/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { S as SourceColor, H as HeicWarning, a as SupportReport, D as DecodeOptions, b as DecodedImage, c as DecoderAdapter } from './types-Bv9KPnri.js';
2
- export { A as AdapterRequest, d as AdapterResult, O as OutputColorSpace, e as Strategy, T as TransformsApplied } from './types-Bv9KPnri.js';
1
+ import { S as SourceColor, H as HeicWarning, a as SupportReport, C as ConvertOptions, b as ConvertedImage, D as DecodeOptions, c as DecodedImage, d as DecoderAdapter } from './types-nCqKJJpp.js';
2
+ export { A as AdapterRequest, e as AdapterResult, O as OutputColorSpace, f as Strategy, T as TransformsApplied } from './types-nCqKJJpp.js';
3
3
 
4
4
  /**
5
5
  * Typed error hierarchy. The parser consumes hostile input and the decode path
@@ -433,6 +433,12 @@ declare function isHeic(input: BinaryInput): Promise<IsHeicResult>;
433
433
  * naming what was missing, rather than silently fetching a megabyte.
434
434
  */
435
435
  declare function decodeHeic(input: BinaryInput, options?: DecodeOptions): Promise<DecodedImage>;
436
+ /**
437
+ * Convert the primary HEIC image to JPEG or PNG using the browser's encoder.
438
+ * Uses the same explicit decode cascade as decodeHeic and releases all pixels.
439
+ * Source EXIF is not copied into the output file.
440
+ */
441
+ declare function convertHeic(input: BinaryInput, options?: ConvertOptions): Promise<ConvertedImage>;
436
442
  /**
437
443
  * Registers a wasm (or other) fallback adapter for every subsequent decode.
438
444
  *
@@ -442,4 +448,4 @@ declare function decodeHeic(input: BinaryInput, options?: DecodeOptions): Promis
442
448
  declare function registerDecoderAdapter(adapter: DecoderAdapter | undefined): void;
443
449
  declare function getRegisteredAdapter(): DecoderAdapter | undefined;
444
450
 
445
- export { type BinaryInput, DecodeOptions, DecodedImage, DecoderAdapter, type GridDescriptor, HeicAbortError, HeicDecodeError, HeicError, type HeicErrorContext, HeicParseError, HeicUnsupportedError, HeicWarning, type HeifFile, type HvcC, type ImagePlan, type IsHeicResult, type ItemInfo, type ItemLocation, type ItemProperties, type ItemProperty, type ItemReferences, type PlannedTile, SourceColor, SupportReport, type TileGroup, type TransformOp, decodeHeic, findProperty, getRegisteredAdapter, hvccToAnnexBPrologue, hvccToCodecString, isHeic, lengthPrefixedToAnnexB, parseGridPayload, parseHeif, parseHvcC, planDecode, probeSupport, propertiesForItem, readGrid, readItemData, registerDecoderAdapter };
451
+ export { type BinaryInput, ConvertOptions, ConvertedImage, DecodeOptions, DecodedImage, DecoderAdapter, type GridDescriptor, HeicAbortError, HeicDecodeError, HeicError, type HeicErrorContext, HeicParseError, HeicUnsupportedError, HeicWarning, type HeifFile, type HvcC, type ImagePlan, type IsHeicResult, type ItemInfo, type ItemLocation, type ItemProperties, type ItemProperty, type ItemReferences, type PlannedTile, SourceColor, SupportReport, type TileGroup, type TransformOp, convertHeic, decodeHeic, findProperty, getRegisteredAdapter, hvccToAnnexBPrologue, hvccToCodecString, isHeic, lengthPrefixedToAnnexB, parseGridPayload, parseHeif, parseHvcC, planDecode, probeSupport, propertiesForItem, readGrid, readItemData, registerDecoderAdapter };
package/dist/index.js CHANGED
@@ -1553,6 +1553,37 @@ async function decodeHeic(input, options = {}) {
1553
1553
  itemId: plan.primaryItemId
1554
1554
  });
1555
1555
  }
1556
+ async function convertHeic(input, options = {}) {
1557
+ const { type = "image/jpeg", quality = 0.92, ...decodeOptions } = options;
1558
+ if (type !== "image/jpeg" && type !== "image/png") {
1559
+ throw new TypeError("type must be image/jpeg or image/png");
1560
+ }
1561
+ if (!Number.isFinite(quality) || quality < 0 || quality > 1) {
1562
+ throw new RangeError("quality must be a finite number between 0 and 1");
1563
+ }
1564
+ const decoded = await decodeHeic(input, decodeOptions);
1565
+ let canvas;
1566
+ try {
1567
+ throwIfAborted(options.signal);
1568
+ canvas = createCanvas(decoded.width, decoded.height);
1569
+ const ctx = canvas.getContext("2d", { colorSpace: options.colorSpace ?? "srgb", alpha: false });
1570
+ if (!ctx) throw new HeicDecodeError("Could not get a 2d context for conversion", {});
1571
+ ctx.drawImage(decoded.image, 0, 0);
1572
+ const blob = await canvas.convertToBlob({ type, quality });
1573
+ throwIfAborted(options.signal);
1574
+ if (blob.type !== type || blob.size === 0) {
1575
+ throw new HeicDecodeError(`Browser could not encode ${type}`, {});
1576
+ }
1577
+ const { image: _image, ...metadata } = decoded;
1578
+ return { ...metadata, blob };
1579
+ } finally {
1580
+ decoded.image.close();
1581
+ if (canvas) {
1582
+ canvas.width = 0;
1583
+ canvas.height = 0;
1584
+ }
1585
+ }
1586
+ }
1556
1587
  var registeredAdapter;
1557
1588
  function registerDecoderAdapter(adapter) {
1558
1589
  registeredAdapter = adapter;
@@ -1643,6 +1674,6 @@ function describeError(error) {
1643
1674
  return error instanceof Error ? error.message : String(error);
1644
1675
  }
1645
1676
 
1646
- export { decodeHeic, findProperty, getRegisteredAdapter, hvccToAnnexBPrologue, hvccToCodecString, isHeic, lengthPrefixedToAnnexB, parseGridPayload, parseHeif, parseHvcC, planDecode, probeSupport, propertiesForItem, readGrid, readItemData, registerDecoderAdapter };
1677
+ export { convertHeic, decodeHeic, findProperty, getRegisteredAdapter, hvccToAnnexBPrologue, hvccToCodecString, isHeic, lengthPrefixedToAnnexB, parseGridPayload, parseHeif, parseHvcC, planDecode, probeSupport, propertiesForItem, readGrid, readItemData, registerDecoderAdapter };
1647
1678
  //# sourceMappingURL=index.js.map
1648
1679
  //# sourceMappingURL=index.js.map