自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Go:通過 io.Writer 將 JPEG 轉(zhuǎn)為 JFIF

開發(fā) 后端
Go 的標準庫可讓你對 JPEG 圖像進行編碼。在 One of these JPEGs is not like the other[1] 一文中,Ben Cox 指出某些硬件不會解碼這些 JPEG 圖像,除非它們被增強為 JFIF 圖像。JFIF 代表“JPEG 文件交換格式”,在概念上是原始 JPEG 格式的次要版本。

 [[439345]]

大家好,我是程序員幽鬼。

Go 的標準庫可讓你對 JPEG 圖像進行編碼。在 One of these JPEGs is not like the other[1] 一文中,Ben Cox 指出某些硬件不會解碼這些 JPEG 圖像,除非它們被增強為 JFIF 圖像。JFIF 代表“JPEG 文件交換格式”,在概念上是原始 JPEG 格式的次要版本。

硬件缺乏支持有點令人驚訝,因為 JPEG 是一種無處不在的文件格式。他 fork[2] 并 修復[3] 標準 image/jpeg 包以插入必要的 JFIF 字節(jié)。

01 JPEG Wire 格式

就網(wǎng)絡(luò)(或磁盤)上的字節(jié)而言,JPEG 由一系列連接在一起的塊組成。每個塊要么是一個裸標記(兩個字節(jié),以 開頭 0xff)要么是一個標記段(四個或更多字節(jié)是一個兩字節(jié)標記,同樣以 0xff 開頭,一個兩字節(jié)的長度,然后是一個額外的數(shù)據(jù)負載)。以下是 Wikipedia 的Example.jpg[4] 十六進制表示:

  1. $ wget --quiet https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg 
  2. $ hd Example.jpg | head -n 5 
  3. 00000000  ff d8 ff e0 00 10 4a 46  49 46 00 01 01 01 00 48  |......JFIF.....H| 
  4. 00000010  00 48 00 00 ff e1 00 16  45 78 69 66 00 00 4d 4d  |.H......Exif..MM| 
  5. 00000020  00 2a 00 00 00 08 00 00  00 00 00 00 ff fe 00 17  |.*..............| 
  6. 00000030  43 72 65 61 74 65 64 20  77 69 74 68 20 54 68 65  |Created with The| 
  7. 00000040  20 47 49 4d 50 ff db 00  43 00 05 03 04 04 04 03  | GIMP...C.......| 

在打開的 80 個字節(jié)標記:

  • 一個 ff d8 SOI(圖像的開始)標記。
  • 一個 ff e0 APP0 標記段;有效載荷以 “JFIF” 開頭。
  • 一個 ff e1 APP1 標記段;有效載荷以 “Exif” 開頭。
  • 一個 ff fe 注釋標記段,“Created 等等”。
  • 一個 ff db DQT(定義量化表)標記段。

file 命令也認為這是 JFIF(帶 Exif),而不僅僅是 JPEG:

  1. $ file Example.jpg 
  2. Example.jpg: JPEG image data, JFIF... Exif... baseline... 

02 JFIF Wire 格式

JFIF 文件是一個 JPEG 文件,它的第二個塊(在作為第一個塊的 SOI 之后)是一個 APP0 塊,其有效載荷以 “JFIF” 開頭。一個有趣的點是 JFIF 和 EXIF 規(guī)范在技術(shù)上不兼容,因為它們都想占用第二塊(the second chunk):

  • JFIF 規(guī)范[5]第 2 頁提到:“JPEG FIF APP0 標記必須緊跟在 SOI 標記之后”。
  • EXIF 規(guī)范[6] 第 4.5.4 段提到:“APP1 是緊跟在 SOI 標記之后的”。

在實踐中,似乎 JFIF 'won' 和 EXIF 可以是第三個塊。

03 生成普通的舊 JPEG

這篇博文提供了不需要任何標準庫補丁(或 forks)的 Cox 方法的替代方法。與往常一樣,fork 具有從上游緩慢分叉的長期風險。Go 標準庫的上游補丁受制于“3 個月的新功能,3 個月的穩(wěn)定” 發(fā)布周期[7],并決定額外的 JFIF 塊是強制性的還是可選的(如果可選,API 應(yīng)該是什么,受兼容性限制[8])。

該方案的主要思想是 jpeg.Encode[9] 函數(shù)接受一個 io.Writer 參數(shù),并且很容易包裝 io.Writer 以在正確的位置插入 JFIF 字節(jié)。

首先,讓我們編寫一個簡單的程序來生成一張 1x1 JPEG 圖像。

  1. package main 
  2.  
  3. import ( 
  4.     "image" 
  5.     "image/jpeg" 
  6.     "os" 
  7.  
  8. func main() { 
  9.     m := image.NewGray(image.Rect(0, 0, 1, 1)) 
  10.     if err := jpeg.Encode(os.Stdout, m, nil); err != nil { 
  11.         os.Stderr.WriteString(err.Error() + "\n"
  12.         os.Exit(1) 
  13.     } 

運行它會生成一個 JPEG(但不是 JFIF)文件。

  1. $ go run from-jpeg-to-jfif.go > x 
  2. $ hd x | head -n 5 
  3. 00000000  ff d8 ff db 00 84 00 08  06 06 07 06 05 08 07 07  |................| 
  4. 00000010  07 09 09 08 0a 0c 14 0d  0c 0b 0b 0c 19 12 13 0f  |................| 
  5. 00000020  14 1d 1a 1f 1e 1d 1a 1c  1c 20 24 2e 27 20 22 2c  |......... $.' ",| 
  6. 00000030  23 1c 1c 28 37 29 2c 30  31 34 34 34 1f 27 39 3d  |#..(7),01444.'9=| 
  7. 00000040  38 32 3c 2e 33 34 32 01  09 09 09 0c 0b 0c 18 0d  |82<.342.........| 
  8. $ file x 
  9. x: JPEG image data, baseline, precision 8, 1x1, components 1 

04 一個 JFIFifying Writer

我們編寫一個 jfifEncode 函數(shù),它可以直接替代 jpeg.Encode 但添加額外的 JFIF 字節(jié),只要第二個標記(緊接在 SOI 之后的那個)不是 APP0。

  1. package main 
  2.  
  3. import ( 
  4.     "errors" 
  5.     "image" 
  6.     "image/jpeg" 
  7.     "io" 
  8.     "os" 
  9.  
  10. func main() { 
  11.     m := image.NewGray(image.Rect(0, 0, 1, 1)) 
  12.     if err := jfifEncode(os.Stdout, m, nil); err != nil { 
  13.         os.Stderr.WriteString(err.Error() + "\n"
  14.         os.Exit(1) 
  15.     } 
  16.  
  17. func jfifEncode(w io.Writer, m image.Image, o *jpeg.Options) error { 
  18.     return jpeg.Encode(&jfifWriter{w: w}, m, o) 
  19.  
  20. // jfifWriter wraps an io.Writer to convert the data written to it from a plain 
  21. // JPEG to a JFIF-enhanced JPEG. It implicitly buffers the first three bytes 
  22. // written to it. The fourth byte will tell whether the original JPEG already 
  23. // has the APP0 chunk that JFIF requires. 
  24. type jfifWriter struct { 
  25.     // w is the wrapped io.Writer. 
  26.     w io.Writer 
  27.     // n ranges between 0 and 4 inclusive. It is the number of bytes written to 
  28.     // this (which also implements io.Writer), saturating at 4. The first three 
  29.     // bytes are expected to be {0xff, 0xd8, 0xff}. The fourth byte indicates 
  30.     // whether the second JPEG chunk is an APP0 chunk or something else
  31.     n int 
  32.  
  33. func (jw *jfifWriter) Write(p []byte) (int, error) { 
  34.     nSkipped := 0 
  35.  
  36.     for jw.n < 3 { 
  37.         if len(p) == 0 { 
  38.             return nSkipped, nil 
  39.         } else if p[0] != jfifChunk[jw.n] { 
  40.             return nSkipped, errors.New("jfifWriter: input was not a JPEG"
  41.         } 
  42.         nSkipped++ 
  43.         jw.n++ 
  44.         p = p[1:] 
  45.     } 
  46.  
  47.     if jw.n == 3 { 
  48.         if len(p) == 0 { 
  49.             return nSkipped, nil 
  50.         } 
  51.         chunk := jfifChunk 
  52.         if p[0] == 0xe0 { 
  53.             // The input JPEG already has an APP0 marker. Just write SOI (2 
  54.             // bytes) and an 0xff: the three bytes we've previously skipped. 
  55.             chunk = chunk[:3] 
  56.         } 
  57.         if _, err := jw.w.Write(chunk); err != nil { 
  58.             return nSkipped, err 
  59.         } 
  60.         jw.n = 4 
  61.     } 
  62.  
  63.     n, err := jw.w.Write(p) 
  64.     return n + nSkipped, err 
  65.  
  66. // jfifChunk is a sequence: an SOI chunk, an APP0/JFIF chunk and finally the 
  67. // 0xff that starts the third chunk. 
  68. var jfifChunk = []byte{ 
  69.     0xff, 0xd8, // SOI  marker. 
  70.     0xff, 0xe0, // APP0 marker. 
  71.     0x00, 0x10, // Length: 16 byte payload (including these two bytes). 
  72.     0x4a, 0x46, 0x49, 0x46, 0x00, // "JFIF\x00"
  73.     0x01, 0x01, // Version 1.01. 
  74.     0x00,       // No density units. 
  75.     0x00, 0x01, // Horizontal pixel density. 
  76.     0x00, 0x01, // Vertical   pixel density. 
  77.     0x00, // Thumbnail width. 
  78.     0x00, // Thumbnail height. 
  79.     0xff, // Start of the third chunk's marker. 

現(xiàn)在運行它會生成一個 JFIF 文件,而不僅僅是一個 JPEG 文件。

  1. $ go run from-jpeg-to-jfif.go > y 
  2. $ hd y | head -n 5 
  3. 00000000  ff d8 ff e0 00 10 4a 46  49 46 00 01 01 00 00 01  |......JFIF......| 
  4. 00000010  00 01 00 00 ff db 00 84  00 08 06 06 07 06 05 08  |................| 
  5. 00000020  07 07 07 09 09 08 0a 0c  14 0d 0c 0b 0b 0c 19 12  |................| 
  6. 00000030  13 0f 14 1d 1a 1f 1e 1d  1a 1c 1c 20 24 2e 27 20  |........... $.' | 
  7. 00000040  22 2c 23 1c 1c 28 37 29  2c 30 31 34 34 34 1f 27  |",#..(7),01444.'| 
  8. $ file y 
  9. y: JPEG image data, JFIF... baseline... 

05 結(jié)論

這里的細節(jié)是關(guān)于 JPEG 和 JFIF 的,但一般的想法是,如果 encoding 庫(Go 中的一個包)缺少一個功能,你可以不通過更改該庫來修復它(或以其他方式對其進行處理),而是預處理輸入或處理輸出。

原文鏈接:https://nigeltao.github.io/blog/2021/from-jpeg-to-jfif.html

參考資料

[1]One of these JPEGs is not like the other: https://blog.benjojo.co.uk/post/not-all-jpegs-are-the-same

[2]fork: https://github.com/benjojo/app0-image-jpeg

[3]修復: https://github.com/benjojo/app0-image-jpeg/commit/645750c1672807c80c08a57a684a0ada7bf371d9

[4]Example.jpg: https://en.wikipedia.org/wiki/File:Example.jpg

[5]JFIF 規(guī)范: https://www.w3.org/Graphics/JPEG/jfif3.pdf

[6]EXIF 規(guī)范: https://www.exif.org/Exif2-2.PDF

[7]發(fā)布周期: https://github.com/golang/go/wiki/Go-Release-Cycle

[8]兼容性限制: https://golang.org/doc/go1compat

[9]jpeg.Encode: https://pkg.go.dev/image/jpeg#Encode

本文轉(zhuǎn)載自微信公眾號「程序員ug」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系程序員ug公眾號。

 

責任編輯:武曉燕 來源: 程序員ug
相關(guān)推薦

2024-07-09 08:07:37

Go性能工具

2025-02-12 11:06:24

Go函數(shù)MultiBytes

2022-10-20 08:59:18

Go接口類型

2023-11-28 08:52:48

Go日志庫

2023-10-07 09:08:32

Golangbufio

2023-11-07 09:02:07

Golangbytes

2024-04-29 08:45:16

Go語言PDF

2020-10-25 06:30:48

Go語言編程語言

2012-05-19 22:17:30

Android

2022-02-15 11:49:08

eBPFGo內(nèi)存

2015-12-10 10:47:25

微軟Windows Liv開源

2020-11-11 17:00:02

PythonOffice文件PDF

2021-08-27 15:30:13

PSPhotoshopPS格式

2022-04-24 15:29:17

微服務(wù)go

2021-12-29 07:56:32

Go byte io.Reader

2021-09-17 14:13:28

JavaScript編程字符串

2023-01-30 15:41:10

Channel控制并發(fā)

2014-11-14 09:54:08

災難恢復DRaaS云災難恢復

2021-02-01 06:39:42

模塊封裝庫

2019-01-03 15:40:38

數(shù)據(jù)庫SQL查詢JSON
點贊
收藏

51CTO技術(shù)棧公眾號