Navigation Menu

Skip to content

Commit

Permalink
Fix for #2843, doesn't work when QuantumDepth != 8
Browse files Browse the repository at this point in the history
  • Loading branch information
rmagick committed Nov 17, 2005
1 parent 9f83379 commit a935e60
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions examples/histogram.rb
Expand Up @@ -10,6 +10,7 @@ class Image
private
HISTOGRAM_COLS = 256
HISTOGRAM_ROWS = 200
MAX_QUANTUM = 255
AIR_FACTOR = 1.025

# The alpha frequencies are shown as white dots.
Expand Down Expand Up @@ -59,16 +60,16 @@ def channel_histograms(red, green, blue, int, scale, fg, bg)

yf = Float(y)
if yf >= HISTOGRAM_ROWS - (red[x] * scale)
red_column[y].red = MaxRGB
rgb_column[y].red = MaxRGB
red_column[y].red = MAX_QUANTUM
rgb_column[y].red = MAX_QUANTUM
end
if yf >= HISTOGRAM_ROWS - (green[x] * scale)
green_column[y].green = MaxRGB
rgb_column[y].green = MaxRGB
green_column[y].green = MAX_QUANTUM
rgb_column[y].green = MAX_QUANTUM
end
if yf >= HISTOGRAM_ROWS - (blue[x] * scale)
blue_column[y].blue = MaxRGB
rgb_column[y].blue = MaxRGB
blue_column[y].blue = MAX_QUANTUM
rgb_column[y].blue = MAX_QUANTUM
end
if yf >= HISTOGRAM_ROWS - (int[x] * scale)
int_column[y].opacity = TransparentOpacity
Expand Down Expand Up @@ -159,9 +160,9 @@ def intensity_hist(int_histogram)
return int_histogram
end

# Returns a value between 0 and 255. Same as the PixelIntensity macro.
# Returns a value between 0 and MAX_QUANTUM. Same as the PixelIntensity macro.
def pixel_intensity(pixel)
(306*pixel.red + 601*pixel.green + 117*pixel.blue)/1024
(306*(pixel.red & MAX_QUANTUM) + 601*(pixel.green & MAX_QUANTUM) + 117*(pixel.blue & MAX_QUANTUM))/1024
end

public
Expand All @@ -177,13 +178,13 @@ def histogram(fg='white', bg='black')
rows.times do |row|
pixels = get_pixels(0, row, columns, 1)
pixels.each do |pixel|
red[pixel.red] += 1
green[pixel.green] += 1
blue[pixel.blue] += 1
red[pixel.red & MAX_QUANTUM] += 1
green[pixel.green & MAX_QUANTUM] += 1
blue[pixel.blue & MAX_QUANTUM] += 1

# Only count opacity channel if some pixels are not opaque.
if !opaque?
alpha[pixel.opacity] += 1
alpha[pixel.opacity & MAX_QUANTUM] += 1
end
v = pixel_intensity(pixel)
int[v] += 1
Expand Down

0 comments on commit a935e60

Please sign in to comment.