Skip to content

Commit

Permalink
Make image resizing not blurry
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGreatRambler committed Mar 9, 2022
1 parent ed88356 commit d6e57db
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/Drawers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,29 +299,31 @@ void Drawers::DrawImageOpacity(std::string path, double opacity, int x, int y, i
fmt::print("Printing {} at X:{} Y:{} with W:{} and H:{}\n", path, x, y, targetWidth, targetHeight);

if(!noRender) {
cairo_surface_t* image = nullptr;
if(imageCache.contains(path)) {
image = imageCache[path];
cairo_pattern_t* pattern = nullptr;
if(patternCache.contains(path)) {
pattern = patternCache[path];
} else {
image = cairo_image_surface_create_from_png(path.c_str());
cairo_surface_t* image = cairo_image_surface_create_from_png(path.c_str());

if(cairo_surface_status(image) == CAIRO_STATUS_FILE_NOT_FOUND) {
fmt::print("Image {} does not exist\n", path);
return;
}

imageCache[path] = image;
pattern = cairo_pattern_create_for_surface(image);
cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
patternCache[path] = pattern;
}

int imageWidth = cairo_image_surface_get_width(imageCache[path]);
int imageHeight = cairo_image_surface_get_height(imageCache[path]);
cairo_save(cr);
cairo_translate(cr, (double)x, (double)y);
cairo_scale(cr, (double)targetWidth / imageWidth, (double)targetHeight / imageHeight);
cairo_set_source_surface(cr, image, 0, 0);
cairo_set_source(cr, pattern);
cairo_paint_with_alpha(cr, opacity);
cairo_restore(cr);
cairo_surface_destroy(image);
}

if(addDrawingInstructions) {
Expand All @@ -343,18 +345,21 @@ void Drawers::DrawImageRotate(std::string path, double angle, int x, int y, int
fmt::print("Printing {} at X:{} Y:{} with W:{} and H:{}\n", path, x, y, targetWidth, targetHeight);

if(!noRender) {
cairo_surface_t* image = nullptr;
if(imageCache.contains(path)) {
image = imageCache[path];
cairo_pattern_t* pattern = nullptr;
if(patternCache.contains(path)) {
pattern = patternCache[path];
} else {
image = cairo_image_surface_create_from_png(path.c_str());
cairo_surface_t* image = cairo_image_surface_create_from_png(path.c_str());

if(cairo_surface_status(image) == CAIRO_STATUS_FILE_NOT_FOUND) {
fmt::print("Image {} does not exist\n", path);
return;
}

imageCache[path] = image;
pattern = cairo_pattern_create_for_surface(image);
cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
patternCache[path] = pattern;
}

int imageWidth = cairo_image_surface_get_width(imageCache[path]);
Expand All @@ -364,10 +369,9 @@ void Drawers::DrawImageRotate(std::string path, double angle, int x, int y, int
cairo_rotate(cr, angle);
cairo_scale(cr, (double)targetWidth / imageWidth, (double)targetHeight / imageHeight);
cairo_translate(cr, -imageWidth / 2, -imageHeight / 2);
cairo_set_source_surface(cr, image, 0, 0);
cairo_set_source(cr, pattern);
cairo_paint(cr);
cairo_restore(cr);
cairo_surface_destroy(image);
}

if(addDrawingInstructions) {
Expand All @@ -390,18 +394,21 @@ void Drawers::DrawImageRotateOpacity(
fmt::print("Printing {} at X:{} Y:{} with W:{} and H:{}\n", path, x, y, targetWidth, targetHeight);

if(!noRender) {
cairo_surface_t* image = nullptr;
if(imageCache.contains(path)) {
image = imageCache[path];
cairo_pattern_t* pattern = nullptr;
if(patternCache.contains(path)) {
pattern = patternCache[path];
} else {
image = cairo_image_surface_create_from_png(path.c_str());
cairo_surface_t* image = cairo_image_surface_create_from_png(path.c_str());

if(cairo_surface_status(image) == CAIRO_STATUS_FILE_NOT_FOUND) {
fmt::print("Image {} does not exist\n", path);
return;
}

imageCache[path] = image;
pattern = cairo_pattern_create_for_surface(image);
cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
patternCache[path] = pattern;
}

int imageWidth = cairo_image_surface_get_width(imageCache[path]);
Expand All @@ -411,10 +418,9 @@ void Drawers::DrawImageRotateOpacity(
cairo_rotate(cr, angle);
cairo_scale(cr, (double)targetWidth / imageWidth, (double)targetHeight / imageHeight);
cairo_translate(cr, -imageWidth / 2, -imageHeight / 2);
cairo_set_source_surface(cr, image, 0, 0);
cairo_set_source(cr, pattern);
cairo_paint_with_alpha(cr, opacity);
cairo_restore(cr);
cairo_surface_destroy(image);
}

if(addDrawingInstructions) {
Expand Down

0 comments on commit d6e57db

Please sign in to comment.