I am working on an application in which the best way that I (so far) have discovered that I am manipulating images is with the Graphics.Netpbm library. I will delegate many operations, such as raw file development, to an external application, and I just need a very clean, well-supported format.
I really got his job. I can tell ufraw-batch to โdevelopโ the raw file and output the result in pnm format to stdout. Graphics.Netpbm.parsePPM perfectly parses the image. Then I need to convert it to pixbuf for display in GtkImage widgets, and although I have a solution, I think it is pretty important. I would like it to be better.
When reading this wall of code, you can skip the entire let declaration and understand that img_list :: Netpbm.PPM -> [CUChar] .
display_image :: Graphics.UI.Gtk.Image -> Netpbm.PPM -> IO () display_image canvas image_bitmap = let get_rgb_vector = Netpbm.pixelVectorToList . (\(Netpbm.PpmPixelDataRGB8 v) -> v) . Netpbm.ppmData vector_to_cuchar_list = map CUChar . concat . map (\(Netpbm.PpmPixelRGB8 rgb) -> r:g:b:[]) img_list = vector_to_cuchar_list . get_rgb_vector len = length . img_list width = Netpbm.ppmWidth . Netpbm.ppmHeader height = Netpbm.ppmHeight . Netpbm.ppmHeader in do img_ptr <- mallocBytes (len image_bitmap) mapM_ (\(b, off) -> pokeByteOff img_ptr off (b :: CUChar)) (zip (img_list image_bitmap) [0,1..]) pb <- pixbufNewFromData img_ptr ColorspaceRgb False 8 (width image_bitmap) (height image_bitmap) ((width image_bitmap) * 3) imageSetFromPixbuf canvas pb
The function at the top starts with many utilities to actually get the original pixel data. They are also quite dirty, and I donโt know yet how best to organize them. But the gross part is in the do block. I allocate an array of C arrays to store all the data. Then I repeat [CUChar], which represents the image, knocking every single byte to the right place in array C. And then I create GtkPixbuf from this data.
Ug. What is a faster, cleaner and safer way to do this?
pixbufNewFromData takes Ptr CUChar as the first parameter, where Netpbm with all the deployments can ultimately provide me [Word8] . Therefore, in my opinion, I need a pure function that does [Word8] -> Ptr CUChar . It does not have to be clean.
I also do not mind making major changes, but my data flow as a whole
operation that generates a ByteString of pnm data ---> GtkImage