(bytes, offset)
| 2627 | * @returns {Uint8Array} |
| 2628 | */ |
| 2629 | export function extractJPEG(bytes, offset) { |
| 2630 | const stream = new Stream(bytes.slice(offset)); |
| 2631 | |
| 2632 | while (stream.hasMore()) { |
| 2633 | const marker = stream.getBytes(2); |
| 2634 | if (marker[0] !== 0xff) throw new Error(`Invalid marker while parsing JPEG at pos ${stream.position}: ${marker}`); |
| 2635 | |
| 2636 | let segmentSize = 0; |
| 2637 | switch (marker[1]) { |
| 2638 | // No length |
| 2639 | case 0xd8: // Start of Image |
| 2640 | case 0x01: // For temporary use in arithmetic coding |
| 2641 | break; |
| 2642 | case 0xd9: // End found |
| 2643 | return stream.carve(); |
| 2644 | |
| 2645 | // Variable size segment |
| 2646 | case 0xc0: // Start of frame (Baseline DCT) |
| 2647 | case 0xc1: // Start of frame (Extended sequential DCT) |
| 2648 | case 0xc2: // Start of frame (Progressive DCT) |
| 2649 | case 0xc3: // Start of frame (Lossless sequential) |
| 2650 | case 0xc4: // Define Huffman Table |
| 2651 | case 0xc5: // Start of frame (Differential sequential DCT) |
| 2652 | case 0xc6: // Start of frame (Differential progressive DCT) |
| 2653 | case 0xc7: // Start of frame (Differential lossless) |
| 2654 | case 0xc8: // Reserved for JPEG extensions |
| 2655 | case 0xc9: // Start of frame (Extended sequential DCT) |
| 2656 | case 0xca: // Start of frame (Progressive DCT) |
| 2657 | case 0xcb: // Start of frame (Lossless sequential) |
| 2658 | case 0xcc: // Define arithmetic conditioning table |
| 2659 | case 0xcd: // Start of frame (Differential sequential DCT) |
| 2660 | case 0xce: // Start of frame (Differential progressive DCT) |
| 2661 | case 0xcf: // Start of frame (Differential lossless) |
| 2662 | case 0xdb: // Define Quantization Table |
| 2663 | case 0xde: // Define hierarchical progression |
| 2664 | case 0xe0: // Application-specific |
| 2665 | case 0xe1: // Application-specific |
| 2666 | case 0xe2: // Application-specific |
| 2667 | case 0xe3: // Application-specific |
| 2668 | case 0xe4: // Application-specific |
| 2669 | case 0xe5: // Application-specific |
| 2670 | case 0xe6: // Application-specific |
| 2671 | case 0xe7: // Application-specific |
| 2672 | case 0xe8: // Application-specific |
| 2673 | case 0xe9: // Application-specific |
| 2674 | case 0xea: // Application-specific |
| 2675 | case 0xeb: // Application-specific |
| 2676 | case 0xec: // Application-specific |
| 2677 | case 0xed: // Application-specific |
| 2678 | case 0xee: // Application-specific |
| 2679 | case 0xef: // Application-specific |
| 2680 | case 0xfe: // Comment |
| 2681 | segmentSize = stream.readInt(2, "be"); |
| 2682 | stream.position += segmentSize - 2; |
| 2683 | break; |
| 2684 | |
| 2685 | // 1 byte |
| 2686 | case 0xdf: // Expand reference image |
nothing calls this directly
no test coverage detected