pyjpeg 0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. pyjpeg-0.1/.gitignore +2 -0
  2. pyjpeg-0.1/LICENSE +165 -0
  3. pyjpeg-0.1/PKG-INFO +30 -0
  4. pyjpeg-0.1/README.md +13 -0
  5. pyjpeg-0.1/analyze.py +297 -0
  6. pyjpeg-0.1/jpg2pnm.py +88 -0
  7. pyjpeg-0.1/pnm.py +101 -0
  8. pyjpeg-0.1/pyproject.toml +26 -0
  9. pyjpeg-0.1/src/pyjpeg/__init__.py +139 -0
  10. pyjpeg-0.1/src/pyjpeg/app.py +423 -0
  11. pyjpeg-0.1/src/pyjpeg/arithmetic.py +401 -0
  12. pyjpeg-0.1/src/pyjpeg/arithmetic_dct_ac_successive_scan.py +191 -0
  13. pyjpeg-0.1/src/pyjpeg/arithmetic_dct_dc_successive_scan.py +64 -0
  14. pyjpeg-0.1/src/pyjpeg/arithmetic_dct_scan.py +320 -0
  15. pyjpeg-0.1/src/pyjpeg/arithmetic_lossless_scan.py +265 -0
  16. pyjpeg-0.1/src/pyjpeg/arithmetic_scan.py +259 -0
  17. pyjpeg-0.1/src/pyjpeg/com.py +36 -0
  18. pyjpeg-0.1/src/pyjpeg/dac.py +84 -0
  19. pyjpeg-0.1/src/pyjpeg/dct.py +176 -0
  20. pyjpeg-0.1/src/pyjpeg/dht.py +147 -0
  21. pyjpeg-0.1/src/pyjpeg/dnl.py +51 -0
  22. pyjpeg-0.1/src/pyjpeg/dqt.py +94 -0
  23. pyjpeg-0.1/src/pyjpeg/dri.py +51 -0
  24. pyjpeg-0.1/src/pyjpeg/eoi.py +30 -0
  25. pyjpeg-0.1/src/pyjpeg/exp.py +52 -0
  26. pyjpeg-0.1/src/pyjpeg/golomb_scan.py +117 -0
  27. pyjpeg-0.1/src/pyjpeg/huffman.py +299 -0
  28. pyjpeg-0.1/src/pyjpeg/huffman_dct_ac_successive_scan.py +206 -0
  29. pyjpeg-0.1/src/pyjpeg/huffman_dct_dc_successive_scan.py +67 -0
  30. pyjpeg-0.1/src/pyjpeg/huffman_dct_scan.py +311 -0
  31. pyjpeg-0.1/src/pyjpeg/huffman_lossless_scan.py +164 -0
  32. pyjpeg-0.1/src/pyjpeg/huffman_optimize.py +97 -0
  33. pyjpeg-0.1/src/pyjpeg/huffman_scan.py +179 -0
  34. pyjpeg-0.1/src/pyjpeg/huffman_tables.py +368 -0
  35. pyjpeg-0.1/src/pyjpeg/image.py +130 -0
  36. pyjpeg-0.1/src/pyjpeg/io.py +91 -0
  37. pyjpeg-0.1/src/pyjpeg/lossless.py +116 -0
  38. pyjpeg-0.1/src/pyjpeg/ls_scan.py +967 -0
  39. pyjpeg-0.1/src/pyjpeg/lse.py +229 -0
  40. pyjpeg-0.1/src/pyjpeg/marker.py +60 -0
  41. pyjpeg-0.1/src/pyjpeg/quantization_tables.py +134 -0
  42. pyjpeg-0.1/src/pyjpeg/rst.py +34 -0
  43. pyjpeg-0.1/src/pyjpeg/scan.py +68 -0
  44. pyjpeg-0.1/src/pyjpeg/segment.py +6 -0
  45. pyjpeg-0.1/src/pyjpeg/sof.py +269 -0
  46. pyjpeg-0.1/src/pyjpeg/soi.py +31 -0
  47. pyjpeg-0.1/src/pyjpeg/sos.py +152 -0
  48. pyjpeg-0.1/src/pyjpeg/stream.py +494 -0
pyjpeg-0.1/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ __pycache__
2
+ dist/
pyjpeg-0.1/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
pyjpeg-0.1/PKG-INFO ADDED
@@ -0,0 +1,30 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyjpeg
3
+ Version: 0.1
4
+ Summary: Pure Python JPEG image encoder/decoder
5
+ Project-URL: Homepage, https://github.com/robert-ancell/pyjpeg
6
+ Project-URL: Issues, https://github.com/robert-ancell/pyjpeg/issues
7
+ Author-email: Robert Ancell <robert.ancell@gmail.com>
8
+ License-Expression: LGPL-3.0
9
+ License-File: LICENSE
10
+ Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Topic :: Multimedia :: Graphics
14
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
15
+ Requires-Python: >=3.9
16
+ Description-Content-Type: text/markdown
17
+
18
+ This repository contains a Python encoder and decoder for the [JPEG file format](https://jpeg.org/jpeg/).
19
+
20
+ The easiest way to get PyJPEG is to install from the [Python Packaging Index](https://pypi.org/project/pyjpeg/):
21
+ ```
22
+ pip install pyjpeg
23
+ ```
24
+
25
+ TODO Example:
26
+ ```python
27
+ import pyjpeg
28
+
29
+ # FIXME
30
+ ```
pyjpeg-0.1/README.md ADDED
@@ -0,0 +1,13 @@
1
+ This repository contains a Python encoder and decoder for the [JPEG file format](https://jpeg.org/jpeg/).
2
+
3
+ The easiest way to get PyJPEG is to install from the [Python Packaging Index](https://pypi.org/project/pyjpeg/):
4
+ ```
5
+ pip install pyjpeg
6
+ ```
7
+
8
+ TODO Example:
9
+ ```python
10
+ import pyjpeg
11
+
12
+ # FIXME
13
+ ```
pyjpeg-0.1/analyze.py ADDED
@@ -0,0 +1,297 @@
1
+ #!/usr/bin/env python3
2
+
3
+ import sys
4
+
5
+ import pyjpeg
6
+
7
+
8
+ def print_data_unit(data_unit: list[int]) -> None:
9
+ values = pyjpeg.dct.unzig_zag(data_unit)
10
+
11
+ cols = []
12
+ for x in range(8):
13
+ col = []
14
+ for y in range(8):
15
+ col.append("%d" % values[y * 8 + x])
16
+ cols.append(col)
17
+
18
+ col_widths = []
19
+ for x in range(8):
20
+ width = 0
21
+ for y in range(8):
22
+ width = max(width, len(cols[x][y]))
23
+ col_widths.append(width)
24
+
25
+ for y in range(8):
26
+ row = []
27
+ for x in range(8):
28
+ row.append(cols[x][y].rjust(col_widths[x]))
29
+ print(" %s" % " ".join(row))
30
+
31
+
32
+ if len(sys.argv) != 2:
33
+ print("Usage: analyze.py <filename.jpg>")
34
+ sys.exit(1)
35
+
36
+ data = open(sys.argv[1], "rb").read()
37
+ reader = pyjpeg.BufferedReader(data)
38
+ stream = pyjpeg.Stream.read(reader)
39
+
40
+ is_lossless = False
41
+ is_ls = False
42
+ for segment in stream.segments:
43
+ if isinstance(segment, pyjpeg.StartOfImage):
44
+ print("SOI Start of Image")
45
+ elif isinstance(segment, pyjpeg.JfifHeader):
46
+ print("APP%d JFIF" % segment.n)
47
+ print(" Version: %d.%d" % (segment.version[0], segment.version[1]))
48
+ if segment.density.unit == pyjpeg.JfifDensityUnit.ASPECT_RATIO:
49
+ print(" Aspect Ratio: %dx%d" % (segment.density.x, segment.density.y))
50
+ elif segment.density.unit == pyjpeg.JfifDensityUnit.DPI:
51
+ print(" Density: %dx%ddpi" % (segment.density.x, segment.density.y))
52
+ elif segment.density.unit == pyjpeg.JfifDensityUnit.DPCM:
53
+ print(" Density: %dx%ddpcm" % (segment.density.x, segment.density.y))
54
+ if len(segment.thumbnail_data) > 0:
55
+ # FIXME: Support RGB thumbnails
56
+ s = " Thumbnail %dx%d:" % (
57
+ segment.thumbnail_size[0],
58
+ segment.thumbnail_size[1],
59
+ )
60
+ for i in range(0, len(segment.thumbnail_data), 3):
61
+ if i % (segment.thumbnail_size[0] * 3) == 0:
62
+ s += "\n "
63
+ s += " %d,%d,%d" % (
64
+ segment.thumbnail_data[i],
65
+ segment.thumbnail_data[i + 1],
66
+ segment.thumbnail_data[i + 2],
67
+ )
68
+ print(s)
69
+ elif isinstance(segment, pyjpeg.JfifJpegThumbnail):
70
+ print("APP%d JPEG Thumbnail" % segment.n)
71
+ print(" Data: %s" % repr(segment.data))
72
+ elif isinstance(segment, pyjpeg.JfifPalletizedThumbnail):
73
+ print("APP%d Palletized Thumbnail" % segment.n)
74
+ print(" Width: %d" % segment.width)
75
+ print(" Height: %d" % segment.height)
76
+ print(" Data: %s" % segment.data)
77
+ elif isinstance(segment, pyjpeg.JfifRgbThumbnail):
78
+ print("APP%d RGB Thumbnail" % segment.n)
79
+ print(" Width: %d" % segment.width)
80
+ print(" Height: %d" % segment.height)
81
+ print(" Data: %s" % segment.data)
82
+ elif isinstance(segment, pyjpeg.SpiffHeader):
83
+ print("APP%d SPIFF" % segment.n)
84
+ print(" Version: %d.%d" % (segment.version[0], segment.version[1]))
85
+ print(" Profile: %d" % segment.profile)
86
+ print(" Number of Components: %d" % segment.number_of_components)
87
+ print(" Height: %d" % segment.height)
88
+ print(" Width: %d" % segment.width)
89
+ print(" Color Space: %d" % segment.color_space)
90
+ print(" Bits per Sample: %d" % segment.bits_per_sample)
91
+ print(" Compression Type: %d" % segment.compression_type)
92
+ print(" Resolution Units: %d" % segment.resolution_units)
93
+ print(" Vertical Resolution: %d" % segment.vertical_resoution)
94
+ print(" Horizontal Resolution: %d" % segment.horizontal_resolution)
95
+ elif isinstance(segment, pyjpeg.ExifHeader):
96
+ print("APP%d EXIF" % segment.n)
97
+ print(" Data: %r" % segment.data)
98
+ elif isinstance(segment, pyjpeg.AdobeHeader):
99
+ print("APP%d Adobe" % segment.n)
100
+ print(" Version: %d" % segment.version)
101
+ print(" Flags 0: %04x" % segment.flags0)
102
+ print(" Flags 1: %04x" % segment.flags1)
103
+ print(
104
+ " Colorspace: %s"
105
+ % {
106
+ pyjpeg.AdobeColorSpace.RGB_OR_CMYK: "RGB or CMYK",
107
+ pyjpeg.AdobeColorSpace.Y_CB_CR: "YCbCr",
108
+ pyjpeg.AdobeColorSpace.Y_CB_CR_K: "YCbCrK",
109
+ }.get(segment.color_space, "%d" % segment.color_space)
110
+ )
111
+ elif isinstance(segment, pyjpeg.UnknownApplicationSpecificData):
112
+ print("APP%d Application Specific Data" % segment.n)
113
+ s = " Data: "
114
+ for d in segment.data:
115
+ s += "%02X" % d
116
+ print(s)
117
+ elif isinstance(segment, pyjpeg.Comment):
118
+ print("COM Comment")
119
+ print(" Data: %s" % repr(segment.data))
120
+ elif isinstance(segment, pyjpeg.DefineQuantizationTables):
121
+ print("DQT Define Quantization Tables")
122
+ for quantization_table in segment.tables:
123
+ print(" Table %d:" % quantization_table.destination)
124
+ print(" Precision: %d bits" % quantization_table.precision)
125
+ print_data_unit(quantization_table.values)
126
+ elif isinstance(segment, pyjpeg.DefineHuffmanTables):
127
+ print("DHT Define Huffman Tables")
128
+ for huffman_table in segment.tables:
129
+ print(
130
+ " %s Table %d:"
131
+ % (
132
+ {0: "DC", 1: "AC"}[huffman_table.table_class],
133
+ huffman_table.destination,
134
+ )
135
+ )
136
+ for i, symbols in enumerate(huffman_table.table):
137
+ if len(symbols) > 0:
138
+ s = " Symbols of length %d:" % (i + 1)
139
+ for symbol in symbols:
140
+ s += " %02x" % symbol
141
+ print(s)
142
+ elif isinstance(segment, pyjpeg.DefineArithmeticConditioning):
143
+ print("DAC Define Arithmetic Conditioning")
144
+ for conditioning in segment.tables:
145
+ print(
146
+ " %s Table %d: %s"
147
+ % (
148
+ {0: "DC", 1: "AC"}[conditioning.table_class],
149
+ conditioning.destination,
150
+ repr(conditioning.value),
151
+ )
152
+ )
153
+ elif isinstance(segment, pyjpeg.DefineRestartInterval):
154
+ print("DRI Define Restart Interval")
155
+ print(" Restart interval: %d" % segment.restart_interval)
156
+ elif isinstance(segment, pyjpeg.ExpandReferenceComponents):
157
+ print("EXP Expand Reference Components")
158
+ print(
159
+ " Expand Horizontal: %s"
160
+ % {False: "No", True: "Yes"}[segment.expand_horizontal != 0]
161
+ )
162
+ print(
163
+ " Expand Vertical: %s"
164
+ % {False: "No", True: "Yes"}[segment.expand_vertical != 0]
165
+ )
166
+ elif isinstance(segment, pyjpeg.StartOfFrame):
167
+ is_lossless = segment.n in (3, 7, 11, 15)
168
+ is_ls = segment.n == 55
169
+ print(
170
+ "SOF%d Start of Frame, %s"
171
+ % (
172
+ segment.n,
173
+ {
174
+ pyjpeg.FrameType.BASELINE: "Baseline DCT",
175
+ pyjpeg.FrameType.EXTENDED_HUFFMAN: "Extended sequential DCT, Huffman coding",
176
+ pyjpeg.FrameType.PROGRESSIVE_HUFFMAN: "Progressive DCT, Huffman coding",
177
+ pyjpeg.FrameType.LOSSLESS_HUFFMAN: "Lossless (sequential), Huffman coding",
178
+ pyjpeg.FrameType.DIFFERENTIAL_SEQUENTIAL_HUFFMAN: "Differential sequential DCT, Huffman coding",
179
+ pyjpeg.FrameType.DIFFERENTIAL_PROGRESSIVE_HUFFMAN: "Differential progressive DCT, Huffman coding",
180
+ pyjpeg.FrameType.DIFFERENTIAL_LOSSLESS_HUFFMAN: "Differential lossless (sequential), Huffman coding",
181
+ pyjpeg.FrameType.EXTENDED_ARITHMETIC: "Extended sequential DCT, Arithmetic coding",
182
+ pyjpeg.FrameType.PROGRESSIVE_ARITHMETIC: "Progressive DCT, Arithmetic coding",
183
+ pyjpeg.FrameType.LOSSLESS_ARITHMETIC: "Lossless (sequential), Arithmetic coding",
184
+ pyjpeg.FrameType.DIFFERENTIAL_SEQUENTIAL_ARITHMETIC: "Differential sequential DCT, Arithmetic coding",
185
+ pyjpeg.FrameType.DIFFERENTIAL_PROGRESSIVE_ARITHMETIC: "Differential progressive DCT, Arithmetic coding",
186
+ pyjpeg.FrameType.DIFFERENTIAL_LOSSLESS_ARITHMETIC: "Differential lossless (sequential), Arithmetic coding",
187
+ pyjpeg.FrameType.LS: "JPEG-LS",
188
+ }[segment.n],
189
+ )
190
+ )
191
+ print(" Precision: %d bits" % segment.precision)
192
+ print(
193
+ " Number of lines: %d" % segment.number_of_lines
194
+ ) # FIXME: Note if zero defined later
195
+ print(" Number of samples per line: %d" % segment.samples_per_line)
196
+ for frame_component in segment.components:
197
+ print(" Component:")
198
+ print(" Id: %d" % frame_component.id)
199
+ print(
200
+ " Sampling Factor: %dx%d"
201
+ % (
202
+ frame_component.sampling_factor[0],
203
+ frame_component.sampling_factor[1],
204
+ )
205
+ )
206
+ if not is_lossless and not is_ls:
207
+ print(
208
+ " Quantization Table: %d"
209
+ % frame_component.quantization_table_index
210
+ )
211
+ elif isinstance(segment, pyjpeg.StartOfScan):
212
+ print("SOS Start of Scan")
213
+ for scan_component in segment.components:
214
+ print(" Component:")
215
+ print(" Id: %d" % scan_component.component_selector)
216
+ if is_ls:
217
+ print(" Mapping table: %d" % scan_component.get_mapping_table())
218
+ else:
219
+ print(" DC Table: %d" % scan_component.dc_table)
220
+ if not is_lossless:
221
+ print(" AC Table: %d" % scan_component.ac_table)
222
+ if is_lossless:
223
+ print(" Predictor: %d" % segment.spectral_selection[0])
224
+ elif is_ls:
225
+ print(" Near: %d" % segment.spectral_selection[0])
226
+ print(
227
+ " Interleave Mode: %s"
228
+ % {
229
+ pyjpeg.LSInterleaveMode.NONE: "None",
230
+ pyjpeg.LSInterleaveMode.LINE: "Line",
231
+ pyjpeg.LSInterleaveMode.SAMPLE: "Sample",
232
+ }.get(
233
+ segment.spectral_selection[1], "%d" % segment.spectral_selection[1]
234
+ )
235
+ )
236
+ else:
237
+ print(
238
+ " Spectral Selection: %d-%d"
239
+ % (segment.spectral_selection[0], segment.spectral_selection[1])
240
+ )
241
+
242
+ if (segment.point_transform & 0xF0) != 0:
243
+ print(" Previous Point Transform: %d" % (segment.point_transform >> 4))
244
+ print(" Point Transform: %d" % (segment.point_transform & 0xF))
245
+ elif isinstance(segment, pyjpeg.HuffmanDCTScan) or isinstance(
246
+ segment, pyjpeg.ArithmeticDCTScan
247
+ ):
248
+ for data_unit in segment.data_units:
249
+ print_data_unit(data_unit)
250
+ elif (
251
+ isinstance(segment, pyjpeg.HuffmanLosslessScan)
252
+ or isinstance(segment, pyjpeg.ArithmeticLosslessScan)
253
+ or isinstance(segment, pyjpeg.LSScan)
254
+ ):
255
+ s = " Samples:"
256
+ for sample in segment.samples:
257
+ s += " %d" % sample
258
+ print(s)
259
+ elif isinstance(segment, pyjpeg.Restart):
260
+ print("RST%d Restart" % segment.index)
261
+ elif isinstance(segment, pyjpeg.DefineNumberOfLines):
262
+ print("DNL Define Number of Lines")
263
+ print(" Number of lines: %d" % segment.number_of_lines)
264
+ elif isinstance(segment, pyjpeg.LSCodingParameters):
265
+ print("LSE Coding Parameters")
266
+ print(" Maximum value: %d" % segment.maxval)
267
+ print(
268
+ " Gradient thresholds: %d, %d, %d"
269
+ % (
270
+ segment.gradient_thresholds[0],
271
+ segment.gradient_thresholds[1],
272
+ segment.gradient_thresholds[2],
273
+ )
274
+ )
275
+ print(" Reset: %d" % segment.reset)
276
+ elif isinstance(segment, pyjpeg.LSMappingTable):
277
+ print("LSE Mapping Table")
278
+ print(" Table ID: %d" % segment.table_id)
279
+ print(" Weight: %d" % segment.weight)
280
+ print(" Table Data: %s" % segment.table.hex())
281
+ elif isinstance(segment, pyjpeg.LSMappingTableContinuation):
282
+ print("LSE Mapping Table Continuation")
283
+ print(" Table ID: %d" % segment.table_id)
284
+ print(" Weight: %d" % segment.weight)
285
+ print(" Table Data: %s" % segment.table.hex())
286
+ elif isinstance(segment, pyjpeg.LSOversizeImageDimensions):
287
+ print("LSE Oversize Image Dimensions")
288
+ print(" Number of lines: %d" % segment.number_of_lines)
289
+ print(" Number of samples per line: %d" % segment.samples_per_line)
290
+ elif isinstance(segment, pyjpeg.LSUnknownPresetParameters):
291
+ print("LSE Preset Parameters")
292
+ print(" ID: %d" % segment.id)
293
+ print(" Data: %s" % segment.data.hex())
294
+ elif isinstance(segment, pyjpeg.EndOfImage):
295
+ print("EOI End of Image")
296
+ else:
297
+ print(segment)
pyjpeg-0.1/jpg2pnm.py ADDED
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env python3
2
+
3
+ import sys
4
+
5
+ import pyjpeg
6
+ from pnm import *
7
+
8
+ if len(sys.argv) != 3:
9
+ print("Usage: jpg2pnm.py <input.jpg> <output.pnm>")
10
+ sys.exit(1)
11
+
12
+ data = open(sys.argv[1], "rb").read()
13
+ reader = pyjpeg.BufferedReader(data)
14
+ stream = pyjpeg.Stream.read(reader)
15
+
16
+ data = []
17
+ convert_ycbcr = False
18
+ sof = None
19
+ sos = None
20
+ quantization_tables = [
21
+ [1] * 64,
22
+ [1] * 64,
23
+ [1] * 64,
24
+ [1] * 64,
25
+ ]
26
+ for segment in stream.segments:
27
+ if isinstance(segment, pyjpeg.JfifHeader):
28
+ convert_ycbcr = True
29
+ elif isinstance(segment, pyjpeg.AdobeHeader):
30
+ if segment.color_space == pyjpeg.AdobeColorSpace.Y_CB_CR:
31
+ convert_ycbcr = True
32
+ elif isinstance(segment, pyjpeg.StartOfFrame):
33
+ sof = segment
34
+ data = (
35
+ [0]
36
+ * segment.samples_per_line
37
+ * segment.number_of_lines
38
+ * len(segment.components)
39
+ )
40
+ elif isinstance(segment, pyjpeg.DefineQuantizationTables):
41
+ for table in segment.tables:
42
+ quantization_tables[table.destination] = table.values
43
+ elif isinstance(segment, pyjpeg.StartOfScan):
44
+ sos = segment
45
+ elif isinstance(segment, pyjpeg.HuffmanDCTScan) or isinstance(
46
+ segment, pyjpeg.ArithmeticDCTScan
47
+ ):
48
+ # FIXME: Channels, sampling factor
49
+ du_x = 0
50
+ du_y = 0
51
+ component = sof.get_component(sof.components[0].id)
52
+ for i, data_unit in enumerate(segment.data_units):
53
+ samples = pyjpeg.idct(
54
+ data_unit,
55
+ quantization_tables[component.quantization_table_index],
56
+ sof.precision,
57
+ )
58
+ x_max = 8
59
+ if du_x + x_max > sof.samples_per_line:
60
+ x_max = max(sof.samples_per_line - du_x, 0)
61
+ y_max = 8
62
+ if du_y + y_max > sof.number_of_lines:
63
+ y_max = max(sof.number_of_lines - du_y, 0)
64
+ for y in range(x_max):
65
+ for x in range(y_max):
66
+ data[(du_y + y) * sof.samples_per_line + du_x + x] = samples[
67
+ y * 8 + x
68
+ ]
69
+
70
+ du_x += 8
71
+ if du_x >= sof.samples_per_line:
72
+ du_x = 0
73
+ du_y += 8
74
+ elif isinstance(segment, pyjpeg.HuffmanLosslessScan) or isinstance(
75
+ segment, pyjpeg.ArithmeticLosslessScan
76
+ ):
77
+ # FIXME: Channels, sampling factor
78
+ for i, sample in enumerate(segment.samples):
79
+ data[i] = sample
80
+
81
+
82
+ write_pnm(
83
+ sys.argv[2],
84
+ sof.samples_per_line,
85
+ sof.number_of_lines,
86
+ data,
87
+ channels=len(sof.components),
88
+ )