Skip to content

Commit

Permalink
COMMON: Added sail_mirror()
Browse files Browse the repository at this point in the history
  • Loading branch information
HappySeaFox committed May 14, 2024
1 parent 7b78dd5 commit 156310f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
63 changes: 41 additions & 22 deletions src/sail-common/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,43 +175,62 @@ sail_status_t sail_check_image_valid(const struct sail_image *image)

sail_status_t sail_mirror_vertically(struct sail_image *image) {

SAIL_TRY(sail_check_image_valid(image));
SAIL_TRY(sail_mirror(image, SAIL_ORIENTATION_MIRRORED_VERTICALLY));

void *line;
SAIL_TRY(sail_malloc(image->bytes_per_line, &line));
return SAIL_OK;
}

for (unsigned row1 = 0, row2 = image->height - 1; row1 < row2; row1++, row2--) {
memcpy(line, sail_scan_line(image, row1), image->bytes_per_line);
memcpy(sail_scan_line(image, row1), sail_scan_line(image, row2), image->bytes_per_line);
memcpy(sail_scan_line(image, row2), line, image->bytes_per_line);
}
sail_status_t sail_mirror_horizontally(struct sail_image *image) {

sail_free(line);
SAIL_TRY(sail_mirror(image, SAIL_ORIENTATION_MIRRORED_HORIZONTALLY));

return SAIL_OK;
}

sail_status_t sail_mirror_horizontally(struct sail_image *image) {
sail_status_t sail_mirror(struct sail_image *image, enum SailOrientation orientation)
{
switch (orientation) {
case SAIL_ORIENTATION_MIRRORED_VERTICALLY: {
SAIL_TRY(sail_check_image_valid(image));

SAIL_TRY(sail_check_image_valid(image));
void *line;
SAIL_TRY(sail_malloc(image->bytes_per_line, &line));

const unsigned bytes_per_pixel = (sail_bits_per_pixel(image->pixel_format) + 7) / 8;
for (unsigned row1 = 0, row2 = image->height - 1; row1 < row2; row1++, row2--) {
memcpy(line, sail_scan_line(image, row1), image->bytes_per_line);
memcpy(sail_scan_line(image, row1), sail_scan_line(image, row2), image->bytes_per_line);
memcpy(sail_scan_line(image, row2), line, image->bytes_per_line);
}

void *pixel;
SAIL_TRY(sail_malloc(bytes_per_pixel, &pixel));
sail_free(line);
break;
}
case SAIL_ORIENTATION_MIRRORED_HORIZONTALLY: {
SAIL_TRY(sail_check_image_valid(image));

const unsigned bytes_per_pixel = (sail_bits_per_pixel(image->pixel_format) + 7) / 8;

void *pixel;
SAIL_TRY(sail_malloc(bytes_per_pixel, &pixel));

for (unsigned row = 0; row < image->height; row++) {
unsigned char *scan = sail_scan_line(image, row);
for (unsigned row = 0; row < image->height; row++) {
unsigned char *scan = sail_scan_line(image, row);

for (unsigned col1 = 0, col2 = image->width - bytes_per_pixel; col1 < col2; col1 += bytes_per_pixel, col2 -= bytes_per_pixel) {
memcpy(pixel, scan + col1, bytes_per_pixel);
memcpy(scan + col1, scan + col2, bytes_per_pixel);
memcpy(scan + col2, pixel, bytes_per_pixel);
for (unsigned col1 = 0, col2 = image->width - bytes_per_pixel; col1 < col2; col1 += bytes_per_pixel, col2 -= bytes_per_pixel) {
memcpy(pixel, scan + col1, bytes_per_pixel);
memcpy(scan + col1, scan + col2, bytes_per_pixel);
memcpy(scan + col2, pixel, bytes_per_pixel);
}
}

sail_free(pixel);
break;
}
default: {
SAIL_LOG_AND_RETURN(SAIL_ERROR_INVALID_ARGUMENT);
}
}

sail_free(pixel);

return SAIL_OK;
}

Expand Down
10 changes: 10 additions & 0 deletions src/sail-common/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ SAIL_EXPORT sail_status_t sail_mirror_vertically(struct sail_image *image);
*/
SAIL_EXPORT sail_status_t sail_mirror_horizontally(struct sail_image *image);

/*
* Mirrors the image horizontally or vertically.
*
* Only SAIL_ORIENTATION_MIRRORED_HORIZONTALLY and SAIL_ORIENTATION_MIRRORED_VERTICALLY
* values are accepted.
*
* Returns SAIL_OK on success.
*/
SAIL_EXPORT sail_status_t sail_mirror(struct sail_image *image, enum SailOrientation orientation);

/*
* Returns the scan line at the given row.
* Return NULL if the image or its pixels is NULL.
Expand Down

0 comments on commit 156310f

Please sign in to comment.