Nigel Tao | 79a9455 | 2017-11-30 16:37:20 +1100 | [diff] [blame] | 1 | // Copyright 2017 The Wuffs Authors. |
Nigel Tao | d4372cb | 2017-10-12 11:17:41 +1100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 14 | |
Nigel Tao | 788479d | 2021-08-22 10:52:51 +1000 | [diff] [blame] | 15 | //go:build ignore |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 16 | // +build ignore |
| 17 | |
| 18 | package main |
| 19 | |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 20 | // extract-palette-indexes.go extracts the 1-byte-per-pixel indexes and, for |
Nigel Tao | 34cfc3f | 2018-05-24 10:33:02 +1000 | [diff] [blame] | 21 | // paletted images, the 4-byte-per-entry BGRA (premultiplied alpha) palette of |
| 22 | // a paletted or gray GIF and PNG image. It checks that the GIF and PNG encode |
| 23 | // equivalent images. |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 24 | // |
| 25 | // Usage: go run extract-palette-indexes.go foo.gif foo.png |
| 26 | |
| 27 | import ( |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 28 | "bytes" |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 29 | "fmt" |
| 30 | "image" |
| 31 | "image/gif" |
| 32 | "image/png" |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 33 | "os" |
| 34 | "path/filepath" |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | func main() { |
| 38 | if err := main1(); err != nil { |
| 39 | os.Stderr.WriteString(err.Error() + "\n") |
| 40 | os.Exit(1) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func main1() error { |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 45 | baseFilename, bPalette, bIndexes := "", []byte(nil), []byte(nil) |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 46 | for i, a := range os.Args[1:] { |
| 47 | b := a[:len(a)-len(filepath.Ext(a))] |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 48 | palette, indexes, err := decode(a) |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 49 | if err != nil { |
| 50 | return err |
| 51 | } |
| 52 | if i == 0 { |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 53 | baseFilename, bPalette, bIndexes = b, palette, indexes |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 54 | } else if baseFilename != b { |
| 55 | return fmt.Errorf("arguments do not have a common base path") |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 56 | } else if !bytes.Equal(bPalette, palette) { |
| 57 | return fmt.Errorf("palettes are not equivalent") |
| 58 | } else if !bytes.Equal(bIndexes, indexes) { |
| 59 | return fmt.Errorf("indexes are not equivalent") |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 60 | } |
| 61 | } |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 62 | if bPalette != nil { |
Nigel Tao | 226c476 | 2021-08-22 11:05:43 +1000 | [diff] [blame] | 63 | if err := os.WriteFile(baseFilename+".palette", bPalette, 0644); err != nil { |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 64 | return err |
| 65 | } |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 66 | } |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 67 | if bIndexes != nil { |
Nigel Tao | 226c476 | 2021-08-22 11:05:43 +1000 | [diff] [blame] | 68 | if err := os.WriteFile(baseFilename+".indexes", bIndexes, 0644); err != nil { |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 69 | return err |
| 70 | } |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 71 | } |
| 72 | return nil |
| 73 | } |
| 74 | |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 75 | func decode(filename string) (palette []byte, indexes []byte, err error) { |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 76 | f, err := os.Open(filename) |
| 77 | if err != nil { |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 78 | return nil, nil, err |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 79 | } |
| 80 | defer f.Close() |
| 81 | |
| 82 | m, err := image.Image(nil), error(nil) |
| 83 | switch ext := filepath.Ext(filename); ext { |
| 84 | case ".gif": |
| 85 | m, err = gif.Decode(f) |
| 86 | case ".png": |
| 87 | m, err = png.Decode(f) |
| 88 | default: |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 89 | return nil, nil, fmt.Errorf("unsupported filename extension %q", ext) |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 90 | } |
| 91 | if err != nil { |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 92 | return nil, nil, err |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 93 | } |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 94 | switch m := m.(type) { |
| 95 | case *image.Gray: |
| 96 | return nil, m.Pix, nil |
| 97 | case *image.Paletted: |
Nigel Tao | 34cfc3f | 2018-05-24 10:33:02 +1000 | [diff] [blame] | 98 | palette = make([]byte, 4*256) |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 99 | allGray := true |
| 100 | for i, c := range m.Palette { |
Nigel Tao | 34cfc3f | 2018-05-24 10:33:02 +1000 | [diff] [blame] | 101 | r, g, b, a := c.RGBA() |
| 102 | palette[4*i+0] = uint8(b >> 8) |
| 103 | palette[4*i+1] = uint8(g >> 8) |
| 104 | palette[4*i+2] = uint8(r >> 8) |
| 105 | palette[4*i+3] = uint8(a >> 8) |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 106 | if allGray { |
| 107 | y := uint32(i) |
| 108 | allGray = r>>8 == y && g>>8 == y && b>>8 == y |
| 109 | } |
| 110 | } |
| 111 | if allGray { |
| 112 | return nil, m.Pix, nil |
| 113 | } |
| 114 | return palette, m.Pix, nil |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 115 | } |
Nigel Tao | 6283389 | 2017-06-03 17:35:29 +1000 | [diff] [blame] | 116 | return nil, nil, fmt.Errorf("decoded image type: got %T, want *image.Paletted", m) |
Nigel Tao | 25101a0 | 2017-06-01 10:15:32 +1000 | [diff] [blame] | 117 | } |