pure_jpeg 0.4.0 → 0.4.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +29 -0
- data/CREATIVE.md +100 -0
- data/LICENSE +7 -0
- data/LICENSE-IJG +260 -0
- data/README.md +42 -4
- data/lib/pure_jpeg/bit_reader.rb +21 -4
- data/lib/pure_jpeg/dct.rb +4 -2
- data/lib/pure_jpeg/decoder.rb +159 -78
- data/lib/pure_jpeg/huffman/decoder.rb +10 -0
- data/lib/pure_jpeg/jfif_reader.rb +75 -19
- data/lib/pure_jpeg/version.rb +1 -1
- data/lib/pure_jpeg.rb +6 -7
- metadata +6 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 41b11ef04efbfb35b257ce2e93e40ae4b4fd07fec3fd05918b87eacba9a517eb
|
|
4
|
+
data.tar.gz: 460755287366135f012fca8975822c9c50b52fa9e55144c3d1afc3c00538af37
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2d9c6cb32fb7197cd2fb78bf391e970729036aab6c37b4215c8fca9aef687ca2079d209367956618e6951aaf9f6e809030a64e724b8bc34c070f0de1ad373680
|
|
7
|
+
data.tar.gz: 9d88a0c1e1d15068afe4827f838fa6736c1fde4403720edb32ac4c6e840619ccacf9ac7788e6b1ebe9df6dd8b61cc096bcac07424cd03b8ce9e89f9770ad1977
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.1
|
|
4
|
+
|
|
5
|
+
This is a robustness and compatibility release. It makes decoding more reliable
|
|
6
|
+
across valid JPEG variants, improves handling of malformed input, and strengthens
|
|
7
|
+
Spinel support while preserving the existing public API and encoder output.
|
|
8
|
+
|
|
9
|
+
Fixes:
|
|
10
|
+
|
|
11
|
+
- Decode progressive images correctly when component edges require padded blocks
|
|
12
|
+
- Preserve each scan's restart interval and each component's original quantization table
|
|
13
|
+
- Decode baseline images with separate component scans and handle subsampled luminance
|
|
14
|
+
- Handle JPEG marker fill bytes correctly between scans and at restart boundaries
|
|
15
|
+
- Validate restart markers without recursion; reject malformed frame/scan parameters,
|
|
16
|
+
invalid Huffman tables, excessive coefficient runs, and incomplete scan data with `DecodeError`
|
|
17
|
+
- Reject unsupported precision and RGB/Adobe color transforms explicitly
|
|
18
|
+
- Read ICC headers after the frame header in `PureJPEG.info`; ignore incomplete or
|
|
19
|
+
inconsistent multipart profiles instead of returning partial profile bytes
|
|
20
|
+
- Treat binary garbage containing null bytes as data rather than a filesystem path
|
|
21
|
+
- Avoid a Spinel compiler bug in conditional hash assignment during decoding
|
|
22
|
+
|
|
23
|
+
Testing and packaging:
|
|
24
|
+
|
|
25
|
+
- Add small libjpeg-turbo fixtures with trusted decoded-pixel hashes and malformed-input regressions
|
|
26
|
+
- Add Spinel pixel-parity checks for the decoder fixes; all four package checks and
|
|
27
|
+
all eight selected RawSource tests pass on Spinel revision `2406a5649`
|
|
28
|
+
- Keep all existing tests, public API signatures, source-adapter behavior, and encoder output unchanged
|
|
29
|
+
- Run CI on pushes to `main` and smoke-test the built, installed gem without development dependencies
|
|
30
|
+
- Include creative-options documentation and IJG notices in the gem; correct the DCT algorithm attribution
|
|
31
|
+
|
|
3
32
|
## 0.4.0
|
|
4
33
|
|
|
5
34
|
Performance:
|
data/CREATIVE.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# Creative encoding with PureJPEG
|
|
2
|
+
|
|
3
|
+
PureJPEG exposes access to several 'knobs' to manipulate the JPEG encoding pipeline. These produce a range of aesthetic effects by controlling how the DCT frequency coefficients are quantized.
|
|
4
|
+
|
|
5
|
+
All examples assume:
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
require "chunky_png"
|
|
9
|
+
require_relative "lib/pure_jpeg"
|
|
10
|
+
|
|
11
|
+
image = ChunkyPNG::Image.from_file("input.png")
|
|
12
|
+
source = PureJPEG::Source::ChunkyPNGSource.new(image)
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Scrambled quantization ("early digicam" look)
|
|
16
|
+
|
|
17
|
+
The JPEG spec stores quantization tables in zigzag order. Enabling `scramble_quantization` writes them in raster order instead, which means any standard viewer applies the wrong quantization values to the wrong frequency coefficients. The result is a chaotic, crunchy artifact pattern reminiscent of early 2000s digital cameras (what I really wanted in the first place).
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
PureJPEG.encode(source, quality: 20, scramble_quantization: true).write("scrambled.jpg")
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Lower quality values amplify the effect.
|
|
24
|
+
|
|
25
|
+
## Chroma crush (sharp detail, broken color)
|
|
26
|
+
|
|
27
|
+
Set high luminance quality but aggressively compress the chrominance channels. Detail and edges stay sharp but the color information collapses into large blocky patches with hue shifts.
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
PureJPEG.encode(source, quality: 90, chroma_quality: 5).write("chromacrush.jpg")
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Luma crush (soft image, faithful color)
|
|
34
|
+
|
|
35
|
+
The opposite: crush the luminance channel while preserving chrominance. Produces a soft, almost oil-painting quality where the image is blocky and low-detail but the colors remain surprisingly accurate.
|
|
36
|
+
|
|
37
|
+
```ruby
|
|
38
|
+
PureJPEG.encode(source, quality: 10, chroma_quality: 95).write("lumacrush.jpg")
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## DC-only (8x8 mosaic)
|
|
42
|
+
|
|
43
|
+
A custom quantization table that preserves only the DC coefficient (the average brightness of each 8x8 block) and kills all 63 AC coefficients. Every block becomes a single flat color, producing a hard mosaic effect.
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
dc_only = [1] + [255] * 63
|
|
47
|
+
PureJPEG.encode(source, luminance_table: dc_only, chrominance_table: dc_only).write("dconly.jpg")
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
You can soften the mosaic by letting a few low-frequency AC coefficients through:
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
# Keep DC and the first few AC coefficients
|
|
54
|
+
gentle_mosaic = [1, 1, 1, 2, 4, 8] + [255] * 58
|
|
55
|
+
PureJPEG.encode(source, luminance_table: gentle_mosaic, chrominance_table: gentle_mosaic).write("gentle_mosaic.jpg")
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Inverted frequency emphasis
|
|
59
|
+
|
|
60
|
+
Normally JPEG preserves low frequencies (smooth gradients) and discards high frequencies (fine detail). This modifier inverts that, preserving edges and texture while crushing smooth areas. The result is an embossed or etched look.
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
inverter = ->(table, _channel) {
|
|
64
|
+
max = table.max
|
|
65
|
+
table.map { |v| [max + 1 - v, 1].max }
|
|
66
|
+
}
|
|
67
|
+
PureJPEG.encode(source, quality: 30, quantization_modifier: inverter).write("inverted.jpg")
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Combining effects
|
|
71
|
+
|
|
72
|
+
The `quantization_modifier` proc receives the table and a channel identifier (`:luminance` or `:chrominance`), so you can apply different transformations per channel:
|
|
73
|
+
|
|
74
|
+
```ruby
|
|
75
|
+
modifier = ->(table, channel) {
|
|
76
|
+
case channel
|
|
77
|
+
when :luminance
|
|
78
|
+
# Sharpen luma by halving quantization values
|
|
79
|
+
table.map { |v| [v / 2, 1].max }
|
|
80
|
+
when :chrominance
|
|
81
|
+
# Posterize color by doubling quantization values
|
|
82
|
+
table.map { |v| [v * 2, 255].min }
|
|
83
|
+
end
|
|
84
|
+
}
|
|
85
|
+
PureJPEG.encode(source, quality: 50, quantization_modifier: modifier).write("combined.jpg")
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Full option reference
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
PureJPEG.encode(source,
|
|
92
|
+
quality: 85, # 1-100, overall compression level
|
|
93
|
+
grayscale: false, # encode as single-channel grayscale
|
|
94
|
+
chroma_quality: nil, # 1-100, independent Cb/Cr quality (defaults to quality)
|
|
95
|
+
luminance_table: nil, # custom 64-element quantization table for Y (raster order)
|
|
96
|
+
chrominance_table: nil, # custom 64-element quantization table for Cb/Cr (raster order)
|
|
97
|
+
quantization_modifier: nil, # proc(table, :luminance/:chrominance) -> modified table
|
|
98
|
+
scramble_quantization: false # write quant tables in wrong order (digicam effect)
|
|
99
|
+
)
|
|
100
|
+
```
|
data/LICENSE
CHANGED
|
@@ -19,3 +19,10 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
The integer DCT implementation in lib/pure_jpeg/dct.rb is adapted from the
|
|
24
|
+
Independent JPEG Group's jfdctint.c and jidctint.c. Its upstream copyright,
|
|
25
|
+
disclaimer, and distribution terms are included in LICENSE-IJG. PureJPEG's
|
|
26
|
+
adaptation ports the transforms to Ruby arrays, removes the C integration
|
|
27
|
+
and scaled-output variants, and normalizes forward coefficients before
|
|
28
|
+
quantization. The IJG-derived portions remain subject to those terms.
|
data/LICENSE-IJG
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
libjpeg-turbo note: This file has been modified by The libjpeg-turbo Project
|
|
2
|
+
to include only information relevant to libjpeg-turbo, to wordsmith certain
|
|
3
|
+
sections, and to remove impolitic language that existed in the libjpeg v8
|
|
4
|
+
README. It is included only for reference. Please see README.md for
|
|
5
|
+
information specific to libjpeg-turbo.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
The Independent JPEG Group's JPEG software
|
|
9
|
+
==========================================
|
|
10
|
+
|
|
11
|
+
This distribution contains a release of the Independent JPEG Group's free JPEG
|
|
12
|
+
software. You are welcome to redistribute this software and to use it for any
|
|
13
|
+
purpose, subject to the conditions under LEGAL ISSUES, below.
|
|
14
|
+
|
|
15
|
+
This software is the work of Tom Lane, Guido Vollbeding, Philip Gladstone,
|
|
16
|
+
Bill Allombert, Jim Boucher, Lee Crocker, Bob Friesenhahn, Ben Jackson,
|
|
17
|
+
Julian Minguillon, Luis Ortiz, George Phillips, Davide Rossi, Ge' Weijers,
|
|
18
|
+
and other members of the Independent JPEG Group.
|
|
19
|
+
|
|
20
|
+
IJG is not affiliated with the ISO/IEC JTC1/SC29/WG1 standards committee
|
|
21
|
+
(also known as JPEG, together with ITU-T SG16).
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
DOCUMENTATION ROADMAP
|
|
25
|
+
=====================
|
|
26
|
+
|
|
27
|
+
This file contains the following sections:
|
|
28
|
+
|
|
29
|
+
OVERVIEW General description of JPEG and the IJG software.
|
|
30
|
+
LEGAL ISSUES Copyright, lack of warranty, terms of distribution.
|
|
31
|
+
REFERENCES Where to learn more about JPEG.
|
|
32
|
+
ARCHIVE LOCATIONS Where to find newer versions of this software.
|
|
33
|
+
FILE FORMAT WARS Software *not* to get.
|
|
34
|
+
TO DO Plans for future IJG releases.
|
|
35
|
+
|
|
36
|
+
Other documentation files in the distribution are:
|
|
37
|
+
|
|
38
|
+
User documentation:
|
|
39
|
+
doc/usage.txt Usage instructions for cjpeg, djpeg, jpegtran,
|
|
40
|
+
rdjpgcom, and wrjpgcom.
|
|
41
|
+
doc/*.1 Unix-style man pages for programs (same info as
|
|
42
|
+
usage.txt).
|
|
43
|
+
doc/wizard.txt Advanced usage instructions for JPEG wizards only.
|
|
44
|
+
doc/change.log Version-to-version change highlights.
|
|
45
|
+
Programmer and internal documentation:
|
|
46
|
+
doc/libjpeg.txt How to use the JPEG library in your own programs.
|
|
47
|
+
src/example.c Sample code for calling the JPEG library.
|
|
48
|
+
doc/structure.txt Overview of the JPEG library's internal structure.
|
|
49
|
+
doc/coderules.txt Coding style rules --- please read if you contribute
|
|
50
|
+
code.
|
|
51
|
+
|
|
52
|
+
Please read at least usage.txt. Some information can also be found in the JPEG
|
|
53
|
+
FAQ (Frequently Asked Questions) article. See ARCHIVE LOCATIONS below to find
|
|
54
|
+
out where to obtain the FAQ article.
|
|
55
|
+
|
|
56
|
+
If you want to understand how the JPEG code works, we suggest reading one or
|
|
57
|
+
more of the REFERENCES, then looking at the documentation files (in roughly
|
|
58
|
+
the order listed) before diving into the code.
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
OVERVIEW
|
|
62
|
+
========
|
|
63
|
+
|
|
64
|
+
This package contains C software to implement JPEG image encoding, decoding,
|
|
65
|
+
and transcoding. JPEG (pronounced "jay-peg") is a standardized compression
|
|
66
|
+
method for full-color and grayscale images. JPEG's strong suit is compressing
|
|
67
|
+
photographic images or other types of images that have smooth color and
|
|
68
|
+
brightness transitions between neighboring pixels. Images with sharp lines or
|
|
69
|
+
other abrupt features may not compress well with JPEG, and a higher JPEG
|
|
70
|
+
quality may have to be used to avoid visible compression artifacts with such
|
|
71
|
+
images.
|
|
72
|
+
|
|
73
|
+
JPEG is normally lossy, meaning that the output pixels are not necessarily
|
|
74
|
+
identical to the input pixels. However, on photographic content and other
|
|
75
|
+
"smooth" images, very good compression ratios can be obtained with no visible
|
|
76
|
+
compression artifacts, and extremely high compression ratios are possible if
|
|
77
|
+
you are willing to sacrifice image quality (by reducing the "quality" setting
|
|
78
|
+
in the compressor.)
|
|
79
|
+
|
|
80
|
+
This software implements JPEG baseline, extended-sequential, progressive, and
|
|
81
|
+
lossless compression processes. Provision is made for supporting all variants
|
|
82
|
+
of these processes, although some uncommon parameter settings aren't
|
|
83
|
+
implemented yet. We have made no provision for supporting the hierarchical
|
|
84
|
+
processes defined in the standard.
|
|
85
|
+
|
|
86
|
+
We provide a set of library routines for reading and writing JPEG image files,
|
|
87
|
+
plus two sample applications "cjpeg" and "djpeg", which use the library to
|
|
88
|
+
perform conversion between JPEG and some other popular image file formats.
|
|
89
|
+
The library is intended to be reused in other applications.
|
|
90
|
+
|
|
91
|
+
In order to support file conversion and viewing software, we have included
|
|
92
|
+
considerable functionality beyond the bare JPEG coding/decoding capability;
|
|
93
|
+
for example, the color quantization modules are not strictly part of JPEG
|
|
94
|
+
decoding, but they are essential for output to colormapped file formats. These
|
|
95
|
+
extra functions can be compiled out of the library if not required for a
|
|
96
|
+
particular application.
|
|
97
|
+
|
|
98
|
+
We have also included "jpegtran", a utility for lossless transcoding between
|
|
99
|
+
different JPEG processes, and "rdjpgcom" and "wrjpgcom", two simple
|
|
100
|
+
applications for inserting and extracting textual comments in JFIF files.
|
|
101
|
+
|
|
102
|
+
The emphasis in designing this software has been on achieving portability and
|
|
103
|
+
flexibility, while also making it fast enough to be useful. In particular,
|
|
104
|
+
the software is not intended to be read as a tutorial on JPEG. (See the
|
|
105
|
+
REFERENCES section for introductory material.) Rather, it is intended to
|
|
106
|
+
be reliable, portable, industrial-strength code. We do not claim to have
|
|
107
|
+
achieved that goal in every aspect of the software, but we strive for it.
|
|
108
|
+
|
|
109
|
+
We welcome the use of this software as a component of commercial products.
|
|
110
|
+
No royalty is required, but we do ask for an acknowledgement in product
|
|
111
|
+
documentation, as described under LEGAL ISSUES.
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
LEGAL ISSUES
|
|
115
|
+
============
|
|
116
|
+
|
|
117
|
+
In plain English:
|
|
118
|
+
|
|
119
|
+
1. We don't promise that this software works. (But if you find any bugs,
|
|
120
|
+
please let us know!)
|
|
121
|
+
2. You can use this software for whatever you want. You don't have to pay us.
|
|
122
|
+
3. You may not pretend that you wrote this software. If you use it in a
|
|
123
|
+
program, you must acknowledge somewhere in your documentation that
|
|
124
|
+
you've used the IJG code.
|
|
125
|
+
|
|
126
|
+
In legalese:
|
|
127
|
+
|
|
128
|
+
The authors make NO WARRANTY or representation, either express or implied,
|
|
129
|
+
with respect to this software, its quality, accuracy, merchantability, or
|
|
130
|
+
fitness for a particular purpose. This software is provided "AS IS", and you,
|
|
131
|
+
its user, assume the entire risk as to its quality and accuracy.
|
|
132
|
+
|
|
133
|
+
This software is copyright (C) 1991-2020, Thomas G. Lane, Guido Vollbeding.
|
|
134
|
+
All Rights Reserved except as specified below.
|
|
135
|
+
|
|
136
|
+
Permission is hereby granted to use, copy, modify, and distribute this
|
|
137
|
+
software (or portions thereof) for any purpose, without fee, subject to these
|
|
138
|
+
conditions:
|
|
139
|
+
(1) If any part of the source code for this software is distributed, then this
|
|
140
|
+
README file must be included, with this copyright and no-warranty notice
|
|
141
|
+
unaltered; and any additions, deletions, or changes to the original files
|
|
142
|
+
must be clearly indicated in accompanying documentation.
|
|
143
|
+
(2) If only executable code is distributed, then the accompanying
|
|
144
|
+
documentation must state that "this software is based in part on the work of
|
|
145
|
+
the Independent JPEG Group".
|
|
146
|
+
(3) Permission for use of this software is granted only if the user accepts
|
|
147
|
+
full responsibility for any undesirable consequences; the authors accept
|
|
148
|
+
NO LIABILITY for damages of any kind.
|
|
149
|
+
|
|
150
|
+
These conditions apply to any software derived from or based on the IJG code,
|
|
151
|
+
not just to the unmodified library. If you use our work, you ought to
|
|
152
|
+
acknowledge us.
|
|
153
|
+
|
|
154
|
+
Permission is NOT granted for the use of any IJG author's name or company name
|
|
155
|
+
in advertising or publicity relating to this software or products derived from
|
|
156
|
+
it. This software may be referred to only as "the Independent JPEG Group's
|
|
157
|
+
software".
|
|
158
|
+
|
|
159
|
+
We specifically permit and encourage the use of this software as the basis of
|
|
160
|
+
commercial products, provided that all warranty or liability claims are
|
|
161
|
+
assumed by the product vendor.
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
REFERENCES
|
|
165
|
+
==========
|
|
166
|
+
|
|
167
|
+
We recommend reading one or more of these references before trying to
|
|
168
|
+
understand the innards of the JPEG software.
|
|
169
|
+
|
|
170
|
+
The best short technical introduction to the JPEG compression algorithm is
|
|
171
|
+
Wallace, Gregory K. "The JPEG Still Picture Compression Standard",
|
|
172
|
+
Communications of the ACM, April 1991 (vol. 34 no. 4), pp. 30-44.
|
|
173
|
+
(Adjacent articles in that issue discuss MPEG motion picture compression,
|
|
174
|
+
applications of JPEG, and related topics.) If you don't have the CACM issue
|
|
175
|
+
handy, a PDF file containing a revised version of Wallace's article is
|
|
176
|
+
available at http://www.ijg.org/files/Wallace.JPEG.pdf. The file (actually
|
|
177
|
+
a preprint for an article that appeared in IEEE Trans. Consumer Electronics)
|
|
178
|
+
omits the sample images that appeared in CACM, but it includes corrections
|
|
179
|
+
and some added material. Note: the Wallace article is copyright ACM and IEEE,
|
|
180
|
+
and it may not be used for commercial purposes.
|
|
181
|
+
|
|
182
|
+
A somewhat less technical, more leisurely introduction to JPEG can be found in
|
|
183
|
+
"The Data Compression Book" by Mark Nelson and Jean-loup Gailly, published by
|
|
184
|
+
M&T Books (New York), 2nd ed. 1996, ISBN 1-55851-434-1. This book provides
|
|
185
|
+
good explanations and example C code for a multitude of compression methods
|
|
186
|
+
including JPEG. It is an excellent source if you are comfortable reading C
|
|
187
|
+
code but don't know much about data compression in general. The book's JPEG
|
|
188
|
+
sample code is far from industrial-strength, but when you are ready to look
|
|
189
|
+
at a full implementation, you've got one here...
|
|
190
|
+
|
|
191
|
+
The best currently available description of JPEG is the textbook "JPEG Still
|
|
192
|
+
Image Data Compression Standard" by William B. Pennebaker and Joan L.
|
|
193
|
+
Mitchell, published by Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1.
|
|
194
|
+
Price US$59.95, 638 pp. The book includes the complete text of the ISO JPEG
|
|
195
|
+
standards (DIS 10918-1 and draft DIS 10918-2).
|
|
196
|
+
|
|
197
|
+
The original JPEG standard is divided into two parts, Part 1 being the actual
|
|
198
|
+
specification, while Part 2 covers compliance testing methods. Part 1 is
|
|
199
|
+
titled "Digital Compression and Coding of Continuous-tone Still Images,
|
|
200
|
+
Part 1: Requirements and guidelines" and has document numbers ISO/IEC IS
|
|
201
|
+
10918-1, ITU-T T.81. Part 2 is titled "Digital Compression and Coding of
|
|
202
|
+
Continuous-tone Still Images, Part 2: Compliance testing" and has document
|
|
203
|
+
numbers ISO/IEC IS 10918-2, ITU-T T.83.
|
|
204
|
+
|
|
205
|
+
The JPEG standard does not specify all details of an interchangeable file
|
|
206
|
+
format. For the omitted details, we follow the "JFIF" conventions, revision
|
|
207
|
+
1.02. JFIF version 1 has been adopted as ISO/IEC 10918-5 (05/2013) and
|
|
208
|
+
Recommendation ITU-T T.871 (05/2011): Information technology - Digital
|
|
209
|
+
compression and coding of continuous-tone still images: JPEG File Interchange
|
|
210
|
+
Format (JFIF). It is available as a free download in PDF file format from
|
|
211
|
+
https://www.iso.org/standard/54989.html and http://www.itu.int/rec/T-REC-T.871.
|
|
212
|
+
A PDF file of the older JFIF 1.02 specification is available at
|
|
213
|
+
http://www.w3.org/Graphics/JPEG/jfif3.pdf.
|
|
214
|
+
|
|
215
|
+
The TIFF 6.0 file format specification can be obtained from
|
|
216
|
+
http://mirrors.ctan.org/graphics/tiff/TIFF6.ps.gz. The JPEG incorporation
|
|
217
|
+
scheme found in the TIFF 6.0 spec of 3-June-92 has a number of serious
|
|
218
|
+
problems. IJG does not recommend use of the TIFF 6.0 design (TIFF Compression
|
|
219
|
+
tag 6). Instead, we recommend the JPEG design proposed by TIFF Technical Note
|
|
220
|
+
#2 (Compression tag 7). Copies of this Note can be obtained from
|
|
221
|
+
http://www.ijg.org/files/. It is expected that the next revision
|
|
222
|
+
of the TIFF spec will replace the 6.0 JPEG design with the Note's design.
|
|
223
|
+
Although IJG's own code does not support TIFF/JPEG, the free libtiff library
|
|
224
|
+
uses our library to implement TIFF/JPEG per the Note.
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
ARCHIVE LOCATIONS
|
|
228
|
+
=================
|
|
229
|
+
|
|
230
|
+
The "official" archive site for this software is www.ijg.org.
|
|
231
|
+
The most recent released version can always be found there in
|
|
232
|
+
directory "files".
|
|
233
|
+
|
|
234
|
+
The JPEG FAQ (Frequently Asked Questions) article is a source of some
|
|
235
|
+
general information about JPEG. It is available at
|
|
236
|
+
http://www.faqs.org/faqs/jpeg-faq.
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
FILE FORMAT COMPATIBILITY
|
|
240
|
+
=========================
|
|
241
|
+
|
|
242
|
+
This software implements ITU T.81 | ISO/IEC 10918 with some extensions from
|
|
243
|
+
ITU T.871 | ISO/IEC 10918-5 (JPEG File Interchange Format-- see REFERENCES).
|
|
244
|
+
Informally, the term "JPEG image" or "JPEG file" most often refers to JFIF or
|
|
245
|
+
a subset thereof, but there are other formats containing the name "JPEG" that
|
|
246
|
+
are incompatible with the original JPEG standard or with JFIF (for instance,
|
|
247
|
+
JPEG 2000 and JPEG XR). This software therefore does not support these
|
|
248
|
+
formats. Indeed, one of the original reasons for developing this free software
|
|
249
|
+
was to help force convergence on a common, interoperable format standard for
|
|
250
|
+
JPEG files.
|
|
251
|
+
|
|
252
|
+
JFIF is a minimal or "low end" representation. TIFF/JPEG (TIFF revision 6.0 as
|
|
253
|
+
modified by TIFF Technical Note #2) can be used for "high end" applications
|
|
254
|
+
that need to record a lot of additional data about an image.
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
TO DO
|
|
258
|
+
=====
|
|
259
|
+
|
|
260
|
+
Please send bug reports, offers of help, etc. to jpeg-info@jpegclub.org.
|
data/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
Convert PNG or other pixel data to JPEG. Or the other way! Implements baseline JPEG encoding (DCT, Huffman, 4:2:0 chroma subsampling) and decodes both baseline and progressive JPEGs. Exposes a variety of encoding options to adjust parts of the JPEG pipeline not normally available (I needed this to recreate the JPEG compression styles of older digital cameras - don't ask..)
|
|
8
8
|
|
|
9
|
-
It works on CRuby 3.0+, TruffleRuby 33.0, JRuby 10.0, and [Spinel](https://github.com/matz/spinel) (tested at `7f08abd9`).
|
|
9
|
+
It works on CRuby 3.0+, TruffleRuby 33.0, JRuby 10.0, and [Spinel](https://github.com/matz/spinel) (tested at `7f08abd9`). Tests include external JPEG fixtures with pixel expectations generated by libjpeg-turbo, alongside round-trip and malformed-input checks.
|
|
10
10
|
|
|
11
11
|
> [!NOTE]
|
|
12
12
|
> Rubyists might find the [AI Disclosure](#ai-disclosure) section below of interest.
|
|
@@ -23,6 +23,15 @@ gem "pure_jpeg"
|
|
|
23
23
|
gem install pure_jpeg
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
+
> [!NOTE]
|
|
27
|
+
> Using [Spinel](https://github.com/matz/spinel)? Add PureJPEG from your Spin project directory:
|
|
28
|
+
>
|
|
29
|
+
> ```sh
|
|
30
|
+
> spin add pure_jpeg --git https://github.com/peterc/pure_jpeg
|
|
31
|
+
> ```
|
|
32
|
+
>
|
|
33
|
+
> Then use `require "pure_jpeg"` in your code as usual.
|
|
34
|
+
|
|
26
35
|
There are no runtime dependencies. [ChunkyPNG](https://github.com/wvanbergen/chunky_png) is optional (though quite useful) if you want to use `from_chunky_png`.
|
|
27
36
|
|
|
28
37
|
`examples/` contains some useful example scripts for basic JPEG to PNG and PNG to JPEG conversion if you want to do some quick tests without writing code.
|
|
@@ -151,8 +160,13 @@ info.width # => 1024
|
|
|
151
160
|
info.height # => 768
|
|
152
161
|
info.component_count # => 3
|
|
153
162
|
info.progressive # => false
|
|
163
|
+
info.icc_profile # => binary profile bytes, or nil
|
|
154
164
|
```
|
|
155
165
|
|
|
166
|
+
Metadata queries read headers through the first scan marker, without decoding
|
|
167
|
+
pixels. Incomplete or inconsistent multipart ICC profiles are ignored and
|
|
168
|
+
reported as `nil`. ICC extraction does not perform color-profile conversion.
|
|
169
|
+
|
|
156
170
|
### Iterating pixels
|
|
157
171
|
|
|
158
172
|
```ruby
|
|
@@ -198,8 +212,17 @@ Decoding:
|
|
|
198
212
|
- 1-component (grayscale) and 3-component (YCbCr) images
|
|
199
213
|
- Any chroma subsampling factor (4:4:4, 4:2:2, 4:2:0, etc.)
|
|
200
214
|
- Restart markers (DRI/RST)
|
|
215
|
+
- Separate component scans in baseline JPEGs
|
|
216
|
+
|
|
217
|
+
Encoding and decoding allow dimensions up to 8192 pixels on each axis. The
|
|
218
|
+
decoder allocates whole-image buffers; this dimension limit is not a memory
|
|
219
|
+
budget. `PureJPEG.info` can inspect dimensions without allocating pixel buffers.
|
|
220
|
+
Invalid or unsupported JPEG data raises `PureJPEG::DecodeError`.
|
|
201
221
|
|
|
202
|
-
Not supported: arithmetic coding, 12-bit precision,
|
|
222
|
+
Not supported: arithmetic coding, 12-bit precision, RGB/CMYK JPEG color spaces,
|
|
223
|
+
or EXIF/ICC profile preservation when re-encoding. Transparent PNG sources can
|
|
224
|
+
be composited with the explicit `background:` option above. Raise an issue if
|
|
225
|
+
you need one of the unsupported formats!
|
|
203
226
|
|
|
204
227
|
Possible future improvements: ICC profile rendering/conversion.
|
|
205
228
|
|
|
@@ -215,7 +238,7 @@ On the bundled 1024x1024 images (Apple M5, median of 15 runs after 20 seconds of
|
|
|
215
238
|
|
|
216
239
|
Inputs were loaded before timing, and encoded output was returned in memory; file I/O and PNG loading are excluded. Runtimes were benchmarked sequentially. JRuby used its default JIT on OpenJDK 21.0.10. Spinel was measured separately with the same inputs and timing protocol, using a portable harness with PNG pixels preloaded; its encoded JPEG and decoded pixels matched CRuby byte-for-byte. Results vary with image content, garbage collection, and JIT warmup.
|
|
217
240
|
|
|
218
|
-
The encoder and decoder use
|
|
241
|
+
The encoder and decoder use the IJG accurate integer DCT, based on the Loeffler factorization, with fixed-point arithmetic throughout — no Float operations in the hot path. Color space conversion uses fixed-point integer math, and pixel data is stored as packed integers to avoid per-pixel object allocation. TruffleRuby's Graal JIT compiler can optimize these tight integer loops particularly well once warmed up.
|
|
219
242
|
|
|
220
243
|
## Example scripts
|
|
221
244
|
|
|
@@ -266,6 +289,19 @@ rake benchmark # basic benchmark of encoding and decoding (5 runs after warmup
|
|
|
266
289
|
rake profile # CPU profile with StackProf (requires the stackprof gem)
|
|
267
290
|
```
|
|
268
291
|
|
|
292
|
+
The Minitest suite lives in `test/minitest/`; `rake` and `rake test` run it
|
|
293
|
+
with SimpleCov as before. To run one file:
|
|
294
|
+
|
|
295
|
+
```sh
|
|
296
|
+
bundle exec rake test TEST=test/minitest/test_raw_source.rb
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
With Spinel and Spin installed, `spin test` runs the standalone package checks
|
|
300
|
+
in `test/*.rb`, comparing their output with CRuby. These cover color/grayscale
|
|
301
|
+
round trips, metadata, and invalid input; they supplement the full Minitest
|
|
302
|
+
suite. See [test/SPINEL.md](test/SPINEL.md) for details and the existing
|
|
303
|
+
`rake test:spinel` runner.
|
|
304
|
+
|
|
269
305
|
## Full benchmark script
|
|
270
306
|
|
|
271
307
|
`benchmark/run.rb` is a more thorough benchmark that exercises the encode and decode paths in several ways. It auto-enables YJIT, warms up before measuring, and reports object allocations, throughput (iterations/second via `benchmark-ips`), best-of-N wall-clock times, and a sustained mixed workload across encode (q85, q95 optimized, grayscale) and decode (baseline and progressive).
|
|
@@ -301,4 +337,6 @@ ruby benchmark/run.rb --profile-alloc # retained-object profile with Vernier
|
|
|
301
337
|
|
|
302
338
|
## License
|
|
303
339
|
|
|
304
|
-
MIT
|
|
340
|
+
MIT, with IJG terms for the adapted integer DCT implementation. This software
|
|
341
|
+
is based in part on the work of the Independent JPEG Group. See [LICENSE](LICENSE)
|
|
342
|
+
and [LICENSE-IJG](LICENSE-IJG) for the notices and adaptation details.
|
data/lib/pure_jpeg/bit_reader.rb
CHANGED
|
@@ -35,6 +35,26 @@ module PureJPEG
|
|
|
35
35
|
@buffer = 0
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
+
# Consume exactly the restart expected at an MCU boundary. Marker fill
|
|
39
|
+
# bytes are allowed; entropy bytes and out-of-sequence restarts are not.
|
|
40
|
+
def read_restart(expected)
|
|
41
|
+
reset
|
|
42
|
+
unless @data.getbyte(@pos) == 0xFF
|
|
43
|
+
raise DecodeError, "Missing JPEG restart marker"
|
|
44
|
+
end
|
|
45
|
+
@pos += 1 while @pos < @length && @data.getbyte(@pos) == 0xFF
|
|
46
|
+
unless @data.getbyte(@pos) == 0xD0 + expected
|
|
47
|
+
raise DecodeError, "Unexpected JPEG restart marker (expected RST#{expected})"
|
|
48
|
+
end
|
|
49
|
+
@pos += 1
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def finish_scan!
|
|
53
|
+
# Remaining bits in the last byte are padding, but whole unread bytes
|
|
54
|
+
# indicate extra entropy data or an unexpected trailing restart marker.
|
|
55
|
+
raise DecodeError, "Extra data after JPEG scan" unless @pos == @length
|
|
56
|
+
end
|
|
57
|
+
|
|
38
58
|
# Read additional bits and sign-extend per JPEG spec (receive/extend).
|
|
39
59
|
def receive_extend(size)
|
|
40
60
|
return 0 if size == 0
|
|
@@ -56,10 +76,7 @@ module PureJPEG
|
|
|
56
76
|
raise PureJPEG::DecodeError, "Unexpected end of scan data" if @pos >= @length
|
|
57
77
|
next_byte = @data.getbyte(@pos)
|
|
58
78
|
@pos += 1
|
|
59
|
-
|
|
60
|
-
# Skip restart markers (0xD0-0xD7)
|
|
61
|
-
return fill_buffer if next_byte != 0x00 && next_byte >= 0xD0 && next_byte <= 0xD7
|
|
62
|
-
# For 0x00, the byte is 0xFF (stuffing removed)
|
|
79
|
+
raise DecodeError, "Unexpected marker in JPEG entropy data" unless next_byte == 0x00
|
|
63
80
|
end
|
|
64
81
|
@buffer = byte
|
|
65
82
|
@bits_in_buffer = 8
|
data/lib/pure_jpeg/dct.rb
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
module PureJPEG
|
|
4
4
|
# Integer-scaled DCT based on the IJG (Independent JPEG Group) reference
|
|
5
|
-
# implementation (jfdctint.c / jidctint.c). Uses the
|
|
6
|
-
# factorization with 13-bit fixed-point constants.
|
|
5
|
+
# implementation (jfdctint.c / jidctint.c). Uses the Loeffler-based accurate
|
|
6
|
+
# integer factorization with 13-bit fixed-point constants. The upstream
|
|
7
|
+
# copyright and distribution terms are included in LICENSE-IJG; adaptation
|
|
8
|
+
# details are recorded in LICENSE.
|
|
7
9
|
#
|
|
8
10
|
# All arithmetic is pure Integer (additions, shifts, multiplies) — no Float
|
|
9
11
|
# operations. This is ~3x faster than the matrix-multiply float DCT under
|
data/lib/pure_jpeg/decoder.rb
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module PureJPEG
|
|
4
|
-
# Baseline JPEG decoder.
|
|
4
|
+
# Baseline and progressive JPEG decoder.
|
|
5
5
|
#
|
|
6
|
-
# Decodes baseline
|
|
7
|
-
#
|
|
6
|
+
# Decodes baseline (SOF0) and progressive (SOF2) 8-bit grayscale/YCbCr
|
|
7
|
+
# JPEGs, including separate component scans and restart markers.
|
|
8
8
|
#
|
|
9
9
|
# Use {PureJPEG.read} for a convenient entry point.
|
|
10
10
|
class Decoder
|
|
@@ -13,7 +13,7 @@ module PureJPEG
|
|
|
13
13
|
# @param path_or_data [String] a file path or raw JPEG bytes
|
|
14
14
|
# @return [Image] decoded image with pixel access
|
|
15
15
|
def self.decode(path_or_data)
|
|
16
|
-
data = if path_or_data.is_a?(String) && !path_or_data.start_with?("\xFF\xD8".b) && File.exist?(path_or_data)
|
|
16
|
+
data = if path_or_data.is_a?(String) && !path_or_data.b.include?("\0") && !path_or_data.b.start_with?("\xFF\xD8".b) && File.exist?(path_or_data)
|
|
17
17
|
File.binread(path_or_data)
|
|
18
18
|
else
|
|
19
19
|
path_or_data.b
|
|
@@ -29,90 +29,74 @@ module PureJPEG
|
|
|
29
29
|
jfif = JFIFReader.new(@data)
|
|
30
30
|
@icc_profile = jfif.icc_profile
|
|
31
31
|
validate_dimensions!(jfif.width, jfif.height)
|
|
32
|
+
validate_frame_and_scans!(jfif)
|
|
32
33
|
return decode_progressive(jfif) if jfif.progressive
|
|
33
34
|
|
|
34
35
|
width = jfif.width
|
|
35
36
|
height = jfif.height
|
|
36
37
|
|
|
37
|
-
# Build Huffman decode tables
|
|
38
|
-
dc_tables = {}
|
|
39
|
-
ac_tables = {}
|
|
40
|
-
jfif.huffman_tables.each do |(table_class, table_id), info|
|
|
41
|
-
table = Huffman::DecodeTable.new(info[:bits], info[:values])
|
|
42
|
-
if table_class == 0
|
|
43
|
-
dc_tables[table_id] = table
|
|
44
|
-
else
|
|
45
|
-
ac_tables[table_id] = table
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
# Map component IDs to their info
|
|
50
38
|
comp_info = {}
|
|
51
39
|
jfif.components.each { |c| comp_info[c.id] = c }
|
|
52
|
-
|
|
53
|
-
# Determine max sampling factors
|
|
54
40
|
max_h = jfif.components.map(&:h_sampling).max
|
|
55
41
|
max_v = jfif.components.map(&:v_sampling).max
|
|
56
|
-
|
|
57
|
-
# MCU dimensions in pixels
|
|
58
42
|
mcu_px_w = max_h * 8
|
|
59
43
|
mcu_px_h = max_v * 8
|
|
60
44
|
mcus_x = (width + mcu_px_w - 1) / mcu_px_w
|
|
61
45
|
mcus_y = (height + mcu_px_h - 1) / mcu_px_h
|
|
62
46
|
|
|
63
|
-
# Allocate channel buffers (full padded size)
|
|
64
|
-
padded_w = mcus_x * mcu_px_w
|
|
65
|
-
padded_h = mcus_y * mcu_px_h
|
|
66
47
|
channels = {}
|
|
67
48
|
jfif.components.each do |c|
|
|
68
|
-
ch_w =
|
|
69
|
-
ch_h =
|
|
49
|
+
ch_w = mcus_x * c.h_sampling * 8
|
|
50
|
+
ch_h = mcus_y * c.v_sampling * 8
|
|
70
51
|
channels[c.id] = { data: Array.new(ch_w * ch_h, 0), width: ch_w, height: ch_h }
|
|
71
52
|
end
|
|
72
|
-
|
|
73
|
-
# Decode scan data
|
|
74
|
-
reader = BitReader.new(jfif.scan_data)
|
|
75
|
-
prev_dc = Hash.new(0)
|
|
76
|
-
restart_interval = jfif.restart_interval
|
|
77
|
-
mcu_count = 0
|
|
78
|
-
|
|
79
|
-
# Reusable buffers
|
|
80
53
|
zigzag = Array.new(64, 0)
|
|
81
54
|
dequant = Array.new(64, 0)
|
|
82
55
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
# Inverse pipeline: unzigzag -> dequantize -> IDCT -> level shift
|
|
103
|
-
Quantization.dequantize_zigzag!(zigzag, qt, dequant)
|
|
104
|
-
DCT.inverse!(dequant)
|
|
56
|
+
jfif.scans.each do |scan|
|
|
57
|
+
dc_tables, ac_tables = build_decode_tables(scan)
|
|
58
|
+
reader = BitReader.new(scan.data)
|
|
59
|
+
prev_dc = Hash.new(0)
|
|
60
|
+
restart_interval = scan.restart_interval
|
|
61
|
+
mcu_count = 0
|
|
62
|
+
interleaved = scan.components.length > 1
|
|
63
|
+
scan_components = scan.components.map do |sc|
|
|
64
|
+
comp, dc_tab, ac_tab = resolve_scan_references!(sc, comp_info, dc_tables, ac_tables)
|
|
65
|
+
[sc, comp, dc_tab, ac_tab, fetch_quant_table!(jfif, comp), channels[comp.id]]
|
|
66
|
+
end
|
|
67
|
+
if interleaved
|
|
68
|
+
scan_width = mcus_x
|
|
69
|
+
scan_height = mcus_y
|
|
70
|
+
else
|
|
71
|
+
comp = scan_components[0][1]
|
|
72
|
+
scan_width = (width * comp.h_sampling + mcu_px_w - 1) / mcu_px_w
|
|
73
|
+
scan_height = (height * comp.v_sampling + mcu_px_h - 1) / mcu_px_h
|
|
74
|
+
end
|
|
105
75
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
76
|
+
scan_height.times do |mcu_row|
|
|
77
|
+
scan_width.times do |mcu_col|
|
|
78
|
+
if restart_interval > 0 && mcu_count > 0 && (mcu_count % restart_interval) == 0
|
|
79
|
+
reader.read_restart((mcu_count / restart_interval - 1) % 8)
|
|
80
|
+
prev_dc.clear
|
|
81
|
+
end
|
|
82
|
+
scan_components.each do |sc, comp, dc_tab, ac_tab, qt, ch|
|
|
83
|
+
block_rows = interleaved ? comp.v_sampling : 1
|
|
84
|
+
block_cols = interleaved ? comp.h_sampling : 1
|
|
85
|
+
block_rows.times do |bv|
|
|
86
|
+
block_cols.times do |bh|
|
|
87
|
+
decode_block(reader, dc_tab, ac_tab, prev_dc, sc.id, zigzag)
|
|
88
|
+
Quantization.dequantize_zigzag!(zigzag, qt, dequant)
|
|
89
|
+
DCT.inverse!(dequant)
|
|
90
|
+
bx = (mcu_col * block_cols + bh) * 8
|
|
91
|
+
by = (mcu_row * block_rows + bv) * 8
|
|
92
|
+
write_block(dequant, ch[:data], ch[:width], bx, by)
|
|
93
|
+
end
|
|
110
94
|
end
|
|
111
95
|
end
|
|
96
|
+
mcu_count += 1
|
|
112
97
|
end
|
|
113
|
-
|
|
114
|
-
mcu_count += 1
|
|
115
98
|
end
|
|
99
|
+
reader.finish_scan!
|
|
116
100
|
end
|
|
117
101
|
|
|
118
102
|
# Assemble pixels
|
|
@@ -128,7 +112,85 @@ module PureJPEG
|
|
|
128
112
|
|
|
129
113
|
private
|
|
130
114
|
|
|
115
|
+
def build_decode_tables(scan)
|
|
116
|
+
dc_tables = {}
|
|
117
|
+
ac_tables = {}
|
|
118
|
+
scan.huffman_tables.each do |(table_class, table_id), info|
|
|
119
|
+
table = Huffman::DecodeTable.new(info[:bits], info[:values])
|
|
120
|
+
if table_class == 0
|
|
121
|
+
dc_tables[table_id] = table
|
|
122
|
+
else
|
|
123
|
+
ac_tables[table_id] = table
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
[dc_tables, ac_tables]
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def validate_frame_and_scans!(jfif)
|
|
130
|
+
unless jfif.precision == 8
|
|
131
|
+
raise DecodeError, "Unsupported JPEG precision: #{jfif.precision}"
|
|
132
|
+
end
|
|
133
|
+
count = jfif.components.length
|
|
134
|
+
unless count == 1 || count == 3
|
|
135
|
+
raise DecodeError, "Unsupported number of components: #{count}"
|
|
136
|
+
end
|
|
137
|
+
ids = jfif.components.map(&:id)
|
|
138
|
+
if count == 3 && (jfif.adobe_transform == 0 || ids.sort == [66, 71, 82])
|
|
139
|
+
raise DecodeError, "RGB JPEG decoding is unsupported"
|
|
140
|
+
end
|
|
141
|
+
if jfif.adobe_transform && ![0, 1].include?(jfif.adobe_transform)
|
|
142
|
+
raise DecodeError, "Unsupported Adobe color transform: #{jfif.adobe_transform}"
|
|
143
|
+
end
|
|
144
|
+
raise DecodeError, "JPEG scan data not found" if jfif.scans.empty?
|
|
145
|
+
|
|
146
|
+
progression = {}
|
|
147
|
+
ids.each { |id| progression[id] = Array.new(64) }
|
|
148
|
+
jfif.scans.each do |scan|
|
|
149
|
+
scan_ids = scan.components.map(&:id)
|
|
150
|
+
if scan_ids.empty? || scan_ids.uniq.length != scan_ids.length
|
|
151
|
+
raise DecodeError, "Invalid JPEG scan components"
|
|
152
|
+
end
|
|
153
|
+
scan_ids.each do |id|
|
|
154
|
+
raise DecodeError, "Scan references unknown component id #{id}" unless progression.key?(id)
|
|
155
|
+
end
|
|
156
|
+
if scan_ids.length > 1
|
|
157
|
+
blocks = jfif.components.select { |c| scan_ids.include?(c.id) }.sum { |c| c.h_sampling * c.v_sampling }
|
|
158
|
+
raise DecodeError, "Too many blocks in JPEG MCU" if blocks > 10
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
ss = scan.spectral_start
|
|
162
|
+
se = scan.spectral_end
|
|
163
|
+
ah = scan.successive_high
|
|
164
|
+
al = scan.successive_low
|
|
165
|
+
if jfif.progressive
|
|
166
|
+
valid = ss <= se && se <= 63 && ah <= 13 && al <= 13 &&
|
|
167
|
+
(ss == 0 ? se == 0 : scan_ids.length == 1) &&
|
|
168
|
+
(ah == 0 || al == ah - 1)
|
|
169
|
+
raise DecodeError, "Invalid progressive scan parameters" unless valid
|
|
170
|
+
elsif ss != 0 || se != 63 || ah != 0 || al != 0
|
|
171
|
+
raise DecodeError, "Invalid baseline scan parameters"
|
|
172
|
+
end
|
|
173
|
+
scan_ids.each do |id|
|
|
174
|
+
state = progression[id]
|
|
175
|
+
if ss > 0 && state[0].nil?
|
|
176
|
+
raise DecodeError, "Progressive AC scan precedes DC scan"
|
|
177
|
+
end
|
|
178
|
+
ss.upto(se) do |k|
|
|
179
|
+
unless ah == 0 ? state[k].nil? : state[k] == ah
|
|
180
|
+
raise DecodeError, "Invalid JPEG scan progression"
|
|
181
|
+
end
|
|
182
|
+
state[k] = al
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
if progression.values.any? { |state| state[0].nil? }
|
|
187
|
+
raise DecodeError, "JPEG scan omits a frame component"
|
|
188
|
+
end
|
|
189
|
+
jfif.components.each { |c| fetch_quant_table!(jfif, c) }
|
|
190
|
+
end
|
|
191
|
+
|
|
131
192
|
def validate_dimensions!(width, height)
|
|
193
|
+
raise DecodeError, "JPEG frame header not found" unless width && height
|
|
132
194
|
raise DecodeError, "Invalid image dimensions: #{width}x#{height}" if width <= 0 || height <= 0
|
|
133
195
|
raise DecodeError, "Image too large: #{width}x#{height} (max #{MAX_DIMENSION}x#{MAX_DIMENSION})" if width > MAX_DIMENSION || height > MAX_DIMENSION
|
|
134
196
|
end
|
|
@@ -157,12 +219,15 @@ module PureJPEG
|
|
|
157
219
|
bx = mcus_x * c.h_sampling
|
|
158
220
|
by = mcus_y * c.v_sampling
|
|
159
221
|
coeffs[c.id] = Array.new(bx * by * 64, 0)
|
|
160
|
-
|
|
222
|
+
actual_bx = (width * c.h_sampling + mcu_px_w - 1) / mcu_px_w
|
|
223
|
+
actual_by = (height * c.v_sampling + mcu_px_h - 1) / mcu_px_h
|
|
224
|
+
# Interleaved scans include MCU padding; single-component scans do not.
|
|
225
|
+
# Keep the storage stride separate from the actual scan dimensions.
|
|
226
|
+
comp_blocks[c.id] = [bx, by, actual_bx, actual_by]
|
|
161
227
|
end
|
|
162
228
|
|
|
163
|
-
restart_interval = jfif.restart_interval
|
|
164
|
-
|
|
165
229
|
jfif.scans.each do |scan|
|
|
230
|
+
restart_interval = scan.restart_interval
|
|
166
231
|
# Build Huffman tables from this scan's snapshot (tables change between scans)
|
|
167
232
|
dc_tables = {}
|
|
168
233
|
ac_tables = {}
|
|
@@ -188,6 +253,7 @@ module PureJPEG
|
|
|
188
253
|
prog_scan_interleaved(reader, scan, comp_info, dc_tables, ac_tables,
|
|
189
254
|
coeffs, comp_blocks, mcus_x, mcus_y, restart_interval, ss, se, ah, al)
|
|
190
255
|
end
|
|
256
|
+
reader.finish_scan!
|
|
191
257
|
end
|
|
192
258
|
|
|
193
259
|
# Reconstruct: unzigzag/dequantize, IDCT, write to channel buffers
|
|
@@ -231,18 +297,20 @@ module PureJPEG
|
|
|
231
297
|
def prog_scan_non_interleaved(reader, scan, comp_info, dc_tables, ac_tables,
|
|
232
298
|
coeffs, comp_blocks, restart_interval, ss, se, ah, al)
|
|
233
299
|
sc = scan.components[0]
|
|
234
|
-
comp, dc_tab, ac_tab = resolve_scan_references!(sc, comp_info, dc_tables, ac_tables,
|
|
300
|
+
comp, dc_tab, ac_tab = resolve_scan_references!(sc, comp_info, dc_tables, ac_tables,
|
|
301
|
+
require_dc: ss == 0 && ah == 0, require_ac: ss > 0)
|
|
235
302
|
coeff_buf = coeffs[comp.id]
|
|
236
|
-
bx_count,
|
|
303
|
+
bx_count, _by_count, actual_bx, actual_by = comp_blocks[comp.id]
|
|
237
304
|
|
|
238
305
|
prev_dc = 0
|
|
239
306
|
eobrun = 0
|
|
240
307
|
mcu_count = 0
|
|
241
308
|
|
|
242
|
-
|
|
243
|
-
|
|
309
|
+
actual_by.times do |block_y|
|
|
310
|
+
actual_bx.times do |block_x|
|
|
244
311
|
if restart_interval > 0 && mcu_count > 0 && (mcu_count % restart_interval) == 0
|
|
245
|
-
|
|
312
|
+
raise DecodeError, "Progressive EOB run crosses restart boundary" if eobrun > 0
|
|
313
|
+
reader.read_restart((mcu_count / restart_interval - 1) % 8)
|
|
246
314
|
prev_dc = 0
|
|
247
315
|
eobrun = 0
|
|
248
316
|
end
|
|
@@ -266,6 +334,7 @@ module PureJPEG
|
|
|
266
334
|
mcu_count += 1
|
|
267
335
|
end
|
|
268
336
|
end
|
|
337
|
+
raise DecodeError, "Progressive EOB run exceeds scan" if eobrun > 0
|
|
269
338
|
end
|
|
270
339
|
|
|
271
340
|
def prog_scan_interleaved(reader, scan, comp_info, dc_tables, ac_tables,
|
|
@@ -274,14 +343,15 @@ module PureJPEG
|
|
|
274
343
|
mcu_count = 0
|
|
275
344
|
|
|
276
345
|
scan_components = scan.components.map do |sc|
|
|
277
|
-
comp, dc_tab = resolve_scan_references!(sc, comp_info, dc_tables, ac_tables,
|
|
346
|
+
comp, dc_tab = resolve_scan_references!(sc, comp_info, dc_tables, ac_tables,
|
|
347
|
+
require_dc: ah == 0, require_ac: false)
|
|
278
348
|
[sc, comp, dc_tab, coeffs[comp.id], comp_blocks[comp.id][0]]
|
|
279
349
|
end
|
|
280
350
|
|
|
281
351
|
mcus_y.times do |mcu_row|
|
|
282
352
|
mcus_x.times do |mcu_col|
|
|
283
353
|
if restart_interval > 0 && mcu_count > 0 && (mcu_count % restart_interval) == 0
|
|
284
|
-
reader.
|
|
354
|
+
reader.read_restart((mcu_count / restart_interval - 1) % 8)
|
|
285
355
|
prev_dc.clear
|
|
286
356
|
end
|
|
287
357
|
|
|
@@ -330,6 +400,7 @@ module PureJPEG
|
|
|
330
400
|
if size == 0
|
|
331
401
|
if run == 15
|
|
332
402
|
k += 16
|
|
403
|
+
raise DecodeError, "AC run exceeds spectral band" if k > se + 1
|
|
333
404
|
else
|
|
334
405
|
# EOBn
|
|
335
406
|
eobrun = (1 << run)
|
|
@@ -338,6 +409,7 @@ module PureJPEG
|
|
|
338
409
|
end
|
|
339
410
|
else
|
|
340
411
|
k += run
|
|
412
|
+
raise DecodeError, "AC run exceeds spectral band" if k > se
|
|
341
413
|
coeff_buf[offset + k] = reader.receive_extend(size) << al
|
|
342
414
|
k += 1
|
|
343
415
|
end
|
|
@@ -367,6 +439,7 @@ module PureJPEG
|
|
|
367
439
|
# (the value bits come before refinement bits in the bitstream)
|
|
368
440
|
new_value = nil
|
|
369
441
|
if s != 0
|
|
442
|
+
raise DecodeError, "Invalid progressive AC refinement symbol" unless s == 1
|
|
370
443
|
new_value = reader.receive_extend(s) << al
|
|
371
444
|
elsif r != 15
|
|
372
445
|
# EOBn: refine remaining nonzero coefficients in this block
|
|
@@ -393,7 +466,8 @@ module PureJPEG
|
|
|
393
466
|
end
|
|
394
467
|
|
|
395
468
|
# Place new coefficient at the target zero position
|
|
396
|
-
|
|
469
|
+
raise DecodeError, "AC refinement run exceeds spectral band" if k > se
|
|
470
|
+
if new_value
|
|
397
471
|
coeff_buf[offset + k] = new_value
|
|
398
472
|
end
|
|
399
473
|
k += 1
|
|
@@ -429,6 +503,7 @@ module PureJPEG
|
|
|
429
503
|
end
|
|
430
504
|
break
|
|
431
505
|
elsif symbol == 0xF0 # ZRL (16 zeros)
|
|
506
|
+
raise DecodeError, "AC run exceeds block" if i + 16 > 64
|
|
432
507
|
16.times do
|
|
433
508
|
out[i] = 0
|
|
434
509
|
i += 1
|
|
@@ -436,6 +511,8 @@ module PureJPEG
|
|
|
436
511
|
else
|
|
437
512
|
run = (symbol >> 4) & 0x0F
|
|
438
513
|
size = symbol & 0x0F
|
|
514
|
+
raise DecodeError, "Invalid baseline AC symbol" if size == 0
|
|
515
|
+
raise DecodeError, "AC run exceeds block" if i + run >= 64
|
|
439
516
|
run.times do
|
|
440
517
|
out[i] = 0
|
|
441
518
|
i += 1
|
|
@@ -464,12 +541,14 @@ module PureJPEG
|
|
|
464
541
|
end
|
|
465
542
|
end
|
|
466
543
|
|
|
467
|
-
def resolve_scan_references!(scan_component, comp_info, dc_tables, ac_tables, require_ac: true)
|
|
544
|
+
def resolve_scan_references!(scan_component, comp_info, dc_tables, ac_tables, require_dc: true, require_ac: true)
|
|
468
545
|
comp = comp_info[scan_component.id]
|
|
469
546
|
raise DecodeError, "Scan references unknown component id #{scan_component.id}" unless comp
|
|
470
547
|
|
|
471
548
|
dc_tab = dc_tables[scan_component.dc_table_id]
|
|
472
|
-
|
|
549
|
+
if require_dc && !dc_tab
|
|
550
|
+
raise DecodeError, "Component #{scan_component.id} references missing DC Huffman table #{scan_component.dc_table_id}"
|
|
551
|
+
end
|
|
473
552
|
|
|
474
553
|
if require_ac
|
|
475
554
|
ac_tab = ac_tables[scan_component.ac_table_id]
|
|
@@ -480,7 +559,7 @@ module PureJPEG
|
|
|
480
559
|
end
|
|
481
560
|
|
|
482
561
|
def fetch_quant_table!(jfif, comp)
|
|
483
|
-
qt = jfif.
|
|
562
|
+
qt = jfif.component_quant_tables[comp.id]
|
|
484
563
|
raise DecodeError, "Component #{comp.id} references missing quantization table #{comp.qt_id}" unless qt
|
|
485
564
|
|
|
486
565
|
qt
|
|
@@ -521,6 +600,8 @@ module PureJPEG
|
|
|
521
600
|
cb_data = cb_ch[:data]
|
|
522
601
|
cr_data = cr_ch[:data]
|
|
523
602
|
y_stride = y_ch[:width]
|
|
603
|
+
y_h = y_comp.h_sampling
|
|
604
|
+
y_v = y_comp.v_sampling
|
|
524
605
|
cb_stride = cb_ch[:width]
|
|
525
606
|
cr_stride = cr_ch[:width]
|
|
526
607
|
cb_h = cb_comp.h_sampling
|
|
@@ -532,14 +613,14 @@ module PureJPEG
|
|
|
532
613
|
|
|
533
614
|
height.times do |py|
|
|
534
615
|
dst_row = py * width
|
|
535
|
-
y_row = py * y_stride
|
|
616
|
+
y_row = ((py * y_v) / max_v) * y_stride
|
|
536
617
|
|
|
537
618
|
# Chroma coordinates (nearest-neighbor upsampling)
|
|
538
619
|
cb_row = ((py * cb_v) / max_v) * cb_stride
|
|
539
620
|
cr_row = ((py * cr_v) / max_v) * cr_stride
|
|
540
621
|
|
|
541
622
|
width.times do |px|
|
|
542
|
-
lum = y_data[y_row + px]
|
|
623
|
+
lum = y_data[y_row + (px * y_h) / max_h]
|
|
543
624
|
|
|
544
625
|
cb_x = (px * cb_h) / max_h
|
|
545
626
|
cr_x = (px * cr_h) / max_h
|
|
@@ -4,6 +4,16 @@ module PureJPEG
|
|
|
4
4
|
module Huffman
|
|
5
5
|
class DecodeTable
|
|
6
6
|
def initialize(bits, values)
|
|
7
|
+
unless bits.length == 16 && bits.all? { |n| n.is_a?(Integer) && n.between?(0, 255) } &&
|
|
8
|
+
values.length.between?(1, 256) && bits.sum == values.length
|
|
9
|
+
raise DecodeError, "Invalid JPEG Huffman table lengths"
|
|
10
|
+
end
|
|
11
|
+
available = 1
|
|
12
|
+
bits.each do |count|
|
|
13
|
+
available = available * 2 - count
|
|
14
|
+
# JPEG reserves the all-ones code for entropy padding.
|
|
15
|
+
raise DecodeError, "Oversubscribed JPEG Huffman table" if available <= 0
|
|
16
|
+
end
|
|
7
17
|
@min_code = Array.new(17, 0)
|
|
8
18
|
@max_code = Array.new(17, -1)
|
|
9
19
|
@val_ptr = Array.new(17, 0)
|
|
@@ -3,19 +3,24 @@
|
|
|
3
3
|
module PureJPEG
|
|
4
4
|
class JFIFReader
|
|
5
5
|
attr_reader :width, :height, :components, :quant_tables, :huffman_tables,
|
|
6
|
-
:restart_interval, :progressive, :scans, :icc_profile
|
|
6
|
+
:restart_interval, :progressive, :scans, :icc_profile,
|
|
7
|
+
:precision, :adobe_transform, :component_quant_tables
|
|
7
8
|
|
|
8
9
|
Component = Struct.new(:id, :h_sampling, :v_sampling, :qt_id)
|
|
9
10
|
ScanComponent = Struct.new(:id, :dc_table_id, :ac_table_id)
|
|
10
|
-
Scan = Struct.new(:components, :spectral_start, :spectral_end, :successive_high, :successive_low, :data, :huffman_tables)
|
|
11
|
+
Scan = Struct.new(:components, :spectral_start, :spectral_end, :successive_high, :successive_low, :data, :huffman_tables) do
|
|
12
|
+
attr_accessor :restart_interval
|
|
13
|
+
end
|
|
11
14
|
|
|
12
|
-
def initialize(data, stop_after_frame: false)
|
|
15
|
+
def initialize(data, stop_after_frame: false, metadata_only: false)
|
|
13
16
|
@data = data.b
|
|
14
17
|
@stop_after_frame = stop_after_frame
|
|
18
|
+
@metadata_only = metadata_only
|
|
15
19
|
@pos = 0
|
|
16
20
|
@quant_tables = {}
|
|
17
21
|
@huffman_tables = {}
|
|
18
22
|
@components = []
|
|
23
|
+
@component_quant_tables = {}
|
|
19
24
|
@restart_interval = 0
|
|
20
25
|
@progressive = false
|
|
21
26
|
@scans = []
|
|
@@ -38,16 +43,20 @@ module PureJPEG
|
|
|
38
43
|
expect_marker(0xD8) # SOI
|
|
39
44
|
|
|
40
45
|
loop do
|
|
46
|
+
# Header-only input is useful for metadata queries, even without SOS.
|
|
47
|
+
return if @metadata_only && @width && @pos == @data.bytesize
|
|
41
48
|
marker = read_marker
|
|
42
49
|
case marker
|
|
43
50
|
when 0xE2 # APP2 (may contain ICC profile)
|
|
44
51
|
parse_app2
|
|
45
|
-
when
|
|
52
|
+
when 0xEE # Adobe APP14
|
|
53
|
+
parse_app14
|
|
54
|
+
when 0xE0, 0xE1, 0xE3..0xED, 0xEF # Other application markers
|
|
46
55
|
skip_segment
|
|
47
56
|
when 0xDB # DQT
|
|
48
|
-
parse_dqt
|
|
57
|
+
@metadata_only ? skip_segment : parse_dqt
|
|
49
58
|
when 0xC4 # DHT
|
|
50
|
-
parse_dht
|
|
59
|
+
@metadata_only ? skip_segment : parse_dht
|
|
51
60
|
when 0xC0 # SOF0 (baseline)
|
|
52
61
|
parse_sof0
|
|
53
62
|
return if @stop_after_frame
|
|
@@ -56,17 +65,30 @@ module PureJPEG
|
|
|
56
65
|
@progressive = true
|
|
57
66
|
return if @stop_after_frame
|
|
58
67
|
when 0xDA # SOS
|
|
68
|
+
return if @metadata_only
|
|
59
69
|
scan = parse_sos
|
|
60
70
|
scan.data = extract_scan_data
|
|
61
71
|
scan.huffman_tables = @huffman_tables.dup
|
|
72
|
+
scan.restart_interval = @restart_interval
|
|
73
|
+
# A component keeps the quantizer selected by its first scan, even
|
|
74
|
+
# if another scan later redefines that table ID for other components.
|
|
75
|
+
scan.components.each do |sc|
|
|
76
|
+
comp = @components.find { |c| c.id == sc.id }
|
|
77
|
+
if comp && !@component_quant_tables.key?(comp.id)
|
|
78
|
+
@component_quant_tables[comp.id] = @quant_tables[comp.qt_id]
|
|
79
|
+
end
|
|
80
|
+
end
|
|
62
81
|
@scans << scan
|
|
63
|
-
return unless @progressive
|
|
64
82
|
when 0xFE # COM (comment)
|
|
65
83
|
skip_segment
|
|
66
84
|
when 0xDD # DRI (restart interval)
|
|
67
85
|
parse_dri
|
|
68
86
|
when 0xD9 # EOI
|
|
69
87
|
return
|
|
88
|
+
when 0xC1, 0xC3, 0xC5..0xCB, 0xCD..0xCF
|
|
89
|
+
raise DecodeError, "Unsupported JPEG frame marker: 0x#{marker.to_s(16)}"
|
|
90
|
+
when 0xD0..0xD8, 0x00, 0x01
|
|
91
|
+
raise DecodeError, "Unexpected JPEG marker: 0x#{marker.to_s(16)}"
|
|
70
92
|
else
|
|
71
93
|
skip_segment
|
|
72
94
|
end
|
|
@@ -100,13 +122,24 @@ module PureJPEG
|
|
|
100
122
|
|
|
101
123
|
ICC_PROFILE_SIG = "ICC_PROFILE\0".b
|
|
102
124
|
|
|
125
|
+
def parse_app14
|
|
126
|
+
end_pos = read_segment_end
|
|
127
|
+
if end_pos - @pos >= 12 && @data[@pos, 5] == "Adobe"
|
|
128
|
+
@adobe_transform = @data.getbyte(@pos + 11)
|
|
129
|
+
end
|
|
130
|
+
@pos = end_pos
|
|
131
|
+
end
|
|
132
|
+
|
|
103
133
|
def parse_app2
|
|
104
134
|
end_pos = read_segment_end
|
|
105
135
|
|
|
106
136
|
if (end_pos - @pos) >= 14 && @data[@pos, 12] == ICC_PROFILE_SIG
|
|
107
137
|
@pos += 12
|
|
108
138
|
seq_no = read_byte
|
|
109
|
-
|
|
139
|
+
total = read_byte
|
|
140
|
+
@icc_invalid = true if total == 0 || seq_no == 0 || seq_no > total ||
|
|
141
|
+
(@icc_total && @icc_total != total) || @icc_chunks.key?(seq_no)
|
|
142
|
+
@icc_total = total
|
|
110
143
|
@icc_chunks[seq_no] = @data[@pos, end_pos - @pos]
|
|
111
144
|
end
|
|
112
145
|
|
|
@@ -115,6 +148,7 @@ module PureJPEG
|
|
|
115
148
|
|
|
116
149
|
def assemble_icc_profile
|
|
117
150
|
return if @icc_chunks.empty?
|
|
151
|
+
return if @icc_invalid || @icc_chunks.length != @icc_total
|
|
118
152
|
|
|
119
153
|
@icc_profile = @icc_chunks.sort_by(&:first).map(&:last).join.b
|
|
120
154
|
end
|
|
@@ -130,6 +164,9 @@ module PureJPEG
|
|
|
130
164
|
info = read_byte
|
|
131
165
|
precision = (info >> 4) & 0x0F # 0 = 8-bit, 1 = 16-bit
|
|
132
166
|
table_id = info & 0x0F
|
|
167
|
+
unless precision <= 1 && table_id <= 3
|
|
168
|
+
raise DecodeError, "Invalid JPEG quantization table definition"
|
|
169
|
+
end
|
|
133
170
|
bytes_per_value = precision == 0 ? 1 : 2
|
|
134
171
|
ensure_remaining_in_segment!(end_pos, 64 * bytes_per_value, "DQT")
|
|
135
172
|
|
|
@@ -137,6 +174,7 @@ module PureJPEG
|
|
|
137
174
|
64.times do |i|
|
|
138
175
|
zigzag_table[i] = precision == 0 ? read_byte : read_u16
|
|
139
176
|
end
|
|
177
|
+
raise DecodeError, "Zero JPEG quantization value" if zigzag_table.include?(0)
|
|
140
178
|
# DQT stores values in zigzag order; convert to raster order
|
|
141
179
|
table = Array.new(64)
|
|
142
180
|
64.times { |i| table[Zigzag::ORDER[i]] = zigzag_table[i] }
|
|
@@ -152,20 +190,27 @@ module PureJPEG
|
|
|
152
190
|
info = read_byte
|
|
153
191
|
table_class = (info >> 4) & 0x0F # 0 = DC, 1 = AC
|
|
154
192
|
table_id = info & 0x0F
|
|
193
|
+
unless table_class <= 1 && table_id <= 3
|
|
194
|
+
raise DecodeError, "Invalid JPEG Huffman table definition"
|
|
195
|
+
end
|
|
155
196
|
|
|
156
197
|
bits = Array.new(16) { read_byte }
|
|
157
198
|
total = bits.sum
|
|
158
199
|
ensure_remaining_in_segment!(end_pos, total, "DHT")
|
|
159
200
|
values = Array.new(total) { read_byte }
|
|
201
|
+
if values.any? { |value| table_class == 0 ? value > 11 : (value & 15) > 10 }
|
|
202
|
+
raise DecodeError, "Invalid JPEG Huffman symbol"
|
|
203
|
+
end
|
|
160
204
|
|
|
161
205
|
@huffman_tables[[table_class, table_id]] = { bits: bits, values: values }
|
|
162
206
|
end
|
|
163
207
|
end
|
|
164
208
|
|
|
165
209
|
def parse_sof0
|
|
210
|
+
raise DecodeError, "Multiple JPEG frame headers are unsupported" if @width
|
|
166
211
|
end_pos = read_segment_end
|
|
167
212
|
ensure_remaining_in_segment!(end_pos, 6, "SOF")
|
|
168
|
-
|
|
213
|
+
@precision = read_byte
|
|
169
214
|
@height = read_u16
|
|
170
215
|
@width = read_u16
|
|
171
216
|
num_components = read_byte
|
|
@@ -179,6 +224,13 @@ module PureJPEG
|
|
|
179
224
|
qt_id = read_byte
|
|
180
225
|
Component.new(id, h, v, qt_id)
|
|
181
226
|
end
|
|
227
|
+
raise DecodeError, "JPEG frame has no components" if @components.empty?
|
|
228
|
+
if @components.map(&:id).uniq.length != @components.length
|
|
229
|
+
raise DecodeError, "Duplicate JPEG component id"
|
|
230
|
+
end
|
|
231
|
+
unless @components.all? { |c| c.h_sampling.between?(1, 4) && c.v_sampling.between?(1, 4) }
|
|
232
|
+
raise DecodeError, "Invalid JPEG sampling factors"
|
|
233
|
+
end
|
|
182
234
|
@pos = end_pos
|
|
183
235
|
end
|
|
184
236
|
|
|
@@ -234,19 +286,23 @@ module PureJPEG
|
|
|
234
286
|
def extract_scan_data
|
|
235
287
|
start = @pos
|
|
236
288
|
len = @data.bytesize
|
|
237
|
-
#
|
|
238
|
-
|
|
289
|
+
# Preserve the first FF of a marker's fill run for read_marker. Stuffed
|
|
290
|
+
# bytes and restarts belong to the entropy stream, segment markers do not.
|
|
291
|
+
while @pos < len
|
|
239
292
|
found = @data.index("\xFF".b, @pos)
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
293
|
+
raise DecodeError, "Unexpected end of JPEG scan" unless found
|
|
294
|
+
marker_pos = found + 1
|
|
295
|
+
marker_pos += 1 while marker_pos < len && @data.getbyte(marker_pos) == 0xFF
|
|
296
|
+
raise DecodeError, "Unexpected end of JPEG scan" if marker_pos == len
|
|
297
|
+
code = @data.getbyte(marker_pos)
|
|
298
|
+
if code == 0x00 || code.between?(0xD0, 0xD7)
|
|
299
|
+
@pos = marker_pos + 1
|
|
300
|
+
else
|
|
301
|
+
@pos = found
|
|
302
|
+
return @data[start...found]
|
|
246
303
|
end
|
|
247
|
-
@pos += 2
|
|
248
304
|
end
|
|
249
|
-
|
|
305
|
+
raise DecodeError, "Unexpected end of JPEG scan"
|
|
250
306
|
end
|
|
251
307
|
end
|
|
252
308
|
end
|
data/lib/pure_jpeg/version.rb
CHANGED
data/lib/pure_jpeg.rb
CHANGED
|
@@ -21,10 +21,9 @@ require_relative "pure_jpeg/image"
|
|
|
21
21
|
require_relative "pure_jpeg/encoder"
|
|
22
22
|
require_relative "pure_jpeg/decoder"
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
# color (4:2:0 chroma subsampling), and standard Huffman tables (Annex K).
|
|
24
|
+
# Pure Ruby JPEG encoder and decoder with no native dependencies.
|
|
25
|
+
# Encodes baseline DCT (SOF0), and decodes baseline and progressive (SOF2)
|
|
26
|
+
# 8-bit grayscale and YCbCr JPEGs.
|
|
28
27
|
module PureJPEG
|
|
29
28
|
# Raised when decoding invalid or unsupported JPEG data.
|
|
30
29
|
class DecodeError < StandardError; end
|
|
@@ -70,15 +69,15 @@ module PureJPEG
|
|
|
70
69
|
# Read JPEG dimensions and basic frame metadata without decoding scan data.
|
|
71
70
|
#
|
|
72
71
|
# @param path_or_data [String] a file path or raw JPEG bytes
|
|
73
|
-
# @return [Info]
|
|
72
|
+
# @return [Info] frame metadata and complete ICC profile from headers before SOS
|
|
74
73
|
def self.info(path_or_data)
|
|
75
|
-
data = if path_or_data.is_a?(String) && !path_or_data.start_with?("\xFF\xD8".b) && File.exist?(path_or_data)
|
|
74
|
+
data = if path_or_data.is_a?(String) && !path_or_data.b.include?("\0") && !path_or_data.b.start_with?("\xFF\xD8".b) && File.exist?(path_or_data)
|
|
76
75
|
File.binread(path_or_data)
|
|
77
76
|
else
|
|
78
77
|
path_or_data.b
|
|
79
78
|
end
|
|
80
79
|
|
|
81
|
-
jfif = JFIFReader.new(data,
|
|
80
|
+
jfif = JFIFReader.new(data, metadata_only: true)
|
|
82
81
|
raise DecodeError, "JPEG frame header not found" unless jfif.width && jfif.height
|
|
83
82
|
|
|
84
83
|
Info.new(
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: pure_jpeg
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Peter Cooper
|
|
@@ -37,7 +37,8 @@ dependencies:
|
|
|
37
37
|
- - "~>"
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
39
|
version: '5.0'
|
|
40
|
-
description: A pure Ruby baseline JPEG encoder and
|
|
40
|
+
description: A pure Ruby baseline JPEG encoder and baseline/progressive JPEG decoder
|
|
41
|
+
with no native dependencies.
|
|
41
42
|
email:
|
|
42
43
|
- git@peterc.org
|
|
43
44
|
executables: []
|
|
@@ -45,7 +46,9 @@ extensions: []
|
|
|
45
46
|
extra_rdoc_files: []
|
|
46
47
|
files:
|
|
47
48
|
- CHANGELOG.md
|
|
49
|
+
- CREATIVE.md
|
|
48
50
|
- LICENSE
|
|
51
|
+
- LICENSE-IJG
|
|
49
52
|
- README.md
|
|
50
53
|
- lib/pure_jpeg.rb
|
|
51
54
|
- lib/pure_jpeg/bit_reader.rb
|
|
@@ -69,6 +72,7 @@ files:
|
|
|
69
72
|
homepage: https://github.com/peterc/pure_jpeg
|
|
70
73
|
licenses:
|
|
71
74
|
- MIT
|
|
75
|
+
- IJG
|
|
72
76
|
metadata:
|
|
73
77
|
source_code_uri: https://github.com/peterc/pure_jpeg
|
|
74
78
|
changelog_uri: https://github.com/peterc/pure_jpeg/blob/main/CHANGELOG.md
|