Skip to content

Commit bce68f8

Browse files
committed
Add check on d value
1 parent cafafb9 commit bce68f8

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

Diff for: compressed_image.png

1.48 KB
Loading

Diff for: dct_module.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def dct2_manual(matrix):
1010
sum_val = 0
1111
for x in range(N):
1212
for y in range(N):
13-
sum_val += matrix[x, y] * np.cos(np.pi * u * (2 * x + 1) / (2 * N)) * np.cos(np.pi * v * (2 * y + 1) / (2 * N))
13+
sum_val += matrix[x, y] * np.cos(np.pi * u * (2 * x + 1) / (2 * N)) * \
14+
np.cos(np.pi * v * (2 * y + 1) / (2 * N))
1415
alpha_u = np.sqrt(1/N) if u == 0 else np.sqrt(2/N)
1516
alpha_v = np.sqrt(1/N) if v == 0 else np.sqrt(2/N)
1617
result[u, v] = alpha_u * alpha_v * sum_val
@@ -26,7 +27,8 @@ def idct2_manual(matrix):
2627
for v in range(N):
2728
alpha_u = np.sqrt(1/N) if u == 0 else np.sqrt(2/N)
2829
alpha_v = np.sqrt(1/N) if v == 0 else np.sqrt(2/N)
29-
sum_val += alpha_u * alpha_v * matrix[u, v] * np.cos(np.pi * u * (2 * x + 1) / (2 * N)) * np.cos(np.pi * v * (2 * y + 1) / (2 * N))
30+
sum_val += alpha_u * alpha_v * matrix[u, v] * np.cos(np.pi * u * \
31+
(2 * x + 1) / (2 * N)) * np.cos(np.pi * v * (2 * y + 1) / (2 * N))
3032
result[x, y] = sum_val
3133
return result
3234

Diff for: gui_module.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import threading
99
import queue
1010
import matplotlib.pyplot as plt
11-
from dct_module import compare_dct2_algorithms
11+
from dct_module import compare_dct2_algorithms, dct2_manual
1212

1313
def load_image():
1414
file_path = filedialog.askopenfilename(filetypes=[("BMP files", "*.bmp")])
@@ -28,6 +28,10 @@ def load_image():
2828
try:
2929
F = int(f_entry.get() or 10)
3030
d = int(d_entry.get() or 7)
31+
32+
if d > 2 * F - 2:
33+
messagebox.showerror("Error", f"Invalid value for d. It should be at most 2F-2 ({2*F-2}).")
34+
return
3135
except ValueError:
3236
messagebox.showerror("Error", "Invalid F or d value")
3337
return
@@ -37,8 +41,8 @@ def load_image():
3741
if compressed_file_path:
3842
compressed_size = os.path.getsize(compressed_file_path)
3943
compression_ratio = 100 * (original_size - compressed_size) / original_size
40-
compression_label.configure(text=f"Dimensione originale: {original_size} bytes = {round(original_size / (1024 * 1024), 2) } MB\n"
41-
f"Dimensione compressa: {compressed_size} bytes = {round(compressed_size / (1024 * 1024), 2) }MB\n"
44+
compression_label.configure(text=f"Dimensione originale: {original_size} bytes = {round(original_size / (1024 * 1024), 4) } MB\n"
45+
f"Dimensione compressa: {compressed_size} bytes = {round(compressed_size / (1024 * 1024), 4) }MB\n"
4246
f"Compressione: {compression_ratio:.2f}%")
4347

4448
compressed_image = Image.open(compressed_file_path)
@@ -103,9 +107,13 @@ def test_dct2():
103107
], dtype=np.float32)
104108

105109
result_dct2 = dct2(test_matrix)
110+
result_manualdct2 = dct2_manual(test_matrix)
106111

107112
print("Result of dct2:")
108113
print(result_dct2)
114+
print("Result of the implemented dct2 :")
115+
print(result_manualdct2)
116+
109117

110118
def dct2(matrix):
111119
return dct(dct(matrix.T, norm='ortho').T, norm='ortho')
@@ -129,10 +137,17 @@ def dct2(matrix):
129137
resolution_label = ctk.CTkLabel(dct_frame, text="")
130138
resolution_label.pack(pady=10)
131139

140+
f_label = ctk.CTkLabel(dct_frame, text="Valore di F:")
141+
f_label.pack(pady=5)
132142
f_entry = ctk.CTkEntry(dct_frame)
133143
f_entry.insert(0, "10")
144+
f_entry.pack(pady=5)
145+
146+
d_label = ctk.CTkLabel(dct_frame, text="Valore di d:")
147+
d_label.pack(pady=5)
134148
d_entry = ctk.CTkEntry(dct_frame)
135149
d_entry.insert(0, "7")
150+
d_entry.pack(pady=5)
136151

137152
load_button = ctk.CTkButton(dct_frame, text="Load .bmp image", command=load_image)
138153
file_label = ctk.CTkLabel(dct_frame, text="Nessun file selezionato")
@@ -143,8 +158,6 @@ def dct2(matrix):
143158

144159
img_label = ctk.CTkLabel(image_frame, text="")
145160
compressed_img_label = ctk.CTkLabel(image_frame, text="")
146-
f_entry.pack(pady=10)
147-
d_entry.pack(pady=10)
148161
load_button.pack(pady=10)
149162
file_label.pack(pady=10)
150163
compression_label.pack(pady=10)

0 commit comments

Comments
 (0)