@mapcatch/util 1.0.7 → 1.0.8-b

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapcatch/util",
3
- "version": "1.0.7",
3
+ "version": "1.0.8-b",
4
4
  "main": "./dist/catchUtil.min.esm.js",
5
5
  "repository": "",
6
6
  "author": "wanyanyan",
package/src/exif/index.js CHANGED
@@ -1,16 +1,33 @@
1
1
  import {getData} from './parse_image'
2
2
  import {getAllTags} from './exif'
3
+ import {imageSize} from '../image-size'
3
4
 
4
5
  export const getPhotoTags = (file) => {
5
6
  return new Promise((resolve, reject) => {
6
- getData(file, function (img, err) {
7
- if (err) {
8
- reject(err)
9
- return
10
- }
11
- let info = getAllTags(img)
12
- let exif = JSON.parse(JSON.stringify(info).replaceAll('\\u0000', '')) || {}
13
- resolve(exif)
14
- })
7
+ if (file.type === 'image/jpeg') {
8
+ let sliceFile = file.slice(0, 131072)
9
+ sliceFile.name = file.name
10
+ getData(sliceFile, function (img, err) {
11
+ if (err) {
12
+ reject(err)
13
+ return
14
+ }
15
+ let info = getAllTags(img)
16
+ let exif = JSON.parse(JSON.stringify(info).replaceAll('\\u0000', '')) || {}
17
+ resolve(exif)
18
+ })
19
+ } else {
20
+ var fileReader = new FileReader();
21
+ fileReader.onload = function (e) {
22
+ let u8array = new Uint8Array(e.target.result)
23
+ let size = imageSize(u8array)
24
+ resolve({
25
+ PixelXDimension: size.width,
26
+ PixelYDimension: size.height
27
+ })
28
+ };
29
+
30
+ fileReader.readAsArrayBuffer(file);
31
+ }
15
32
  })
16
33
  }
@@ -4,6 +4,7 @@ import ExifTags from './tags'
4
4
  import StringValues from './string_values'
5
5
  import GPSTags from './gps_tags'
6
6
  import IptcFieldMap from './iptc_field_map'
7
+ import {imageSize} from '../image-size'
7
8
 
8
9
  var isXmpEnabled = false
9
10
 
@@ -86,10 +87,6 @@ function findEXIFinJPEG(file) {
86
87
  var dataView = new DataView(file);
87
88
  let exif = {}
88
89
 
89
- if ((dataView.getUint8(0) != 0xFF) || (dataView.getUint8(1) != 0xD8)) {
90
- return false; // not a valid jpeg
91
- }
92
-
93
90
  var offset = 2,
94
91
  length = file.byteLength,
95
92
  marker;
@@ -105,21 +102,16 @@ function findEXIFinJPEG(file) {
105
102
  // but we're only looking for 0xFFE1 for EXIF data
106
103
 
107
104
  if (marker == 225) {
108
- Object.assign(exif, readEXIFData(dataView, offset + 4, dataView.getUint16(offset + 2) - 2))
109
- } else if (marker === 192) {
110
- let width = dataView.getUint16(offset + 7)
111
- let height = dataView.getUint16(offset + 5)
112
- if (width && height) {
113
- Object.assign(exif, {
114
- PixelXDimension: width,
115
- PixelYDimension: height
116
- })
117
- }
118
- return exif
105
+ exif = readEXIFData(dataView, offset + 4, dataView.getUint16(offset + 2) - 2)
106
+ break
119
107
  }
120
108
  offset += 2 + dataView.getUint16(offset + 2);
121
-
122
109
  }
110
+ let size = imageSize(new Uint8Array(file))
111
+ Object.assign(exif, {
112
+ PixelXDimension: size.width,
113
+ PixelYDimension: size.height
114
+ })
123
115
  return exif
124
116
 
125
117
  }
@@ -0,0 +1,24 @@
1
+ import { typeHandlers, types } from './types/index'
2
+
3
+ // This map helps avoid validating for every single image type
4
+ const firstBytes = new Map([
5
+ [0x38, 'psd'],
6
+ [0x42, 'bmp'],
7
+ [0x44, 'dds'],
8
+ [0x47, 'gif'],
9
+ [0x49, 'tiff'],
10
+ [0x4d, 'tiff'],
11
+ [0x52, 'webp'],
12
+ [0x69, 'icns'],
13
+ [0x89, 'png'],
14
+ [0xff, 'jpg'],
15
+ ])
16
+
17
+ export function detector(input) {
18
+ const byte = input[0]
19
+ const type = firstBytes.get(byte)
20
+ if (type && typeHandlers.get(type).validate(input)) {
21
+ return type
22
+ }
23
+ return types.find((type) => typeHandlers.get(type).validate(input))
24
+ }
@@ -0,0 +1,55 @@
1
+ import * as fs from 'node:fs'
2
+ import * as path from 'node:path'
3
+
4
+ import { lookup } from './lookup'
5
+
6
+ // Maximum input size, with a default of 512 kilobytes.
7
+ // TO-DO: make this adaptive based on the initial signature of the image
8
+ const MaxInputSize = 512 * 1024
9
+
10
+ // This queue is for async `fs` operations, to avoid reaching file-descriptor limits
11
+ const queue = []
12
+
13
+ let concurrency = 100
14
+ export const setConcurrency = (c) => {
15
+ concurrency = c
16
+ }
17
+
18
+ const processQueue = async () => {
19
+ const jobs = queue.splice(0, concurrency)
20
+ const promises = jobs.map(async ({ filePath, resolve, reject }) => {
21
+ let handle
22
+ try {
23
+ handle = await fs.promises.open(path.resolve(filePath), 'r')
24
+ } catch (err) {
25
+ return reject(err)
26
+ }
27
+ try {
28
+ const { size } = await handle.stat()
29
+ if (size <= 0) {
30
+ throw new Error('Empty file')
31
+ }
32
+ const inputSize = Math.min(size, MaxInputSize)
33
+ const input = new Uint8Array(inputSize)
34
+ await handle.read(input, 0, inputSize, 0)
35
+ resolve(lookup(input))
36
+ } catch (err) {
37
+ reject(err)
38
+ } finally {
39
+ await handle.close()
40
+ }
41
+ })
42
+
43
+ await Promise.allSettled(promises)
44
+
45
+ if (queue.length) setTimeout(processQueue, 100)
46
+ }
47
+
48
+ /**
49
+ * @param {string} filePath - relative/absolute path of the image file
50
+ */
51
+ export const imageSize = async (filePath) =>
52
+ new Promise<ISizeCalculationResult>((resolve, reject) => {
53
+ queue.push({ filePath, resolve, reject })
54
+ processQueue()
55
+ })
@@ -0,0 +1,2 @@
1
+ export { types } from './types'
2
+ export { lookup as imageSize, disableTypes } from './lookup'
@@ -0,0 +1,37 @@
1
+ import { typeHandlers } from './types/index'
2
+ import { detector } from './detector'
3
+
4
+ const globalOptions = {
5
+ disabledTypes: [],
6
+ }
7
+
8
+ /**
9
+ * Return size information based on an Uint8Array
10
+ *
11
+ * @param {Uint8Array} input
12
+ * @returns {ISizeCalculationResult}
13
+ */
14
+ export function lookup(input) {
15
+ // detect the file type... don't rely on the extension
16
+ const type = detector(input)
17
+
18
+ if (typeof type !== 'undefined') {
19
+ if (globalOptions.disabledTypes.indexOf(type) > -1) {
20
+ throw new TypeError('disabled file type: ' + type)
21
+ }
22
+
23
+ // find an appropriate handler for this file type
24
+ const size = typeHandlers.get(type).calculate(input)
25
+ if (size !== undefined) {
26
+ size.type = size.type ?? type
27
+ return size
28
+ }
29
+ }
30
+
31
+ // throw up, if we don't understand the file
32
+ throw new TypeError('unsupported file type: ' + type)
33
+ }
34
+
35
+ export const disableTypes = (types) => {
36
+ globalOptions.disabledTypes = types
37
+ }
@@ -0,0 +1,10 @@
1
+ import { toUTF8String, readInt32LE, readUInt32LE } from './utils'
2
+
3
+ export const BMP = {
4
+ validate: (input) => toUTF8String(input, 0, 2) === 'BM',
5
+
6
+ calculate: (input) => ({
7
+ height: Math.abs(readInt32LE(input, 22)),
8
+ width: readUInt32LE(input, 18),
9
+ }),
10
+ }
@@ -0,0 +1,16 @@
1
+ import { ICO } from './ico'
2
+ import { readUInt16LE } from './utils'
3
+
4
+ const TYPE_CURSOR = 2
5
+ export const CUR = {
6
+ validate(input) {
7
+ const reserved = readUInt16LE(input, 0)
8
+ const imageCount = readUInt16LE(input, 4)
9
+ if (reserved !== 0 || imageCount === 0) return false
10
+
11
+ const imageType = readUInt16LE(input, 2)
12
+ return imageType === TYPE_CURSOR
13
+ },
14
+
15
+ calculate: (input) => ICO.calculate(input),
16
+ }
@@ -0,0 +1,10 @@
1
+ import { readUInt32LE } from './utils'
2
+
3
+ export const DDS = {
4
+ validate: (input) => readUInt32LE(input, 0) === 0x20534444,
5
+
6
+ calculate: (input) => ({
7
+ height: readUInt32LE(input, 12),
8
+ width: readUInt32LE(input, 16),
9
+ }),
10
+ }
@@ -0,0 +1,11 @@
1
+ import { toUTF8String, readUInt16LE } from './utils'
2
+
3
+ const gifRegexp = /^GIF8[79]a/
4
+ export const GIF = {
5
+ validate: (input) => gifRegexp.test(toUTF8String(input, 0, 6)),
6
+
7
+ calculate: (input) => ({
8
+ height: readUInt16LE(input, 8),
9
+ width: readUInt16LE(input, 6),
10
+ }),
11
+ }
@@ -0,0 +1,35 @@
1
+ import { findBox, readUInt32BE, toUTF8String } from './utils'
2
+
3
+ const brandMap = {
4
+ avif: 'avif',
5
+ mif1: 'heif',
6
+ msf1: 'heif', // hief-sequence
7
+ heic: 'heic',
8
+ heix: 'heic',
9
+ hevc: 'heic', // heic-sequence
10
+ hevx: 'heic', // heic-sequence
11
+ }
12
+
13
+ export const HEIF = {
14
+ validate(buffer) {
15
+ const ftype = toUTF8String(buffer, 4, 8)
16
+ const brand = toUTF8String(buffer, 8, 12)
17
+ return 'ftyp' === ftype && brand in brandMap
18
+ },
19
+
20
+ calculate(buffer) {
21
+ // Based on https://nokiatech.github.io/heif/technical.html
22
+ const metaBox = findBox(buffer, 'meta', 0)
23
+ const iprpBox = metaBox && findBox(buffer, 'iprp', metaBox.offset + 12)
24
+ const ipcoBox = iprpBox && findBox(buffer, 'ipco', iprpBox.offset + 8)
25
+ const ispeBox = ipcoBox && findBox(buffer, 'ispe', ipcoBox.offset + 8)
26
+ if (ispeBox) {
27
+ return {
28
+ height: readUInt32BE(buffer, ispeBox.offset + 16),
29
+ width: readUInt32BE(buffer, ispeBox.offset + 12),
30
+ type: toUTF8String(buffer, 8, 12),
31
+ }
32
+ }
33
+ throw new TypeError('Invalid HEIF, no size found')
34
+ }
35
+ }
@@ -0,0 +1,112 @@
1
+ import { toUTF8String, readUInt32BE } from './utils'
2
+
3
+ /**
4
+ * ICNS Header
5
+ *
6
+ * | Offset | Size | Purpose |
7
+ * | 0 | 4 | Magic literal, must be "icns" (0x69, 0x63, 0x6e, 0x73) |
8
+ * | 4 | 4 | Length of file, in bytes, msb first. |
9
+ *
10
+ */
11
+ const SIZE_HEADER = 4 + 4 // 8
12
+ const FILE_LENGTH_OFFSET = 4 // MSB => BIG ENDIAN
13
+
14
+ /**
15
+ * Image Entry
16
+ *
17
+ * | Offset | Size | Purpose |
18
+ * | 0 | 4 | Icon type, see OSType below. |
19
+ * | 4 | 4 | Length of data, in bytes (including type and length), msb first. |
20
+ * | 8 | n | Icon data |
21
+ */
22
+ const ENTRY_LENGTH_OFFSET = 4 // MSB => BIG ENDIAN
23
+
24
+ const ICON_TYPE_SIZE = {
25
+ ICON: 32,
26
+ 'ICN#': 32,
27
+ // m => 16 x 16
28
+ 'icm#': 16,
29
+ icm4: 16,
30
+ icm8: 16,
31
+ // s => 16 x 16
32
+ 'ics#': 16,
33
+ ics4: 16,
34
+ ics8: 16,
35
+ is32: 16,
36
+ s8mk: 16,
37
+ icp4: 16,
38
+ // l => 32 x 32
39
+ icl4: 32,
40
+ icl8: 32,
41
+ il32: 32,
42
+ l8mk: 32,
43
+ icp5: 32,
44
+ ic11: 32,
45
+ // h => 48 x 48
46
+ ich4: 48,
47
+ ich8: 48,
48
+ ih32: 48,
49
+ h8mk: 48,
50
+ // . => 64 x 64
51
+ icp6: 64,
52
+ ic12: 32,
53
+ // t => 128 x 128
54
+ it32: 128,
55
+ t8mk: 128,
56
+ ic07: 128,
57
+ // . => 256 x 256
58
+ ic08: 256,
59
+ ic13: 256,
60
+ // . => 512 x 512
61
+ ic09: 512,
62
+ ic14: 512,
63
+ // . => 1024 x 1024
64
+ ic10: 1024,
65
+ }
66
+
67
+ function readImageHeader(
68
+ input,
69
+ imageOffset,
70
+ ) {
71
+ const imageLengthOffset = imageOffset + ENTRY_LENGTH_OFFSET
72
+ return [
73
+ toUTF8String(input, imageOffset, imageLengthOffset),
74
+ readUInt32BE(input, imageLengthOffset),
75
+ ]
76
+ }
77
+
78
+ function getImageSize(type) {
79
+ const size = ICON_TYPE_SIZE[type]
80
+ return { width: size, height: size, type }
81
+ }
82
+
83
+ export const ICNS = {
84
+ validate: (input) => toUTF8String(input, 0, 4) === 'icns',
85
+
86
+ calculate(input) {
87
+ const inputLength = input.length
88
+ const fileLength = readUInt32BE(input, FILE_LENGTH_OFFSET)
89
+ let imageOffset = SIZE_HEADER
90
+
91
+ let imageHeader = readImageHeader(input, imageOffset)
92
+ let imageSize = getImageSize(imageHeader[0])
93
+ imageOffset += imageHeader[1]
94
+
95
+ if (imageOffset === fileLength) return imageSize
96
+
97
+ const result = {
98
+ height: imageSize.height,
99
+ images: [imageSize],
100
+ width: imageSize.width,
101
+ }
102
+
103
+ while (imageOffset < fileLength && imageOffset < inputLength) {
104
+ imageHeader = readImageHeader(input, imageOffset)
105
+ imageSize = getImageSize(imageHeader[0])
106
+ imageOffset += imageHeader[1]
107
+ result.images.push(imageSize)
108
+ }
109
+
110
+ return result
111
+ },
112
+ }
@@ -0,0 +1,74 @@
1
+ import { readUInt16LE } from './utils'
2
+
3
+ const TYPE_ICON = 1
4
+
5
+ /**
6
+ * ICON Header
7
+ *
8
+ * | Offset | Size | Purpose |
9
+ * | 0 | 2 | Reserved. Must always be 0. |
10
+ * | 2 | 2 | Image type: 1 for icon (.ICO) image, 2 for cursor (.CUR) image. Other values are invalid. |
11
+ * | 4 | 2 | Number of images in the file. |
12
+ *
13
+ */
14
+ const SIZE_HEADER = 2 + 2 + 2 // 6
15
+
16
+ /**
17
+ * Image Entry
18
+ *
19
+ * | Offset | Size | Purpose |
20
+ * | 0 | 1 | Image width in pixels. Can be any number between 0 and 255. Value 0 means width is 256 pixels. |
21
+ * | 1 | 1 | Image height in pixels. Can be any number between 0 and 255. Value 0 means height is 256 pixels. |
22
+ * | 2 | 1 | Number of colors in the color palette. Should be 0 if the image does not use a color palette. |
23
+ * | 3 | 1 | Reserved. Should be 0. |
24
+ * | 4 | 2 | ICO format: Color planes. Should be 0 or 1. |
25
+ * | | | CUR format: The horizontal coordinates of the hotspot in number of pixels from the left. |
26
+ * | 6 | 2 | ICO format: Bits per pixel. |
27
+ * | | | CUR format: The vertical coordinates of the hotspot in number of pixels from the top. |
28
+ * | 8 | 4 | The size of the image's data in bytes |
29
+ * | 12 | 4 | The offset of BMP or PNG data from the beginning of the ICO/CUR file |
30
+ *
31
+ */
32
+ const SIZE_IMAGE_ENTRY = 1 + 1 + 1 + 1 + 2 + 2 + 4 + 4 // 16
33
+
34
+ function getSizeFromOffset(input, offset) {
35
+ const value = input[offset]
36
+ return value === 0 ? 256 : value
37
+ }
38
+
39
+ function getImageSize(input, imageIndex) {
40
+ const offset = SIZE_HEADER + imageIndex * SIZE_IMAGE_ENTRY
41
+ return {
42
+ height: getSizeFromOffset(input, offset + 1),
43
+ width: getSizeFromOffset(input, offset),
44
+ }
45
+ }
46
+
47
+ export const ICO = {
48
+ validate(input) {
49
+ const reserved = readUInt16LE(input, 0)
50
+ const imageCount = readUInt16LE(input, 4)
51
+ if (reserved !== 0 || imageCount === 0) return false
52
+
53
+ const imageType = readUInt16LE(input, 2)
54
+ return imageType === TYPE_ICON
55
+ },
56
+
57
+ calculate(input) {
58
+ const nbImages = readUInt16LE(input, 4)
59
+ const imageSize = getImageSize(input, 0)
60
+
61
+ if (nbImages === 1) return imageSize
62
+
63
+ const imgs = [imageSize]
64
+ for (let imageIndex = 1; imageIndex < nbImages; imageIndex += 1) {
65
+ imgs.push(getImageSize(input, imageIndex))
66
+ }
67
+
68
+ return {
69
+ height: imageSize.height,
70
+ images: imgs,
71
+ width: imageSize.width,
72
+ }
73
+ },
74
+ }
@@ -0,0 +1,43 @@
1
+ // load all available handlers explicitely for browserify support
2
+ import { BMP } from './bmp'
3
+ import { CUR } from './cur'
4
+ import { DDS } from './dds'
5
+ import { GIF } from './gif'
6
+ import { HEIF } from './heif'
7
+ import { ICNS } from './icns'
8
+ import { ICO } from './ico'
9
+ import { J2C } from './j2c'
10
+ import { JP2 } from './jp2'
11
+ import { JPG } from './jpg'
12
+ import { KTX } from './ktx'
13
+ import { PNG } from './png'
14
+ import { PNM } from './pnm'
15
+ import { PSD } from './psd'
16
+ import { SVG } from './svg'
17
+ import { TGA } from './tga'
18
+ import { TIFF } from './tiff'
19
+ import { WEBP } from './webp'
20
+
21
+ export const typeHandlers = new Map([
22
+ ['bmp', BMP],
23
+ ['cur', CUR],
24
+ ['dds', DDS],
25
+ ['gif', GIF],
26
+ ['heif', HEIF],
27
+ ['icns', ICNS],
28
+ ['ico', ICO],
29
+ ['j2c', J2C],
30
+ ['jp2', JP2],
31
+ ['jpg', JPG],
32
+ ['ktx', KTX],
33
+ ['png', PNG],
34
+ ['pnm', PNM],
35
+ ['psd', PSD],
36
+ ['svg', SVG],
37
+ ['tga', TGA],
38
+ ['tiff', TIFF],
39
+ ['webp', WEBP],
40
+ ])
41
+
42
+
43
+ export const types = Array.from(typeHandlers.keys())
@@ -0,0 +1,11 @@
1
+ import { toHexString, readUInt32BE } from './utils'
2
+
3
+ export const J2C = {
4
+ // TODO: this doesn't seem right. SIZ marker doesn't have to be right after the SOC
5
+ validate: (input) => toHexString(input, 0, 4) === 'ff4fff51',
6
+
7
+ calculate: (input) => ({
8
+ height: readUInt32BE(input, 12),
9
+ width: readUInt32BE(input, 8),
10
+ }),
11
+ }
@@ -0,0 +1,22 @@
1
+ import { readUInt32BE, findBox } from './utils'
2
+
3
+ export const JP2 = {
4
+ validate(input) {
5
+ if (readUInt32BE(input, 4) !== 0x6a502020 || readUInt32BE(input, 0) < 1) return false
6
+ const ftypBox = findBox(input, 'ftyp', 0)
7
+ if (!ftypBox) return false
8
+ return readUInt32BE(input, ftypBox.offset + 4) === 0x66747970
9
+ },
10
+
11
+ calculate(input) {
12
+ const jp2hBox = findBox(input, 'jp2h', 0)
13
+ const ihdrBox = jp2hBox && findBox(input, 'ihdr', jp2hBox.offset + 8)
14
+ if (ihdrBox) {
15
+ return {
16
+ height: readUInt32BE(input, ihdrBox.offset + 8),
17
+ width: readUInt32BE(input, ihdrBox.offset + 12),
18
+ }
19
+ }
20
+ throw new TypeError('Unsupported JPEG 2000 format')
21
+ },
22
+ }