int imImageInfo(char *filename, int *width, int *height, int *type, int *palette_count) { int error; imFile* ifile = imFileOpen(filename, &error); if (!ifile) return error; int data_type, color_mode; error = imFileReadImageInfo(ifile, 0, width, height, &color_mode, &data_type); if (error) { imFileClose(ifile); return error; } *type = ColorMode2Type(color_mode); if (*type == -1) { imFileClose(ifile); return IM_ERR_DATA; } if (*type == IM_MAP) { long palette[256]; imFileGetPalette(ifile, palette, palette_count); } imFileClose(ifile); return IM_ERR_NONE; }
int imLoadMap(char *filename, unsigned char *map, long *palette) { int error; imFile* ifile = imFileOpen(filename, &error); if (!ifile) return error; int width, height, color_mode, data_type; error = imFileReadImageInfo(ifile, 0, &width, &height, &color_mode, &data_type); if (error) { imFileClose(ifile); return error; } if (imColorModeSpace(color_mode) != IM_MAP && imColorModeSpace(color_mode) != IM_GRAY && imColorModeSpace(color_mode) != IM_BINARY) return IM_ERR_DATA; if (iOldResolutionCB) { double xres = *(float*)imFileGetAttribute(ifile, "XResolution", NULL, NULL); double yres = *(float*)imFileGetAttribute(ifile, "YResolution", NULL, NULL); int res_unit = *(int*)imFileGetAttribute(ifile, "ResolutionUnit", NULL, NULL); iOldResolutionCB(filename, &xres, &yres, &res_unit); } if (iOldTiffImageDescCB) { char* img_desc = (char*)imFileGetAttribute(ifile, "Description", NULL, NULL); iOldTiffImageDescCB(filename, img_desc); } if (iOldGifTranspIndexCB) { unsigned char transp_index = *(unsigned char*)imFileGetAttribute(ifile, "TransparencyIndex", NULL, NULL); iOldGifTranspIndexCB(filename, &transp_index); } if (iOldCounterCB) imCounterSetCallback(filename, iOldFileCounter); error = imFileReadImageData(ifile, map, 1, 0); if (error) { imFileClose(ifile); return error; } int palette_count; imFileGetPalette(ifile, palette, &palette_count); imFileClose(ifile); return IM_ERR_NONE; }
int imSaveMap(int width, int height, int format, unsigned char *map, int palette_count, long *palette, char *filename) { int error; char* new_format = i_format_old2new[format & 0x00FF]; imFile* ifile = imFileNew(filename, new_format, &error); if (!ifile) return error; if (format & 0xFF00) imFileSetInfo(ifile, NULL); else imFileSetInfo(ifile, "NONE"); imFileSetPalette(ifile, palette, palette_count); if (iOldResolutionCB) { double xres, yres; int res_unit; iOldResolutionCB(filename, &xres, &yres, &res_unit); float fxres=(float)xres, fyres=(float)yres; imFileSetAttribute(ifile, "XResolution", IM_FLOAT, 1, (void*)&fxres); imFileSetAttribute(ifile, "YResolution", IM_FLOAT, 1, (void*)&fyres); imFileSetAttribute(ifile, "ResolutionUnit", IM_INT, 1, (void*)&res_unit); } if (iOldTiffImageDescCB) { char img_desc[50]; iOldTiffImageDescCB(filename, img_desc); imFileSetAttribute(ifile, "Description", IM_BYTE, -1, (void*)img_desc); } if (iOldGifTranspIndexCB) { unsigned char transp_index; iOldGifTranspIndexCB(filename, &transp_index); imFileSetAttribute(ifile, "TransparencyIndex", IM_BYTE, 1, (void*)&transp_index); } error = imFileWriteImageInfo(ifile, width, height, IM_MAP, IM_BYTE); if (error) { imFileClose(ifile); return error; } if (iOldCounterCB) imCounterSetCallback(filename, iOldFileCounter); error = imFileWriteImageData(ifile, map); imFileClose(ifile); return error; }
/*****************************************************************************\ gc \*****************************************************************************/ static int imluaFile_gc (lua_State *L) { imFile **ifile_p = (imFile **)lua_touserdata(L, 1); if (ifile_p && *ifile_p) { imFileClose(*ifile_p); *ifile_p = NULL; /* mark as closed */ } return 0; }
/*****************************************************************************\ file:Close() \*****************************************************************************/ static int imluaFileClose (lua_State *L) { imFile** ifile_p = imlua_rawcheckfile(L, 1); if (!(*ifile_p)) luaL_argerror(L, 1, "closed imFile"); imFileClose(*ifile_p); *ifile_p = NULL; /* mark as closed */ return 0; }
int app_open_cb(Ihandle* self) { imFile* ifile; /* file input */ int ret, error; unsigned char* gl_data = (unsigned char*)IupGetAttribute(self, "APP_GL_DATA"); char filename[1024] = ".\\*.*"; /* get a file name */ ret = IupGetFile(filename); if (ret == -1) return IUP_DEFAULT; ifile = imFileOpen(filename, &error); if (!ifile) { IupMessage("Error", "Error reading image file."); return IUP_DEFAULT; } { int width = 0, height = 0, file_color_mode, color_space; Ihandle* dialog = IupGetDialog(self); imFileReadImageInfo(ifile, 0, &width, &height, &file_color_mode, NULL); /* alocates the buffers */ if (gl_data) free(gl_data); gl_data = malloc(width*height*3); IupSetAttribute(dialog, "APP_GL_DATA", gl_data); IupSetfAttribute(dialog, "APP_GL_WIDTH", "%d", width); IupSetfAttribute(dialog, "APP_GL_HEIGHT", "%d", height); imFileReadImageData(ifile, gl_data, 1, IM_PACKED); color_space = imColorModeSpace(file_color_mode); if (color_space == IM_MAP || color_space == IM_GRAY || color_space == IM_BINARY) { long palette[256]; int palette_count; imFileGetPalette(ifile, palette, &palette_count); ConvertMapToGLData(gl_data, width*height, 3, palette, palette_count); } } imFileClose(ifile); return IUP_DEFAULT; }
int imFileFormat(char *filename, int* format) { char new_format[10], compression[10]; int error, image_count; imFile* ifile = imFileOpen(filename, &error); if (!ifile) return error; imFileGetInfo(ifile, new_format, compression, &image_count); imFileClose(ifile); *format = FormatNew2Old(new_format, compression); if (*format == -1) return IM_ERR_FORMAT; return IM_ERR_NONE; }
int imSaveRGB(int width, int height, int format, unsigned char *red, unsigned char *green, unsigned char *blue, char *filename) { int error; char* new_format = i_format_old2new[format & 0x00FF]; imFile* ifile = imFileNew(filename, new_format, &error); if (!ifile) return error; if (format & 0xFF00) imFileSetInfo(ifile, NULL); else imFileSetInfo(ifile, "NONE"); if (iOldResolutionCB) { double xres, yres; int res_unit; iOldResolutionCB(filename, &xres, &yres, &res_unit); float fxres=(float)xres, fyres=(float)yres; imFileSetAttribute(ifile, "XResolution", IM_FLOAT, 1, (void*)&fxres); imFileSetAttribute(ifile, "YResolution", IM_FLOAT, 1, (void*)&fyres); imFileSetAttribute(ifile, "ResolutionUnit", IM_INT, 1, (void*)&res_unit); } if (iOldTiffImageDescCB) { char img_desc[50]; iOldTiffImageDescCB(filename, img_desc); imFileSetAttribute(ifile, "Description", IM_BYTE, -1, (void*)img_desc); } if (iOldGifTranspIndexCB) { unsigned char transp_index; iOldGifTranspIndexCB(filename, &transp_index); imFileSetAttribute(ifile, "TransparencyIndex", IM_BYTE, 1, (void*)&transp_index); } error = imFileWriteImageInfo(ifile, width, height, IM_RGB, IM_BYTE); if (error) { imFileClose(ifile); return error; } if (iOldCounterCB) imCounterSetCallback(filename, iOldFileCounter); int count = width*height; void* data; if (green != red + count || blue != green + count) data = malloc(imImageDataSize(width, height, IM_RGB, IM_BYTE)); else data = red; if (!data) { imFileClose(ifile); return IM_ERR_MEM; } if (data != red) { memcpy(data, red, count); memcpy((unsigned char*)data+count, green, count); memcpy((unsigned char*)data+2*count, blue, count); } error = imFileWriteImageData(ifile, data); imFileClose(ifile); if (data != red) free(data); return error; }
int imLoadRGB(char *filename, unsigned char *red, unsigned char *green, unsigned char *blue) { int error; imFile* ifile = imFileOpen(filename, &error); if (!ifile) return error; int width, height, color_mode, data_type; error = imFileReadImageInfo(ifile, 0, &width, &height, &color_mode, &data_type); if (error) { imFileClose(ifile); return error; } if (iOldResolutionCB) { double xres = *(float*)imFileGetAttribute(ifile, "XResolution", NULL, NULL); double yres = *(float*)imFileGetAttribute(ifile, "YResolution", NULL, NULL); int res_unit = *(int*)imFileGetAttribute(ifile, "ResolutionUnit", NULL, NULL); iOldResolutionCB(filename, &xres, &yres, &res_unit); } if (iOldTiffImageDescCB) { char* img_desc = (char*)imFileGetAttribute(ifile, "Description", NULL, NULL); iOldTiffImageDescCB(filename, img_desc); } if (iOldGifTranspIndexCB) { unsigned char transp_index = *(unsigned char*)imFileGetAttribute(ifile, "TransparencyIndex", NULL, NULL); iOldGifTranspIndexCB(filename, &transp_index); } int count = width*height; void* data; if (green != red + count || blue != green + count) data = malloc(imImageDataSize(width, height, IM_RGB, IM_BYTE)); else data = red; if (!data) { imFileClose(ifile); return IM_ERR_MEM; } if (iOldCounterCB) imCounterSetCallback(filename, iOldFileCounter); error = imFileReadImageData(ifile, data, 1, 0); if (error) { if (data != red) free(data); imFileClose(ifile); return error; } if (imColorModeToBitmap(color_mode) != IM_RGB) { long palette[256]; int palette_count; imFileGetPalette(ifile, palette, &palette_count); iConvertMapToRGB((imbyte*)data, red, green, blue, count, palette, palette_count); } else if (data != red) { memcpy(red, data, count); memcpy(green, (unsigned char*)data+count, count); memcpy(blue, (unsigned char*)data+2*count, count); } imFileClose(ifile); if (data != red) free(data); return IM_ERR_NONE; }
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); }