YOUR ACCOUNT

Login or Register to post new topics or replies
James
James
Posts: 676
Filters: 46
I have made various scripts in Lua that do distortions, curve ops and color effects etc but so far I have been confused how to set a single pixel.

What I want is to simply set the r,g,b,a value at an x,y position. Does anyone have a script snippet for this? Thanks
  Details E-Mail
Theob
Beginner
Posts: 26
Filters: 2
Have you had a look here. Hope it may help smile:)
  Details E-Mail
James
James
Posts: 676
Filters: 46
The closest thing I can find in the library is the pixel art generators in the Bearestroika filter. These use an array however, I don't want to have to set a whole array of colours each time but simply set one pixel of an image inputs colour. There should be a simpler way but so far my experiments have failed but I'm guessing there will be a fairly simple solution.

So say for example I want to set 0,0 of images width and height to 255,0,0,1.0 which should make the very first pixel red.
  Details E-Mail
emme
Posts: 718
Filters: 8
As you probably know, FF works with sample coordinates, not with pixel coordinates - so there isn't really a simple way to set pixel values like you would on a bitmap based system.

You can use the global variables OUTPUT_WIDTH and OUTPUT_HEIGHT to get the image dimensions in pixels and use those calculate the pixel coordinates.

For example:

Code

function prepare()
   px,py = get_slider_input(PX),get_slider_input(PY)
   w,h = OUTPUT_WIDTH, OUTPUT_HEIGHT
   px,py = math.floor(px*w), math.floor(py*h)
end;

function get_sample(x, y)
   local v = 0
   if math.floor(x*w) == px and math.floor(y*h) == py then v = 1 end;
   return v,v,v
end;


Note that this only works with square images. Some extra code is needed to compensate for different aspect ratios.
  Details E-Mail
Rachel Duim
So Called Tortured Artist

Posts: 2498
Filters: 188
Another thing to know, FF is pixel aware. Although this only draws lines and not points, you can use this example to set single pixels if you wish: Pixel Image with Grid
Math meets art meets psychedelia.
  Details E-Mail
James
James
Posts: 676
Filters: 46
Thanks emme, that basically does what I want but due to the sample coordinates system you mention your script steps a bit too much each pixel value increased and also when the input value is max it seems to make the pixel vanish.

Another thing I noticed is if you don't use a slider input and just set px,py = 1,1 instead then the pixel vanishes. Also how would you increase the pixel size from 1x1 to say 10x10?

My goal was to port some lua scripts that draw various things using a set pixel function but it seems that due to the sample coordinates system there would be problems.

Also thanks for the reply Rachel Duim, currently I only have FF version 3 so I can't check the map script that filter uses. The script emme posted is basically what i want but there is the few issues I mention.
  Details E-Mail
emme
Posts: 718
Filters: 8
Yes, the way the SliderControl UI works doesn't allow for much precision. ValueControl outputs fractional values, but I don't think it's available in FF3. You can also manually ins ert fractional values to the SliderControl for more precision, or set an IntSlider range to match the image dimensions. These are not great solutions, but that's the best you can do. The pixel coordinates can of course be precisely set via code.

For a quick and dirty fix for the rounding problem, just subtract 0.5 from w and h in the if statement.

Code
if math.ceil(x*w-0.5) == px and math.ceil(y*h-0.5) == py then v = 1 end;


Yeah, I mean you could make a list / array of all the se t pixel instructions and just loop through them, but that would not be very efficient in FF smile:)
  Details E-Mail
James
James
Posts: 676
Filters: 46
Thanks emme, that code makes the pixel disappear if set to 0,0 however but I fixed it using an if statement:

Code
function prepare()
   px,py = get_slider_input(PX),get_slider_input(PY)
   w,h = OUTPUT_WIDTH, OUTPUT_HEIGHT
   px,py = math.floor(px*w), math.floor(py*h)
end;

function get_sample(x, y)
   local r = 0
   local g = 0
   local b = 0
   local a = 1

   if px == 0 and py == 0 then
      if math.floor(x*w) == px and math.floor(y*h) == py then
         r = 255
         g = 0
         b = 0
         a = 1
      end;
   else
      if math.ceil(x*w-0.5) == px and math.ceil(y*h-0.5) == py then
         r = 255
         g = 0
         b = 0
         a = 1
      end;
   end;
   
   return r,g,b,a
end;


I made a script where I convert the setPixel part into a function but for me it doesn't work in FF3, possibly due to the fractional values thing. Could someone confirm if the following script works in later versions?

Code
function prepare()
   w,h = OUTPUT_WIDTH, OUTPUT_HEIGHT
end;

function setPixel(x1,y1,w,h,px,py,r,g,b,a)
   px2,py2 = math.floor(px*w), math.floor(py*h)
   if math.floor(x1*w) == px2 and math.floor(y1*h) == py2 then
   end;
   return r,g,b,a
end

function get_sample(x, y)
   local r = 0
   local g = 0
   local b = 0
   local a = 1

   setPixel(x,y,w,h,1,1,255,0,0,1)

   return r,g,b,a
end;


If it doesn't work in later versions perhaps my code is broken somehow but it looks as if it should work.

Edit - Maybe the developers could a a new node "Pixel Script" in FF11 which can correctly set pixels and maybe also do html5 canvas style things like drawing lines and rectangles etc with simple code. That would allow for all sorts of interesting scripts to be made.
  Details E-Mail
emme
Posts: 718
Filters: 8
Slight modification, should work now more or less smile:)

Code

function prepare()
   w,h = OUTPUT_WIDTH, OUTPUT_HEIGHT
end;

function setPixel(x1,y1,w,h,px,py,r,g,b,a)
   px2,py2 = math.floor(px*w), math.floor(py*h)
   if math.floor(x1*w-0.5) == px2 and math.floor(y1*h-0.5) == py2 then   
      return r,g,b,a
   else
      return 0,0,0,1
   end;
end

function get_sample(x, y)
   local r,b,g,a = setPixel(x,y,w,h,0.5,0.5,255,0,0,1)
   return r,g,b,a
end;
  Details E-Mail
James
James
Posts: 676
Filters: 46
Thanks that show me an output. smile:)
  Details E-Mail
Rachel Duim
So Called Tortured Artist

Posts: 2498
Filters: 188
If anyone is interested, here is setting a single pixel in FF7 without lua.

Pixel Set.ffxml
Math meets art meets psychedelia.
  Details E-Mail

Join Our Community!

Filter Forge has a thriving, vibrant, knowledgeable user community. Feel free to join us and have fun!

33,712 Registered Users
+19 new in 30 days!

153,533 Posts
+31 new in 30 days!

15,348 Topics
+73 new in year!

Create an Account

Online Users Last minute:

35 unregistered users.