+use std::f32::consts::PI;
use std::convert::From;
use std::env;
use std::io::Cursor;
use std::fmt;
use std::borrow::BorrowMut;
use std::sync::{Arc, Mutex};
-use log::{debug, warn, LevelFilter};
+use log::{debug, LevelFilter};
use num_traits::Zero;
use num_traits::cast::AsPrimitive;
use percent_encoding::percent_decode_str;
Ok(response_image!(cursor))
}
-#[get("/gen/4/{scale}")]
-async fn img_gen4(req: HttpRequest, path: web::Path<u32>) -> Result<impl Responder> {
- let scale = path.into_inner();
- let signal = &mut extract_header!(req, "user-agent").bytes();
- let mut resimg: RgbImage = ImageBuffer::new(1600, 600);
- let mut x: f32 = 0.0;
+#[get("/gen/4/{dim_x}/{dim_y}/{scale}/{wave_offset_x}/{wave_offset_y}/{amplitude}/{freq}/{run}/{wave_color}/{data}")]
+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() - 1) as f32;
- let run: f32 = 20.0;
- let amplitude: f32 = 200.0;
- let wave_offset: f32 = (resimg.height() / 2) as f32 + amplitude / 2.0 - 1.0;
- let freq_base: f32 = 50.0;
- while x < max_x {
+ 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);
- warn!("Next char: {:>1} {:#02X}", s as char, s);
+ //debug!("Next char: {:>1} {:#02X}", s as char, s);
while ((x - px) < run) && (x < max_x) {
let y = if s != 0 {
- (x / (freq_base / s as f32)).sin() * amplitude + wave_offset
+ (2.0 * PI * freq_base * x * s as f32).sin() * amplitude + wave_offset_y
}
- else { wave_offset };
+ else { wave_offset_y };
x += 0.001;
- debug!("{x}, {y}");
- resimg.put_pixel(x as u32, y as u32, Rgb([255, 255, 255]));
- resimg.put_pixel(x as u32, s.into(), Rgb([255, 0, 0]));
+ //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 cursor = image_to_cursor(resimg, scale)?;