Images in Livecode

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Images in Livecode

Post by jacque » Wed Aug 09, 2017 7:31 am

You don't need to use two sets of image data, either of these will work:

Code: Select all

put url ("binfile:" & tFilePath) into tData 
set the text of image x to tData
Or:

Code: Select all

put tData into image x
ImageData is just the bitmap on screen, it isn't the full binary data so it will never have a real alpha channel. Once you've retrieved the binary data from the file on disk, just "put" it into the image, or set the text of the image to it. That preserves all the binary information. (And yes it's weird, but the "text" of an image is its binary content.)

To move one image into another:

Code: Select all

put image 1 into image 2 
OR
set the text of image 2 to the text of image 1
Sorry you had to go through all that, but I suppose it isn't a bad thing to become familiar with arrays. :-)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Re: Images in Livecode

Post by bidgeeman » Wed Aug 09, 2017 7:48 am

Hello jaque.
I just tried that but it does not work? How do you save out the binary text data of the image in the first place?
Just to be clear I am trying to save out a png as an binary file then reload it as an image.
It's probably my bad coding :) I end up with nothing in the resulting file.

Code: Select all

on mouseUp

   ask file "Save to?"
   if it is empty then   exit mouseUp

   put it into tFile
   if tFile is empty then
      exit mouseUp
   else 
set the text of image "Background" of stack "ImageArea" to tData
 put tData into  URL ("file:" & tFile)
   end if
end mouseUp
Bidge

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Images in Livecode

Post by Thierry » Wed Aug 09, 2017 8:10 am

bidgeeman wrote: set the text of image "Background" of stack "ImageArea" to tData
put tData into URL ("file:" & tFile)
Bidge
Hi,

What happens if you slightly modify these 2 lines ?

Code: Select all

put the text of image "Background" of stack "ImageArea" into tData
 put tData into  URL ("binfile:" & tFile)
HTH,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Re: Images in Livecode

Post by bidgeeman » Wed Aug 09, 2017 8:18 am

Hi Thierrty
Thanks for your input.Nothing at all ends up in the resulting file with your adjusted code?
This is getting so frustrating. I don't know what to do?

I had success with the way I was doing it before using imageData and alphaData but i don't know how to save two data chunks from one image into the one external file and then retrieve them separately so they can be applied to a variable.


Bidge
Attachments
SetText.jpg
None of these buttons put the text of an image in the field?

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Re: Images in Livecode

Post by richmond62 » Wed Aug 09, 2017 11:39 am

I'm probably missing something, BUT . . .

How about "just" importing an image straight into a TEXT field:
IG2.jpg
Here's the original.
Boom Boom.jpg
Although, personally, I think that is a complete waste of time.
IMAGE GAME.livecode.zip
Here's the stack
(7.26 KiB) Downloaded 223 times
The exercise of pushing the text into the empty image ONLY seems to work with JPEGs.
Last edited by richmond62 on Wed Aug 09, 2017 11:57 am, edited 2 times in total.

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Images in Livecode

Post by Klaus » Wed Aug 09, 2017 11:43 am

Richard Gaskin told you that storing image data ( and binary stuff in general) in a FIELD may damage the image data.
I pointed you again to Richards posting and everyone here is telling you to use BINfile for storing binary stuff.

Doesn't that tell you something? 8)

OK, please find attached a working stack that will save and load image and alpha data to and from your desktop.

VERY IMPORTANT HIMNT:
When setting the imagedata of an image it MUST have the same dimensions (height/width) as the original image!
Otherwise you will get scrambled eggs.
Attachments
save_load_img_alpha_data.livecode.zip
(128.41 KiB) Downloaded 231 times

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Images in Livecode

Post by Klaus » Wed Aug 09, 2017 11:58 am

If you do NOT want to mess around with image and alpha data, why not save the complete image into a file? You simply:

Code: Select all

...
## Hint: BINFILE!
put img 1 into url("binfile:" specialfolderpath("desktop") & "/complete image with image and alpha data.png")
## Hint: BINFILE!
...
Hint: BINFILE! :D

When you put something into a field or url("FILE:"...) then LC adds some platform specific line endings which
will render the binary stuff unusable most of the time!

If you need to store some binary stuff right in LC, use a local or global variable or a custom property!
...
set the cImageData of this stack to the imagedata of img 1
set the cAlphaData of this stack to the alphadata of img 1
...

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Re: Images in Livecode

Post by richmond62 » Wed Aug 09, 2017 12:01 pm

Richard Gaskin told you
Who? Me?
Elton_John_-_Don't_Shoot_Me_I'm_Only_the_Piano_Player.jpg
Elton_John_-_Don't_Shoot_Me_I'm_Only_the_Piano_Player.jpg (24.28 KiB) Viewed 7585 times
Come to think of things: have a shot at the guitar player :twisted:

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Images in Livecode

Post by [-hh] » Wed Aug 09, 2017 12:11 pm

The text of an image is binary data, you can't display it correctly in a field.

Usually Thierry's method works and is the best if you wish to use the original data and not the displayed data. But by that there may arise "problems" with using it depending on whether the image is referenced (it has a filename) or the resizeQuality is "best" (what creates an extra alphaChannel) or you changed only the alphadata (masked the image).
The following seems here to be more stable, works in LC 6/7/8/9 without problems (until now).

This summarizes several posts of Klaus here and elsewhere ;-) ;-)

Code: Select all

on importImage
  put "Import an image:" into pp
  answer file pp with the effective filename of this stack titled pp
  if it is not empty then
    put it into fn
    set itemdel to slash; put item -1 of fn into ii
    set itemdel to ","
    if there is no img ii then create img ii -- optionally create invisible
    put url("binfile:"&fn) into img ii
  end if
end importImage

Code: Select all

-- tt is the short name of the image
on exportImage tt
  put "Export an image:" into pp
  put the effective filename of this stack into fn
  set itemdel to "/"
  put tt into last item of fn
  set itemdel to ","
  put "export.png" after fn -- or .jpg etc.
  ask file pp with fn titled pp
  if it is not empty then
    put it into fn
    put img tt into url("binfile:"&fn)
  end if
end exportImage
shiftLock happens

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Re: Images in Livecode

Post by bidgeeman » Wed Aug 09, 2017 12:28 pm

Thank you for all the replies. Klaus....thank you for the stack. I had a good laugh at the comments :)
I managed work out how to save the imageData and the alphaData earlier today but I was wondering if there was a way to save the imageData and the AlphaData into 1 combined binary (other than a .png image) file so they remain separate so can be retrieved from the one file, separated and applied back into variables?

Bidge

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Images in Livecode

Post by [-hh] » Wed Aug 09, 2017 12:48 pm

If you really need that for some reason: This is very simple.

Write _binary_ data:
Write the width of the image binary encoded to the first 4 bytes,
then write the height of the image binary encoded to the next 4 bytes,
then write the alphaData,
then write the imageData.

Read _binary_ data:
decode the first 4 bytes to the width,
then decode the next 4 bytes to the height,
then read the next width x height bytes as alphaData,
then read the rest (= 4 x width x height bytes) as imageData.
shiftLock happens

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Re: Images in Livecode

Post by bidgeeman » Wed Aug 09, 2017 1:01 pm

Thank you HH.
That might be a bit out of my league at the moment .
I'll keep trying my hardest with the binary stuff.

Bidge

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Images in Livecode

Post by Klaus » Wed Aug 09, 2017 1:41 pm

Hi David,

there are a lot of cats and ways to skin these in Livecode! :D
You could:
1.

Code: Select all

...
put specialfolderpath("desktop") & "/img.data" into tFile
put the imagedata of img 1 into tIData
put the alphadata of img 1 into tAData

## Turn binary stuff into TEXT:
put base64encode(tIData) into tIData
put base64encode(tAData) into tAData

## Now make this text a ONE liner:
put urlencode(tIData) into tIData
put urlencode(tAData) into tAData

## Now save as TEXT file:
put tIData & CR & tAData into url("file:" & tFile)
...
To retrieve the data, just go backwards :-)

2. Use a STACK as a container for your data

Code: Select all

...
put specialfolderpath("desktop") & "/img.data" into tFile
put the imagedata of img 1 into tIData
put the alphadata of img 1 into tAData

## Now create a stack on the fly and store the data in a custom property
create invisible stack "img.data"
set the filename of stack "img.data" to tFile
set the cImageData of stack "img.data" to tIData
set the cAlphaData of stack "img.data" to tAData
save stack "img.data"
close stack "img.data"
...
Then you can:

Code: Select all

...
put specialfolderpath("desktop") & "/img.data" into tFile
set the imagedata of img 1 to the cImageData of stack tFile
set the alphadata of img 1 to the cAlphaData of stack tFile
...
Just to show two more ways to tease your poor cat :D

Best

Klaus

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Images in Livecode

Post by [-hh] » Wed Aug 09, 2017 1:52 pm

Here's the small cat (because I said it is very simple).

Code: Select all

-- Write as "special" binary data
on mouseUp
  put specialfolderPath("Documents") into fn
  if not (fn ends with "/") then put "/" after fn -- for use in LC 6/7/8/9
  put ( binaryEncode("I2",the width of img 1,the height of img 1) & \
        the alphaData of img 1 & the imageData of img 1 ) \
        into url("binfile:" & fn &  "test.data")
end mouseUp

Code: Select all

-- Read the "special" binary data
on mouseUp
  put specialfolderPath("Documents") into fn
  if not (fn ends with "/") then put "/" after fn -- for use in LC 6/7/8/9
  put url("binfile:" & fn &"test.data") into tmp
  put binaryDecode("I2",byte 1 to 8 of tmp,tWidth,tHeight) into nirwana
  put byte 9 to 8 + tWidth*tHeight of tmp into tAlphaData
  put byte 9 + tWidth*tHeight to -1 of tmp into tImageData
end mouseUp
Edit. Improved script a little bit.
Last edited by [-hh] on Fri Aug 11, 2017 1:55 pm, edited 1 time in total.
shiftLock happens

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am

Re: Images in Livecode

Post by bidgeeman » Wed Aug 09, 2017 2:02 pm

Oh...Thank you so much Klaus and HH!!!!!! That works perfectly both methods. You have made my day! :D
More beginner questions tomorrow then. Have a good all and I will have a good night ;)

Bidge

Post Reply