use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use serde::{Deserialize, Serialize};
use crate::client::Bibby;
use crate::error::Result;
const PRODUCT: &str = "images";
#[derive(Debug, Clone, Copy)]
pub struct ImagesClient<'a> {
client: &'a Bibby,
}
impl<'a> ImagesClient<'a> {
pub(crate) fn new(client: &'a Bibby) -> Self {
Self { client }
}
pub async fn info(&self, image: ImageSource) -> Result<ImageInfo> {
#[derive(Serialize)]
struct Body {
image: ImageSource,
}
self.client
.post_json(PRODUCT, "images/info", &Body { image })
.await
}
pub async fn transform(&self, request: &TransformRequest) -> Result<EncodedImage> {
self.client
.post_json(PRODUCT, "images/transform", request)
.await
}
pub async fn remove_background(
&self,
request: &RemoveBackgroundRequest,
) -> Result<EncodedImage> {
self.client
.post_json(PRODUCT, "images/remove-background", request)
.await
}
pub async fn vectorize(&self, request: &VectorizeRequest) -> Result<VectorImage> {
self.client
.post_json(PRODUCT, "images/vectorize", request)
.await
}
pub async fn analyze(&self, request: &AnalyzeRequest) -> Result<ImageAnalysis> {
self.client
.post_json(PRODUCT, "images/analyze", request)
.await
}
pub async fn prepare(&self, request: &PrepareRequest) -> Result<EncodedImage> {
self.client
.post_json(PRODUCT, "images/prepare", request)
.await
}
pub async fn icons(&self, request: &IconsRequest) -> Result<IconSet> {
self.client
.post_json(PRODUCT, "images/icons", request)
.await
}
pub async fn compare(&self, request: &CompareRequest) -> Result<Comparison> {
self.client
.post_json(PRODUCT, "images/compare", request)
.await
}
pub async fn read_qr_codes(&self, image: ImageSource) -> Result<QrCodes> {
#[derive(Serialize)]
struct Body {
image: ImageSource,
}
self.client
.post_json(PRODUCT, "qr-codes/read", &Body { image })
.await
}
pub async fn generate_qr_code(&self, request: &GenerateQrCodeRequest) -> Result<EncodedImage> {
self.client
.post_json(PRODUCT, "qr-codes/generate", request)
.await
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ImageSource {
Base64 {
base64: String,
},
Url {
url: String,
},
}
impl ImageSource {
pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Self {
Self::Base64 {
base64: STANDARD.encode(bytes.as_ref()),
}
}
pub fn from_base64(base64: impl Into<String>) -> Self {
Self::Base64 {
base64: base64.into(),
}
}
pub fn from_url(url: impl Into<String>) -> Self {
Self::Url { url: url.into() }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ImageFormat {
Png,
#[serde(alias = "jpg")]
Jpeg,
Webp,
Gif,
Bmp,
Tiff,
Svg,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Fit {
Contain,
Cover,
Fill,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum FlipDirection {
Horizontal,
Vertical,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum Operation {
Resize {
#[serde(skip_serializing_if = "Option::is_none")]
width: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
height: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
fit: Option<Fit>,
},
Crop {
x: u32,
y: u32,
width: u32,
height: u32,
},
Rotate {
degrees: i32,
},
Flip {
direction: FlipDirection,
},
Grayscale,
Invert,
Blur {
sigma: f64,
},
Sharpen {
#[serde(skip_serializing_if = "Option::is_none")]
sigma: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
threshold: Option<u8>,
},
Brightness {
amount: i32,
},
Contrast {
amount: f64,
},
Hue {
degrees: i32,
},
Trim {
#[serde(skip_serializing_if = "Option::is_none")]
tolerance: Option<u8>,
},
Pad {
#[serde(skip_serializing_if = "Option::is_none")]
top: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
right: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
bottom: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
left: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
color: Option<String>,
},
Background {
color: String,
},
Pixelate {
block: u32,
#[serde(skip_serializing_if = "Option::is_none")]
x: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
y: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
width: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
height: Option<u32>,
},
}
#[derive(Debug, Clone, Serialize)]
pub struct TransformRequest {
pub image: ImageSource,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub operations: Vec<Operation>,
#[serde(skip_serializing_if = "Option::is_none")]
pub format: Option<ImageFormat>,
#[serde(skip_serializing_if = "Option::is_none")]
pub quality: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_bytes: Option<usize>,
}
impl TransformRequest {
pub fn new(image: ImageSource) -> Self {
Self {
image,
operations: Vec::new(),
format: None,
quality: None,
max_bytes: None,
}
}
pub fn operations(mut self, operations: Vec<Operation>) -> Self {
self.operations = operations;
self
}
pub fn op(mut self, operation: Operation) -> Self {
self.operations.push(operation);
self
}
pub fn format(mut self, format: ImageFormat) -> Self {
self.format = Some(format);
self
}
pub fn quality(mut self, quality: u8) -> Self {
self.quality = Some(quality);
self
}
pub fn max_bytes(mut self, max_bytes: usize) -> Self {
self.max_bytes = Some(max_bytes);
self
}
}
#[derive(Debug, Clone, Serialize)]
pub struct RemoveBackgroundRequest {
pub image: ImageSource,
#[serde(skip_serializing_if = "Option::is_none")]
pub background: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub format: Option<ImageFormat>,
}
impl RemoveBackgroundRequest {
pub fn new(image: ImageSource) -> Self {
Self {
image,
background: None,
format: None,
}
}
pub fn background(mut self, hex: impl Into<String>) -> Self {
self.background = Some(hex.into());
self
}
pub fn format(mut self, format: ImageFormat) -> Self {
self.format = Some(format);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum VectorStyle {
Poster,
Photo,
LineArt,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum VectorCurves {
Smooth,
Polygon,
Pixel,
}
#[derive(Debug, Clone, Serialize)]
pub struct VectorizeRequest {
pub image: ImageSource,
#[serde(skip_serializing_if = "Option::is_none")]
pub style: Option<VectorStyle>,
#[serde(skip_serializing_if = "Option::is_none")]
pub curves: Option<VectorCurves>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_colors: Option<u32>,
}
impl VectorizeRequest {
pub fn new(image: ImageSource) -> Self {
Self {
image,
style: None,
curves: None,
max_colors: None,
}
}
pub fn style(mut self, style: VectorStyle) -> Self {
self.style = Some(style);
self
}
pub fn curves(mut self, curves: VectorCurves) -> Self {
self.curves = Some(curves);
self
}
pub fn max_colors(mut self, max_colors: u32) -> Self {
self.max_colors = Some(max_colors);
self
}
}
#[derive(Debug, Clone, Serialize)]
pub struct AnalyzeRequest {
pub image: ImageSource,
#[serde(skip_serializing_if = "Option::is_none")]
pub colors: Option<u32>,
}
impl AnalyzeRequest {
pub fn new(image: ImageSource) -> Self {
Self {
image,
colors: None,
}
}
pub fn colors(mut self, colors: u32) -> Self {
self.colors = Some(colors);
self
}
}
#[derive(Debug, Clone, Serialize)]
pub struct PrepareRequest {
pub image: ImageSource,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_side: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_pixels: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub format: Option<ImageFormat>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_bytes: Option<usize>,
}
impl PrepareRequest {
pub fn new(image: ImageSource) -> Self {
Self {
image,
max_side: None,
max_pixels: None,
format: None,
max_bytes: None,
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct IconsRequest {
pub image: ImageSource,
#[serde(skip_serializing_if = "Option::is_none")]
pub background: Option<String>,
}
impl IconsRequest {
pub fn new(image: ImageSource) -> Self {
Self {
image,
background: None,
}
}
pub fn background(mut self, hex: impl Into<String>) -> Self {
self.background = Some(hex.into());
self
}
}
#[derive(Debug, Clone, Serialize)]
pub struct CompareRequest {
pub first: ImageSource,
pub second: ImageSource,
#[serde(skip_serializing_if = "Option::is_none")]
pub tolerance: Option<u8>,
}
impl CompareRequest {
pub fn new(first: ImageSource, second: ImageSource) -> Self {
Self {
first,
second,
tolerance: None,
}
}
pub fn tolerance(mut self, tolerance: u8) -> Self {
self.tolerance = Some(tolerance);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ErrorCorrection {
Low,
Medium,
Quartile,
High,
}
#[derive(Debug, Clone, Serialize)]
pub struct GenerateQrCodeRequest {
pub text: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub margin: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error_correction: Option<ErrorCorrection>,
#[serde(skip_serializing_if = "Option::is_none")]
pub dark: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub light: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub format: Option<ImageFormat>,
}
impl GenerateQrCodeRequest {
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
size: None,
margin: None,
error_correction: None,
dark: None,
light: None,
format: None,
}
}
pub fn size(mut self, size: u32) -> Self {
self.size = Some(size);
self
}
pub fn margin(mut self, margin: u32) -> Self {
self.margin = Some(margin);
self
}
pub fn error_correction(mut self, level: ErrorCorrection) -> Self {
self.error_correction = Some(level);
self
}
pub fn dark(mut self, hex: impl Into<String>) -> Self {
self.dark = Some(hex.into());
self
}
pub fn light(mut self, hex: impl Into<String>) -> Self {
self.light = Some(hex.into());
self
}
pub fn format(mut self, format: ImageFormat) -> Self {
self.format = Some(format);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EncodedImage {
pub format: String,
pub mime_type: String,
pub width: u32,
pub height: u32,
pub size_bytes: u64,
pub data: String,
}
impl EncodedImage {
pub fn decode_bytes(&self) -> Result<Vec<u8>> {
Ok(STANDARD.decode(&self.data)?)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImageInfo {
pub format: String,
pub mime_type: String,
pub width: u32,
pub height: u32,
pub has_alpha: bool,
pub color: String,
pub size_bytes: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct VectorImage {
pub mime_type: String,
pub width: u32,
pub height: u32,
pub paths: u64,
pub size_bytes: u64,
pub svg: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PaletteColor {
pub hex: String,
pub share: f64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ImageAnalysis {
pub width: u32,
pub height: u32,
pub transparent: bool,
pub average_color: Option<String>,
#[serde(default)]
pub palette: Vec<PaletteColor>,
pub blurhash: String,
pub thumbhash: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IconFile {
pub name: String,
pub mime_type: String,
pub size: u32,
pub size_bytes: u64,
pub data: String,
}
impl IconFile {
pub fn decode_bytes(&self) -> Result<Vec<u8>> {
Ok(STANDARD.decode(&self.data)?)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IconSet {
pub files: Vec<IconFile>,
pub html: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Dimensions {
pub width: u32,
pub height: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChangedArea {
pub x: u32,
pub y: u32,
pub width: u32,
pub height: u32,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PixelDiff {
pub changed_pixels: u64,
pub changed_share: f64,
pub changed_area: Option<ChangedArea>,
pub diff: EncodedImage,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Comparison {
pub identical: bool,
pub similarity: f64,
pub same_picture: bool,
pub first: Dimensions,
pub second: Dimensions,
pub pixels: Option<PixelDiff>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct QrCorner {
pub x: i32,
pub y: i32,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct QrCode {
pub text: String,
#[serde(default)]
pub corners: Vec<QrCorner>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct QrCodes {
#[serde(default)]
pub codes: Vec<QrCode>,
}