Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't make GIFs work properly #173

Open
ivanjaros opened this issue Nov 9, 2024 · 2 comments
Open

Can't make GIFs work properly #173

ivanjaros opened this issue Nov 9, 2024 · 2 comments

Comments

@ivanjaros
Copy link

ivanjaros commented Nov 9, 2024

I am, unable to produce proper gifs, there are always artifacts or loss of visual quality, even if gif is encoded without errors. I saw examples here and am processing individual frames but the result is always garbage. Changing filters did not help either. Any idea why?

                        g, err := gif.DecodeAll(src)
			if err != nil {
				return err
			}

			if len(g.Image) == 0 {
				_, err := io.Copy(dst, src)
				return err
			}

			g.Config.Width = 0
			g.Config.Height = 0

			for k := range g.Image {
				for j := range rules {
					if result, ok := rules[j].Apply(g.Image[k], filter).(*image.NRGBA); ok {
						g.Image[k] = &image.Paletted{
							Pix:     result.Pix,
							Stride:  result.Stride,
							Rect:    result.Rect,
							Palette: g.Image[k].Palette,
						}
					}
				}
				if g.Image[k].Bounds().Max.X > g.Config.Width || g.Image[k].Bounds().Max.Y > g.Config.Height {
					g.Config.Width = g.Image[k].Bounds().Max.X
					g.Config.Height = g.Image[k].Bounds().Max.Y
				}
			}

			return gif.EncodeAll(dst, g)

I have just one resize rule for testing(called by the Apply).

The source is random gif from the internet with 800x600 dimensions and I am testing resizing to half.

Source:
source

Result(also 3x in file size)
dst

@ivanjaros
Copy link
Author

Also, it seems that if the gif is larger(not sure when this triggers), i will get

panic: lzw: input byte too large for the litWidth

This is caused by something off with colors in the pallete after transformation, triggered in gif.encoder.writeImageBlock.

obrázok

obrázok

@ivanjaros
Copy link
Author

This produces something:

        g, err := gif.DecodeAll(src)
	if err != nil {
		return err
	}

	g.Config.Height = 0
	g.Config.Width = 0

	for k := range g.Image {
		src := image.Image(g.Image[k])

		for j := range rules {
			src = rules[j].Apply(src, filter)
		}

		tmp := image.NewPaletted(src.Bounds(), g.Image[k].Palette)
		draw.Draw(tmp, src.Bounds(), src, image.Point{}, draw.Src)
		g.Image[k] = tmp

		// find the largest image in the set(should be the first one)
		if tmp.Bounds().Max.X > g.Config.Width || tmp.Bounds().Max.Y > g.Config.Height {
			g.Config.Width = tmp.Bounds().Max.X
			g.Config.Height = tmp.Bounds().Max.Y
		}
	}

	return gif.EncodeAll(dst, g)

But it is inconsistent. The following are three random gifs from the internet. The first one is most problematic. It's like there are two gifs within one(it's weird and I cannot make it work properly with this one), second one is losing color information and produces artifacts. Third one is totally fine.

dst

dst2

dst3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant