Create Histograms from Images

From Director Online Wiki
Jump to: navigation, search
-- by Josh Chunick
on createLuminanceHistogram img, maxHeight, lumValsList
  -- luminance --> RGB values from here: http://www.marginalsoftware.com/HowtoScan/color_channels.htm
  -- lumValsList = [0.30, 0.59, 0.11]
  lumRed = 0.2126
  lumGreen = 0.7152
  lumBlue = 0.0722
  if the paramCount = 3 then
    if listP(lumValsList) then
      lumRed = lumValsList[1]
      lumGreen = lumValsList[2]
      lumBlue = lumValsList[3]
    end if
  end if
 
  histImg = image(256, maxHeight, 8)
  lumList = []
  lumList[256] = 0
  w = img.width
  h = img.height
  w = w - 1
  h = h - 1
  repeat with x = 0 to w 
    repeat with y = 0 to h
      colour = img.getPixel(x,y)
      lum = (lumRed * colour.red) + (lumGreen * colour.green) + (lumBlue * colour.blue) 
      lumList[lum + 1] = lumList[lum + 1] + 1
    end repeat
  end repeat
  tmpList = lumList.duplicate()
  tmpList.sort()
  maxLum = float(tmpList[tmpList.count])
  repeat with i = 1 to 256
    y2 = maxHeight - integer(lumList[i]/maxLum * maxHeight)
    histImg.draw(point(i - 1, maxHeight), point(i - 1, y2), [#lineSize: 1, #color: color(0,0,0)])
  end repeat
 
  return histImg
 
end
 
on createRGBHistogram img, maxHeight
  theStart = the milliseconds
  histImg = image(256, maxHeight, 32)
  histImg.floodFill(0,0, rgb(0, 255, 0))
  binList = []
  binList[256] = 0
  w = img.width
  h = img.height
  --  numPixels = w * h
  w = w - 1
  h = h - 1
  percent = 0
  repeat with x = 0 to w 
    repeat with y = 0 to h
      --      colour = (img.getPixel(x,y, #integer)) + 16777216
      colour = img.getPixel(x,y)
      intColour = colour.red * 65536 + colour.green * 256 + colour.blue
      binVal = intColour/65536
      --      binVal = colour/65536
      binList[binVal + 1] = binList[binVal + 1] + 1
    end repeat
  end repeat
  tmpList = binList.duplicate()
  tmpList.sort()
  maxBin = float(tmpList[tmpList.count])
  repeat with i = 1 to 256
    y2 = maxHeight - integer(binList[i]/maxBin * maxHeight)
    histImg.draw(point(i - 1, maxHeight), point(i - 1, y2), [#lineSize: 1, #color: color(i,i,i)])
  end repeat
  put the milliseconds - theStart
  return histImg
 
end
 
on createRedHistogram img, maxHeight
 
  histImg = image(256, maxHeight, 8)
  binList = []
  binList[256] = 0
  w = img.width
  h = img.height
  w = w - 1
  h = h - 1
  repeat with x = 0 to w 
    repeat with y = 0 to h
      binVal = (img.getPixel(x,y)).red
      -- if binVal < 1 or binVal > 256 then put binVal
      binList[binVal + 1] = binList[binVal + 1] + 1
    end repeat
  end repeat
  tmpList = binList.duplicate()
  tmpList.sort()
  maxBin = float(tmpList[tmpList.count])
  repeat with i = 1 to 256
    y2 = maxHeight - integer(binList[i]/maxBin * maxHeight)
    histImg.draw(point(i - 1, maxHeight), point(i - 1, y2), [#lineSize: 1, #color: color(0,0,0)])
  end repeat
 
  return histImg
 
end
 
on createGreenHistogram img, maxHeight
 
  histImg = image(256, maxHeight, 8)
  binList = []
  binList[256] = 0
  w = img.width
  h = img.height
  w = w - 1
  h = h - 1
  repeat with x = 0 to w 
    repeat with y = 0 to h
      binVal = (img.getPixel(x,y)).green
      -- if binVal < 1 or binVal > 256 then put binVal
      binList[binVal + 1] = binList[binVal + 1] + 1
    end repeat
  end repeat
  tmpList = binList.duplicate()
  tmpList.sort()
  maxBin = float(tmpList[tmpList.count])
  repeat with i = 1 to 256
    y2 = maxHeight - integer(binList[i]/maxBin * maxHeight)
    histImg.draw(point(i - 1, maxHeight), point(i - 1, y2), [#lineSize: 1, #color: color(0,0,0)])
  end repeat
 
  return histImg
 
end
 
on createBlueHistogram img, maxHeight
 
  histImg = image(256, maxHeight, 8)
  binList = []
  binList[256] = 0
  w = img.width
  h = img.height
  w = w - 1
  h = h - 1
  repeat with x = 0 to w 
    repeat with y = 0 to h
      binVal = (img.getPixel(x,y))
      -- if binVal < 1 or binVal > 256 then put binVal
      binList[binVal + 1] = binList[binVal + 1] + 1
    end repeat
  end repeat
  tmpList = binList.duplicate()
  tmpList.sort()
  maxBin = float(tmpList[tmpList.count])
  repeat with i = 1 to 256
    y2 = maxHeight - integer(binList[i]/maxBin * maxHeight)
    histImg.draw(point(i - 1, maxHeight), point(i - 1, y2), [#lineSize: 1, #color: color(0,0,0)])
  end repeat
 
  return histImg
 
end
 
-- have to work on this some more
-- don't know how best to approach it.
on createAlphaHistogram img, maxHeight
  alphaImg = img.extractAlpha()
  histImg = image(256, maxHeight, 8)
  binList = []
  binList[256] = 0
  w = alphaImg.width
  h = alphaImg.height
  w = w - 1
  h = h - 1
  repeat with x = 0 to w 
    repeat with y = 0 to h
      binVal = (alphaImg.getPixel(x,y, #integer))
      --      put binVal.red & ", " & binVal.green & ", " & binVal.blue
      binList[binVal + 1] = binList[binVal + 1] + 1
    end repeat
  end repeat
  tmpList = binList.duplicate()
  tmpList.sort()
  maxBin = float(tmpList[tmpList.count])
  repeat with i = 1 to 256
    y2 = maxHeight - integer(binList[i]/maxBin * maxHeight)
    histImg.draw(point(i - 1, maxHeight), point(i - 1, y2), [#lineSize: 1, #color: color(0,0,0)])
  end repeat
 
  return histImg
 
end