Pixel Manipulation in Turing
Author |
Message |
GlobeTrotter
|
Posted: Wed Oct 26, 2005 9:03 pm Post subject: Pixel Manipulation in Turing |
|
|
Basically, I want to be able to save the RGB values of each pixel into an array. The problem is that the colours get messed up when I try using whatdotcolor. Look at this example, you can use any picture with it. It should theoretically draw out the picture, exactly as it was. If you can maybe point me to some RGB. command that will read the pixels better, I would appreciate it.
code: |
type pixel :
record
R : real
G : real
B : real
end record
var PixelArray : flexible array 1 .. 0, 1 .. 0 of pixel
procedure loadPicture (var filename : string)
var picID : int
picID := Pic.FileNew (filename)
loop
exit when picID > 0
loop
put "Enter the filename of the picture"
get filename
if index (Str.Upper (filename), ".JPG") = 0 and index (Str.Upper (filename), ".BMP") = 0 then
filename := ""
put "Be sure to include the extension (.JPG or .BMP)"
end if
exit when length (filename) >= 5
end loop
picID := Pic.FileNew (filename)
end loop
setscreen ("graphics:" + intstr (Pic.Width (picID)) + ";" + intstr (Pic.Height (picID)) + "")
Pic.Draw (picID, 0, 0, picCopy)
Pic.Free (picID)
end loadPicture
procedure drawPicture
for x : 1 .. maxx + 1
for y : 1 .. maxy + 1
drawdot (x, y, RGB.AddColour (PixelArray (x, y).R, PixelArray (x, y).G, PixelArray (x, y).B))
end for
end for
end drawPicture
procedure getPixels
new PixelArray, maxx + 1, maxy + 1
for x : 1 .. maxx + 1
for y : 1 .. maxy + 1
RGB.GetColor (whatdotcolour (x, y), PixelArray (x, y).R, PixelArray (x, y).G, PixelArray (x, y).B)
drawdot (x, y, RGB.AddColour (1 - PixelArray (x, y).R, 1 - PixelArray (x, y).G, 1 - PixelArray (x, y).B))
end for
end for
end getPixels
var picName := "test.jpg"
loadPicture (picName)
getPixels
drawPicture
|
I have another question, dealing with the same program. This next procedure is incomplete, and it doesn't work. It should theoretically, go through each pixel, and find the average of x pixels surrounding it, and change the array of pixels to that. Thus, the pixel would be blurred. The procedure checks if the pixel it is averaging is in the middle of the picture such that pixels too far left/right/up/down won't be checked. I'm not sure why, but I'm getting the same array out of bounds error that I just checked to make sure wouldn't happen. Please help.
code: |
procedure SquareBlur (BlurLength : int)
var OrigPixelArray : flexible array 1 .. 0, 1 .. 0 of pixel
var pixelLocation : array - 1 .. 1, -1 .. 1 of boolean
var tempAvg : pixel
new OrigPixelArray, maxx + 1, maxy + 1
for x : 1 .. maxx + 1
for y : 1 .. maxy + 1
OrigPixelArray (x, y) := PixelArray (x, y)
end for
end for
for x : 1 .. maxx + 1
for y : 1 .. maxy + 1
for i : -1 .. 1
for j : -1 .. 1
pixelLocation (i, j) := false
end for
end for
tempAvg.R := 0
tempAvg.G := 0
tempAvg.B := 0
if x < BlurLength then
if y < BlurLength then
pixelLocation (-1, -1) := true
elsif y > maxy + 1 - BlurLength then
pixelLocation (-1, 1) := true
else
pixelLocation (-1, 0) := true
end if
elsif x > maxx + 1 - BlurLength then
if y < BlurLength then
pixelLocation (1, -1) := true
elsif y > maxy + 1 - BlurLength then
pixelLocation (1, 1) := true
else
pixelLocation (1, 0) := true
end if
else
pixelLocation (0, 0) := true
end if
if pixelLocation (0, 0) then
for blurx : x - BlurLength .. x + BlurLength
for blury : y - BlurLength .. y + BlurLength
tempAvg.R += OrigPixelArray (blurx, blury).R / BlurLength ** 2
tempAvg.G += OrigPixelArray (blurx, blury).G / BlurLength ** 2
tempAvg.B += OrigPixelArray (blurx, blury).B / BlurLength ** 2
end for
end for
PixelArray (x, y) := tempAvg
end if
end for
end for
end SquareBlur
|
Just call that procedure inbetween getpixels and drawpixels. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Thu Oct 27, 2005 7:51 am Post subject: (No subject) |
|
|
the whatdotcolour() function is flawed as it will return the pallet colour that matches the dot the closest. Then RGB.GetColour returns RGB of that 8bit estimation.
I'm not sure if this will work, but if you precreate your pallet by looping though RGB, you could expend it to 31bit (Turing's integers are signed)
Otherwise you could read RGB values directly from the file. You might been to figure out the format first. BMP should be the easiest as there's no compression. |
|
|
|
|
![](images/spacer.gif) |
GlobeTrotter
|
Posted: Thu Oct 27, 2005 4:57 pm Post subject: (No subject) |
|
|
Okay, thanks... but what about my second question? Why am I getting out of bounds errors? |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Fri Oct 28, 2005 9:02 am Post subject: (No subject) |
|
|
well you have PixelArray (x, y) := tempAvg while the loop goes up to x = maxx + 1
what's the boundry of PixelArray? |
|
|
|
|
![](images/spacer.gif) |
GlobeTrotter
|
Posted: Fri Oct 28, 2005 3:41 pm Post subject: (No subject) |
|
|
code: | new PixelArray, maxx + 1, maxy + 1 |
|
|
|
|
|
![](images/spacer.gif) |
GlobeTrotter
|
Posted: Mon Oct 31, 2005 5:53 pm Post subject: (No subject) |
|
|
Okay, I tried reading straight from a .BMP file, but I'm having some trouble. The tutorial I'm reading is done in C, I think, and I don't know C. I've managed to read the header information, but am having trouble reading the pixel information. Please help.
The site I'm working from is here: http://www.xbdev.net/image_formats/bmp/index.php?leftbarHIDE=1
Basically, I'm trying to copy that code into Turing, and I'm finding it difficult, especially to part about to pointer to an int, and some of the bit shifting.
code: |
setscreen ("text")
const fileName := "8x8 24.bmp"
var stType : string (1) := "" %I couldn't get this to fit inside the reccord. The bytes wouldn't match up, I'm not sure why
%This should go under BitmapFileHeader.
type BitmapFileHeader :
record
iSize : nat4 %Whole size of the bitmap file on your pc
iReserved1 : nat2 %Unused - just ignore
iReserved2 : nat2 %Unused - just ignore
iOffSetBits : nat4 %Offset from the start of the file to our pixel data
end record
type BitmapInfoHeader :
record
iSize : nat4 %Size of this Header - 40 bytes
iWidth : nat4 %Image Width
iHeight : nat4 %Image Height
iPlanes : nat2
iBitCount : nat2 %Bits per pixel - 1,4,8,16,24 or 32
iCompression : nat4 %Compression type - 0=RGB(No Compression), 1=RLE8, 2=RLE4, 3=BITFIELDS
iSizeImage : nat4 %Size of image
iXPelsPerMeter : nat4 %Preferred resolution in pixels per meter
iYPelsPerMeter : nat4 %Preferred resolution in pixels per meter
iClrUsed : nat4 %Number of entries in the colour map that are actually used
iClrImportant : nat4 %Number of significant colours
end record
%This is how the tutorial did it, I'm not sure exactly how to implement to pointer though.
% type Image :
% record
% iWidth : nat4
% iHeight : nat4
% pARGB : ponater to nat4
% end record
%Pixels are stored BGR, not RGB
type PixelType :
record
B : nat1
G : nat1
R : nat1
end record
var PixelArray : flexible array 0 .. 0 of nat4
var FileHeader : BitmapFileHeader
var InfoHeader : BitmapInfoHeader
%var ImageBits : Image
var fileNo : int
open : fileNo, fileName, read
read : fileNo, stType
if stType = "BM" then
read : fileNo, FileHeader
read : fileNo, InfoHeader
%Again, this was the pointer implementation I didn't understand
% ImageBits.iWidth := InfoHeader.iWidth
% ImageBits.iHeight := InfoHeader.iHeight
%
% ImageBits.pARGB := ^ (ImageBits.iWidth * ImageBits.iHeight)
var iNumPaddedBytes := (InfoHeader.iWidth * 3) mod 4
%The pixels will be read, then converted to hexadecimals, and stored in an array
var TempPixel : PixelType
%The number of pixels will be w*h
new PixelArray, InfoHeader.iWidth * InfoHeader.iHeight
%Loop through, read each pixel, then try and assign the value to the array
%I didn't use a 2D array because it wouldn't let me resize it
for h : 0 .. InfoHeader.iHeight
for w : 0 .. InfoHeader.iWidth
read : fileNo, TempPixel
PixelArray (w + h * InfoHeader.iWidth) := (TempPixel.R shl 16 or TempPixel.G shl 8 or TempPixel.B)
end for
end for
end if
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Display all the gathered data
put "iType: " + stType
put "iSize: " + natstr (FileHeader.iSize)
put "iReserved1: " + natstr (FileHeader.iReserved1)
put "iReserved2: " + natstr (FileHeader.iReserved2)
put "iOffSetBits: " + natstr (FileHeader.iOffSetBits)
put "iSize: " + natstr (InfoHeader.iSize)
put "iWidth: " + natstr (InfoHeader.iWidth)
put "iHeight: " + natstr (InfoHeader.iHeight)
put "iPlanes: " + natstr (InfoHeader.iPlanes)
put "iBitCount: " + natstr (InfoHeader.iBitCount)
put "iCompression: " + natstr (InfoHeader.iCompression)
put "iSizeImage: " + natstr (InfoHeader.iSizeImage)
put "iXPelsPerMeter: " + natstr (InfoHeader.iXPelsPerMeter)
put "iYPelsPerMeter: " + natstr (InfoHeader.iYPelsPerMeter)
put "iClrUsed: " + natstr (InfoHeader.iClrUsed)
put "iClrImportant: " + natstr (InfoHeader.iClrImportant)
for h : 0 .. InfoHeader.iHeight
for w : 0 .. InfoHeader.iWidth
put natstr (PixelArray (w + h * InfoHeader.iWidth))
end for
end for
|
|
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
|
|
|
![](images/spacer.gif) |
|
|