int imProcessGrayMorphConvolve(const imImage* src_image, imImage* dst_image, const imImage *kernel, int ismax) { int ret = 0; int counter = imProcessCounterBegin("Gray Morphological Convolution"); const char* msg = (const char*)imImageGetAttribute(kernel, "Description", NULL, NULL); if (!msg) msg = "Processing..."; imCounterTotal(counter, src_image->depth*src_image->height, msg); imImage* fkernel = NULL; if ((src_image->data_type == IM_FLOAT || src_image->data_type == IM_DOUBLE) && kernel->data_type != src_image->data_type) { fkernel = imImageCreate(kernel->width, kernel->height, IM_GRAY, src_image->data_type); imProcessConvertDataType(kernel, fkernel, 0, 0, 0, IM_CAST_DIRECT); kernel = fkernel; } for (int i = 0; i < src_image->depth; i++) { switch(src_image->data_type) { case IM_BYTE: ret = DoGrayMorphConvolve((imbyte*)src_image->data[i], (imbyte*)dst_image->data[i], src_image->width, src_image->height, kernel, counter, ismax, (int)0); break; case IM_SHORT: ret = DoGrayMorphConvolve((short*)src_image->data[i], (short*)dst_image->data[i], src_image->width, src_image->height, kernel, counter, ismax, (int)0); break; case IM_USHORT: ret = DoGrayMorphConvolve((imushort*)src_image->data[i], (imushort*)dst_image->data[i], src_image->width, src_image->height, kernel, counter, ismax, (int)0); break; case IM_INT: ret = DoGrayMorphConvolve((int*)src_image->data[i], (int*)dst_image->data[i], src_image->width, src_image->height, kernel, counter, ismax, (int)0); break; case IM_FLOAT: ret = DoGrayMorphConvolve((float*)src_image->data[i], (float*)dst_image->data[i], src_image->width, src_image->height, kernel, counter, ismax, (float)0); break; case IM_DOUBLE: ret = DoGrayMorphConvolve((double*)src_image->data[i], (double*)dst_image->data[i], src_image->width, src_image->height, kernel, counter, ismax, (double)0); break; } if (!ret) break; } if (fkernel) imImageDestroy(fkernel); imProcessCounterEnd(counter); return ret; }
int canvas_action_cb(Ihandle* canvas) { int x, y, canvas_width, canvas_height; void* gldata; unsigned int ri, gi, bi; imImage* image; Ihandle* config = (Ihandle*)IupGetAttribute(canvas, "CONFIG"); const char* background = IupConfigGetVariableStrDef(config, "MainWindow", "Background", "255 255 255"); IupGetIntInt(canvas, "DRAWSIZE", &canvas_width, &canvas_height); IupGLMakeCurrent(canvas); /* OpenGL configuration */ glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* image data alignment is 1 */ glViewport(0, 0, canvas_width, canvas_height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, canvas_width, 0, canvas_height, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* draw the background */ sscanf(background, "%u %u %u", &ri, &gi, &bi); glClearColor(ri / 255.f, gi / 255.f, bi / 255.f, 1); glClear(GL_COLOR_BUFFER_BIT); /* draw the image at the center of the canvas */ image = (imImage*)IupGetAttribute(canvas, "IMAGE"); if (image) { x = (canvas_width - image->width) / 2; y = (canvas_height - image->height) / 2; gldata = (void*)imImageGetAttribute(image, "GLDATA", NULL, NULL); glRasterPos2i(x, y); /* this will not work for negative values, OpenGL limitation */ glDrawPixels(image->width, image->height, GL_RGB, GL_UNSIGNED_BYTE, gldata); /* no zoom support, must use texture */ } IupGLSwapBuffers(canvas); return IUP_DEFAULT; }
void* imImageGetOpenGLData(const imImage* image, int *format) { if (!imImageIsBitmap(image)) return NULL; int transp_count; imbyte* transp_index = (imbyte*)imImageGetAttribute(image, "TransparencyIndex", NULL, NULL); imbyte* transp_map = (imbyte*)imImageGetAttribute(image, "TransparencyMap", NULL, &transp_count); imbyte* transp_color = (imbyte*)imImageGetAttribute(image, "TransparencyColor", NULL, NULL); int glformat; switch(image->color_space) { case IM_MAP: if (image->has_alpha || transp_map || transp_index) glformat = GL_RGBA; else glformat = GL_RGB; break; case IM_RGB: if (image->has_alpha || transp_color) glformat = GL_RGBA; else glformat = GL_RGB; break; case IM_BINARY: default: /* IM_GRAY */ if (image->has_alpha || transp_map || transp_index) glformat = GL_LUMINANCE_ALPHA; else glformat = GL_LUMINANCE; break; } int gldepth; switch (glformat) { case GL_RGB: gldepth = 3; break; case GL_RGBA: gldepth = 4; break; case GL_LUMINANCE_ALPHA: gldepth = 2; break; default: /* GL_LUMINANCE */ gldepth = 1; break; } int size = image->count*gldepth; imImageSetAttribute(image, "GLDATA", IM_BYTE, size, NULL); imbyte* gldata = (imbyte*)imImageGetAttribute(image, "GLDATA", NULL, NULL); int depth = image->depth; if (image->has_alpha) depth++; /* copy data, including alpha */ if (image->color_space != IM_MAP) { imConvertPacking(image->data[0], gldata, image->width, image->height, depth, gldepth, IM_BYTE, 0); if (image->color_space == IM_BINARY) iImageMakeGray(gldata, gldepth, image->count); } else { /* copy map data */ memcpy(gldata, image->data[0], image->size); /* size does not include alpha */ /* expand MAP to RGB or RGBA */ imConvertMapToRGB(gldata, image->count, gldepth, 1, image->palette, image->palette_count); if (image->has_alpha) iImageGLCopyMapAlpha((imbyte*)image->data[1], gldata, gldepth, image->count); /* copy the alpha plane */ } /* set alpha based on attributes */ if (!image->has_alpha) { if (image->color_space == IM_RGB) { if (transp_color) iImageGLSetTranspColor(gldata, image->count, *(transp_color+0), *(transp_color+1), *(transp_color+2)); } else { if (transp_map) iImageGLSetTranspMap((imbyte*)image->data[0], gldata, image->count, transp_map, transp_count); else if (transp_index) iImageGLSetTranspIndex((imbyte*)image->data[0], gldata, gldepth, image->count, *transp_index); } } if (format) *format = glformat; return gldata; }
/*****************************************************************************\ image:GetAttribute(attrib) \*****************************************************************************/ static int imluaImageGetAttribute (lua_State *L) { int data_type; int i, count; const void *data; int as_string = 0; imImage* image = imlua_checkimage(L, 1); const char *attrib = luaL_checkstring(L, 2); data = imImageGetAttribute(image, attrib, &data_type, &count); if (!data) { lua_pushnil(L); return 1; } if (data_type == IM_BYTE && lua_isboolean(L, 3)) as_string = lua_toboolean(L, 3); if (!as_string) lua_newtable(L); switch (data_type) { case IM_BYTE: { if (as_string) { lua_pushstring(L, (const char*)data); } else { imbyte *data_byte = (imbyte*) data; for (i = 0; i < count; i++, data_byte++) { lua_pushnumber(L, *data_byte); lua_rawseti(L, -2, i+1); } } } break; case IM_SHORT: { short *data_short = (short*) data; for (i = 0; i < count; i++, data_short += 2) { lua_pushnumber(L, *data_short); lua_rawseti(L, -2, i+1); } } break; case IM_USHORT: { imushort *data_ushort = (imushort*) data; for (i = 0; i < count; i++, data_ushort += 2) { lua_pushnumber(L, *data_ushort); lua_rawseti(L, -2, i+1); } } break; case IM_INT: { int *data_int = (int*) data; for (i = 0; i < count; i++, data_int++) { lua_pushnumber(L, *data_int); lua_rawseti(L, -2, i+1); } } break; case IM_FLOAT: { float *data_float = (float*) data; for (i = 0; i < count; i++, data_float++) { lua_pushnumber(L, *data_float); lua_rawseti(L, -2, i+1); } } break; case IM_CFLOAT: { float *data_float = (float*) data; for (i = 0; i < count; i++, data_float += 2) { imlua_newarrayfloat(L, data_float, 2, 1); lua_rawseti(L, -2, i+1); } } break; } lua_pushnumber(L, data_type); return 2; }