Skip to content

Instantly share code, notes, and snippets.

@ekowcharles
Last active August 13, 2024 05:32
Show Gist options
  • Save ekowcharles/0339b35b535afdae2901bce6135f5ce4 to your computer and use it in GitHub Desktop.
Save ekowcharles/0339b35b535afdae2901bce6135f5ce4 to your computer and use it in GitHub Desktop.
Interprate JPEG file according to spec
// specs -> https://www.w3.org/Graphics/JPEG/jfif3.pdf
// Suppose you have a JPEG file with the following segments:
// SOI (Start of Image): FFD8
// APP0 (JFIF): FFE0 followed by length and JFIF metadata
// DQT (Quantization Table): FFDB followed by length and quantization table data
// SOF0 (Baseline DCT): FFC0 followed by length and frame information
// DHT (Huffman Table): FFC4 followed by length and Huffman tables
// SOS (Start of Scan): FFDA followed by length and scan parameters
// Compressed Image Data: Huffman-encoded DCT coefficients
// EOI (End of Image): FFD9
// [SOI] [APP0/APP1] [DQT] [SOF] [DHT] [DRI] [SOS] [Compressed Data] [EOI]
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
)
func main() {
f, err := os.Open("my-jpeg-file-path")
if err != nil {
panic(err)
}
br := bufio.NewReader(f)
prev := ""
for {
b, err := br.ReadByte()
if err != nil && !errors.Is(err, io.EOF) {
fmt.Println(err)
break
}
prev = prev + fmt.Sprintf("%02X", b)
if len(prev) == 4 {
fmt.Printf("0x%s\n", prev)
prev = ""
}
if err != nil {
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment