diff options
author | Pixel <pixel@nobis-crew.org> | 2009-11-04 11:56:41 -0800 |
---|---|---|
committer | Pixel <pixel@nobis-crew.org> | 2009-11-04 11:59:33 -0800 |
commit | d577d991b97ae2b5ee1af23641bcffc3f83af5b2 (patch) | |
tree | 590639d50205d1bcfaff2a7d2dc6ebf3f373c7ed /im/src/process/im_logic.cpp |
Initial import. Contains the im, cd and iup librairies, and a "working" Makefile for them under linux.
Diffstat (limited to 'im/src/process/im_logic.cpp')
-rwxr-xr-x | im/src/process/im_logic.cpp | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/im/src/process/im_logic.cpp b/im/src/process/im_logic.cpp new file mode 100755 index 0000000..82e607d --- /dev/null +++ b/im/src/process/im_logic.cpp @@ -0,0 +1,136 @@ +/** \file + * \brief Logical Arithmetic Operations + * + * See Copyright Notice in im_lib.h + * $Id: im_logic.cpp,v 1.1 2008/10/17 06:16:33 scuri Exp $ + */ + + +#include <im.h> +#include <im_util.h> + +#include "im_process_pon.h" + +#include <stdlib.h> +#include <memory.h> + +template <class T> +static void DoBitwiseOp(T *map1, T *map2, T *map, int count, int op) +{ + int i; + + switch(op) + { + case IM_BIT_AND: + for (i = 0; i < count; i++) + map[i] = map1[i] & map2[i]; + break; + case IM_BIT_OR: + for (i = 0; i < count; i++) + map[i] = map1[i] | map2[i]; + break; + case IM_BIT_XOR: + for (i = 0; i < count; i++) + map[i] = (T)~(map1[i] | map2[i]); + break; + } +} + +void imProcessBitwiseOp(const imImage* src_image1, const imImage* src_image2, imImage* dst_image, int op) +{ + int count = src_image1->count*src_image1->depth; + + switch(src_image1->data_type) + { + case IM_BYTE: + DoBitwiseOp((imbyte*)src_image1->data[0], (imbyte*)src_image2->data[0], (imbyte*)dst_image->data[0], count, op); + break; + case IM_USHORT: + DoBitwiseOp((imushort*)src_image1->data[0], (imushort*)src_image2->data[0], (imushort*)dst_image->data[0], count, op); + break; + case IM_INT: + DoBitwiseOp((int*)src_image1->data[0], (int*)src_image2->data[0], (int*)dst_image->data[0], count, op); + break; + } +} + +template <class T> +static void DoBitwiseNot(T *map1, T *map, int count) +{ + for (int i = 0; i < count; i++) + map[i] = ~map1[i]; +} + +static void DoBitwiseNotBin(imbyte *map1, imbyte *map, int count) +{ + for (int i = 0; i < count; i++) + map[i] = map1[i]? 0: 1; +} + +void imProcessBitwiseNot(const imImage* src_image, imImage* dst_image) +{ + int count = src_image->count*src_image->depth; + + if (dst_image->color_space == IM_BINARY) + { + DoBitwiseNotBin((imbyte*)src_image->data[0], (imbyte*)dst_image->data[0], count); + return; + } + + switch(src_image->data_type) + { + case IM_BYTE: + DoBitwiseNot((imbyte*)src_image->data[0], (imbyte*)dst_image->data[0], count); + break; + case IM_USHORT: + DoBitwiseNot((imushort*)src_image->data[0], (imushort*)dst_image->data[0], count); + break; + case IM_INT: + DoBitwiseNot((int*)src_image->data[0], (int*)dst_image->data[0], count); + break; + } +} + +void imProcessBitMask(const imImage* src_image, imImage* dst_image, unsigned char mask, int op) +{ + imbyte* src_map = (imbyte*)src_image->data[0]; + imbyte* dst_map = (imbyte*)dst_image->data[0]; + int i; + int count = dst_image->count * dst_image->depth; + switch(op) + { + case IM_BIT_AND: + for (i = 0; i < count; i++) + *dst_map++ = *src_map++ & mask; + break; + case IM_BIT_OR: + for (i = 0; i < count; i++) + *dst_map++ = *src_map++ | mask; + break; + case IM_BIT_XOR: + for (i = 0; i < count; i++) + *dst_map++ = (imbyte)~(*src_map++ | mask); + break; + } + + if ((op == IM_BIT_XOR || op == IM_BIT_OR) && dst_image->color_space == IM_BINARY && mask > 1) + dst_image->color_space = IM_GRAY; +} + +void imProcessBitPlane(const imImage* src_image, imImage* dst_image, int plane, int reset) +{ + imbyte mask = imbyte(0x01 << plane); + if (reset) mask = ~mask; + imbyte* src_map = (imbyte*)src_image->data[0]; + imbyte* dst_map = (imbyte*)dst_image->data[0]; + int count = dst_image->count * dst_image->depth; + for (int i = 0; i < count; i++) + { + if (reset) + *dst_map++ = *src_map & mask; + else + *dst_map++ = (*src_map & mask)? 1: 0; + + src_map++; + } +} |