UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Array 'vertical flip'

Forums > Swift

Are algorithm questions allowed here? Well here goes anyway.

I am loading a large dataset into an array, doing some data processing to generate an array of pixel data which I then save as a PNG file.

For the original data, the reference point for the data is the bottom-left corner, but when saving to a PNG file the reference point is the top-left corner - so I have to do a vertical flip.

To add a little complication, I am not able to use CoreGraphics image handling as my code will run on a RaspberryPi, so I am using PureSwift/PNG (https://github.com/PureSwift/png) instead.

The code I have works but my method could be a bit slow - the typical images that I am generating being 7200x7200px (~52M pixels). Redesigning my implementation such that the pixelData is [UInt8][UInt8] might allow me to process the pixels a line at a time, but that would require a big rewrite of other code - which I am trying to avoid.!

import Foundation
import PNG

func writePixelstoFilePNG(_ pixelData: [UInt8], height: Int, width: Int, to destinationURL: URL) -> Bool {
  let size: (x: Int, y: Int) = (width, height)
  let layout: (rgb: PNG.Layout, v: PNG.Layout) = (
    rgb:  .init(format: .rgb8(palette: [], fill: nil, key: nil)),
    v:    .init(format:   .v8(             fill: nil, key: nil))
  )

  var px = pixelData
  vFlipImagePixels(&px, height: height, width: width)

  let image: PNG.Data.Rectangular = .init(packing: px, size: size, layout: layout.v)
  do {
    try image.compress(path: destinationURL.path, level: 0)
  } catch {
    return false
  }
  return true
}

func vFlipImagePixels(_ px: inout [UInt8], height: Int, width: Int) {
  for r in 0 ..< ((height >> 1)) {
    for c in 0 ..< width {
      px.swapAt((height * r) + c, (height - 1 - r) * width + c)
    }
  }
}

2      

Figured out an alternative way to vertically flip the array, by rows, using Array.replaceSubrange

Not sure if it is any more efficient than flipping pixel-by-pixel, but for me this seems a little more intuituive.

func vFlipImagePixelsByRow(_ px: inout [UInt8], height: Int, width: Int) {
  for r in 0 ..< (height >> 1) {
    let rangeA = (r*width) ..< (r*width + width)
    let rangeB = ((height - r - 1) * width) ..< (((height - r - 1) * width) + width)

    let tmpRow = px[rangeA]

    px.replaceSubrange(rangeA, with: px[rangeB])
    px.replaceSubrange(rangeB, with: tmpRow)
  }
}

2      

@KieranConlon -sorry this is not about the question, but can you suggest a good resource for algorithm, paid/free ...thanks

2      

Hi Amit,

Apple have a Swift algorithm's package which, I suppose is a general purpose 'toolbox' for anyone to use, swift-algorithms.

The Ray Wenderlich site (is it OK to mention then on here?) is a good resource, in fact they even have a tutorial for the swift-algorithms mentioned above, RW-Swift-Algorithms.

You could try searching StackOverflow, chances are that someone has asked a similar question in the past.

The application I am writing has very specific algorithms and mathematical methods that I had to implement. I found Mathematics and GIS communities on StackExchange to be really helpful.

Also, for mathematics algorithms and methods I found WolframAlpha's Mathworld to be excellent.

Otherwise, wikipedia is also a good place to look.

Kieran

3      

Thanks a lot never saw MathWorld before, seems great

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.