Skip to content

Commit bce1bea

Browse files
authored
No hardcoded image dimensions in twix (HULKs#2204)
* add dimensions function to JpegImage * Do not hardcode image dimensions in twix
1 parent b14956e commit bce1bea

1 file changed

Lines changed: 35 additions & 30 deletions

File tree

  • tools/twix/src/panels/image

tools/twix/src/panels/image/mod.rs

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ use std::{env::temp_dir, fs::create_dir_all, path::PathBuf, sync::Arc};
22

33
use chrono::{DateTime, Utc};
44
use color_eyre::{
5-
eyre::{bail, eyre},
5+
eyre::{bail, eyre, Context as _},
66
Result,
77
};
8-
use coordinate_systems::Pixel;
9-
use eframe::egui::{ColorImage, Response, SizeHint, TextureOptions, Ui, UiBuilder, Widget};
8+
use eframe::egui::{
9+
ColorImage, Context, Response, SizeHint, TextureId, TextureOptions, Ui, Widget,
10+
};
1011
use geometry::rectangle::Rectangle;
1112
use linear_algebra::{point, vector};
1213
use log::{info, warn};
@@ -143,19 +144,28 @@ impl Widget for &mut ImagePanel {
143144
}
144145
}
145146
});
147+
148+
let (texture_id, (width, height)) = match self.load_latest_texture(ui.ctx()) {
149+
Ok(result) => result,
150+
Err(error) => {
151+
return ui.scope(|ui| ui.label(format!("{error}"))).response;
152+
}
153+
};
154+
146155
let (response, mut painter) = TwixPainter::allocate(
147156
ui,
148-
vector![640.0, 480.0],
157+
vector![width as f32, height as f32],
149158
point![0.0, 0.0],
150159
Orientation::LeftHanded,
151160
);
152161
self.zoom_and_pan.apply(ui, &mut painter, &response);
153-
154-
if let Err(error) = self.show_image(&painter) {
155-
ui.scope_builder(UiBuilder::new().max_rect(response.rect), |ui| {
156-
ui.label(format!("{error}"))
157-
});
158-
};
162+
painter.image(
163+
texture_id,
164+
Rectangle {
165+
min: point!(0.0, 0.0),
166+
max: point!(width as f32, height as f32),
167+
},
168+
);
159169

160170
self.overlays.paint(&painter);
161171

@@ -178,11 +188,9 @@ impl ImagePanel {
178188
self.image_buffer = subscribe_image(&self.nao, jpeg, self.show_depth_image);
179189
}
180190

181-
fn show_image(&self, painter: &TwixPainter<Pixel>) -> Result<()> {
182-
let context = painter.context();
183-
191+
fn load_latest_texture(&self, context: &Context) -> Result<(TextureId, (u32, u32))> {
184192
let image_identifier = "bytes://image-vision".to_string();
185-
let image = match &self.image_buffer {
193+
match &self.image_buffer {
186194
RawOrJpeg::Raw(buffer) => {
187195
let ros_image = buffer
188196
.get_last_value()?
@@ -198,38 +206,35 @@ impl ImagePanel {
198206
[ros_image.width as usize, ros_image.height as usize],
199207
&ros_image.data,
200208
);
201-
context
209+
let id = context
202210
.load_texture(&image_identifier, image, TextureOptions::NEAREST)
203-
.id()
211+
.id();
212+
213+
Ok((id, (ros_image.width, ros_image.height)))
204214
}
205215
RawOrJpeg::Jpeg(buffer) => {
206216
let jpeg = buffer
207217
.get_last_value()?
208218
.ok_or_else(|| eyre!("no image available"))?;
219+
let (width, height) = jpeg
220+
.dimensions()
221+
.wrap_err("failed to read image dimensions")?;
209222
context.forget_image(&image_identifier);
210223
context.include_bytes(image_identifier.clone(), jpeg.data);
211-
context
224+
let id = context
212225
.try_load_texture(
213226
&image_identifier,
214227
TextureOptions::NEAREST,
215228
SizeHint::Size {
216-
width: 640,
217-
height: 480,
229+
width,
230+
height,
218231
maintain_aspect_ratio: true,
219232
},
220233
)?
221234
.texture_id()
222-
.unwrap()
235+
.unwrap();
236+
Ok((id, (width, height)))
223237
}
224-
};
225-
226-
painter.image(
227-
image,
228-
Rectangle {
229-
min: point!(0.0, 0.0),
230-
max: point!(640.0, 480.0),
231-
},
232-
);
233-
Ok(())
238+
}
234239
}
235240
}

0 commit comments

Comments
 (0)