Advertisement
kolbka_

Untitled

Feb 18th, 2022
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. void image::cropp(uint32_t x,
  2.                   uint32_t y,
  3.                   uint32_t new_width_in_pixels,
  4.                   uint32_t new_height_in_pixels) {
  5.     auto bytes_in_pixel = bmp_info_header.bit_count / 8;
  6.     std::vector<uint8_t> data_new;
  7.     auto byte_count_per_row = bmp_info_header.width * bytes_in_pixel;
  8.     auto new_padding_bytes =
  9.         (4 - ((new_width_in_pixels * bytes_in_pixel) % 4)) % 4;
  10.     auto new_size_data =
  11.         (bytes_in_pixel * new_width_in_pixels + new_padding_bytes) *
  12.         new_height_in_pixels * bytes_in_pixel;
  13.     data_new.resize(new_size_data);
  14.     auto cur_bytes = 0;
  15.     auto new_width_image_in_bytes = bytes_in_pixel * new_width_in_pixels;
  16.     for (auto cur_line = static_cast<int32_t>(new_height_in_pixels);
  17.          cur_line >= 0; cur_line--) {
  18.         uint32_t cur_x_position =
  19.             (bmp_info_header.height - y - cur_line) * byte_count_per_row +
  20.             bytes_in_pixel * x;
  21.         for (uint32_t i = cur_x_position;
  22.              i < cur_x_position + new_width_image_in_bytes; i++, cur_bytes++) {
  23.             data_new[cur_bytes] = data[i];
  24.         }
  25.         for (uint32_t i = 0; i < new_padding_bytes; i++) {
  26.             data_new[cur_bytes + i] = 0;
  27.             cur_bytes++;
  28.         }
  29.  
  30.     }
  31.     data.clear();
  32.     data = data_new;
  33.     bmp_info_header.height = static_cast<int>(new_height_in_pixels);
  34.     bmp_info_header.width = static_cast<int>(new_width_in_pixels);
  35.     image::write("out1.bmp");
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement