Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronsor committed Jan 3, 2023
0 parents commit 8d13804
Show file tree
Hide file tree
Showing 113 changed files with 8,541 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
majokko
test.png
test.zng
test.jpg
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (C) 2022-2023 Ronsor Labs

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Majokko

`majokko` is intended to be a lightweight ImageMagick `convert` and
`identify` alternative written in Go. The underlying library, found
in the `henshin/` directory, can be reused by other programs.

**This software is still in alpha, and as such, we do not commit to
providing a stable API.**

## Supported Formats

```
+--- (D)ecode
|+-- (E)ncode
||+- (M)etadata wrangling
||| == Format ==
DE- JPEG
DEM PNG
DEM ZNG (Zstd PNG)
DE- GIF[1]
DEM NetPBM (PPM, PGM, etc.)
DE- JPEG XL[2][3]
DE- QOI (Quite OK Image format)
D-- WEBP
[1] Animated GIFs are not yet supported.
[2] Requires external `cjxl` and `djxl` binaries. Enable with the
`--enable-external-codecs` option.
[3] Format not yet supported.
```

## License and Copyright Notice

Copyright © 2022-2023 Ronsor Labs.

This software is provided to you under the terms of the MIT license.
See the included `LICENSE` file for more information.
9 changes: 9 additions & 0 deletions format/png/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# format/png

This version of `image/png` has been modified to support reading and writing
custom chunks (e.g. `tEXt` data).

## License and Copyright Notice

Copyright © 2009 The Go Authors
Copyright © 2022 Ronsor Labs
77 changes: 77 additions & 0 deletions format/png/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package png_test

import (
"encoding/base64"
"fmt"
"image"
"image/color"
"image/png"
"io"
"log"
"os"
"strings"
)

const gopher = `iVBORw0KGgoAAAANSUhEUgAAAEsAAAA8CAAAAAALAhhPAAAFfUlEQVRYw62XeWwUVRzHf2+OPbo9d7tsWyiyaZti6eWGAhISoIGKECEKCAiJJkYTiUgTMYSIosYYBBIUIxoSPIINEBDi2VhwkQrVsj1ESgu9doHWdrul7ba73WNm3vOPtsseM9MdwvvrzTs+8/t95ze/33sI5BqiabU6m9En8oNjduLnAEDLUsQXFF8tQ5oxK3vmnNmDSMtrncks9Hhtt/qeWZapHb1ha3UqYSWVl2ZmpWgaXMXGohQAvmeop3bjTRtv6SgaK/Pb9/bFzUrYslbFAmHPp+3WhAYdr+7GN/YnpN46Opv55VDsJkoEpMrY/vO2BIYQ6LLvm0ThY3MzDzzeSJeeWNyTkgnIE5ePKsvKlcg/0T9QMzXalwXMlj54z4c0rh/mzEfr+FgWEz2w6uk8dkzFAgcARAgNp1ZYef8bH2AgvuStbc2/i6CiWGj98y2tw2l4FAXKkQBIf+exyRnteY83LfEwDQAYCoK+P6bxkZm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LMQN+rsmo3grHl9VNJHbbwxoa47Vw5gupIqrZcjPh9R4Nye3nRDk199V+aetmvVtDRE8/+cbgAAgMIWGb3UA0MGLE9SCbWX670TDy1y98c3D27eppUjsZ6fql3jcd5rUe7+ZIlLNQny3Rd+E5Tct3WVhTM5RBCEdiEK0b6B+/ca2gYU393nFj/n1AygRQxPIUA043M42u85+z2SnssKrPl8Mx76NL3E6eXc3be7OD+H4WHbJkKI8AU8irbITQjZ+0hQcPEgId/Fn/pl9crKH02+5o2b9T/eMx7pKoskYgAAAABJRU5ErkJggg==`

// gopherPNG creates an io.Reader by decoding the base64 encoded image data string in the gopher constant.
func gopherPNG() io.Reader { return base64.NewDecoder(base64.StdEncoding, strings.NewReader(gopher)) }

func ExampleDecode() {
// This example uses png.Decode which can only decode PNG images.
// Consider using the general image.Decode as it can sniff and decode any registered image format.
img, err := png.Decode(gopherPNG())
if err != nil {
log.Fatal(err)
}

levels := []string{" ", "░", "▒", "▓", "█"}

for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
c := color.GrayModel.Convert(img.At(x, y)).(color.Gray)
level := c.Y / 51 // 51 * 5 = 255
if level == 5 {
level--
}
fmt.Print(levels[level])
}
fmt.Print("\n")
}
}

func ExampleEncode() {
const width, height = 256, 256

// Create a colored image of the given width and height.
img := image.NewNRGBA(image.Rect(0, 0, width, height))

for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
img.Set(x, y, color.NRGBA{
R: uint8((x + y) & 255),
G: uint8((x + y) << 1 & 255),
B: uint8((x + y) << 2 & 255),
A: 255,
})
}
}

f, err := os.Create("image.png")
if err != nil {
log.Fatal(err)
}

if err := png.Encode(f, img); err != nil {
f.Close()
log.Fatal(err)
}

if err := f.Close(); err != nil {
log.Fatal(err)
}
}
71 changes: 71 additions & 0 deletions format/png/paeth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package png

// intSize is either 32 or 64.
const intSize = 32 << (^uint(0) >> 63)

func abs(x int) int {
// m := -1 if x < 0. m := 0 otherwise.
m := x >> (intSize - 1)

// In two's complement representation, the negative number
// of any number (except the smallest one) can be computed
// by flipping all the bits and add 1. This is faster than
// code with a branch.
// See Hacker's Delight, section 2-4.
return (x ^ m) - m
}

// paeth implements the Paeth filter function, as per the PNG specification.
func paeth(a, b, c uint8) uint8 {
// This is an optimized version of the sample code in the PNG spec.
// For example, the sample code starts with:
// p := int(a) + int(b) - int(c)
// pa := abs(p - int(a))
// but the optimized form uses fewer arithmetic operations:
// pa := int(b) - int(c)
// pa = abs(pa)
pc := int(c)
pa := int(b) - pc
pb := int(a) - pc
pc = abs(pa + pb)
pa = abs(pa)
pb = abs(pb)
if pa <= pb && pa <= pc {
return a
} else if pb <= pc {
return b
}
return c
}

// filterPaeth applies the Paeth filter to the cdat slice.
// cdat is the current row's data, pdat is the previous row's data.
func filterPaeth(cdat, pdat []byte, bytesPerPixel int) {
var a, b, c, pa, pb, pc int
for i := 0; i < bytesPerPixel; i++ {
a, c = 0, 0
for j := i; j < len(cdat); j += bytesPerPixel {
b = int(pdat[j])
pa = b - c
pb = a - c
pc = abs(pa + pb)
pa = abs(pa)
pb = abs(pb)
if pa <= pb && pa <= pc {
// No-op.
} else if pb <= pc {
a = b
} else {
a = c
}
a += int(cdat[j])
a &= 0xff
cdat[j] = uint8(a)
c = b
}
}
}
91 changes: 91 additions & 0 deletions format/png/paeth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package png

import (
"bytes"
"math/rand"
"testing"
)

func slowAbs(x int) int {
if x < 0 {
return -x
}
return x
}

// slowPaeth is a slow but simple implementation of the Paeth function.
// It is a straight port of the sample code in the PNG spec, section 9.4.
func slowPaeth(a, b, c uint8) uint8 {
p := int(a) + int(b) - int(c)
pa := slowAbs(p - int(a))
pb := slowAbs(p - int(b))
pc := slowAbs(p - int(c))
if pa <= pb && pa <= pc {
return a
} else if pb <= pc {
return b
}
return c
}

// slowFilterPaeth is a slow but simple implementation of func filterPaeth.
func slowFilterPaeth(cdat, pdat []byte, bytesPerPixel int) {
for i := 0; i < bytesPerPixel; i++ {
cdat[i] += paeth(0, pdat[i], 0)
}
for i := bytesPerPixel; i < len(cdat); i++ {
cdat[i] += paeth(cdat[i-bytesPerPixel], pdat[i], pdat[i-bytesPerPixel])
}
}

func TestPaeth(t *testing.T) {
for a := 0; a < 256; a += 15 {
for b := 0; b < 256; b += 15 {
for c := 0; c < 256; c += 15 {
got := paeth(uint8(a), uint8(b), uint8(c))
want := slowPaeth(uint8(a), uint8(b), uint8(c))
if got != want {
t.Errorf("a, b, c = %d, %d, %d: got %d, want %d", a, b, c, got, want)
}
}
}
}
}

func BenchmarkPaeth(b *testing.B) {
for i := 0; i < b.N; i++ {
paeth(uint8(i>>16), uint8(i>>8), uint8(i))
}
}

func TestPaethDecode(t *testing.T) {
pdat0 := make([]byte, 32)
pdat1 := make([]byte, 32)
pdat2 := make([]byte, 32)
cdat0 := make([]byte, 32)
cdat1 := make([]byte, 32)
cdat2 := make([]byte, 32)
r := rand.New(rand.NewSource(1))
for bytesPerPixel := 1; bytesPerPixel <= 8; bytesPerPixel++ {
for i := 0; i < 100; i++ {
for j := range pdat0 {
pdat0[j] = uint8(r.Uint32())
cdat0[j] = uint8(r.Uint32())
}
copy(pdat1, pdat0)
copy(pdat2, pdat0)
copy(cdat1, cdat0)
copy(cdat2, cdat0)
filterPaeth(cdat1, pdat1, bytesPerPixel)
slowFilterPaeth(cdat2, pdat2, bytesPerPixel)
if !bytes.Equal(cdat1, cdat2) {
t.Errorf("bytesPerPixel: %d\npdat0: % x\ncdat0: % x\ngot: % x\nwant: % x", bytesPerPixel, pdat0, cdat0, cdat1, cdat2)
break
}
}
}
}
Loading

0 comments on commit 8d13804

Please sign in to comment.