Reading pixel from images using Haskell - haskell

Does anyone know how to read a pixel from images using Haskell? I am now using a juicy pixel as a library.
Thanks for your help!

JuicyPixel provides the pixelAt function to get a pixel at a given coordinate.
pixelAt :: Image a -> Int -> Int -> a
Extract a pixel at a given position, (x, y), the origin is assumed to be at the corner top left, positive y to the bottom of the image

Related

Get the Height and Width of an image

I'm very new to Haskell so I apologise if this is too basic, or if it makes very little sense. I'm trying to read an image; I can get it to a list of pixel data with the following code:
data Pixel = Pixel {
red :: Integer,
green :: Integer,
blue :: Integer,
alpha :: Integer
} deriving (Show)
getImagePixelArray :: FilePath -> IO (Codec.Picture.Repa.Img RGBA)
getImagePixelArray fp = do
img <- (either error return =<< readImageRGBA fp)
return img
getImagePixelData :: Codec.Picture.Repa.Img RGBA -> [(Word8,Word8,Word8,Word8)]
getImagePixelData img = R.toList (collapseColorChannel img)
rawPixelToPixel :: (Word8,Word8,Word8,Word8) -> Pixel
rawPixelToPixel (r, g, b, a) = Pixel {
red = (toInteger r),
green = (toInteger g),
blue = (toInteger b),
alpha = (toInteger a)
}
main = do
imageData <- getImagePixelArray "./images/image1.png"
let imageRawPixels = getImagePixelData imageData
let asPixels = Prelude.map rawPixelToPixel imageRawPixels
mapM print asPixels
I more or less follow what's going on, but my knowledge of Haskell is still limited, so when it comes to making sense of api documentation, I'm struggling a little.
I really want to be able to parse the positions of the pixels; I understand how to do that if I know the width and height of an image, but I can't figure out how to get the width/height of an image.
I'm making my way through a Haskell course, but I'm also trying to put it to some practical use along the way to help the concepts sink in.
Start with the documentation. None of what you're asking for is obviously available on Img, so where else could it be? There's a related Image type, mentioned in convertImage's documentation - I wonder what that is. As it turns out, it has a width and a height. How can we get an Image from our Img, then? imgToImage will give us a DynamicImage, which has several constructors each containing an Image. Figure out what kind of Image you have inside your DynamicImage, and then ask that Image its dimensions.
Perhaps there is something easier with the other lens-y stuff in that module, but it is not obvious to me, and this approach seems simple enough, if a hassle.

ellipse in haskell CodeWorld

Can anyone tell me how to draw an Ellipse using the CodeWorld package of Haskell? I want it to be like the rectangle function where I give two arguments for length and width. I have tried using solidClosedCurve-am I heading in the right direction?
Using a closed curve, you can set the 4 vertices like so:
ellipse'(a, b) = closedCurve([(a,0),(0,b),(-a,0),(0,-b)])
Another way to do it is to say that an ellipse is a circle rescaled in one direction.
ellipse(a, b) = scaled(circle(1), a, b)
https://code.world/#Ps8tKc4KH4v8Z4iq91NZsew

Rasterizing TrueType fonts in Haskell

I'd like to build a function that takes a given Unicode character and a given TrueType font and rasterises the corresponding glyph into a two-dimensional 1-bit-per-pixel bitmap.
Something similar to this:
rasterize :: Font -> Char -> Size -> Bitmap
rasterize font char size = ...
Requirements
The rasterize function should:
produce a bitmap of maximal width w and height h, such that w <= size and h <= size
rasterize the glyph so that it completely fills the bitmap, leaving no padding.
The Bitmap type should support the following operations (or similar):
width :: Bitmap -> Int
height :: Bitmap -> Int
bit :: Bitmap -> (Int, Int) -> Bool
where bit b (x, y) should evaluate to True if (and only if) the bit within Bitmap b at index position (x, y) is set.
Question
Are there any Haskell libraries that can already do this, or something similar?
Example
Evaluating the expression rasterize fontUtopiaStandard 'o' 64 would result in a Bitmap b that if rendered would look similar to the following image (viewed at 800% scale, with grid), where (width b, height b) = (60, 64):
Notes
I've already tried using the stb-truetype package, but any attempts to get at the pixel data seem to cause segmentation faults, even when compiling with version of GHC (6.12) similar to the version on which the package was tested. (I can provide more details of the segfaulting code if anyone's interested.)
I'm aware of the existence of libraries that render TrueType fonts within an OpenGL context, but I can't see how to get access to the pixel data.

How to find custom shape speicific area?

please , see following image, here you can see blue rectangle is custom shape bounds and custom shape is shoe , i want to find area of a portion written in image and i want that area in form of rectangle
do is there any path iterator concept ?
Note
custom shape i derived from image of the same size.
I would do it like this:
1.create table for all bounding box-rect perimeter lines
each value in it will represent the empty space length form border line to shape
something like this:
the values are found by simple image scanning until first non space color found
2.now bruteforce find the biggest rectangle area
x,y = top left corner
for xs = 1 to bounding box width
now scan the max valid height of rectangle from x to x + xs (x grows to the right)
// it should be the min y0[x..x+xs]
remember the biggest valid area/size combination
do this for all 4 combinations (star from the other corners)
I now Brute-force is slow but
you can divide perimeter lines not by pixels but with some step instead
also I am sure this can be optimized somehow
for example by derivation of perimeter find the extremes and check from them backwards
when the size will start shrinking then stop ...
of course take in mind that on complicated shapes this optimization will not work ...

jung ObservableCachingLayout vs staticLayout

i m trying to get the x and y cordinate from the layout but when geting the layout from a VisualizationViewer the returned type is not staticLayout but ObservableCachingLayout (of course it couldn't be casted to static).
Is there a way to get the staticlayout from a VisualizationViewer ?
Or a way to get the x and y from the viz ? thx
To get the x and y coordinate from the Layout, you call layout.transform(vertex). (A Layout is a Transformer from vertices to Point objects.)
StaticLayout is an instance of Layout that allows the user to easily specify the coordinates of each vertex; it's intended for the case in which you already have coordinates and don't need an algorithm to determine them.

Resources