treeform / pixie

Full-featured 2d graphics library for Nim.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unexpected Crash on Windows when using Stb

PrplYoru opened this issue · comments

When trying to read a JPG image, the program crashes unexpectedly, on Windows 11.

This is the example code I tried to run:

import pixie
let image = readImage("test.jpg")

Additional Information

Nim Compiler Version 1.6.4 [Windows: amd64]
Pixie version: 3.1.4
Compiled with nim c -d:pixieUseStb test.nim
When I run the program it suddenly crashes with no debug traceback.
It works fine for reading PNG images.

Do you have the test.jpg?

Sure, here's the image.
test

I tested loading this jpg on Mac and Windows 10 and everything worked fine (Nim 1.6.4, refc and arc).

Do all jpgs fail or just a specific one? What Nim version are you using (nim -v).

I don't have access to Windows 11 and am not at all interested in upgrading so if this is a Windows 11 specific issues, that'll be tough. I doubt it is though.

I tried with more than one JPG file and the program always fails.

Nim Compiler Version 1.6.4 [Windows: amd64]
Compiled at 2022-02-09
Copyright (c) 2006-2021 by Andreas Rumpf

active boot switches: -d:release

Could you try loading the image using this stb_image binding and see if the same thing happens there? https://github.com/define-private-public/stb_image-Nim

I tried it, and stb_image works fine.

import stb_image/read as stbi

var
  width, height, channels: int
  data: seq[uint8]

data = stbi.load("test.jpg", width, height, channels, stbi.Default)

echo "12345"

Ok, that's good to see. One thing though, we do not use the file loading version, we use stbi_load_from_memory since we need to support files or buffers. Maybe that is the difference?

https://github.com/define-private-public/stb_image-Nim/blob/ba5f45286bfa9bed93d8d6b941949cd6218ec888/stb_image/read.nim#L59

Since I can't reproduce the issue I'm not able to try any of this myself.

It is mostly the same call but you'll var data = readFile("test.jpg"). Then pass in data[0].addr and data.len.

In the meantime ill try grabbing the latest source, maybe it has some fix?

This code works fine.

import stb_image/read

var
  width, height, channels: int
  data: seq[byte]
  file = open("test.jpg", fmRead)
  str = file.readAll()

data.setLen(len(str))
copyMem(addr data[0], addr str[0], len(str))

let image = loadFromMemory(data, width, height, channels, Default)
echo "Pixels: ", image.len

I tried the same code, without success, after upgrading to latest Pixie release.

$ nimble uninstall pixie
$ nimble install https://github.com/treeform/pixie@#master
import pixie

let image = readImage("test.jpg")

This is how I am running the program. It still crashes.

$ nim r -d:pixieUseStb .\main.nim

Hm, ok well I wish I could reproduce this to work on it.

If you're up for more debugging, could you try putting maybe echos in or something to see how far it gets into readFile?

For example, we have a very tiny wrapper for JPG using stb_image: https://github.com/treeform/pixie/blob/master/src/pixie/fileformats/stb_image/stb_image.nim

If you put echos in there at each line, what line does it crash on? Maybe try commenting out some stuff and see if you can learn anything to share?

You know what? I was trying to debug the library once again, when I noticed that it just began to work. I tried to find the culprit, with no success. I tried reinstalling the whole library from nimble, and it doesn't crash anymore. I guess we can mark this issue as solved, thank y'all!

Happy to hear things are working now. Much better to have it mysteriously start working than continue to have a mysterious issue I'd say.

Same problem

import std/json
import pixie
import pixie/fileformats/jpg
import utils/similarity

type Path = string

type Config = object
  target: Path
  genefile: Path


const config_file = "./config.json"
var config: Config = parseFile(config_file).to(Config)

proc save() = 
  var file = open(config_file, FileMode.fmWrite)
  file.write((%config).pretty())
  file.close()


echo "aa"
when isMainModule:
  # var target = readImage(config.target)
  # # echo pHash(target, readImage("target/aff0d2cd8ac171e8.png"))
  # # echo histogram(target, readImage("target/aff0d2cd8ac171e8.png"), HistogramType.htGrey)
  # echo pHash(decodeJpg("target/sample_1.jpg"), decodeJpg("target/sample_2.jpg"))
  echo histogram(readImage("target/sample_1.jpg"), readImage("target/sample_2.jpg"), HistogramType.htGrey)

nimble run

aa
C:\Users\{USERNAME}\source\hawk\src\hawk.nim(28) hawk
C:\Users\{USERNAME}\.nimble\pkgs\pixie-4.1.0\pixie.nim(51) readImage
C:\Users\{USERNAME}\.nimble\pkgs\pixie-4.1.0\pixie.nim(26) decodeImage
C:\Users\{USERNAME}\.nimble\pkgs\pixie-4.1.0\pixie\fileformats\jpg.nim(12) decodeJpg
Error: unhandled exception: Decoding JPG requires -d:pixieUseStb [PixieError]

when I run
nimble -d:pixieUseStb run
the program crashes and no output

Hey, we been working on a pure Nim jpeg decoder. By trying head can you see it if works for you?

To get most recent pixie do a git clone:
git clone https://github.com/treeform/pixie
Then install it:
cd pixie
nimble develop

Then try jpeg decoder without -d:pixieUseStb flag.

I tested your "GIMP" image with our new decoder and it works great here.

Thanks!

Hey, we been working on a pure Nim jpeg decoder. By trying head can you see it if works for you?

To get most recent pixie do a git clone: git clone https://github.com/treeform/pixie Then install it: cd pixie nimble develop

Then try jpeg decoder without -d:pixieUseStb flag.

I tested your "GIMP" image with our new decoder and it works great here.

Thanks!

It works.

We have tagged 4.3.0 which includes the new pre Nim decoder. Closing this issue since we no longer use stb_image.

Thanks for confirming it is working @MetaphysicianLeo