Hello Guest

Author Topic: Loading PNGs from byte array  (Read 12172 times)

MindJuiceKen

  • Newbie
  • *
  • Posts: 5
    • View Profile
Loading PNGs from byte array
« on: March 03, 2012, 02:08:46 am »
I have a project where I need to do the following:
  • Load a ZIP file using the WWW class
  • Unzip the PNG files contained in the ZIP into byte arrays (one per image)
  • Create a 2D UI object that is based on that PNG image data

Ideally, I could keep this all in memory rather than saving the PNG out to a file first.  The code might look something like this:

  // Pseudo-code for Zip reading
  byte[] data = myZipFile.sizeInBytes(filenameToUnzip);
  bool result = myZipFile.read(filenameToUnzip, data);
  // Create an object to use with 2D Toolkit (not sure what name your class would be)
  Sprite s = Sprite.create(data);


If there was no other way, it would also be OK to create the image from a PNG file.  In that case, I would unzip the PNGs into byte arrays and save them to the local file system.

  Sprite s = Sprite.loadFromZip("myFile.zip");

Even more ideally, I could call a method like this and it would load an image directly from the ZIP file itself:

  String pathToTheFileToReadFrom = "Folder/SomeFilename.png";
  Sprite s = Sprite.createFromZip("myFile.zip", pathToTheFileToReadFrom);


Is this possible with 2DToolkit now?  In the future?

Thanks!

Ken

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Loading PNGs from byte array
« Reply #1 on: March 03, 2012, 10:08:40 am »
2d toolkit doesn't support this out of the box. The problem here is that while its possible with a few tiny code modifications, these "sprites" will not be atlased.

What are your requirements? If you are loading an atlas, I'd suggest creating an assetbundle of a sprite collection instead of a png, it will Just Work. If you simply need to load a single image, without any concern for atlasing and such, then I could probably post a sample which does this.

MindJuiceKen

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Loading PNGs from byte array
« Reply #2 on: March 04, 2012, 04:56:41 am »
I am creating a separate (non-Unity-based) product that will allow my customers to generate "project" ZIP files that will contain various configuration files and data displays.

The project will be loaded over the network by a Unity-based app that I am working on now.

Many of the data displays are made up of one or more PNG images (e.g., up and down image for button, background for a numeric display).

As far as I understand, Asset Bundles are not an option because they must be built with Unity, and my customers will only have my app, not Unity.

For now, at least, loading images and displaying them is enough.  Performance on an iPad or Tegra-based tablet should be more than enough to handle the extra draw calls for our use case.

In the near future, it would also be possible for me to combine images into an atlas file with a PNG and corresponding data file if that is something that could be handled too.

Thanks!


Ken

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Loading PNGs from byte array
« Reply #3 on: March 04, 2012, 11:37:39 am »
Yes, this is certainly doable. I have added it to the TODO - basically a function to create a sprite collection from a texture. I will have to get the next version out first as a matter of priority, before I will be able to look into this.

MindJuiceKen

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Loading PNGs from byte array
« Reply #4 on: March 05, 2012, 02:52:06 am »
Understood.  Just to be clear, you said "create a sprite collection from a texture".  The "texture" I have in this case is the PNG itself, not decoded into an in-memory texture.

I know the WWW class can return a file as a texture, so the ability must be there under the covers, but I haven't found a method to create a texture from a byte array.

Do you know if that exists?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Loading PNGs from byte array
« Reply #5 on: March 05, 2012, 08:38:48 am »
I don't think there is a way to decode a PNG in memory - once decoded and you have ARGB data in memory, you can create a Texture, write pixels into it, and Apply() and use it like any other normal texture.

marcosramos

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Loading PNGs from byte array
« Reply #6 on: July 17, 2012, 08:57:20 pm »
Yes, this is certainly doable. I have added it to the TODO - basically a function to create a sprite collection from a texture. I will have to get the next version out first as a matter of priority, before I will be able to look into this.

What is the status of this feature? I really need to load an image from unity WWW class into a tk2dSprite object.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Loading PNGs from byte array
« Reply #7 on: July 17, 2012, 09:23:53 pm »
You can load it as shown in the runtime sprite collection example. I've linked it here: http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,340.0.html

Its in the private support forum - you'll need to register your copy to get access.


marcosramos

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Loading PNGs from byte array
« Reply #8 on: July 17, 2012, 10:46:21 pm »
Thank you unikron! It works like a charm!  ;D