Class: ZSteg::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/zsteg/analyzer.rb

Instance Method Summary collapse

Constructor Details

#initialize(image, params = {}) ⇒ Analyzer

Returns a new instance of Analyzer.



5
6
7
8
# File 'lib/zsteg/analyzer.rb', line 5

def initialize image, params = {}
  @params = params
  @image = image.is_a?(ZPNG::Image) ? image : ZPNG::Image.load(image)
end

Instance Method Details

#analyze!Object



10
11
12
13
14
# File 'lib/zsteg/analyzer.rb', line 10

def analyze!
  if bs = detect_block_size
    puts "[!] possible image block size is #{bs.join('x')}, downscaling may be necessary".yellow
  end
end

#check_block_size(dx, dy, x0, y0) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/zsteg/analyzer.rb', line 16

def check_block_size dx, dy, x0, y0
  c0 = @image[x0,y0]
  y0.upto(y0+dy-1) do |y|
    x0.upto(x0+dx-1) do |x|
      return if @image[x,y] != c0
    end
  end
  true
end

#detect_block_sizeObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/zsteg/analyzer.rb', line 26

def detect_block_size
  x=y=0
  c0 = @image[x,y]
  dx = dy = 1

  while (x+dx) < @image.width && @image[x+dx,y] == c0
    dx+=1
  end
  while (y+dy) < @image.height && @image[x,y+dy] == c0
    dy+=1
  end

  return if dx<2 && dy<2
  return if [1, @image.width].include?(dx) && [1, @image.height].include?(dy)

  # check 3x3 block
  0.step([dy*3, @image.height-1].min, dy) do |y|
    0.step([dx*3, @image.width-1].min, dx) do |x|
      return unless check_block_size dx, dy, x, y
    end
  end

  [dx,dy]
end