From f40ef01244b912baa7a6525ffd040db92af120f1 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 31 Oct 2021 15:35:14 +1100 Subject: [PATCH] Have make-red-blue-gradient use iCCP profile name --- script/make-red-blue-gradient.go | 36 +++++++++++++++++- ...blue-gradient.dcip3d65-no-chrm-no-gama.png | Bin 1146 -> 1153 bytes 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/script/make-red-blue-gradient.go b/script/make-red-blue-gradient.go index 01d22e57..a704efec 100644 --- a/script/make-red-blue-gradient.go +++ b/script/make-red-blue-gradient.go @@ -25,6 +25,7 @@ package main import ( "bytes" "compress/zlib" + "errors" "flag" "hash/crc32" "image" @@ -32,6 +33,8 @@ import ( "image/png" "math" "os" + "path/filepath" + "strings" ) var ( @@ -111,9 +114,14 @@ func main1() error { if err != nil { return err } + name, err := iccpName(*iccp) + if err != nil { + return err + } data := &bytes.Buffer{} - data.WriteString("icc\x00") // An arbitrary NUL terminated name. - data.WriteByte(0x00) // 0x00 means zlib compression. + data.WriteString(name) + data.WriteByte(0x00) // The name is NUL terminated. + data.WriteByte(0x00) // 0x00 means zlib compression. w := zlib.NewWriter(data) w.Write(raw) w.Close() @@ -147,3 +155,27 @@ func appendPNGChunk(b []byte, name string, chunk ...byte) []byte { byte(c>>0), ) } + +// iccpName determines a "bar" iCCP profile name (that satisifes the PNG +// specification re the iCCP chunk) based on the "foo/bar.icc" filename. +func iccpName(filename string) (string, error) { + s := filepath.Base(filename) + if i := strings.IndexByte(s, '.'); i >= 0 { + s = s[:i] + } + s = strings.TrimSpace(s) + if strings.Index(s, " ") >= 0 { + return "", errors.New("ICCP name cannot contain consecutive spaces") + } else if (len(s) < 1) || (79 < len(s)) { + return "", errors.New("ICCP name has invalid length.") + } + for i := 0; i < len(s); i++ { + if (s[i] < ' ') || ('~' < s[i]) { + // Strictly speaking, the PNG specification allows the iCCP name to + // be printable Latin-1, not just printable ASCII, but printable + // ASCII is easier and avoids any later Latin-1 vs UTF-8 ambiguity. + return "", errors.New("ICCP name is not printable ASCII") + } + } + return s, nil +} diff --git a/test/data/red-blue-gradient.dcip3d65-no-chrm-no-gama.png b/test/data/red-blue-gradient.dcip3d65-no-chrm-no-gama.png index c5e43ab4ce7935bbfba9790a550c9670ac95f36a..aa44cc9a512945dd9f7992ceb95afb301a1a1b93 100644 GIT binary patch delta 36 scmeyx(a1SLg}*A(**U<)*;6;bSl7kObfZoIBg=$^Ccc{&Fy3MU0Mfq;nE(I) delta 29 lcmZqV{KYXrg`+sr**PFHIeDXT0VB(F*WWWYFK4{P1OT1P3n~Br