Example #1
0
/*****************************************************************************\
 im.FileNew(filename, format)
\*****************************************************************************/
static int imluaFileNew (lua_State *L)
{
  const char *filename = luaL_checkstring(L, 1);
  const char *format = imlua_checkformat(L, 2);
  int error;

  imFile *ifile = imFileNew(filename, format, &error);
  return imlua_pushifileerror(L, ifile, error);
}
Example #2
0
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;
}
Example #3
0
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;
}