Richard Young Claude commited on
Commit
d658499
·
1 Parent(s): 9fd4e71

Fix uint8 overflow in LSB steganography embedding

Browse files

- Convert numpy uint8 to Python int before bitwise operations
- Mask result with 0xFF and explicitly cast back to uint8
- Fixes 'Python integer -2 out of bounds for uint8' error
- Ensures pixel values stay in valid 0-255 range

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

Files changed (1) hide show
  1. steg_embedder.py +4 -2
steg_embedder.py CHANGED
@@ -152,7 +152,8 @@ class StegEmbedder:
152
  break
153
 
154
  # Clear LSBs and set new bits
155
- pixel = flat_array[i]
 
156
  for bit in range(bits_per_channel):
157
  if bit_index >= len(bit_string):
158
  break
@@ -163,7 +164,8 @@ class StegEmbedder:
163
  pixel = pixel | (1 << bit)
164
  bit_index += 1
165
 
166
- flat_array[i] = pixel
 
167
 
168
  # Reshape and save
169
  steg_img_array = flat_array.reshape(img_array.shape)
 
152
  break
153
 
154
  # Clear LSBs and set new bits
155
+ # Convert to Python int to avoid numpy uint8 overflow issues
156
+ pixel = int(flat_array[i])
157
  for bit in range(bits_per_channel):
158
  if bit_index >= len(bit_string):
159
  break
 
164
  pixel = pixel | (1 << bit)
165
  bit_index += 1
166
 
167
+ # Ensure result stays within uint8 range (0-255)
168
+ flat_array[i] = np.uint8(pixel & 0xFF)
169
 
170
  # Reshape and save
171
  steg_img_array = flat_array.reshape(img_array.shape)