Skip to content

Commit

Permalink
Add different resolutions for unsplash image suite
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivordir committed Nov 17, 2023
1 parent e3ca606 commit 9e3e7fc
Show file tree
Hide file tree
Showing 69 changed files with 106 additions and 75 deletions.
21 changes: 18 additions & 3 deletions examples/accuracy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)]

use std::{
ffi::OsStr,
fmt::{self, Display},
path::PathBuf,
};
Expand Down Expand Up @@ -180,9 +181,22 @@ fn main() {

fn report(options: Report) {
let images = if options.images.is_empty() {
util::load_image_dir_relative_to_root(util::CQ100_DIR)
util::load_image_dir_relative_to_root(
["img", "CQ100", "img"].into_iter().collect::<PathBuf>(),
)
.into_iter()
.map(|(path, img)| {
let name = path.file_stem().and_then(OsStr::to_str).unwrap().to_owned();
(name, img)
})
.collect::<Vec<_>>()
} else {
util::load_images(&options.images)
options
.images
.iter()
.map(|path| path.display().to_string())
.zip(util::load_images(&options.images))
.collect()
};

// use char count as supplement for grapheme count
Expand All @@ -199,7 +213,8 @@ fn report(options: Report) {
.k
.iter()
.map(|k| format!(
"{k:>0$} {1}",
"{:>1$} {2}",
k.into_inner(),
COL_WIDTH - NUM_DECIMALS - 1,
str::repeat(" ", NUM_DECIMALS)
))
Expand Down
3 changes: 2 additions & 1 deletion img/unsplash/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
The images in this directory (excluding subdirectories) were gathered from [Unsplash](https://unsplash.com/)
The images in the `Original` directory were gathered from [Unsplash](https://unsplash.com/)
and are available under the [Unsplash License](https://unsplash.com/license).
The other directories contain the same images resized to different resolutions.

# Attribution

Expand Down
Binary file added img/unsplash/img/1920x1280/Akihabara.jpg
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 img/unsplash/img/1920x1280/Boothbay.jpg
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 img/unsplash/img/1920x1280/Bryggen.jpg
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 img/unsplash/img/1920x1280/Futuristic.jpg
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 img/unsplash/img/1920x1280/Hokkaido.jpg
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 img/unsplash/img/1920x1280/Jewel Changi.jpg
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 img/unsplash/img/1920x1280/Lake Atitlan.jpg
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 img/unsplash/img/1920x1280/Louvre.jpg
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 img/unsplash/img/1920x1280/Rosetta Nebula.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 img/unsplash/img/240x160/Akihabara.jpg
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 img/unsplash/img/240x160/Boothbay.jpg
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 img/unsplash/img/240x160/Bryggen.jpg
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 img/unsplash/img/240x160/Futuristic.jpg
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 img/unsplash/img/240x160/Hokkaido.jpg
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 img/unsplash/img/240x160/Jewel Changi.jpg
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 img/unsplash/img/240x160/Lake Atitlan.jpg
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 img/unsplash/img/240x160/Louvre.jpg
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 img/unsplash/img/240x160/Rosetta Nebula.jpg
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 img/unsplash/img/240x160/Termas Geometricas.jpg
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 img/unsplash/img/240x160/Yosemite Tunnel View.jpg
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 img/unsplash/img/3840x2560/Akihabara.jpg
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 img/unsplash/img/3840x2560/Boothbay.jpg
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 img/unsplash/img/3840x2560/Bryggen.jpg
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 img/unsplash/img/3840x2560/Futuristic.jpg
Binary file added img/unsplash/img/3840x2560/Hokkaido.jpg
Binary file added img/unsplash/img/3840x2560/Jewel Changi.jpg
Binary file added img/unsplash/img/3840x2560/Lake Atitlan.jpg
Binary file added img/unsplash/img/3840x2560/Louvre.jpg
Binary file added img/unsplash/img/3840x2560/Rosetta Nebula.jpg
Binary file added img/unsplash/img/3840x2560/Termas Geometricas.jpg
Binary file added img/unsplash/img/480x320/Akihabara.jpg
Binary file added img/unsplash/img/480x320/Boothbay.jpg
Binary file added img/unsplash/img/480x320/Bryggen.jpg
Binary file added img/unsplash/img/480x320/Futuristic.jpg
Binary file added img/unsplash/img/480x320/Hokkaido.jpg
Binary file added img/unsplash/img/480x320/Jewel Changi.jpg
Binary file added img/unsplash/img/480x320/Lake Atitlan.jpg
Binary file added img/unsplash/img/480x320/Louvre.jpg
Binary file added img/unsplash/img/480x320/Rosetta Nebula.jpg
Binary file added img/unsplash/img/480x320/Termas Geometricas.jpg
Binary file added img/unsplash/img/480x320/Yosemite Tunnel View.jpg
Binary file added img/unsplash/img/960x640/Akihabara.jpg
Binary file added img/unsplash/img/960x640/Boothbay.jpg
Binary file added img/unsplash/img/960x640/Bryggen.jpg
Binary file added img/unsplash/img/960x640/Futuristic.jpg
Binary file added img/unsplash/img/960x640/Hokkaido.jpg
Binary file added img/unsplash/img/960x640/Jewel Changi.jpg
Binary file added img/unsplash/img/960x640/Lake Atitlan.jpg
Binary file added img/unsplash/img/960x640/Louvre.jpg
Binary file added img/unsplash/img/960x640/Rosetta Nebula.jpg
Binary file added img/unsplash/img/960x640/Termas Geometricas.jpg
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
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
157 changes: 86 additions & 71 deletions util/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![allow(dead_code)]

use std::{
cmp::Reverse,
ffi::OsStr,
fs,
path::{Path, PathBuf},
sync::OnceLock,
};
Expand All @@ -9,98 +12,110 @@ use image::RgbImage;
use palette::{IntoColor, Oklab};
use quantette::IndexedColorCounts;

pub fn load_images(images: &[PathBuf]) -> Vec<(String, RgbImage)> {
pub fn load_images<P>(images: &[P]) -> Vec<RgbImage>
where
P: AsRef<Path>,
{
images
.iter()
.map(|path| {
image::open(path).map(|image| {
(
path.file_name().unwrap().to_owned().into_string().unwrap(),
image.into_rgb8(),
)
})
})
.collect::<Result<_, _>>()
.unwrap()
.map(|path| image::open(path).unwrap().into_rgb8())
.collect()
}

pub fn load_image_dir(dir: impl AsRef<Path>) -> Vec<(String, RgbImage)> {
let mut paths = std::fs::read_dir(dir)
.unwrap()
.collect::<Result<Vec<_>, _>>()
pub fn load_image_dir(dir: impl AsRef<Path>) -> Vec<(PathBuf, RgbImage)> {
let mut paths = fs::read_dir(dir.as_ref())
.unwrap()
.iter()
.map(std::fs::DirEntry::path)
.map(|entry| entry.unwrap().path())
.collect::<Vec<_>>();

paths.sort();
paths.sort_unstable();

load_images(&paths)
}
let images = load_images(&paths);

fn generate_resolutions(images: Vec<(String, RgbImage)>) -> Vec<(String, RgbImage)> {
images
.into_iter()
.flat_map(|(path, image)| {
let mut images = Vec::new();
let (width, height) = image.dimensions();
let pixels = width * height;
let mut w = 240;
let mut h = 160;
let mut next_width = w * 2;
let mut next_height = h * 2;
while next_width * next_height < pixels {
let image = image::imageops::thumbnail(&image, w, h);
images.push((format!("{path}@{w}x{h}"), image));
w = next_width;
h = next_height;
next_width *= 2;
next_height *= 2;
}
images.push((format!("{path}@original"), image));
images
})
.collect()
paths.into_iter().zip(images).collect()
}

fn to_oklab_counts(
images: &[(String, RgbImage)],
) -> Vec<(String, IndexedColorCounts<Oklab, f32, 3>)> {
images
.iter()
.map(|(path, image)| {
(
path.clone(),
IndexedColorCounts::try_from_rgbimage_par(image, |srgb| {
srgb.into_linear().into_color()
})
.unwrap(),
)
})
.collect()
}

pub const CQ100_DIR: &str = "img/CQ100/img";
pub const UNSPLASH_DIR: &str = "img/unsplash/img";

pub fn load_image_dir_relative_to_root(dir: impl AsRef<Path>) -> Vec<(String, RgbImage)> {
// assume current exe path is something like: target/build/deps/current_exe
let exe = std::env::current_exe().unwrap();
let root = exe
fn root_dir() -> PathBuf {
// assume current exe path is something like: target/profile/dir/current_exe
std::env::current_exe()
.unwrap()
.parent()
.and_then(Path::parent)
.and_then(Path::parent)
.and_then(Path::parent)
.unwrap();
.unwrap()
.into()
}

load_image_dir(root.join(dir.as_ref()))
pub fn load_image_dir_relative_to_root(dir: impl AsRef<Path>) -> Vec<(PathBuf, RgbImage)> {
let mut root = root_dir();
root.push(dir.as_ref());
load_image_dir(root)
}

static BENCHMARK_IMAGES: OnceLock<Vec<(String, RgbImage)>> = OnceLock::new();

pub fn benchmark_images() -> &'static [(String, RgbImage)] {
BENCHMARK_IMAGES
.get_or_init(|| generate_resolutions(load_image_dir_relative_to_root(UNSPLASH_DIR)))
BENCHMARK_IMAGES.get_or_init(|| {
let images = {
let mut path = root_dir();
path.push("img");
path.push("unsplash");
path.push("img");
path
};

let mut images = fs::read_dir(images)
.unwrap()
.map(|dir| {
let dir = dir.unwrap().path();
let resolution = dir.file_stem().and_then(OsStr::to_str).unwrap();
let resolution = if let Some((width, height)) = resolution.split_once('x') {
(width.parse().unwrap(), height.parse().unwrap())
} else {
(u32::MAX, u32::MAX)
};
(resolution, dir)
})
.collect::<Vec<_>>();

images.sort_unstable_by_key(|&(res, _)| Reverse(res));

images
.into_iter()
.flat_map(|(_, dir)| load_image_dir(dir))
.map(|(path, img)| {
let name = path
.components()
.rev()
.take(2)
.collect::<Vec<_>>()
.into_iter()
.rev()
.collect::<PathBuf>()
.display()
.to_string();

(name, img)
})
.collect()
})
}

fn to_oklab_counts(
images: &[(String, RgbImage)],
) -> Vec<(String, IndexedColorCounts<Oklab, f32, 3>)> {
images
.iter()
.map(|(path, image)| {
let counts = IndexedColorCounts::try_from_rgbimage_par(image, |srgb| {
srgb.into_linear().into_color()
})
.unwrap();

(path.clone(), counts)
})
.collect()
}

static BENCHMARK_COUNTS: OnceLock<Vec<(String, IndexedColorCounts<Oklab, f32, 3>)>> =
Expand Down

0 comments on commit 9e3e7fc

Please sign in to comment.