Bit shifting and Images
Author |
Message |
copthesaint
![](http://compsci.ca/v3/uploads/user_avatars/15853548854c9c056fda48d.jpg)
|
Posted: Tue May 08, 2012 10:47 pm Post subject: Bit shifting and Images |
|
|
I am trying getting the bit values of any image and Im trying to obtain the R,G,B values.
the values obtained from takePic are defined as BGRB, GRBG, RBGR generally. If for example one pixel values were 255,128,60,55 then
the result of that one value from takePic would be (255 * 2^0 + 128 * 2 ^ 8 + 60 * 2 ^ 16 + 55* 2 ^ 24) which would = 1853424126
When you use take pic it gives you the bit value but not the value of the colors themselves. Can someone help me with this? I want to get back the values of RGB from the bit value.
Turing: |
type Pixel :
record
CLR : array 1 .. 3 of int2
Alpha : real4
end record
type Buffer :
record
RGB : Pixel
Depth : int1
end record
class Layer
import Pixel, Buffer
export Capture, Display, Scale
const TransperentColor : int1 := 0
var widthLayer, heightLayer : int2 := 0
var posXLayer, posYLayer : int2 := 0
var scale : int2 := 0
var layer : flexible array 1 .. 0 of Buffer
var layerBoundSize : int := 0
procedure Scale (x : int2)
scale := x
end Scale
procedure Capture (xPos1, yPos1, xPos2, yPos2 : int)
var b : array 1 .. sizepic (xPos1, yPos1, xPos2, yPos2 ) of int
takepic (xPos1, yPos1, xPos2, yPos2, b )
heightLayer := b (1)
widthLayer := b (2)
posXLayer := xPos1
posYLayer := yPos1
layerBoundSize := widthLayer * heightLayer
if layerBoundSize > upper (layer ) then
new layer, layerBoundSize
end if
b (7) := 48
var id := 18
var imageID : int := 0
var n : int := 0
for j : 1 .. heightLayer
for i : 1 .. widthLayer
imageID := imageID + 1
for p : 1 .. 3
layer (imageID ).RGB.CLR (4 - p ):= b (id ) & 1 shr n
if n < 24 then
n + = 8
else
n := 0
id + = 1
end if
end for
end for
if n > 0 then
id + = 1
n := 0
end if
end for
end Capture
procedure Display
cls
var b : array 1 .. sizepic (1, 1, widthLayer, heightLayer ) of int
takepic (1, 1, widthLayer, heightLayer, b )
for i : 19 .. upper (b )
b (i ) := 0
end for
b (7) := 48
var id := 19
var imageID : int := 0
var n : int := 0
for j : 1 .. heightLayer
for i : 1 .. widthLayer
imageID := imageID + 1
for p : 1 .. 3
b (id )| = layer (imageID ).RGB.CLR (4 - p ) shl n
if n < 24 then
n + = 8
else
n := 0
id + = 1
end if
end for
end for
if n > 0 then
id + = 1
n := 0
end if
end for
drawpic (posXLayer, posYLayer, b, 1)
var pic1 : int := Pic.New (posXLayer, posYLayer, posXLayer + widthLayer, posYLayer + heightLayer )
var pic2 : int := Pic.Scale (pic1, widthLayer * scale, heightLayer * scale )
Pic.Draw (pic2, posXLayer, posXLayer, picCopy)
View.Update
Pic.Free (pic1 )
Pic.Free (pic2 )
end Display
end Layer
class Render
procedure Color
end Color
procedure Update
end Update
end Render
View.Set ("Graphics:640;440,offscreenonly,nobuttonbar")
procedure Test1
var layer1 : pointer to Layer
new Layer, layer1
var pic1 : int := Pic.FileNew ("test.bmp")
Pic.Draw (pic1, 1, 1, picCopy)
View.Update
layer1 -> Capture (1, 1, Pic.Width (pic1 ) , Pic.Height (pic1 ))
Pic.Free (pic1 )
cls
layer1 -> Scale (2)
layer1 -> Display
end Test1
Test1
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Wed May 09, 2012 9:02 am Post subject: RE:Bit shifting and Images |
|
|
RGB.GetColour (x : int, var r, g, b : real)
I think that does what you're looking for
|
|
|
|
|
![](images/spacer.gif) |
copthesaint
![](http://compsci.ca/v3/uploads/user_avatars/15853548854c9c056fda48d.jpg)
|
Posted: Wed May 09, 2012 10:42 am Post subject: RE:Bit shifting and Images |
|
|
No, I already know of it, I want to know how I can use the bitwise operators to get the colors of the image.
When you use takepic (x1,y1,x2,y2 : int , var b : array 1 .. * of int)
It sets every color value of the image in the buffer array. I am trying to figure out how to get the rgb values from the color numbers.
the first few values in the buffer are identities and at 18, the numbers are in a sence an index to a color.
Edit: Currently I have my picture displaying after getting the values, I changed line 48:
Turing: | layer (imageID).RGB.CLR (4 - p):= b (id) & 1shr n |
To
Turing: | layer (imageID).RGB.CLR (4 - p):= b (id) & 255 shr n |
because it made more sence being there are 256 avalible colors however
the image gets black lines down the middle now...
Description: |
|
Filesize: |
42.12 KB |
Viewed: |
63 Time(s) |
![Striped Image.JPG](uploads/attachments/thumbs/t_striped_image_286.jpg)
|
|
|
|
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Wed May 09, 2012 1:28 pm Post subject: RE:Bit shifting and Images |
|
|
Sorry, thats all I have out of curiosity, what's this for?
|
|
|
|
|
![](images/spacer.gif) |
copthesaint
![](http://compsci.ca/v3/uploads/user_avatars/15853548854c9c056fda48d.jpg)
|
Posted: Wed May 09, 2012 1:36 pm Post subject: RE:Bit shifting and Images |
|
|
I am making a new draw module (using turing predefines) to see how well it will work, I want to be able to rotate images as 3D objects easier in turing, however I didnt want to use RGB.SetColor since it takes up a freaking huge amount of time and you can only set at most 1000 colors, I know that 256*256*256 colors will be enough for me though
If you want to know the difference between turing draw module (8bit) and my draw module I hope to finish (24bit) glimpse at this wiki
http://en.wikipedia.org/wiki/Color_depth
I still cant figure out how to properly get the colors from the image...
|
|
|
|
|
![](images/spacer.gif) |
Amarylis
|
Posted: Wed May 09, 2012 8:37 pm Post subject: RE:Bit shifting and Images |
|
|
I wish you luck in your quest...
Also, a personal request, if at all possible, could you make the drawpic work with 2D arrays as opposed to 1D? I find that messing with pixel data is a lot simpler when you can use coordinates for them
I saw this a while ago and I made a procedure in my Draw module that would work as what I mentioned above, though it's really just a bit of a cheap trick...
Turing: |
procedure FromBuffer (buffer : array 0 .. *, 0 .. * of int, x, y : int)
for i : 0 .. upper (buffer, 1)
for o : 0 .. upper (buffer, 2)
Draw.Dot (x + i, y + o, buffer (i, o ))
end for
end for
end FromBuffer
|
Edit: My suggestion for you is to make the class Layer be a module instead, so that way you dont have to create an object for it every time you want to draw an image
|
|
|
|
|
![](images/spacer.gif) |
copthesaint
![](http://compsci.ca/v3/uploads/user_avatars/15853548854c9c056fda48d.jpg)
|
Posted: Wed May 09, 2012 10:30 pm Post subject: Re: Bit shifting and Images |
|
|
The layer class is purposely a class for the render module I will make so then individual drawings can be done to sperate layers, Making fills and other future implements much easier. trust me, it is better this way Also It is very simple to draw to a 1D array since the equation is only,
Turing: | (y - 1) * widthLayer + x |
I am doing everything specifically for what I have in mind for the future. Just sometimes, like now for instance, I run into a road block.
|
|
|
|
|
![](images/spacer.gif) |
copthesaint
![](http://compsci.ca/v3/uploads/user_avatars/15853548854c9c056fda48d.jpg)
|
Posted: Thu May 10, 2012 10:39 am Post subject: RE:Bit shifting and Images |
|
|
Bump ~
After spending the time to write everything on paper and reading more, I have come up with the solution on my own.
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Amarylis
|
Posted: Thu May 10, 2012 12:24 pm Post subject: RE:Bit shifting and Images |
|
|
Oh? What did you come up with?
|
|
|
|
|
![](images/spacer.gif) |
copthesaint
![](http://compsci.ca/v3/uploads/user_avatars/15853548854c9c056fda48d.jpg)
|
Posted: Thu May 10, 2012 1:28 pm Post subject: Re: Bit shifting and Images |
|
|
It took a while to figure out how to correctly do this, but here is what I implimented into my module:
Turing: | var clr : array 0 .. 3 of int := init (255, 255, 255, 255) %example of colors sent from a bitmap image
var x : nat4 := clr (0) shl 0| clr (1) shl 8| clr (2) shl 16| clr (3) shl 24 %the actual value sent from a bitmap image
var shiftValues : array 0 .. 3 of int := init (0, 8, 16, 24) %shift values, all colors are seperated by 8 bits
var y : nat4 := x %A temp variable to hold the value sent from the bitmap
for i : 0 .. 3 %for all 4 values
clr (i) := y shr shiftValues (3 - i) %the color is equal to temp variable's value shift right (3-i)*8
y := y - (clr (i) shl shiftValues (3 - i)) % the temp variable is subtracted by the color value shift left (3-i)*8
end for % end for
for i : 0 .. 3
put clr (i)
end for |
|
|
|
|
|
![](images/spacer.gif) |
|
|