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
use opbasics::*;

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct OpGoFloat {
  pub width: usize,
  pub height: usize,
  pub x: usize,
  pub y: usize,
  pub cpp: usize,
  pub is_cfa: bool,
}

impl OpGoFloat {
  pub fn new(img: &RawImage) -> OpGoFloat {
    // Calculate the resulting width/height and top-left corner after crops
    OpGoFloat{
      width: img.width - img.crops[1] - img.crops[3],
      height: img.height - img.crops[0] - img.crops[2],
      x: img.crops[3],
      y: img.crops[0],
      cpp: img.cpp,
      is_cfa: img.cfa.is_valid(),
    }
  }
}

impl<'a> ImageOp<'a> for OpGoFloat {
  fn name(&self) -> &str {"gofloat"}
  fn run(&self, pipeline: &PipelineGlobals, _buf: Arc<OpBuffer>) -> Arc<OpBuffer> {
    let img = &pipeline.image;
    let x = self.x;
    let y = self.y;

    Arc::new(match img.data {
      RawImageData::Integer(ref data) => {
        if self.cpp == 1 && !self.is_cfa {
          // We're in a monochrome image so turn it into RGB
          let mut out = OpBuffer::new(self.width, self.height, 4);
          out.mutate_lines(&(|line: &mut [f32], row| {
            for (o, i) in line.chunks_mut(4).zip(data[img.width*(row+y)+x..].chunks(1)) {
              o[0] = i[0] as f32;
              o[1] = i[0] as f32;
              o[2] = i[0] as f32;
              o[3] = 0.0;
            }
          }));
          out
        } else if self.cpp == 3 {
          // We're in an RGB image, turn it into four channel
          let mut out = OpBuffer::new(self.width, self.height, 4);
          out.mutate_lines(&(|line: &mut [f32], row| {
            for (o, i) in line.chunks_mut(4).zip(data[(img.width*(row+y)+x)*3..].chunks(3)) {
              o[0] = i[0] as f32;
              o[1] = i[1] as f32;
              o[2] = i[2] as f32;
              o[3] = 0.0;
            }
          }));
          out
        } else {
          let mut out = OpBuffer::new(self.width, self.height, img.cpp);
          out.mutate_lines(&(|line: &mut [f32], row| {
            for (o, i) in line.chunks_mut(1).zip(data[img.width*(row+y)+x..].chunks(1)) {
              o[0] = i[0] as f32;
            }
          }));
          out
        }
      },
      RawImageData::Float(ref data) => {
        if self.cpp == 1 && !self.is_cfa {
          // We're in a monochrome image so turn it into RGB
          let mut out = OpBuffer::new(self.width, self.height, 4);
          out.mutate_lines(&(|line: &mut [f32], row| {
            for (o, i) in line.chunks_mut(4).zip(data[img.width*(row+y)+x..].chunks(1)) {
              o[0] = i[0];
              o[1] = i[0];
              o[2] = i[0];
              o[3] = 0.0;
            }
          }));
          out
        } else if self.cpp == 3 {
          // We're in an RGB image, turn it into four channel
          let mut out = OpBuffer::new(self.width, self.height, 4);
          out.mutate_lines(&(|line: &mut [f32], row| {
            for (o, i) in line.chunks_mut(4).zip(data[(img.width*(row+y)+x)*3..].chunks(3)) {
              o[0] = i[0];
              o[1] = i[1];
              o[2] = i[2];
              o[3] = 0.0;
            }
          }));
          out
        } else {
          let mut out = OpBuffer::new(self.width, self.height, img.cpp);
          out.mutate_lines(&(|line: &mut [f32], row| {
            for (o, i) in line.chunks_mut(1).zip(data[img.width*(row+y)+x..].chunks(1)) {
              o[0] = i[0];
            }
          }));
          out
        }
      },
    })
  }
}