treeform / flippy

Flippy is a simple 2d image and drawing library.
MIT License
59 stars 9 forks source link

STBI Exception #5

Closed Lecale closed 4 years ago

Lecale commented 5 years ago

When trying to use fllippy to minify an image I keep getting an error on

loadImage
stb_image\read.nim(91) load
Error: unhandled exception: can't fopen [STBI Exception]

How did I call? var a = reduceImage("C:\\Nim\\NimExamples\\MyRecipes\\a.png", "C:\\Nim\\NimExamples\\MyRecipes\\b.png", 3) against proc

proc reduceImage*(inFile: string, outFile: string, scale: cint): cint {.cdecl, exportc, dynlib.} =
  ## reduces image
  var image = loadImage(inFile)
  image = image.minify(scale)
  image.save(outFile)
  result = scale

Is it my own stupidity, or is it a real issue? I am on windows as you probably guessed.

treeform commented 5 years ago

I usually get that error from STBI when I supply the wrong path. Are you sure you can open the file?

discard readFile("C:\\Nim\\NimExamples\\MyRecipes\\a.png") work?

Are you sure its a png file? STBI can't open some png files what are odd like monochrome ...

Does it work with png files in the test folder?

EasiestSoft commented 4 years ago

The same exception accurred on Windows 10 (64): Nim Compiler Version 1.0.4 [Windows: i386] MinGW32 from Nim official site

Tested with fillCirle.png in the test folder and made sure the path was correct by: doAssert img.fileExists

May be a link issue:

https://gitlab.com/define-private-public/stb_image-Nim/issues/8

EasiestSoft commented 4 years ago

This issue can be solved by adding cdecl to stb_... procs:

# Internal functions
proc stbi_write_png(
  filename: cstring;
  w, h, comp: cint;
  data: pointer,
  stride_in_bytes: int
): cint
  {.importc: "stbi_write_png",cdecl.}

As jangko said on Nim forum:

depends on your C compiler configuration, most of the times you need to specify the calling convention explicitly, for example: stdcall for msvc or cdecl for gcc

in your case, try add cdecl:

proc addTwoIntegers(a, b: cint): cint {.importc,cdecl.}

treeform commented 4 years ago

I switched to using https://github.com/jangko/nimPNG which has no C code. Does this work for you now?

EasiestSoft commented 4 years ago

The nimPNG based version of flippy works for me and now it supports Unicode file names on Windows(Using MinGW)