From 63fcc1165b378e63debbc1f769661bb362ff3533 Mon Sep 17 00:00:00 2001 From: JackieWu Date: Tue, 1 Sep 2020 00:27:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89imread=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=B8=AD=E5=A4=9A=E4=BD=99=E7=9A=84=E6=95=B0=E6=8D=AE=E6=8B=B7?= =?UTF-8?q?=E8=B4=9D=20(#392)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * remove unnecessary copy * combine loop --- examples/common/tengine_operations.c | 46 ++++++---------------------- 1 file changed, 10 insertions(+), 36 deletions(-) diff --git a/examples/common/tengine_operations.c b/examples/common/tengine_operations.c index 52386ea0..f29d25fd 100644 --- a/examples/common/tengine_operations.c +++ b/examples/common/tengine_operations.c @@ -634,41 +634,23 @@ void combination_image(image source, image dest, int dx, int dy) image imread(const char* filename) { - image im = load_image_stb(filename, 0); - for (int c = 0; c < im.c; c++) - { - for (int h = 0; h < im.h; h++) - { - for (int w = 0; w < im.w; w++) - { - int newIndex = ( c )*im.h * im.w + h * im.w + w; - im.data[newIndex] = im.data[newIndex]; - } - } - } - return im; + return load_image_stb(filename, 0); } image imread2post(const char* filename) { image im = load_image_stb(filename, 0); - for (int c = 0; c < im.c; c++) - { - for (int h = 0; h < im.h; h++) - { - for (int w = 0; w < im.w; w++) - { - int newIndex = ( c )*im.h * im.w + h * im.w + w; - im.data[newIndex] = im.data[newIndex] * 255; - } - } + const int len = im.c * im.h * im.w; + for (int i = 0; i < len; ++i) { + im.data[i] *= 255; } return im; } image rgb2bgr_premute(image src) { - float* GRB = ( float* )malloc(sizeof(float) * src.c * src.h * src.w); + const int len = src.c * src.h * src.w; + float* GRB = ( float* )malloc(sizeof(float) * len); for (int c = 0; c < src.c; c++) { for (int h = 0; h < src.h; h++) @@ -681,16 +663,8 @@ image rgb2bgr_premute(image src) } } } - for (int c = 0; c < src.c; c++) - { - for (int h = 0; h < src.h; h++) - { - for (int w = 0; w < src.w; w++) - { - int newIndex = ( c )*src.h * src.w + h * src.w + w; - src.data[newIndex] = GRB[newIndex]; - } - } + for (int i = 0; i < len; ++i) { + src.data[i] = GRB[i]; } free(GRB); return src; @@ -808,7 +782,7 @@ void draw_circle(image im, int x, int y, int radius, int r, int g, int b) void subtract(image a, image b, image c) { - int size = a.c * a.h * a.w; + const int size = a.c * a.h * a.w; for (int i = 0; i < size; i++) { c.data[i] = a.data[i] - b.data[i]; @@ -820,7 +794,7 @@ void subtract(image a, image b, image c) void multi(image a, float value, image b) { - int size = a.c * a.h * a.w; + const int size = a.c * a.h * a.w; for (int i = 0; i < size; i++) { b.data[i] = a.data[i] * value; -- GitLab