kunzmi / managedCuda

ManagedCUDA aims an easy integration of NVidia's CUDA in .net applications written in C#, Visual Basic or any other .net language.
Other
440 stars 79 forks source link

Jpeg decode error in example JpegNPP.cs #71

Open gsw88 opened 5 years ago

gsw88 commented 5 years ago

in ManagedCudaSamples/JpegNPPCompression/Jpeg.npp line:726

jpeg is decoded incorrectly and results in a poor quality image.

Data copied to device memory differs from c++ npp example as seen in the cuda toolkit version 10.0 examples. The c++ example passes new array populated with values obtained from looking up the values of aQuantizationTables[x] in the look up table, Npp8u aZigZag.

replacing JpegNPP.cs lines 726 to 732 with the following results in a better jpeg decompression

        //from jpegNPP.cpp in cuda samples from the cuda toolkit version 10.0
        byte[] aZigzag = {
        0,  1,  5,  6, 14, 15, 27, 28,
        2,  4,  7, 13, 16, 26, 29, 42,
        3,  8, 12, 17, 25, 30, 41, 43,
        9, 11, 18, 24, 31, 40, 44, 53,
        10, 19, 23, 32, 39, 45, 52, 54,
        20, 22, 33, 38, 46, 51, 55, 60,
        21, 34, 37, 47, 50, 56, 59, 61,
        35, 36, 48, 49, 57, 58, 62, 63
        };

        // Copy DCT coefficients and Quantization Tables from host to device
        for (int i = 0; i < 4; ++i)
        {
            byte[] temp = new byte[64];
            for (int k = 0; k < 32; ++k)
            {
                temp[2 * k + 0] = aQuantizationTables[i].aTable[aZigzag[k + 0]];
                temp[2 * k + 1] = aQuantizationTables[i].aTable[aZigzag[k + 32]];
            }

            //the cpp version of this has this zig zag step before copying
            pdQuantizationTables[i].CopyToDevice(temp);
        }

This change is necessary to get an accurate image for me after I rebuilt NPP.dll and ManagedCuda.dll for x64 with cuda toolkit 10.0 installed. I never installed cuda toolkit 8.0, which appears to be required by the dlls included with the repo in Samples\lib so I don't know if this is also necessary for that version.

Also, the method NPPImage_8uC1::Resize no longer exists in 8.0.22.0 replacing it with NPPImage_8uC1::SqrPixelAdvanced allows the code to compile with the updated assembly reference

kunzmi commented 5 years ago

I haven't updated the samples for a while now and given that NPP had a few changes till the current version, it might be useful to update these samples. Given that you already made these, would you mind to create a pull request? Best, Michael

digi-chris commented 5 years ago

@gsw88 , I've been trying to get the JpegNPPCompression sample to work as well. I've rebuilt NPP.dll and ManagedCuda.dll, however when I run the program I get the following error:

ErrorNotInitialized: The CUDA driver API is not yet initialized. Call cuInit(Flags) before any other driver API calls.

If I add the following just before calling the Jpeg save/load functions:

var result = ManagedCuda.DriverAPINativeMethods.cuInit(0);

The result is 'ErrorUnknown' and I still get the same error.

Any ideas what could cause this? Did you have a similar problem?

digi-chris commented 5 years ago

So, it turns out I was being stupid - had the CUDA toolkit installed, but not the right drivers for my GPU!

Apologies for posting in with an unrelated issue.