On 24/10/05 5:44 pm, "Valentin Schmidt" <[EMAIL PROTECTED]> wrote:
> here a handler which produces a similar result, but without the
> pixel-wise GetPixel/SetPixel, so much faster:
>
> on colorize (tGrayscaleImage, tColor)
>   tImage = image(tGrayscaleImage.width, tGrayscaleImage.height, 24)
>   tImage.copypixels(tGrayscaleImage, tImage.rect, tImage.rect, \
> [#bgcolor: tColor, #maskImage:tGrayscaleImage.createMask() ])
>   return tImage
> end


Here's a handler which is half the speed of Valentin's, but which will cope
with any color (not just primary colors), and which will accept colored
images as input.  The result is somewhat darker than Valentin's.

It works in two passes: The first pass creates an image with colors from
white to the chosen color; the second pass desaturates colors progressively
as they get darker, so that the darkest color areas become black.

If you use black as the input color, you end up with an image in 128 shades
of gray, where any initial pixel darker than rgb(127, 127, 127) becomes
black.

James

----------------------------------------------------------------------------


on GetMonochromeImage(anImage, aColor) -------------------------------
  -- INPUT: <anImage> should be an 8-bit grayscale image, a member
  --         with an image property, or the name or number of such a
  --         member.
  -- OUTPUT: Returns a 32-bit image in shades of aColor, ranging from
  --         white to black, or an error symbol.
  --------------------------------------------------------------------
  
  -- Check and convert input parameters
  if ilk(aColor) <> #color then
    return #colorExpected
  end if
  
  case ilk(anImage) of
    #image: -- continue
      
    #member:
      case anImage.type of
        #bitmap,#Flash,#realMedia,#text,#shockwave3D,#vectorShape:
          anImage = anImage.image
          return GetMonochromeImage(anImage, aColor)
          
        otherwise:
          return #imageMemberExpected
      end case
      
    #integer, #string:
      anImage = member(anImage)
      return GetMonochromeImage(anImage, aColor)
      
    otherwise:
      return #imageExpected
  end case
  
  -- If we get here, anImage is a valid image object, and aColor is
  -- a valid color object.
  
  vRect   = anImage.rect
  vWidth  = vRect.width
  vHeight = vRect.height
  
  vOutput = image(vWidth, vHeight, 32)
  
  if anImage.depth > 8 or anImage.paletteRef <> #grayScale then
    -- Convert the input image to 8-bit grayscale
    vTemp   = anImage
    anImage = image(vWidth, vHeight, 8, #grayscale)
    anImage.copyPixels(vTemp, vRect, vRect)
  end if
  
  -- Create an inverted grayscale image, for desaturating the output
  vInverse = image(vWidth, vHeight, 8, #grayscale)
  vInverse.fill(vRect, rgb(0, 0, 0))
  vInverse.copyPixels(anImage, vRect, vRect, [#ink: #reverse])
  
  -- Copy a colorized version of the image, where the color range is
  -- from white to aColor
  vTemp    = image(vWidth, vHeight, 32)
  vTemp.fill(vRect, aColor)
  vOutput.copyPixels(vTemp, vRect, vRect, [#maskImage: anImage])
  
  -- Remove color from the dark areas.  This is done by subtracting
  -- the inverse grayscale image.  Areas which are aColor will have
  -- rgb(255, 255, 255) subtracted from them, and will therefore
  -- become black.
  vOutput.copyPixels(vInverse, vRect, vRect, [#ink: #subtractPin])
  
  return vOutput
end GetMonochromeImage

[To remove yourself from this list, or to change to digest mode, go to 
http://www.penworks.com/lingo-l.cgi  To post messages to the list, email 
[email protected]  (Problems, email [EMAIL PROTECTED]). Lingo-L is for 
learning and helping with programming Lingo.  Thanks!]

Reply via email to