{"id":3616,"date":"2016-12-04T02:23:42","date_gmt":"2016-12-04T02:23:42","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=3616"},"modified":"2019-04-25T13:05:18","modified_gmt":"2019-04-25T13:05:18","slug":"codejava-argb_8888-pixel-abstraction","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/codejava-argb_8888-pixel-abstraction\/","title":{"rendered":"[CODE\/JAVA] ARGB_8888 Pixel Abstraction"},"content":{"rendered":"<p>This is one of the ways you can decode Pixel colors out of the integers you get from Android Pixels.<\/p>\n<p>ARGB_8888, stands for Alpha, Reg, Green, Blue. The 8&#8217;s stand for the number of bits per channel.<\/p>\n<p>In Android, signed <code>int<\/code>&#8216;s are used to represent pixel&#8217;s alpha\/color information.<br \/>\nSince Android&#8217;s language of choice is java, these ints are 32-bit integers, each <code>int<\/code> takes 4 bytes.<\/p>\n<p>4 bytes = 32 bits = 8bits + 8bits + 8bits + 8bits.<\/p>\n<p>If you had an <code>int<\/code> like <code>0xFFAABBCC<\/code>, each pair of letters on that hexadecimal would mean the following from left to right<\/p>\n<p><code>{alpha=0xFF}{red=0xAA){green=0xBB}{blue=0xCC}<\/code><\/p>\n<p>If you&#8217;ve done web programming and played with rgb colors and you didn&#8217;t know about this, now it all should click on how your web browser represents colors, except in HTML you don&#8217;t deal with the alpha value in the front. On Android&#8217;s XML you do.<\/p>\n<p>In bits (binary), the <code>0xFFAABBCC<\/code> value would look like this:<\/p>\n<pre><code>alpha red green blue\n0xFF 0xAA 0xBB 0xCC\n255 170 187 204\n{0b11111111}{0b10101010}{0b10111011}{0b11001100}\n<\/code><\/pre>\n<p>If you wanted to look at the entire number in binary\/bits, it&#8217;d be something like this (leaving spaces for visual help):<\/p>\n<pre><code>0b11111111 10101010 10111011 11001100 == 0xFFAABBCC == 4289379276\n<\/code><\/pre>\n<p>So if you wanted to get the <em>red<\/em> channel (<code>0xAA = 170 = 0b10101010<\/code>) , you&#8217;d have to move the whole thing towards the right 16 places, and then compare it with only the rightmost 8 bits.<\/p>\n<p>So, we shift 16 places to the right, we&#8217;d get rid of the 2 bytes on the right side and end up only with the left half<\/p>\n<pre><code>0b11111111 10101010\n<\/code><\/pre>\n<p>Since we only care about those 8 bits on the right, we do an &#8220;&amp;&#8221; (bitwise &#8220;and&#8221;) against <code>0xFF=255=0b111111111<\/code> (all 8 rightmost bits set to 1)<\/p>\n<pre><code>0b11111111 10101010 &amp;\n0b00000000 11111111\n====================\n0b00000000 10101010\n<\/code><\/pre>\n<p>So with simple right bit shifting and &#8220;&amp; 0xff&#8221; of the shifted value we can extract the values per channel.<\/p>\n<p>This class also features a &#8220;multiplyByFloat()&#8221; function, which I was using to multiply to each channel as I was operating with convolution kernels while playing with image filters.<\/p>\n<p>[pastacode lang=&#8221;java&#8221; manual=&#8221;%2F**%20ARGB_8888%20Pixel%20abstraction%20*%2F%0Apublic%20static%20class%20PixelARGB_8888%20%7B%0A%20public%20final%20byte%20a%3B%0A%20public%20final%20byte%20r%3B%0A%20public%20final%20byte%20g%3B%0A%20public%20final%20byte%20b%3B%0A%20public%20final%20int%20intVal%3B%0A%0A%20public%20PixelARGB_8888(final%20int%20argb32bitInt)%20%7B%0A%20%20a%20%3D%20(byte)((argb32bitInt%20%3E%3E%2024)%20%26%200xff)%3B%0A%20%20r%20%3D%20(byte)((argb32bitInt%20%3E%3E%2016)%20%26%200xff)%3B%0A%20%20g%20%3D%20(byte)((argb32bitInt%20%3E%3E%208)%20%26%200xff)%3B%0A%20%20b%20%3D%20(byte)(argb32bitInt%20%26%200xff)%3B%0A%20%20intVal%20%3D%20argb32bitInt%3B%0A%20%7D%0A%0A%20public%20PixelARGB_8888(byte%20a%2C%20byte%20r%2C%20byte%20g%2C%20byte%20b)%20%7B%0A%20%20this.a%20%3D%20a%3B%0A%20%20this.r%20%3D%20r%3B%0A%20%20this.g%20%3D%20g%3B%0A%20%20this.b%20%3D%20b%3B%0A%20%20intVal%20%3D%20(a%20%3C%3C%2024)%20%2B%20(r%20%3C%3C%2016)%20%2B%20(g%20%3C%3C%208)%20%2B%20b%3B%0A%20%7D%0A%0A%20public%20static%20int%20multiplyByFloat(float%20factor%2C%20int%20arg32bitInt)%20%7B%0A%20%20return%20multiplyByFloat(factor%2C%20arg32bitInt%2C%20false)%3B%0A%20%7D%0A%0A%20public%20static%20int%20multiplyByFloat(float%20factor%2C%20int%20argb32bitInt%2C%20boolean%20multiplyAlphaChannel)%20%7B%0A%20%20PixelARGB_8888%20original%20%3D%20new%20PixelARGB_8888(argb32bitInt)%3B%0A%20%20byte%20alpha%20%3D%20original.a%3B%0A%20%20if%20(multiplyAlphaChannel)%20%7B%0A%20%20%20alpha%20%3D%20(byte)(original.a%20*%20factor)%3B%0A%20%20%7D%0A%20%20PixelARGB_8888%20multiplied%20%3D%20new%20PixelARGB_8888(alpha%2C%20(byte)(factor%20*%20original.r)%2C%20(byte)(factor%20*%20original.g)%2C%20(byte)(factor%20*%20original.b))%3B%0A%20%20return%20multiplied.intVal%3B%0A%20%7D%0A%7D&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is one of the ways you can decode Pixel colors out of the integers you get from Android Pixels. ARGB_8888, stands for Alpha, Reg, Green, Blue. The 8&#8217;s stand for the number of bits per channel. In Android, signed int&#8216;s are used to represent pixel&#8217;s alpha\/color information. Since Android&#8217;s language of choice is java, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[1549,1437,1550,1548,583,1272],"class_list":["post-3616","post","type-post","status-publish","format-standard","hentry","category-code","tag-argb","tag-code","tag-filers","tag-image-processing","tag-java","tag-pixel"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-Wk","jetpack-related-posts":[{"id":2887,"url":"https:\/\/www.gubatron.com\/blog\/android-changing-textview-alpha-transparency-across-different-target-sdks\/","url_meta":{"origin":3616,"position":0},"title":"Android: Changing TextView alpha transparency across different target SDKs","author":"gubatron","date":"November 30, 2012","format":false,"excerpt":"Sometimes you may need to make a TextView (label) look a little transparent to make emphasis on other parts of your UI. The .setAlpha() function on TextView is not supported after later in the SDK. Here's a static workaround you can place on some sort of UIUtils class you may\u2026","rel":"","context":"In \"Android\"","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/tag\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2138,"url":"https:\/\/www.gubatron.com\/blog\/java-how-to-create-dynamic-pngs-jpgs-gifs\/","url_meta":{"origin":3616,"position":1},"title":"Java: How to create dynamic PNGs, JPGs, GIFs.","author":"gubatron","date":"February 14, 2010","format":false,"excerpt":"Sometimes you need to create graphics, or compose images and have them saved as regular PNGs, JPEGs or GIFs. Here's a quick and dirty reference of how to do it with BufferedImages, Graphics2D and javax.imageio.*. Very straightforward. [java] import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO;\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1269,"url":"https:\/\/www.gubatron.com\/blog\/how-to-make-a-quick-dirty-hexviewer-updated\/","url_meta":{"origin":3616,"position":2},"title":"How to make a Quick &#038; Dirty HexViewer &#8211; Updated","author":"gubatron","date":"April 20, 2009","format":false,"excerpt":"After I received comments from ispak on Flickr I made a few fixes. ispak pointed out that it was a bad idea reading one byte at the time, also I had a gay ass try\/catch that didn't catch any exception :p So now I read 16 byte chunks, and I\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"HexViewer - r2","src":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1928,"url":"https:\/\/www.gubatron.com\/blog\/droid-vs-nexus-1-who-can-calculate-md5-faster\/","url_meta":{"origin":3616,"position":3},"title":"Droid vs Nexus 1: Who can calculate MD5 faster?","author":"gubatron","date":"May 21, 2010","format":false,"excerpt":"Nexus 1 indeed. 17 files get their MD5 calculated on the Droid and 17 files get their MD5 hash calculated on the Nexus 1 Nexus 1 pwns. Here's the code in case you're curious. [java] public void onClick(View v) { _logTextView.setText(\"MD5 Benchmark on \" + Build.DEVICE + \"nn\"); if (GlobalVariables.APP_CONTEXT\u2026","rel":"","context":"In &quot;Android&quot;","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2142,"url":"https:\/\/www.gubatron.com\/blog\/how-to-convert-android-gps-coordinates-into-xy-coordinates\/","url_meta":{"origin":3616,"position":4},"title":"How to convert Android GPS coordinates into X,Y coordinates.","author":"gubatron","date":"December 30, 2010","format":false,"excerpt":"Without further math bullshit about all the conversion systems, when you have a bunch of Android GPS coordinates (which are compatible with Google Earth and Google Maps), and you want to draw them on a finite 2D plane, here's what worked for me. [java] int x = (int) ((PLANE_WIDTH\/360.0) *\u2026","rel":"","context":"In &quot;Android&quot;","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2047,"url":"https:\/\/www.gubatron.com\/blog\/map-function-in-java\/","url_meta":{"origin":3616,"position":5},"title":"Map function in Java","author":"gubatron","date":"August 31, 2010","format":false,"excerpt":"I read on some email signature something along the lines of: \"If I had a dollar for every for(int i=0; i < size; i++) { ... } I've written I'd be rich\" After coding on Android and learning about some of the tips for performance, like \"With an ArrayList, a\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=3616"}],"version-history":[{"count":11,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3616\/revisions"}],"predecessor-version":[{"id":3781,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3616\/revisions\/3781"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=3616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=3616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=3616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}