Пример #1
0
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;
}
Пример #2
0
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;
}
Пример #3
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;
}
Пример #4
0
/*****************************************************************************\
 file:ReadImageInfo()
\*****************************************************************************/
static int imluaFileReadImageInfo (lua_State *L)
{
  int width, height;
  int file_color_mode, file_data_type;
  int error;

  imFile *ifile = imlua_checkfile(L, 1);
  int index = luaL_optinteger(L, 2, 0);

  error = imFileReadImageInfo(ifile, index, &width, &height, &file_color_mode, &file_data_type);

  imlua_pusherror(L, error);
  if (error)
    return 1;

  lua_pushnumber(L, width);
  lua_pushnumber(L, height);
  lua_pushnumber(L, file_color_mode);
  lua_pushnumber(L, file_data_type);
  return 5;
}
Пример #5
0
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;
}
Пример #6
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);  
}