<commit_before>EasyImagy
===========================

_EasyImagy_ makes it easy to deal with images in Swift.

```swift
var image = Image<RGBA>(named: "ImageName")!

print(image[x, y])
image[x, y] = RGBA(red: 255, green: 0, blue: 0, alpha: 127)
image[x, y] = RGBA(0xFF00007F) // red: 255, green: 0, blue: 0, alpha: 127

// Iterates over all pixels
for pixel in image {
    // ...
}

// Converts the image (e.g. binarizations)
let binarized: Image<RGBA> = image.map { $0.gray < 128 ? .black : .white }

// From/to `UIImage`
image = Image<RGBA>(uiImage: imageView.image!)!
imageView.image = image.uiImage
```

Introduction
---------------------------

Dealing with images using _CoreGraphics_ is too complicated for casual use: various formats, old C APIs and painful memory management. _EasyImagy_ provides the easier way to deal with images in exchange for some performance.

Typically `Image`s in _EasyImagy_ are used with `RGBA`. `RGBA` is a simple structure declared as follows.

```swift
struct RGBA {
    var red: UInt8
    var green: UInt8
    var blue: UInt8
    var alpha: UInt8
}
```

You can access to a pixel easily using subscripts like `image[x, y]` and its channels by properties `red`, `green`, `blue` and `alpha`.

`Image` and `RGBA` also provide some convenient methods and properties to make processing images easy. For example, it is possible to convert an image to grayscale combining `Image#map` with `RGBA#gray` in one line as shown below.

```swift
let result = image.map { $0.gray }
```

Usage
---------------------------

### Import

```swift
import EasyImagy
```

### Initialization

```swift
let image = Image<RGBA>(named: "ImageName")!
```

```swift
let image = Image<RGBA>(contentsOfFile: "path/to/file")!
```

```swift
let image = Image<RGBA>(data: NSData(/* ... */))!
```

```swift
let image = Image<RGBA>(uiImage: imageView.image!)!
```

```swift
let image = Image<RGBA>(width: 640, height: 480, pixel: .black) // a black image
```

```swift
let image = Image<RGBA>(width: 640, height: 480, pixels: pixels)
```

### Access to a pixel

```swift
// Gets a pixel by subscripts
do {
    let pixel = image[x, y]
    /**/ _ = pixel
}
```

```swift
// Sets a pixel by subscripts
image[x, y] = RGBA(0xFF0000FF)
image[x, y].alpha = 127
```

```swift
// Safe get for a pixel
if let pixel = image.pixel(x, y) {
    print(pixel.red)
    print(pixel.green)
    print(pixel.blue)
    print(pixel.alpha)
    
    print(pixel.gray) // (red + green + blue) / 3
    print(pixel) // formatted like "#FF0000FF"
} else {
    // `pixel` is safe: `nil` is returned when out of bounds
    print("Out of bounds")
}
```

### Iteration

```swift
for pixel in image {
    ...<commit_msg>Fix README<commit_after>EasyImagy
===========================

_EasyImagy_ makes it easy to deal with images in Swift.

```swift
var image = Image<RGBA>(named: "ImageName")!

print(image[x, y])
image[x, y] = RGBA(red: 255, green: 0, blue: 0, alpha: 127)
image[x, y] = RGBA(0xFF00007F) // red: 255, green: 0, blue: 0, alpha: 127

// Iterates over all pixels
for pixel in image {
    // ...
}

// Converts the image (e.g. binarizations)
let binarized: Image<RGBA> = image.map { $0.gray < 128 ? .black : .white }

// From/to `UIImage`
image = Image<RGBA>(uiImage: imageView.image!)!
imageView.image = image.uiImage
```

Introduction
---------------------------

Dealing with images using _CoreGraphics_ is too complicated for casual use: various formats, old C APIs and painful memory management. _EasyImagy_ provides the easier way to deal with images in exchange for some performance.

Typically `Image`s in _EasyImagy_ are used with `RGBA`. `RGBA` is a simple structure declared as follows.

```swift
struct RGBA {
    var red: UInt8
    var green: UInt8
    var blue: UInt8
    var alpha: UInt8
}
```

You can access to a pixel easily using subscripts like `image[x, y]` and its channels by properties `red`, `green`, `blue` and `alpha`.

`Image` and `RGBA` also provide some convenient methods and properties to make processing images easy. For example, it is possible to convert an image to grayscale combining `Image#map` with `RGBA#gray` in one line as shown below.

```swift
let result = image.map { $0.gray }
```

Usage
---------------------------

### Import

```swift
import EasyImagy
```

### Initialization

```swift
let image = Image<RGBA>(named: "ImageName")!
```

```swift
let image = Image<RGBA>(contentsOfFile: "path/to/file")!
```

```swift
let image = Image<RGBA>(data: NSData(/* ... */))!
```

```swift
let image = Image<RGBA>(uiImage: imageView.image!)!
```

```swift
let image = Image<RGBA>(width: 640, height: 480, pixel: .black) // a black image
```

```swift
let image = Image<RGBA>(width: 640, height: 480, pixels: pixels)
```

### Access to a pixel

```swift
// Gets a pixel by subscripts
let pixel = image[x, y]
```

```swift
// Sets a pixel by subscripts
image[x, y] = RGBA(0xFF0000FF)
image[x, y].alpha = 127
```

```swift
// Safe get for a pixel
if let pixel = image.pixel(x, y) {
    print(pixel.red)
    print(pixel.green)
    print(pixel.blue)
    print(pixel.alpha)
    
    print(pixel.gray) // (red + green + blue) / 3
    print(pixel) // formatted like "#FF0000FF"
} else {
    // `pixel` is safe: `nil` is returned when out of bounds
    print("Out of bounds")
}
```

### Iteration

```swift
for pixel in image {
    ...
{'fraction_non_alphanumeric': 0.17367732286408674, 'fraction_numerical': 0.020938493176294634, 'mean_word_length': 3.4251447477253927, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3643770', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1757, 'n_words': 629}