Class: ZIMG::JPEG::Decoder
- Inherits:
-
Object
- Object
- ZIMG::JPEG::Decoder
- Defined in:
- lib/zimg/jpeg/decoder.rb
Instance Method Summary collapse
- #decode_baseline(component, dst) ⇒ Object
- #decode_block(component, decode_fn, mcu) ⇒ Object
- #decode_mcu(component, decode_fn, mcu, row, col) ⇒ Object
- #decode_mcu_AC_first(component, dst) ⇒ Object
- #decode_mcu_AC_refine(component, dst) ⇒ Object
- #decode_mcu_DC_first(component, dst) ⇒ Object
- #decode_mcu_DC_refine(_component, dst) ⇒ Object
-
#decode_scan ⇒ Object
rubocop:enable Metrics/ParameterLists.
-
#initialize(data, frame, components, reset_interval, spectral_start, spectral_end, ah, al) ⇒ Decoder
constructor
rubocop:disable Metrics/ParameterLists.
Constructor Details
#initialize(data, frame, components, reset_interval, spectral_start, spectral_end, ah, al) ⇒ Decoder
rubocop:disable Metrics/ParameterLists
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/zimg/jpeg/decoder.rb', line 229 def initialize(data, frame, components, reset_interval, spectral_start, spectral_end, ah, al) @components = components @reset_interval = reset_interval @spectral_end = spectral_end @spectral_start = spectral_start @al = al @ah = ah @mcus_per_line = frame.mcus_per_line @mcus_per_column = frame.mcus_per_column @progressive = frame.progressive @offset = 0 @bit_io = BitEnumerator.new(data) end |
Instance Method Details
#decode_baseline(component, dst) ⇒ Object
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/zimg/jpeg/decoder.rb', line 357 def decode_baseline(component, dst) t = component.huffman_table_dc.decode(@bit_io) diff = t == 0 ? 0 : @bit_io.receive_extend(t) dst[0] = (component.pred += diff) k = 1 while k < 64 rs = component.huffman_table_ac.decode(@bit_io) s = rs & 15 r = rs >> 4 if s == 0 break if r < 15 k += 16 next end k += r z = DCT_ZIGZAG[k] dst[z] = @bit_io.receive_extend(s) k += 1 end end |
#decode_block(component, decode_fn, mcu) ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/zimg/jpeg/decoder.rb', line 342 def decode_block(component, decode_fn, mcu) block_row = mcu / component.blocks_per_line block_col = mcu % component.blocks_per_line if @bit_io.eof? # If we've run out of data, just leave the MCU set to zeroes. # This way, we return uniform gray for the remainder of the segment. else decode_fn.call(component, component.blocks[block_row][block_col]) end rescue StopIteration # catch unexpected end of data (partial_progressive.jpg, non-interleaved_progressive-*.jpg) warn "[?] unexpected EOF" end |
#decode_mcu(component, decode_fn, mcu, row, col) ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/zimg/jpeg/decoder.rb', line 326 def decode_mcu(component, decode_fn, mcu, row, col) # If we've run out of data, just leave the MCU set to zeroes. # This way, we return uniform gray for the remainder of the segment. return if @bit_io.eof? mcu_row = mcu / @mcus_per_line mcu_col = mcu % @mcus_per_line block_row = mcu_row * component.v + row block_col = mcu_col * component.h + col decode_fn.call(component, component.blocks[block_row][block_col]) rescue StopIteration # catch unexpected end of data (partial_progressive.jpg, non-interleaved_progressive-*.jpg) warn "[?] unexpected EOF" end |
#decode_mcu_AC_first(component, dst) ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/zimg/jpeg/decoder.rb', line 379 def decode_mcu_AC_first(component, dst) if @eobrun > 0 @eobrun -= 1 return end k = @spectral_start e = @spectral_end while k <= e break unless (rs = component.huffman_table_ac.decode(@bit_io)) s = rs & 15 r = rs >> 4 if s == 0 if r < 15 @eobrun = @bit_io.receive(r) + (1 << r) - 1 break end # r == 15 k += 16 next end # s > 0 k += r z = DCT_ZIGZAG[k] dst[z] = @bit_io.receive_extend(s) * (1 << @al) k += 1 end end |
#decode_mcu_AC_refine(component, dst) ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/zimg/jpeg/decoder.rb', line 408 def decode_mcu_AC_refine(component, dst) next_value = nil k = @spectral_start e = @spectral_end r = 0 z = 0 while k <= e z = DCT_ZIGZAG[k] direction = dst[z] < 0 ? -1 : 1 case @successive_ac_state when 0 # initial state rs = component.huffman_table_ac.decode(@bit_io) s = rs & 15 r = rs >> 4 case s when 0 if r < 15 @eobrun = @bit_io.receive(r) + (1 << r) @successive_ac_state = 4 else r = 16 @successive_ac_state = 1 end when 1 next_value = @bit_io.receive_extend(1) @successive_ac_state = r == 0 ? 3 : 2 else raise "invalid ACn encoding" # libjpeg-turbo: "Corrupt JPEG data: bad Huffman code" end next when 1, 2 # skipping r zero items if dst[z] != 0 dst[z] += (@bit_io.next << @al) * direction else r -= 1 if r == 0 @successive_ac_state = @successive_ac_state == 2 ? 3 : 0 end end when 3 # set value for a zero item if dst[z] != 0 dst[z] += (@bit_io.next << @al) * direction else dst[z] = next_value << @al @successive_ac_state = 0 end when 4 # eob dst[z] += (@bit_io.next << @al) * direction if dst[z] != 0 else raise "invalid AC state #{@successive_ac_state}" end # case k += 1 end # while # corresponds to "Output newly nonzero coefficient" line of jdphuff.c # (samples/jpeg/jpeg-decoder/tests/reftest/images/partial_progressive.jpg) if @successive_ac_state == 3 && next_value && z < 64 dst[z] = next_value << @al next_value = nil end return unless @successive_ac_state == 4 @eobrun -= 1 @successive_ac_state = 0 if @eobrun == 0 end |
#decode_mcu_DC_first(component, dst) ⇒ Object
475 476 477 478 479 |
# File 'lib/zimg/jpeg/decoder.rb', line 475 def decode_mcu_DC_first(component, dst) t = component.huffman_table_dc.decode(@bit_io) diff = t == 0 ? 0 : (@bit_io.receive_extend(t) << @al) dst[0] = (component.pred += diff) end |
#decode_mcu_DC_refine(_component, dst) ⇒ Object
481 482 483 |
# File 'lib/zimg/jpeg/decoder.rb', line 481 def decode_mcu_DC_refine(_component, dst) dst[0] |= (@bit_io.next << @al) end |
#decode_scan ⇒ Object
rubocop:enable Metrics/ParameterLists
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/zimg/jpeg/decoder.rb', line 246 def decode_scan @successive_ac_state = 0 decode_fn = if @progressive if @spectral_start == 0 @ah == 0 ? :decode_mcu_DC_first : :decode_mcu_DC_refine else @ah == 0 ? :decode_mcu_AC_first : :decode_mcu_AC_refine end else :decode_baseline end decode_fn = method(decode_fn) mcu = 0 mcu_expected = if @components.size == 1 @components[0].blocks_per_line * @components[0].blocks_per_column else @mcus_per_line * @mcus_per_column end @reset_interval ||= mcu_expected while mcu < mcu_expected @bit_io.reset! # reset interval stuff @components.each { |c| c.pred = 0 } @eobrun = 0 if @components.size == 1 component = @components[0] @reset_interval.times do decode_block(component, decode_fn, mcu) mcu += 1 end else @reset_interval.times do @components.each do |c| c.v.times do |j| c.h.times do |k| decode_mcu(c, decode_fn, mcu, j, k) end end end mcu += 1 # If we've reached our expected MCU's, stop decoding break if mcu == mcu_expected end end loop do marker = @bit_io.peek_bytes(2) case marker when "\xFF\xD0".."\xFF\xD7" # RSTx @bit_io.skip_bytes(2) break when "\xFF\x00" # stuffed 0 @bit_io.skip_bytes(2) when "\xFF\xFF" # maybe a series of FF's followed by 0 @bit_io.skip_bytes(1) when nil, "" # EOF? # return @offset break else warn "[?] expected RSTx, but got #{marker.inspect}" return @offset end end end # while @offset end |