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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
// ok to copy/adapt
/*
* header contains the image header for referencing sizes
* x is my x coordinate from the left
* y is my y coordinate from the bottom
* c is my color coordinate in order BGR
* x, y, and c are zero indexed
* returns the index within BMPImage.data
*/
int _index_bmp_image(const BMPHeader* header, int x, int y, int c) {
int bytes_per_pixel = header->bits_per_pixel / 8;
int bytes_per_row_no_padding = header->width_px * bytes_per_pixel;
int padding = 4 - (bytes_per_row_no_padding % 4);
if (padding == 4) {
padding = 0;
}
int bytes_per_row = bytes_per_row_no_padding + padding;
// !! y here starts from the bottom of the image !!
// it might be easier to start y from the top, because that is what crop_bmp assumes
int start_of_row = bytes_per_row * y;
int start_of_pixel = start_of_row + bytes_per_pixel * x;
int start_of_color = start_of_pixel + c;
return start_of_color;
}
uint8_t _get_image_color(const BMPImage* image, int x, int y, int c) {
return image->data[_index_bmp_image(&(image->header), x, y, c)];
}
void _set_image_color(const BMPImage* image, int x, int y, int c, uint8_t new_color) {
image->data[_index_bmp_image(&(image->header), x, y, c)] = new_color;
}
// !! important: y starts from the top for this parameter !!
BMPImage* crop_bmp(const BMPImage* image, int x, int y, int w, int h, const char** a_error) {
// validate the header
BMPHeader new_header = image->header;
// modify new_header to set the sizes
// !! make sure the new header is valid !!
// validate new header with your check_bmp_header
BMPImage* new_image = malloc(sizeof(*image));
new_image->header = new_header;
new_image->data = malloc(sizeof((*new_image->data) * new_header.image_size_bytes));
// copy over cropped data
// _index_bmp_image(&(new_image->header), ...)
// for each y in new image
// for each x in new image
// for each color in new image
// copy pixel to new_image from image, can use _get_image_color and _set_image_color
// !! important: coordinates in the two images may not be the same !!
// can offset them using x and y parameters
int main(int argc, char* argv[]) {
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2024 Alexander J. Quinn & David Burnett This content is protected and may not be shared, uploaded, or distributed.