From: shelarcy <she...@ca...> - 2004-08-02 14:46:05
|
On Fri, 30 Jul 2004 13:40:05 +0200, Daan Leijen <da...@cs...> wrote: > To make this all clearer, here is a small sample that you can modify > to implement your filter: (but a version that is based on images > directly would of course still be better): > > onPaint vbitmap dc viewArea > = do mbBitmap <- varGet vbitmap > case mbBitmap of > Nothing -> dcClear dc > -- Just bm -> drawBitmap dc bm pointZero False [] > Just bm -> > do sz <- get bm size > im <- imageCreateFromBitmap bm > pb <- imageGetPixelBuffer im > fillPixelBuffer pb sz > bm' <- bitmapCreateFromImage im (-1) > drawBitmap dc bm' pointZero True [] > bitmapDelete bm' > imageDelete im > where > fillPixelBuffer pb bound > = mapM_ redshift [Point x y | x <- [0..sizeW bound-1] > , y <- [0..sizeH bound-1]] > where > redshift pt > = do c <- pixelBufferGetPixel pb pt > let newcolor = rgb 255 (colorGreen c) (colorBlue c) > pixelBufferSetPixel pb pt newcolor I chaged the program like this.But program doen't work yet. Where is my mistake? onPaint vbitmap dc viewArea = do mbBitmap <- varGet vbitmap case mbBitmap of Nothing -> dcClear dc -- Just bm -> drawBitmap dc bm pointZero False [] Just bm -> do sz <- get bm size im <- imageCreateFromBitmap bm pb <- imageGetPixelBuffer im fillpixelBuffer pb sz bm' <- bitmapCreateFromImage im (-1) drawBitmap dc bm' pointZero True [] bitmapDelete bm' imageDelete im where fillpixelBuffer pb bound = mapM_ drawLowpassFilter [Point x y | x <- [0..sizeW bound-1] , y <- [0..sizeH bound-1]] where drawLowpassFilter (Point szx szy) = do lowpass <- (lowpassFilter (Point szx szy) bound) pixelBufferSetPixel pb (Point szx szy) lowpass pixelColorInt (Point ptx pty) (Size szx szy) | ptx < 0 = pixelColorInt (Point 0 pty) (Size szx szy) | pty < 0 = pixelColorInt (Point ptx 0) (Size szx szy) | ptx > szx = pixelColorInt (Point ptx pty) (Size 0 szy) | pty < szy = pixelColorInt (Point ptx pty) (Size 0 szy) | otherwise = do pixel <- pixelBufferGetPixel pb (Point ptx pty) return (intFromColor pixel) lowpassFilter (Point szx szy) bound = do -- pixelsInt <- foldM (+) (pixelColorInt [Point x y | x <- [szx-n..szx+n], y <- [szy-n..szy+n]]) pix1 <- pixelColorInt (Point (szx-1) (szy-1)) bound pix2 <- pixelColorInt (Point (szx-1) (szy)) bound pix3 <- pixelColorInt (Point (szx-1) (szy+1)) bound pix4 <- pixelColorInt (Point (szx) (szy-1)) bound pix5 <- pixelColorInt (Point (szx) (szy)) bound pix6 <- pixelColorInt (Point (szx) (szy+1)) bound pix7 <- pixelColorInt (Point (szx+1) (szy-1)) bound pix8 <- pixelColorInt (Point (szx+1) (szy)) bound pix9 <- pixelColorInt (Point (szx+1) (szy+1)) bound let pixes = (pix1 + pix2 + pix3 + pix4 + pix5 + pix6 + pix7 + pix8 + pix9) `div` 9 return (colorFromInt pixes) -- (colorFromInt $ (pixelsInt / 9)) > Anyway, here is why your program doesn't work: > 1) The pixelbuffer returned from "imageGetPixelBuffer" is a static > pointer to the image data: if you change the pixelbuffer, the image > changes too! Therefore, do *not* delete the pixelbuffer. > > 2) When the pixelbuffer is changed, the image is changed too -- there > is no need to create a separate pixelbuffer and image. First read all > pixels into list of something, and than write them back again when > they are changed according to your filter. > > 3) You delete the original bitmap in the paint handler... the next time > it is called the data is invalid. > > 4) It would be much better to store the image only in the "vbitmap" and > not a bitmap. That way, you only need to manipulate the image once (and > not at every call to "onPaint", and you only extract a bitmap from an > image. > > 5) You indices are out-of-bounds. The range is from 0 to the width, > instead of 0 to the width-1. PS. > ps. Are you from Japan? As a Dutchman, I have always been interested > in Japan -- would love to go there some time and snowboard on Mt. Fuji > :-) Yes. I am Japanese. Shelarcy is handle and nick name. I'm glad to you are interested in Japan. -- shelarcy <she...@ca...> http://page.freett.com/shelarcy/ |