DATA transfer via JPG

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

DATA transfer via JPG

Post 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 3671 times
Last edited by Dutchman on Fri Jan 20, 2023 12:10 pm, edited 17 times in total.

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: DATA transfer via JPG

Post by Mr. Kibernetik »

Yes, it can be very useful :D

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

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: DATA transfer via JPG

Post 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.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: DATA transfer via JPG

Post 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:
The only thing that gets me down is gravity...

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: DATA transfer via JPG

Post 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

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: DATA transfer via JPG

Post 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.
The only thing that gets me down is gravity...

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: DATA transfer via JPG

Post 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

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: DATA transfer via JPG

Post by rbytes »

Yes, this will be an excellent project and very useful.
The only thing that gets me down is gravity...

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: DATA transfer via JPG

Post 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 3683 times

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: DATA transfer via JPG

Post 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.

Post Reply