TypeLoadException trying to use FJCore (FluxJpeg)

Hey, I’m trying to use the FJCore image library to rescale some images I download before making textures out of them. I am using Monodevelop-Unity to compile it into a library and I am copying the resulting dll into the plugins folder.

My project will compile, but when I actually call methods from this library I am getting the following error:

TypeLoadException: Could not load type 'FluxJpeg.Core.JpegHuffmanTable' from assembly 'FJCore, Version=1.0.4511.30407, Culture=neutral, PublicKeyToken=null'.
TextureLoadTest+<loadFluxJpegSync>c__IteratorB.MoveNext () (at Assets/scripts/TextureLoadTest.cs:179)

From what I read this is usually caused when the library that your library references is updated, but this is not the case here.

I think it might be because JpegHuffmanTable uses Lambda expressions, which might be too new for Unity, but before I start rewriting the entire library for Unity, is there perhaps an easy way to deal with this?

Thanks!

Right, as it often goes, first you ask the question, then you find out about the answer. I decided to try compiling against the source of FJCore (instead of the dll) to see if I got a clearer error message and yes: turns out one of the public methods had a param of a type that wasn’t public (but internal).

So it’s working fine now, Lambda expressions and all. Haven’t gotten it to work on iOS yet, but I’m digging into that part tomorrow.

next day: OK, I dug into the iOS code, turns out it uses the ILGenerator to compile assembly code and this throws an exception like this:

System.ExecutionEngineException: Attempting to JIT compile method (wrapper managed-to-managed) 

Which is caused by this code:

    private QuantizeDel EmitQuantize()
    {
        Type[] args = { typeof(float[]) };

        DynamicMethod quantizeMethod = new DynamicMethod("Quantize",
            null, // no return type
            args); // input array

        ILGenerator il = quantizeMethod.GetILGenerator();

        for (int i = 0; i < quantizationTable.Length; i++)
        {
            float mult = (float)quantizationTable*;*

// Sz Stack:
il.Emit(OpCodes.Ldarg_0); // 1 {arr}
il.Emit(OpCodes.Ldc_I4_S, (short)i); // 3 {arr,i}
il.Emit(OpCodes.Ldarg_0); // 1 {arr,i,arr}
il.Emit(OpCodes.Ldc_I4_S, (short)i); // 3 {arr,i,arr,i}
il.Emit(OpCodes.Ldelem_R4); // 1 {arr,i,arr*}
il.Emit(OpCodes.Ldc_R4, mult); // 5 {arr,i,arr,mult}
il.Emit(OpCodes.Mul); // 1 {arr,i,arr_mult}
il.Emit(OpCodes.Stelem_R4); // 1 {}
}

il.Emit(OpCodes.Ret);
return (QuantizeDel)quantizeMethod.CreateDelegate(typeof(QuantizeDel));
}
Which I suppose makes the inner-most loop of jpeg compression faster by hardcoding a loop to assembly. I replaced it with the following:
private QuantizeDel EmitQuantize()
{
* return SlowQuantize;
}*_

* private void SlowQuantize( float[] arr )*
* {*
* for (int i = 0; i < quantizationTable.Length; i++)*
* {*
arr = quantizationTable;*
* }*
* }*
And now it works! I can now decode and resize jpegs in system memory, in a separate thread, before loading them into a texture. Unfortunately, it is 100x slower then www.texture, but at least no hiccups. I’m really hoping Unity will create an async version of www.texture that allows resizing before committing it to the GPU.