From 8b53bac3e333286925e56530c2a983a1c1ebfbbe Mon Sep 17 00:00:00 2001 From: Weiyi <131678005+Anglebase@users.noreply.github.com> Date: Sun, 22 Sep 2024 22:01:59 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=AD=A3IMAGE::gentexture?= =?UTF-8?q?=E7=9A=84=E4=BC=9A=E5=BC=95=E5=8F=91=E6=A0=88=E6=BA=A2=E5=87=BA?= =?UTF-8?q?=E7=9A=84=E5=87=BD=E6=95=B0=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/image.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/image.cpp b/src/image.cpp index 0bf9121..d896230 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -129,7 +129,7 @@ void IMAGE::gentexture(bool gen) } } else { if (m_texture != NULL) { - gentexture(true); + gentexture(false); } Gdiplus::Bitmap* bitmap = new Gdiplus::Bitmap(getwidth(), getheight(), getwidth() * 4, PixelFormat32bppARGB, (BYTE*)getbuffer()); From cba71f50c74a83a61ea380ef340f54b63fc14865 Mon Sep 17 00:00:00 2001 From: Weiyi <131678005+Anglebase@users.noreply.github.com> Date: Sun, 17 Nov 2024 20:35:09 +0800 Subject: [PATCH 2/3] =?UTF-8?q?EGE=E5=85=B3=E4=BA=8E=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E6=9C=BA=E5=9B=BE=E5=83=8F=E5=A4=84=E7=90=86=E7=9A=84=E5=86=85?= =?UTF-8?q?=E5=AE=B9=EF=BC=88=E6=98=A0=E5=B0=84=E5=8F=98=E6=8D=A2=E5=92=8C?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E5=8F=98=E6=8D=A2=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/ege.h | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/include/ege.h b/include/ege.h index 303671f..30e6a3c 100644 --- a/include/ege.h +++ b/include/ege.h @@ -1321,6 +1321,113 @@ int EGEAPI savepng (PCIMAGE pimg, const wchar_t* filename, bool withAlphaChann int EGEAPI savebmp (PCIMAGE pimg, const char* filename, bool withAlphaChannel = false); int EGEAPI savebmp (PCIMAGE pimg, const wchar_t* filename, bool withAlphaChannel = false); +// 映射处理,此函数会对图像中的每个像素进行映射处理 +// 例如: +// - 灰度变换: rgb2gray +// - 颜色反转: [](color_t c)->color_t { return EGERGB(255 - EGEGET_R(c), 255 - EGEGET_G(c), 255 - EGEGET_B(c));} +// - 二值化(灰度): [](color_t c)->color_t { return EGEGET_R(c) > 128? WHITE : BLACK;} +inline int image_transform( + PIMAGE img, // 输入图像 + color_t(*trans)(color_t) // 映射函数 +) { + const int width = getwidth(img); + const int height = getheight(img); + color_t(*buf)[width] = (color_t(*)[width])getbuffer(img); + // apply transformation to each pixel in the image + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + buf[i][j] = trans(buf[i][j]); + } + } + return 0; +} + +// 模板处理模式 +// 它用于决定如何处理图像边界的像素 +enum TEMPLATE_MODE { + WHITE_OUTLINE, // 超出图像范围的按照白色进行处理 + BLACK_OUTLINE, // 超出图像范围的按照黑色进行处理 + IGNORE_OUTLINE, // 超出图像范围的在应用模板时忽略该位置 + NOT_ANYTING // 不对应用模板会超出图像范围的像素进行处理 +}; + +// 模板处理 +// 此函数会对图像中的每个像素进行模板处理 +// 例如: +// - 模糊: +// const int size = 3; +// float kernel_blur7[size][size] = { +// {1.f / 9.f, 1.f / 9.f, 1.f / 9.f}, +// {1.f / 9.f, 1.f / 9.f, 1.f / 9.f}, +// {1.f / 9.f, 1.f / 9.f, 1.f / 9.f} +// }; +// image_template(img2, img, (float**)kernel_blur7, size, NOT_ANYTING); +// - 锐化: +// const int size = 3; +// float kernel_sharpen[size][size] = { +// {-0.5f, -0.5f, -0.5f}, +// {-0.5f, +5.0f, -0.5f}, +// {-0.5f, -0.5f, -0.5f} +// }; +// image_template(img2, img, (float**)kernel_sharpen, size, NOT_ANYTING); +// - 边缘检测: +// const int size = 3; +// float kernel_edge[size][size] = { +// {-1.f, -1.f, -1.f}, +// {-1.f, 8.f, -1.f}, +// {-1.f, -1.f, -1.f} +// }; +// image_template(img2, img, (float**)kernel_edge, size, NOT_ANYTING); +inline int image_template( + PIMAGE dest, // 输出图像 + PCIMAGE src, // 输入图像 + float** temp, // 模板,是一个二维数组,每个元素代表模板的权重 + int size, // 模板大小,必须为奇数且为模板的边长 + TEMPLATE_MODE mode = NOT_ANYTING // 模板处理模式 +) { + const int width = getwidth(src); + const int height = getheight(src); + resize(dest, width, height); + float (*temp_)[size] = (float(*)[size])temp; + color_t(*buf)[width] = (color_t(*)[width])getbuffer(src); + color_t(*buf2)[width] = (color_t(*)[width])getbuffer(dest); + // apply template to each pixel in the image + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + float sum_r = 0.f, sum_g = 0.f, sum_b = 0.f; + unsigned char r, g, b; + for (int k = -size / 2; k <= size / 2; k++) { + for (int l = -size / 2; l <= size / 2; l++) { + int x = i + k, y = j + l, m = k + size / 2, n = l + size / 2; + color_t c; + if (x < 0 || x >= height || y < 0 || y >= width) { + switch (mode) { + case WHITE_OUTLINE: c = WHITE; break; + case BLACK_OUTLINE: c = BLACK; break; + case IGNORE_OUTLINE: continue; + case NOT_ANYTING: goto not_anything; + default: return 1; + } + } + else { + c = buf[x][y]; + } + sum_r += temp_[m][n] * EGEGET_R(c); + sum_g += temp_[m][n] * EGEGET_G(c); + sum_b += temp_[m][n] * EGEGET_B(c); + } + } + r = sum_r > 255 ? 255 : sum_r < 0 ? 0 : (unsigned char)sum_r; + g = sum_g > 255 ? 255 : sum_g < 0 ? 0 : (unsigned char)sum_g; + b = sum_b > 255 ? 255 : sum_b < 0 ? 0 : (unsigned char)sum_b; + buf2[i][j] = EGERGB(r, g, b); + not_anything: + (void)0; + } + } + return 0; +} + int EGEAPI putimage_transparent( PIMAGE imgDest, // handle to dest PCIMAGE imgSrc, // handle to source From 7b25e09472a70a8c29f08b830975e33ace391938 Mon Sep 17 00:00:00 2001 From: Weiyi <131678005+Anglebase@users.noreply.github.com> Date: Sun, 17 Nov 2024 20:59:47 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E9=80=82=E7=94=A8=E4=BA=8EMSVC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/ege.h | 52 +++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/include/ege.h b/include/ege.h index 30e6a3c..74efdd0 100644 --- a/include/ege.h +++ b/include/ege.h @@ -1332,11 +1332,11 @@ inline int image_transform( ) { const int width = getwidth(img); const int height = getheight(img); - color_t(*buf)[width] = (color_t(*)[width])getbuffer(img); + color_t* buf = (color_t*)getbuffer(img); // apply transformation to each pixel in the image for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { - buf[i][j] = trans(buf[i][j]); + buf[i * width + j] = trans(buf[i * width + j]); } } return 0; @@ -1356,41 +1356,41 @@ enum TEMPLATE_MODE { // 例如: // - 模糊: // const int size = 3; -// float kernel_blur7[size][size] = { -// {1.f / 9.f, 1.f / 9.f, 1.f / 9.f}, -// {1.f / 9.f, 1.f / 9.f, 1.f / 9.f}, -// {1.f / 9.f, 1.f / 9.f, 1.f / 9.f} +// float kernel_blur7[size * size] = { +// 1.f / 9.f, 1.f / 9.f, 1.f / 9.f, +// 1.f / 9.f, 1.f / 9.f, 1.f / 9.f, +// 1.f / 9.f, 1.f / 9.f, 1.f / 9.f // }; -// image_template(img2, img, (float**)kernel_blur7, size, NOT_ANYTING); +// image_template(img2, img, kernel_blur7, size, NOT_ANYTING); // - 锐化: // const int size = 3; -// float kernel_sharpen[size][size] = { -// {-0.5f, -0.5f, -0.5f}, -// {-0.5f, +5.0f, -0.5f}, -// {-0.5f, -0.5f, -0.5f} +// float kernel_sharpen[size * size] = { +// -0.5f, -0.5f, -0.5f, +// -0.5f, +5.0f, -0.5f, +// -0.5f, -0.5f, -0.5f // }; -// image_template(img2, img, (float**)kernel_sharpen, size, NOT_ANYTING); +// image_template(img2, img, kernel_sharpen, size, NOT_ANYTING); // - 边缘检测: // const int size = 3; -// float kernel_edge[size][size] = { -// {-1.f, -1.f, -1.f}, -// {-1.f, 8.f, -1.f}, -// {-1.f, -1.f, -1.f} +// float kernel_edge[size * size] = { +// -1.f, -1.f, -1.f, +// -1.f, 8.f, -1.f, +// -1.f, -1.f, -1.f // }; -// image_template(img2, img, (float**)kernel_edge, size, NOT_ANYTING); +// image_template(img2, img, kernel_edge, size, NOT_ANYTING); inline int image_template( PIMAGE dest, // 输出图像 PCIMAGE src, // 输入图像 - float** temp, // 模板,是一个二维数组,每个元素代表模板的权重 + float* temp, // 模板,是一个二维数组,每个元素代表模板的权重 int size, // 模板大小,必须为奇数且为模板的边长 TEMPLATE_MODE mode = NOT_ANYTING // 模板处理模式 ) { const int width = getwidth(src); const int height = getheight(src); resize(dest, width, height); - float (*temp_)[size] = (float(*)[size])temp; - color_t(*buf)[width] = (color_t(*)[width])getbuffer(src); - color_t(*buf2)[width] = (color_t(*)[width])getbuffer(dest); + color_t* buf = (color_t*)getbuffer(src); + color_t* buf2 = (color_t*)getbuffer(dest); + float* temp_ = temp; // apply template to each pixel in the image for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { @@ -1410,17 +1410,17 @@ inline int image_template( } } else { - c = buf[x][y]; + c = buf[x * width + y]; } - sum_r += temp_[m][n] * EGEGET_R(c); - sum_g += temp_[m][n] * EGEGET_G(c); - sum_b += temp_[m][n] * EGEGET_B(c); + sum_r += temp_[m * size + n] * EGEGET_R(c); + sum_g += temp_[m * size + n] * EGEGET_G(c); + sum_b += temp_[m * size + n] * EGEGET_B(c); } } r = sum_r > 255 ? 255 : sum_r < 0 ? 0 : (unsigned char)sum_r; g = sum_g > 255 ? 255 : sum_g < 0 ? 0 : (unsigned char)sum_g; b = sum_b > 255 ? 255 : sum_b < 0 ? 0 : (unsigned char)sum_b; - buf2[i][j] = EGERGB(r, g, b); + buf2[i * width + j] = EGERGB(r, g, b); not_anything: (void)0; }