Lab 6. Processing images

This solutions show only the main function. the other functions are the same as in the provided source code

Task 2. Cyan / Pink

int main() {

  int in[W][H][C];
  int out[W][H][C];
  int width, height;
  
  read(in, width, height);
  
  // filter
  for(int i = 0; i<width; i++){
    for(int j = 0; j<height; j++) {
      int r = in[i][j][0];
      int g = in[i][j][1];
      int b = in[i][j][2];
    
      if ( i < width/2 ) {
        out[i][j][0] = 0;
        out[i][j][1] = g;
        out[i][j][2] = b;
      }
      else {
        out[i][j][0] = r;
        out[i][j][1] = 0;
        out[i][j][2] = b;
      }

    }
  }

  write(out, width, height);

}

Task 3. Top-right corner

int main() {

  int in[W][H][C];
  int out[W][H][C];
  int width, height;
  
  read(in, width, height);
  
  int w = width / 2;
  int h = height / 2;

  // filter
  for(int i = 0; i < w; i++){
    for(int j = 0; j < h; j++) {
      // x-coordinate in the original image
      int i_orig = width - w + i;
      int r = in[i_orig][j][0];
      int g = in[i_orig][j][1];
      int b = in[i_orig][j][2];

      out[i][j][0] = r;
      out[i][j][1] = g;
      out[i][j][2] = b;
    }
  }

  write(out, w, h);

}

Task 4. Scaling by the factor of two

int main() {

  int in[W][H][C];
  int out[W][H][C];
  int width, height;
  
  read(in, width, height);
  
  int w = width * 2;
  int h = height * 2;

  // filter
  for(int i = 0; i < width; i++){
    for(int j = 0; j < height; j++) {
      // pixel (i,j) of the original image
      //  must be copied at 4 coordinates of the output array:
      //    (2i, 2j),    (2i + 1, j)
      //    (2i, 2j+1),  (2i + 1, 2j + 1)
          
      for(int ii = 0; ii < 2; ii++) {
        for(int jj = 0; jj < 2; jj++) {

          // copy 3 color components, red, green, and blue
          for(int k = 0 ; k < C; k++)
            out[2*i + ii][2*j + jj][k] = in[i][j][k];

        }
      }

    }
  }

  write(out, w, h);

}