diff options
author | scuri <scuri> | 2009-10-01 02:56:38 +0000 |
---|---|---|
committer | scuri <scuri> | 2009-10-01 02:56:38 +0000 |
commit | 62783aee16f96fe5e513fb230b8efddaa02981df (patch) | |
tree | 9dc512b0c758025c5cddba9709420f1bf9058675 /src/process | |
parent | 9a5e93213e08601a58725f44035ac622fb68e849 (diff) |
New: functions imProcessUnsharp and imProcessSharp.
Changed: now imProcessUnArithmeticOp, imProcessArithmeticConstOp and imProcessArithmeticOp willl crop the result to 0-255 if destiny has data type byte.
Changed: removed IM_UN_INC operation from imProcessUnArithmeticOp. It was not an unary operation. Can simply be done in place by imProcessArithmeticOp and IM_BIN_ADD.
Diffstat (limited to 'src/process')
-rw-r--r-- | src/process/im_arithmetic_bin.cpp | 98 | ||||
-rw-r--r-- | src/process/im_arithmetic_un.cpp | 64 | ||||
-rw-r--r-- | src/process/im_convolve.cpp | 190 |
3 files changed, 282 insertions, 70 deletions
diff --git a/src/process/im_arithmetic_bin.cpp b/src/process/im_arithmetic_bin.cpp index 74fe010..494b6c0 100644 --- a/src/process/im_arithmetic_bin.cpp +++ b/src/process/im_arithmetic_bin.cpp @@ -2,7 +2,7 @@ * \brief Binary Arithmetic Operations * * See Copyright Notice in im_lib.h - * $Id: im_arithmetic_bin.cpp,v 1.1 2008/10/17 06:16:33 scuri Exp $ + * $Id: im_arithmetic_bin.cpp,v 1.2 2009/10/01 02:56:58 scuri Exp $ */ @@ -61,6 +61,47 @@ static void DoBinaryOp(T1 *map1, T2 *map2, T3 *map, int count, int op) } } +static void DoBinaryOpByte(imbyte *map1, imbyte *map2, imbyte *map, int count, int op) +{ + int i; + + switch(op) + { + case IM_BIN_ADD: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(add_op((int)map1[i], (int)map2[i])); + break; + case IM_BIN_SUB: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(sub_op((int)map1[i], (int)map2[i])); + break; + case IM_BIN_MUL: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(mul_op((int)map1[i], (int)map2[i])); + break; + case IM_BIN_DIV: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(div_op((int)map1[i], (int)map2[i])); + break; + case IM_BIN_DIFF: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(diff_op((int)map1[i], (int)map2[i])); + break; + case IM_BIN_MIN: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(min_op((int)map1[i], (int)map2[i])); + break; + case IM_BIN_MAX: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(max_op((int)map1[i], (int)map2[i])); + break; + case IM_BIN_POW: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(pow_op((int)map1[i], (int)map2[i])); + break; + } +} + static void DoBinaryOpCpxReal(imcfloat *map1, float *map2, imcfloat *map, int count, int op) { int i; @@ -118,7 +159,7 @@ void imProcessArithmeticOp(const imImage* src_image1, const imImage* src_image2, else if (dst_image->data_type == IM_INT) DoBinaryOp((imbyte*)src_image1->data[i], (imbyte*)src_image2->data[i], (int*)dst_image->data[i], count, op); else - DoBinaryOp((imbyte*)src_image1->data[i], (imbyte*)src_image2->data[i], (imbyte*)dst_image->data[i], count, op); + DoBinaryOpByte((imbyte*)src_image1->data[i], (imbyte*)src_image2->data[i], (imbyte*)dst_image->data[i], count, op); break; case IM_USHORT: if (dst_image->data_type == IM_FLOAT) @@ -304,6 +345,48 @@ static void DoBinaryConstOp(T1 *map1, T2 value, T3 *map, int count, int op) } } +template <class T1> +static void DoBinaryConstOpByte(T1 *map1, int value, imbyte *map, int count, int op) +{ + int i; + + switch(op) + { + case IM_BIN_ADD: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(add_op((int)map1[i], value)); + break; + case IM_BIN_SUB: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(sub_op((int)map1[i], value)); + break; + case IM_BIN_MUL: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(mul_op((int)map1[i], value)); + break; + case IM_BIN_DIV: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(div_op((int)map1[i], value)); + break; + case IM_BIN_DIFF: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(diff_op((int)map1[i], value)); + break; + case IM_BIN_MIN: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(min_op((int)map1[i], value)); + break; + case IM_BIN_MAX: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(max_op((int)map1[i], value)); + break; + case IM_BIN_POW: + for (i = 0; i < count; i++) + map[i] = (imbyte)crop_byte(pow_op((int)map1[i], value)); + break; + } +} + void imProcessArithmeticConstOp(const imImage* src_image1, float value, imImage* dst_image, int op) { int count = src_image1->count; @@ -320,7 +403,7 @@ void imProcessArithmeticConstOp(const imImage* src_image1, float value, imImage* else if (dst_image->data_type == IM_INT) DoBinaryConstOp((imbyte*)src_image1->data[i], (int)value, (int*)dst_image->data[i], count, op); else - DoBinaryConstOp((imbyte*)src_image1->data[i], (imushort)value, (imbyte*)dst_image->data[i], count, op); + DoBinaryConstOpByte((imbyte*)src_image1->data[i], (int)value, (imbyte*)dst_image->data[i], count, op); break; case IM_USHORT: if (dst_image->data_type == IM_FLOAT) @@ -328,7 +411,7 @@ void imProcessArithmeticConstOp(const imImage* src_image1, float value, imImage* else if (dst_image->data_type == IM_INT) DoBinaryConstOp((imushort*)src_image1->data[i], (int)value, (int*)dst_image->data[i], count, op); else if (dst_image->data_type == IM_BYTE) - DoBinaryConstOp((imushort*)src_image1->data[i], (imushort)value, (imbyte*)dst_image->data[i], count, op); + DoBinaryConstOpByte((imushort*)src_image1->data[i], (int)value, (imbyte*)dst_image->data[i], count, op); else DoBinaryConstOp((imushort*)src_image1->data[i], (imushort)value, (imushort*)dst_image->data[i], count, op); break; @@ -338,7 +421,7 @@ void imProcessArithmeticConstOp(const imImage* src_image1, float value, imImage* else if (dst_image->data_type == IM_USHORT) DoBinaryConstOp((int*)src_image1->data[i], (int)value, (imushort*)dst_image->data[i], count, op); else if (dst_image->data_type == IM_BYTE) - DoBinaryConstOp((int*)src_image1->data[i], (int)value, (imbyte*)dst_image->data[i], count, op); + DoBinaryConstOpByte((int*)src_image1->data[i], (int)value, (imbyte*)dst_image->data[i], count, op); else DoBinaryConstOp((int*)src_image1->data[i], (int)value, (int*)dst_image->data[i], count, op); break; @@ -367,7 +450,8 @@ void imProcessMultipleMean(const imImage** src_image_list, int src_image_count, for(int i = 0; i < src_image_count; i++) { const imImage *image = src_image_list[i]; - imProcessUnArithmeticOp(image, acum_image, IM_UN_INC); + imProcessArithmeticOp(image, acum_image, acum_image, IM_BIN_ADD); /* acum_image += image */ + } imProcessArithmeticConstOp(acum_image, float(src_image_count), dst_image, IM_BIN_DIV); @@ -393,7 +477,7 @@ void imProcessMultipleStdDev(const imImage** src_image_list, int src_image_count imProcessUnArithmeticOp(aux_image, aux_image, IM_UN_SQR); // dst_image += aux_image - imProcessUnArithmeticOp(aux_image, dst_image, IM_UN_INC); + imProcessArithmeticOp(aux_image, dst_image, dst_image, IM_BIN_ADD); } // dst_image = dst_image / src_image_count; diff --git a/src/process/im_arithmetic_un.cpp b/src/process/im_arithmetic_un.cpp index 59e384c..e4dba8a 100644 --- a/src/process/im_arithmetic_un.cpp +++ b/src/process/im_arithmetic_un.cpp @@ -2,7 +2,7 @@ * \brief Unary Arithmetic Operations * * See Copyright Notice in im_lib.h - * $Id: im_arithmetic_un.cpp,v 1.1 2008/10/17 06:16:33 scuri Exp $ + * $Id: im_arithmetic_un.cpp,v 1.2 2009/10/01 02:56:58 scuri Exp $ */ @@ -71,10 +71,6 @@ static void DoUnaryOp(T1 *map, T2 *new_map, int count, int op) for (i = 0; i < count; i++) new_map[i] = (T2)map[i]; break; - case IM_UN_INC: - for (i = 0; i < count; i++) - new_map[i] = (T2)(new_map[i] + map[i]); - break; case IM_UN_LESS: for (i = 0; i < count; i++) new_map[i] = less_op((T2)map[i]); @@ -85,7 +81,7 @@ static void DoUnaryOp(T1 *map, T2 *new_map, int count, int op) break; case IM_UN_SQRT: for (i = 0; i < count; i++) - new_map[i] = (T2)sqrt_op(map[i]); + new_map[i] = sqrt_op((T2)map[i]); break; case IM_UN_LOG: for (i = 0; i < count; i++) @@ -114,6 +110,56 @@ static void DoUnaryOp(T1 *map, T2 *new_map, int count, int op) } } +template <class T1> +static void DoUnaryOpByte(T1 *map, imbyte *new_map, int count, int op) +{ + int i; + + switch(op) + { + case IM_UN_ABS: + for (i = 0; i < count; i++) + new_map[i] = (imbyte)crop_byte(abs_op((int)map[i])); + break; + case IM_UN_INV: + for (i = 0; i < count; i++) + new_map[i] = (imbyte)crop_byte(inv_op((int)map[i])); /* will always be 0 */ + break; + case IM_UN_EQL: + for (i = 0; i < count; i++) + new_map[i] = (imbyte)crop_byte((int)map[i]); + break; + case IM_UN_LESS: + for (i = 0; i < count; i++) + new_map[i] = (imbyte)crop_byte(less_op((int)map[i])); + break; + case IM_UN_SQR: + for (i = 0; i < count; i++) + new_map[i] = (imbyte)crop_byte(sqr_op((int)map[i])); + break; + case IM_UN_SQRT: + for (i = 0; i < count; i++) + new_map[i] = (imbyte)crop_byte(sqrt_op((int)map[i])); + break; + case IM_UN_LOG: + for (i = 0; i < count; i++) + new_map[i] = (imbyte)crop_byte(log_op((int)map[i])); + break; + case IM_UN_SIN: + for (i = 0; i < count; i++) + new_map[i] = (imbyte)crop_byte(sin_op((int)map[i])); + break; + case IM_UN_COS: + for (i = 0; i < count; i++) + new_map[i] = (imbyte)crop_byte(cos_op((int)map[i])); + break; + case IM_UN_EXP: + for (i = 0; i < count; i++) + new_map[i] = (imbyte)crop_byte(exp_op((int)map[i])); + break; + } +} + void imProcessUnArithmeticOp(const imImage* src_image, imImage* dst_image, int op) { int total_count = src_image->count * src_image->depth; @@ -128,11 +174,11 @@ void imProcessUnArithmeticOp(const imImage* src_image, imImage* dst_image, int o else if (dst_image->data_type == IM_USHORT) DoUnaryOp((imbyte*)src_image->data[0], (imushort*)dst_image->data[0], total_count, op); else - DoUnaryOp((imbyte*)src_image->data[0], (imbyte*)dst_image->data[0], total_count, op); + DoUnaryOpByte((imbyte*)src_image->data[0], (imbyte*)dst_image->data[0], total_count, op); break; case IM_USHORT: if (dst_image->data_type == IM_BYTE) - DoUnaryOp((imushort*)src_image->data[0], (imbyte*)dst_image->data[0], total_count, op); + DoUnaryOpByte((imushort*)src_image->data[0], (imbyte*)dst_image->data[0], total_count, op); else if (dst_image->data_type == IM_INT) DoUnaryOp((imushort*)src_image->data[0], (int*)dst_image->data[0], total_count, op); else if (dst_image->data_type == IM_FLOAT) @@ -142,7 +188,7 @@ void imProcessUnArithmeticOp(const imImage* src_image, imImage* dst_image, int o break; case IM_INT: if (dst_image->data_type == IM_BYTE) - DoUnaryOp((int*)src_image->data[0], (imbyte*)dst_image->data[0], total_count, op); + DoUnaryOpByte((int*)src_image->data[0], (imbyte*)dst_image->data[0], total_count, op); else if (dst_image->data_type == IM_USHORT) DoUnaryOp((int*)src_image->data[0], (imushort*)dst_image->data[0], total_count, op); else if (dst_image->data_type == IM_FLOAT) diff --git a/src/process/im_convolve.cpp b/src/process/im_convolve.cpp index bca2dcd..a88a25c 100644 --- a/src/process/im_convolve.cpp +++ b/src/process/im_convolve.cpp @@ -2,7 +2,7 @@ * \brief Convolution Operations * * See Copyright Notice in im_lib.h - * $Id: im_convolve.cpp,v 1.1 2008/10/17 06:16:33 scuri Exp $ + * $Id: im_convolve.cpp,v 1.2 2009/10/01 02:56:58 scuri Exp $ */ @@ -1426,59 +1426,6 @@ int imProcessDiffOfGaussianConvolve(const imImage* src_image, imImage* dst_image return 1; } -#ifdef _TEST_CODE_ -int imProcessDiffOfGaussianConvolveTEST(const imImage* src_image, imImage* dst_image, float stddev1, float stddev2) -{ - int kernel_size1 = imGaussianStdDev2KernelSize(stddev1); - int kernel_size2 = imGaussianStdDev2KernelSize(stddev2); - int size = kernel_size1; - if (kernel_size1 < kernel_size2) size = kernel_size2; - - imImage* kernel1 = imImageCreate(size, size, IM_GRAY, IM_FLOAT); - imImage* kernel2 = imImageCreate(size, size, IM_GRAY, IM_FLOAT); - if (!kernel1 || !kernel2) - { - if (kernel1) imImageDestroy(kernel1); - if (kernel2) imImageDestroy(kernel2); - return 0; - } - - imImageSetAttribute(kernel1, "Description", IM_BYTE, -1, (void*)"Gaussian"); - imImageSetAttribute(kernel2, "Description", IM_BYTE, -1, (void*)"Gaussian"); - - imProcessRenderGaussian(kernel1, stddev1); - imProcessRenderGaussian(kernel2, stddev2); - - // ERROR: kernel 1 should be multiplied by a factor to improve the difference. - - imProcessArithmeticOp(kernel1, kernel2, kernel1, IM_BIN_SUB); - imImageSetAttribute(kernel1, "Description", IM_BYTE, -1, (void*)"Difference of Gaussian"); - - int ret = 0; - if (src_image->data_type == IM_BYTE || src_image->data_type == IM_USHORT) - { - imImage* aux_image = imImageClone(dst_image); - if (!aux_image) - { - imImageDestroy(kernel1); - imImageDestroy(kernel2); - return 0; - } - - imProcessUnArithmeticOp(src_image, aux_image, IM_UN_EQL); // Convert to IM_INT - ret = imProcessConvolve(aux_image, dst_image, kernel1); - imImageDestroy(aux_image); - } - else - ret = imProcessConvolve(src_image, dst_image, kernel1); - - imImageDestroy(kernel1); - imImageDestroy(kernel2); - - return ret; -} -#endif - int imProcessMeanConvolve(const imImage* src_image, imImage* dst_image, int ks) { int counter = imCounterBegin("Mean Convolve"); @@ -1510,3 +1457,138 @@ int imProcessMeanConvolve(const imImage* src_image, imImage* dst_image, int ks) return ret; } + +template <class T1, class T2> +static void DoSharpOp(T1 *src_map, T1 *dst_map, int count, float amount, T2 threshold, int gauss) +{ + int i; + T1 min, max; + + int size_of = sizeof(imbyte); + if (sizeof(T1) == size_of) + { + min = 0; + max = 255; + } + else + { + imMinMax(src_map, count, min, max); + + if (min == max) + { + max = min + 1; + + if (min != 0) + min = min - 1; + } + } + + for (i = 0; i < count; i++) + { + T2 diff; + + if (gauss) + diff = 20*(src_map[i] - dst_map[i]); /* dst_map contains a gaussian filter of the source image, must compensate for small edge values */ + else + diff = dst_map[i]; /* dst_map contains a laplacian filter of the source image */ + + if (threshold && abs_op(2*diff) < threshold) + diff = 0; + + T2 value = (T2)(src_map[i] + amount*diff); + if (value < min) + value = min; + else if (value > max) + value = max; + + dst_map[i] = (T1)value; + } +} + +static void doSharp(const imImage* src_image, imImage* dst_image, float amount, float threshold, int gauss) +{ + int count = src_image->count; + + for (int i = 0; i < src_image->depth; i++) + { + switch(src_image->data_type) + { + case IM_BYTE: + DoSharpOp((imbyte*)src_image->data[i], (imbyte*)dst_image->data[i], count, amount, (int)threshold, gauss); + break; + case IM_USHORT: + DoSharpOp((imushort*)src_image->data[i], (imushort*)dst_image->data[i], count, amount, (int)threshold, gauss); + break; + case IM_INT: + DoSharpOp((int*)src_image->data[i], (int*)dst_image->data[i], count, amount, (int)threshold, gauss); + break; + case IM_FLOAT: + DoSharpOp((float*)src_image->data[i], (float*)dst_image->data[i], count, amount, (float)threshold, gauss); + break; + } + } +} + +int imProcessUnsharp(const imImage* src_image, imImage* dst_image, float stddev, float amount, float threshold) +{ + int kernel_size = imGaussianStdDev2KernelSize(stddev); + + imImage* kernel = imImageCreate(kernel_size, kernel_size, IM_GRAY, IM_FLOAT); + if (!kernel) + return 0; + + imImageSetAttribute(kernel, "Description", IM_BYTE, -1, (void*)"Unsharp"); + imProcessRenderGaussian(kernel, stddev); + + int ret = imProcessConvolveSep(src_image, dst_image, kernel); + doSharp(src_image, dst_image, amount, threshold, 1); + + imImageDestroy(kernel); + + return ret; +} + +int imProcessSharp(const imImage* src_image, imImage* dst_image, float amount, float threshold) +{ + imImage* kernel = imKernelLaplacian8(); + if (!kernel) + return 0; + + int ret = imProcessConvolve(src_image, dst_image, kernel); + doSharp(src_image, dst_image, amount, threshold, 0); + + imImageDestroy(kernel); + + return ret; +} + +static int iProcessCheckKernelType(const imImage* kernel) +{ + if (kernel->data_type == IM_INT) + { + int* kernel_data = (int*)kernel->data[0]; + for (int i = 0; i < kernel->count; i++) + { + if (kernel_data[i] < 0) /* if there are negative values, assume kernel is an edge detector */ + return 0; + } + } + else if (kernel->data_type == IM_FLOAT) + { + float* kernel_data = (float*)kernel->data[0]; + for (int i = 0; i < kernel->count; i++) + { + if (kernel_data[i] < 0) /* if there are negative values, assume kernel is an edge detector */ + return 0; + } + } + return 1; /* default is kernel is a smooth filter */ +} + +int imProcessSharpKernel(const imImage* src_image, const imImage* kernel, imImage* dst_image, float amount, float threshold) +{ + int ret = imProcessConvolve(src_image, dst_image, kernel); + doSharp(src_image, dst_image, amount, threshold, iProcessCheckKernelType(kernel)); + return ret; +} + |