Commit aeb8b458 authored by Nigel Tao's avatar Nigel Tao

image/color/palette: move Plan9Palette and WebSafePalette out of the

image/color package into their own package. They require some non-
trivial init-time code (interface conversions, currently 40KiB of text)
that would otherwise burden any Go program that imported image/color.

R=r
CC=golang-dev
https://golang.org/cl/13256046
parent 5d6ea6f7
This diff is collapsed.
......@@ -7,7 +7,7 @@
package main
// This program generates palette.go. Invoke it as
// go run gen.go |gofmt >palette.go
// go run gen.go | gofmt > palette.go
import (
"fmt"
......@@ -16,7 +16,10 @@ import (
func main() {
fmt.Println("// generated by go run gen.go; DO NOT EDIT")
fmt.Println()
fmt.Println("package color")
fmt.Println("// Package palette provides standard color palettes.")
fmt.Println("package palette")
fmt.Println()
fmt.Println(`import "image/color"`)
fmt.Println()
printPlan9()
printWebSafe()
......@@ -46,14 +49,14 @@ func printPlan9() {
c[2] = b * num / den
}
lines[i+(j&0x0f)] =
fmt.Sprintf("\tRGBA{0x%02x, 0x%02x, 0x%02x, 0xff},", c[0], c[1], c[2])
fmt.Sprintf("\tcolor.RGBA{0x%02x, 0x%02x, 0x%02x, 0xff},", c[0], c[1], c[2])
}
}
}
}
fmt.Println("// Plan9Palette is a 256-color palette that partitions the 24-bit RGB space")
fmt.Println("// Plan9 is a 256-color palette that partitions the 24-bit RGB space")
fmt.Println("// into 4×4×4 subdivision, with 4 shades in each subcube. Compared to the")
fmt.Println("// WebSafePalette, the idea is to reduce the color resolution by dicing the")
fmt.Println("// WebSafe, the idea is to reduce the color resolution by dicing the")
fmt.Println("// color cube into fewer cells, and to use the extra space to increase the")
fmt.Println("// intensity resolution. This results in 16 gray shades (4 gray subcubes with")
fmt.Println("// 4 samples in each), 13 shades of each primary and secondary color (3")
......@@ -63,7 +66,7 @@ func printPlan9() {
fmt.Println("//")
fmt.Println("// This palette was used in the Plan 9 Operating System, described at")
fmt.Println("// http://plan9.bell-labs.com/magic/man2html/6/color")
fmt.Println("var Plan9Palette = []Color{")
fmt.Println("var Plan9 = []color.Color{")
for _, line := range lines {
fmt.Println(line)
}
......@@ -77,15 +80,15 @@ func printWebSafe() {
for g := 0; g < 6; g++ {
for b := 0; b < 6; b++ {
lines[36*r+6*g+b] =
fmt.Sprintf("\tRGBA{0x%02x, 0x%02x, 0x%02x, 0xff},", 0x33*r, 0x33*g, 0x33*b)
fmt.Sprintf("\tcolor.RGBA{0x%02x, 0x%02x, 0x%02x, 0xff},", 0x33*r, 0x33*g, 0x33*b)
}
}
}
fmt.Println("// WebSafePalette is a 216-color palette that was popularized by early versions")
fmt.Println("// WebSafe is a 216-color palette that was popularized by early versions")
fmt.Println("// of Netscape Navigator. It is also known as the Netscape Color Cube.")
fmt.Println("//")
fmt.Println("// See http://en.wikipedia.org/wiki/Web_colors#Web-safe_colors for details.")
fmt.Println("var WebSafePalette = []Color{")
fmt.Println("var WebSafe = []color.Color{")
for _, line := range lines {
fmt.Println(line)
}
......
This diff is collapsed.
......@@ -11,11 +11,11 @@ import (
// header, palette and trailer are parts of a valid 2x1 GIF image.
const (
header = "GIF89a" +
headerStr = "GIF89a" +
"\x02\x00\x01\x00" + // width=2, height=1
"\x80\x00\x00" // headerFields=(a color map of 2 pixels), backgroundIndex, aspect
palette = "\x10\x20\x30\x40\x50\x60" // the color map, also known as a palette
trailer = "\x3b"
paletteStr = "\x10\x20\x30\x40\x50\x60" // the color map, also known as a palette
trailerStr = "\x3b"
)
func TestDecode(t *testing.T) {
......@@ -41,8 +41,8 @@ func TestDecode(t *testing.T) {
}
for _, tc := range testCases {
b := &bytes.Buffer{}
b.WriteString(header)
b.WriteString(palette)
b.WriteString(headerStr)
b.WriteString(paletteStr)
// Write an image with bounds 2x1 but tc.nPix pixels. If tc.nPix != 2
// then this should result in an invalid GIF image. First, write a
// magic 0x2c (image descriptor) byte, bounds=(0,0)-(2,1), a flags
......@@ -61,7 +61,7 @@ func TestDecode(t *testing.T) {
b.WriteString("\x01\x02") // A 1-byte payload with an 0x02 byte.
}
b.WriteByte(0x00) // An empty block signifies the end of the image data.
b.WriteString(trailer)
b.WriteString(trailerStr)
got, err := Decode(b)
if err != tc.wantErr {
......@@ -143,7 +143,7 @@ func TestNoPalette(t *testing.T) {
// Manufacture a GIF with no palette, so any pixel at all
// will be invalid.
b.WriteString(header[:len(header)-3])
b.WriteString(headerStr[:len(headerStr)-3])
b.WriteString("\x00\x00\x00") // No global palette.
// Image descriptor: 2x1, no local palette.
......@@ -159,7 +159,7 @@ func TestNoPalette(t *testing.T) {
b.Write(enc.Bytes())
b.WriteByte(0x00) // An empty block signifies the end of the image data.
b.WriteString(trailer)
b.WriteString(trailerStr)
try(t, b.Bytes(), "gif: invalid pixel value")
}
......@@ -169,8 +169,8 @@ func TestPixelOutsidePaletteRange(t *testing.T) {
b := &bytes.Buffer{}
// Manufacture a GIF with a 2 color palette.
b.WriteString(header)
b.WriteString(palette)
b.WriteString(headerStr)
b.WriteString(paletteStr)
// Image descriptor: 2x1, no local palette.
b.WriteString("\x2c\x00\x00\x00\x00\x02\x00\x01\x00\x00\x02")
......@@ -185,7 +185,7 @@ func TestPixelOutsidePaletteRange(t *testing.T) {
b.Write(enc.Bytes())
b.WriteByte(0x00) // An empty block signifies the end of the image data.
b.WriteString(trailer)
b.WriteString(trailerStr)
// No error expected, unless the pixels are beyond the 2 color palette.
want := ""
......
......@@ -10,6 +10,7 @@ import (
"errors"
"image"
"image/color"
"image/color/palette"
"image/draw"
"io"
)
......@@ -248,7 +249,7 @@ type Options struct {
NumColors int
// Quantizer is used to produce a palette with size NumColors.
// color.Plan9Palette is used in place of a nil Quantizer.
// palette.Plan9 is used in place of a nil Quantizer.
Quantizer draw.Quantizer
// Drawer is used to convert the source image to the desired palette.
......@@ -308,7 +309,7 @@ func Encode(w io.Writer, m image.Image, o *Options) error {
pm, ok := m.(*image.Paletted)
if !ok || len(pm.Palette) > opts.NumColors {
// TODO: Pick a better sub-sample of the Plan 9 palette.
pm = image.NewPaletted(b, color.Plan9Palette[:opts.NumColors])
pm = image.NewPaletted(b, palette.Plan9[:opts.NumColors])
if opts.Quantizer != nil {
pm.Palette = opts.Quantizer.Quantize(make(color.Palette, 0, opts.NumColors), m)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment