Module | ChunkyPNG::Canvas::Adam7Interlacing |
In: |
lib/chunky_png/canvas/adam7_interlacing.rb
|
Methods for decoding and encoding Adam7 interlacing.
Adam7 interlacing extracts 7 pass images out of a single image, that can be encoded to a stream separately so the image can be built up progressively. The module is included into ChunkyPNG canvas and is used to extract the pass images from the original image, or to reconstruct an original image from separate pass images.
Extracts a pass from a complete image @param [Integer] pass The pass number, should be in 0..6. @param [ChunkyPNG::Canvas] canvas The image that is being deconstructed. @return [ChunkyPNG::Canvas] The extracted pass image.
# File lib/chunky_png/canvas/adam7_interlacing.rb, line 57 57: def adam7_extract_pass(pass, canvas) 58: x_shift, x_offset, y_shift, y_offset = adam7_multiplier_offset(pass) 59: sm_pixels = [] 60: 61: y_offset.step(canvas.height - 1, 1 << y_shift) do |y| 62: x_offset.step(canvas.width - 1, 1 << x_shift) do |x| 63: sm_pixels << canvas[x, y] 64: end 65: end 66: 67: new_canvas_args = adam7_pass_size(pass, canvas.width, canvas.height) + [sm_pixels] 68: ChunkyPNG::Canvas.new(*new_canvas_args) 69: end
Merges a pass image into a total image that is being constructed. @param [Integer] pass The pass number, should be in 0..6. @param [ChunkyPNG::Canvas] canvas The image that is being constructed. @param [ChunkyPNG::Canvas] subcanvas The pass image that should be merged
# File lib/chunky_png/canvas/adam7_interlacing.rb, line 42 42: def adam7_merge_pass(pass, canvas, subcanvas) 43: x_shift, x_offset, y_shift, y_offset = adam7_multiplier_offset(pass) 44: for y in 0...subcanvas.height do 45: for x in 0...subcanvas.width do 46: new_x = (x << x_shift) | x_offset 47: new_y = (y << y_shift) | y_offset 48: canvas[new_x, new_y] = subcanvas[x, y] 49: end 50: end 51: end
Returns an array with the x-shift, x-offset, y-shift and y-offset for the requested pass. @param [Integer] pass The pass number, should be in 0..6.
# File lib/chunky_png/canvas/adam7_interlacing.rb, line 14 14: def adam7_multiplier_offset(pass) 15: [3 - (pass >> 1), (pass & 1 == 0) ? 0 : 8 >> ((pass + 1) >> 1), 16: pass == 0 ? 3 : 3 - ((pass - 1) >> 1), (pass == 0 || pass & 1 == 1) ? 0 : 8 >> (pass >> 1)] 17: end
Returns the pixel dimensions of the requested pass. @param [Integer] pass The pass number, should be in 0..6. @param [Integer] original_width The width of the original image. @param [Integer] original_height The height of the original image.
# File lib/chunky_png/canvas/adam7_interlacing.rb, line 23 23: def adam7_pass_size(pass, original_width, original_height) 24: x_shift, x_offset, y_shift, y_offset = adam7_multiplier_offset(pass) 25: [ (original_width - x_offset + (1 << x_shift) - 1) >> x_shift, 26: (original_height - y_offset + (1 << y_shift) - 1) >> y_shift] 27: end
Returns an array of the dimension of all the pass images. @param [Integer] original_width The width of the original image. @param [Integer] original_height The height of the original image. @return [Array<Array<Integer>>] Returns an array with 7 pairs of dimensions. @see adam7_pass_size
# File lib/chunky_png/canvas/adam7_interlacing.rb, line 34 34: def adam7_pass_sizes(original_width, original_height) 35: (0...7).map { |pass| adam7_pass_size(pass, original_width, original_height) } 36: end