<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 15, 2017, at 9:56 AM, Nevin Brackett-Rozinsky via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">For one of my programs, I need to take an array of Floats and use the underlying bytes as the RGBA data for a bitmap image. In particular, the 32 bits of each Float must be treated as the color of the corresponding pixel. So the result is an image of height 1 and width the length of the array.</span></div></blockquote><div class=""><div style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div></div><div style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Make a pixmap, an array of UInt8 that’s 4x longer than your original array, where the elements are interpreted as R, G, B, A, R, G, B, A… Then fill it in based on the original float values.</div><div style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">In particular, I need to be able to save the image as a PNG file, then later load it from disk and reconstruct each Float from its pixel, with no loss or change to the value of the Floats. So, how do I go from [Float] to a bitmap image suitable for saving to disk, without altering the underlying bytes?</div></div></blockquote></div><br class=""><div class="">You’ll need to call some external image-encoding library, passing it your pixmap. If you’re on an Apple platform try ImageIO. If not, or if you want to be cross-platform, try libpng.</div><div class=""><br class=""></div><div class="">—Jens</div></body></html>