|
Author |
Topic: diffuse blur from SEB (Read 1596 times) |
|
kensuguro
|
diffuse blur from SEB
« on: Nov 8th, 2004, 6:34am » |
|
not so much a question about how to do a blur, but more about JAVA syntax.. Can someone explain to me what the & does in this case? I understand it's a bit operator, but I don't understand what it's doing here. Code: sum=pixels[left+index] & 255; // left middle pixels[index]=(sum<<16)+(sum<<8)+sum; CODE IS TOTALLY OUT OF CONTEXT |
| I also don't understand what the left shift is doing. sum is shifted left by 16 (bits?). I've been searching the net on bit operations, but I couldn't find any explanations in a graphical context. Sorry if it's one of those repeating questions, I'm totally new with this.
|
|
|
|
fjen
|
Re: diffuse blur from SEB
« Reply #1 on: Nov 8th, 2004, 4:14pm » |
|
small excurse into ARGB. in processing rgb-colors are stored as ARGB, means they are a construct which fits into an int that's made of four values between 0 - 255. Code: 32-bit color - integer AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB single 8-bit integers inside (4x8=32): AAAAAAAA RRRRRRRR GGGGGGGG BBBBBBBB 0-255 0-255 0-255 0-255 00000000 00000000 00000000 00000000 (binary can be anything between 00000000 - 11111111) |
| to change any of the values you'd need to extract them out of the integer-construct, change it, then put it back in. to extract a color you would shift (>>) it value to the right, then mask (&) them to get rid of the other values. example, say we want to edit green: int argb = color(0,2,1,0); // that's color(red,green,blue,alpha) 00000000 00000000 00000010 00000001 as we can see above green is the second-right 8-bit value, so let's shift it 8 bit to the right to make it the right-most: int barg = argb >> 8; 00000001 00000000 00000000 00000010 by the nature of bitwise shifts the former right-most 8-bit-value is now the left-most. values that "fall off" an edge get put to to the other side. so 001 >> 1 (which is one) would give 100 (which is four) in a 3-bit-integer. so, to get rid of the other values in barg we mask it with an 8-bit-integer: int green = barg & 0xFF; // or barg & 255 00000001 00000000 00000000 00000010 (value) & 00000000 00000000 00000000 11111111 (mask) = 00000000 00000000 00000000 00000010 (result) ! left-most value is masked out ! now green contains value 2. green + 2; // green is now 4 00000000 00000000 00000000 00000100 let's assume we'd have done the same with alpha,red and blue ... to get things back together we do the reverse: int myColor = (alpha << 24) | (red << 16) | (green << 8 ) | blue; // or (alpha << 24) + (red << 16) + (green << 8 ) + (blue <<0) the | is an bitwise "or" operation: 001 | 101 ----- 101 say we have allready shifted the values into position: 00000000 00000000 00000100 00000000 (green, 4<<8 ) | 00000000 00000000 00000000 00000100 (blue, 4) = 00000000 00000000 00000100 00000100 some real code: Code: int my_color = color(1,2,3,4); int alpha = (my_color >> 24) & 0xFF; int red = (my_color >> 16) & 0xFF; int green = (my_color >> 8 ) & 0xFF; int blue = (my_color) & 0xFF; // do something with alpha, red, green, blue int my_other_color = (alpha << 24) | (red << 16) | (green << 8 ) | blue; |
| hopefully this was somewhat understandable, otherwise just ask .. /F
|
« Last Edit: Nov 8th, 2004, 4:19pm by fjen » |
|
|
|
|
kensuguro
|
Re: diffuse blur from SEB
« Reply #2 on: Nov 8th, 2004, 4:18pm » |
|
perfect! Thanks for taking the time to write such an extensive explanation!
|
|
|
|
kensuguro
|
Re: diffuse blur from SEB
« Reply #3 on: Nov 11th, 2004, 3:25pm » |
|
I have a follow up question to the bit operations.. What's a good way to overwrite just the alpha channel? I need to go through all the pixels, assign an alpha value... but in doing so, I think I'm assigning zeros to the other values. Code: for(int i=0; i<320*240; i++) { BImage.pixels[i]= value << 24; } |
| I think my goal should look something like: Code: for(int i=0; i<320*240; i++) { BImage.pixels[i]= value << 24 | "red from original" | "green from original".. and so on } |
| The problem is, how do I read the colors from the original? To make that question clearer: I know & 255 keeps only the right most value, but how do I do a filter that keeps the second from the right value? (like a 00ff00 or a ff0000 filter) Or on a even more basic level, that's the format for writing the hex? 0xff = 255, but how do I add more digits to it? 0x00 << 16 | 0xff << 8 | 0x00? Well, anyway, if this isn't the most sensual way to go about this, then what would be a better way?
|
|
|
|
fjen
|
Re: diffuse blur from SEB
« Reply #4 on: Nov 11th, 2004, 6:19pm » |
|
Quote:I need to go through all the pixels, assign an alpha value... but in doing so, I think I'm assigning zeros to the other values. Code: for(int i=0; i<320*240; i++) { BImage.pixels[i]= value << 24; } |
| yes, the way you do it you assign zeros. do like this: zero alpha in the pixel and then mask the value on to it: for(int i=0; i<320*240; i++) { BImage.pixels[i]= (BImage.pixels[i] & 0xff000000) | (value << 24); } btw: make sure value is 0-255 .. otherwise it will wrap around. value %= 255; look up "mod" in the reference if you're not sure what this does. hex for 255 = 0xff or 0x000000ff hex for 255 << 8 = 0xff00 or 0x0000ff00 get the point? /F
|
|
|
|
toxi
|
Re: diffuse blur from SEB
« Reply #5 on: Nov 11th, 2004, 6:39pm » |
|
actually, florian - with that kind of bit masking you're also overwriting the existing RGB values. here's some other code to overwrite the alpha channel for an image: http://processing.org/discourse/yabb/board_Syntax_action_displa_y_num_1098891414.html#3 as for hex numbers, it's exactly the same principle as with decimal ones. you add digits on the right to increase the order/magnitude of the value. only note that hex integers are limited to 8 hex digits.
|
http://toxi.co.uk/
|
|
|
fjen
|
Re: diffuse blur from SEB
« Reply #6 on: Nov 11th, 2004, 7:03pm » |
|
jikes! you're right ... makes no sense. sorry. to keep things together here's the correct way: for(int i=0; i<320*240; i++) { BImage.pixels[i]= BImage.pixels[i] & 0xffffff | (value << 24); }
|
|
|
|
|