[swift-users] How do I turn an array into a bitmap image?

Nevin Brackett-Rozinsky nevin.brackettrozinsky at gmail.com
Wed Mar 15 14:01:50 CDT 2017


I think I might not be explaining this very well.

I want to create a bitmap image where each pixel is RGBA with 8 bits
(UInt8) per channel, and I want to specify the exact color for every pixel.
I do not know how to do that.

Also, for my specific application, I need to take an array of floating
point numbers and say, “Hey, the underlying bits that make up the IEEE-754
representation of these numbers are exactly the bits that I want to use for
the colors of the pixels.” I do not know how to do that in Swift.

In C I would malloc a buffer, write to it as (float*), then pass it to a
function which takes (char*) and saves a PNG. Thus there is only one
allocation, the buffer is filled with float values, and the exact
bit-pattern is interpreted as RGBA pixels.

How can I do the equivalent in Swift?

Nevin


On Wed, Mar 15, 2017 at 2:43 PM, Jens Alfke <jens at mooseyard.com> wrote:

>
> On Mar 15, 2017, at 11:07 AM, Nevin Brackett-Rozinsky via swift-users <
> swift-users at swift.org> wrote:
>
> Ideally I’d like to create the bitmap image without having to allocate a
> second buffer. The sole purpose of the Float array is for creating this
> image, so I’d like to directly use its bytes as the pixels data. I expect
> this will involve some Unsafe[…] APIs in Swift, though I don’t know exactly
> how.
>
>
> AFAIK there aren’t any pixmap formats that use floats. If you want to
> produce ARGB pixels, then emit your data in that format, i.e. as a [UInt8].
> No unsafe stuff needed.
>
> And yes, I am on an Apple platform. Despite reading boatloads of Core
> Graphics documentation, I still can’t make heads or tails out of how to
> turn a pre-existing array into the backing data for a bitmap image. This
> part is only Swift-related in the sense that I am trying to do it in Swift,
> so if there’s a better place to ask/learn about it I’d appreciate being
> pointed there.
>
>
> Either Apple’s developer forums, or the cocoa-dev mailing list hosted at
> http://lists.apple.com.
>
> —Jens
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170315/e6826b0c/attachment.html>


More information about the swift-users mailing list