qrdx 0.0.56 → 0.0.58
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 +27 -6
- package/dist/angular.js +33 -3
- package/dist/angular.js.map +1 -1
- package/dist/angular.mjs +33 -3
- package/dist/angular.mjs.map +1 -1
- package/dist/core.js +7 -7
- package/dist/core.js.map +1 -1
- package/dist/core.mjs +7 -7
- package/dist/core.mjs.map +1 -1
- package/dist/element.browser.mjs +6 -6
- package/dist/element.browser.mjs.map +1 -1
- package/dist/element.js +1 -1
- package/dist/element.js.map +1 -1
- package/dist/element.mjs +1 -1
- package/dist/element.mjs.map +1 -1
- package/dist/frames-index.js +1 -1
- package/dist/frames-index.js.map +1 -1
- package/dist/frames-index.mjs +1 -1
- package/dist/frames-index.mjs.map +1 -1
- package/dist/frames-official.js +1 -1
- package/dist/frames-official.js.map +1 -1
- package/dist/frames-official.mjs +1 -1
- package/dist/frames-official.mjs.map +1 -1
- package/dist/index.js +7 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +7 -7
- package/dist/index.mjs.map +1 -1
- package/dist/qr-code-canvas.js +1 -1
- package/dist/qr-code-canvas.js.map +1 -1
- package/dist/qr-code-canvas.mjs +1 -1
- package/dist/qr-code-canvas.mjs.map +1 -1
- package/dist/schemas.js +1 -1
- package/dist/schemas.js.map +1 -1
- package/dist/schemas.mjs +1 -1
- package/dist/schemas.mjs.map +1 -1
- package/dist/src/angular.d.ts +9 -3
- package/dist/src/bmp.d.ts +7 -0
- package/dist/src/bmp.d.ts.map +1 -0
- package/dist/src/download.d.ts +39 -6
- package/dist/src/download.d.ts.map +1 -1
- package/dist/src/element.d.ts +10 -1
- package/dist/src/element.d.ts.map +1 -1
- package/dist/src/engine.d.ts +6 -2
- package/dist/src/engine.d.ts.map +1 -1
- package/dist/src/eps.d.ts.map +1 -1
- package/dist/src/export.d.ts.map +1 -1
- package/dist/src/frame.d.ts +24 -0
- package/dist/src/frame.d.ts.map +1 -1
- package/dist/src/frames/catalogue/all.d.ts.map +1 -1
- package/dist/src/frames/catalogue/batches.d.ts +6 -0
- package/dist/src/frames/catalogue/batches.d.ts.map +1 -0
- package/dist/src/frames/catalogue/call-to-action.d.ts.map +1 -1
- package/dist/src/frames/catalogue/counts.d.ts +4 -2
- package/dist/src/frames/catalogue/counts.d.ts.map +1 -1
- package/dist/src/frames/catalogue/index.d.ts.map +1 -1
- package/dist/src/frames/catalogue/objects.d.ts +5 -0
- package/dist/src/frames/catalogue/objects.d.ts.map +1 -0
- package/dist/src/frames/catalogue/playful.d.ts +5 -0
- package/dist/src/frames/catalogue/playful.d.ts.map +1 -0
- package/dist/src/frames/catalogue/tags.d.ts.map +1 -1
- package/dist/src/frames/collections.d.ts.map +1 -1
- package/dist/src/frames/official-types.d.ts +1 -1
- package/dist/src/frames/official-types.d.ts.map +1 -1
- package/dist/src/frames/official.d.ts.map +1 -1
- package/dist/src/gif.d.ts +40 -0
- package/dist/src/gif.d.ts.map +1 -0
- package/dist/src/helpers.d.ts +1 -1
- package/dist/src/helpers.d.ts.map +1 -1
- package/dist/src/ico.d.ts +11 -0
- package/dist/src/ico.d.ts.map +1 -0
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/looks.d.ts +101 -0
- package/dist/src/looks.d.ts.map +1 -0
- package/dist/src/plain.d.ts.map +1 -1
- package/dist/src/print.d.ts.map +1 -1
- package/dist/src/qr-code-canvas.d.ts.map +1 -1
- package/dist/src/qr-code-props.d.ts.map +1 -1
- package/dist/src/qr-frame-svg.d.ts +10 -0
- package/dist/src/qr-frame-svg.d.ts.map +1 -0
- package/dist/src/scan-score.d.ts.map +1 -1
- package/dist/src/utils/bmp.d.ts +18 -0
- package/dist/src/utils/bmp.d.ts.map +1 -0
- package/dist/src/utils/gif.d.ts +61 -0
- package/dist/src/utils/gif.d.ts.map +1 -0
- package/dist/src/utils/ico.d.ts +30 -0
- package/dist/src/utils/ico.d.ts.map +1 -0
- package/dist/src/utils/parse-color.d.ts +1 -1
- package/dist/src/utils/parse-color.d.ts.map +1 -1
- package/dist/src/utils/print-shared.d.ts.map +1 -1
- package/dist/src/utils/svg-painter.d.ts +13 -0
- package/dist/src/utils/svg-painter.d.ts.map +1 -1
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/vanilla-api.d.ts +2 -2
- package/dist/src/vanilla-api.d.ts.map +1 -1
- package/dist/src/vue.d.ts +2 -2
- package/dist/src/vue.d.ts.map +1 -1
- package/dist/types/constants.d.ts +41 -0
- package/dist/types/constants.d.ts.map +1 -1
- package/dist/types/index.js +2 -2
- package/dist/types/index.js.map +1 -1
- package/dist/types/index.mjs +2 -2
- package/dist/types/index.mjs.map +1 -1
- package/dist/types/plain.d.ts +2 -2
- package/dist/types/plain.d.ts.map +1 -1
- package/dist/types/qr.d.ts +399 -1
- package/dist/types/qr.d.ts.map +1 -1
- package/dist/types/react.d.ts +10 -1
- package/dist/types/react.d.ts.map +1 -1
- package/dist/types/schemas.d.ts +1 -1
- package/dist/types/schemas.d.ts.map +1 -1
- package/dist/vanilla.browser.mjs +34 -34
- package/dist/vanilla.browser.mjs.map +1 -1
- package/dist/vue.js +1 -1
- package/dist/vue.js.map +1 -1
- package/dist/vue.mjs +1 -1
- package/dist/vue.mjs.map +1 -1
- package/package.json +6 -2
- package/src/angular.ts +14 -2
- package/src/bmp.ts +25 -0
- package/src/download.tsx +128 -8
- package/src/element.ts +16 -1
- package/src/engine.ts +23 -0
- package/src/eps.ts +8 -1
- package/src/export.ts +22 -0
- package/src/frame.tsx +51 -0
- package/src/frames/catalogue/all.ts +202 -0
- package/src/frames/catalogue/batches.ts +114 -0
- package/src/frames/catalogue/call-to-action.ts +58 -0
- package/src/frames/catalogue/counts.ts +4 -2
- package/src/frames/catalogue/index.ts +808 -0
- package/src/frames/catalogue/objects.ts +65 -0
- package/src/frames/catalogue/playful.ts +97 -0
- package/src/frames/catalogue/tags.ts +137 -0
- package/src/frames/collections.ts +2 -0
- package/src/frames/official/badge-bottom-light.json +91 -0
- package/src/frames/official/badge-bottom.json +79 -0
- package/src/frames/official/billboard-campaign.json +274 -0
- package/src/frames/official/billboard-light.json +287 -0
- package/src/frames/official/bookmark-light.json +88 -0
- package/src/frames/official/bookmark-strip.json +83 -0
- package/src/frames/official/border-medium.json +71 -0
- package/src/frames/official/border-thick.json +71 -0
- package/src/frames/official/border-thin.json +71 -0
- package/src/frames/official/bottle-label.json +136 -0
- package/src/frames/official/bottle-light.json +133 -0
- package/src/frames/official/camera-viewfinder.json +174 -0
- package/src/frames/official/cassette-tape.json +236 -0
- package/src/frames/official/cat-with-card-light.json +246 -0
- package/src/frames/official/cat-with-card.json +234 -0
- package/src/frames/official/cinema-marquee.json +537 -0
- package/src/frames/official/circle-viewfinder.json +87 -0
- package/src/frames/official/clapperboard-light.json +163 -0
- package/src/frames/official/clapperboard-scene.json +151 -0
- package/src/frames/official/comic-panel.json +386 -0
- package/src/frames/official/corner-tab-light.json +101 -0
- package/src/frames/official/corner-tab.json +89 -0
- package/src/frames/official/crystal-ball.json +105 -0
- package/src/frames/official/delivery-box-light.json +132 -0
- package/src/frames/official/delivery-box-track.json +132 -0
- package/src/frames/official/fish-tank.json +276 -0
- package/src/frames/official/flag-light.json +136 -0
- package/src/frames/official/flag-team.json +127 -0
- package/src/frames/official/floppy-disk.json +106 -0
- package/src/frames/official/flying-saucer.json +128 -0
- package/src/frames/official/gallery-frame.json +195 -0
- package/src/frames/official/globe-stand.json +136 -0
- package/src/frames/official/gumball-machine.json +451 -0
- package/src/frames/official/heart-light.json +59 -0
- package/src/frames/official/heart-scan.json +51 -0
- package/src/frames/official/hexagon-light.json +87 -0
- package/src/frames/official/hexagon-scan.json +72 -0
- package/src/frames/official/hot-air-balloon.json +132 -0
- package/src/frames/official/jigsaw-piece-light.json +68 -0
- package/src/frames/official/jigsaw-piece.json +60 -0
- package/src/frames/official/laptop-screen.json +137 -0
- package/src/frames/official/lighthouse-tower.json +132 -0
- package/src/frames/official/magnifying-glass.json +85 -0
- package/src/frames/official/mailbox-flag.json +112 -0
- package/src/frames/official/mailbox-light.json +120 -0
- package/src/frames/official/map-pin-light.json +76 -0
- package/src/frames/official/map-pin.json +58 -0
- package/src/frames/official/matchbox-drawer.json +170 -0
- package/src/frames/official/microwave-door.json +220 -0
- package/src/frames/official/milk-carton.json +129 -0
- package/src/frames/official/monitor-screen.json +111 -0
- package/src/frames/official/monster-mouth.json +180 -0
- package/src/frames/official/mug-light.json +138 -0
- package/src/frames/official/mug-refill.json +125 -0
- package/src/frames/official/museum-plaque-light.json +187 -0
- package/src/frames/official/museum-plaque.json +187 -0
- package/src/frames/official/music-player-light.json +151 -0
- package/src/frames/official/music-player.json +136 -0
- package/src/frames/official/octagon-light.json +126 -0
- package/src/frames/official/octagon-sign.json +120 -0
- package/src/frames/official/open-book.json +156 -0
- package/src/frames/official/parking-meter.json +107 -0
- package/src/frames/official/passport-stamp.json +132 -0
- package/src/frames/official/pizza-box.json +122 -0
- package/src/frames/official/polka-dot-border.json +502 -0
- package/src/frames/official/popcorn-bucket.json +305 -0
- package/src/frames/official/porthole.json +264 -0
- package/src/frames/official/raised-card-thin.json +111 -0
- package/src/frames/official/raised-card.json +99 -0
- package/src/frames/official/retro-radio.json +180 -0
- package/src/frames/official/ribbon-top-light.json +125 -0
- package/src/frames/official/ribbon-top.json +113 -0
- package/src/frames/official/robot-chest.json +197 -0
- package/src/frames/official/roll-up-banner-light.json +137 -0
- package/src/frames/official/roll-up-banner.json +125 -0
- package/src/frames/official/round-tag-light.json +86 -0
- package/src/frames/official/round-tag.json +79 -0
- package/src/frames/official/sailboat-sail.json +99 -0
- package/src/frames/official/scroll-story.json +184 -0
- package/src/frames/official/sketch-border.json +92 -0
- package/src/frames/official/sketch-circle.json +57 -0
- package/src/frames/official/snow-globe.json +170 -0
- package/src/frames/official/soda-can.json +160 -0
- package/src/frames/official/spin-rays.json +126 -0
- package/src/frames/official/spiral-notebook.json +476 -0
- package/src/frames/official/stage-curtains.json +303 -0
- package/src/frames/official/starburst-light.json +128 -0
- package/src/frames/official/starburst.json +118 -0
- package/src/frames/official/swirl-border.json +122 -0
- package/src/frames/official/tablet-screen.json +130 -0
- package/src/frames/official/text-frame.json +79 -0
- package/src/frames/official/toaster-toast.json +114 -0
- package/src/frames/official/train-window.json +176 -0
- package/src/frames/official/treasure-map.json +207 -0
- package/src/frames/official/tv-set.json +256 -0
- package/src/frames/official/vending-machine.json +218 -0
- package/src/frames/official/video-player.json +118 -0
- package/src/frames/official/video-tape.json +137 -0
- package/src/frames/official/vinyl-sleeve-light.json +139 -0
- package/src/frames/official/vinyl-sleeve.json +127 -0
- package/src/frames/official/washing-machine.json +146 -0
- package/src/frames/official-types.ts +3 -1
- package/src/frames/official.ts +4 -3
- package/src/gif.ts +188 -0
- package/src/helpers.ts +12 -0
- package/src/ico.ts +55 -0
- package/src/index.tsx +2 -0
- package/src/looks.ts +467 -0
- package/src/plain.tsx +6 -0
- package/src/print.ts +12 -0
- package/src/qr-code-canvas.tsx +23 -2
- package/src/qr-code-props.ts +22 -0
- package/src/qr-frame-svg.tsx +26 -0
- package/src/scan-score.ts +38 -6
- package/src/utils/bmp.ts +126 -0
- package/src/utils/gif.ts +632 -0
- package/src/utils/ico.ts +150 -0
- package/src/utils/parse-color.ts +163 -23
- package/src/utils/print-shared.ts +25 -1
- package/src/utils/svg-painter.ts +18 -0
- package/src/utils.tsx +136 -17
- package/src/vanilla-api.ts +2 -2
- package/src/vue.ts +8 -2
- package/types/constants.ts +47 -0
- package/types/plain.ts +19 -0
- package/types/qr.ts +98 -0
- package/types/react.ts +11 -1
- package/types/schemas.ts +5 -0
package/src/scan-score.ts
CHANGED
|
@@ -17,8 +17,13 @@ import { withQRSize } from "./download";
|
|
|
17
17
|
import { getQRAsSVGDataUri } from "./export";
|
|
18
18
|
import { getFrameDimensions, getFrameGapRatio, isFrameActive } from "./frame";
|
|
19
19
|
import { getLogoBlockDamage } from "./logo-budget";
|
|
20
|
+
import {
|
|
21
|
+
getEyeStyleRisk,
|
|
22
|
+
getHaloRisk,
|
|
23
|
+
resolveHalo,
|
|
24
|
+
weakestEyePattern,
|
|
25
|
+
} from "./looks";
|
|
20
26
|
import { circleQuietZone, getLayoutMargin, isShapedCode } from "./qr-shape";
|
|
21
|
-
import { getStylePairRisk } from "./scan-risk";
|
|
22
27
|
import { getTemplateCodeShare } from "./template-placement";
|
|
23
28
|
import { getImageSettings, getLuminance, resolveErrorLevel } from "./utils";
|
|
24
29
|
import { parseColor, type RGBAColor } from "./utils/parse-color";
|
|
@@ -519,6 +524,11 @@ export function getScanContrast(
|
|
|
519
524
|
...perEye,
|
|
520
525
|
];
|
|
521
526
|
const lightColors = samples(props.bgColor, DEFAULT_BGCOLOR);
|
|
527
|
+
// A halo wraps every dark shape in its color: the code sits on it too
|
|
528
|
+
const haloColor = resolveHalo(props.halo, props)?.color;
|
|
529
|
+
if (haloColor) {
|
|
530
|
+
lightColors.push({ color: haloColor, picture: false });
|
|
531
|
+
}
|
|
522
532
|
// A transparent background prints on whatever is behind it; assume paper.
|
|
523
533
|
const background = (
|
|
524
534
|
lightColors.length > 0
|
|
@@ -783,9 +793,25 @@ function quietZoneCheck(props: QRProps): { check: ScanCheck; penalty: number } {
|
|
|
783
793
|
};
|
|
784
794
|
}
|
|
785
795
|
|
|
796
|
+
/** A halo the engine refuses to draw (looks.ts getHaloRisk): the design asked for a look it can't have. */
|
|
797
|
+
function haloCheck(props: QRProps): {
|
|
798
|
+
check: ScanCheck | null;
|
|
799
|
+
penalty: number;
|
|
800
|
+
} {
|
|
801
|
+
const risk = getHaloRisk(props);
|
|
802
|
+
if (!risk) {
|
|
803
|
+
return { check: null, penalty: 0 };
|
|
804
|
+
}
|
|
805
|
+
return {
|
|
806
|
+
check: { id: "halo", passed: false, message: risk.message, fix: risk.fix },
|
|
807
|
+
penalty: 15,
|
|
808
|
+
};
|
|
809
|
+
}
|
|
810
|
+
|
|
786
811
|
function eyeCheck(props: QRProps): { check: ScanCheck; penalty: number } {
|
|
787
|
-
// QRCodeSVG draws "gear" eyes when no pattern is given
|
|
788
|
-
|
|
812
|
+
// QRCodeSVG draws "gear" eyes when no pattern is given; one fragile eye
|
|
813
|
+
// among the three (eyeShapes) is judged like all three.
|
|
814
|
+
const pattern = weakestEyePattern(props);
|
|
789
815
|
if (pattern === "curly") {
|
|
790
816
|
return {
|
|
791
817
|
check: {
|
|
@@ -936,9 +962,12 @@ async function scoreArtwork(
|
|
|
936
962
|
penalty += quietZone.penalty;
|
|
937
963
|
const eye = eyeCheck(props);
|
|
938
964
|
penalty += eye.penalty;
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
965
|
+
const halo = haloCheck(props);
|
|
966
|
+
penalty += halo.penalty;
|
|
967
|
+
// Known-weak style pairs and centres (see scan-risk.ts), on any of the
|
|
968
|
+
// three eyes; weak eyes on their own are already covered by the
|
|
969
|
+
// eye-shape check above.
|
|
970
|
+
const styleRisk = getEyeStyleRisk(props);
|
|
942
971
|
const pairRisk = styleRisk?.kind === "eye" ? null : styleRisk;
|
|
943
972
|
// Sharp-render pairs fail one strict decoder only (see scan-risk.ts): they
|
|
944
973
|
// show as a check with its fix but cost nothing until a phone test
|
|
@@ -975,6 +1004,9 @@ async function scoreArtwork(
|
|
|
975
1004
|
checks.push(logo.check);
|
|
976
1005
|
}
|
|
977
1006
|
checks.push(eye.check, quietZone.check);
|
|
1007
|
+
if (halo.check) {
|
|
1008
|
+
checks.push(halo.check);
|
|
1009
|
+
}
|
|
978
1010
|
if (pairRisk) {
|
|
979
1011
|
checks.push({
|
|
980
1012
|
id: "style-pair",
|
package/src/utils/bmp.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: BMP format constants */
|
|
2
|
+
/** biome-ignore-all lint/suspicious/noBitwiseOperators: row padding and little-endian fields */
|
|
3
|
+
// A BMP writer in plain TypeScript with no DOM. Canvases can't encode BMP
|
|
4
|
+
// (toBlob("image/bmp") hands back a PNG), so every browser, and the server,
|
|
5
|
+
// uses this one.
|
|
6
|
+
//
|
|
7
|
+
// The file is an uncompressed 24-bit bitmap: a 14-byte file header, a 40-byte
|
|
8
|
+
// BITMAPINFOHEADER, then the pixel rows bottom-up in blue-green-red order,
|
|
9
|
+
// each row padded to a multiple of 4 bytes. 24-bit has no alpha channel, so
|
|
10
|
+
// a transparent or see-through pixel is laid over white (the legacy readers
|
|
11
|
+
// BMP is for don't agree on 32-bit alpha).
|
|
12
|
+
|
|
13
|
+
/** Largest width or height written. */
|
|
14
|
+
export const BMP_MAX_DIMENSION = 16_384;
|
|
15
|
+
|
|
16
|
+
const FILE_HEADER_SIZE = 14;
|
|
17
|
+
const INFO_HEADER_SIZE = 40;
|
|
18
|
+
const HEADER_SIZE = FILE_HEADER_SIZE + INFO_HEADER_SIZE;
|
|
19
|
+
const BYTES_PER_PIXEL = 3;
|
|
20
|
+
/** 72 pixels per inch, in pixels per metre, as most writers record it. */
|
|
21
|
+
const PIXELS_PER_METRE = 2835;
|
|
22
|
+
const OPAQUE = 255;
|
|
23
|
+
|
|
24
|
+
/** Bytes in one stored row: the pixels, padded to a multiple of 4. */
|
|
25
|
+
const rowStride = (width: number): number => (width * BYTES_PER_PIXEL + 3) & ~3;
|
|
26
|
+
|
|
27
|
+
/** A colour channel laid over white at `alpha` (0 to 255). */
|
|
28
|
+
const overWhite = (channel: number, alpha: number): number =>
|
|
29
|
+
Math.floor((channel * alpha + OPAQUE * (OPAQUE - alpha) + 127) / OPAQUE);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* RGBA pixels (as a canvas's getImageData returns them: `width * height * 4`
|
|
33
|
+
* bytes, top row first) as a 24-bit BMP file. Throws a RangeError for a size
|
|
34
|
+
* that is not a whole number from 1 to BMP_MAX_DIMENSION, or pixels that
|
|
35
|
+
* don't match it.
|
|
36
|
+
*/
|
|
37
|
+
export function encodeBMP(
|
|
38
|
+
rgba: ArrayLike<number>,
|
|
39
|
+
width: number,
|
|
40
|
+
height: number
|
|
41
|
+
): Uint8Array {
|
|
42
|
+
if (
|
|
43
|
+
!(Number.isInteger(width) && Number.isInteger(height)) ||
|
|
44
|
+
width < 1 ||
|
|
45
|
+
height < 1 ||
|
|
46
|
+
width > BMP_MAX_DIMENSION ||
|
|
47
|
+
height > BMP_MAX_DIMENSION
|
|
48
|
+
) {
|
|
49
|
+
throw new RangeError(
|
|
50
|
+
`BMP width and height must be whole numbers from 1 to ${BMP_MAX_DIMENSION}, got ${width} x ${height}`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
if (rgba.length !== width * height * 4) {
|
|
54
|
+
throw new RangeError(
|
|
55
|
+
`Expected ${width * height * 4} RGBA bytes for ${width} x ${height}, got ${rgba.length}`
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const stride = rowStride(width);
|
|
60
|
+
const imageSize = stride * height;
|
|
61
|
+
const file = new Uint8Array(HEADER_SIZE + imageSize);
|
|
62
|
+
const view = new DataView(file.buffer);
|
|
63
|
+
|
|
64
|
+
file[0] = 0x42; // "B"
|
|
65
|
+
file[1] = 0x4d; // "M"
|
|
66
|
+
view.setUint32(2, file.length, true);
|
|
67
|
+
view.setUint32(10, HEADER_SIZE, true); // where the pixels start
|
|
68
|
+
view.setUint32(14, INFO_HEADER_SIZE, true);
|
|
69
|
+
view.setInt32(18, width, true);
|
|
70
|
+
view.setInt32(22, height, true); // positive: rows run bottom to top
|
|
71
|
+
view.setUint16(26, 1, true); // colour planes
|
|
72
|
+
view.setUint16(28, 24, true); // bits per pixel
|
|
73
|
+
view.setUint32(30, 0, true); // BI_RGB: no compression
|
|
74
|
+
view.setUint32(34, imageSize, true);
|
|
75
|
+
view.setInt32(38, PIXELS_PER_METRE, true);
|
|
76
|
+
view.setInt32(42, PIXELS_PER_METRE, true);
|
|
77
|
+
// The colour table counts (offsets 46 and 50) stay 0: all colours, no table.
|
|
78
|
+
|
|
79
|
+
for (let y = 0; y < height; y++) {
|
|
80
|
+
let source = y * width * 4;
|
|
81
|
+
let target = HEADER_SIZE + (height - 1 - y) * stride;
|
|
82
|
+
for (let x = 0; x < width; x++) {
|
|
83
|
+
const alpha = rgba[source + 3] as number;
|
|
84
|
+
const red = rgba[source] as number;
|
|
85
|
+
const green = rgba[source + 1] as number;
|
|
86
|
+
const blue = rgba[source + 2] as number;
|
|
87
|
+
if (alpha === OPAQUE) {
|
|
88
|
+
file[target] = blue;
|
|
89
|
+
file[target + 1] = green;
|
|
90
|
+
file[target + 2] = red;
|
|
91
|
+
} else {
|
|
92
|
+
file[target] = overWhite(blue, alpha);
|
|
93
|
+
file[target + 1] = overWhite(green, alpha);
|
|
94
|
+
file[target + 2] = overWhite(red, alpha);
|
|
95
|
+
}
|
|
96
|
+
source += 4;
|
|
97
|
+
target += BYTES_PER_PIXEL;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return file;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* True when `bytes` is a BMP this writer would make: the "BM" signature, a
|
|
105
|
+
* file size that matches, the 40-byte header, one plane and 24 bits. Used to
|
|
106
|
+
* check a file before it is saved, so nothing else is handed out as BMP.
|
|
107
|
+
*/
|
|
108
|
+
export function isBMPFile(bytes: Uint8Array): boolean {
|
|
109
|
+
if (bytes.length < HEADER_SIZE || bytes[0] !== 0x42 || bytes[1] !== 0x4d) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
113
|
+
const width = view.getInt32(18, true);
|
|
114
|
+
const height = view.getInt32(22, true);
|
|
115
|
+
return (
|
|
116
|
+
view.getUint32(2, true) === bytes.length &&
|
|
117
|
+
view.getUint32(10, true) === HEADER_SIZE &&
|
|
118
|
+
view.getUint32(14, true) === INFO_HEADER_SIZE &&
|
|
119
|
+
width > 0 &&
|
|
120
|
+
height > 0 &&
|
|
121
|
+
view.getUint16(26, true) === 1 &&
|
|
122
|
+
view.getUint16(28, true) === 24 &&
|
|
123
|
+
view.getUint32(30, true) === 0 &&
|
|
124
|
+
bytes.length === HEADER_SIZE + rowStride(width) * height
|
|
125
|
+
);
|
|
126
|
+
}
|