Skip to content

Commit

Permalink
Merge pull request #34 from madosuki/develop
Browse files Browse the repository at this point in the history
fix around of calcurate aspect raito and size
  • Loading branch information
madosuki authored Feb 17, 2023
2 parents 91381ab + 6ff16bf commit b087c14
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 39 deletions.
33 changes: 5 additions & 28 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,15 @@ int get_hash(uint8_t *bytes, const ssize_t bytes_size, uint8_t *result)
return 1;
}

double mygcd(double x, double y)
double *calc_aspect_raito(int width, int height)
{
if(x < 0 || y == 0)
return 1;


if(x == y)
return 1;


double left = fmin(x, y);
double right = fmod(fmax(x, y), fmin(x, y));
while(1) {
double tmp = fmod(left, right);
if(tmp <= 0.0)
return right;

left = right;
right = tmp;

}
}

double *calc_aspect_raito(int width, int height, double gcd)
{
double width_aspect = width / gcd;
double height_aspect = height / gcd;
double for_width_aspect = (double)width / height;
double for_height_aspect = (double)height / width;

double *tuple = (double*)calloc(2, sizeof(double));

tuple[0] = width_aspect;
tuple[1] = height_aspect;
tuple[0] = for_width_aspect;
tuple[1] = for_height_aspect;

return tuple;
}
Expand Down
4 changes: 1 addition & 3 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ extern const uint8_t compress_headers_flag;

int get_hash(uint8_t *bytes, const ssize_t bytes_size, uint8_t *result);

double mygcd(double x, double y);

double *calc_aspect_raito(int width, int height, double gcd);
double *calc_aspect_raito(int width, int height);

int detect_image(uint8_t *buf, int size);

Expand Down
16 changes: 8 additions & 8 deletions src/viewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ void set_image_container(ulong position)
int width = comic_container->image_container_list[position]->src_width;
int height = comic_container->image_container_list[position]->src_height;

comic_container->image_container_list[position]->aspect_raito = calc_aspect_raito(width, height, mygcd(width, height));
comic_container->image_container_list[position]->aspect_raito = calc_aspect_raito(width, height);

}

Expand Down Expand Up @@ -661,7 +661,7 @@ void set_image_container(ulong position)
int width = comic_container->image_container_list[position]->src_width;
int height = comic_container->image_container_list[position]->src_height;

comic_container->image_container_list[position]->aspect_raito = calc_aspect_raito(width, height, mygcd(width, height));
comic_container->image_container_list[position]->aspect_raito = calc_aspect_raito(width, height);

}

Expand Down Expand Up @@ -697,7 +697,7 @@ int resize_when_single(int position)

isOverHeight = TRUE;

int result = (int)ceil((double)height * (w_aspect / h_aspect));
int result = (int)ceil((double)height / h_aspect);
width = result;
}

Expand Down Expand Up @@ -835,14 +835,14 @@ void scale_when_oversize(int *x, int *y, int window_width, int window_height, do
if(isOverWidth) {
int diff = *x - window_width;
int width = *x - diff;
int height = (int)ceil((double)width * (h_aspect / w_aspect));
int height = (int)ceil((double)width / w_aspect);

*x = width;
*y = height;
} else {
int diff = *y - window_height;
int height = *y - diff;
int width = (int)ceil((double)height * (w_aspect / h_aspect));
int width = (int)ceil((double)height / h_aspect);

*x = width;
*y = height;
Expand Down Expand Up @@ -907,7 +907,7 @@ int resize_when_spread(int page)
img_y_aspect = (double)comic_container->image_container_list[page]->aspect_raito[1];

img_width = half_width;
img_height = (int)ceil((double)img_width * (img_y_aspect / img_x_aspect));
img_height = (int)ceil((double)img_width / img_x_aspect);

if(img_height > diff_height_between_window_and_menu_and_button_bar) {
scale_when_oversize(&img_width, &img_height, window_width, diff_height_between_window_and_menu_and_button_bar, img_x_aspect, img_y_aspect, FALSE);
Expand Down Expand Up @@ -938,7 +938,7 @@ int resize_when_spread(int page)
left_y_aspect = (double)comic_container->image_container_list[left_pos]->aspect_raito[1];

left_width = half_width;
left_height = (int)ceil((double)left_width * (left_y_aspect / left_x_aspect));
left_height = (int)ceil((double)left_width / left_x_aspect);

right_src_width = comic_container->image_container_list[right_pos]->src_width;
right_src_height = comic_container->image_container_list[right_pos]->src_height;
Expand All @@ -959,7 +959,7 @@ int resize_when_spread(int page)
comic_container->image_container_list[left_pos]->dst_height = left_height;

right_width = half_width;
right_height = (int)ceil((double)right_width * (right_y_aspect / right_x_aspect));
right_height = (int)ceil((double)right_width / right_x_aspect);

if(right_height > diff_height_between_window_and_menu_and_button_bar) {
scale_when_oversize(&right_width, &right_height, window_width, diff_height_between_window_and_menu_and_button_bar, right_x_aspect, right_y_aspect, FALSE);
Expand Down

0 comments on commit b087c14

Please sign in to comment.