Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Sep 9, 2024
1 parent 4f9d8bf commit 57e0413
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 49 deletions.
28 changes: 14 additions & 14 deletions include/plutovg.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,19 @@ PLUTOVG_API void plutovg_matrix_init_translate(plutovg_matrix_t* matrix, float t
PLUTOVG_API void plutovg_matrix_init_scale(plutovg_matrix_t* matrix, float sx, float sy);

/**
* @brief Initializes a 2D transformation matrix for shearing.
* @brief Initializes a 2D transformation matrix for rotation.
* @param matrix A pointer to the `plutovg_matrix_t` object to be initialized.
* @param shx The shearing factor in the x-direction.
* @param shy The shearing factor in the y-direction.
* @param angle The rotation angle in radians.
*/
PLUTOVG_API void plutovg_matrix_init_shear(plutovg_matrix_t* matrix, float shx, float shy);
PLUTOVG_API void plutovg_matrix_init_rotate(plutovg_matrix_t* matrix, float angle);

/**
* @brief Initializes a 2D transformation matrix for rotation.
* @brief Initializes a 2D transformation matrix for shearing.
* @param matrix A pointer to the `plutovg_matrix_t` object to be initialized.
* @param angle The rotation angle in radians.
* @param shx The shearing factor in the x-direction.
* @param shy The shearing factor in the y-direction.
*/
PLUTOVG_API void plutovg_matrix_init_rotate(plutovg_matrix_t* matrix, float angle);
PLUTOVG_API void plutovg_matrix_init_shear(plutovg_matrix_t* matrix, float shx, float shy);

/**
* @brief Adds a translation with offsets `tx` and `ty` to the matrix.
Expand All @@ -198,19 +198,19 @@ PLUTOVG_API void plutovg_matrix_translate(plutovg_matrix_t* matrix, float tx, fl
PLUTOVG_API void plutovg_matrix_scale(plutovg_matrix_t* matrix, float sx, float sy);

/**
* @brief Shears the matrix by factors `shx` and `shy`.
* @brief Rotates the matrix by the specified angle (in radians).
* @param matrix A pointer to the `plutovg_matrix_t` object to be modified.
* @param shx The shearing factor in the x-direction.
* @param shy The shearing factor in the y-direction.
* @param angle The rotation angle in radians.
*/
PLUTOVG_API void plutovg_matrix_shear(plutovg_matrix_t* matrix, float shx, float shy);
PLUTOVG_API void plutovg_matrix_rotate(plutovg_matrix_t* matrix, float angle);

/**
* @brief Rotates the matrix by the specified angle (in radians).
* @brief Shears the matrix by factors `shx` and `shy`.
* @param matrix A pointer to the `plutovg_matrix_t` object to be modified.
* @param angle The rotation angle in radians.
* @param shx The shearing factor in the x-direction.
* @param shy The shearing factor in the y-direction.
*/
PLUTOVG_API void plutovg_matrix_rotate(plutovg_matrix_t* matrix, float angle);
PLUTOVG_API void plutovg_matrix_shear(plutovg_matrix_t* matrix, float shx, float shy);

/**
* @brief Multiplies `left` and `right` matrices and stores the result in `matrix`.
Expand Down
18 changes: 9 additions & 9 deletions source/plutovg-matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ void plutovg_matrix_init_scale(plutovg_matrix_t* matrix, float sx, float sy)
plutovg_matrix_init(matrix, sx, 0, 0, sy, 0, 0);
}

void plutovg_matrix_init_shear(plutovg_matrix_t* matrix, float shx, float shy)
{
plutovg_matrix_init(matrix, 1, tanf(shy), tanf(shx), 1, 0, 0);
}

void plutovg_matrix_init_rotate(plutovg_matrix_t* matrix, float angle)
{
float c = cosf(angle);
float s = sinf(angle);
plutovg_matrix_init(matrix, c, s, -s, c, 0, 0);
}

void plutovg_matrix_init_shear(plutovg_matrix_t* matrix, float shx, float shy)
{
plutovg_matrix_init(matrix, 1, tanf(shy), tanf(shx), 1, 0, 0);
}

void plutovg_matrix_translate(plutovg_matrix_t* matrix, float tx, float ty)
{
plutovg_matrix_t m;
Expand All @@ -51,17 +51,17 @@ void plutovg_matrix_scale(plutovg_matrix_t* matrix, float sx, float sy)
plutovg_matrix_multiply(matrix, &m, matrix);
}

void plutovg_matrix_shear(plutovg_matrix_t* matrix, float shx, float shy)
void plutovg_matrix_rotate(plutovg_matrix_t* matrix, float angle)
{
plutovg_matrix_t m;
plutovg_matrix_init_shear(&m, shx, shy);
plutovg_matrix_init_rotate(&m, angle);
plutovg_matrix_multiply(matrix, &m, matrix);
}

void plutovg_matrix_rotate(plutovg_matrix_t* matrix, float angle)
void plutovg_matrix_shear(plutovg_matrix_t* matrix, float shx, float shy)
{
plutovg_matrix_t m;
plutovg_matrix_init_rotate(&m, angle);
plutovg_matrix_init_shear(&m, shx, shy);
plutovg_matrix_multiply(matrix, &m, matrix);
}

Expand Down
50 changes: 24 additions & 26 deletions source/plutovg-surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,53 +242,51 @@ bool plutovg_surface_write_to_jpg_stream(const plutovg_surface_t* surface, pluto
void plutovg_convert_argb_to_rgba(unsigned char* dst, const unsigned char* src, int width, int height, int stride)
{
for(int y = 0; y < height; y++) {
const uint32_t* src_row = (const uint32_t*)(src + stride * y);
uint32_t* dst_row = (uint32_t*)(dst + stride * y);
uint32_t* src_row = (uint32_t*)(src + stride * y);
for(int x = 0; x < width; x++) {
uint32_t pixel = src_row[x];
uint32_t a = (pixel >> 24) & 0xFF;
if(a == 0) {
dst_row[x] = 0x00000000;
continue;
} else {
uint32_t r = (pixel >> 16) & 0xFF;
uint32_t g = (pixel >> 8) & 0xFF;
uint32_t b = (pixel >> 0) & 0xFF;
if(a != 255) {
r = (r * 255) / a;
g = (g * 255) / a;
b = (b * 255) / a;
}

dst_row[x] = (a << 24) | (b << 16) | (g << 8) | r;
}

uint32_t r = (pixel >> 16) & 0xFF;
uint32_t g = (pixel >> 8) & 0xFF;
uint32_t b = (pixel >> 0) & 0xFF;
if(a != 255) {
r = (r * 255) / a;
g = (g * 255) / a;
b = (b * 255) / a;
}

dst_row[x] = (a << 24) | (b << 16) | (g << 8) | r;
}
}
}

void plutovg_convert_rgba_to_argb(unsigned char* dst, const unsigned char* src, int width, int height, int stride)
{
for(int y = 0; y < height; y++) {
const uint32_t* src_row = (const uint32_t*)(src + stride * y);
uint32_t* dst_row = (uint32_t*)(dst + stride * y);
uint32_t* src_row = (uint32_t*)(src + stride * y);
for(int x = 0; x < width; x++) {
uint32_t pixel = src_row[x];
uint32_t a = (pixel >> 24) & 0xFF;
if(a == 0) {
dst_row[x] = 0x00000000;
continue;
} else {
uint32_t b = (pixel >> 16) & 0xFF;
uint32_t g = (pixel >> 8) & 0xFF;
uint32_t r = (pixel >> 0) & 0xFF;
if(a != 255) {
r = (r * a) / 255;
g = (g * a) / 255;
b = (b * a) / 255;
}

dst_row[x] = (a << 24) | (r << 16) | (g << 8) | b;
}

uint32_t b = (pixel >> 16) & 0xFF;
uint32_t g = (pixel >> 8) & 0xFF;
uint32_t r = (pixel >> 0) & 0xFF;
if(a != 255) {
r = (r * a) / 255;
g = (g * a) / 255;
b = (b * a) / 255;
}

dst_row[x] = (a << 24) | (r << 16) | (g << 8) | b;
}
}
}

0 comments on commit 57e0413

Please sign in to comment.