From 09b8483ed67d3b85e983ef86c34260081975e1cb Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 2 May 2025 06:47:07 +0900 Subject: fix(blog/nuldoc): remove unnecessary "language" attribute --- .../index.html | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'vhosts/blog/public/posts/2023-04-01/implementation-of-minimal-png-image-encoder/index.html') diff --git a/vhosts/blog/public/posts/2023-04-01/implementation-of-minimal-png-image-encoder/index.html b/vhosts/blog/public/posts/2023-04-01/implementation-of-minimal-png-image-encoder/index.html index f8f850da..30ee2471 100644 --- a/vhosts/blog/public/posts/2023-04-01/implementation-of-minimal-png-image-encoder/index.html +++ b/vhosts/blog/public/posts/2023-04-01/implementation-of-minimal-png-image-encoder/index.html @@ -88,7 +88,7 @@

以下のソースコードをベースにする。 今回 PNG のデコーダは扱わないので、読み込みには Go の標準ライブラリ image/png を用いる。

-
+
package main
 
 import (
@@ -168,7 +168,7 @@
               

writeSignature の実装はこちら:

-
+
import "encoding/binary"
 
 func writeSignature(w io.Writer) {
@@ -211,7 +211,7 @@
               

CRC (Cyclic Redundancy Check) は誤り検出符号の一種。Go 言語では hash/crc32 パッケージにあるが、今回はこれも自前で実装する。PNG の仕様書に C 言語のサンプルコードが載っている ( D. Sample CRC implementation ) ので、これを Go に移植する。

-
+
var (
 	crcTable         [256]uint32
 	crcTableComputed bool
@@ -251,7 +251,7 @@
               

できた crc 関数を使って、chunk 一般を書き込む関数も用意しておこう。

-
+
func writeChunk(w io.Writer, chunkType string, data []byte) {
 	typeAndData := make([]byte, 0, len(chunkType)+len(data))
 	typeAndData = append(typeAndData, []byte(chunkType)...)
@@ -347,7 +347,7 @@
               

今回ほとんどのデータは決め打ちするので、データに応じて変わるのは width と height だけになる。コードは次のようになる。

-
+
import "bytes"
 
 func writeChunkIhdr(w io.Writer, width, height uint32) {
@@ -394,7 +394,7 @@
                 

Adler-32 も CRC と同じく誤り検出符号である。こちらも zlib の仕様書に C 言語でサンプルコードが記載されている ( 9. Appendix: Sample code ) ので、Go に移植する。

-
+
const adler32Base = 65521
 
 func updateAdler32(adler uint32, buf []byte) uint32 {
@@ -435,7 +435,7 @@
                 

実際にこの手抜き zlib を実装したものがこちら:

-
+
func encodeZlib(data []byte) []byte {
 	var buf bytes.Buffer
 
@@ -473,7 +473,7 @@
                 

先ほどの encodeZlib も使って実際に実装したものがこちら:

-
+
func writeChunkIdat(w io.Writer, width, height uint32, img image.Image) {
 	var pixels bytes.Buffer
 	for y := uint32(0); y < height; y++ {
@@ -499,7 +499,7 @@
               

特に追加のデータはなく、必要なのは chunk type の IEND くらいなので実装は簡単:

-
+
func writeChunkIend(w io.Writer) {
 	writeChunk(w, "IEND", nil)
 }
@@ -511,7 +511,7 @@

最後に全ソースコードを再掲しておく。

-
+
package main
 
 import (
-- 
cgit v1.2.3-70-g09d2