gojpegmarkers

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2024 License: MPL-2.0 Imports: 2 Imported by: 0

README

go-jpeg-markers CI

Simply native Go tool for fetch all markers from JPEG image. Only Go, no more...

What are "markers" in JPEG?

According to CCITT T.81 THE INTERNATIONAL (09/92) markers serve to identify the various structural parts of the compressed data formats. Most markers start marker segments containing a related group of parameters, some markers stand alone. All markers are assigned two-byte codes: an 0xFF byte followed by a byte which is not equal to 0x00 or 0xFF.

Motivation

Working a lot of times with images, in most cases with JPEG, there is no instrument was found through the Internet that will allow you to find or to list all JPEG markers spending a minimum time. This instrument will allow you to inspect or to check needed JPEG markers in extremely short time. For example, very helpful for work with GPU CUDA-workers and for checking restart markers (RSTm) into the JPEG file. Also, helpful for checking any markers in source code at runtime.

Inspired by: https://github.com/MavEtJu/jpeg-markers

How to use as a lib?

go get -u github.com/nzamulov/go-jpeg-markers
package main

import (
	"bufio"
	"fmt"
	"io"
	"os"

	"github.com/nzamulov/go-jpeg-markers"
)

func main() {
	file, err := os.Open("./image.jpg")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	img, err := io.ReadAll(bufio.NewReader(file))
	if err != nil {
		panic(err)
	}

	var (
		markers = gojpegmarkers.GetAllMarkers(img)
		hasRSTm = gojpegmarkers.HasRSTm(img)
	)

	fmt.Printf("markers len: %d, RSTm: %t\n", len(markers), hasRSTm)
}

How to use as a CLI?

make scan <path_to_file_in_fs | link_from_internet>

File from FS:

make scan testdata/image.jpg
2024/03/29 12:41:15.652918 [go-jpeg-markers] offset:      0 - 0xFFD8: Start Of Image
2024/03/29 12:41:15.653262 [go-jpeg-markers] offset:      2 - 0xFFE0: JFIF [Identifier:JFIF, JFIF version:1.01 Density units:1, Xdensity:72, Ydensity:72, Xthumbnail:0, Ythumbnail:0]
2024/03/29 12:41:15.653266 [go-jpeg-markers] offset:     14 - 0xFFED: APP13
2024/03/29 12:41:15.653268 [go-jpeg-markers] offset:   223c - 0xFFE1: EXIF
2024/03/29 12:41:15.653270 [go-jpeg-markers] offset:   2262 - 0xFFE2: APP2
2024/03/29 12:41:15.653272 [go-jpeg-markers] offset:   2ebc - 0xFFE1: EXIF
2024/03/29 12:41:15.653274 [go-jpeg-markers] offset:   448f - 0xFFDB: Define Quantization Table(s)
2024/03/29 12:41:15.653277 [go-jpeg-markers] offset:   44d4 - 0xFFDB: Define Quantization Table(s)
2024/03/29 12:41:15.653279 [go-jpeg-markers] offset:   4519 - 0xFFC2: Start Of Frame (SOF2) (Progressive DCT, Huffman coding) [P:8, Y:522, X:783, Nf:3]
2024/03/29 12:41:15.653281 [go-jpeg-markers] offset:   452c - 0xFFC4: Define Huffman Table(s)
2024/03/29 12:41:15.653283 [go-jpeg-markers] offset:   454b - 0xFFC4: Define Huffman Table(s)
2024/03/29 12:41:15.653284 [go-jpeg-markers] offset:   4565 - 0xFFDA: Start Of Scan [Ns: 3] (8184 bytes)
2024/03/29 12:41:15.653286 [go-jpeg-markers] offset:   656b - 0xFFC4: Define Huffman Table(s)
2024/03/29 12:41:15.653288 [go-jpeg-markers] offset:   659f - 0xFFDA: Start Of Scan [Ns: 1] (21274 bytes)
2024/03/29 12:41:15.653290 [go-jpeg-markers] offset:   b8c3 - 0xFFC4: Define Huffman Table(s)
2024/03/29 12:41:15.653291 [go-jpeg-markers] offset:   b8f8 - 0xFFDA: Start Of Scan [Ns: 1] (1333 bytes)
2024/03/29 12:41:15.653293 [go-jpeg-markers] offset:   be37 - 0xFFC4: Define Huffman Table(s)
2024/03/29 12:41:15.653304 [go-jpeg-markers] offset:   be6b - 0xFFDA: Start Of Scan [Ns: 1] (1282 bytes)
2024/03/29 12:41:15.653306 [go-jpeg-markers] offset:   c377 - 0xFFC4: Define Huffman Table(s)
2024/03/29 12:41:15.653308 [go-jpeg-markers] offset:   c3cc - 0xFFDA: Start Of Scan [Ns: 1] (147491 bytes)
2024/03/29 12:41:15.653310 [go-jpeg-markers] offset:  303f9 - 0xFFC4: Define Huffman Table(s)
2024/03/29 12:41:15.653311 [go-jpeg-markers] offset:  30422 - 0xFFDA: Start Of Scan [Ns: 1] (44414 bytes)
2024/03/29 12:41:15.653313 [go-jpeg-markers] offset:  3b1aa - 0xFFDA: Start Of Scan [Ns: 3] (2426 bytes)
2024/03/29 12:41:15.653316 [go-jpeg-markers] offset:  3bb32 - 0xFFC4: Define Huffman Table(s)
2024/03/29 12:41:15.653317 [go-jpeg-markers] offset:  3bb57 - 0xFFDA: Start Of Scan [Ns: 1] (575 bytes)
2024/03/29 12:41:15.653319 [go-jpeg-markers] offset:  3bda0 - 0xFFC4: Define Huffman Table(s)
2024/03/29 12:41:15.653321 [go-jpeg-markers] offset:  3bdc5 - 0xFFDA: Start Of Scan [Ns: 1] (577 bytes)
2024/03/29 12:41:15.653324 [go-jpeg-markers] offset:  3c010 - 0xFFC4: Define Huffman Table(s)
2024/03/29 12:41:15.653326 [go-jpeg-markers] offset:  3c039 - 0xFFDA: Start Of Scan [Ns: 1] (48374 bytes)
2024/03/29 12:41:15.653328 [go-jpeg-markers] offset:  47d39 - 0xFFD9: End Of Image

File from link:

make scan https://sun9-80.userapi.com/V936_Yc4ULoWeaJ0_KRy_QfObYjex7mhPo1Qgg/i6DWh5it8lk.jpg
2024/03/29 13:16:44.789233 [go-jpeg-markers] offset:      0 - 0xFFD8: Start Of Image
2024/03/29 13:16:44.789690 [go-jpeg-markers] offset:      2 - 0xFFDB: Define Quantization Table(s)
2024/03/29 13:16:44.789693 [go-jpeg-markers] offset:     47 - 0xFFDB: Define Quantization Table(s)
2024/03/29 13:16:44.789695 [go-jpeg-markers] offset:     8c - 0xFFC0: Start Of Frame (SOF0) (Baseline DCT) [P:8, Y:50, X:50, Nf:3]
2024/03/29 13:16:44.789697 [go-jpeg-markers] offset:     9f - 0xFFC4: Define Huffman Table(s)
2024/03/29 13:16:44.789699 [go-jpeg-markers] offset:     bc - 0xFFC4: Define Huffman Table(s)
2024/03/29 13:16:44.789700 [go-jpeg-markers] offset:     ec - 0xFFC4: Define Huffman Table(s)
2024/03/29 13:16:44.789702 [go-jpeg-markers] offset:    103 - 0xFFC4: Define Huffman Table(s)
2024/03/29 13:16:44.789704 [go-jpeg-markers] offset:    124 - 0xFFDD: Define Restart Interval [Ri: 1]
2024/03/29 13:16:44.789706 [go-jpeg-markers] offset:    12a - 0xFFDA: Start Of Scan [Ns: 3] (6 bytes)
2024/03/29 13:16:44.789708 [go-jpeg-markers] offset:    13e - 0xFFD0: RST0 (240 bytes)
2024/03/29 13:16:44.789740 [go-jpeg-markers] offset:    230 - 0xFFD1: RST1 (79 bytes)
2024/03/29 13:16:44.789747 [go-jpeg-markers] offset:    281 - 0xFFD2: RST2 (6 bytes)
2024/03/29 13:16:44.789749 [go-jpeg-markers] offset:    289 - 0xFFD3: RST3 (113 bytes)
2024/03/29 13:16:44.789752 [go-jpeg-markers] offset:    2fc - 0xFFD4: RST4 (256 bytes)
2024/03/29 13:16:44.789753 [go-jpeg-markers] offset:    3fe - 0xFFD5: RST5 (108 bytes)
2024/03/29 13:16:44.789755 [go-jpeg-markers] offset:    46c - 0xFFD6: RST6 (6 bytes)
2024/03/29 13:16:44.789757 [go-jpeg-markers] offset:    474 - 0xFFD7: RST7 (67 bytes)
2024/03/29 13:16:44.789758 [go-jpeg-markers] offset:    4b9 - 0xFFD0: RST0 (177 bytes)
2024/03/29 13:16:44.789760 [go-jpeg-markers] offset:    56c - 0xFFD1: RST1 (99 bytes)
2024/03/29 13:16:44.789762 [go-jpeg-markers] offset:    5d1 - 0xFFD2: RST2 (6 bytes)
2024/03/29 13:16:44.789763 [go-jpeg-markers] offset:    5d9 - 0xFFD3: RST3 (6 bytes)
2024/03/29 13:16:44.789765 [go-jpeg-markers] offset:    5e1 - 0xFFD4: RST4 (36 bytes)
2024/03/29 13:16:44.789768 [go-jpeg-markers] offset:    607 - 0xFFD5: RST5 (11 bytes)
2024/03/29 13:16:44.789770 [go-jpeg-markers] offset:    614 - 0xFFD6: RST6 (6 bytes)
2024/03/29 13:16:44.789771 [go-jpeg-markers] offset:    61c - 0xFFD9: End Of Image

License

Mozilla Public License Version 2.0

Documentation

Index

Constants

View Source
const (
	SOI = 0xFFD8 // Start Of Image
	EOI = 0xFFD9 // End Of Image
	DQT = 0xFFDB // Define Quantization Table(s)
	DHT = 0xFFC4 // Define Huffman Table(s)
	COM = 0xFFFE // Comment
	SOS = 0xFFDA // Start Of Scan
	DRI = 0xFFDD // Define Restart Interval
	JPG = 0xFFC8 // Reserved for JPEG extensions
	DAC = 0xFFCC // Define arithmetic coding conditioning(s)
	DNL = 0xFFDC // Define number of lines
	DHP = 0xFFDE // Define hierarchical progression
	EXP = 0xFFDF // Expand reference component(s)
)

Common JPEG markers.

View Source
const (
	SOF0 = 0xFFC0 + iota // Baseline DCT
	SOF1                 // Extended sequential DCT
	SOF2                 // Progressive DCT, Huffman coding
	SOF3                 // Lossless (sequential)
)

Start of Frame header specifies the source image characteristics: the components of the frame, and the sampling factors for each component, and specifies the destinations from which the quantized tables to be used with each component are retrieved.

Start of Frame: non-differential, Huffman coding.

View Source
const (
	SOF4 = DHT + iota // SOF4 = DHT
	SOF5              // Differential sequential DCT
	SOF6              // Differential progressive DCT
	SOF7              // Differential lossless (sequential)
)

Start of Frame: differential, Huffman coding.

View Source
const (
	SOF8  = JPG + iota // SOF8 = JPG
	SOF9               // Extended sequential DCT
	SOF10              // Progressive DCT
	SOF11              // Lossless (sequential)
)

Start of Frame: non-differential, arithmetic coding.

View Source
const (
	SOF12 = DAC + iota // SOF12 = DAC
	SOF13              // Differential sequential DCT
	SOF14              // Differential progressive DCT
	SOF15              // Differential lossless (sequential)
)

Start of Frame: differential, arithmetic coding.

View Source
const (
	APP0 = 0xFFE0 + iota
	EXIF
	APP2
	APP3
	APP4
	APP5
	APP6
	APP7
	APP8
	APP9
	APP10
	APP11
	APP12
	APP13
	APP14
	APP15
)

Application-specific markers.

For example, an Exif JPEG file uses an APP1 (EXIF) marker to store metadata, laid out in a structure based closely on TIFF.

View Source
const (
	RST0 = 0xFFD0 + iota
	RST1
	RST2
	RST3
	RST4
	RST5
	RST6
	RST7
)

Restart markers.

Inserted every r macroblocks, where r is the restart interval set by a DRI marker. Not used if there was no DRI marker. The low three bits of the marker code cycle in value from 0 to 7.

Variables

View Source
var SOFComments = map[int]string{
	SOF0: "Baseline DCT",
	SOF1: "Extended sequential DCT",
	SOF2: "Progressive DCT, Huffman coding",
	SOF3: "Lossless (sequential)",

	SOF5: "Differential sequential DCT",
	SOF6: "Differential progressive DCT",
	SOF7: "Differential lossless (sequential)",

	SOF9:  "Extended sequential DCT",
	SOF10: "Progressive DCT",
	SOF11: "Lossless (sequential)",

	SOF13: "Differential sequential DCT",
	SOF14: "Differential progressive DCT",
	SOF15: "Differential lossless (sequential)",
}

SOFComments goes from CCITT Rec. T.81 (1992 E)

Functions

func HasRSTm added in v1.2.0

func HasRSTm(img []byte) bool

Types

type Marker

type Marker struct {
	ID, Offset int
	Comment    string
}

func GetAllMarkers added in v1.2.0

func GetAllMarkers(img []byte) (markers []Marker)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL