Skip to content

Instantly share code, notes, and snippets.

@Mathias-Fuchs
Created October 29, 2021 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mathias-Fuchs/1d3ade66df2df6dacc59125c76b0414a to your computer and use it in GitHub Desktop.
Save Mathias-Fuchs/1d3ade66df2df6dacc59125c76b0414a to your computer and use it in GitHub Desktop.
Round the corners of a png
#define _CRT_SECURE_NO_WARNINGS
#define STB_IMAGE_IMPLEMENTATION
#include "../stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "../stb/stb_image_write.h"
int main(int argc, char** argv)
{
char* filename[6];
filename[0] = "C:\\Users\\Z77Pro\\source\\repos\\eigenPlayGround\\Resources\\0A.png";
filename[1] = "C:\\Users\\Z77Pro\\source\\repos\\eigenPlayGround\\Resources\\0B.png";
filename[2] = "C:\\Users\\Z77Pro\\source\\repos\\eigenPlayGround\\Resources\\1A.png";
filename[3] = "C:\\Users\\Z77Pro\\source\\repos\\eigenPlayGround\\Resources\\1B.png";
filename[4] = "C:\\Users\\Z77Pro\\source\\repos\\eigenPlayGround\\Resources\\2A.png";
filename[5] = "C:\\Users\\Z77Pro\\source\\repos\\eigenPlayGround\\Resources\\2B.png";
for (int ii = 0; ii < 6; ii++) {
int width, height, channels;
unsigned char* img = stbi_load(filename[ii], &width, &height, &channels, 0);
if (img == NULL) {
fprintf(stderr, "Error in loading the image\n");
exit(1);
}
if (channels != 4) {
fprintf(stderr, "Need 4 channels");
exit(1);
}
int r = 30;
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
if (
(i < r && j < r && (i - r) * (i - r) + (j - r) * (j - r) >= r * r) ||
(height - 1 - i < r && j < r && (height - 1 - i - r) * (height - 1 - i - r) + (j - r) * (j - r) >= r * r) ||
(i < r && (width - 1 - j) < r && (i - r) * (i - r) + ((width - 1 - j) - r) * ((width - 1 - j) - r) >= r * r) ||
(height - 1 - i < r && (width - 1 - j) < r && (height - 1 - i - r) * (height - 1 - i - r) + ((width - 1 - j) - r) * ((width - 1 - j) - r) >= r * r)
)
img[4 * (i * width + j) + 3] = 0;
stbi_write_png(filename[ii], width, height, channels, img, width * channels);
stbi_image_free(img);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment