I'm trying get LibJPEG-turbo's executable cjpeg.exe (http://www.libjpeg-turbo.org/ with Windows official binaries) to work inside a DLL I'm creating to handle batch processing of large images. I'm using Visual Studio 2010 and .NET 4. The reason for using this vs .NETs jpeg encoder/decoder is vastly greater quality and speed is essential, and other libraries such as BitMiracles .NET JPEG library is tremendously slow even after I added some unsafe code to speed up sections of the library, such as reading in the bitmap data.
My first attempt at using cjpeg.exe was to use the Process class stream redirection:
ProcessStartInfo pi = new ProcessStartInfo();
pi.FileName = @"C:\Users\spl3001\Documents\Visual Studio 2010\Projects\TurboJpeg\TurboJpeg\cjpeg.exe";
pi.RedirectStandardOutput = true;
pi.RedirectStandardInput = false;
pi.RedirectStandardError = true;
pi.UseShellExecute = false;
pi.CreateNoWindow = true;
pi.Arguments = " -quality " + quality.ToString() + " \"" + inFile + "\"";
And then using this code to write the data out to a file:
Process proc = new Process();
proc.StartInfo = pi;
proc.Start();
string data = proc.StandardOutput.ReadToEnd();
StreamWriter sw = new StreamWriter(outFile);
sw.Write(data);
sw.Close();
proc.WaitForExit();
What I get is a JPEG "file", with some jpeg data inside, but the header isn't quite correct (both from a hex editor ASCII view):
ÿØÿà ..JFIF..
instead of
ÿØÿà..JFIF......
which is what is created when cjpeg.exe is run on the sample data I've created via normal shell use (typing it in cmd shell). The first incorrect file won't load in any viewer software, while the second one works perfectly.
I think it has to do with the StandardOutput stream being read out as a string and some UTF-8 treatment of the data when writing it out as a file. Also, the output file created by the code at the top is larger than if done by hand.
My next attempt was to turn off all the redirection and have in the argument the shell standard output redirection operator " >outputfile" as is the recommended use of cjpeg.exe. This just throws up an error saying that cjpeg is receiving too many input files.
However, if I copy the the argument string out EXACTLY as it is run inside the program and run it by hand, it works correctly.
Any suggestions would be greatly appreciated.