Skip to content
This repository has been archived by the owner on Oct 4, 2019. It is now read-only.

Commit

Permalink
Added background color support and missing tile test
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-ward committed Apr 10, 2018
1 parent 721c945 commit f7ff832
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[![GoDoc](https://godoc.org/github.com/brendan-ward/tilemerge?status.svg)](https://godoc.org/github.com/brendan-ward/tilemerge)
[![Build Status](https://travis-ci.org/brendan-ward/tilemerge.svg?branch=master)](https://travis-ci.org/brendan-ward/tilemerge)
[![Coverage Status](https://coveralls.io/repos/github/brendan-ward/tilemerge/badge.svg?branch=master)](https://coveralls.io/github/brendan-ward/tilemerge?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/brendan-ward/tilemerge)](https://goreportcard.com/report/github.com/brendan-ward/tilemerge)

# tilemerge
Merge 2D map tiles into a single image.

Expand Down
Binary file added test_data/output/test_background.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test_data/output/test_missing.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions tilemerge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"image/color"
"image/draw"

_ "image/jpeg"
_ "image/png"
_ "image/jpeg" // load jpeg decoder
_ "image/png" // load png decoder
)

// TILE_SIZE uses default size of map tiles for now
Expand All @@ -34,11 +34,15 @@ type Tiles struct {
// Any tile that
func Merge(tiles Tiles, xOff, yOff, width, height int, bg color.Color) (image.Image, error) {

// TODO: fill with background color. Is this needed for transparency?
img := image.NewRGBA(image.Rect(0, 0,
(tiles.X1-tiles.X0+1)*TILE_SIZE,
(tiles.Y1-tiles.Y0+1)*TILE_SIZE))

if bg != nil {
// Fill background color
draw.Draw(img, img.Bounds(), &image.Uniform{bg}, image.ZP, draw.Src)
}

// tile transform is x = (tile.X - x0) * TILE_SIZE, y = (tile.Y - y) * TILE_SIZE
for _, tile := range tiles.Tiles {
if tile.Data == nil {
Expand Down
35 changes: 35 additions & 0 deletions tilemerge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"image"
"image/color"
"image/jpeg"
"io/ioutil"
"os"
Expand Down Expand Up @@ -249,3 +250,37 @@ func Test_Merge_crop(t *testing.T) {
verifyDimensions(t, img, width, height)
verifyJPG(t, img, "test_data/output/test_crop.jpg")
}

func Test_Merge_Missing_Tile(t *testing.T) {
tiles := jpgTiles()
// remove a tile's data
tiles.Tiles[0].Data = nil
img, err := Merge(tiles, 0, 0, 2*TILE_SIZE, 2*TILE_SIZE, nil)
if err != nil {
panic(err)
}

if *update {
exportJPG(img, "test_data/output/test_missing.jpg")
}

verifyDimensions(t, img, 2*TILE_SIZE, 2*TILE_SIZE)
verifyJPG(t, img, "test_data/output/test_missing.jpg")
}

func Test_Merge_Background(t *testing.T) {
tiles := jpgTiles()
// remove a tile's data
tiles.Tiles[0].Data = nil
img, err := Merge(tiles, 0, 0, 2*TILE_SIZE, 2*TILE_SIZE, color.RGBA{uint8(255), uint8(0), uint8(0), uint8(255)})
if err != nil {
panic(err)
}

if *update {
exportJPG(img, "test_data/output/test_background.jpg")
}

verifyDimensions(t, img, 2*TILE_SIZE, 2*TILE_SIZE)
verifyJPG(t, img, "test_data/output/test_background.jpg")
}

0 comments on commit f7ff832

Please sign in to comment.