hpastetwo

.

author
Kashyap
age
41 days
language
haskell
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import qualified  Data.ByteString as B
import Control.Monad.State

width = 256
height = 256
bytesInImage = width * height * 3
blankImage =  B.pack $ take bytesInImage (repeat  0)

type Color = (Int,Int,Int)
setPixel1 :: B.ByteString -> Int -> Int -> Color ->  B.ByteString
setPixel1 image x y (r,g,b) = B.concat [beforePixel,  pixel, afterPixel]
         where
                 beforePixel = B.take before image
                 afterPixel = B.drop (before+3)  image
                 pixel=B.pack [(fromIntegral r),(fromIntegral g),(fromIntegral  b)]
                 before = (y * width * 3) + (x * 3) -  3



setPixel' :: Int -> Int -> Color ->  B.ByteString -> ((), B.ByteString)
setPixel'  x y (r,g,b)  image = ((), B.concat [beforePixel, pixel, afterPixel])
         where
                 beforePixel = B.take before image
                 afterPixel = B.drop (before+3)  image
                 pixel=B.pack [(fromIntegral r),(fromIntegral g),(fromIntegral  b)]
                 before = (y * width * 3) + (x * 3) -  3

setPixel x y rgb = State $ setPixel' x y rgb


drawPixels = do
	setPixel 5 10 (255, 255, 255)
	setPixel 100 100 (255, 0, 0)
	setPixel 101 100 (255, 0, 0)
	setPixel 101 101 (255, 0, 0)
	setPixel 100 101 (255, 0, 0)
	setPixel 90 2 (255, 255,  255)
	
modifiedImage = execState drawPixels  blankImage



main = do
        putStrLn "P6"
        putStrLn (  (show width) ++ " " ++ (show height) )
        putStrLn "255"
	B.putStr modifiedImage
        -- Set a red  pixel at 100 100
        --B.putStr (setPixel 100 100 (255,0,0) blankImage)