Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write out to non-file IO stream #54

Open
david-sledge opened this issue Feb 19, 2024 · 4 comments
Open

Write out to non-file IO stream #54

david-sledge opened this issue Feb 19, 2024 · 4 comments

Comments

@david-sledge
Copy link

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

@lehins
Copy link
Owner

lehins commented Feb 20, 2024

@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
Copy link
Author

david-sledge commented Feb 20, 2024

@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
Copy link
Owner

lehins commented Feb 20, 2024

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
Copy link
Owner

lehins commented Feb 20, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants