Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell what type the DynamicImage loaded by Codec.Picture is

I am trying to load a PNG using the JuicyPixels library and I am able to do this successfully but I then can't figure out what type the underlying image is. In the library the DynamicImage is defined as follows.

data DynamicImage =
       ImageY8   (Image Pixel8)
     | ImageYA8  (Image PixelYA8)
     | ImageRGB8 (Image PixelRGB8)
     | ImageRGBA8 (Image PixelRGBA8)
     | ImageYCbCr8 (Image PixelYCbCr8)

Through simple trial and error I was able to find out that it was a ImageRGBA8 by doing this.

img = (\(ImageRBA8 t) -> t) dynImage

then just doing

imageData img

In ghci to print the image data, all other types give an error. So how can I figure out the what type of DynamicImage has been loaded without trial an error. I'm sure this is quite simple and I am just missing something.

like image 674
DiegoNolan Avatar asked Oct 05 '22 04:10

DiegoNolan


1 Answers

DynamicImage is just an ADT so you can pattern match against it in a function definition or case expression. The exact constructor used will depend on the type of image you loaded from the file (so in this case it was RGA8 but in other cases it could be different).

For example:

case img of
  ImageY8   imgPixel8   -> ... do something ...
  ImageYA8  imgPixelYA8 -> ...
  ImageRGB8 imgPixelRGB8 -> ...
  ImageRGBA8 imgPixelRGBA8 -> ...
  ImageYCbCr8 imgPixelYCbCr8 -> ...
like image 161
Thomas M. DuBuisson Avatar answered Oct 09 '22 12:10

Thomas M. DuBuisson