Skip to content

Commit

Permalink
COMMON: Fix mirroring horizontally
Browse files Browse the repository at this point in the history
  • Loading branch information
HappySeaFox committed May 15, 2024
1 parent 156310f commit e1fbce3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/sail-common/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,22 @@ sail_status_t sail_mirror(struct sail_image *image, enum SailOrientation orienta
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;
const unsigned bits_per_pixel = sail_bits_per_pixel(image->pixel_format);

if (bits_per_pixel % 8 != 0) {
SAIL_LOG_ERROR("Only byte-aligned pixels are supported for the horizontal mirroring");
SAIL_LOG_AND_RETURN(SAIL_ERROR_UNSUPPORTED_PIXEL_FORMAT);
}

const unsigned bytes_per_pixel = bits_per_pixel / 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 col1 = 0, col2 = image->width - bytes_per_pixel; col1 < col2; col1 += bytes_per_pixel, col2 -= bytes_per_pixel) {
for (unsigned col1 = 0, col2 = (image->width - 1) * 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);
Expand Down
6 changes: 4 additions & 2 deletions src/sail-common/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ SAIL_EXPORT sail_status_t sail_check_image_valid(const struct sail_image *image)
SAIL_EXPORT sail_status_t sail_mirror_vertically(struct sail_image *image);

/*
* Mirrors the image horizontally.
* Mirrors the image horizontally. The image pixel size must be a multiple of 8,
* e.g. 8, 16, 24 etc.
*
* Returns SAIL_OK on success.
*/
Expand All @@ -226,7 +227,8 @@ 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.
* values are accepted. When mirroring horizontally, the image pixel size must be a multiple of 8,
* e.g. 8, 16, 24 etc.
*
* Returns SAIL_OK on success.
*/
Expand Down

0 comments on commit e1fbce3

Please sign in to comment.