Ok(img)
}
+fn sine_encode(mut img: RgbImage, wave_offset: (u32, u32), amplitude: f32, freq_base: f32, run: u32, wave_color: Rgb<u8>,
+ data: &mut dyn Iterator<Item = u8>) -> Result<RgbImage, ImageError>
+{
+ let run: f32 = run as f32;
+ let (wave_offset_x, wave_offset_y) = wave_offset;
+ let (wave_offset_x, wave_offset_y) = (wave_offset_x as f32, wave_offset_y as f32);
+ let signal = &mut data.peekable();
+ let mut x: f32 = wave_offset_x as f32;
+ let max_x: f32 = (img.width() - 1) as f32;
+ let max_y: f32 = img.height() as f32;
+ while signal.peek().is_some() && (x < max_x) {
+ let px = x;
+ let s = signal.next().unwrap_or(0);
+ while ((x - px) < run) && (x < max_x) {
+ let y = if s != 0 {
+ (2.0 * PI * freq_base * x * s as f32).sin() * amplitude + wave_offset_y
+ }
+ else { wave_offset_y };
+ x += 0.001;
+ if (y > 0.0) && (y < max_y) {
+ img.put_pixel(x as u32, y as u32, wave_color);
+ }
+ }
+ }
+ Ok(img)
+}
+
fn apply_to_canvas<'a, F>(f: F, canvas_option: &'a mut Option<Canvas>, dimensions: Option<(u32, u32)>, data: &str) -> Result<&'a mut Canvas, CanvasError>
where F: Fn(Canvas, &mut dyn Iterator<Item = u8>) -> Result<Canvas, CanvasError>
{
}
#[get("/gen/4/{dim_x}/{dim_y}/{scale}/{wave_offset_x}/{wave_offset_y}/{amplitude}/{freq}/{run}/{wave_color}/{data}")]
+#[allow(clippy::type_complexity)]
async fn img_gen4(req: HttpRequest, path: web::Path<(u32, u32, u32, u32, u32, f32, f32, u32)>) -> Result<impl Responder> {
let (dim_x, dim_y, scale, wave_offset_x, wave_offset_y, amplitude, freq_base, run) = path.into_inner();
let freq_base = freq_base / 1000.0;
- let run: f32 = run as f32;
- let wave_offset_y: f32 = wave_offset_y as f32;
let wave_color = extract_data!(req, 11);
debug!("Wave color input: {:?}", wave_color);
let wave_color = ToRgbIter::new(&mut percent_decode_str(wave_color)).next().unwrap_or(Rgb([255, 255, 255]));
debug!("Decoded color: {:?}", wave_color);
- let signal = &mut percent_decode_str(extract_data!(req, 12)).peekable();
- let mut resimg: RgbImage = ImageBuffer::new(dim_x, dim_y);
- let mut x: f32 = wave_offset_x as f32;
- let max_x: f32 = (resimg.width() - 1) as f32;
- let max_y: f32 = resimg.height() as f32;
- while signal.peek().is_some() && (x < max_x) {
- let px = x;
- let s = signal.next().unwrap_or(0);
- //debug!("Next char: {:>1} {:#02X}", s as char, s);
- while ((x - px) < run) && (x < max_x) {
- let y = if s != 0 {
- (2.0 * PI * freq_base * x * s as f32).sin() * amplitude + wave_offset_y
- }
- else { wave_offset_y };
- x += 0.001;
- //debug!("{x}, {y}");
- if (y > 0.0) && (y < max_y) {
- resimg.put_pixel(x as u32, y as u32, wave_color);
- }
- //resimg.put_pixel(x as u32, s.into(), Rgb([255, 0, 0]));
- }
- }
+ let signal = &mut percent_decode_str(extract_data!(req, 12));
+ let resimg = sine_encode(ImageBuffer::new(dim_x, dim_y), (wave_offset_x, wave_offset_y), amplitude, freq_base, run, wave_color, signal)?;
let cursor = image_to_cursor(resimg, scale)?;
Ok(response_image!(cursor))
}