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!
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.)
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.
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
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
None of these buttons put the text of an image in the field?
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?
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.
...
## Hint: BINFILE!
put img 1 into url("binfile:" specialfolderpath("desktop") & "/complete image with image and alpha data.png")
## Hint: BINFILE!
...
Hint: BINFILE!
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
...
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
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
-- 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
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?
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.
...
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)
...
...
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"
...
...
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
...
-- 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
-- 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.
Oh...Thank you so much Klaus and HH!!!!!! That works perfectly both methods. You have made my day!
More beginner questions tomorrow then. Have a good all and I will have a good night