Skip to content

Fix preconditions and generic types (#1) #3

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions Sources/SGLImage/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ final public class SGLImageRGBA8 : SGLImageType {

public subscript(x:Int, y:Int) -> (r:UInt8,g:UInt8,b:UInt8,a:UInt8) {
get {
precondition(x > 0 && x < width)
precondition(y > 0 && y < height)
precondition(x >= 0 && x < width)
precondition(y >= 0 && y < height)
return array[x + y * width]
}
set {
precondition(x > 0 && x < width)
precondition(y > 0 && y < height)
precondition(x >= 0 && x < width)
precondition(y >= 0 && y < height)
array[x + y * width] = newValue
}
}
Expand All @@ -98,7 +98,7 @@ final public class SGLImageRGBA8 : SGLImageType {


// Generic image container.
public class SGLImage<T> : SGLImageType {
public class SGLImage<T:BinaryInteger> : SGLImageType {
public let width:Int, height:Int, channels:Int
private let buffer:UnsafeMutableBufferPointer<T>

Expand All @@ -125,22 +125,22 @@ public class SGLImage<T> : SGLImageType {

public subscript(x:Int, y:Int, channel:Int) -> T {
get {
precondition(x > 0 && x < width)
precondition(y > 0 && y < height)
precondition(channel > 0 && channel < channels)
precondition(x >= 0 && x < width)
precondition(y >= 0 && y < height)
precondition(channel >= 0 && channel < channels)
return buffer[channel + x * channels + y * width * channels]
}
set {
precondition(x > 0 && x < width)
precondition(y > 0 && y < height)
precondition(channel > 0 && channel < channels)
precondition(x >= 0 && x < width)
precondition(y >= 0 && y < height)
precondition(channel >= 0 && channel < channels)
buffer[channel + x * channels + y * width * channels] = newValue
}
}
}


final public class SGLImageGreyscale<T> : SGLImage<T> {
final public class SGLImageGreyscale<T:BinaryInteger> : SGLImage<T> {
public init(_ loader:SGLImageLoader) {
precondition(loader.error == nil)
super.init(
Expand All @@ -153,7 +153,7 @@ final public class SGLImageGreyscale<T> : SGLImage<T> {
}


final public class SGLImageGreyAlpha<T> : SGLImage<T> {
final public class SGLImageGreyAlpha<T:BinaryInteger> : SGLImage<T> {
public init(_ loader:SGLImageLoader) {
precondition(loader.error == nil)
super.init(
Expand All @@ -166,7 +166,7 @@ final public class SGLImageGreyAlpha<T> : SGLImage<T> {
}


final public class SGLImageRGB<T> : SGLImage<T> {
final public class SGLImageRGB<T:BinaryInteger> : SGLImage<T> {
public init(_ loader:SGLImageLoader) {
precondition(loader.error == nil)
super.init(
Expand All @@ -179,7 +179,7 @@ final public class SGLImageRGB<T> : SGLImage<T> {
}


final public class SGLImageRGBA<T> : SGLImage<T> {
final public class SGLImageRGBA<T:BinaryInteger> : SGLImage<T> {
public init(_ loader:SGLImageLoader) {
precondition(loader.error == nil)
super.init(
Expand Down