nicastel / renderscript_texture_compressor

RenderscriptTextureCompressor
Apache License 2.0
4 stars 6 forks source link

Handle Alpha band #2

Open nicastel opened 10 years ago

nicastel commented 10 years ago

Handle alpha band via "ETC1(RGB)+ETC1(ALPHA)" as adobe did in ATF : http://wiki.starling-framework.org/manual/atf_textures#creating_an_atf_texture

nicastel commented 10 years ago

Some ideas about how to handle it after compression : http://malideveloper.arm.com/develop-for-mali/sample-code/etcv1-texture-compression-and-alpha-channels/

METHOD 1&2 are basically the same from a file point of view (2 series of bit compressed with ETC1) so my priority is to support this one.

frmz commented 9 years ago

It would be nice to have a quick way to understand if this is needed starting from a Bitmap, i mean, given an Image its not very easy to understand if that Bitmap has an used alpha channel and thus require alpha or if the alpha channel is not used.

ndorigatti commented 9 years ago

hello @frmz , as far as I know there is no fast and easy way to understand if the alpha channel of an image is not used. In android bitmaps can have different configurations, so if the bitmap is for example RGB_565 for sure there is no alpha. But if the bitmap is ARGB_8888 the only way you have is to check pixel by pixel... there are some tricks, but maybe only for PNGs or with specific code... http://stackoverflow.com/questions/12080083/check-transparency-existence-load-only-rgb-values

frmz commented 9 years ago

@ndorigatti suprisingly i have found that Bitmap.hasAlpha() method actually works, probably it keeps track of this natively while Bitmap is in use (off course you need to have an ARGB_8888 bitmap fully loaded first, so, memory wise, might not be good for everyone), i am going to try to use ETC1 only in that case and create it on the fly using this lib, hope it will work, i am really surprised that this repo didnt get much attention, i think using RS is really a veryvery good idea

ndorigatti commented 9 years ago

@frmz does it works really? Does it tell you if there is at least one pixel with alpha? This is good (and hopefully working for every android version). Yes, RS is really fast at doing this and since now minsdk is 14-15 (does somebody really care about gingerbread?! :D) i think it should be used for this task!

frmz commented 9 years ago

@ndorigatti yeah, well, on my tests it works, my min sdk is 19 with GLES 3 so i would really love to see this ported to ETC2 also since most of my users have it, i saw that there is a bug request for this on the Android bugtracker hope it will be solved soon.

ndorigatti commented 9 years ago

I don't know about gls3 but I'm not sure are widely supported. I know etc2 supports alpha in a single file with data, but still didn't work with that, could you try do achieve this?

Sent from my Nexus 4 Il 17/nov/2014 13:38 "Eric" notifications@github.com ha scritto:

@ndorigatti https://github.com/ndorigatti yeah, well, on my tests it works, my min sdk is 19 with GLES 3 so i would really love to see this ported to ETC2 also since most of my users have it, i saw that there is a bug request for this on the Android bugtracker hope it will be solved soon.

— Reply to this email directly or view it on GitHub https://github.com/nicastel/renderscript_texture_compressor/issues/2#issuecomment-63298731 .

frmz commented 9 years ago

@ndorigatti nope i have no competencies for that, i was hoping on a port from @nicastel :)

nicastel commented 9 years ago

Hello all, ETC2 is an extension of ETC1 wich means that an ETC1 texture is a vaild ETC2 texture. ETC2 add specific block mode to handle the case where ETC1 compression is not working well (bad quality).

Supporting ETC2 with good texture quality and performance can be tricky because you have to guess if you need to use a special ETC2 block mode or the default one (wich means the ETC1 mode).

Supporting only the alpha capabilities of ETC2 is a lot easier, I can implements this functionnality in the first time (same quality as ETC1 but with alpha support) if it suits your need but remember that it will only works if you have a device with Android 4.3 min and a GLES 3 compatible GPU.

frmz commented 9 years ago

@nicastel that would be great!! i think what would be even more awesome would be turning this project into a library that can be used instead of the ETC1Util provided by android with direct helper methods to convert an ARGB8888 or 565 bitmap into an ETC2/1 texture or a JPG/PNG file into a PKM one.

there is nothing around to do this runtime within an app and i think it would really help people a LOT.

frmz commented 9 years ago

BTW any way to convert an ARGB8888 image instead of using a 565?

nicastel commented 9 years ago

I am pleased to know that my library is useful to someone.

I have one more question. Which mode does best suit your need ? : 1) 4 bit/pixel : alpha handled as punch-trough (transparent or not transparent) 2) 8 bit/pixel : true alpha channel (256 transparency level)

This 2 modes needs two different implementation, mode 2 is more complicated to implement

frmz commented 9 years ago

@nicastel awesome! i think the most useful is the 2 since whe Bitmap.hasAlpha() returns true you dont know if the pixel is fully transparent or not

ndorigatti commented 9 years ago

i have the code for converting from PNG to PKM+alpha pkm and the PNG is full alpha (not 565). Is it useful?

2014-11-17 17:23 GMT+01:00 Eric notifications@github.com:

@nicastel https://github.com/nicastel awesome! i think the most useful is the 2 since whe Bitmap.hasAlpha() returns true you dont know if the pixel is fully transparent or not

— Reply to this email directly or view it on GitHub https://github.com/nicastel/renderscript_texture_compressor/issues/2#issuecomment-63330358 .

frmz commented 9 years ago

@ndorigatti that would be great! how do you load it then?

ndorigatti commented 9 years ago

honestly I can't remember now, I'm out of office. I do remember we do it in pure java code with a simple PNG library but we managed to do it in android with quite the same code. A full png is downloaded and saved as png file or as a ARGB8888 bitmap and then saved as PKM, it is quite slower than the renderscript one but it is working!! we do in java because we put that in a swing application that create and packages data for android worldwind.

2014-11-17 18:25 GMT+01:00 Eric notifications@github.com:

@ndorigatti https://github.com/ndorigatti that would be great! how do you load it then?

— Reply to this email directly or view it on GitHub https://github.com/nicastel/renderscript_texture_compressor/issues/2#issuecomment-63340870 .

nicastel commented 9 years ago

I have added in the renderscript code the support of alpha via the creation of a separate ETC1 alpha texture. The method PKMEncoder.encodeTextureAsETC1_Rs() now return 2 ETC1texture when needed. You will have to blend the 2 texture in the pixel shader.

I have not yet tested extensively the code so please let me know if there is something wrong.

frmz commented 9 years ago

@nicastel what about Texture Atlas as described here: http://malideveloper.arm.com/develop-for-mali/sample-code/etcv1-texture-compression-and-alpha-channels/

Do you think it could be a viable option with that encoder? It would save 1 texture. P.S. I will test the code asap.

nicastel commented 9 years ago

Well I see no problem about supporting the texture atlas method. I just need to concatenate the 2 buffer produced into one that is twice the size. I do not even need to modify the Renderscript code for that, this can be done in Java. Aniway the 2 method are quite similar : you need to merge the rgb and alpha part in the pixel shader.

I also want to add the capability to generate a DDS file with 2 separate texture but only one file via the "texture array" capability of the format. This can be done with the DDS or KTX format but not with the PKM format.

In which kind of application do you plan to use this lib? Do you want specifically to use the PKM format with only one file? As explained in the malidevelopper page both method (texture atlas or separately packed alpha) have their benefits and drawbacks. The best solution really depends from your application so I will implements both of them.

frmz commented 9 years ago

I am trying to implement the atlas option even if both are not really optimal (Atlas as you said can be implemented already with what we have), i am working on a live wallpaper and i generate texture dynamically once in a while so a quick way to get them compressed is really life changing for me