Skip to content

Commit

Permalink
convert the basic decoding/encoding examples into Snippets, and repla…
Browse files Browse the repository at this point in the history
…ce BasicEncoding test image with more professionally appropriate example
  • Loading branch information
tayloraswift committed Jan 30, 2024
1 parent c00a7fb commit ca3091b
Show file tree
Hide file tree
Showing 29 changed files with 2,896 additions and 71 deletions.
Binary file removed Examples/encode-basic/example-color-rgb.png
Binary file not shown.
Binary file removed Examples/encode-basic/[email protected]
Binary file not shown.
Binary file removed Examples/encode-basic/[email protected]
Binary file not shown.
Binary file removed Examples/encode-basic/[email protected]
Binary file not shown.
Binary file removed Examples/encode-basic/[email protected]
Binary file not shown.
Binary file removed Examples/encode-basic/example-color-v.png
Binary file not shown.
Binary file removed Examples/encode-basic/example-luminance-rgb.png
Binary file not shown.
Binary file removed Examples/encode-basic/example-luminance-v.png
Binary file not shown.
1 change: 0 additions & 1 deletion Examples/encode-basic/example.rgba

This file was deleted.

37 changes: 1 addition & 36 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ let package:Package = .init(name: "swift-png",
.executable(name: "compression-benchmark", targets: ["PNGCompressionBenchmarks"]),
.executable(name: "decompression-benchmark", targets: ["PNGDecompressionBenchmarks"]),

.executable(name: "decode-basic", targets: ["PNGDecodeBasic"]),
.executable(name: "encode-basic", targets: ["PNGEncodeBasic"]),
.executable(name: "indexing", targets: ["PNGIndexing"]),
.executable(name: "iphone-optimized", targets: ["PNGiPhoneOptimized"]),
.executable(name: "metadata", targets: ["PNGMetadata"]),
Expand Down Expand Up @@ -106,40 +104,7 @@ let package:Package = .init(name: "swift-png",
],
path: "Benchmarks/Decompression/Swift"),

.executableTarget(name: "PNGDecodeBasic",
dependencies:
[
.target(name: "PNG"),
],
path: "Examples/decode-basic",
exclude:
[
"example.png.rgba",
"example.png.v.png",
"example.png.rgba.png",
"example.png",
"example.png.v",
"example.png.va.png",
"example.png.va",
]),
.executableTarget(name: "PNGEncodeBasic",
dependencies:
[
.target(name: "PNG"),
],
path: "Examples/encode-basic",
exclude:
[
"[email protected]",
"[email protected]",
"example-color-v.png",
"[email protected]",
"example-color-rgb.png",
"example-luminance-rgb.png",
"example.rgba",
"[email protected]",
"example-luminance-v.png",
]),

.executableTarget(name: "PNGIndexing",
dependencies:
[
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Swift *PNG* is [available](LICENSE) under the [Mozilla Public License 2.0](https

## [tutorials and example programs](examples/)

1. [basic decoding](examples/#basic-decoding) ([sources](examples/decode-basic/))
2. [basic encoding](examples/#basic-encoding) ([sources](examples/encode-basic/))
1. [basic decoding](Snippets/BasicDecoding.swift) ([sources](Snippets/BasicDecoding.swift/))
2. [basic encoding](Snippets/BasicEncoding.swift) ([sources](Snippets/BasicEncoding.swift/))
3. [using indexed images](examples/#using-indexed-images) ([sources](examples/indexed/))
4. [using iphone-optimized images](examples/#using-iphone-optimized-images) ([sources](examples/iphone-optimized/))
5. [working with metadata](examples/#working-with-metadata) ([sources](examples/metadata/))
Expand Down Expand Up @@ -48,7 +48,8 @@ Decode an image:
import PNG
func decode(png path:String) throws
{
guard let image:PNG.Data.Rectangular = try .decompress(path: path)
guard
let image:PNG.Data.Rectangular = try .decompress(path: path)
else
{
// failed to access file from file system
Expand Down
39 changes: 23 additions & 16 deletions Examples/decode-basic/main.swift → Snippets/BasicDecoding.swift
Original file line number Diff line number Diff line change
@@ -1,50 +1,57 @@
import PNG
import PNG

let path:String = "examples/decode-basic/example"
let path:String = "Snippets/BasicDecoding/example"

guard let image:PNG.Data.Rectangular = try .decompress(path: "\(path).png")
else
guard
let image:PNG.Data.Rectangular = try .decompress(path: "\(path).png")
else
{
fatalError("failed to open file '\(path).png'")
}

let rgba:[PNG.RGBA<UInt8>] = image.unpack(as: PNG.RGBA<UInt8>.self)
guard let _:Void = (System.File.Destination.open(path: "\(path).png.rgba")
guard
let _:Void = (System.File.Destination.open(path: "\(path).png.rgba")
{
guard let _:Void = $0.write(rgba.flatMap{ [$0.r, $0.g, $0.b, $0.a] })
else
guard
let _:Void = $0.write(rgba.flatMap{ [$0.r, $0.g, $0.b, $0.a] })
else
{
fatalError("failed to write to file '\(path).png.rgba'")
}
})
})
else
{
fatalError("failed to open file '\(path).png.rgba'")
}

let va:[PNG.VA<UInt8>] = image.unpack(as: PNG.VA<UInt8>.self)
guard let _:Void = (System.File.Destination.open(path: "\(path).png.va")
guard
let _:Void = (System.File.Destination.open(path: "\(path).png.va")
{
guard let _:Void = $0.write(va.flatMap{ [$0.v, $0.a] })
else
guard
let _:Void = $0.write(va.flatMap{ [$0.v, $0.a] })
else
{
fatalError("failed to write to file '\(path).png.va'")
}
})
})
else
{
fatalError("failed to open file '\(path).png.va'")
}

let v:[UInt8] = image.unpack(as: UInt8.self)
guard let _:Void = (System.File.Destination.open(path: "\(path).png.v")
guard
let _:Void = (System.File.Destination.open(path: "\(path).png.v")
{
guard let _:Void = $0.write(v)
else
guard
let _:Void = $0.write(v)
else
{
fatalError("failed to write to file '\(path).png.v'")
}
})
})
else
{
fatalError("failed to open file '\(path).png.v'")
Expand Down
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
34 changes: 19 additions & 15 deletions Examples/encode-basic/main.swift → Snippets/BasicEncoding.swift
Original file line number Diff line number Diff line change
@@ -1,62 +1,66 @@
import PNG

let path:String = "examples/encode-basic/example",
size:(x:Int, y:Int) = (800, 1228)
guard let rgba:[PNG.RGBA<UInt8>] = (System.File.Source.open(path: "\(path).rgba")
/// https://commons.wikimedia.org/wiki/File:Photo_of_a_venetian_mask_in_a_studio_photo_session.jpg
let path:String = "Snippets/BasicEncoding/example"
let size:(x:Int, y:Int) = (638, 425)

guard
let rgba:[PNG.RGBA<UInt8>] = (System.File.Source.open(path: "\(path).rgba")
{
guard let data:[UInt8] = $0.read(count: 4 * size.x * size.y)
else
guard
let data:[UInt8] = $0.read(count: 4 * size.x * size.y)
else
{
fatalError("failed to read from file '\(path).rgba'")
}

return (0 ..< size.x * size.y).map
return (0 ..< size.x * size.y).map
{
(i:Int) -> PNG.RGBA<UInt8> in
.init(data[4 * i], data[4 * i + 1], data[4 * i + 2], data[4 * i + 3])
}
})
})
else
{
fatalError("failed to open file '\(path).rgba'")
}

let layout:(rgb:PNG.Layout, v:PNG.Layout) =
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))
)

do
do
{
let image:PNG.Data.Rectangular = .init(packing: rgba, size: size, layout: layout.rgb)
try image.compress(path: "\(path)-color-rgb.png", level: 9)

for level:Int in [0, 4, 8, 13]
{
try image.compress(path: "\(path)-color-rgb@\(level).png", level: level)
}
}
do
do
{
let image:PNG.Data.Rectangular = .init(packing: rgba, size: size, layout: layout.v)
try image.compress(path: "\(path)-color-v.png", level: 9)
}

let luminance:[UInt8] = rgba.map
let luminance:[UInt8] = rgba.map
{
let r:Double = .init($0.r),
let r:Double = .init($0.r),
g:Double = .init($0.g),
b:Double = .init($0.b)
let l:Double = (0.299 * r * r + 0.587 * g * g + 0.114 * b * b).squareRoot()
return .init(max(0, min(l.rounded(), 255)))
}
do
do
{
let image:PNG.Data.Rectangular = .init(packing: luminance, size: size, layout: layout.v)
try image.compress(path: "\(path)-luminance-v.png", level: 9)
}
do
do
{
let image:PNG.Data.Rectangular = .init(packing: luminance, size: size, layout: layout.rgb)
try image.compress(path: "\(path)-luminance-rgb.png", level: 9)
Expand Down
Binary file added Snippets/BasicEncoding/example-color-rgb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Snippets/BasicEncoding/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Snippets/BasicEncoding/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Snippets/BasicEncoding/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Snippets/BasicEncoding/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Snippets/BasicEncoding/example-color-v.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Snippets/BasicEncoding/example-luminance-rgb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Snippets/BasicEncoding/example-luminance-v.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2,849 changes: 2,849 additions & 0 deletions Snippets/BasicEncoding/example.rgba

Large diffs are not rendered by default.

0 comments on commit ca3091b

Please sign in to comment.