1

Without using System.Drawing?

I have a C# application that receives a list of filepaths pointing to a number of .PNG files. I would like to extract an array of pixel (color) data from the image, but I am having trouble determining the best way to extract that.

I had originally hoped to be able to use System.Drawing, as outlined in this answer, but I am unable to access System.Drawing, since my application (Unity3d) uses OpenGL (and therefore, is incompatible with System.Drawing)

The PngBitmapDecoder is likewise out of reach of the application. Is there any other way I can extract the data I'm interested in, short of writing my own PNG Decoder?

Community
  • 1
  • 1
Raven Dreamer
  • 6,940
  • 13
  • 64
  • 101
  • Perhaps [PngCs](https://code.google.com/p/pngcs/) fits the bill? – leonbloy Feb 19 '14 at 13:35
  • *Incompatible with `System.Drawing`* sounds funny. You don't necessary have to *draw* with it, but you still can include it and use (for things like creating/loading bitmap and reading its pixels), no? – Sinatr Feb 19 '14 at 13:45
  • @Sinatr No, I don't have access to the DLL at all. – Raven Dreamer Feb 19 '14 at 13:45
  • Oh surely you [can have](http://answers.unity3d.com/questions/53170/using-drawing-package-like-systemdrawing.html), when you really need to. – Sinatr Feb 19 '14 at 13:47
  • 1
    @Sinatr As far as I know, `dll`s can only be included like that if one has a Unity Pro license. (I do not, or I would have tried that method, as I did discover it in my searching.). – Raven Dreamer Feb 19 '14 at 13:53
  • 1
    Why is [Texture2D.LoadImage](http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.LoadImage.html) not working? –  Feb 19 '14 at 15:23
  • @Jessy I did not know if its existence. I'd repost that as an answer. :) – Raven Dreamer Feb 19 '14 at 18:05

1 Answers1

2

If you are using Unity, then you have access to their API which does exactly what you need.

http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.LoadImage.html http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.GetPixels.html http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.GetPixels32.html

  • I am using `byte[] bytes = System.IO.File.ReadAllBytes(PATH);` in conjunction with LoadImage, but the loaded texture is coming in darker than the .PNG is saved as (comparing RGB values directly). Any clue why that'd be the case? It *is* working otherwise. – Raven Dreamer Feb 22 '14 at 23:20
  • 1
    Sounds like you've got gamma or color space info in your PNG; LoadImage doesn't respect either, as far as I know, so you'll have to bake both. –  Feb 23 '14 at 04:00
  • Any suggestions on how to save the .pngs to remove that? I don't do much work with images, and I have no idea if something like that was added (or how). – Raven Dreamer Feb 23 '14 at 04:17
  • I've only worked with profiles; apparently PNG supports gamma metadata, but I don't know how to work with it. On OS X, you can see the "Color Profile" under the "More Info" disclosure triangle, in the file info panel. If you don't have a profile, then I'm stumped, but if you do, you can just open the file in Photoshop and re-save, unchecking "Embed Color Profile" at the bottom of the save dialog. –  Feb 23 '14 at 15:50
  • 2
    Did some googling, gamma was being set; as you say, loadImage does not do gamma correction, so that **was** the difference. Thanks for your assistance! – Raven Dreamer Feb 23 '14 at 16:49
  • 2
    I didn't. Rather, I *un* baked the source image, removing the gamma bytes from the PNG entirely via [this program](http://entropymine.com/jason/tweakpng/). That made the dynamic load equivalent to the editor load. It's unideal, but at least it confirmed what was actually happening with LoadImage. – Raven Dreamer Feb 23 '14 at 20:41