Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Simon Says · Oct 16, 2014 at 11:06 PM · jpgimage loader

Know a run-time JPEG decoder?

Hi,

Does anyone know of a decent run-time JPEG decoder that would work out of the box in Unity? On mobile devices. Doesn't have to be super fast; I'd use it when loading a scene.

What I need essentially is something that would make a byte[] -> Color32[] conversion without extra memory allocations, as I need to merge the Color32 array into another array again. I know about

 Texture2D.LoadImage()

and its sister

 WWW.LoadImageIntoTexture()

but I'd really like to avoid copying the buffer twice. (As I imagine the Texture2D.GetPixels32() only returns copied data.) The image is relatively large.

Surely there must be a library or a bunch of scripts somewhere. Although I can't find anything...

Comment
Add comment · Show 5
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 KpjComp · Oct 16, 2014 at 11:45 PM 0
Share

GetPixels allows you to select a portion of the texture to read, so if the image is larger you just split into sections, you can then use SetPixels to put them back.

http://docs.unity3d.com/ScriptReference/Texture2D.GetPixels.html

avatar image Simon Says · Oct 17, 2014 at 10:00 AM 0
Share

@$$anonymous$$pjComp Yeah, I know about it. It's similar to GetPixels32() only less efficient. One problem is it works with floats, so: 4x bigger buffers, probably a conversion needed between my code and internal Texture2D buffers (I can't imagine Unity would store raster colour data in floats).

Another problem is it can only read uncompressed RGB(A) data (and DXT I think). *32 version can also manage PVR (tried) and probably other formats. The reason I'm talking about this is I need to store the source images somewhere on disc in some form and having it as uncompressed RGB data is unacceptable. Hence run-time loaded JPEGs or PNGs look like the best way to go. Hence I don't need a Texture2D at all until I finally write into one; I need a decoder.

avatar image KpjComp · Oct 17, 2014 at 02:30 PM 1
Share

Oh, right I see yes,.. That now makes sense. With you saying PNG (lossless), I wonder if you could get about the same compression on DXT files by just using a LZF Decompresser like this one -> http://forum.unity3d.com/threads/lzf-compression-and-decompression-for-unity.152579/ as I think that's what PNG's are using anyway.

avatar image Simon Says · Oct 17, 2014 at 02:44 PM 0
Share

@$$anonymous$$pjComp I think I could, yes. It's certainly an option. I zipped a Targa (raw RGB data) and it came out only moderately worse than PNG (for 3 $$anonymous$$B RGB the PNG was 1.7 $$anonymous$$B and ZIP 2.15 $$anonymous$$B). Still, JPEG comes out better for my data with mere 1 $$anonymous$$B. Thank you for the link. I'll have a look at it if the JPEG decoder doesn't work out.

avatar image Simon Says · Oct 18, 2014 at 01:32 PM 0
Share

@$$anonymous$$pjComp Unfortunately, the LZF compressor does almost nothing for my data. Savings in size are negligible, so not worth a bother. (Still a great script though.) Looks like I'll have to roll with JPEGs and Texture2D.LoadImage() after all.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Oct 16, 2014 at 11:47 PM

I've just found this decoder on google code. It looks like it'a a whole JPEG framework. It also has an encoder. Haven't tested it yet if it runs in Unity, but it looks like it's pure managed code, but i might be wrong.

Comment
Add comment · Show 4 · 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 Simon Says · Oct 17, 2014 at 10:00 AM 0
Share

Thank you, I'll check it out.

avatar image Simon Says · Oct 17, 2014 at 10:04 AM 0
Share

Hah, FJCore. I've seen a post somewhere on forums or answers where a guy gave it a try. It barfed on iOS. Generated IL as I recall. But maybe it's old news and they changed it...

avatar image Simon Says · Oct 17, 2014 at 02:13 PM 0
Share

Hmm, still true. Quantization is emitted. But it's a short section of code; I'll see if I can rewrite it. Otherwise looks very promising. Got to do it proper decoding on PC.

avatar image Simon Says · Oct 18, 2014 at 11:27 AM 0
Share

I've been able to get it to work on iOS in Unity. But I also run memory profiling and results aren't pretty. The decoder allocated extra ~25 $$anonymous$$B on mono heap for a 1 $$anonymous$$B JPG picture (decoded data excluded) that didn't even get reclaimed after GC.Collect(). (Even if it were, the damage is done.)

This way it's just better to use a custom Texture2D. I just don't see the point to allocate the decoded data twice -- once on Unity heap and then on mono heap -- if I'm going to process and discard it.

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

30 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

Related Questions

How can I prevent Unity from inflating the size of my images? 1 Answer

Getting red question mark for texture animated jpg 2 Answers

Save file from Resources folder to local directory 1 Answer

Is it possible to post images from URLs into the game environment? 1 Answer

some JPG images not loading properly with WWW 0 Answers


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