-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjpeg_decoder.py
More file actions
1805 lines (1498 loc) · 82.1 KB
/
jpeg_decoder.py
File metadata and controls
1805 lines (1498 loc) · 82.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
from collections import deque, namedtuple
from itertools import product
from math import ceil, cos, pi
from scipy.interpolate import griddata
from typing import Callable, Tuple, Union
from pathlib import Path
# JPEG markers (for our supported segments)
SOI = bytes.fromhex("FFD8") # Start of image
SOF0 = bytes.fromhex("FFC0") # Start of frame (Baseline DCT)
SOF2 = bytes.fromhex("FFC2") # Start of frame (Progressive DCT)
DHT = bytes.fromhex("FFC4") # Define Huffman table
DQT = bytes.fromhex("FFDB") # Define quantization table
DRI = bytes.fromhex("FFDD") # Define restart interval
SOS = bytes.fromhex("FFDA") # Start of scan
DNL = bytes.fromhex("FFDC") # Define number of lines
EOI = bytes.fromhex("FFD9") # End of image
# Restart markers
RST = tuple(bytes.fromhex(hex(marker)[2:]) for marker in range(0xFFD0, 0xFFD8))
# Containers for the parameters of each color component
ColorComponent = namedtuple("ColorComponent", "name order vertical_sampling horizontal_sampling quantization_table_id repeat shape")
HuffmanTable = namedtuple("HuffmanTable", "dc ac")
class JpegDecoder():
def __init__(self, file:Path) -> None:
# Open file
with open(file, "rb") as image:
self.raw_file = image.read()
self.file_size = len(self.raw_file) # Size (in bytes) of the file
self.file_path = file if isinstance(file, Path) else Path(file)
# Check if file is a JPEG image
# (The file needs to start with bytes 'FFD8FF')
if not self.raw_file.startswith(SOI + b"\xFF"):
raise NotJpeg("File is not a JPEG image.")
print(f"Reading file '{file.name}' ({self.file_size:,} bytes)")
# Handlers for the markers
self.handlers = {
DHT: self.define_huffman_table,
DQT: self.define_quantization_table,
DRI: self.define_restart_interval,
SOF0: self.start_of_frame,
SOF2: self.start_of_frame,
SOS: self.start_of_scan,
EOI: self.end_of_image,
}
# Initialize decoding paramenters
self.file_header = 2 # Offset (in bytes, 0-index) from the beginning of the file
self.scan_finished = False # If the 'end of image' marker has been reached
self.scan_mode = None # Supported modes: 'baseline_dct' or 'progressive_dct'
self.image_width = 0 # Width in pixels of the image
self.image_height = 0 # Height in pixels of the image
self.color_components = {} # Hold each color component and its respective paramenters
self.sample_shape = () # Size to upsample the subsampled color components
self.huffman_tables = {} # Hold all huffman tables
self.quantization_tables = {} # Hold all quantization tables
self.restart_interval = 0 # How many MCUs before each restart marker
self.image_array = None # Store the color values for each pixel of the image
self.scan_count = 0 # Counter for the performed scans
# Main loop to find and process the supported file segments
"""NOTE
We are sequentially looking for markers on the file. Once a recognized
marker is found, control is passed to a method to handle it. The method
then gives the control back to the main loop, which continues from where
the method stopped.
Each marker begins with 0xFF. If this byte is followed by a 0x00, then
it is escaped.
If the marker isn't recognized, then its data segment is just skipped.
"""
while not self.scan_finished:
try:
current_byte = self.raw_file[self.file_header]
except IndexError:
del self.raw_file
break
# Whether the current byte is 0xFF
if (current_byte == 0xFF):
# Read the next byte
my_marker = self.raw_file[self.file_header : self.file_header+2]
self.file_header += 2
# Whether the two bytes form a marker (and isn't a restart marker)
if (my_marker != b"\xFF\x00") and (my_marker not in RST):
# Attempt to get the handler for the marker
my_handler = self.handlers.get(my_marker)
my_size = bytes_to_uint(self.raw_file[self.file_header : self.file_header+2]) - 2
self.file_header += 2
if my_handler is not None:
# If a handler was found, pass the control to it
my_data = self.raw_file[self.file_header : self.file_header+my_size]
my_handler(my_data)
else:
# Otherwise, just skip the data segment
self.file_header += my_size
else:
# Move to the next byte if the current byte is not 0xFF
self.file_header += 1
def start_of_frame(self, data:bytes) -> None:
"""Parse the information on the Start of Frame segment: scan mode,
image dimensions, color space, sampling, quantization tables used."""
data_size = len(data)
data_header = 0
"""NOTE
The byte structure of the segment (in order):
- 2 bytes: length of the segment
- 1 byte: sample precision
- 1 byte: image height
- 1 byte: image width
- 1 byte: amount of color components
- For each color component:
- 1 byte: ID of the component
- 4 bits: horizontal sample
- 4 bits: vertical sample
- 1 byte: ID of the quantization table used on the component
If there are 3 color components, the image is considered to be in the YCbCr
color space. The first component of the segment is Y, the next Cb, and the
last is Cr.
If there is a single component, then the image is considered to be greyscale.
"""
# Check encoding mode
# (the marker used for the segment determines the scan mode)
mode = self.raw_file[self.file_header-4 : self.file_header-2]
if mode == SOF0:
self.scan_mode = "baseline_dct"
print("Scan mode: Sequential")
elif mode == SOF2:
self.scan_mode = "progressive_dct"
print("Scan mode: Progressive")
else:
raise UnsupportedJpeg("Encoding mode not supported. Only 'Baseline DCT' and 'Progressive DCT' are supported.")
# Check sample precision
# (This is the number of bits used to represent each color value of a pixel)
precision = data[data_header]
if precision != 8:
raise UnsupportedJpeg("Unsupported color depth. Only 8-bit greyscale and 24-bit RGB are supported.")
data_header += 1
# Get image dimensions
self.image_height = bytes_to_uint(data[data_header : data_header+2])
data_header += 2
"""NOTE
If the height is specified as zero here, then it means that the height value
is going to be specidied on the DNL segment after the first scan.
This is for the case when the final height is unknown when the image is
being created, for example when a scanner is generating the image.
"""
self.image_width = bytes_to_uint(data[data_header : data_header+2])
data_header += 2
print(f"Image dimensions: {self.image_width} x {self.image_height}")
if self.image_width == 0:
raise CorruptedJpeg("Image width cannot be zero.")
# Check number of color components
components_amount = data[data_header]
if components_amount not in (1, 3):
if components_amount == 4:
raise UnsupportedJpeg("CMYK color space is not supported. Only RGB and greyscale are supported.")
else:
raise UnsupportedJpeg("Unsupported color space. Only RGB and greyscale are supported.")
data_header += 1
if components_amount == 3:
print("Color space: YCbCr")
else:
print("Color space: greyscale")
# Get the color components and their parameters
components = (
"Y", # Luminance
"Cb", # Blue chrominance
"Cr", # Red chrominance
)
try:
for count, component in enumerate(components, start=1):
# Get the ID of the color component
my_id = data[data_header]
data_header += 1
# Get the horizontal and vertical sampling of the component
sample = data[data_header] # This value is 8 bits long
horizontal_sample = sample >> 4 # Get the first 4 bits of the value
vertical_sample = sample & 0x0F # Get the last 4 bits of the value
data_header += 1
# Get the quantization table for the component
my_quantization_table = data[data_header]
data_header += 1
# Group the parameters of the component
my_component = ColorComponent(
name = component, # Name of the color component
order = count-1, # Order in which the component will come in the image
horizontal_sampling = horizontal_sample, # Amount of pixels sampled in the horizontal
vertical_sampling = vertical_sample, # Amount of pixels sampled in the vertical
quantization_table_id = my_quantization_table, # Quantization table selector
repeat = horizontal_sample * vertical_sample, # Amount of times the component repeats during decoding
shape = (8*horizontal_sample, 8*vertical_sample), # Dimensions (in pixels) of the MCU for the component
)
# Add the component parameters to the dictionary
self.color_components.update({my_id: my_component})
# Have we parsed all components?
if count == components_amount:
break
except IndexError:
raise CorruptedJpeg("Failed to parse the start of frame.")
# Shape of the sampling area
# (these values will be used to upsample the subsampled color components)
sample_width = max(component.shape[0] for component in self.color_components.values())
sample_height = max(component.shape[1] for component in self.color_components.values())
self.sample_shape = (sample_width, sample_height)
# Display the samplings
print(f"Horizontal sampling: {' x '.join(str(component.horizontal_sampling) for component in self.color_components.values())}")
print(f"Vertical sampling : {' x '.join(str(component.vertical_sampling) for component in self.color_components.values())}")
# Move the file header to the end of the data segment
self.file_header += data_size
def define_huffman_table(self, data:bytes) -> None:
"""Parse the Huffman tables from the file.
"""
data_size = len(data)
data_header = 0
"""NOTE
The Huffman tables are used to compress and decompress the image data.
For an explanantion of how they work in general:
https://www.youtube.com/watch?v=NjhJJYHpYsg
A JPEG image has up to 4 of Huffman tables: 2 for luminance and 2 for chrominance.
In the context of a JPEG image, these are the very basics of where those tables come from:
- When an image is encoded to JPEG, it is divided in blocks of 8x8 pixels.
- The luminance and chrominance values of those pixels are stored on an 8x8 matrix,
for each block.
- The top left value of each matrix is called DC, and all other values AC.
- When compressing the values, all the DC values are grouped and compressed
separately from the AC values.
- So the 4 tables refer to:
- Luminance DC
- Luminance AC
- Chrominance DC
- Chrominance AC
Here is a more in-depth explanation:
https://www.impulseadventure.com/photo/jpeg-huffman-coding.html
In progressive JPEG, a Huffman table might be overwritten by a new one after
a scan. Thus allowing the encoder to create tables that are optimized for the
next scan.
"""
# Get all huffman tables on the data
"""NOTE
Each Huffman table begins with the 0xFFC4 marker, followed by two bytes that
indicate the lenght (in bytes) of the section, and then 1 byte to indicate
the destination that the table refers to.
The lower nibble of the destination is the ID of the table, and the upper
nibble is if the table is for DC values (0x0) or AC values (0x1).
"""
while (data_header < data_size):
table_destination = data[data_header]
data_header += 1
# Count how many codes of each length there are
"""NOTE
Then the next 16 bytes following the destination indicate the bit lenghts
of the elements stored on the table: first byte of this sequence is the amount
of elements that are 1 bit long, second byte the amount of elements 2 bits long,
and so on.
"""
codes_count = {
bit_length: count
for bit_length, count
in zip(range(1, 17), data[data_header : data_header+16])
}
data_header += 16
# Get the Huffman values (HUFFVAL)
"""NOTE
The bytes following the lengths are the values themselves in order of
increasing bit-length.
"""
huffval_dict = {} # Dictionary that associates each code bit-length to all its respective Huffman values
for bit_length, count in codes_count.items():
huffval_dict.update(
{bit_length: data[data_header : data_header+count]}
)
data_header += count
# Error checking
if (data_header > data_size):
# If we tried to read more bytes than what the data has, then something is wrong with the file
raise CorruptedJpeg("Failed to parse Huffman tables.")
# Build the Huffman tree
"""NOTE
A Huffman tree starts from a root node (depth 0), and each node has two
nodes bellow it (depths 1, 2, 3, ...). On a JPEG file, the tree goes up
to depth 16.
The tree contains elements that appear on the original data (before
compression). Elements are assigned to some nodes in a way that the most
common elements take shorter to navigate to than the least common. The
path used to navigate throug the tree is the codeword that is stored
pn the compressed data
To navigate through the tree: starting from the root node, if you go to
the left node you append the value of 0 to the codeword, and if you go
right you append the value of 1. When you get to a non-empty node you
stop, the value contained on the node is the value that the codeword
represents. The amount of steps you took to get to the element is the
bit-length of the codeword.
The JPEG file stores the amount of elements of each bit-length, which
we have already extracted to self.huffman_tables. In order to build the
tree from this data:
1. Starting from the root node (depth 0), add two empty nodes (depth 1).
2. Depth 1: the empty nodes are filled from left to right with the
elements bit-length 1.
3. To the remaing empty nodes (depth 1) add 2 nodes to each (creating
depth 2)
4. Steps 2 and 3 are repeated, with depth N being filled with elements
of bit-depth N.
5. The process stops when all elements are used
Our Huffman tree will be stored in a Python dictionary. It associates the
codeword (as a string containg only '0' and '1') with its respective value.
"""
huffman_tree = {}
code = 0
for bit_length, values_list in huffval_dict.items():
code <<= 1
for huffval in values_list:
code_string = bin(code)[2:].rjust(bit_length, "0")
huffman_tree.update({code_string: huffval})
code += 1
# Add tree to the Huffman table dictionary
self.huffman_tables.update({table_destination: huffman_tree})
print(f"Parsed Huffman table - ", end="")
print(f"ID: {table_destination & 0x0F} ({'DC' if table_destination >> 4 == 0 else 'AC'})")
"""NOTE
Depending on the encoder, all the Huffman tables might be in the same
segment of the file, or each table can be in its own segment.
If the tables are in the same segment, then the bytes of the next table
immediatelly follows the bytes of the previous one. The order remains
the same: destination, lengths, values.
"""
# Move the file header to the end of the data segment
self.file_header += data_size
def define_quantization_table(self, data:bytes) -> None:
"""Parse the quantization table from the file.
"""
data_size = len(data)
data_header = 0
"""NOTE
The color values of a JPEG image are not stored directly, but rather as
a table of frequencies (DCT coefficients). The quantization table is used
to "cut" those coefficients deemed "unecessary" for how the human eye
will perceive the image.
The quantization table is a 8 x 8 matrix. The quantization is the lossy
step of the JPEG encoding. The image is broken in blocks of 8 x 8 pixels.
The DCT coefficients of the block are calculated, and then they are
divided element-wise by the quantization table. The results are rounded
to the nearest integer.
The quantization essentially decreases the resolution of the coefficients.
It assumes that the human eye cannot perceive quick variation of details
within a small area. The coefficients closer to the top left have a bigger
imact on how the eye will perceive the image, because they represent
smaller frequencies of details (smaller frequency means larger wavelength),
which the eye should perceive them better.
It is worth noting that the smaller DCT coefficients are going to become 0.
That will be the case of most, if not all, of the coefficients of the lower
right section of the block. This large amount of repeated values make
the data to be better compressed.
The smaller values of the other coefficients also aids in compression,
because those values can be represented using less bits.
IMPORTANT: The 8 x 8 block of quantized coefficients are stored in zig-zag
order, starting from the top left. This makes the zero values to be mostly
grouped together in the end of the sequence, which helps with compression.
The sequence is (from 0 to 63):
0 1 5 6 14 15 27 28
2 4 7 13 16 26 29 42
3 8 12 17 25 30 41 43
9 11 18 24 31 40 44 53
10 19 23 32 39 45 52 54
20 22 33 38 46 51 55 60
21 34 37 47 50 56 59 61
35 36 48 49 57 58 62 63
"""
# Get all quantization tables on the data
while (data_header < data_size):
table_destination = data[data_header]
data_header += 1
"""NOTE
The quantization table segments on the file begin with the marker 0xFFDB.
The next two bytes represent the length of the segment. The next byte is
the ID of the table. And the 64 bytes afterwards are the 64 values of
the quantization table in zig-zag order.
"""
# Get the 64 values of the 8 x 8 quantization table
qt_values = np.array([value for value in data[data_header : data_header+64]], dtype="int16")
try:
quantization_table = undo_zigzag(qt_values)
except ValueError:
raise CorruptedJpeg("Failed to parse quantization tables.")
data_header += 64
# Add the table to the quantization tables dictionary
self.quantization_tables.update({table_destination: quantization_table})
print(f"Parsed quantization table - ID: {table_destination}")
"""NOTE
Each quantization table can come each in its own segment on the file.
Or all tables in the same segment, one imediately after the other,
following the same byte structure (ID, then the 64 values).
"""
# Move the file header to the end of the data segment
self.file_header += data_size
def define_restart_interval(self, data:bytes) -> None:
"""Parse the restart interval value."""
self.restart_interval = bytes_to_uint(data[:2])
self.file_header += 2
print(f"Restart interval: {self.restart_interval}")
"""NOTE
The JPEG standart allow to restart markers to be added to the encoded image data.
Those are meant to aid in error correction. The restart markers, when present,
are added each a certain amount of MCUs. This amount is specified in the
"Define Restart Interval" (DRI) segment, which starts after the 0xFFDD marker.
The restart markers are the bytes from 0xFFD0 to 0xFFD7. They are used sequentially,
and wrap back to 0xFFD0 after 0xFFD7.
It is worth noting that the MCUs encoded on the data stream are not necessarily
aligned to the byte boundary (8-bits). So after reaching the amount of MCUs specified
on the restart interval, it is necessary to move the bits header to the begining of
the next byte, by taking the modulo of the position,if the header isn't already there:
if (header_position % 8) != 0:
header_position += 8 - (header_position % 8)
header_position += 16
We also need to jump the marker itself, which is 16-bits long. So we also added 16
to the header position.
It is worth noting that the restart interval can be defined again after a scan.
The latest defined value is what counts for each scan.
"""
def start_of_scan(self, data:bytes) -> None:
"""Parse the information necessary to decode a segment of encoded image data,
then passes this information to the method that handles the scan mode used."""
data_size = len(data)
data_header = 0
"""NOTE
The structure of the Start of Scan header is (in order):
- 2 bytes: length of the segment
- 1 byte: amount of color components in the current scan
- For each color component in the scan:
- 1 byte: ID of the component
- 4 bits: ID of the Huffman table for DC values of the component
- 4 bits: ID of the Huffman table for AC values of the component
- 1 byte: Start of the spectral selection
- 1 byte: End of the spectral selection
- 4 bits: Successive approximation (high)
- 4 bits: Successive approximation (low)
Note: Spectral selection and successive approximation are relevant for
the progressive scan. They have no meaning for baseline scan.
"""
# Number of color components in the scan
components_amount = data[data_header]
data_header += 1
# Get parameters of the components in the scan
my_huffman_tables = {}
my_color_components = {}
for component in range(components_amount):
component_id = data[data_header] # Should match the component ID's on the 'start of frame'
data_header += 1
# Selector for the Huffman tables
tables = data[data_header]
data_header += 1
dc_table = tables >> 4 # Should match the tables ID's on the 'detect huffman table'
ac_table = (tables & 0x0F) | 0x10
"""NOTE
The ID of the AC tables is a byte value which the first hexadecimal digit is 1.
Usually: 0x10 and 0x11 (16 and 17, in decimal).
On the other hand, the ID of the DC tables begin with hexadecimal digit 0.
Usually: 0x00 and 0x01.
"""
# Store the parameters
my_huffman_tables.update({component_id: HuffmanTable(dc=dc_table, ac=ac_table)})
my_color_components.update({component_id: self.color_components[component_id]})
# Get spectral selection and successive approximation
if self.scan_mode == "progressive_dct":
spectral_selection_start = data[data_header] # Index of the first values of the data unit
spectral_selection_end = data[data_header+1] # Index of the last values of the data unit
bit_position_high = data[data_header+2] >> 4 # The position of the last bit sent in the previous scan
bit_position_low = data[data_header+2] & 0x0F # The position of the bit sent in the current scan
"""NOTE
The data unit is formed by blocks of 8 x 8 pixels (64 values in total, indexed from 0 to 63).
The progressive scan breaks the values in different scans. And the bits of those values can
also be broken in separate scans. It is up to the encoder to decide how to split the data.
After all scans, it should be possible to reconstruct all values of all data units.
"""
data_header += 3
# Move the file header to the begining of the entropy encoded segment
self.file_header += data_size
# Define number of lines
if self.image_height == 0:
dnl_index = self.raw_file[self.file_header:].find(DNL)
if dnl_index != -1:
dnl_index += self.file_header
self.image_height = bytes_to_uint(self.raw_file[dnl_index+4 : dnl_index+6])
else:
raise CorruptedJpeg("Image height cannot be zero.")
# Dimensions of the MCU (minimum coding unit)
"""NOTE
If there is only one color component in the scan, then the MCU size is always 8 x 8.
If there is more than one color component, the MCU size is determined by the component
with the highest resolution (considering all the components of the image, not only the
components in the scan).
"""
if components_amount > 1:
self.mcu_width:int = 8 * max(component.horizontal_sampling for component in self.color_components.values())
self.mcu_height:int = 8 * max(component.vertical_sampling for component in self.color_components.values())
self.mcu_shape = (self.mcu_width, self.mcu_height)
else:
self.mcu_width:int = 8
self.mcu_height:int = 8
self.mcu_shape = (8, 8)
# Amount of MCUs in the whole image (horizontal, vertical, and total)
"""NOTE
If the image dimensions are not multiples of the MCU dimensions, then
then the image right and bottom borders are padded with enough pixels
to fit a full MCU (normally by just repeating the pixels on the borders).
A JPEG decoder will just disregard those padding pixels when rendering
the image.
"""
if components_amount > 1:
self.mcu_count_h = (self.image_width // self.mcu_width) + (0 if self.image_width % self.mcu_width == 0 else 1)
self.mcu_count_v = (self.image_height // self.mcu_height) + (0 if self.image_height % self.mcu_height == 0 else 1)
else:
component = my_color_components[component_id]
sample_ratio_h = self.sample_shape[0] / component.shape[0]
sample_ratio_v = self.sample_shape[1] / component.shape[1]
layer_width = self.image_width / sample_ratio_h
layer_height = self.image_height / sample_ratio_v
self.mcu_count_h = ceil(layer_width / self.mcu_width)
self.mcu_count_v = ceil(layer_height / self.mcu_height)
self.mcu_count = self.mcu_count_h * self.mcu_count_v
# Create the image array (if one does not exist already)
if self.image_array is None:
# 3-dimensional array to store the color values of each pixel on the image
# array(x-coordinate, y-coordinate, RBG-color)
count_h = (self.image_width // self.sample_shape[0]) + (0 if self.image_width % self.sample_shape[0] == 0 else 1)
count_v = (self.image_height // self.sample_shape[1]) + (0 if self.image_height % self.sample_shape[1] == 0 else 1)
self.array_width = self.sample_shape[0] * count_h
self.array_height = self.sample_shape[1] * count_v
self.array_depth = len(self.color_components)
self.image_array = np.zeros(shape=(self.array_width, self.array_height, self.array_depth), dtype="int16")
# Setup scan counter
if self.scan_count == 0:
self.scan_amount = self.raw_file[self.file_header:].count(SOS) + 1
print(f"Number of scans: {self.scan_amount}")
# Begin the scan of the entropy encoded segment
if self.scan_mode == "baseline_dct":
self.baseline_dct_scan(my_huffman_tables, my_color_components)
elif self.scan_mode == "progressive_dct":
self.progressive_dct_scan(
my_huffman_tables,
my_color_components,
spectral_selection_start,
spectral_selection_end,
bit_position_high,
bit_position_low
)
else:
raise UnsupportedJpeg("Encoding mode not supported. Only 'Baseline DCT' and 'Progressive DCT' are supported.")
def bits_generator(self) -> Callable[[int, bool], str]:
"""Returns a function that fetches the bits values in order from the raw file.
"""
bit_queue = deque()
# This nested function "remembers" the contents of bit_queue between different calls
def get_bits(amount:int=1, restart:bool=False) -> str:
"""Fetches a certain amount of bits from the raw file, and moves the file header
when a new byte is reached.
"""
nonlocal bit_queue
# Should be set to 'True' when the restart interval is reached
if restart:
bit_queue.clear() # Discard the remaining bits
self.file_header += 2 # Jump over the restart marker
# Fetch more bits if the queue has less than the requested amount
while amount > len(bit_queue):
next_byte = self.raw_file[self.file_header]
self.file_header += 1
if next_byte == 0xFF:
self.file_header += 1 # Jump over the stuffed byte
"""NOTE
In order to prevent a sequence to be mistaken for a marker, when a 0xFF byte
appears on the image data, the encoder adds a 0x00 afterwards. This is called
'byte stuffing', and it is up to the decoder to remove it before decoding the
stream.
"""
bit_queue.extend(
np.unpackbits(
bytearray((next_byte,)) # Unpack the bits and add them to the end of the queue
)
)
# Return the bits sequence as a string
return "".join(str(bit_queue.popleft()) for bit in range(amount))
# Return the nested function
return get_bits
def baseline_dct_scan(self, huffman_tables_id:dict, my_color_components:dict) -> None:
"""Decode the image data from the entropy encoded segment.
The file header should be at the beginning of said segment, and at
the after the decoding the header will be moved to the end of the segment.
"""
print(f"\nScan {self.scan_count+1} of {self.scan_amount}")
print(f"Color components: {', '.join(component.name for component in my_color_components.values())}")
print(f"MCU count: {self.mcu_count}")
print(f"Decoding MCUs and performing IDCT...")
# Function to read the bits from the file's bytes
next_bits = self.bits_generator()
# Function to decode the next Huffman value
def next_huffval() -> int:
codeword = ""
huffman_value = None
while huffman_value is None:
codeword += next_bits()
if len(codeword) > 16:
raise CorruptedJpeg(f"Failed to decode image ({current_mcu}/{self.mcu_count} MCUs decoded).")
huffman_value = huffman_table.get(codeword)
return huffman_value
# Function to perform the inverse discrete cosine transform (IDCT)
idct = InverseDCT()
# Function to resize a block of color values
resize = ResizeGrid()
# Number of color components in the scan
components_amount = len(my_color_components)
# Decode all MCUs in the entropy encoded data
current_mcu = 0
previous_dc = np.zeros(components_amount, dtype="int16")
while (current_mcu < self.mcu_count):
"""NOTE
The decoding process goes through all MCUs in the image.
The MCUs are encoded sequentially on the stream, starting from the top
left of the image, then going left-to-right and then top-to-bottom.
Each MCU has 64 elements, the first being the DC value and the other 63
being the AC values.
If the image is greyscale, the color values are sequential: you get the
64 values of the Luminance MCU, and then move to the next MCU.
If the image is colored, the color values are interleaved: first you get
the values of the Luminance MCU, followed by the values of the Blue
Chrominance MCU, and then the values of the Red Chrominance MCU. Both
Red and Blue chrominances share the same Huffman trees.
However the amount of values that each MCU has depend on the chroma subsampling.
The subsampling is made by taking a number adjascent pixels, then averaging
their chromaminance values, and treating the result as a single pixel.
So each 8x8 block of chrominance values end up representing an area larger
than 8x8 pixels.
In order to compensate for that, when decoding the data stream, you first get
(consecutively) a number of 8x8 blocks of luminance values enough to
cover the area of the chrominance blocks. And only after those luminance blocks
you get one 8x8 blue chrominance block, and then one 8x8 red chrominance block.
And then the whole process repeats until all the data is scanned.
The area covered by the 8x8 luminance blocks starts from the top left, then
goes from left to right, and finally from top to bottom. This area starts
from the top left of the image, and also follows left to right and top to bottom.
"""
# (x, y) coordinates, on the image, for the current MCU
mcu_y, mcu_x = divmod(current_mcu, self.mcu_count_h)
# Loop through all color components
for depth, (component_id, component) in enumerate(my_color_components.items()):
# Quantization table of the color component
quantization_table = self.quantization_tables[component.quantization_table_id]
# Minimum coding unit (MCU) of the component
if components_amount > 1:
my_mcu = np.zeros(shape=component.shape, dtype="int16")
repeat = component.repeat
else:
my_mcu = np.zeros(shape=(8, 8), dtype="int16")
repeat = 1
"""NOTE
When there is more than one color component in the scan, the components are
interleaved. And the component with the highest resolution is repeated enough
times to cover the area of the subsampled components.
For example, if you subsampled the chrominance by 2 pixels in the vertical and
in the horizontal, then a 8 x 8 block of the chrominance layer actually covers
an area of 16 x 16 in the image. While the luminance blocks still cover an
area of 8 x 8. Four of those blocks are necessary to cover an area of 16 x 16,
So in this case, while decoding we get 4 blocks of luminance followed by 1 block
of blue chrominance and then 1 block of red chrominance. All this set of blocks
is the MCU (Minimum Coding Unit).
The repeated blocks cover the MCU area starting from the top left, then moving
from left to right and from top to bottom. The MCUs themselves also cover the
image following the same pattern (left to right, then bottom to top).
"""
for block_count in range(repeat):
# Block of 8 x 8 pixels for the color component
block = np.zeros(64, dtype="int16")
# DC value of the block
table_id = huffman_tables_id[component_id].dc
huffman_table:dict = self.huffman_tables[table_id]
huffman_value = next_huffval()
"""NOTE
For the DC values decoding, the huffman_value represents the bit-length
of the next DC value.
"""
dc_value = bin_twos_complement(next_bits(huffman_value)) + previous_dc[depth]
previous_dc[depth] = dc_value
block[0] = dc_value
"""NOTE
The DC value is delta encoded in relation to the previous DC value of the
same color component.
Delta encoding is the difference between two consecutive values. So the
decoded value is just added to the previous DC value in order to find
the current value.
For the first DC value, the previous value is considered to be zero.
"""
# AC values of the block
table_id = huffman_tables_id[component_id].ac
huffman_table:dict = self.huffman_tables[table_id]
index = 1
while index < 64:
huffman_value = next_huffval()
"""NOTE
The huffman_value is one byte long. For the AC value decoding, the eight bits
of the huffman value are in the following format:
RRRRSSSS
Where:
RRRR represents the amount of zeroes before the next non-zero AC value
SSSS represents the bit-length of the next AC value
A huffman_value of 0x00, however, has a different meaning: it marks the end of
the block (all remaining AC values of the block are zero).
"""
# A huffman_value of 0 means the 'end of block' (all remaining AC values are zero)
if huffman_value == 0x00:
break
# Amount of zeroes before the next AC value
zero_run_length = huffman_value >> 4
index += zero_run_length
if index >= 64:
break
# Get the AC value
ac_bit_length = huffman_value & 0x0F
if ac_bit_length > 0:
ac_value = bin_twos_complement(next_bits(ac_bit_length))
block[index] = ac_value
# Go to the next AC value
index += 1
# Undo the zigzag scan and apply dequantization
block = undo_zigzag(block) * quantization_table
# Apply the inverse discrete cosine transform (IDCT)
block = idct(block)
# Coordinates of the block on the current MCU
block_y, block_x = divmod(block_count, component.horizontal_sampling)
block_y, block_x = 8*block_y, 8*block_x
# Add the block to the MCU
my_mcu[block_x : block_x+8, block_y : block_y+8] = block
# Upsample the block if necessary
if component.shape != self.sample_shape:
my_mcu = resize(my_mcu, self.sample_shape)
"""NOTE
Linear interpolation is performed on subsampled color components.
"""
# Add the MCU to the image
x = self.mcu_width * mcu_x
y = self.mcu_height * mcu_y
self.image_array[x : x+self.mcu_width, y : y+self.mcu_height, component.order] = my_mcu
# Go to the next MCU
current_mcu += 1
print_progress(current_mcu, self.mcu_count)
# Check for restart interval
if (self.restart_interval > 0) and (current_mcu % self.restart_interval == 0) and (current_mcu != self.mcu_count):
next_bits(amount=0, restart=True)
previous_dc[:] = 0
"""NOTE
When the Restart Interval is reached, the previous DC values are reseted to zero
and the file header is moved to the byte boundary after the marker.
"""
self.scan_count += 1
print_progress(current_mcu, self.mcu_count, done=True)
def progressive_dct_scan(self,
huffman_tables_id:dict,
my_color_components:dict,
spectral_selection_start:int,
spectral_selection_end:int,
bit_position_high:int,
bit_position_low:int) -> None:
# Whether to the scan contains DC or AC values
if (spectral_selection_start == 0) and (spectral_selection_end == 0):
values = "dc"
elif (spectral_selection_start > 0) and (spectral_selection_end >= spectral_selection_start):
values = "ac"
else:
raise CorruptedJpeg("Progressive JPEG images cannot contain both DC and AC values in the same scan.")
"""NOTE
In sequential JPEG both DC and AC values come in the same scan, however in progressive JPEG
they must come in different scans.
"""
# Whether this is a refining scan
if bit_position_high == 0:
refining = False
elif (bit_position_high - bit_position_low) == 1:
refining = True
else:
raise CorruptedJpeg("Progressive JPEG images cannot contain more than 1 bit for each value on a refining scan.")
"""NOTE
The first scan of a value sends a certain amount of the value's most significant bits.
The following scans of the same value send the next bits, in order, one bit per scan.
Those scans are called "refining scans".
"""
print(f"\nScan {self.scan_count+1} of {self.scan_amount}")
print(f"Color components: {', '.join(component.name for component in my_color_components.values())}")
print(f"Spectral selection: {spectral_selection_start}-{spectral_selection_end} ({values.upper()})")
print(f"Successive approximation: {bit_position_high}-{bit_position_low} ({'refining' if refining else 'first'} scan)")
print(f"MCU count: {self.mcu_count}")
print(f"Decoding MCUs...")
# Function to read the bits from the file's bytes
next_bits = self.bits_generator()
# Function to decode the next Huffman value
def next_huffval() -> int:
codeword = ""
huffman_value = None
while huffman_value is None:
codeword += next_bits()
if len(codeword) > 16:
raise CorruptedJpeg(f"Failed to decode image ({current_mcu}/{self.mcu_count} MCUs decoded).")
huffman_value = huffman_table.get(codeword)
return huffman_value
# Beginning of scan
current_mcu = 0
components_amount = len(my_color_components)
if (values == "ac") and (components_amount > 1):
raise CorruptedJpeg("An AC progressive scan can only have a single color component.")
"""NOTE
A DC progressive scan can have more than one color component, while an AC progressive
scan must have only one color component.
"""
# DC values scan
if values == "dc":
# First scan (DC)
"""NOTE
For the most part, the first DC scan on progressive mode is the same as on baseline mode.
The only difference is that the decoded value needs to have to undergo through a left
bit shift by the amount specified in 'bit_position_low', because the progressive scan
only gives this amount of the first bits of the value on the first scan.
"""
if not refining:
# Previous DC values
previous_dc = np.zeros(components_amount, dtype="int16")
while (current_mcu < self.mcu_count):
# Loop through all color components
for depth, (component_id, component) in enumerate(my_color_components.items()):
# (x, y) coordinates, on the image, for the current MCU
x = (current_mcu % self.mcu_count_h) * component.shape[0]
y = (current_mcu // self.mcu_count_h) * component.shape[1]
# Minimum coding unit (MCU) of the component
if components_amount > 1:
repeat = component.repeat
else:
repeat = 1