Example #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;
}
Example #2
0
/*****************************************************************************\
 file:GetPalette()
\*****************************************************************************/
static int imluaFileGetPalette (lua_State *L)
{
  imFile *ifile = imlua_checkfile(L, 1);
  long* palette = imPaletteNew(256);
  int count;
  imFileGetPalette(ifile, palette, &count);
  imlua_pushpalette(L, palette, count);
  return 1;
}
Example #3
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;
}
Example #4
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;
}
Example #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;
}