picseal_pub/libs/jpg_bin_mem.py

80 lines
1.6 KiB
Python

#
# I tihnk this is support for the website =)
# instead of reading from a file, read from memory
# goal is to avoid reading/writing to the file system
#
from libs.jpg_bin import JpgBin
from libs.jpg_picseal import JpgPicSeal
class JpgBinWriteMem:
#
def __init__(self, jpg_in):
self.fhr = jpg_in
self.mem = none
#
# input is the Crypto Sig class
#
def writeJpgPicSealPub(self, crypto_sig, fp):
self.__writeJpgHeader()
ps = JpgPicSeal(crypto_sig)
ps.writePub(self.fhw)
self.__writeJpgImg(fp)
#
# input is the Crypto Sig class
#
def writeJpgPicSealPvt(self, crypto_sig, fp):
self.__writeJpgHeader()
ps = JpgPicSeal(crypto_sig)
ps.writePvt(self.fhw)
self.__writeJpgImg(fp)
#
def __writeJpgImg(self, fp):
self.__writeJpgMetadata(fp.markers_meta)
self.__writeJpgImgData(fp.markers_img)
self.__writeJpgFooter()
self.fhw.flush()
self.fhw.close()
#
# array of the marker information
#
def __writeJpgMetadata(self, markers):
for marker in markers:
self.fhr.seek(marker.fpos)
data = self.fhr.read(marker.len)
self.fhw.write(data)
self.fhw.flush()
#
# array of the marker information
#
def __writeJpgImgData(self, markers):
for marker in markers:
# most of the image data is sequential
cpos = self.fhr.tell()
if (marker.fpos != cpos):
self.fhr.seek(marker.fpos)
data = self.fhr.read(marker.len)
self.fhw.write(data)
self.fhw.flush()
#
def __writeJpgHeader(self):
self.fhw.write(bytes(JpgBin.soi_marker))
#
def __writeJpgFooter(self):
self.fhw.write(bytes(JpgBin.eof_marker))