/* Copyright (c) <2021> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import FITS import Foundation /** Decodes chunked RGB to interleved RRR RGB GGG -> RGB BBB RGB */ public struct RGB_Decoder : ImageDecoderGCD { public typealias Paramter = Void public typealias Pixel = Format public typealias Out = Float private var width1 : Int private var height1 : Int private var size1 : Int private var size2 : Int private var max1 : Float private var scale : Float private var add1 : Float public init(_ parameter: Void, width: Int, height: Int, bscale: Float, bzero: Float, min: Byte, max: Byte) { self.width1 = width self.height1 = height self.size1 = width*height self.size2 = width*height*2 self.scale = bscale self.add1 = bzero - (bzero + min.float * bscale) self.max1 = (bzero + max.float * bscale) - (bzero + min.float * bscale) } public func block(for thread: Int, of: Int, _ data: UnsafeBufferPointer, _ out: UnsafeMutableBufferPointer) where In : FITSByte { let cap = height1/of for y in stride(from: thread*cap, to: (thread+1)*cap, by: 1) { switch self { case is RGB_Decoder: RGBrow(line: y, data, out) case is RGB_Decoder: ARGBrow(line: y, data, out) case is RGB_Decoder: RGBArow(line: y, data, out) case is RGB_Decoder: GRAYrow(line: y, data, out) default: fatalError("Pixel Format not recognized") } } } /** computes average from a vertical line ... ... ... ... x ... ... ... ... */ private func native(_ data: UnsafeBufferPointer, _ offset: Int) -> Float { let sum = data[offset].bigEndian.float // top return (add1 + sum) / max1 } } // MARK: - RGB w/o alpha extension RGB_Decoder { public func RGBrow(line y: Int, _ data: UnsafeBufferPointer, _ out: UnsafeMutableBufferPointer) { var offset = y*width1 var pixel = offset*3 for _ in 0..(line y: Int, _ data: UnsafeBufferPointer, _ out: UnsafeMutableBufferPointer) { var offset = y*width1 var pixel = offset*4 for _ in 0..(line y: Int, _ data: UnsafeBufferPointer, _ out: UnsafeMutableBufferPointer) { var offset = y*width1 var pixel = offset*4 for _ in 0..(line y: Int, _ data: UnsafeBufferPointer, _ out: UnsafeMutableBufferPointer) { var offset = y*width1 var pixel = offset for _ in 0..