Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Pangamini · Sep 05, 2019 at 10:37 AM · encodingjpg

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hrgchris · Sep 05, 2019 at 02:04 PM 0
Share

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.

avatar image hrgchris hrgchris · Sep 05, 2019 at 02:10 PM 0
Share

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.

avatar image Pangamini hrgchris · Sep 05, 2019 at 02:12 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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,

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Sep 05, 2019 at 11:36 AM 0
Share

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-

avatar image Pangamini · Sep 05, 2019 at 11:38 AM 0
Share

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)

avatar image Pangamini Pangamini · Sep 05, 2019 at 11:44 AM 0
Share

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

avatar image Bunny83 Pangamini · Sep 05, 2019 at 01:40 PM 0
Share

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?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

110 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges