void imProcessInsert(const imImage* src_image, const imImage* rgn_image, imImage* dst_image, int xmin, int ymin) { int type_size = imDataTypeSize(src_image->data_type); int dst_size1 = xmin*type_size; int dst_size2 = src_image->line_size - (rgn_image->line_size + dst_size1); int dst_offset2 = dst_size1+rgn_image->line_size; int ymax = ymin+rgn_image->height-1; int rgn_size, rgn_line_size = rgn_image->line_size; int src_line_size = src_image->line_size; int dst_line_size = dst_image->line_size; int src_depth = src_image->has_alpha? src_image->depth+1: src_image->depth; if (dst_size2 < 0) { dst_size2 = 0; rgn_size = src_line_size - dst_size1; dst_offset2 = dst_size1 + rgn_line_size; } else rgn_size = rgn_line_size; if (ymax > src_image->height-1) ymax = src_image->height-1; for (int i = 0; i < src_depth; i++) { imbyte *src_map = (imbyte*)src_image->data[i]; imbyte *rgn_map = (imbyte*)rgn_image->data[i]; imbyte *dst_map = (imbyte*)dst_image->data[i]; #ifdef _OPENMP #pragma omp parallel for if (IM_OMP_MINHEIGHT(src_image->height)) #endif for (int y = 0; y < src_image->height; y++) { if (y < ymin || y > ymax) { if (dst_map != src_map) // avoid in-place processing memcpy(dst_map + y*dst_line_size, src_map + y*src_line_size, src_line_size); } else { if (dst_size1) { if (dst_map != src_map) // avoid in-place processing memcpy(dst_map + y*dst_line_size, src_map + y*src_line_size, dst_size1); } memcpy(dst_map + y*dst_line_size + dst_size1, rgn_map + (y-ymin)*rgn_line_size, rgn_size); if (dst_size2) { if (dst_map != src_map) // avoid in-place processing memcpy(dst_map + y*dst_line_size + dst_offset2, src_map + y*src_line_size + dst_offset2, dst_size2); } } } } }
static int iFileDataTypeSize(int file_data_type, int switch_type) { int type_size = imDataTypeSize(file_data_type); if ((file_data_type == IM_FLOAT || file_data_type == IM_CFLOAT) && switch_type) type_size *= 2; return type_size; }
void imProcessAddMargins(const imImage* src_image, imImage* dst_image, int xmin, int ymin) { int type_size = imDataTypeSize(src_image->data_type); int src_depth = src_image->has_alpha? src_image->depth+1: src_image->depth; for (int i = 0; i < src_depth; i++) { imbyte *dst_map = (imbyte*)dst_image->data[i]; imbyte *src_map = (imbyte*)src_image->data[i]; #ifdef _OPENMP #pragma omp parallel for if (IM_OMP_MINHEIGHT(src_image->height)) #endif for (int y = 0; y < src_image->height; y++) { int src_offset = y*src_image->line_size; int dst_offset = (y + ymin)*dst_image->line_size + xmin*type_size; memcpy(&dst_map[dst_offset], &src_map[src_offset], src_image->line_size); } } }
/*****************************************************************************\ im.DataTypeSize(data_type) \*****************************************************************************/ static int imluaDataTypeSize (lua_State *L) { lua_pushinteger(L, imDataTypeSize(luaL_checkinteger(L, 1))); return 1; }
/*****************************************************************************\ file:SetAttribute(attrib, data_type, data) \*****************************************************************************/ static int imluaFileSetAttribute (lua_State *L) { int i, count = 0; void *data = NULL; imFile *ifile = imlua_checkfile(L, 1); const char *attrib = luaL_checkstring(L, 2); int data_type = luaL_checkinteger(L, 3); if (!lua_isnil(L, 4)) { if (!lua_isstring(L, 4)) { luaL_checktype(L, 4, LUA_TTABLE); count = imlua_getn(L, 4); data = malloc(imDataTypeSize(data_type) * count); } else if (data_type != IM_BYTE) luaL_argerror(L, 4, "if value is string, then data type must be byte"); switch (data_type) { case IM_BYTE: { if (lua_isstring(L, 4)) { const char* str = lua_tostring(L, 4); count = (int)strlen(str)+1; data = malloc(imDataTypeSize(data_type) * count); memcpy(data, str, count); } else { imbyte *d = (imbyte*) data; for (i = 0; i < count; i++) { lua_rawgeti(L, 4, i+1); d[i] = (imbyte) luaL_checkinteger(L, -1); lua_pop(L, 1); } } } break; case IM_SHORT: { short *d = (short*) data; for (i = 0; i < count; i++) { lua_rawgeti(L, 4, i+1); d[i] = (short) luaL_checkinteger(L, -1); lua_pop(L, 1); } } break; case IM_USHORT: { imushort *d = (imushort*) data; for (i = 0; i < count; i++) { lua_rawgeti(L, 4, i+1); d[i] = (imushort) luaL_checkinteger(L, -1); lua_pop(L, 1); } } break; case IM_INT: { int *d = (int*) data; for (i = 0; i < count; i++) { lua_rawgeti(L, 4, i+1); d[i] = luaL_checkinteger(L, -1); lua_pop(L, 1); } } break; case IM_FLOAT: { float *d = (float*) data; for (i = 0; i < count; i++) { lua_rawgeti(L, 4, i+1); d[i] = (float) luaL_checknumber(L, -1); lua_pop(L, 1); } } break; case IM_CFLOAT: { float *data_float = (float*) data; for (i = 0; i < count; i++) { int two; float *value = imlua_toarrayfloat(L, -1, &two, 1); if (two != 2) { free(value); luaL_argerror(L, 4, "invalid value"); } data_float[i] = value[0]; data_float[i+1] = value[1]; free(value); lua_pop(L, 1); } } break; case IM_DOUBLE: { double *d = (double*) data; for (i = 0; i < count; i++) { lua_rawgeti(L, 4, i+1); d[i] = (double)luaL_checknumber(L, -1); lua_pop(L, 1); } } break; case IM_CDOUBLE: { double *data_double = (double*) data; for (i = 0; i < count; i++) { int two; double *value = imlua_toarraydouble(L, -1, &two, 1); if (two != 2) { free(value); luaL_argerror(L, 4, "invalid value"); } data_double[i] = value[0]; data_double[i+1] = value[1]; free(value); lua_pop(L, 1); } } break; } } imFileSetAttribute(ifile, attrib, data_type, count, data); return 0; }
void PrintImageInfo(const char* file_name) { printf("IM Info\n"); printf(" File Name:\n %s\n", file_name); int error; imFile* ifile = imFileOpen(file_name, &error); if (!ifile) { PrintError(error); return; } double file_size = FileSize(file_name); printf(" File Size: %.2f %s\n", file_size, GetSizeDesc(&file_size)); char format[10]; char compression[20]; int image_count; imFileGetInfo(ifile, format, compression, &image_count); char format_desc[50]; imFormatInfo(format, format_desc, NULL, NULL); printf(" Format: %s - %s\n", format, format_desc); printf(" Compression: %s\n", compression); printf(" Image Count: %d\n", image_count); for (int i = 0; i < image_count; i++) { int width, height, color_mode, data_type; error = imFileReadImageInfo(ifile, i, &width, &height, &color_mode, &data_type); if (error != IM_ERR_NONE) { PrintError(error); imFileClose(ifile); return; } printf(" Image #%d\n", i); printf(" Width: %d\n", width); printf(" Height: %d\n", height); printf(" Color Space: %s\n", imColorModeSpaceName(color_mode)); printf(" Has Alpha: %s\n", imColorModeHasAlpha(color_mode)? "Yes": "No"); printf(" Is Packed: %s\n", imColorModeIsPacked(color_mode)? "Yes": "No"); printf(" Is Top Down: %s\n", imColorModeIsTopDown(color_mode)? "Yes": "No"); printf(" Data Type: %s\n", imDataTypeName(data_type)); double image_size = imImageDataSize(width, height, color_mode, data_type); printf(" Data Size: %.2f %s\n", image_size, GetSizeDesc(&image_size)); char* attrib_list[50]; // should be dynamic allocated int attrib_list_count; imFileGetAttributeList(ifile, attrib_list, &attrib_list_count); for (int a = 0; a < attrib_list_count; a++) { if (a == 0) printf(" Attributes:\n"); int attrib_data_type, attrib_count; const void* attrib_data = imFileGetAttribute(ifile, attrib_list[a], &attrib_data_type, &attrib_count); if (attrib_count == 1) printf(" %s: %s\n", attrib_list[a], AttribData2Str(attrib_data, attrib_data_type)); else if (attrib_data_type == IM_BYTE && FindZero((imbyte*)attrib_data, attrib_count)) printf(" %s: %s\n", attrib_list[a], attrib_data); else printf(" %s: %s %s ...\n", attrib_list[a], AttribData2Str(attrib_data, attrib_data_type), AttribData2Str((imbyte*)attrib_data + imDataTypeSize(attrib_data_type), attrib_data_type)); } } imFileClose(ifile); }