~ruther/CTU-FEE-B0B35APO-Semestral-project

c2d2df67e9eab17c34d09ebf1449f0d1bad7f84f — František Boháček 3 years ago 83e710f
refactor: handle correctly get and set pixel out of bounds
2 files changed, 15 insertions(+), 0 deletions(-)

M image-viewer/src/display_utils.c
M image-viewer/src/image.c
M image-viewer/src/display_utils.c => image-viewer/src/display_utils.c +8 -0
@@ 102,10 102,18 @@ void display_clear(display_t *display, bool render) {
}

display_pixel_t display_get_pixel(display_t *display, uint16_t x, uint16_t y) {
  if (y >= DISPLAY_HEIGHT || x >= DISPLAY_WIDTH) {
    return BLACK_PIXEL;
  }

  return display->pixels[y * DISPLAY_WIDTH + x];
}

void display_set_pixel(display_t *display, uint16_t x, uint16_t y,
                       display_pixel_t pixel) {
  if (y >= DISPLAY_HEIGHT || x >= DISPLAY_WIDTH) {
    return;
  }

  display->pixels[y * DISPLAY_WIDTH + x] = pixel;
}

M image-viewer/src/image.c => image-viewer/src/image.c +7 -0
@@ 59,11 59,18 @@ bool image_region_move_within(image_region_t *to_move, direction_t direction,
}

display_pixel_t image_get_pixel(image_t *image, uint16_t x, uint16_t y) {
  if (y >= image->height || x >= image->width) {
    return BLACK_PIXEL;
  }
  return image->pixels[y * image->width + x];
}

void image_set_pixel(image_t *image, uint16_t x, uint16_t y,
                       display_pixel_t pixel) {
  if (y >= image->height || x >= image->width) {
    return;
  }

  image->pixels[y * image->width + x] = pixel;
}


Do not follow this link