char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
92d1bb0c9
char/aliceai-t5-moe
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 [] { 6const u16 = ( v :number ) => ( littleEndian ?[ v & 0xff , v >> 8 ] :[ v >> 8 , v & 0xff ]); 7const u32 = ( v :number ) => 8littleEndian 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 12return [ 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 { 29const bytes :number [] = [ 0xff , 0xd8 ]; 30 31if ( prependApp0 ) { 32// JFIF APP0 segment, irrelevant content the parser walks over 33bytes . push ( 0xff , 0xe0 , 0x00 , 0x07 , 0x4a , 0x46 , 0x49 , 0x46 , 0x00 ); 34} 35 36if ( tiff ) { 37const payload = [ 0x45 , 0x78 , 0x69 , 0x66 , 0x00 , 0x00 , ...tiff ]; 38const length = payload . length + 2 ; 39 40bytes . push ( 0xff , 0xe1 , length >> 8 , length & 0xff , ...payload ); 41} 42 43// SOS marker terminates the metadata scan 44bytes . push ( 0xff , 0xda , 0x00 , 0x02 ); 45 46return `data:image/jpeg;base64, ${ btoa ( String . fromCharCode ( ... bytes ))} ` ; 47} 48 49describe ( 'getJpegOrientationFromDataURL' , () => { 50it ( 'returns the orientation from a little endian EXIF block' , () => { 51expect ( getJpegOrientationFromDataURL ( buildJpegDataURL ( buildTiff ( true , 0x0112 , 6 )))). toBe ( 6 ); 52}); 53 54it ( 'returns the orientation from a big endian EXIF block' , () => { 55expect ( getJpegOrientationFromDataURL ( buildJpegDataURL ( buildTiff ( false , 0x0112 , 8 )))). toBe ( 8 ); 56}); 57 58it ( 'walks over a leading APP0 segment' , () => { 59expect ( getJpegOrientationFromDataURL ( buildJpegDataURL ( buildTiff ( true , 0x0112 , 3 ), true ))). toBe ( 603 61); 62}); 63 64it ( 'returns 1 when the EXIF block holds no orientation tag' , () => { 65expect ( getJpegOrientationFromDataURL ( buildJpegDataURL ( buildTiff ( true , 0x0100 , 6 )))). toBe ( 1 ); 66}); 67 68it ( 'returns 1 when the orientation value is out of range' , () => { 69expect ( getJpegOrientationFromDataURL ( buildJpegDataURL ( buildTiff ( true , 0x0112 , 9 )))). toBe ( 1 ); 70}); 71 72it ( 'returns 1 when the JPEG has no APP1 segment' , () => { 73expect ( getJpegOrientationFromDataURL ( buildJpegDataURL ( null , true ))). toBe ( 1 ); 74}); 75 76it ( 'returns 1 for a payload that is not a JPEG' , () => { 77const png = btoa ( String . fromCharCode ( 0x89 , 0x50 , 0x4e , 0x47 , 0x0d , 0x0a , 0x1a , 0x0a )); 78 79expect ( getJpegOrientationFromDataURL ( `data:image/png;base64, ${ png } ` )). toBe ( 1 ); 80}); 81 82it ( 'returns 1 for a truncated payload' , () => { 83const truncated = btoa ( String . fromCharCode ( 0xff , 0xd8 , 0xff )); 84 85expect ( getJpegOrientationFromDataURL ( `data:image/jpeg;base64, ${ truncated } ` )). toBe ( 1 ); 86}); 87 88it ( 'returns 1 for a malformed data URL' , () => { 89expect ( getJpegOrientationFromDataURL ( 'not a data url' )). toBe ( 1 ); 90}); 91}); 92 93describe ( 'isJpegMimeType' , () => { 94it ( 'matches both JPEG MIME variants' , () => { 95expect ( isJpegMimeType ( 'image/jpeg' )). toBe ( true ); 96expect ( isJpegMimeType ( 'image/jpg' )). toBe ( true ); 97}); 98 99it ( 'rejects other image MIME types' , () => { 100expect ( isJpegMimeType ( 'image/png' )). toBe ( false ); 101expect ( isJpegMimeType ( 'image/webp' )). toBe ( false ); 102}); 103});