From dbc209dd7e02ae98d047527b6bcd2289756adc42 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 14 Oct 2021 15:58:03 +1100 Subject: [PATCH] Add test/data/red-blue-gradient.*.png --- script/make-red-blue-gradient.go | 111 +++++++++++++++++++++ test/data/README.md | 2 + test/data/red-blue-gradient.gamma1dot0.png | Bin 0 -> 103 bytes test/data/red-blue-gradient.gamma1dot8.png | Bin 0 -> 103 bytes test/data/red-blue-gradient.gamma2dot2.png | Bin 0 -> 103 bytes 5 files changed, 113 insertions(+) create mode 100644 script/make-red-blue-gradient.go create mode 100644 test/data/red-blue-gradient.gamma1dot0.png create mode 100644 test/data/red-blue-gradient.gamma1dot8.png create mode 100644 test/data/red-blue-gradient.gamma2dot2.png diff --git a/script/make-red-blue-gradient.go b/script/make-red-blue-gradient.go new file mode 100644 index 00000000..5f912a0b --- /dev/null +++ b/script/make-red-blue-gradient.go @@ -0,0 +1,111 @@ +// Copyright 2021 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build ignore +// +build ignore + +package main + +// make-red-blue-gradient.go makes a simple PNG image with optional color +// correction chunks. +// +// Usage: go run make-red-blue-gradient.go > foo.png + +import ( + "bytes" + "flag" + "hash/crc32" + "image" + "image/color" + "image/png" + "math" + "os" +) + +var ( + gama = flag.Float64("gama", 2.2, "Gamma correction value (e.g. 2.2)") +) + +func main() { + if err := main1(); err != nil { + os.Stderr.WriteString(err.Error() + "\n") + os.Exit(1) + } +} + +func main1() error { + flag.Parse() + + // Make a red-blue gradient. + m := image.NewRGBA(image.Rect(0, 0, 16, 16)) + for y := 0; y < 16; y++ { + for x := 0; x < 16; x++ { + m.SetRGBA(x, y, color.RGBA{ + R: uint8(x * 0x11), + G: 0x00, + B: uint8(y * 0x11), + A: 0xFF, + }) + } + } + + // Encode to PNG. + buf := &bytes.Buffer{} + png.Encode(buf, m) + original := buf.Bytes() + + // Split the encoding into a magic+IHDR prefix and an IDAT+IEND suffix. The + // starting magic number is 8 bytes long. The IHDR chunk is 25 bytes long. + prefix, suffix := original[:33], original[33:] + out := []byte(nil) + out = append(out, prefix...) + + // Insert the optional gAMA chunk between the prefix and suffix. + if *gama > 0 { + g := uint32(math.Round(100000 / *gama)) + out = appendPNGChunk(out, "gAMA", + byte(g>>24), + byte(g>>16), + byte(g>>8), + byte(g>>0), + ) + } + + // Append the suffix and write to stdout. + out = append(out, suffix...) + _, err := os.Stdout.Write(out) + return err +} + +func appendPNGChunk(b []byte, name string, chunk ...byte) []byte { + n := uint32(len(chunk)) + b = append(b, + byte(n>>24), + byte(n>>16), + byte(n>>8), + byte(n>>0), + ) + b = append(b, name...) + b = append(b, chunk...) + hasher := crc32.NewIEEE() + hasher.Write([]byte(name)) + hasher.Write(chunk) + c := hasher.Sum32() + return append(b, + byte(c>>24), + byte(c>>16), + byte(c>>8), + byte(c>>0), + ) +} diff --git a/test/data/README.md b/test/data/README.md index 099b8a71..8a254d15 100644 --- a/test/data/README.md +++ b/test/data/README.md @@ -110,6 +110,8 @@ which is in the public domain. `pjw-thumbnail.*` are various encodings of an image derived from an iconic, original photo of Peter J. Weinberger by Rob Pike . +`red-blue-gradient.*.png` was generated by `script/make-red-blue-gradient.go`. + `rfc-6901-json-pointer.json` is the example JSON document given in the [RFC 6901 "JavaScript Object Notation (JSON) Pointer"](https://tools.ietf.org/rfc/rfc6901.txt) specification. diff --git a/test/data/red-blue-gradient.gamma1dot0.png b/test/data/red-blue-gradient.gamma1dot0.png new file mode 100644 index 0000000000000000000000000000000000000000..d91e74f27df02148e45bd55140d1991e04c4dc4b GIT binary patch literal 103 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4kiW$h6xih%orFLSkfJR9T^zg78t&m7SF)I zAm{1g7*a7ODKQ~IK!tTWjM&hE?#3=IGOKl;FAU!7hyk%57M!PC{xWt~$( F698=U8n*xd literal 0 HcmV?d00001 diff --git a/test/data/red-blue-gradient.gamma1dot8.png b/test/data/red-blue-gradient.gamma1dot8.png new file mode 100644 index 0000000000000000000000000000000000000000..77897180a6f6c1e6858f87d5ce90ba9483bdc442 GIT binary patch literal 103 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4kiW$h6xih%orFLSkfJR9T^yIvfSBp3nVP( z>EaktF()Z8AwfWebz_64(18_gfxOP{%nA$)|NlSwz+_*YUN(_|fq}u()z4*}Q$iB} Danl;f literal 0 HcmV?d00001 diff --git a/test/data/red-blue-gradient.gamma2dot2.png b/test/data/red-blue-gradient.gamma2dot2.png new file mode 100644 index 0000000000000000000000000000000000000000..23aa8168de1908c148c6d5136df4b449a9c7c3dc GIT binary patch literal 103 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4kiW$h6xih%orFLSkfJR9T^xl_H+M9WMyDr zkn?nL45^rtl$ekppu)Pb!BgnKinc&rXLn`=28RFtAAMl5uTC$U$iTqB;OXk;vd$@? F2>?&Y8ZQ6< literal 0 HcmV?d00001