F3.py 2.63 KB
__author__ = 'chunk'


import math
import numpy as np
from msteg import *
from common import *


class F3(StegBase):
    """ This module has two methods: <i>embed_raw_data</i> to embed data
    with the F3 algorithm and <i>extract_raw_data</i> to extract data
    which was embedded previously. """

    def __init__(self):
        """
        Constructor of the F3 class.
        """
        StegBase.__init__(self)

    def embed_raw_data(self, src_cover, src_hidden, tgt_stego):
        """ This method embeds arbitrary data into a cover image.
        The cover image must be a JPEG.

        src_cover - A valid pathname to an image file which serves as cover image
        (the image which the secret image is embedded into).

        src_hidden - A valid pathname to an arbitrary file that is supposed to be
        embedded into the cover image.

        tgt_stego - Target pathname of the resulting stego image. You should save to a
        PNG or another lossless format, because many LSBs don't survive
        lossy compression.
        """
        self.t0 = time.time()
        self._post_embed_actions(src_cover, src_hidden, tgt_stego)

    def extract_raw_data(self, src_steg, tgt_hidden):
        """ This method extracts secret data from a stego image. It is
        (obviously) the inverse operation of embed_raw_data.

        src_stego - A valid pathname to an image file which serves as stego image.

        tgt_hidden - A pathname denoting where the extracted data should be saved to.
        """
        self.t0 = time.time()
        self._post_extract_actions(src_steg, tgt_hidden)

    def _raw_embed(self, cov_data, hid_data):
        """
        cov_data - 4-D numpy.int32 array
        hid_data - 1-D numpy.uint8 array
        """
        hid_data = bytes2bits(hid_data)
        i = 0
        cnt = -1
        for x in np.nditer(cov_data, op_flags=['readwrite']):
            cnt = cnt + 1
            if x == 0 or cnt % 64 == 0: continue

            m = (hid_data[i] & 1)
            if x & 1 != m:
                x[...] -= math.copysign(1, x)
                if x == 0: continue
            i += 1
            if i == hid_data.size: break

        return cov_data,i

    def _raw_extract(self, steg_data, num_bits):
        """
        Just a small helper function to extract hidden data.
        """
        hid_data = np.zeros(num_bits, np.uint8)
        j = 0
        cnt = -1
        for x in np.nditer(steg_data):
            cnt = cnt + 1
            if x == 0 or cnt % 64 == 0: continue
            if j >= num_bits: break
            hid_data[j] = x & 1
            j = j + 1

        return hid_data,j

    def __str__(self):
        return 'F3'