- Home /
A simple c# jpeg encoder? (that doesn't use System.Drawing)
I am looking for a simple c# jpeg encoder that takes Color / Color32 array as input and returns jpeg byte.I once could find such snippet, but all I can find now is dependent on some framework.
I am trying to solve the problem with built-in ImageConversion class only working on the main thread
Hey - I know you specifically said you didn't want this (hence it not being an answer). However if you're ending up looking like you have to use some other 3rd party library (which I nearly did), it turned out a lot easier for me to make a $$anonymous$$imal c# project that does use System.Drawing and plonk that in my Unity project.
ps - just saw your other question about perf. I run the above library on a seperate thread, so it doesn't block the render thread. However I still see some hit when copying the pixels into a texture, which I believe is still a tricky one to solve with Unity unless they've added some new set pixel api since I last looked. This is especially high if you ask it to generate mip maps as there's a load of extra work to do.
Yes, reading the pixels will always slow it down a bit. Perhaps some native GPU texture memory access would be possible here, but for my application this is not a problem
Answer by Bunny83 · Sep 05, 2019 at 11:31 AM
Well JPEG compression is not that simple therefore your requirement for a simple encoder is hard to fulfill ^^. To get a bit of a clue on the complexity I recommend watching the computerphile series on JPEG compression which is actually pretty good. Though the most difficult part (how to actually compute the discrete cosine tranform) is not really explained ^^.
For a possible solution, see this codeproject. Keep in mind that this is distributed under the CodeProject Open License so make sure you read it. Though the usage of the code shouldn't be an issue,
I would actually assume that if you want to implement such an encoder yourself, PNG might be simpler ^^. As explained in the computerphile videos the actual JPEG standard isn't really supported by any common library. We generally use the JFIF standard which is a much simpler subset but still quite complex-
By 'simple' i meant - a simple class that does all the job without external dependencies. I once had such code from somewhere, but I can't find it anywhere now.
Anyway, I wonder why unity's ImageConversion doesn't support threaded workflow (Eg. with a Color32[] array)
I don't really care much for the final quality, or the format (PNG works as well). All I want is to save a screenshot with the savegame file. The gameplay data gather is fast, screenshot is fast, encoding and writing to the file is threaded, the only blocking and slow thing is the image encoding. And since it's a small screenshot that gets blurred and stretched over the screen during the loading, the quality / artifacts are not such an issue, compared to the blocking during the savegame. Anyway thanks for the answer, I will try to look for some PNG encoder
Hmm, if the actual image is small it shouldn't really block that long. What resolution does you image have? Is it a power of 2 or at least divisible by 8? If it's really just a small image you may just want to store it uncompressed? If you need a way to downscale / crop / letterbox the image I've written a simple image resampler. Since it's just a small image, maybe a grayscale or indexed color image is enough? Does it need to be an image format that is supported outside your game / application?
Your answer
Follow this Question
Related Questions
Which character encoding/file format should I use in a shader to avoid compilation issues? 0 Answers
EncodetoPNG and all other file types leaves the image in the wrong color space 0 Answers
How to import a jpg to animate with motion and sound effects 0 Answers
iOS Audio best practice 0 Answers
Generator function visibility? 1 Answer