char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Linting & Formatting scripts (#26819)92d1bb0c9

char/aliceai-t5-moe
3.4 KiB103 linesraw
1import { getJpegOrientationFromDataURL, isJpegMimeType } from '$lib/utils/jpeg-orientation';
2import { describe, expect, it } from 'vitest';
3
4// Builds the TIFF payload of an APP1 segment holding a single IFD0 entry
5function buildTiff(littleEndian: boolean, tag: number, value: number): number[] {
6	const u16 = (v: number) => (littleEndian ? [v & 0xff, v >> 8] : [v >> 8, v & 0xff]);
7	const u32 = (v: number) =>
8		littleEndian
9			? [v & 0xff, (v >> 8) & 0xff, (v >> 16) & 0xff, (v >> 24) & 0xff]
10			: [(v >> 24) & 0xff, (v >> 16) & 0xff, (v >> 8) & 0xff, v & 0xff];
11
12	return [
13		...(littleEndian ? [0x49, 0x49] : [0x4d, 0x4d]),
14		...u16(42),
15		...u32(8),
16		...u16(1),
17		...u16(tag),
18		...u16(3),
19		...u32(1),
20		// SHORT value sits left justified in the 4 byte value field
21		...u16(value),
22		...u16(0),
23		...u32(0)
24	];
25}
26
27// Wraps a TIFF payload into a complete minimal JPEG data URL
28function buildJpegDataURL(tiff: number[] | null, prependApp0 = false): string {
29	const bytes: number[] = [0xff, 0xd8];
30
31	if (prependApp0) {
32		// JFIF APP0 segment, irrelevant content the parser walks over
33		bytes.push(0xff, 0xe0, 0x00, 0x07, 0x4a, 0x46, 0x49, 0x46, 0x00);
34	}
35
36	if (tiff) {
37		const payload = [0x45, 0x78, 0x69, 0x66, 0x00, 0x00, ...tiff];
38		const length = payload.length + 2;
39
40		bytes.push(0xff, 0xe1, length >> 8, length & 0xff, ...payload);
41	}
42
43	// SOS marker terminates the metadata scan
44	bytes.push(0xff, 0xda, 0x00, 0x02);
45
46	return `data:image/jpeg;base64,${btoa(String.fromCharCode(...bytes))}`;
47}
48
49describe('getJpegOrientationFromDataURL', () => {
50	it('returns the orientation from a little endian EXIF block', () => {
51		expect(getJpegOrientationFromDataURL(buildJpegDataURL(buildTiff(true, 0x0112, 6)))).toBe(6);
52	});
53
54	it('returns the orientation from a big endian EXIF block', () => {
55		expect(getJpegOrientationFromDataURL(buildJpegDataURL(buildTiff(false, 0x0112, 8)))).toBe(8);
56	});
57
58	it('walks over a leading APP0 segment', () => {
59		expect(getJpegOrientationFromDataURL(buildJpegDataURL(buildTiff(true, 0x0112, 3), true))).toBe(
60			3
61		);
62	});
63
64	it('returns 1 when the EXIF block holds no orientation tag', () => {
65		expect(getJpegOrientationFromDataURL(buildJpegDataURL(buildTiff(true, 0x0100, 6)))).toBe(1);
66	});
67
68	it('returns 1 when the orientation value is out of range', () => {
69		expect(getJpegOrientationFromDataURL(buildJpegDataURL(buildTiff(true, 0x0112, 9)))).toBe(1);
70	});
71
72	it('returns 1 when the JPEG has no APP1 segment', () => {
73		expect(getJpegOrientationFromDataURL(buildJpegDataURL(null, true))).toBe(1);
74	});
75
76	it('returns 1 for a payload that is not a JPEG', () => {
77		const png = btoa(String.fromCharCode(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a));
78
79		expect(getJpegOrientationFromDataURL(`data:image/png;base64,${png}`)).toBe(1);
80	});
81
82	it('returns 1 for a truncated payload', () => {
83		const truncated = btoa(String.fromCharCode(0xff, 0xd8, 0xff));
84
85		expect(getJpegOrientationFromDataURL(`data:image/jpeg;base64,${truncated}`)).toBe(1);
86	});
87
88	it('returns 1 for a malformed data URL', () => {
89		expect(getJpegOrientationFromDataURL('not a data url')).toBe(1);
90	});
91});
92
93describe('isJpegMimeType', () => {
94	it('matches both JPEG MIME variants', () => {
95		expect(isJpegMimeType('image/jpeg')).toBe(true);
96		expect(isJpegMimeType('image/jpg')).toBe(true);
97	});
98
99	it('rejects other image MIME types', () => {
100		expect(isJpegMimeType('image/png')).toBe(false);
101		expect(isJpegMimeType('image/webp')).toBe(false);
102	});
103});