1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use crate::coord::Coord;
use crate::drawable::Dimensions;
use crate::drawable::Drawable;
use crate::pixelcolor::PixelColor;
use crate::transform::Transform;
use crate::unsignedcoord::{ToSigned, UnsignedCoord};
use core::marker::PhantomData;
pub trait ImageType {}
#[derive(Debug)]
pub struct Image<'a, C, T>
where
C: PixelColor,
T: ImageType,
{
pub(crate) width: u32,
pub(crate) height: u32,
pub(crate) imagedata: &'a [u8],
pub offset: Coord,
pixel_type: PhantomData<C>,
image_type: PhantomData<T>,
}
impl<'a, C, T> Image<'a, C, T>
where
C: PixelColor,
T: ImageType,
{
pub fn new(imagedata: &'a [u8], width: u32, height: u32) -> Self {
Self {
width,
height,
imagedata,
offset: Coord::new(0, 0),
pixel_type: PhantomData,
image_type: PhantomData,
}
}
}
impl<'a, C, T> Dimensions for Image<'a, C, T>
where
C: PixelColor,
T: ImageType,
{
fn top_left(&self) -> Coord {
self.offset
}
fn bottom_right(&self) -> Coord {
self.top_left() + self.size().to_signed()
}
fn size(&self) -> UnsignedCoord {
let height = self.height;
let width = self.width;
UnsignedCoord::new(width, height)
}
}
impl<'a, C, T> Drawable for Image<'a, C, T>
where
C: PixelColor,
T: ImageType,
{
}
impl<'a, C, T> Transform for Image<'a, C, T>
where
C: PixelColor,
T: ImageType,
{
fn translate(&self, by: Coord) -> Self {
Self {
offset: self.offset + by,
..*self.clone()
}
}
fn translate_mut(&mut self, by: Coord) -> &mut Self {
self.offset += by;
self
}
}
#[derive(Debug)]
pub struct ImageIterator<'a, C: 'a, T>
where
C: PixelColor,
T: ImageType,
{
pub(crate) x: u32,
pub(crate) y: u32,
pub(crate) im: &'a Image<'a, C, T>,
}
impl<'a, C, T> ImageIterator<'a, C, T>
where
C: PixelColor,
T: ImageType,
{
pub fn new(image: &'a Image<'a, C, T>) -> Self {
ImageIterator {
im: image,
x: 0,
y: 0,
}
}
}
pub trait ImageFile<'a>: Dimensions + Sized {
fn new(filedata: &'a [u8]) -> Result<Self, ()>;
fn width(&self) -> u32;
fn height(&self) -> u32;
}