DATA transfer via JPG
Posted: Wed Aug 02, 2017 7:22 pm
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.
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}