Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Is it possible to store an image in a variable?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Filipe_Br
Master Cheater
Reputation: 3

Joined: 07 Jan 2016
Posts: 272
Location: My house

PostPosted: Sun Dec 18, 2016 4:44 pm    Post subject: Is it possible to store an image in a variable? Reply with quote

Is there any chance of a variable having a string, and this string is an image, and I open this image?
_________________
...
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 470

Joined: 09 May 2003
Posts: 25791
Location: The netherlands

PostPosted: Sun Dec 18, 2016 5:38 pm    Post subject: Reply with quote

the stringstream object is useful for this.
Code:

ss=createStringStream('string')


p=createPicture()
p.loadFromStream(ss)

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Filipe_Br
Master Cheater
Reputation: 3

Joined: 07 Jan 2016
Posts: 272
Location: My house

PostPosted: Sun Dec 18, 2016 5:54 pm    Post subject: Reply with quote

How can I get the string of an image?
_________________
...
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 470

Joined: 09 May 2003
Posts: 25791
Location: The netherlands

PostPosted: Sun Dec 18, 2016 6:03 pm    Post subject: Reply with quote

you can download it from a website. (the internet object in ce will download images as a string by default)

or I guess you could save the image as a table file and then do:
Code:

s=createStringStream()
z=findTableFile('MyImage')
z.Stream.Position=0
s.CopyFrom(z.Stream,z.Stream.Size)
string=s.DataString


although if you already have the image in your table I don't know why you'd want it as a string because you can then just as well do:
Code:

z=findTableFile('MyImage')
p=createPicture()
p.loadFromStream(z.Stream)

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Sun Dec 18, 2016 7:53 pm    Post subject: Reply with quote

Load picture image with a function

add a picture image on Table > add file

function 1
Load picture from stream (picture show as original and no border)
Code:
function attachBackground(wc,tblFile)
  local p = createPicture()
  p.loadFromStream(findTableFile(tblFile).Stream)
  wc.OnPaint = function(sender)
    local c = sender.getCanvas()
    local bitmap = p.getBitmap()
    c.draw(0,0,bitmap)
  end
end

f = createForm(false)

attachBackground(f,[[MyPicture.jpg]])
f.show()


function 2
Load picture image from stream (with border and stretch)
Code:
function attachBackground(wc,tblFile,xoff,yoff,w,h,withBorderWidth)
  if wc and wc.getCanvas then
    rawset(_G,'bmCache',rawget(_G,'bmCache') or {})
    if not bmCache[tblFile] then
      local p = createPicture()
      p.loadFromStream(findTableFile(tblFile).Stream)
      bmCache[tblFile] = p.getBitmap()
    end
    local b = bmCache[tblFile]
    xoff,yoff,w,h = xoff or 0,yoff or 0,w or b.Width, h or b.Height
    wc.OnPaint = function(sender)
      local c,bw=sender.Canvas,withBorderWidth == true and wc.BorderWidth or 0
      c.copyRect(bw,bw,wc.Width-bw,wc.Height-bw,b.Canvas,xoff,yoff,xoff+w,yoff+h)
    end
    return true
  else
    return false,'the object has no canvas'
  end
end

-- to draw withBorderWidth
f = createForm(false)
attachBackground(f,[[MyPicture.jpg]],nil,nil,nil,nil,true)
f.show()
Back to top
View user's profile Send private message
Filipe_Br
Master Cheater
Reputation: 3

Joined: 07 Jan 2016
Posts: 272
Location: My house

PostPosted: Mon Dec 19, 2016 4:48 am    Post subject: Reply with quote

I guess what I want to do is impossible.
It would be kind of like that. A variable would get an image, and using print () I would get the string that forms this image. Then it would get this string and assign it to a variable, and in the end it would show the image.

If this is not possible, how to store an image inside the .exe application.
(If that was what you said, I did not realize it)

_________________
...
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Mon Dec 19, 2016 7:46 am    Post subject: Reply with quote

If exe application not encrypted
use a tool "ResourceHacker" to change bitmap image, icon, etc on the exe app

http://www.wexpert.webs.com/documents/Resouce%20hacker%20tutorial.pdf

also check this (convert image to lua format)

http://principiagame.com/lua/img
Back to top
View user's profile Send private message
Filipe_Br
Master Cheater
Reputation: 3

Joined: 07 Jan 2016
Posts: 272
Location: My house

PostPosted: Mon Dec 19, 2016 9:43 am    Post subject: Reply with quote

I think that's what I needed. "http://principiagame.com/lua/img".
Using this image as an example. "http://cheatengine.org/celogo.png".
Using that site, how do I make the image appear on my form?

_________________
...
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Mon Dec 19, 2016 8:37 pm    Post subject: Reply with quote

Converting image to base64 image string.
Load decoded image using LuaSocket (mean need lua library ). No idea how to do this use pure lua (without "require")

By lua socket :

Code:

local ltn12 = require "ltn12"
local mime = require "mime"

mystring = "somedata"
myoutfile = "out.gif"

ltn12.pump.all(
  ltn12.source.string(mystring),
  ltn12.sink.chain(
    mime.decode("base64"),
    ltn12.sink.file(io.open(outfile,"w"))
  )
)


Or doing using HTML5 code

Note :
http://principiagame.com/lua/img -- this for principia game, some command / statement are not recognized by pure lua 5.1, 5.2. 5.3 such as "draw_sprite_textel" - "this : sprite..", etc...
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Mon Dec 19, 2016 9:12 pm    Post subject: Reply with quote

Base64 Encoded Image
Code:
function createPictureFromURL(url)
  local http = getInternet()
  local file = http.getURL(url)
  http.destroy()
  local picture = createPicture()
  local stream = createStringStream(file)
  picture.loadFromStream(stream)
  return picture
end

function createPictureFromBase64(data)
  local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  data = string.gsub(data, '[^'..b..'=]', '')
  data = (data:gsub('.', function(x)
    if (x == '=') then return '' end
    local r,f='',(b:find(x)-1)
    for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
    return r;
  end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
    if (#x ~= 8) then return '' end
    local c=0
    for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
    return string.char(c)
  end))
  local picture = createPicture()
  local stream = createStringStream(data)
  picture.loadFromStream(stream)
  return picture
end

if form == nil then
  form = createForm(false)
end

if imgage == nil then
  imgage = createImage(form)
end

if picture == nil then
  picture = createPictureFromBase64("base64 encoded string")
  --picture = createPictureFromURL("http://cheatengine.org/celogo.png")
  imgage.setPicture(picture)
  imgage.Height = 128
  imgage.Width = 128
end

form.show()
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Mon Dec 19, 2016 10:32 pm    Post subject: Reply with quote

Zanzer, how to load image if Base64 Encoded Image stores as a bin file as CE stream file.
eq. "celogo.bin' ?

Thanks
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Mon Dec 19, 2016 11:03 pm    Post subject: Reply with quote

If you're storing the file in the table, there's no reason to encode it in base64. Smile
But if you must... use DB's code.
Code:
s=createStringStream()
z=findTableFile('celogo.bin')
z.Stream.Position=0
s.CopyFrom(z.Stream,z.Stream.Size)
data=s.DataString
picture = createPictureFromBase64(data)
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Tue Dec 20, 2016 5:52 am    Post subject: Reply with quote

i try :

Code:
url = "http://pastebin.com/sT9AAKm4"
file = "celogo.png"

function createPictureFromURL(url)
  local http = getInternet()
  local file = http.getURL(url)
  http.destroy()
  local picture = createPicture()
  local stream = createStringStream(file)
  picture.loadFromStream(stream)
  return picture
end

function createPictureFromBase64(data)
  local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  data = string.gsub(data, '[^'..b..'=]', '')
  data = (data:gsub('.', function(x)
    if (x == '=') then return '' end
    local r,f='',(b:find(x)-1)
    for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
    return r;
  end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
    if (#x ~= 8) then return '' end
    local c=0
    for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
    return string.char(c)
  end))
  local picture = createPicture()
  local stream = createStringStream(data)
  picture.loadFromStream(stream)
  return picture
end

if form == nil then
  form = createForm(false)
end

if imgage == nil then
  imgage = createImage(form)
end

if picture == nil then
  picture = createPictureFromBase64(file)
--  picture = createPictureFromURL(url)
  imgage.setPicture(picture)
  imgage.Height = 128
  imgage.Width = 128
end

form.show()


- When tested result give "Error : Unknown picture format"

Next, I did my base64 encode image as a HTML file and store it as celogo.html on a online storage. This celogo.html if open with a browser will displaying "ce logo", withoutt any issue.
How load that celogo.html and executing with default browser using this "http = getInternet()" ?


I doing with local base64 image string :

i
Code:
mg = [["iVBORw0KGgVWh3III/Uhlu ......... etc...etc .....RK5CYII="]]

function createPictureFromBase64(data)
  local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  data = string.gsub(data, '[^'..b..'=]', '')
  data = (data:gsub('.', function(x)
    if (x == '=') then return '' end
    local r,f='',(b:find(x)-1)
    for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
    return r;
  end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
    if (#x ~= 8) then return '' end
    local c=0
    for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
    return string.char(c)
  end))
  local picture = createPicture()
  local stream = createStringStream(data)
  picture.loadFromStream(stream)
  return picture
end

if form == nil then
  form = createForm(false)
end

if imgage == nil then
  imgage = createImage(form)
end

if picture == nil then
  picture = createPictureFromBase64(img)
  --picture = createPictureFromURL("http://cheatengine.org/celogo.png")
  imgage.setPicture(picture)
  imgage.Height = 128
  imgage.Width = 128
end

form.show()


Thank you



Capture.JPG
 Description:
 Filesize:  16.46 KB
 Viewed:  11531 Time(s)

Capture.JPG


Back to top
View user's profile Send private message
Filipe_Br
Master Cheater
Reputation: 3

Joined: 07 Jan 2016
Posts: 272
Location: My house

PostPosted: Tue Dec 20, 2016 7:55 am    Post subject: Reply with quote

I gave up trying this
_________________
...
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Tue Dec 20, 2016 9:16 am    Post subject: Reply with quote

Code:
---- Image file handle

path = "D://images.jpg"      ---- put your image file name here (+ path / directory)
file = io.open(path, "rb")
image = file:read("*a")


---- Convert image to base64 encode string

function createBase64romPicture(data)
 local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
    return ((data:gsub('.', function(x)
        local r,b='',x:byte()
        for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
        return r;
    end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
        if (#x < 6) then return '' end
        local c=0
        for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
        return b:sub(c+1,c+1)
    end)..({ '', '==', '=' })[#data%3+1])
end

bs64 = createBase64romPicture(image)    --- creating base64 image
-- print(bs64)
img = tostring(bs64)  --- store as string variable


---- Convert back Base64 image string as a image picture and show

function createPictureFromBase64(data)
  local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  data = string.gsub(data, '[^'..b..'=]', '')
  data = (data:gsub('.', function(x)
    if (x == '=') then return '' end
    local r,f='',(b:find(x)-1)
    for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
    return r;
  end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
    if (#x ~= 8) then return '' end
    local c=0
    for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
    return string.char(c)
  end))
  local picture = createPicture()
  local stream = createStringStream(data)
  picture.loadFromStream(stream)
  return picture
end

if form == nil then
  form = createForm(false)
end

if imgage == nil then
  imgage = createImage(form)
end

if picture == nil then
  picture = createPictureFromBase64(img)
  imgage.setPicture(picture)
  imgage.Height = 280
  imgage.Width = 325
end

form.show()
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites