Files
CourseWork/debug_decode.py
Игорь c6ceca94ea работающая стеганография LSB и DCT
я устал... DCT убивает
2026-04-14 11:59:41 +03:00

81 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

cat > debug_decode.py << 'EOF'
print("Отладка decode_dct")
from core.dct import _apply_dct, _extract_bits_from_block, MID_FREQ_INDICES
from core.utils import bits_to_text
from PIL import Image
import numpy as np
# Загружаем закодированное изображение
print("Загружаем out_min.png...")
img = Image.open("out_min.png").convert('L')
pixels = np.array(img, dtype=np.float64)
height, width = pixels.shape
print(f"Размер: {height}x{width}")
# Извлекаем все биты
print("\nИзвлекаем биты из всех блоков...")
all_bits = []
block_count = 0
for i in range(0, height - 8 + 1, 8):
for j in range(0, width - 8 + 1, 8):
block = pixels[i:i+8, j:j+8]
dct_block = _apply_dct(block)
bits_from_block = _extract_bits_from_block(dct_block, len(MID_FREQ_INDICES))
all_bits.extend(bits_from_block)
block_count += 1
if block_count <= 5:
print(f"Блок {block_count}: биты={bits_from_block[:20]}...")
print(f"\nВсего извлечено битов: {len(all_bits)}")
# Покажем первые 50 битов
print(f"\nПервые 50 битов: {all_bits[:50]}")
# Поиск стоп-маркера (8 нулей подряд)
print("\nПоиск стоп-маркера (8 нулей подряд)...")
stop_counter = 0
stop_position = -1
for i, bit in enumerate(all_bits):
if bit == 0:
stop_counter += 1
if stop_counter == 8:
stop_position = i - 7
print(f"Стоп-маркер найден на позиции бита {stop_position}")
break
else:
stop_counter = 0
if stop_position == -1:
print("Стоп-маркер НЕ НАЙДЕН!")
# Покажем где встречаются нули
print("\nПоиск 8 нулей подряд:")
for i in range(len(all_bits) - 7):
if all_bits[i:i+8] == [0,0,0,0,0,0,0,0]:
print(f" Найдено на позиции {i}")
break
else:
print(f"Битов до стоп-маркера: {stop_position}")
bits_to_use = all_bits[:stop_position]
# Преобразуем в байты
print("\nБайты до стоп-маркера:")
for k in range(0, len(bits_to_use), 8):
if k + 8 <= len(bits_to_use):
byte_bits = bits_to_use[k:k+8]
byte_val = 0
for b in byte_bits:
byte_val = (byte_val << 1) | b
print(f" Байт {k//8}: {byte_val:3d} (0x{byte_val:02x}) биты={byte_bits}")
# Декодируем
bits_string = ''.join(str(b) for b in bits_to_use)
try:
decoded = bits_to_text(bits_string)
print(f"\nДекодировано: '{decoded}'")
except Exception as e:
print(f"\nОшибка декодирования: {e}")
EOF