| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Tim Windelschmidt | 8e19fa4 | 2024-11-12 13:39:43 +0000 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Tim Windelschmidt | 8e19fa4 | 2024-11-12 13:39:43 +0000 | [diff] [blame] | 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "flag" |
| 8 | "fmt" |
| 9 | "image/color" |
| 10 | "image/png" |
| 11 | "log" |
| 12 | "os" |
| 13 | ) |
| 14 | |
| 15 | func main() { |
| 16 | input := flag.String("input", "", "") |
| 17 | output := flag.String("output", "", "") |
| 18 | flag.Parse() |
| 19 | |
| 20 | if *input == "" || *output == "" { |
| 21 | log.Fatal("missing input or output flag") |
| 22 | } |
| 23 | |
| 24 | inputFile, err := os.Open(*input) |
| 25 | if err != nil { |
| 26 | log.Fatal("Error opening image file:", err) |
| 27 | return |
| 28 | } |
| 29 | defer inputFile.Close() |
| 30 | |
| 31 | img, err := png.Decode(inputFile) |
| 32 | if err != nil { |
| 33 | log.Fatal("Error decoding image:", err) |
| 34 | } |
| 35 | |
| 36 | if img.Bounds().Dx() != 80 || img.Bounds().Dy() != 20 { |
| 37 | log.Fatal("Image dimensions must be 80x20") |
| 38 | } |
| 39 | |
| 40 | var linear []uint8 |
| 41 | for y := 0; y < img.Bounds().Dy(); y++ { |
| 42 | for x := 0; x < img.Bounds().Dx(); x++ { |
| 43 | gray := color.GrayModel.Convert(img.At(x, y)).(color.Gray).Y |
| 44 | linear = append(linear, gray) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Perform RLE compression |
| 49 | var rle []uint8 |
| 50 | for len(linear) > 0 { |
| 51 | val := linear[0] |
| 52 | l := uint8(1) |
| 53 | for i := 1; i < len(linear); i++ { |
| 54 | if linear[i] != val { |
| 55 | break |
| 56 | } |
| 57 | l++ |
| 58 | } |
| 59 | |
| 60 | L := l |
| 61 | for l > 0 { |
| 62 | block := l |
| 63 | if block > 127 { |
| 64 | block = 127 |
| 65 | } |
| 66 | rle = append(rle, (val<<7)|block) |
| 67 | l -= block |
| 68 | } |
| 69 | linear = linear[L:] |
| 70 | } |
| 71 | |
| 72 | rle = append(rle, 0) |
| 73 | |
| 74 | outputFile, err := os.Create(*output) |
| 75 | if err != nil { |
| 76 | log.Fatalf("failed creating output file: %v", err) |
| 77 | } |
| 78 | defer outputFile.Close() |
| 79 | |
| 80 | outputFile.WriteString("logo: db ") |
| 81 | for i, r := range rle { |
| 82 | if i > 0 { |
| 83 | outputFile.WriteString(", ") |
| 84 | } |
| 85 | fmt.Fprintf(outputFile, "0x%02x", r) |
| 86 | } |
| 87 | outputFile.WriteString("\n") |
| 88 | } |