lehins / hip

Haskell Image Processing Library
116 stars 17 forks source link

Write out to non-file IO stream #54

Open david-sledge opened 7 months ago

david-sledge commented 7 months ago

Is there an option to write an image to IO locations other than a file, such as part of an HTTP response?

lehins commented 7 months ago

@david-sledge You can use encode to convert an image to ByteString, which you can later use for other IO operations like transferring over the network

david-sledge commented 7 months ago

@david-sledge You can use encode to convert an image to ByteString, which you can later use for other IO operations like transferring over the network

Thanks. After looking at the code for writeImage, I realized I should have been able to figure that out myself.

From the functions readImage and writeImage, I've derived the following:

deserializeImage :: forall arr cs e .
             (Array VS cs e, Array arr cs e,
              Readable (Image VS cs e) InputFormat) =>
              InputFormat -- ^ Image format
           -> BL.ByteString -- ^ A serialized image
           -> Either String (Image arr cs e)
deserializeImage format imgstr =
  let swapEither = either pure Left in
  exchange (undefined :: arr)
    <$> swapEither . M.foldM (\ err fmt ->
        ((err ++ "\n") ++) <$> swapEither (decode fmt imgstr)
      ) "" (format : P.filter (/= format) (enumFrom $ toEnum 0))

serializeImage :: (Array VS cs e, Array arr cs e,
               Writable (Image VS cs e) OutputFormat) =>
              OutputFormat -- ^ Image format
           -> Image arr cs e -- ^ An image to serialize.
           -> BL.ByteString
serializeImage format = encode format [] . exchange VS

If there's no objections, I'd like to submit a pull request to add the above along with the counterparts for readImageExact and writeImageExact.

lehins commented 7 months ago

Current master has diverged significantly from hip version that is currently on hackage.

It has been completely rewritten with the use of massiv, massiv-io and Color.

Unfortunately I got really busy with work before I was able to complete the transition. Maybe sometime later this year I'll be able to finish it up, but at this moment I don't have any time for this.

That being said, I could create a feature branch that will continue support for older version and if you really want to submit those functions as a PR that would allow you to do that and I could create a release for that minor version. Let me know if that is something you are still interested in doing

lehins commented 7 months ago

I totally forgot, there is already a branch like that: https://github.com/lehins/hip/tree/hip-1.x

Make sure you target that branch