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
30#[derive(Debug, Copy, Clone, PartialEq, Eq)]
31pub(crate) enum Rgb30 {
32 Ar30 = 0,
33 Ra30 = 1,
34}
35
36impl From<usize> for Rgb30 {
37 fn from(value: usize) -> Self {
38 match value {
39 0 => Rgb30::Ar30,
40 1 => Rgb30::Ra30,
41 _ => {
42 unimplemented!("Rgb30 is not implemented for value {}", value)
43 }
44 }
45 }
46}
47
48/// Converts a value from host byte order to network byte order.
49#[inline]
50const fn htonl(hostlong: u32) -> u32 {
51 hostlong.to_be()
52}
53
54/// Converts a value from network byte order to host byte order.
55#[inline]
56const fn ntohl(netlong: u32) -> u32 {
57 u32::from_be(netlong)
58}
59
60impl Rgb30 {
61 // #[inline]
62 // pub(crate) const fn pack_w_a<const STORE: usize>(self, r: i32, g: i32, b: i32, a: i32) -> u32 {
63 // let value: u32 = match self {
64 // Rgb30::Ar30 => (((a << 30) | (b << 20)) | ((g << 10) | r)) as u32,
65 // Rgb30::Ra30 => (((r << 22) | (g << 12)) | ((b << 2) | a)) as u32,
66 // };
67 // if STORE == 0 {
68 // value
69 // } else {
70 // htonl(value)
71 // }
72 // }
73
74 #[inline]
75 pub(crate) const fn pack_w_a<const STORE: usize>(self, r: i32, g: i32, b: i32, _: i32) -> u32 {
76 let value: u32 = match self {
77 Rgb30::Ar30 => ((3u32 << 30u32) | ((b as u32) << 20)) | (((g as u32) << 10) | r as u32),
78 Rgb30::Ra30 => (((r as u32) << 22) | ((g as u32) << 12)) | (((b as u32) << 2) | 3),
79 };
80 if STORE == 0 {
81 value
82 } else {
83 htonl(value)
84 }
85 }
86
87 #[inline(always)]
88 pub(crate) const fn unpack<const STORE: usize>(self, value: u32) -> (u32, u32, u32, u32) {
89 let pixel = if STORE == 0 { value } else { ntohl(value) };
90 match self {
91 Rgb30::Ar30 => {
92 let r10 = pixel & 0x3ff;
93 let g10 = (pixel >> 10) & 0x3ff;
94 let b10 = (pixel >> 20) & 0x3ff;
95 // let a10 = pixel >> 30;
96 (r10, g10, b10, 3)
97 }
98 Rgb30::Ra30 => {
99 // let a2 = pixel & 0x3;
100 let r10 = (pixel >> 22) & 0x3ff;
101 let g10 = (pixel >> 12) & 0x3ff;
102 let b10 = (pixel >> 2) & 0x3ff;
103 (r10, g10, b10, 3)
104 }
105 }
106 }
107}
108
109#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
110/// Defines storage byte order for RGBA1010102 or RGBA2101010
111///
112/// Some systems require to be bytes in network byte order instead of host.
113pub enum Ar30ByteOrder {
114 Host = 0,
115 Network = 1,
116}
117
118impl From<usize> for Ar30ByteOrder {
119 fn from(value: usize) -> Self {
120 match value {
121 0 => Ar30ByteOrder::Host,
122 1 => Ar30ByteOrder::Network,
123 _ => {
124 unimplemented!("Rgb30ByteOrder is not implemented for value {}", value)
125 }
126 }
127 }
128}