move: string projects
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
# The Brief Anatomy of a PNG image
|
||||
|
||||
```
|
||||
The first 24 bytes:
|
||||
|
||||
+=================================+
|
||||
| PNG Header | 8 bytes | -> 89 50 4e 47 0d 0a 1a 0a
|
||||
+---------------------+-----------+
|
||||
| IHDR Chunk Header | |
|
||||
| Chunk Length | 4 bytes | -> The length of the IHDR Chunk Data
|
||||
| Chunk Type | 4 bytes | -> 49 48 44 52
|
||||
+---------------------+-----------+
|
||||
| IHDR Chunk Data | |
|
||||
| Width | 4 bytes | -> uint32 — big endian
|
||||
| Height | 4 bytes | -> uint32 — big endian
|
||||
+=================================+
|
||||
```
|
@@ -0,0 +1,26 @@
|
||||
# The Brief Anatomy of a PNG image
|
||||
|
||||
```
|
||||
The first 24 bytes:
|
||||
(all numbers are uint32 big-endian)
|
||||
|
||||
PNG HEADER
|
||||
╔════╗╔════╗╔════╗╔════╗╔════╗╔════╗╔════╗╔════╗
|
||||
║ 89 ║║ 50 ║║ 4e ║║ 47 ║║ 0d ║║ 0a ║║ 1a ║║ 0a ║
|
||||
╚════╝╚════╝╚════╝╚════╝╚════╝╚════╝╚════╝╚════╝
|
||||
0 1 2 3 4 5 6 7
|
||||
|
||||
CHUNK LENGTH CHUNK TYPE (IHDR)
|
||||
╔════╗╔════╗╔════╗╔════╗╔════╗╔════╗╔════╗╔════╗
|
||||
║ 00 ║║ 00 ║║ 00 ║║ 0d ║║ 49 ║║ 48 ║║ 44 ║║ 52 ║
|
||||
╚════╝╚════╝╚════╝╚════╝╚════╝╚════╝╚════╝╚════╝
|
||||
8 9 10 11 12 13 14 15
|
||||
|
||||
WIDTH HEIGHT
|
||||
╔════╗╔════╗╔════╗╔════╗╔════╗╔════╗╔════╗╔════╗
|
||||
║ 00 ║║ 00 ║║ 02 ║║ 4c ║║ 00 ║║ 00 ║║ 03 ║║ 20 ║
|
||||
╚════╝╚════╝╚════╝╚════╝╚════╝╚════╝╚════╝╚════╝
|
||||
16 17 18 19 20 21 22 23
|
||||
|
||||
... rest of the bytes in the png image ...
|
||||
```
|
@@ -0,0 +1,5 @@
|
||||
<EFBFBD>PNG
|
||||
|
||||
/<2F>}P<><50><EFBFBD><EFBFBD>̒<>><3E><>0jȂ&<26>✉\<5C>@<40><06><><EFBFBD><EFBFBD>T<03>LU
|
||||
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>x<><78>/<2F>
|
||||
Z`<60><14>w<EFBFBD><77>;<3B>9<05><>c<EFBFBD><03>
|
After Width: | Height: | Size: 80 B |
Binary file not shown.
BIN
x-tba/project-png-parser/png-parser-project/images/gopher.png
Normal file
BIN
x-tba/project-png-parser/png-parser-project/images/gopher.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 66 KiB |
91
x-tba/project-png-parser/png-parser-project/main.go
Normal file
91
x-tba/project-png-parser/png-parser-project/main.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
if len(args) == 0 {
|
||||
fmt.Println("run with a PNG file")
|
||||
return
|
||||
}
|
||||
|
||||
// this is not the best way
|
||||
// it's better only to read the first 24 bytes
|
||||
// this reads the whole file into memory
|
||||
img, err := ioutil.ReadFile(args[0])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
report()
|
||||
img = append([]byte(nil), img[:24]...)
|
||||
// img = img[:24:24] // unnecessary
|
||||
report()
|
||||
|
||||
// s.PrintBacking = true
|
||||
// s.MaxPerLine = 8
|
||||
// s.MaxElements = 24
|
||||
// s.PrintBytesHex = true
|
||||
// s.Show("first 24 bytes", img)
|
||||
|
||||
var (
|
||||
pngHeader = [...]byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a}
|
||||
ihdrChunkType = [...]byte{0x49, 0x48, 0x44, 0x52}
|
||||
)
|
||||
|
||||
// ------------------------------------------
|
||||
// Read the PNG header
|
||||
// https://www.w3.org/TR/2003/REC-PNG-20031110/#5PNG-file-signature
|
||||
// ------------------------------------------
|
||||
lpng := len(pngHeader)
|
||||
if !bytes.Equal(img[:lpng], pngHeader[:]) {
|
||||
fmt.Println("missing PNG header")
|
||||
return
|
||||
}
|
||||
|
||||
// skip the png header
|
||||
img = img[lpng:]
|
||||
|
||||
// ------------------------------------------
|
||||
// Read the IHDR chunk header
|
||||
// The IHDR chunk shall be the first chunk in the PNG datastream.
|
||||
// https://www.w3.org/TR/2003/REC-PNG-20031110/#11IHDR
|
||||
// ------------------------------------------
|
||||
header := img[:8] // get the length and chunk type
|
||||
|
||||
// ensure that the chunk type is IHDR
|
||||
if !bytes.Equal(header[4:8], ihdrChunkType[:]) {
|
||||
fmt.Println("missing IHDR chunk")
|
||||
return
|
||||
}
|
||||
|
||||
// ------------------------------------------
|
||||
// Read the IHDR Chunk Data
|
||||
// ------------------------------------------
|
||||
img = img[len(header):] // skip the IHDR chunk header data
|
||||
ihdr := img[:8] // read the width&height from the ihdr chunk
|
||||
|
||||
// All integers that require more than one byte shall be in:
|
||||
// network byte order = Big Endian
|
||||
// https://www.w3.org/TR/2003/REC-PNG-20031110/#7Integers-and-byte-order
|
||||
fmt.Printf("dimensions: %dx%d\n",
|
||||
// read the first 4 bytes (width)
|
||||
binary.BigEndian.Uint32(ihdr[:4]),
|
||||
// read the next 4 bytes (height)
|
||||
binary.BigEndian.Uint32(ihdr[4:8]))
|
||||
}
|
||||
|
||||
func report() {
|
||||
var m runtime.MemStats
|
||||
runtime.GC()
|
||||
runtime.ReadMemStats(&m)
|
||||
fmt.Printf(" > Memory Usage: %v KB\n", m.Alloc/1024)
|
||||
}
|
Reference in New Issue
Block a user