@@ -9,16 +9,16 @@ pub use reducer::*;
9
9
10
10
pub trait Image : Sized {
11
11
/// Get the width of the image, in pixels
12
- fn width ( & self ) -> u32 ;
12
+ fn width ( & self ) -> u16 ;
13
13
14
14
/// Get the height of the image, in pixels
15
- fn height ( & self ) -> u32 ;
15
+ fn height ( & self ) -> u16 ;
16
16
17
17
/// Get the color at the given coordinates
18
- fn color_at ( & self , x : u32 , y : u32 ) -> Option < Color > ;
18
+ fn color_at ( & self , x : u16 , y : u16 ) -> Option < Color > ;
19
19
20
20
/// Get the color at the given coordinates skipping bound checks
21
- unsafe fn color_at_unchecked ( & self , x : u32 , y : u32 ) -> Color ;
21
+ unsafe fn color_at_unchecked ( & self , x : u16 , y : u16 ) -> Color ;
22
22
23
23
/// Convert this image trait object to a raw image
24
24
fn to_raw_image ( & self ) -> RawImage ;
@@ -53,12 +53,12 @@ pub enum RawImageError {
53
53
#[ derive( Clone ) ]
54
54
pub struct RawImage {
55
55
data : Vec < u8 > ,
56
- width : u32 ,
57
- height : u32 ,
56
+ width : u16 ,
57
+ height : u16 ,
58
58
}
59
59
60
60
impl RawImage {
61
- pub const CHANNELS : u32 = 3 ;
61
+ pub const CHANNELS : u16 = 3 ;
62
62
63
63
pub fn write_to_kitty ( & self , out : & mut dyn std:: io:: Write ) -> Result < ( ) , RawImageError > {
64
64
// Buffer for raw PNG data
@@ -68,8 +68,8 @@ impl RawImage {
68
68
// Write PNG to buffer
69
69
encoder. encode (
70
70
& self . data [ ..] ,
71
- self . width ,
72
- self . height ,
71
+ self . width as _ ,
72
+ self . height as _ ,
73
73
image:: ColorType :: Rgb8 ,
74
74
) ?;
75
75
// Encode to base64
@@ -105,45 +105,28 @@ impl RawImage {
105
105
}
106
106
107
107
impl Image for RawImage {
108
- fn width ( & self ) -> u32 {
108
+ fn width ( & self ) -> u16 {
109
109
self . width
110
110
}
111
111
112
- fn height ( & self ) -> u32 {
112
+ fn height ( & self ) -> u16 {
113
113
self . height
114
114
}
115
115
116
- fn color_at ( & self , x : u32 , y : u32 ) -> Option < Color > {
116
+ fn color_at ( & self , x : u16 , y : u16 ) -> Option < Color > {
117
117
if x < self . width && y < self . height {
118
- unsafe {
119
- Some ( Color :: new (
120
- * self
121
- . data
122
- . get_unchecked ( ( ( y * self . width + x) * Self :: CHANNELS ) as usize ) ,
123
- * self
124
- . data
125
- . get_unchecked ( ( ( y * self . width + x) * Self :: CHANNELS + 1 ) as usize ) ,
126
- * self
127
- . data
128
- . get_unchecked ( ( ( y * self . width + x) * Self :: CHANNELS + 2 ) as usize ) ,
129
- ) )
130
- }
118
+ unsafe { Some ( self . color_at_unchecked ( x, y) ) }
131
119
} else {
132
120
None
133
121
}
134
122
}
135
123
136
- unsafe fn color_at_unchecked ( & self , x : u32 , y : u32 ) -> Color {
124
+ unsafe fn color_at_unchecked ( & self , x : u16 , y : u16 ) -> Color {
125
+ let idx = ( y as usize * self . width as usize + x as usize ) * Self :: CHANNELS as usize ;
137
126
Color :: new (
138
- * self
139
- . data
140
- . get_unchecked ( ( ( y * self . width + x) * Self :: CHANNELS ) as usize ) ,
141
- * self
142
- . data
143
- . get_unchecked ( ( ( y * self . width + x) * Self :: CHANNELS + 1 ) as usize ) ,
144
- * self
145
- . data
146
- . get_unchecked ( ( ( y * self . width + x) * Self :: CHANNELS + 2 ) as usize ) ,
127
+ * self . data . get_unchecked ( idx) ,
128
+ * self . data . get_unchecked ( idx + 1 ) ,
129
+ * self . data . get_unchecked ( idx + 2 ) ,
147
130
)
148
131
}
149
132
@@ -173,59 +156,63 @@ impl TryFrom<(Vec<u8>, u32, u32)> for RawImage {
173
156
type Error = RawImageError ;
174
157
175
158
fn try_from ( ( data, width, height) : ( Vec < u8 > , u32 , u32 ) ) -> Result < Self , Self :: Error > {
176
- let expected = ( width * height * Self :: CHANNELS ) as usize ;
159
+ let expected = width as usize * height as usize * Self :: CHANNELS as usize ;
177
160
178
161
if data. len ( ) != expected {
179
162
return Err ( RawImageError :: InvalidData {
180
163
data : data. len ( ) ,
181
164
width,
182
165
height,
183
- channels : Self :: CHANNELS ,
166
+ channels : Self :: CHANNELS as _ ,
184
167
expected,
185
168
} ) ;
186
169
} else if width == 0 {
187
170
return Err ( RawImageError :: ZeroWidth ) ;
188
171
} else if height == 0 {
189
172
return Err ( RawImageError :: ZeroHeight ) ;
173
+ } else if width >= u16:: MAX as _ {
174
+ return Err ( RawImageError :: InvalidWidth ) ;
175
+ } else if height >= u16:: MAX as _ {
176
+ return Err ( RawImageError :: InvalidHeight ) ;
190
177
}
191
178
192
179
Ok ( Self {
193
180
data,
194
- width,
195
- height,
181
+ width : width as _ ,
182
+ height : height as _ ,
196
183
} )
197
184
}
198
185
}
199
186
200
187
pub struct ImageView < ' i , T : Image > {
201
188
inner : & ' i T ,
202
- xmin : u32 ,
203
- xmax : u32 ,
204
- ymin : u32 ,
205
- ymax : u32 ,
189
+ xmin : u16 ,
190
+ xmax : u16 ,
191
+ ymin : u16 ,
192
+ ymax : u16 ,
206
193
}
207
194
208
195
impl < ' i , T : Image > Image for ImageView < ' i , T > {
209
- fn width ( & self ) -> u32 {
196
+ fn width ( & self ) -> u16 {
210
197
self . xmax - self . xmin
211
198
}
212
199
213
- fn height ( & self ) -> u32 {
200
+ fn height ( & self ) -> u16 {
214
201
self . ymax - self . ymin
215
202
}
216
203
217
- fn color_at ( & self , x : u32 , y : u32 ) -> Option < Color > {
204
+ fn color_at ( & self , x : u16 , y : u16 ) -> Option < Color > {
218
205
self . inner . color_at ( x + self . xmin , y + self . ymin )
219
206
}
220
207
221
- unsafe fn color_at_unchecked ( & self , x : u32 , y : u32 ) -> Color {
208
+ unsafe fn color_at_unchecked ( & self , x : u16 , y : u16 ) -> Color {
222
209
self . inner . color_at_unchecked ( x + self . xmin , y + self . ymin )
223
210
}
224
211
225
212
fn to_raw_image ( & self ) -> RawImage {
226
213
let w = self . width ( ) ;
227
214
let h = self . height ( ) ;
228
- let mut data = Vec :: with_capacity ( ( w * h * RawImage :: CHANNELS ) as usize ) ;
215
+ let mut data = Vec :: with_capacity ( w as usize * h as usize * RawImage :: CHANNELS as usize ) ;
229
216
230
217
unsafe {
231
218
for y in 0 ..h {
@@ -240,18 +227,18 @@ impl<'i, T: Image> Image for ImageView<'i, T> {
240
227
241
228
RawImage {
242
229
data,
243
- width : w as _ ,
244
- height : h as _ ,
230
+ width : w,
231
+ height : h,
245
232
}
246
233
}
247
234
}
248
235
249
236
pub trait ImageViewExt : Image {
250
- fn wrap ( & self , x : std:: ops:: Range < u32 > , y : std:: ops:: Range < u32 > ) -> ImageView < Self > ;
237
+ fn wrap ( & self , x : std:: ops:: Range < u16 > , y : std:: ops:: Range < u16 > ) -> ImageView < Self > ;
251
238
}
252
239
253
240
impl < T : Image > ImageViewExt for T {
254
- fn wrap ( & self , x : std:: ops:: Range < u32 > , y : std:: ops:: Range < u32 > ) -> ImageView < Self > {
241
+ fn wrap ( & self , x : std:: ops:: Range < u16 > , y : std:: ops:: Range < u16 > ) -> ImageView < Self > {
255
242
ImageView {
256
243
inner : self ,
257
244
xmin : x. start ,
0 commit comments