Wednesday, September 18, 2013

Color Pixels

BY Maya Pratiwi No comments

Dari banyak sumber yang ngejelasin tentang cara ngambil informasi nilai warna tiap pixel dari sebuah citra gambar menggunakan java, shampir semuanya kayak gini

Image Pixels are Arrays of Integers [32 bits/4Bytes]
         * Consider a 32 pixel as 11111111-00110011-00111110-00011110
         * First Byte From Left [11111111]= Alpha
         * Second Byte From Left[00110011]= Red
         * Third Byte From Left [00111110]= Green
         * Fourth Byte From Left[00011110]= Blue
         * The following method will do a proper bit shift and
         * logical AND operation to extract the correct values
         * of different color/alpha components.
         */
        public String getPixelARGBData() {
                String pixelARGBData = "";
                /**
                 * Shift all pixels 24 bits to the right.
                 * Do a logical and with 0x000000FF
                 * i.e. 0000 0000 0000 0000 0000 0000 1111 1111
                 * You will get the alpha value for the pixel
                 */
                int alpha = getAlpha();
                /**
                 * Shift all pixels 16 bits to the right.
                 * Do a logical and with 0x000000FF
                 * i.e. 0000 0000 0000 0000 0000 0000 1111 1111
                 * You will get the red value for the pixel
                 */
                int red = getRed();
                /**
                 * Shift all pixels 8 bits to the right.
                 * Do a logical and with 0x000000FF
                 * i.e. 0000 0000 0000 0000 0000 0000 1111 1111
                 * You will get the green value for the pixel
                 */
                int green = getGreen();
                /**
                 * Dont do any shift.
                 * Do a logical and with 0x000000FF
                 * i.e. 0000 0000 0000 0000 0000 0000 1111 1111
                 * You will get the blue value for the pixel
                 */
                int blue = getBlue();
                pixelARGBData = "Alpha: " + alpha + ", " + "Red: " + red + ", "
                + "Green: " + green + ", " + "Blue: " + blue;
                return pixelARGBData;
        }

Yang aku heran, kenapa harus di shift ke kanan dan di AND pake 0x000000FF -__-

Ternyata, 
Komponen warna tiap pixel itu terdiri dari ARGB (Alpha, Red, Green, Blue). Yang masing-masing itu besarnya 8 bit. seperti berikut :
Consider a 32 pixel as 11111111-00110011-00111110-00011110
         *
         * First Byte From Left [11111111]= Alpha
         * Second Byte From Left[00110011]= Red
         * Third Byte From Left [00111110]= Green
         * Fourth Byte From Left[00011110]= Blue

Lalu, kenapa harus di shift ke kanan? (istilah lainnya di geser ke kanan). 
gunanya adalah untuk meng-ekstrak nilai masing-masing komponen. kalo misal 32 bit itu langsung di ubah ke desimal per 8 bit, maka sulit mengenali berapa alphanya, red-nya, green-nya dan blue-nya. Tujuan di geser sampe n bit itu agar bisa di AND-kan dengan FFFF yang nilainya sama dengan 1111 1111. berapapun yang di AND dengan 1 hasilnya ya bilangan itu sendiri.

contoh
misal kita punya warna 11111111-00110011-00111110-00011110
Dan kita pengen tau berapa sih alphanya. caranya adalah
  1. Geser sejauh 24 bit untuk mendapat alpha
  2. AND-kan dengan 000000ff
000000ff  = 0000 0000 0000 0000 0000 0000 1111 1111
alpha        =                                                       1111 1111  0011 0011 0011 1110.....
hasil         =                                                       1111 1111

Nah kalo udah di And kan jadi bisa tau, ternyata alpha-nya bernilai 255 :)

0 comments:

Post a Comment

Hai, terima kasih sudah membaca.
Silakan tinggalkan komentar kamu disini.

Jangan lupa centang "Notify Me" yaa agar kamu bisa menerima balasan dari saya

Terima kasih :)