Page 1 of 2

DATA transfer via JPG

Posted: Wed Aug 02, 2017 7:22 pm
by Dutchman
Data can be added to JPG-files after the EOI-marker, End Of Image code 'FFD9', as indicated in viewtopic.php?f=20&p=11523#p11523
The following code gives an example.
It uses two functions of the included file "DATA_viaJPG.func": the function 'DATA_storeJPG$(…)' and 'DATA_extract$(…)' for saving or restoring data to or from a JPG-file.
The function 'DATA_storeJPG$(…)' generates a JPG-file to which the data is added.
Data is extracted from that file with the function 'DATA_extractJPG$(…)'
The test-program stores and extracts the explosion soundfile from the "/Examples"-folder.
The function library-file "DATA_viaJPG.func" is attached as "DATA_viaJPG.txt". It should be renamed to "DATA_viaJPG.func" and placed in the folder "./System".
The initial JPG-file "DATA_explosion.wav.JPG" without data, is also attached to show the resulting display.

Code: Select all

'DATA_viaJPG-Example for data transfer via JPG-file
'data is extracted after EOI (End Of Image marker) 'FFD9'
'Updates:
'20170803
'  Error in storage corrected.
'  Test on contents added
'  Function added to generate header-JPG file
20170805
'  Name of input-file is extracted from path-name
'  Default name of extracted file is derived from JPG-Filename
'  Major functions separated into include file
20170812
'  Error handling prepared for multple file processing
'
'==== Filename, complete path for testing
InFile$="/Examples/Music & Sound/files/explosion.wav"
'
'==== Process
OPTION BASE 0 'has no effect
'r'--- store data and get filename
DataFile$=DATA_storeJPG$(InFile$,"")
IF DATA_.Error THEN 'file error
  PRINT!PRINT "=== ERROR ==="
  PRINT DATA_.Msg$
  STOP
ENDIF
'' print info
PRINT "Data stored in """&DATAFile$&"""."
PRINT "Initial JPG-file ('header') contains";DATA_headerJPG.nd;"bytes."
PRINT DATA_storeJPG$.nd;" bytes data stored."
'
'--- extract data
PRINT!PRINT "Extracting data from JPG-file"
'r' extract file and get name
OutFile$=DATA_extractJPG$(DataFile$,"")
IF DATA_.Error THEN 'file error
  PRINT!PRINT "=== ERROR ==="
  PRINT DATA_.Msg$
  STOP
ENDIF
'' print info
PRINT DATA_extractJPG$.nd;" bytes written to """&OutFile$&"""."
PRINT "Extracted filesize """&OutFile$&""" is";FILE_SIZE(OutFile$);" bytes."
'
'==== Test extracted file 
PRINT!PRINT "Original file:"
CALL TestFileData(InFile$)
PRINT!PRINT "Exctracted file:"
CALL TestFileData(OutFile$)
'--- functional test
PRINT!PRINT "Functional test, playing """&OutFile$&"""."
MUSIC 1 LOAD OutFile$
MUSIC 1 PLAY
PAUSE 3
'
'==== delete extracted
FILE OutFile$ DELETE
PRINT "Done."
END

'==== Testfunction
DEF TestFileData(file$)
FILE file$ SETPOS 0
PRINT "Content of """&file$&""":"
FOR i=1 to 5
  FILE file$ READ x
  PRINT "###":x;
NEXT i
PRINT " ..."
END DEF

'r'Include DATA_viaJPG functions
{./System/DATA_viaJPG.func}
DATA_viaJPG.txt
(3.63 KiB) Downloaded 332 times
DATA_explosion.wav.JPG
DATA_explosion.wav.JPG (16.14 KiB) Viewed 3668 times

Re: DATA transfer via JPG

Posted: Wed Aug 02, 2017 7:26 pm
by Mr. Kibernetik
Yes, it can be very useful :D

I just want to correct that EOI is 0xFFD9, not 0xFFD8.

Re: DATA transfer via JPG

Posted: Wed Aug 02, 2017 7:30 pm
by Dutchman
Mr. Kibernetik wrote:
Wed Aug 02, 2017 7:26 pm
Yes, it can be very useful :D

I just want to correct that EOI is 0xFFD9, not 0xFFD8.
Yes, for testing I used the correct decimal value 217. I will change where necessary.

Re: DATA transfer via JPG

Posted: Wed Aug 02, 2017 7:55 pm
by rbytes
This is a very efficient encoding system. Thanks, Dutchman.

For my purposes, I would use a larger jpg that can be imprinted with identifying information. I suspect at some point I will have dozens of such jpg images and will need to be able to tell them apart. :lol:

Re: DATA transfer via JPG

Posted: Wed Aug 02, 2017 7:57 pm
by Mr. Kibernetik
rbytes wrote:
Wed Aug 02, 2017 7:55 pm
This is a very efficient encoding system. Thanks, Dutchman.

For my purposes, I would use a larger jpg that can be imprinted with identifying information. I suspect at some point I will have dozens of such jpg images and will need to be able to tell them apart. :lol:
JPG files can have different names, according to encoded file names. Decoder can use JPG file name to restore data to original file name, like

MYFONT.TTF.JPG -> MYFONT.TTF

Re: DATA transfer via JPG

Posted: Wed Aug 02, 2017 9:01 pm
by rbytes
Yes, I saw that. I am thinking more of the image data files I download to Camera Roll or exchange via Photostream. When looking for one, I only have the visual info as to what is in it.

Re: DATA transfer via JPG

Posted: Wed Aug 02, 2017 10:30 pm
by Dutchman
rbytes wrote:
Wed Aug 02, 2017 9:01 pm
Yes, I saw that. I am thinking more of the image data files I download to Camera Roll or exchange via Photostream. When looking for one, I only have the visual info as to what is in it.
I'm going to try to generate a data-specific JPG file that displays the name of the file. A nice challenge to get back into SB. :D

Re: DATA transfer via JPG

Posted: Thu Aug 03, 2017 12:58 am
by rbytes
Yes, this will be an excellent project and very useful.

Re: DATA transfer via JPG

Posted: Thu Aug 03, 2017 8:46 am
by Dutchman
The code contained a serious error.
The test was done on the inputfile, not the outputfile, so the error was not detected.
The code has been updated.
A test on the file-content has been added.
screenshot.PNG
screenshot.PNG (120.91 KiB) Viewed 3680 times

Re: DATA transfer via JPG

Posted: Thu Aug 03, 2017 2:05 pm
by Dutchman
I have extended the code with a function to generate an application-specific 'JPG-header'-file.
The primary post in this thread has been updated.