pure_jpeg 0.4.0 → 0.5.0

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.
@@ -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,30 @@
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)
13
- @data = data.b
15
+ def initialize(data, stop_after_frame: false, metadata_only: false, max_pixels: nil)
16
+ if metadata_only && !data.is_a?(String)
17
+ @header_io = data
18
+ @data = "".b
19
+ else
20
+ @data = data.b
21
+ end
14
22
  @stop_after_frame = stop_after_frame
23
+ @metadata_only = metadata_only
24
+ @max_pixels = max_pixels
15
25
  @pos = 0
16
26
  @quant_tables = {}
17
27
  @huffman_tables = {}
18
28
  @components = []
29
+ @component_quant_tables = {}
19
30
  @restart_interval = 0
20
31
  @progressive = false
21
32
  @scans = []
@@ -38,16 +49,20 @@ module PureJPEG
38
49
  expect_marker(0xD8) # SOI
39
50
 
40
51
  loop do
41
- marker = read_marker
52
+ # Header-only input is useful for metadata queries, even without SOS.
53
+ marker = read_marker(@metadata_only && @width)
54
+ return if marker.nil?
42
55
  case marker
43
56
  when 0xE2 # APP2 (may contain ICC profile)
44
57
  parse_app2
45
- when 0xE0, 0xE1, 0xE3..0xEF # APP0, APP1, APP3-APP15
58
+ when 0xEE # Adobe APP14
59
+ parse_app14
60
+ when 0xE0, 0xE1, 0xE3..0xED, 0xEF # Other application markers
46
61
  skip_segment
47
62
  when 0xDB # DQT
48
- parse_dqt
63
+ @metadata_only ? skip_segment : parse_dqt
49
64
  when 0xC4 # DHT
50
- parse_dht
65
+ @metadata_only ? skip_segment : parse_dht
51
66
  when 0xC0 # SOF0 (baseline)
52
67
  parse_sof0
53
68
  return if @stop_after_frame
@@ -56,36 +71,56 @@ module PureJPEG
56
71
  @progressive = true
57
72
  return if @stop_after_frame
58
73
  when 0xDA # SOS
74
+ return if @metadata_only
59
75
  scan = parse_sos
60
76
  scan.data = extract_scan_data
61
77
  scan.huffman_tables = @huffman_tables.dup
78
+ scan.restart_interval = @restart_interval
79
+ # A component keeps the quantizer selected by its first scan, even
80
+ # if another scan later redefines that table ID for other components.
81
+ scan.components.each do |sc|
82
+ comp = @components.find { |c| c.id == sc.id }
83
+ if comp && !@component_quant_tables.key?(comp.id)
84
+ @component_quant_tables[comp.id] = @quant_tables[comp.qt_id]
85
+ end
86
+ end
62
87
  @scans << scan
63
- return unless @progressive
64
88
  when 0xFE # COM (comment)
65
89
  skip_segment
66
90
  when 0xDD # DRI (restart interval)
67
91
  parse_dri
68
92
  when 0xD9 # EOI
69
93
  return
94
+ when 0xC1, 0xC3, 0xC5..0xCB, 0xCD..0xCF
95
+ raise DecodeError, "Unsupported JPEG frame marker: 0x#{marker.to_s(16)}"
96
+ when 0xD0..0xD8, 0x00, 0x01
97
+ raise DecodeError, "Unexpected JPEG marker: 0x#{marker.to_s(16)}"
70
98
  else
71
99
  skip_segment
72
100
  end
73
101
  end
74
102
  end
75
103
 
76
- def read_byte
77
- raise PureJPEG::DecodeError, "Unexpected end of JPEG data" if @pos >= @data.bytesize
78
- byte = @data.getbyte(@pos)
79
- @pos += 1
80
- byte
104
+ def read_byte(allow_eof = false)
105
+ if @pos < @data.bytesize
106
+ byte = @data.getbyte(@pos)
107
+ @pos += 1
108
+ return byte
109
+ end
110
+ if @header_io
111
+ byte = @header_io.read(1)&.getbyte(0)
112
+ return byte if byte
113
+ end
114
+ raise PureJPEG::DecodeError, "Unexpected end of JPEG data" unless allow_eof
81
115
  end
82
116
 
83
117
  def read_u16
84
118
  (read_byte << 8) | read_byte
85
119
  end
86
120
 
87
- def read_marker
88
- byte = read_byte
121
+ def read_marker(allow_eof = false)
122
+ byte = read_byte(allow_eof)
123
+ return if byte.nil?
89
124
  raise PureJPEG::DecodeError, "Expected 0xFF, got 0x#{byte.to_s(16)}" unless byte == 0xFF
90
125
  # Skip padding 0xFF bytes
91
126
  code = read_byte
@@ -100,13 +135,24 @@ module PureJPEG
100
135
 
101
136
  ICC_PROFILE_SIG = "ICC_PROFILE\0".b
102
137
 
138
+ def parse_app14
139
+ end_pos = read_segment_end
140
+ if end_pos - @pos >= 12 && @data[@pos, 5] == "Adobe"
141
+ @adobe_transform = @data.getbyte(@pos + 11)
142
+ end
143
+ @pos = end_pos
144
+ end
145
+
103
146
  def parse_app2
104
147
  end_pos = read_segment_end
105
148
 
106
149
  if (end_pos - @pos) >= 14 && @data[@pos, 12] == ICC_PROFILE_SIG
107
150
  @pos += 12
108
151
  seq_no = read_byte
109
- _total = read_byte
152
+ total = read_byte
153
+ @icc_invalid = true if total == 0 || seq_no == 0 || seq_no > total ||
154
+ (@icc_total && @icc_total != total) || @icc_chunks.key?(seq_no)
155
+ @icc_total = total
110
156
  @icc_chunks[seq_no] = @data[@pos, end_pos - @pos]
111
157
  end
112
158
 
@@ -115,6 +161,7 @@ module PureJPEG
115
161
 
116
162
  def assemble_icc_profile
117
163
  return if @icc_chunks.empty?
164
+ return if @icc_invalid || @icc_chunks.length != @icc_total
118
165
 
119
166
  @icc_profile = @icc_chunks.sort_by(&:first).map(&:last).join.b
120
167
  end
@@ -130,6 +177,9 @@ module PureJPEG
130
177
  info = read_byte
131
178
  precision = (info >> 4) & 0x0F # 0 = 8-bit, 1 = 16-bit
132
179
  table_id = info & 0x0F
180
+ unless precision <= 1 && table_id <= 3
181
+ raise DecodeError, "Invalid JPEG quantization table definition"
182
+ end
133
183
  bytes_per_value = precision == 0 ? 1 : 2
134
184
  ensure_remaining_in_segment!(end_pos, 64 * bytes_per_value, "DQT")
135
185
 
@@ -137,6 +187,7 @@ module PureJPEG
137
187
  64.times do |i|
138
188
  zigzag_table[i] = precision == 0 ? read_byte : read_u16
139
189
  end
190
+ raise DecodeError, "Zero JPEG quantization value" if zigzag_table.include?(0)
140
191
  # DQT stores values in zigzag order; convert to raster order
141
192
  table = Array.new(64)
142
193
  64.times { |i| table[Zigzag::ORDER[i]] = zigzag_table[i] }
@@ -152,22 +203,33 @@ module PureJPEG
152
203
  info = read_byte
153
204
  table_class = (info >> 4) & 0x0F # 0 = DC, 1 = AC
154
205
  table_id = info & 0x0F
206
+ unless table_class <= 1 && table_id <= 3
207
+ raise DecodeError, "Invalid JPEG Huffman table definition"
208
+ end
155
209
 
156
210
  bits = Array.new(16) { read_byte }
157
211
  total = bits.sum
158
212
  ensure_remaining_in_segment!(end_pos, total, "DHT")
159
213
  values = Array.new(total) { read_byte }
214
+ if values.any? { |value| table_class == 0 ? value > 11 : (value & 15) > 10 }
215
+ raise DecodeError, "Invalid JPEG Huffman symbol"
216
+ end
160
217
 
161
218
  @huffman_tables[[table_class, table_id]] = { bits: bits, values: values }
162
219
  end
163
220
  end
164
221
 
165
222
  def parse_sof0
223
+ raise DecodeError, "Multiple JPEG frame headers are unsupported" if @width
166
224
  end_pos = read_segment_end
167
225
  ensure_remaining_in_segment!(end_pos, 6, "SOF")
168
- read_byte # precision (always 8 for baseline)
226
+ @precision = read_byte
169
227
  @height = read_u16
170
228
  @width = read_u16
229
+ # Reject oversized decodes before collecting scans or allocating image buffers.
230
+ if @max_pixels && @width * @height > @max_pixels
231
+ raise DecodeError, "Image too large: #{@width}x#{@height} (max #{@max_pixels} pixels)"
232
+ end
171
233
  num_components = read_byte
172
234
  ensure_remaining_in_segment!(end_pos, num_components * 3, "SOF")
173
235
 
@@ -179,6 +241,13 @@ module PureJPEG
179
241
  qt_id = read_byte
180
242
  Component.new(id, h, v, qt_id)
181
243
  end
244
+ raise DecodeError, "JPEG frame has no components" if @components.empty?
245
+ if @components.map(&:id).uniq.length != @components.length
246
+ raise DecodeError, "Duplicate JPEG component id"
247
+ end
248
+ unless @components.all? { |c| c.h_sampling.between?(1, 4) && c.v_sampling.between?(1, 4) }
249
+ raise DecodeError, "Invalid JPEG sampling factors"
250
+ end
182
251
  @pos = end_pos
183
252
  end
184
253
 
@@ -216,6 +285,12 @@ module PureJPEG
216
285
  length = read_u16
217
286
  raise PureJPEG::DecodeError, "Invalid JPEG segment length: #{length}" if length < 2
218
287
 
288
+ # File metadata queries buffer one segment at a time. Its parser uses
289
+ # the same offsets and truncation checks as it does for in-memory data.
290
+ if @header_io
291
+ @data = @header_io.read(length - 2) || "".b
292
+ @pos = 0
293
+ end
219
294
  end_pos = @pos + length - 2
220
295
  if end_pos > @data.bytesize
221
296
  raise PureJPEG::DecodeError, "JPEG segment length exceeds available data"
@@ -234,19 +309,23 @@ module PureJPEG
234
309
  def extract_scan_data
235
310
  start = @pos
236
311
  len = @data.bytesize
237
- # Scan forward looking for a marker that isn't a stuffing byte or restart
238
- while @pos < len - 1
312
+ # Preserve the first FF of a marker's fill run for read_marker. Stuffed
313
+ # bytes and restarts belong to the entropy stream, segment markers do not.
314
+ while @pos < len
239
315
  found = @data.index("\xFF".b, @pos)
240
- break unless found && found < len - 1
241
- @pos = found
242
- next_byte = @data.getbyte(@pos + 1)
243
- # 0x00 is byte stuffing, 0xD0-0xD7 are restart markers, 0xFF is padding — all part of scan data
244
- if next_byte != 0x00 && !(next_byte >= 0xD0 && next_byte <= 0xD7) && next_byte != 0xFF
245
- break
316
+ raise DecodeError, "Unexpected end of JPEG scan" unless found
317
+ marker_pos = found + 1
318
+ marker_pos += 1 while marker_pos < len && @data.getbyte(marker_pos) == 0xFF
319
+ raise DecodeError, "Unexpected end of JPEG scan" if marker_pos == len
320
+ code = @data.getbyte(marker_pos)
321
+ if code == 0x00 || code.between?(0xD0, 0xD7)
322
+ @pos = marker_pos + 1
323
+ else
324
+ @pos = found
325
+ return @data[start...found]
246
326
  end
247
- @pos += 2
248
327
  end
249
- @data[start...@pos]
328
+ raise DecodeError, "Unexpected end of JPEG scan"
250
329
  end
251
330
  end
252
331
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PureJPEG
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
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
- # Pure Ruby JPEG encoder and decoder with no native dependencies.
25
- #
26
- # Supports baseline DCT (SOF0) with 8-bit precision, grayscale and YCbCr
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
@@ -32,6 +31,9 @@ module PureJPEG
32
31
  # Maximum image dimension (width or height) allowed for encoding and decoding.
33
32
  MAX_DIMENSION = 8192
34
33
 
34
+ # Default maximum decoded image area. Override per read with +max_pixels:+.
35
+ MAX_PIXELS = 8_000_000
36
+
35
37
  # Encode a pixel source as a JPEG.
36
38
  #
37
39
  # @param source [#width, #height, #[]] any object responding to +width+,
@@ -62,23 +64,23 @@ module PureJPEG
62
64
  # Decode a JPEG from a file path or binary string.
63
65
  #
64
66
  # @param path_or_data [String] a file path or raw JPEG bytes
67
+ # @param max_pixels [Integer] positive maximum image area (default: 8,000,000)
65
68
  # @return [Image] decoded image with pixel access
66
- def self.read(path_or_data)
67
- Decoder.decode(path_or_data)
69
+ def self.read(path_or_data, max_pixels: MAX_PIXELS)
70
+ Decoder.decode(path_or_data, max_pixels: max_pixels)
68
71
  end
69
72
 
70
73
  # Read JPEG dimensions and basic frame metadata without decoding scan data.
71
74
  #
72
75
  # @param path_or_data [String] a file path or raw JPEG bytes
73
- # @return [Info] image metadata parsed from the frame header
76
+ # @return [Info] frame metadata and complete ICC profile from headers before SOS
74
77
  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)
76
- File.binread(path_or_data)
78
+ jfif = if path_or_data.is_a?(String) && !path_or_data.b.start_with?("\xFF\xD8".b) && !path_or_data.b.include?("\0") && File.exist?(path_or_data)
79
+ File.open(path_or_data, "rb") { |file| JFIFReader.new(file, metadata_only: true) }
77
80
  else
78
- path_or_data.b
81
+ JFIFReader.new(path_or_data.b, metadata_only: true)
79
82
  end
80
83
 
81
- jfif = JFIFReader.new(data, stop_after_frame: true)
82
84
  raise DecodeError, "JPEG frame header not found" unless jfif.width && jfif.height
83
85
 
84
86
  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.0
4
+ version: 0.5.0
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 decoder with no native dependencies.
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