Skip to content

Commit

Permalink
SAIL: Use sail_scan_line() more
Browse files Browse the repository at this point in the history
  • Loading branch information
HappySeaFox committed Nov 9, 2023
1 parent 5ae64c3 commit 1dfab50
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/sail-codecs/common/bmp/bmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ sail_status_t bmp_private_read_frame(void *state, struct sail_io *io, struct sai
bool skip_pad_bytes = true;

for (unsigned i = image->height; i > 0; i--) {
unsigned char *scan = (unsigned char *)image->pixels + image->bytes_per_line * (bmp_state->flipped ? (i - 1) : (image->height - i));
unsigned char *scan = sail_scan_line(image->pixels, bmp_state->flipped ? (i - 1) : (image->height - i));

for (unsigned pixel_index = 0; pixel_index < image->width;) {
if (bmp_state->version >= SAIL_BMP_V3 && bmp_state->v3.compression == SAIL_BI_RLE4) {
Expand Down
4 changes: 2 additions & 2 deletions src/sail-codecs/jpeg/jpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ SAIL_EXPORT sail_status_t sail_codec_load_frame_v8_jpeg(void *state, struct sail
}

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

JSAMPROW samprow = (JSAMPROW)scanline;
(void)jpeg_read_scanlines(jpeg_state->decompress_context, &samprow, 1);
Expand Down Expand Up @@ -382,7 +382,7 @@ SAIL_EXPORT sail_status_t sail_codec_save_frame_v8_jpeg(void *state, const struc
}

for (unsigned row = 0; row < image->height; row++) {
JSAMPROW samprow = (JSAMPROW)((const unsigned char *)image->pixels + row * image->bytes_per_line);
JSAMPROW samprow = (JSAMPROW)sail_scan_line(image, row);
jpeg_write_scanlines(jpeg_state->compress_context, &samprow, 1);
}

Expand Down
4 changes: 2 additions & 2 deletions src/sail-codecs/jpeg2000/jpeg2000.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,15 @@ SAIL_EXPORT sail_status_t sail_codec_load_frame_v8_jpeg2000(void *state, struct
}

if (jpeg2000_state->channel_depth_scaled == 8) {
unsigned char *scan = (unsigned char *)image->pixels + row * image->bytes_per_line;
unsigned char *scan = sail_scan_line(image, row);

for (unsigned column = 0; column < image->width; column++) {
for (int channel = 0; channel < jpeg2000_state->number_channels; channel++) {
*scan++ = (unsigned char)(jas_matrix_getv(jpeg2000_state->matrix[channel], column) << jpeg2000_state->shift);
}
}
} else {
uint16_t *scan = (uint16_t *)((uint8_t *)image->pixels + row * image->bytes_per_line);
uint16_t *scan = sail_scan_line(image, row);

for (unsigned column = 0; column < image->width; column++) {
for (int channel = 0; channel < jpeg2000_state->number_channels; channel++) {
Expand Down
2 changes: 1 addition & 1 deletion src/sail-codecs/pcx/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ sail_status_t pcx_private_build_palette(enum SailPixelFormat pixel_format, struc
sail_status_t pcx_private_read_uncompressed(struct sail_io *io, unsigned bytes_per_plane_to_read, unsigned planes, unsigned char *buffer, struct sail_image *image) {

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

/* Read plane by plane and then merge them into the image pixels. */
for (unsigned plane = 0; plane < planes; plane++) {
Expand Down
2 changes: 1 addition & 1 deletion src/sail-codecs/pcx/pcx.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ SAIL_EXPORT sail_status_t sail_codec_load_frame_v8_pcx(void *state, struct sail_
}

/* Merge planes into the image pixels. */
unsigned char * const scan = (unsigned char *)image->pixels + image->bytes_per_line * row;
unsigned char * const scan = sail_scan_line(image, row);

for (unsigned plane = 0; plane < pcx_state->pcx_header.planes; plane++) {
const unsigned buffer_plane_offset = plane * pcx_state->pcx_header.bytes_per_line;
Expand Down
8 changes: 4 additions & 4 deletions src/sail-codecs/png/png.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ SAIL_EXPORT sail_status_t sail_codec_load_frame_v8_png(void *state, struct sail_
#ifdef PNG_APNG_SUPPORTED
if (png_state->is_apng) {
for (unsigned row = 0; row < image->height; row++) {
unsigned char *scanline = (unsigned char *)image->pixels + row * image->bytes_per_line;
unsigned char *scanline = sail_scan_line(image, row);

memcpy(scanline, png_state->prev[row], png_state->first_image->bytes_per_line);

Expand Down Expand Up @@ -430,12 +430,12 @@ SAIL_EXPORT sail_status_t sail_codec_load_frame_v8_png(void *state, struct sail_
}
} else {
for (unsigned row = 0; row < image->height; row++) {
png_read_row(png_state->png_ptr, (unsigned char *)image->pixels + row * image->bytes_per_line, NULL);
png_read_row(png_state->png_ptr, sail_scan_line(image, row), NULL);
}
}
#else
for (unsigned row = 0; row < image->height; row++) {
png_read_row(png_state->png_ptr, (unsigned char *)image->pixels + row * image->bytes_per_line, NULL);
png_read_row(png_state->png_ptr, sail_scan_line(image, row), NULL);
}
#endif
}
Expand Down Expand Up @@ -638,7 +638,7 @@ SAIL_EXPORT sail_status_t sail_codec_save_frame_v8_png(void *state, const struct

for (int current_pass = 0; current_pass < png_state->interlaced_passes; current_pass++) {
for (unsigned row = 0; row < image->height; row++) {
png_write_row(png_state->png_ptr, (const unsigned char *)image->pixels + row * image->bytes_per_line);
png_write_row(png_state->png_ptr, sail_scan_line(image, row));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/sail-codecs/psd/psd.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ SAIL_EXPORT sail_status_t sail_codec_load_frame_v8_psd(void *state, struct sail_
SAIL_TRY(psd_state->io->strict_read(psd_state->io->stream, &value, sizeof(value)));

for (unsigned i = count; i < count + c; i++) {
unsigned char *scan = (unsigned char *)image->pixels + row * image->bytes_per_line + i * bpp;
unsigned char *scan = (unsigned char *)sail_scan_line(image, row) + i * bpp;
*(scan + channel) = value;
}
} else if (c < 128) {
Expand All @@ -282,7 +282,7 @@ SAIL_EXPORT sail_status_t sail_codec_load_frame_v8_psd(void *state, struct sail_
unsigned char value;
SAIL_TRY(psd_state->io->strict_read(psd_state->io->stream, &value, sizeof(value)));

unsigned char *scan = (unsigned char *)image->pixels + row * image->bytes_per_line + i * bpp;
unsigned char *scan = (unsigned char *)sail_scan_line(image, row) + i * bpp;
*(scan + channel) = value;
}
}
Expand All @@ -297,7 +297,7 @@ SAIL_EXPORT sail_status_t sail_codec_load_frame_v8_psd(void *state, struct sail_
SAIL_TRY(psd_state->io->strict_read(psd_state->io->stream, psd_state->scan_buffer, psd_state->bytes_per_channel));

for (unsigned count = 0; count < psd_state->bytes_per_channel; count++) {
unsigned char *scan = (unsigned char *)image->pixels + row * image->bytes_per_line + count * bpp;
unsigned char *scan = (unsigned char *)sail_scan_line(image, row) + count * bpp;
*(scan + channel) = *(psd_state->scan_buffer + count);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/sail-codecs/tiff/tiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ SAIL_EXPORT sail_status_t sail_codec_save_frame_v8_tiff(void *state, const struc
}

for (unsigned row = 0; row < image->height; row++) {
if (TIFFWriteScanline(tiff_state->tiff, (unsigned char *)image->pixels + row * image->bytes_per_line, tiff_state->line++, 0) < 0) {
if (TIFFWriteScanline(tiff_state->tiff, sail_scan_line(image, row), tiff_state->line++, 0) < 0) {
SAIL_LOG_AND_RETURN(SAIL_ERROR_UNDERLYING_CODEC);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/sail-codecs/webp/webp.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ SAIL_EXPORT sail_status_t sail_codec_load_frame_v8_webp(void *state, struct sail
SAIL_LOG_AND_RETURN(SAIL_ERROR_UNDERLYING_CODEC);
}

uint8_t *dst_scanline = (uint8_t *)webp_state->canvas_image->pixels + webp_state->frame_y * image->bytes_per_line + webp_state->frame_x * webp_state->bytes_per_pixel;
uint8_t *dst_scanline = (uint8_t *)sail_scan_line(webp_state->canvas_image, webp_state->frame_y) + webp_state->frame_x * webp_state->bytes_per_pixel;
uint8_t *src_scanline = image->pixels;

for (unsigned row = 0; row < webp_state->frame_height; row++, dst_scanline += webp_state->canvas_image->bytes_per_line,
Expand Down
8 changes: 4 additions & 4 deletions src/sail-common/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ sail_status_t sail_mirror_vertically(struct sail_image *image) {
SAIL_TRY(sail_malloc(image->bytes_per_line, &line));

for (unsigned row1 = 0, row2 = image->height - 1; row1 < row2; row1++, row2--) {
memcpy(line, (unsigned char *)image->pixels + image->bytes_per_line * row1, image->bytes_per_line);
memcpy((unsigned char *)image->pixels + image->bytes_per_line * row1, (unsigned char *)image->pixels + image->bytes_per_line * row2, image->bytes_per_line);
memcpy((unsigned char *)image->pixels + image->bytes_per_line * row2, line, image->bytes_per_line);
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_free(line);
Expand All @@ -201,7 +201,7 @@ sail_status_t sail_mirror_horizontally(struct sail_image *image) {
SAIL_TRY(sail_malloc(bytes_per_pixel, &pixel));

for (unsigned row = 0; row < image->height; row++) {
unsigned char *scan = (unsigned char *)image->pixels + image->bytes_per_line * 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);
Expand Down

0 comments on commit 1dfab50

Please sign in to comment.