Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Dec 1, 2023
1 parent 28626ed commit bd5f891
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 80 deletions.
6 changes: 3 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::*;
use crate::{ffi, Intent};
use std::cell::UnsafeCell;
use std::collections::HashMap;
use std::ffi::CStr;
Expand Down Expand Up @@ -220,7 +220,7 @@ impl ThreadContext {
/// Sets a function to be called if there is an error.
pub fn set_error_logging_function(&mut self, handler: ffi::LogErrorHandlerFunction) {
unsafe {
ffi::cmsSetLogErrorHandlerTHR(self.handle, handler)
ffi::cmsSetLogErrorHandlerTHR(self.handle, handler);
}
}
}
Expand Down Expand Up @@ -271,7 +271,7 @@ fn context() {
let mut c = ThreadContext::new();
assert!(c.user_data().is_null());
c.unregister_plugins();
assert!(Profile::new_icc_context(&c, &[]).is_err());
assert!(crate::Profile::new_icc_context(&c, &[]).is_err());

assert!(c.supported_intents().contains_key(&Intent::RelativeColorimetric));

Expand Down
4 changes: 2 additions & 2 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl FloatOrU16 for f32 {

#[inline]
unsafe fn eval_pipeline(handle: *const ffi::Pipeline, input: &[Self], out: &mut [Self]) {
ffi::cmsPipelineEvalFloat(input.as_ptr(), out.as_mut_ptr(), handle)
ffi::cmsPipelineEvalFloat(input.as_ptr(), out.as_mut_ptr(), handle);
}

#[inline]
Expand All @@ -31,7 +31,7 @@ impl FloatOrU16 for u16 {

#[inline]
unsafe fn eval_pipeline(handle: *const ffi::Pipeline, input: &[Self], out: &mut [Self]) {
ffi::cmsPipelineEval16(input.as_ptr(), out.as_mut_ptr(), handle)
ffi::cmsPipelineEval16(input.as_ptr(), out.as_mut_ptr(), handle);
}

#[inline]
Expand Down
66 changes: 40 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,77 @@
//! The main types you need to use in this crate are `Profile` and `Transform`
#![doc(html_logo_url = "https://kornelski.github.io/rust-lcms2/lcms_logo.png")]
#![doc(html_root_url = "https://kornelski.github.io/rust-lcms2")]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::doc_markdown)]
#![allow(clippy::enum_glob_use)]
#![allow(clippy::if_not_else)]
#![allow(clippy::map_unwrap_or)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::missing_panics_doc)]
#![allow(clippy::missing_safety_doc)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::redundant_closure_for_method_calls)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::unreadable_literal)]
#![allow(clippy::upper_case_acronyms)]
#![allow(clippy::wildcard_imports)]

extern crate lcms2_sys as ffi;
use lcms2_sys as ffi;

#[macro_use]
extern crate foreign_types;

mod profile;
mod tag;
mod ciecam;
mod context;
mod mlu;
mod namedcolorlist;
mod pipeline;
mod error;
mod eval;
mod ext;
mod flags;
mod locale;
mod mlu;
mod namedcolorlist;
mod pipeline;
mod profile;
mod stage;
mod transform;
mod tag;
mod tonecurve;
mod error;
mod transform;
use std::marker::PhantomData;

/// `Transform` requires pixel types to implement these traits.
///
/// This is necesary to prevent unsafe writes to abitrary types with pointers or padding.
pub use bytemuck::{Pod, Zeroable};

pub use crate::profile::*;
pub use crate::error::*;
pub use crate::ciecam::*;
pub use crate::context::{GlobalContext, ThreadContext};
pub use crate::mlu::*;
pub use crate::error::*;
pub use crate::ext::*;
pub use crate::flags::*;
pub use crate::locale::*;
pub use crate::mlu::*;
pub use crate::namedcolorlist::*;
pub use crate::pipeline::*;
pub use crate::profile::*;
pub use crate::stage::*;
pub use crate::transform::*;
pub use crate::tonecurve::*;
pub use crate::namedcolorlist::*;
pub use crate::transform::*;

pub use crate::ffi::CIEXYZ;
pub use crate::ffi::CIELab;
#[doc(hidden)]
pub use crate::ffi::CIExyYTRIPLE;
#[doc(hidden)]
/// Part of [`CIExyYTRIPLE`]
pub use crate::ffi::CIExyY;
/// For [`Profile::new_rgb`]
pub use crate::ffi::CIExyYTRIPLE;
#[doc(hidden)]
pub use crate::ffi::JCh;
pub use crate::ffi::CIEXYZ;

pub use crate::ffi::PixelFormat;
pub use crate::ffi::ColorSpaceSignature;
pub use crate::ffi::InfoType;
pub use crate::ffi::TagSignature;
pub use crate::ffi::Intent;
pub use crate::ffi::ColorSpaceSignature;
pub use crate::ffi::PixelFormat;
pub use crate::ffi::ProfileClassSignature;
pub use crate::ffi::ViewingConditions;
pub use crate::ffi::TagSignature;
pub use crate::ffi::VideoSignalType;
pub use crate::ffi::ViewingConditions;

#[derive(Debug)]
#[non_exhaustive]
Expand Down Expand Up @@ -113,19 +125,21 @@ pub fn version() -> u32 {
}

#[allow(non_snake_case)]
#[must_use]
pub fn xyY2XYZ(xyY: &CIExyY) -> CIEXYZ {
let mut xyz = CIEXYZ::default();
unsafe {
crate::ffi::cmsxyY2XYZ(&mut xyz, xyY)
crate::ffi::cmsxyY2XYZ(&mut xyz, xyY);
}
xyz
}

#[allow(non_snake_case)]
#[must_use]
pub fn XYZ2xyY(xyz: &CIEXYZ) -> CIExyY {
let mut xyY = CIExyY::default();
unsafe {
crate::ffi::cmsXYZ2xyY(&mut xyY, xyz)
crate::ffi::cmsXYZ2xyY(&mut xyY, xyz);
}
xyY
}
12 changes: 6 additions & 6 deletions src/mlu.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ffi::wchar_t;
use crate::*;
use foreign_types::{ForeignType, ForeignTypeRef};
use crate::{ffi, Error, LCMSResult, Locale};
use foreign_types::{foreign_type, ForeignType, ForeignTypeRef};
use std::char::{decode_utf16, REPLACEMENT_CHARACTER};
use std::ffi::CString;
use std::fmt;
Expand Down Expand Up @@ -36,7 +36,7 @@ impl MLURef {
let Ok(cstr) = CString::new(text) else { return false };
unsafe {
ffi::cmsMLUsetASCII(
self as *mut _ as *mut _,
(self as *mut Self).cast(),
locale.language_ptr(),
locale.country_ptr(),
cstr.as_ptr(),
Expand All @@ -54,7 +54,7 @@ impl MLURef {

unsafe {
ffi::cmsMLUsetWide(
self as *mut _ as *mut _,
(self as *mut Self).cast(),
locale.language_ptr(),
locale.country_ptr(),
chars[..].as_ptr(),
Expand Down Expand Up @@ -82,7 +82,7 @@ impl MLURef {
self.as_ptr(),
locale.language_ptr(),
locale.country_ptr(),
buf[..].as_mut_ptr() as *mut _,
buf[..].as_mut_ptr().cast(),
len,
);
if let Some(0) = buf.pop() { // terminating zero
Expand Down Expand Up @@ -114,7 +114,7 @@ impl MLURef {
self.as_ptr(),
locale.language_ptr(),
locale.country_ptr(),
buf[..].as_mut_ptr() as *mut wchar_t,
buf[..].as_mut_ptr().cast::<wchar_t>(),
len_bytes,
);
if let Some(0) = buf.pop() { // terminating zero
Expand Down
9 changes: 6 additions & 3 deletions src/namedcolorlist.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::*;
use foreign_types::ForeignTypeRef;
use crate::{ffi, Error, LCMSResult};
use foreign_types::{foreign_type, ForeignTypeRef};
use std::ffi::{CStr, CString};
use std::fmt;
use std::os::raw::c_char;
Expand Down Expand Up @@ -47,12 +47,14 @@ impl NamedColorListRef {
}

/// Find color by name
#[must_use]
pub fn index_of(&self, color_name: &str) -> usize {
let s = CString::new(color_name).unwrap();
unsafe { ffi::cmsNamedColorIndex(self.as_ptr(), s.as_ptr()) as usize }
unsafe { ffi::cmsNamedColorIndex(self.as_ptr(), s.as_ptr()).try_into().unwrap() }
}

/// Get color info
#[must_use]
pub fn get(&self, index: usize) -> Option<NamedColorInfo> {
let mut name = [0 as c_char; 256];
let mut prefix = [0 as c_char; 33];
Expand Down Expand Up @@ -84,6 +86,7 @@ impl NamedColorListRef {
}
}

#[must_use]
pub fn colors(&self) -> Vec<NamedColorInfo> {
(0..self.len()).filter_map(|i| self.get(i)).collect()
}
Expand Down
8 changes: 4 additions & 4 deletions src/pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::eval::FloatOrU16;
use crate::stage::{StageRef, StagesIter};
use crate::*;
use foreign_types::ForeignTypeRef;
use crate::{ffi, Error, LCMSResult};
use foreign_types::{foreign_type, ForeignTypeRef};
use std::fmt;
use std::ptr;

Expand Down Expand Up @@ -35,7 +35,7 @@ impl PipelineRef {
if append.input_channels() != self.output_channels() {
return false;
}
unsafe { ffi::cmsPipelineCat(self as *mut _ as *mut _, append.as_ptr()) != 0 }
unsafe { ffi::cmsPipelineCat((self as *mut Self).cast(), append.as_ptr()) != 0 }
}

#[must_use]
Expand Down Expand Up @@ -73,7 +73,7 @@ impl PipelineRef {
}

pub fn set_8bit(&mut self, on: bool) -> bool {
unsafe { ffi::cmsPipelineSetSaveAs8bitsFlag(self as *mut _ as *mut _, i32::from(on)) != 0 }
unsafe { ffi::cmsPipelineSetSaveAs8bitsFlag((self as *mut Self).cast(), i32::from(on)) != 0 }
}

#[must_use]
Expand Down
33 changes: 14 additions & 19 deletions src/profile.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::*;
use crate::context::Context;
use crate::*;
use foreign_types::ForeignTypeRef;
use std::default::Default;
use std::fmt;
use std::fs::File;
use std::io::Read;
use std::io;
use std::io::Read;
use std::mem::MaybeUninit;
use std::os::raw::c_void;
use std::path::Path;
Expand Down Expand Up @@ -275,7 +275,8 @@ impl<Ctx: Context> Profile<Ctx> {
unsafe { ffi::cmsSetPCS(self.handle, pcs) }
}

#[must_use] pub fn info(&self, info: InfoType, locale: Locale) -> Option<String> {
#[must_use]
pub fn info(&self, info: InfoType, locale: Locale) -> Option<String> {
let size = unsafe {
ffi::cmsGetProfileInfo(self.handle,
info,
Expand Down Expand Up @@ -337,7 +338,7 @@ impl<Ctx: Context> Profile<Ctx> {
#[must_use]
pub fn detect_black_point(&self, intent: Intent) -> Option<CIEXYZ> {
unsafe {
let mut b = Default::default();
let mut b = CIEXYZ::default();
if ffi::cmsDetectBlackPoint(&mut b, self.handle, intent, 0) != 0 {
Some(b)
} else {
Expand All @@ -350,7 +351,7 @@ impl<Ctx: Context> Profile<Ctx> {
#[must_use]
pub fn detect_destination_black_point(&self, intent: Intent) -> Option<CIEXYZ> {
unsafe {
let mut b = Default::default();
let mut b = CIEXYZ::default();
if ffi::cmsDetectDestinationBlackPoint(&mut b, self.handle, intent, 0) != 0 {
Some(b)
} else {
Expand Down Expand Up @@ -412,23 +413,17 @@ impl<Ctx: Context> Profile<Ctx> {

#[inline]
pub fn write_tag(&mut self, sig: TagSignature, tag: Tag<'_>) -> bool {
unsafe {
ffi::cmsWriteTag(self.handle, sig, tag.data_for_signature(sig).cast()) != 0
}
unsafe { ffi::cmsWriteTag(self.handle, sig, tag.data_for_signature(sig).cast()) != 0 }
}

#[inline]
pub fn remove_tag(&mut self, sig: TagSignature) -> bool {
unsafe {
ffi::cmsWriteTag(self.handle, sig, std::ptr::null()) != 0
}
unsafe { ffi::cmsWriteTag(self.handle, sig, std::ptr::null()) != 0 }
}

#[inline]
pub fn link_tag(&mut self, sig: TagSignature, dst: TagSignature) -> bool {
unsafe {
ffi::cmsLinkTag(self.handle, sig, dst) != 0
}
unsafe { ffi::cmsLinkTag(self.handle, sig, dst) != 0 }
}

/// Retrieves the Profile ID stored in the profile header.
Expand All @@ -454,7 +449,7 @@ impl<Ctx: Context> Profile<Ctx> {
#[inline]
pub fn set_profile_id(&mut self, id: ffi::ProfileID) {
unsafe {
ffi::cmsSetHeaderProfileID(self.handle, &id as *const ffi::ProfileID as *mut _);
ffi::cmsSetHeaderProfileID(self.handle, std::ptr::addr_of!(id) as *mut _);
}
}

Expand Down Expand Up @@ -499,9 +494,9 @@ impl<Ctx: Context> Profile<Ctx> {
ffi::cmsCreateRGBProfileTHR(context.as_ref().as_ptr(),
white_point,
primaries,
[transfer_function[0].as_ptr() as *const _,
transfer_function[1].as_ptr() as *const _,
transfer_function[2].as_ptr() as *const _]
[transfer_function[0].as_ptr().cast_const(),
transfer_function[1].as_ptr().cast_const(),
transfer_function[2].as_ptr().cast_const()]
.as_ptr())
})
}
Expand All @@ -515,7 +510,7 @@ impl<Ctx: Context> Profile<Ctx> {
/// Number of tone curves must be sufficient for the color space.
#[inline]
pub unsafe fn new_linearization_device_link_context(context: impl AsRef<Ctx>, color_space: ColorSpaceSignature, curves: &[ToneCurveRef]) -> LCMSResult<Self> {
let v: Vec<_> = curves.iter().map(|c| c.as_ptr() as *const _).collect();
let v: Vec<_> = curves.iter().map(|c| c.as_ptr().cast_const()).collect();
Self::new_handle(ffi::cmsCreateLinearizationDeviceLinkTHR(context.as_ref().as_ptr(), color_space, v.as_ptr()))
}

Expand Down
6 changes: 3 additions & 3 deletions src/stage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::context::Context;
use crate::eval::FloatOrU16;
use crate::*;
use foreign_types::ForeignTypeRef;
use crate::{ffi, Error, GlobalContext, LCMSResult, ToneCurveRef};
use foreign_types::{foreign_type, ForeignTypeRef};
use std::fmt;
use std::ptr;

Expand Down Expand Up @@ -32,7 +32,7 @@ impl Stage {

/// Creates a stage that contains nChannels tone curves, one per channel.
pub fn new_tone_curves(curves: &[&ToneCurveRef]) -> LCMSResult<Stage> {
let ptrs: Vec<_> = curves.iter().map(|c| c.as_ptr() as *const _).collect();
let ptrs: Vec<_> = curves.iter().map(|c| c.as_ptr().cast_const()).collect();
unsafe {
Error::if_null(ffi::cmsStageAllocToneCurves(
GlobalContext::new().as_ptr(),
Expand Down
Loading

0 comments on commit bd5f891

Please sign in to comment.