pic_scale/ar30.rs
1/*
2 * Copyright (c) Radzivon Bartoshyk. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29#![forbid(unsafe_code)]
30
31#[derive(Debug, Copy, Clone, PartialEq, Eq)]
32pub(crate) enum Rgb30 {
33 Ar30 = 0,
34 Ra30 = 1,
35}
36
37impl From<usize> for Rgb30 {
38 fn from(value: usize) -> Self {
39 match value {
40 0 => Rgb30::Ar30,
41 1 => Rgb30::Ra30,
42 _ => {
43 unimplemented!("Rgb30 is not implemented for value {}", value)
44 }
45 }
46 }
47}
48
49/// Converts a value from host byte order to network byte order.
50#[inline]
51const fn htonl(hostlong: u32) -> u32 {
52 hostlong.to_be()
53}
54
55/// Converts a value from network byte order to host byte order.
56#[inline]
57const fn ntohl(netlong: u32) -> u32 {
58 u32::from_be(netlong)
59}
60
61impl Rgb30 {
62 // #[inline]
63 // pub(crate) const fn pack_w_a<const STORE: usize>(self, r: i32, g: i32, b: i32, a: i32) -> u32 {
64 // let value: u32 = match self {
65 // Rgb30::Ar30 => (((a << 30) | (b << 20)) | ((g << 10) | r)) as u32,
66 // Rgb30::Ra30 => (((r << 22) | (g << 12)) | ((b << 2) | a)) as u32,
67 // };
68 // if STORE == 0 {
69 // value
70 // } else {
71 // htonl(value)
72 // }
73 // }
74
75 #[inline]
76 pub(crate) const fn pack_w_a<const STORE: usize>(self, r: i32, g: i32, b: i32, _: i32) -> u32 {
77 let value: u32 = match self {
78 Rgb30::Ar30 => ((3u32 << 30u32) | ((b as u32) << 20)) | (((g as u32) << 10) | r as u32),
79 Rgb30::Ra30 => (((r as u32) << 22) | ((g as u32) << 12)) | (((b as u32) << 2) | 3),
80 };
81 if STORE == 0 { value } else { htonl(value) }
82 }
83
84 #[inline(always)]
85 pub(crate) const fn unpack<const STORE: usize>(self, value: u32) -> (u32, u32, u32, u32) {
86 let pixel = if STORE == 0 { value } else { ntohl(value) };
87 match self {
88 Rgb30::Ar30 => {
89 let r10 = pixel & 0x3ff;
90 let g10 = (pixel >> 10) & 0x3ff;
91 let b10 = (pixel >> 20) & 0x3ff;
92 // let a10 = pixel >> 30;
93 (r10, g10, b10, 3)
94 }
95 Rgb30::Ra30 => {
96 // let a2 = pixel & 0x3;
97 let r10 = (pixel >> 22) & 0x3ff;
98 let g10 = (pixel >> 12) & 0x3ff;
99 let b10 = (pixel >> 2) & 0x3ff;
100 (r10, g10, b10, 3)
101 }
102 }
103 }
104}
105
106#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
107/// Defines storage byte order for RGBA1010102 or RGBA2101010
108///
109/// Some systems require to be bytes in network byte order instead of host.
110pub enum Ar30ByteOrder {
111 Host = 0,
112 Network = 1,
113}
114
115impl From<usize> for Ar30ByteOrder {
116 fn from(value: usize) -> Self {
117 match value {
118 0 => Ar30ByteOrder::Host,
119 1 => Ar30ByteOrder::Network,
120 _ => {
121 unimplemented!("Rgb30ByteOrder is not implemented for value {}", value)
122 }
123 }
124 }
125}