Skip to content

Commit

Permalink
makes stego easier to use outside frames
Browse files Browse the repository at this point in the history
  • Loading branch information
huettenhain committed Nov 21, 2023
1 parent 0001dcc commit db5de63
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 11 additions & 2 deletions refinery/units/formats/stego.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ class PIXEL_PART(IntEnum):
class stego(Unit):
"""
Decodes the RGBA (red/green/blue/alpha) values of the pixels of a given image file and outputs
these values as bytes. Each row of the image is transformed and output as an individual chunk.
these values as bytes. By default, the pixels are converted left to right, top to bottom.
"""
def __init__(
self,
transpose: Arg.Switch('-t', help='Return the columns of the image rather than the rows.'),
split: Arg.Switch('-m', help='Emit the individual rows or columns as separate outputs.') = False,
parts: Arg('parts', nargs='?', type=str, help=(
'A string containing any ordering of the letters R, G, B, and A (case-insensitive). '
'These pixel components will be extracted from every pixel in the given order. The '
Expand All @@ -29,6 +30,7 @@ def __init__(
):
super().__init__(
transpose=transpose,
split=split,
parts=tuple(Arg.AsOption(p, PIXEL_PART) for p in parts)
)

Expand All @@ -38,12 +40,14 @@ def _image():
return Image

def process(self, data):
split = self.args.split
parts = self.args.parts
image = self._image.open(MemoryFile(data))
if self.args.transpose:
image = image.transpose(self._image.Transpose.ROTATE_90)
width, height = image.size
chunk_size = len(parts)
output = MemoryFile()
buffer = bytearray(chunk_size * width)
for y in range(height):
offset = 0
Expand All @@ -52,4 +56,9 @@ def process(self, data):
next_offset = offset + chunk_size
buffer[offset:next_offset] = (pixel[p] for p in parts)
offset = next_offset
yield buffer
if split:
yield buffer
else:
output.write(buffer)
if not split:
yield output.getvalue()
5 changes: 4 additions & 1 deletion test/units/formats/test_stego.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ def test_simple(self):
'E002000000DCECECB4E2E2F5222299A7A7D9D9D9020202EAFDFDB4DFDFD8FEFE8A567CCFC3DE9AB90000000049454E'
'44AE426082'
)
stego = self.load('RGB')
stego = self.load('RGB', split=True)
self.assertListEqual(expected_rows, list(image | stego))

stego = self.load('RGB', split=False)
self.assertEqual(b''.join(expected_rows), next(image | stego))

0 comments on commit db5de63

Please sign in to comment.