Skip to content

Commit

Permalink
tools/diffvram - image diffing tool
Browse files Browse the repository at this point in the history
  • Loading branch information
JaCzekanski committed Oct 5, 2019
1 parent d1a63ee commit c1d69bb
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
build/
.vscode/
.idea/

*.elf
*.exe
*.zip

*-amd64
*-386
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ services:

script:
- docker run -it -v $(pwd):/build jaczekanski/psn00bsdk:latest make
- find . \( -iname "*.exe" -o -iname "*.png" -o -iname "*.log" \) -print | zip tests.zip -@
- ./tools/diffvram/build-ci.sh
- find . \( -iname "*.exe" -o -iname "*.png" -o -iname "*.log" -o -iname "*-amd64" \) -print | zip tests.zip -@

before_deploy:
- git config --global user.email "[email protected]"
Expand All @@ -25,4 +26,4 @@ deploy:
skip_cleanup: true
on:
repo: JaCzekanski/ps1-tests
tags: false
tags: false
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ps1-tests

Collection of PlayStation 1 tests for emulator development and hardware verification

## Tests

Name | Description
Expand All @@ -17,6 +19,12 @@ timers | Run Timer0,1,2 using various clock sources and sync modes

Note: Make sure your PS-EXE loaded does set default value for Stack Pointer - these .exes has SP set to 0.

## Tools

Name | Description
---------------------|------------
diffvram | Diff two images and write diff png if image contents aren't exactly the same

## Download

[Latest release](https://github.com/JaCzekanski/ps1-tests/releases/latest)
Expand Down
6 changes: 2 additions & 4 deletions gpu/benchmark/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void initVideo()
}

void clearScreen() {
FILL* f = ptr;
FILL* f = (FILL*)ptr;
ptr += sizeof(FILL);

setFill(f);
Expand All @@ -47,8 +47,6 @@ void clearScreen() {
addPrim(ot[db] + (OT_LEN-1), f);
}

POLY_G4 poly;

int main()
{
initVideo();
Expand All @@ -62,7 +60,7 @@ int main()

ClearOTagR( ot[db] + (OT_LEN-1), OT_LEN );
for (int i = 0; i < 100; i++) {
POLY_G4 *p = ptr;
POLY_G4 *p = (POLY_G4*)ptr;
ptr += sizeof(POLY_G4);
setPolyG4(p);
setSemiTrans(p, 0); // Disable transparency
Expand Down
27 changes: 27 additions & 0 deletions tools/diffvram/build-ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

name="diffvram"
docker_image="golang:1.12-alpine"
platforms=("windows/amd64" "linux/amd64" "darwin/amd64")
basedir=$(dirname "$0")

pushd $basedir
for platform in "${platforms[@]}"
do
platform_split=(${platform//\// })
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
output_name=$name'-'$GOOS'-'$GOARCH
if [ $GOOS = "windows" ]; then
output_name+='.exe'
fi

echo Building $output_name
env GOOS=$GOOS GOARCH=$GOARCH docker run -v $(pwd):/build -w /build golang:1.12-alpine go build -o $output_name .
if [ $? -ne 0 ]; then
echo 'An error has occurred! Aborting the script execution...'
exit 1
fi
done

popd
136 changes: 136 additions & 0 deletions tools/diffvram/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package main

import (
"image"
"image/color"
"image/png"
"log"
"os"
)

func min(a, b int) int {
if a < b {
return a
} else {
return b
}
}

func abs(a int) int {
if a < 0 {
return -a
} else {
return a
}
}

func diff(a, b uint32) uint8 {
if a == b {
return 0
}

d := abs(int(a) - int(b))

return uint8(128 + d/2)
}

func main() {
log.SetFlags(0)

if len(os.Args) < 3 {
log.Printf("usage: diffvram img1.png img2.png [diff.png]")
return
}

fimg1, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
return
}

fimg2, err := os.Open(os.Args[2])
if err != nil {
log.Fatal(err)
return
}

img1, _, err := image.Decode(fimg1)
if err != nil {
log.Fatalf("%s: %s.\n", os.Args[1], err)
return
}

img2, _, err := image.Decode(fimg2)
if err != nil {
log.Fatalf("%s: %s.\n", os.Args[2], err)
return
}

size1 := img1.Bounds().Size()
size2 := img2.Bounds().Size()

if size1 != size2 {
log.Printf("Size difference: %dx%d vs %dx%d\n", size1.X, size1.Y, size2.X, size2.Y)
}

size := image.Point{
X: min(size1.X, size2.X),
Y: min(size1.Y, size2.Y),
}

diffImage := image.NewRGBA(image.Rectangle{
Min: image.Point{
X: 0,
Y: 0,
},
Max: size,
})

diffCount := 0
for y := 0; y < size.Y; y++ {
for x := 0; x < size.X; x++ {
p1 := img1.At(x, y)
p2 := img2.At(x, y)

if p1 != p2 {
diffCount++
}

r1, g1, b1, a1 := p1.RGBA()
r2, g2, b2, a2 := p2.RGBA()
c := color.RGBA{
R: diff(r1, r2),
G: diff(g1, g2),
B: diff(b1, b2),
A: 255 - diff(a1, a2),
}

diffImage.Set(x, y, c)
}
}

if diffCount != 0 {
log.Printf("Images are different (%d pixels)\n", diffCount)
diffName := "diff.png"
if len(os.Args) >= 4 {
diffName = os.Args[3]
}

fdiff, err := os.Create(diffName)
if err != nil {
log.Fatal(err)
return
}
err = png.Encode(fdiff, diffImage)
if err != nil {
log.Fatalf("%s: %s.\n", diffName, err)
return
}

log.Printf("Diff written to %s", diffName)
os.Exit(2)
return
}

log.Printf("Images are the same\n")
}

0 comments on commit c1d69bb

Please sign in to comment.