Skip to content

Commit

Permalink
LIBSAIL-MANIP: Merge grayscale functions (up to bpp8) into one
Browse files Browse the repository at this point in the history
  • Loading branch information
HappySeaFox committed Nov 9, 2023
1 parent 1ed10d1 commit 882c10e
Showing 1 changed file with 18 additions and 64 deletions.
82 changes: 18 additions & 64 deletions src/sail-manip/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ static sail_status_t convert_from_bpp8_indexed(const struct sail_image *image, p
return SAIL_OK;
}

static sail_status_t convert_from_bpp1_grayscale(const struct sail_image *image, pixel_consumer_t pixel_consumer, const struct output_context *output_context) {
static sail_status_t convert_from_grayscale_up_to_bpp8(const struct sail_image *image,
const unsigned input_bit_shift, const unsigned bit_shift_decrease_by,
const unsigned input_bit_mask, const unsigned bit_mask_shift_by,
const unsigned multiplicator_to_255,
pixel_consumer_t pixel_consumer, const struct output_context *output_context) {

unsigned row;

Expand All @@ -249,19 +253,19 @@ static sail_status_t convert_from_bpp1_grayscale(const struct sail_image *image,
const uint8_t *scan_input = (uint8_t *)image->pixels + image->bytes_per_line * row;

for (unsigned column = 0; column < image->width;) {
unsigned bit_shift = 7;
unsigned bit_mask = 1 << 7;
unsigned bit_shift = input_bit_shift;
unsigned bit_mask = input_bit_mask;
const uint8_t byte = *scan_input++;

while (bit_mask > 0 && column < image->width) {
const uint8_t index = (byte & bit_mask) >> bit_shift;

sail_rgba32_t rgba32;
spread_gray8_to_rgba32(index == 0 ? 0 : 255, &rgba32);
spread_gray8_to_rgba32((uint8_t)(index * multiplicator_to_255), &rgba32);
pixel_consumer(output_context, row, column, &rgba32, NULL);

bit_shift--;
bit_mask >>= 1;
bit_shift -= bit_shift_decrease_by;
bit_mask >>= bit_mask_shift_by;
column++;
}
}
Expand All @@ -270,80 +274,30 @@ static sail_status_t convert_from_bpp1_grayscale(const struct sail_image *image,
return SAIL_OK;
}

static sail_status_t convert_from_bpp2_grayscale(const struct sail_image *image, pixel_consumer_t pixel_consumer, const struct output_context *output_context) {

unsigned row;

#pragma omp parallel for
for (row = 0; row < image->height; row++) {
const uint8_t *scan_input = (uint8_t *)image->pixels + image->bytes_per_line * row;
static sail_status_t convert_from_bpp1_grayscale(const struct sail_image *image, pixel_consumer_t pixel_consumer, const struct output_context *output_context) {

for (unsigned column = 0; column < image->width;) {
unsigned bit_shift = 6;
unsigned bit_mask = 3 << 6; /* 11000000 */
const uint8_t byte = *scan_input++;
SAIL_TRY(convert_from_grayscale_up_to_bpp8(image, 7, 1, /* 10000000 */ 0x80, 1, 255, pixel_consumer, output_context));

while (bit_mask > 0 && column < image->width) {
const uint8_t index = (byte & bit_mask) >> bit_shift;
return SAIL_OK;
}

sail_rgba32_t rgba32;
spread_gray8_to_rgba32(index * 85, &rgba32);
pixel_consumer(output_context, row, column, &rgba32, NULL);
static sail_status_t convert_from_bpp2_grayscale(const struct sail_image *image, pixel_consumer_t pixel_consumer, const struct output_context *output_context) {

bit_shift -= 2;
bit_mask >>= 2;
column++;
}
}
}
SAIL_TRY(convert_from_grayscale_up_to_bpp8(image, 6, 2, /* 11000000 */ 0xC0, 2, 85, pixel_consumer, output_context));

return SAIL_OK;
}

static sail_status_t convert_from_bpp4_grayscale(const struct sail_image *image, pixel_consumer_t pixel_consumer, const struct output_context *output_context) {

unsigned row;

#pragma omp parallel for
for (row = 0; row < image->height; row++) {
const uint8_t *scan_input = (uint8_t *)image->pixels + image->bytes_per_line * row;

for (unsigned column = 0; column < image->width;) {
unsigned bit_shift = 4;
unsigned bit_mask = 15 << 4; /* 11110000 */
const uint8_t byte = *scan_input++;

while (bit_mask > 0 && column < image->width) {
const uint8_t index = (byte & bit_mask) >> bit_shift;

sail_rgba32_t rgba32;
spread_gray8_to_rgba32(index * 17, &rgba32);
pixel_consumer(output_context, row, column, &rgba32, NULL);

bit_shift -= 4;
bit_mask >>= 4;
column++;
}
}
}
SAIL_TRY(convert_from_grayscale_up_to_bpp8(image, 4, 4, /* 11110000 */ 0xF0, 4, 17, pixel_consumer, output_context));

return SAIL_OK;
}

static sail_status_t convert_from_bpp8_grayscale(const struct sail_image *image, pixel_consumer_t pixel_consumer, const struct output_context *output_context) {

unsigned row;

#pragma omp parallel for
for (row = 0; row < image->height; row++) {
const uint8_t *scan_input = (uint8_t *)image->pixels + image->bytes_per_line * row;

for (unsigned column = 0; column < image->width; column++) {
sail_rgba32_t rgba32;
spread_gray8_to_rgba32(*scan_input++, &rgba32);
pixel_consumer(output_context, row, column, &rgba32, NULL);
}
}
SAIL_TRY(convert_from_grayscale_up_to_bpp8(image, 0, 0, /* 11111111 */ 0xFF, 8, 1, pixel_consumer, output_context));

return SAIL_OK;
}
Expand Down

0 comments on commit 882c10e

Please sign in to comment.