Encapsulated Image in external File

Visuals, audio, animation. Blended, not stirred. If LiveCode is part of your rich media production toolbox, this is the forum for you.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Encapsulated Image in external File

Post by Xero » Fri Jul 12, 2019 11:29 am

Hi all!
Long time no post.
I have been chugging through an app idea for painters that allows:
- An image to be loaded
- A grid to be laid on the image (3x3 etc. or vertical only, or horizontal only, or angled)
- Grid colour and transparency changes
- Copying the image and greyscaling it
- Showing only a section of it (i.e. crop, but retaining whole image, masked away)
- Colour picking of an area (actually a pixel) and displaying a colour swatch for paint matching
- scaling of the final image size so gridlines can be easily transferred to a canvas and image drawn at scale
- File menu with: New, Open, Save, Print and exit.
Just a little app! :lol:
I have managed some good features so far, and am just rounding out the file menu stuff and have come across an issue.

Basically, I need to save a custom file with:
1. The details of how the image is displayed (size, location)
2. The information to draw any gridlines (points, colour)
3. The information to place any measurements (location and details)
4. The image itself.
So... I am writing a custom file script that is saving perfectly well. It writes the above, in the same order, line-by line with text. This should encapsulate all of the information needed to resume any work wherever you left off. This is saving OK, showing everything I need in the place that I need it
On opening the file, I can extract the first 3 sections perfectly well, and can display the image size and location, and any gridlines and measurements perfectly.
The problem is coming with the image.
I am using this method to save: put cr & the imagedata of img "Imagename" after tWriteData (tWriteData then becomes the contents of the file.)
Effectively writing the imagedata at the end of the file to be recovered later.
On opening, I make sure the image is the same size as it was when the imagedata was copied (I have also tried the original unscaled image size) and then set the imagedata of a dummy image onscreen to the last section of the custom file (i.e. the imagedata that was copied upon saving).
When I do this, I get a wierd deinterlaced image (The image I saved, but with lines through it)- but ONLY when using a colour image. When I use a B&W image, it comes in perfectly!
Is there something I can do to get the colour imagedata copied into the custom file so I can recover it? I'd like to have colour and B&W capabilities.
My other option is the just use the filename, and effectively "reference" the image rather than encapsulate it in the file. This has the drawback of: if you move the image file, you lose the reference... so I don't want to do it. This method does, however, work with colour and B&W.
Thankyou in advance brainstrust!
Xero

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

Re: Encapsulated Image in external File

Post by [-hh] » Fri Jul 12, 2019 4:50 pm

You could use for writing out:

Code: Select all

put cr & base64Encode(the text of img "ïmagename") after tWriteData
and then use for reading in:

Code: Select all

set text of img "ïmagename" to base64Decode(line x to -1 of tWriteData)
shiftLock happens

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Encapsulated Image in external File

Post by jacque » Fri Jul 12, 2019 4:53 pm

Are you saving the file as binary (binfile)?

I'd be tempted to use an array for each section and then arrayEncode it and save it as binfile, but that's just me. hh's suggestion does convert it to ascii which doesn't require binary.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Encapsulated Image in external File

Post by Xero » Sat Jul 13, 2019 5:23 am

Thanks -hh and jacque. I will give the base64encode a go when I next get on to it. I have tried "the text of img "Imagename" but that didn't save anything into the file. Only the imagedata yielded something into the file.
I'll see how it goes.
As for the binary file... I don't think I am saving as a binary file. For all intents, I am saving a text file (.txt) but calling it .acf. Opening the file in a text editor will produce a text file. I did that because I need to just simply recall the first 3 sections (i.e. put line 1 of file into pOriginalsize, put item 1 of line 5 into pLinePoints, etc.)

It interests me that the colour files are the problem. When I look at the file, the B&W files are a chunk of text, and the colour ones are scattered lines, including blank lines. I am wondering if it has to do with the channel (Alpha, R,G,B) data. My understanding is that greyscale still has R,G & B values...

One of the images looks like there is an overlap of the pixels, as if the number of pixels out numbers the size of the image, and it runs on to the next line. But the colours are off as well, so it might be putting extra pixel data...

I've attached images to show what I mean. Both of these use the imagedata of img, written into text file...
Attachments
ColourNotWorking.jpg
BWWorking.jpg

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Encapsulated Image in external File

Post by jacque » Sat Jul 13, 2019 6:19 am

Images are always binary data. The blank lines and spaces you saw are non-printable characters that are part of the image (you can see a representation of those if you open an image file in a text editor that shows invisible characters.) I'm not sure why grayscale images appear to be okay but they won't be hurt by saving the file as binary, and may appear better.

You can use either of LC's two ways to deal with binary files:

Code: Select all

put  the text of img "Imagename" into tData 
open file myFile for binary update
write tData to file myFile - - or before/after myFile 
close file myFile 

Or:
put tData into url ("binfile://" & myFile)
But if you're more comfortable with plain text then base64encode works fine too.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Encapsulated Image in external File

Post by Xero » Sat Jul 13, 2019 9:26 am

Using "The text of the image" yields no data in the file, with or without base64Encode.

Using "the imagedata of the image" and Base64Encode of colour image yields no data written into the file. In a B&W image, the file has data.
Consequently, B&W works, Colour doesn't.

I tried a couple of different methods of encoding text (textEncode) and no data was written into the file

Got me stumped!

I went back to my original code (using imagedata) and used a couple of different images, and the colour images worked. But the first image I used didn't work! I also used a version of the non-working colour image that I saved through GIMP, and that worked. Seems like the jpg saved from the web doesn't work, but the same jpg run through GIMP
Maybe there is something in the formatting of the data for certain images?
Any thoughts?
Xero

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

Re: Encapsulated Image in external File

Post by [-hh] » Sat Jul 13, 2019 11:35 am

Xero wrote:Using "The text of the image" yields no data in the file, with or without base64Encode.
I use base 64 encoding/decoding of images without problems all day here. This is "the" way to use images from LC with HTML5.

But there may be problems with the file read/write procedure you use. With your method you will possibly get different problems on different platforms because you mix binary data into a text file.

Also the text of images (which uses native encoding) needs a "refresh" for referenced images (images with a filename). You said you don't use referenced images ...

So please try (iName is the name of the image):

Writing:

Code: Select all

-- next two lines needed for referenced images only
  set the alphadata of img iName to the alphadata of img iName
  set the imagedata of img iName to the imagedata of img iName
-- encode binary data
  put base64Encode(the text of img iName) into bData
  replace linefeed with empty in bData -- now we have one line of data
  put cr & bData after tWrite
Reading:

Code: Select all

-- decode binary data
  set the text of img iName to base64Decode(line -1 of tWrite)
[If you use imagedata only for Read/Write you'll get problems with images of resizequality "best" that have (internally) a separate alpha channel or with images that come from the clipboard.]

In case you plan to make a standalone then you should also do once (e.g. on openstack)

Code: Select all

set the paintcompression to PNG
Before you have more questions you should please give first an info about your read/write procedures, the paintcompression you use, is the image referenced or not, the resizeQuality of your image. Also give an info about the OS you use and the LC version. It's hard to guess all that from the given problems.
shiftLock happens

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Encapsulated Image in external File

Post by Xero » Sat Jul 13, 2019 3:39 pm

I am using Windows 10 and Livecode Community 9.0.4.
I haven't changed the paintcompression.
I don't want the image referenced (I can get it to work with referenced images), because it's easy to lose data if the referenced image file is moved or deleted...
I hope that helps with context...
I am using the following code to save the file:

Code: Select all

   
   --write some info about the rest of the file, all in simple text (This works fine)
   put cr & the imagedata of image "Image" of stack "Image" after tWriteFileData
   ask file "Save to?"  with type "ACF Files|acf"
   put it into tFile
   if tFile is empty then 
      exit saveFile
   else
      if NOT (tFile ends with ".acf") THEN
         put ".acf" after tFile
      end if
      put tWriteFileData into URL ("file:" & tFile)
   end if
end SaveFile
I am then using this code to open the file and read it:

Code: Select all

answer file "An ACF file" with type ("ACF files|acf" & return & "all files|*|*")
   if it <> "" then
      put it into tFileName
      put URL ("file:" & tFileName) into tOpenFileData
   else
      beep
   end if
   repeat with y= 6 to (the number of lines of tOpenFileData) -- This is the location of the image data in the file--
      put cr& line y of tOpenFileData after tOpenImageData
   end repeat
   delete line 1 of tOpenImageData
-- set the height and width of the image as it was when the imagedata was saved--
   set the imagedata of Image "Image" of stack "Image" to tOpenImageData
One of the problems I am having is when I use "the text of the image", nothing gets written into the file. When I use "the imagedata of the image", I get something written into the file. When I use base64encode on the text or imagedata, I get nothing written in the file.

I have tried changing the paintcomression, and even trying to change the resizequality... no fix sadly. I might also work out what files don't work, and have a look at their properties and see if anything pops out as being different to the ones that do work.

Anyway, thanks for the advice, and I will keep plugging away!
Xero

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

Re: Encapsulated Image in external File

Post by [-hh] » Sat Jul 13, 2019 5:04 pm

The code I gave above works here on Win 10 using LC 8/9.

The only possibility that base64Encode(the imagedata of image iName) is empty is that the imageData of image iName is empty.

So once again, as you insist on using imageData only and you say the image that you are going to save(=image "Image") is NOT a referenced image then you could script when writing out:

Code: Select all

put base64Encode(the imageData of image "Image" of stack "Image") into bData
replace linefeed with empty in bData
put cr & bData after tWriteFileData
and when reading in:

Code: Select all

set the imageData of image "Image" of stack "Image" to \
     base64Decode(line -1 of tOpenImageData)
This works here tested inserted into your code on Win 10 using LC 8.1.10 and 9.0.2 and 9.5.0-dp1.
shiftLock happens

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Encapsulated Image in external File

Post by jacque » Sat Jul 13, 2019 5:08 pm

As far as I know, referenced images are the only way the text would be empty. Did you import the image using the Import Control item in the File menu?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Encapsulated Image in external File

Post by [-hh] » Sat Jul 13, 2019 5:13 pm

As I said above, the text of an image, whether referenced or not, is not empty after using:

Code: Select all

-- next two lines needed for referenced images only
  set the alphadata of img iName to the alphadata of img iName
  set the imagedata of img iName to the imagedata of img iName
shiftLock happens

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Encapsulated Image in external File

Post by jacque » Sat Jul 13, 2019 5:46 pm

[-hh] wrote:
Sat Jul 13, 2019 5:13 pm
As I said above, the text of an image, whether referenced or not, is not empty after using:

Code: Select all

-- next two lines needed for referenced images only
  set the alphadata of img iName to the alphadata of img iName
  set the imagedata of img iName to the imagedata of img iName
Right, I was asking the OP who says his images are not referenced. I should have included @.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Encapsulated Image in external File

Post by jacque » Sat Jul 13, 2019 5:54 pm

@Xero, what do you get if you put this into the message box?

Code: Select all

put  the text of img "Imagename"
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Encapsulated Image in external File

Post by Xero » Sun Jul 14, 2019 5:26 am

Thanks Jacque and -hh,
I have played with quite a few images now, and it seems that the two I first used seem to be the only ones that are having issues. They seem to be the exception rather than the rule.
For what I need, I think I can accept that those couple of images just won't work, but the bulk of images will.
Thanks for your efforts and advice.
Much appreciated, I have learned a lot.
Xero

Post Reply

Return to “Multimedia”