faiface / pixel

A hand-crafted 2D game library in Go
MIT License
4.46k stars 246 forks source link

Opening spritesheet larger than 64Kb displays as black #259

Closed danielkberry closed 4 years ago

danielkberry commented 4 years ago

Hi team, been struggling with this issue for a few days now, any advice is appreciated.

EDIT: I was unable to reproduce this on Linux (Ubuntu 20.04LTS), but it appears on MacOS 10.14

Main issue:

As best I can tell, loading a png file larger than 64kb results in black being drawn on the screen

Details

As a minimal working example, I'm using only the code from the drawing sprites tutorial and performing two runs: one with the original file, and one with the smaller file. The only variable which has changed between the two runs is the spritesheet file. The smaller (working) file was constructed by chopping the last 500 columns of pixels from the original image (chopping 400 or fewer pixels off still had the bug, but chopping 500 or more pixels did not have the bug). While the colormaps are different between the two images (see below), I don't believe this is related because I am able to read images of either colormap as long as they are smaller than 64kb.

Daniels-MacBook-Pro:assets danielberry$ file mushroom*
mushroom_chopped.png: PNG image data, 16300 x 320, 8-bit colormap, non-interlaced
mushroom_sheet.png:   PNG image data, 16800 x 320, 8-bit/color RGBA, non-interlaced

Files

The two files are:

Behavior

Expected behavior (occurs with the smaller "mushroom_chopped.png"):

Screen Shot 2020-09-20 at 8 47 00 AM

Actual behavior (occurs with the original "mushroom_sheet.png"):

Screen Shot 2020-09-20 at 8 47 16 AM

Code

(Using the tutorial as my minimal reproducible example)

package main

import (
    "image"
    "os"

    _ "image/png"

    "github.com/faiface/pixel"
    "github.com/faiface/pixel/pixelgl"
    "golang.org/x/image/colornames"
)

func loadPicture(path string) (pixel.Picture, error) {
    file, err := os.Open(path)
    if err != nil {
        return nil, err
    }
    defer file.Close()
    img, _, err := image.Decode(file)
    if err != nil {
        return nil, err
    }
    return pixel.PictureDataFromImage(img), nil
}

func run() {
    cfg := pixelgl.WindowConfig{
        Title:  "Pixel Rocks!",
        Bounds: pixel.R(0, 0, 1024, 768),
        VSync:  true,
    }
    win, err := pixelgl.NewWindow(cfg)
    if err != nil {
        panic(err)
    }

    pic, err := loadPicture("/Users/danielberry/HouseMouse/assets/mushroom_sheet.png")
    if err != nil {
        panic(err)
    }

    sprite := pixel.NewSprite(pic, pic.Bounds())

    win.Clear(colornames.Greenyellow)

    sprite.Draw(win, pixel.IM.Moved(win.Bounds().Center()))

    for !win.Closed() {
        win.Update()
    }
}

func main() {
    pixelgl.Run(run)
}
danielkberry commented 4 years ago

This appears to only impact that one image. Other spritesheets larger that 64kb still work. Since the issue is very specific and I was unable to reproduce on another computer I'm going to close this issue until I have more information.