Author Topic: TI.Image: Get pixel color  (Read 5229 times)

0 Members and 1 Guest are viewing this topic.

Offline Shujal

  • LV0 Newcomer (Next: 5)
  • Posts: 3
  • Rating: +0/-0
    • View Profile
TI.Image: Get pixel color
« on: April 18, 2014, 11:27:10 am »
Is there a way to obtain the color of one pixel and plot that to the screen. I'd like to do some textured 3D rendering.
I don't think there is a method to do that and image metatables are hiden.
I thought of implementing it like this:
Code: [Select]
function plotPixelFromImage(gc, sx, sy, img, ix, iy)
    gc:clipRect("set", sx, sy, 1, 1)
    gc:drawImage(img, sx-ix, sy-iy)
    gc:clipRect("reset")
end
but I think it would be quite CPU intensive. Or would it be easier to make my own image format?

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: TI.Image: Get pixel color
« Reply #1 on: April 18, 2014, 11:49:58 am »
Well, the format is explained here : http://wiki.inspired-lua.org/TI.Image ... So parsing the string would be feasible, but quite long.... I guess it depends your image size (you could build a rgb matrix, though, once, and then read it from here)
But that would be for < 3.6 anyway.

And, by the way, doing anything that you want to be accetably-smooth/fast will have to be done without images (if you're < 3.6, and still, prefer not dealing with images when it's not "simple" things).


Edit : and yeah lol, I think your clipRect-powered function/trick would be cpu-intensive ^^
« Last Edit: April 18, 2014, 11:54:23 am by Adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Shujal

  • LV0 Newcomer (Next: 5)
  • Posts: 3
  • Rating: +0/-0
    • View Profile
Re: TI.Image: Get pixel color
« Reply #2 on: April 18, 2014, 02:38:44 pm »
Thank you for your reply. I will try to use low-resolution (probably 16x16) matrix. I'm just going to have to write a simple converter program.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: TI.Image: Get pixel color
« Reply #3 on: April 18, 2014, 03:20:39 pm »
Code: [Select]
platform.apiLevel = "2.0"

local imgstr = "\032\000\000\000\032\000\000\000\000\000\000\000\064\000\000\000\016\000\001\000\255\255\255\255\255\255\255\255\000\252\255\255\255\255\000\252\255\255\255\255\255\255\255\255\255\255\255\255\255\255_\144_\144_\144_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\252\255\255\255\255\000\252\255\255\255\255\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\252\255\255\255\255\000\252\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\252\255\255\255\255\000\252\255\255\255\255\241\167\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\252\000\252\000\252\000\252\255\255\255\255\241\167_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\252\255\255\255\255\000\252\255\255\255\255\241\167\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\252\255\255\255\255\000\252\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\031\161\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255_\144\241\167\255\255\255\255\255\255\255\255\255\255\255\255\031\161\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255_\144\255\255\241\167_\144\255\255\255\255\255\255\255\255\255\255\031\161\255\255\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\241\167\255\255_\144\255\255\255\255\255\255\255\255\031\161\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\031\161\255\255\255\255\255\255\255\255\255\255\255\255\231\252\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\247\211_\144\255\255\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\031\161\255\255\255\255\255\255\255\255\255\255\231\252_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\247\211\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\031\161\255\255\255\255\255\255\255\255\255\255\231\252\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255_\144\255\255\255\255\247\211\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\231\252\231\252\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\247\211\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\231\252\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\247\211\255\255\255\255\255\255\255\255\231\252\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255_\144\247\211\255\255\255\255\231\252\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\004\128\004\128\255\255\231\252\255\255\255\255\004\128\004\128\004\128\004\128\004\128\004\128\004\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255_\144\255\255\255\255\255\255\255\255\255\255\255\255\004\128\004\128\255\255_\144\255\255\247\211\247\211\255\255\004\128_\144_\144\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\004\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\004\128\255\255\231\252_\144_\144\255\255\247\211\247\211\004\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\004\128\004\128\255\255\004\128\004\128\255\255\255\255\255\255\255\255\004\128\231\252\255\255\255\255\255\255_\144_\144_\144\004\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\004\128\004\128\255\255\255\255\004\128_\144\255\255\255\255\255\255\231\252\004\128\004\128\255\255\255\255\255\255\255\255\255\255\255\255\004\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\004\128\004\128\255\255\255\255\004\128\255\255\255\255_\144_\144\231\252\255\255\255\255\004\128\004\128\004\128\004\128\004\128\255\255\255\255\004\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\004\128\004\128\255\255\255\255\255\255\004\128\255\255\231\252\231\252\255\255_\144_\144_\144_\144\255\255\255\255\255\255\004\128\255\255\255\255\004\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\004\128\004\128\004\128\255\255\255\255\255\255\004\128\004\128\231\252\255\255\255\255\255\255\004\128\255\255\255\255\255\255\255\255\255\255\004\128\255\255\255\255\004\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\004\128\004\128\255\255\004\128\255\255\255\255\255\255\004\128\004\128\255\255\255\255\255\255\004\128\004\128\255\255\255\255\255\255\255\255\255\255\004\128\255\255\255\255\004\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\004\128\255\255\255\255\004\128\004\128\255\255\255\255\255\255\004\128\004\128\255\255\004\128\004\128\255\255\255\255\255\255\255\255\255\255\004\128\004\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\004\128\004\128\255\255\255\255\255\255\004\128\004\128\004\128\255\255\255\255\255\255\255\255\004\128\004\128\004\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\004\128\004\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\004\128\255\255\004\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\004\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255"
local img = image.new(imgstr)

local modf = math.modf

local function getPixel(imgstr, x, y, imgwidth)
   local p = 21 + (y * imgwidth + x) * 2
   local d = imgstr:byte(p+1) * 256 + imgstr:byte(p)
   
   local b, g
   d, b = modf(d / 32)
   d, g = modf(d / 32)
   if d >= 32 then d=d-32 end
   
   return d * 8, g * 256, b * 256
end

local function drawImage(gc, imgstr, x, y, w, h)
    for yy=1, h do
        for xx=1, w do
            gc:setColorRGB(getPixel(imgstr, xx-1, yy-1, w))
            gc:fillRect(x+xx, y+yy, 1, 1)
        end
    end
end

function on.paint(gc)
    gc:drawImage(img, 10, 10)
    drawImage(gc, imgstr, 10, 100, 32, 32)
end


I suppose you want something like that.
(but indeed, it might be better to use something other than the TI.Image format)

Offline Shujal

  • LV0 Newcomer (Next: 5)
  • Posts: 3
  • Rating: +0/-0
    • View Profile
Re: TI.Image: Get pixel color
« Reply #4 on: April 18, 2014, 04:25:21 pm »
For anyone who is interested, here is the conversion program (java):


Code: [Select]
import java.awt.image.BufferedImage;


import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;




public class Main {


public static void main(String[] args) {

JFileChooser open = new JFileChooser();
if(open.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
try{

BufferedImage theImg = ImageIO.read(open.getSelectedFile());
String out = "{";
for(int j=0; j<theImg.getWidth(); j++){
out+="{";
for(int i=0; j<theImg.getHeight(); i++){
//System.out.println(i+","+j);
out+="{"+((theImg.getRGB(j, i)&0x00ff0000)>>16) /*red*/
+","+((theImg.getRGB(j, i)&0x0000ff00)>>8) /*green*/
+","+((theImg.getRGB(j, i)&0x000000ff))+"},"; /*blue*/

}
out+="},";

}
out+="}";
System.out.println(out.replaceAll(",}", "}"));

}catch(Exception e){e.printStackTrace();}
}else{

JOptionPane.showMessageDialog(null, "You clicked cancel :_(");

}

}
}


Just paste the result into your lua code. You can access a pixel's color using


Code: [Select]
texture[xIndex][yIndex][color](color=1:red, color=2:green, color=3:blue)  It is still pretty slow (1 fps with two faces being rendered on a TI-npire CX CAS), maybe I should play with the resolution.