#
#
#
class JpgBinWrite:

  soi_marker     = 0xffd8
  eof_marker     = 0xffd9
  picseal_marker = 0xffe0

  #
  def __init__(self, jpg_in, jpg_out):
    self.fhr = jpg_in
    self.fhw = jpg_out

  #
  # input is the Crypto Sig class
  #
  def writeJpgPicSealPub(self, crypto_sig, fp):
    self.__writeJpgHeader()

    # write picseal serialize data

    self.__writeJpgImg(fp)
  
  #
  # input is the Crypto Sig class
  #
  def writeJpgPicSealPvt(self, crypto_sig, fp):
    self.__writeJpgHeader()

    # write picseal serialize data

    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)
    
  #
  # 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)

  #
  def __writeJpgHeader(self):
    self.fhw.write(bytes(JpgBinWrite.soi_marker))

  #
  def __writeJpgFooter(self):
    self.fhw.write(bytes(JpgBinWrite.eof_marker))
