Exemple #1
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;
}
Exemple #2
0
int imFileReadImageInfo(imFile* ifile, int index, int *width, int *height, int *file_color_mode, int *file_data_type)
{
  assert(ifile);
  assert(!ifile->is_new);
  imFileFormatBase* ifileformat = (imFileFormatBase*)ifile;

  if (index >= ifile->image_count)
    return IM_ERR_DATA;

  if (ifile->image_index != -1 &&
      ifile->image_index == index)
  {
    if(width) *width = ifile->width;
    if(height) *height = ifile->height;
    if(file_color_mode) *file_color_mode = ifile->file_color_mode;
    if(file_data_type) *file_data_type = ifile->file_data_type;

    return IM_ERR_NONE;
  }

  ifile->convert_bpp = 0;
  ifile->switch_type = 0;

  int error = ifileformat->ReadImageInfo(index);
  if (error) return error;

  if (!imImageCheckFormat(ifile->file_color_mode, ifile->file_data_type))
    return IM_ERR_DATA;

  if (imColorModeSpace(ifile->file_color_mode) == IM_BINARY)
  {
    ifile->palette_count = 2;
    ifile->palette[0] = imColorEncode(0, 0, 0);
    ifile->palette[1] = imColorEncode(255, 255, 255);
  }

  if (imColorModeSpace(ifile->file_color_mode) == IM_MAP)
  {    
    if (iFileCheckPaletteGray(ifile))
      ifile->file_color_mode = (ifile->file_color_mode & 0xFF00) | IM_GRAY;

    if (iFileCheckPaletteBinary(ifile))
      ifile->file_color_mode = (ifile->file_color_mode & 0xFF00) | IM_BINARY;
  }

  if(width) *width = ifile->width;
  if(height) *height = ifile->height;
  if(file_color_mode) *file_color_mode = ifile->file_color_mode;
  if(file_data_type) *file_data_type = ifile->file_data_type;

  ifile->image_index = index; 

  return IM_ERR_NONE;
}
Exemple #3
0
int imColorModeIsBitmap(int color_mode, int data_type)
{
  if (imColorModeSpace(color_mode) == IM_BINARY || 
      imColorModeSpace(color_mode) == IM_MAP)
    return 1;

  if ((imColorModeSpace(color_mode) == IM_RGB || 
       imColorModeSpace(color_mode) == IM_GRAY) &&
      (data_type == IM_BYTE))
    return 1;

  return 0;
}
Exemple #4
0
int imFileWriteImageInfo(imFile* ifile, int width, int height, int user_color_mode, int user_data_type)
{
  assert(ifile);
  assert(ifile->is_new);
  imFileFormatBase* ifileformat = (imFileFormatBase*)ifile;

  if (!imImageCheckFormat(user_color_mode, user_data_type))
    return IM_ERR_DATA;

  int error = ifileformat->iformat->CanWrite(ifile->compression, user_color_mode, user_data_type);
  if (error) return error;

  ifile->width = width;
  ifile->height = height;
  ifile->user_color_mode = user_color_mode;
  ifile->user_data_type = user_data_type;

  if (imColorModeSpace(user_color_mode) == IM_BINARY)
  {
    ifile->palette_count = 2;
    ifile->palette[0] = imColorEncode(0, 0, 0);
    ifile->palette[1] = imColorEncode(255, 255, 255);
  }

  return ifileformat->WriteImageInfo();
}
int imFileFormatPFM::WriteImageInfo()
{
  this->file_data_type = this->user_data_type;
  this->file_color_mode = imColorModeSpace(this->user_color_mode);

  if (this->file_color_mode == IM_GRAY)
    this->image_type = 'f';
  else
  {
    this->image_type = 'F';
    this->file_color_mode |= IM_PACKED;
  }

  imBinFilePrintf(handle, "P%c\n", (int)this->image_type);

  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  imBinFilePrintf(handle, "%d ", this->width);
  imBinFilePrintf(handle, "%d\n", this->height);

  if (imBinCPUByteOrder() == IM_BIGENDIAN)
    imBinFilePrintf(handle, "1.0\n");
  else
    imBinFilePrintf(handle, "-1.0\n");

  /* tests if everything was ok */
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  return IM_ERR_NONE;
}
Exemple #6
0
int imColorModeDepth(int color_mode)
{
  int depth = 0;

  int color_space = imColorModeSpace(color_mode);
  switch (color_space)
  {
  case IM_GRAY:
  case IM_BINARY:
  case IM_MAP:   
    depth = 1; 
    break;
  case IM_CMYK:
    depth = 4; 
    break;
  default:
    depth = 3; 
    break;
  }

  if (imColorModeHasAlpha(color_mode))
    depth++;

  return depth;
}
Exemple #7
0
int imFileReadImageData(imFile* ifile, void* data, int convert2bitmap, int color_mode_flags)
{
  assert(ifile);
  assert(!ifile->is_new);
  imFileFormatBase* ifileformat = (imFileFormatBase*)ifile;

  if (ifile->image_index == -1)
    return IM_ERR_DATA;

  ifile->user_color_mode = ifile->file_color_mode;
  ifile->user_data_type = ifile->file_data_type;

  if (convert2bitmap)
  {
    ifile->user_data_type = IM_BYTE;
    ifile->user_color_mode = imColorModeToBitmap(ifile->file_color_mode);
  }

  if (color_mode_flags != -1)
  {
    ifile->user_color_mode = imColorModeSpace(ifile->user_color_mode);
    ifile->user_color_mode |= color_mode_flags;
  }

  if (!imImageCheckFormat(ifile->user_color_mode, ifile->user_data_type))
    return IM_ERR_DATA;

  if (!imFileCheckConversion(ifile))
    return IM_ERR_DATA;

  imFileLineBufferInit(ifile);

  int ret = ifileformat->ReadImageData(data);

  // here we can NOT change the file_color_mode we already returned to the user
  // so just check for gray and binary consistency

  if (imColorModeSpace(ifile->file_color_mode) == IM_GRAY && ifile->file_data_type == IM_BYTE)
    iFileCheckConvertGray(ifile, (imbyte*)data);

  if (imColorModeSpace(ifile->file_color_mode) == IM_BINARY)
    iFileCheckConvertBinary(ifile, (imbyte*)data);

  return ret;
}
Exemple #8
0
static int ColorMode2Type(int color_mode)
{
  switch (imColorModeSpace(color_mode))
  {
  case IM_BINARY:
  case IM_GRAY:
  case IM_MAP:
    return IM_MAP;
  default:
    return IM_RGB;
  }
}
int imFileFormatLED::WriteImageInfo()
{
  this->file_data_type = IM_BYTE;
  this->file_color_mode = imColorModeSpace(this->user_color_mode);
  this->file_color_mode |= IM_TOPDOWN;

  if (WritePalette() != IM_ERR_NONE)
    return IM_ERR_ACCESS;

  imBinFilePrintf(handle, "(%d, %d\n", this->width, this->height);

  return IM_ERR_NONE;
}
Exemple #10
0
int imColorModeToBitmap(int color_mode)
{
  int color_space = imColorModeSpace(color_mode);
  switch (color_space)
  {
  case IM_BINARY:
  case IM_GRAY:
  case IM_MAP:
    return color_space;
  default:
    return IM_RGB;
  }
}
Exemple #11
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;
}
Exemple #12
0
const char* imColorModeSpaceName(int color_mode)
{
  int color_space = imColorModeSpace(color_mode);
  switch (color_space)
  {
  case IM_RGB:    return "RGB";
  case IM_MAP:    return "Map";
  case IM_GRAY:   return "Gray";
  case IM_BINARY: return "Binary";
  case IM_CMYK:   return "CMYK";
  case IM_YCBCR:  return "Y'CbCr";
  case IM_LAB:    return "CIE L*a*b*";
  case IM_LUV:    return "CIE L*u*v*";
  case IM_XYZ:    return "CIE XYZ";
  }

  return NULL;
}
Exemple #13
0
int imFormatPFM::CanWrite(const char* compression, int color_mode, int data_type) const
{
  int color_space = imColorModeSpace(color_mode);

  if (color_space != IM_GRAY && color_space != IM_RGB)
    return IM_ERR_DATA;                       
                                              
  if (data_type != IM_FLOAT)
    return IM_ERR_DATA;

  if (!compression || compression[0] == 0)
    return IM_ERR_NONE;

  if (!imStrEqual(compression, "NONE"))
    return IM_ERR_COMPRESS;

  return IM_ERR_NONE;
}
int imFormatJPEG::CanWrite(const char* compression, int color_mode, int data_type) const
{
  int color_space = imColorModeSpace(color_mode);

  if (color_space == IM_MAP || color_space == IM_LAB || 
      color_space == IM_LUV || color_space == IM_XYZ)
    return IM_ERR_DATA;                       
                                              
  if (data_type != IM_BYTE)
    return IM_ERR_DATA;

  if (!compression || compression[0] == 0)
    return IM_ERR_NONE;

  if (!imStrEqual(compression, "JPEG"))
    return IM_ERR_COMPRESS;

  return IM_ERR_NONE;
}
Exemple #15
0
int imFormatJP2::CanWrite(const char* compression, int color_mode, int data_type) const
{
  int color_space = imColorModeSpace(color_mode);

  if (color_space == IM_MAP || color_space == IM_CMYK || 
      color_space == IM_LUV)
    return IM_ERR_DATA;                       
                                              
  if (data_type != IM_BYTE && data_type != IM_USHORT)
    return IM_ERR_DATA;

  if (!compression || compression[0] == 0)
    return IM_ERR_NONE;

  if (!imStrEqual(compression, "JPEG-2000"))
    return IM_ERR_COMPRESS;

  return IM_ERR_NONE;
}
int imFormatSGI::CanWrite(const char* compression, int color_mode, int data_type) const
{
  int color_space = imColorModeSpace(color_mode);

  if (color_space == IM_YCBCR || color_space == IM_LAB || 
      color_space == IM_LUV || color_space == IM_XYZ ||
      color_space == IM_CMYK || color_space == IM_MAP)
    return IM_ERR_DATA;                       
                                              
  if (data_type != IM_BYTE && data_type != IM_USHORT)
    return IM_ERR_DATA;

  if (!compression || compression[0] == 0)
    return IM_ERR_NONE;

  if (!imStrEqual(compression, "NONE") && !imStrEqual(compression, "RLE"))
    return IM_ERR_COMPRESS;

  return IM_ERR_NONE;
}
Exemple #17
0
/*****************************************************************************\
 im.ColorModeSpace(color_mode)
\*****************************************************************************/
static int imluaColorModeSpace (lua_State *L)
{
  lua_pushinteger(L, imColorModeSpace(luaL_checkinteger(L, 1)));
  return 1;
}
int imFileFormatSGI::WriteImageInfo()
{
  unsigned int dword_value;
  unsigned short word_value;
  unsigned char dummy[404];
  memset(dummy, 0, 404);

  this->comp_type = SGI_VERBATIM;
  if (imStrEqual(this->compression, "RLE"))
    this->comp_type = SGI_RLE;

  unsigned int color_map_id = SGI_NORMAL;

  this->file_color_mode = imColorModeSpace(this->user_color_mode);

  int dimension = 2;
  if (this->file_color_mode == IM_BINARY)
    this->convert_bpp = -1; // expand 1 to 255
  else if (this->file_color_mode == IM_RGB)
  {
    dimension = 3;
    if (imColorModeHasAlpha(this->user_color_mode))
      this->file_color_mode |= IM_ALPHA;
  }

  this->file_data_type = this->user_data_type;

  this->bpc = 1;
  int max = 255;
  if (this->file_data_type == IM_USHORT)
  {
    max = 65535;
    this->bpc = 2;
  }

  this->starttab = NULL;
  this->lengthtab = NULL;

  /* writes the SGI file header */
  word_value = SGI_ID;
  imBinFileWrite(handle, &word_value, 1, 2); /* identifier */
  imBinFileWrite(handle, &this->comp_type, 1, 1); /* storage */
  imBinFileWrite(handle, &this->bpc, 1, 1); /* bpc */
  word_value = (imushort)dimension;
  imBinFileWrite(handle, &word_value, 1, 2); /* dimension */
  word_value = (unsigned short)this->width;
  imBinFileWrite(handle, &word_value, 1, 2); /* image width */
  word_value = (unsigned short)this->height;
  imBinFileWrite(handle, &word_value, 1, 2); /* image height */
  word_value = (imushort)imColorModeDepth(this->file_color_mode);
  imBinFileWrite(handle, &word_value, 1, 2); /* depth */
  dword_value = 0;
  imBinFileWrite(handle, &dword_value, 1, 4); /* min */
  dword_value = max;
  imBinFileWrite(handle, &dword_value, 1, 4); /* max */
  imBinFileWrite(handle, dummy, 4, 1); /* dummy */

  /* tests if everything was ok */
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  int size;
  char* image_name = (char*)AttribTable()->Get("Description", NULL, &size);
  if (image_name)
  {
    if (size < 80)
    {
      imBinFileWrite(handle, image_name, size, 1); 
      imBinFileWrite(handle, dummy, 80-size, 1); 
    }
    else
    {
      imBinFileWrite(handle, image_name, 79, 1); 
      imBinFileWrite(handle, (void*)"\0", 1, 1); 
    }
  }
  else
    imBinFileWrite(handle, dummy, 80, 1); /* empty image name */

  dword_value = color_map_id;
  imBinFileWrite(handle, &dword_value, 1, 4); /* color_map_id */
  imBinFileWrite(handle, dummy, 404, 1); /* dummy */

  /* tests if everything was ok */
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  if (this->comp_type == SGI_RLE)
  {
    int tablen = this->height * imColorModeDepth(this->file_color_mode);
    this->starttab = (unsigned int *)malloc(tablen*4);
    this->lengthtab = (unsigned int *)malloc(tablen*4);

    /* writes the empty compression control information */
    /* we will write again at the end */
    imBinFileWrite(handle, this->starttab, tablen*4, 1);
    imBinFileWrite(handle, this->lengthtab, tablen*4, 1);

    // allocates more than enough since compression algoritm can be ineficient
    this->line_buffer_extra = 2*imImageLineSize(this->width, this->file_color_mode, this->file_data_type);
  }

  /* tests if everything was ok */
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  return IM_ERR_NONE;
}
int imFileFormatJPEG::WriteImageInfo()
{
  this->file_color_mode = imColorModeSpace(this->user_color_mode);
  this->file_color_mode |= IM_TOPDOWN;

  if (imColorModeDepth(this->file_color_mode) > 1)
    this->file_color_mode |= IM_PACKED;

  this->file_data_type = IM_BYTE;

  /* Step 3: set parameters for compression */
  this->cinfo.image_width = this->width;   /* image width and height, in pixels */
  this->cinfo.image_height = this->height;

  this->cinfo.input_components = imColorModeDepth(this->file_color_mode);

  switch (imColorModeSpace(this->user_color_mode))
  {
  case IM_BINARY:
    this->convert_bpp = -1; // expand 1 to 255
  case IM_GRAY:
    this->cinfo.in_color_space = JCS_GRAYSCALE;
    break;
  case IM_RGB:   
    this->cinfo.in_color_space = JCS_RGB;
    break;
  case IM_CMYK:
    this->cinfo.in_color_space = JCS_CMYK;
    break;
  case IM_YCBCR:
    this->cinfo.in_color_space = JCS_YCbCr;
    break;
  default:
    this->cinfo.in_color_space = JCS_UNKNOWN;
    break;
  }

  if (setjmp(this->jerr.setjmp_buffer)) 
    return IM_ERR_ACCESS;

  jpeg_set_defaults(&this->cinfo);

  imAttribTable* attrib_table = AttribTable();

  int* auto_ycbcr = (int*)attrib_table->Get("AutoYCbCr");
  if (auto_ycbcr && *auto_ycbcr == 0 &&
      this->cinfo.in_color_space == JCS_RGB)
  {
    jpeg_set_colorspace(&this->cinfo, JCS_RGB);
  }

  int* interlaced = (int*)attrib_table->Get("Interlaced");
  if (interlaced && *interlaced)
    jpeg_simple_progression(&this->cinfo);

  int* quality = (int*)attrib_table->Get("JPEGQuality");
  if (quality)
    jpeg_set_quality(&this->cinfo, *quality, TRUE);

  char* res_unit = (char*)attrib_table->Get("ResolutionUnit");
  if (res_unit)
  {
    float* xres = (float*)attrib_table->Get("XResolution");
    float* yres = (float*)attrib_table->Get("YResolution");

    if (xres && yres)
    {
      if (imStrEqual(res_unit, "DPI"))
        this->cinfo.density_unit = 1;
      else
        this->cinfo.density_unit = 2;

      this->cinfo.X_density = (UINT16)*xres;
      this->cinfo.Y_density = (UINT16)*yres;
    }
  }

  /* Step 4: Start compressor */
  jpeg_start_compress(&this->cinfo, TRUE);

  int desc_size;
  char* desc = (char*)attrib_table->Get("Description", NULL, &desc_size);
  if (desc)
    jpeg_write_marker(&this->cinfo, JPEG_COM, (JOCTET*)desc, desc_size-1);

#ifdef USE_EXIF
  iWriteExifAttrib(attrib_table);
#endif

  return IM_ERR_NONE;
}
Exemple #20
0
int imFileFormatJP2::WriteImageInfo()
{
  this->file_data_type = this->user_data_type;
  this->file_color_mode = imColorModeSpace(this->user_color_mode);
  this->file_color_mode |= IM_TOPDOWN;

  int prec = 8;
  if (this->file_data_type == IM_USHORT)
    prec = 16;

  jas_clrspc_t clrspc;
  switch (imColorModeSpace(this->user_color_mode))
  {
  case IM_BINARY:
    prec = 1;    
  case IM_GRAY:
    clrspc = JAS_CLRSPC_SGRAY;
    break;
  case IM_RGB:   
    clrspc = JAS_CLRSPC_SRGB;
    break;
  case IM_XYZ:
    clrspc = JAS_CLRSPC_CIEXYZ;
    break;
  case IM_LAB:
    clrspc = JAS_CLRSPC_CIELAB;
    break;
  case IM_YCBCR:
    clrspc = JAS_CLRSPC_SYCBCR;
    break;
  default:
    return IM_ERR_DATA;
  }

  if (imColorModeHasAlpha(this->user_color_mode))
    this->file_color_mode |= IM_ALPHA;

  int numcmpts = imColorModeDepth(this->file_color_mode);
  
  jas_image_cmptparm_t cmptparms[4];
  for (int i = 0; i < numcmpts; i++) 
  {
    jas_image_cmptparm_t* cmptparm = &cmptparms[i];

    cmptparm->tlx = 0;
    cmptparm->tly = 0;
    cmptparm->hstep = 1;
    cmptparm->vstep = 1;
    cmptparm->width = this->width;
    cmptparm->height = this->height;
    cmptparm->prec = prec;
    cmptparm->sgnd = 0;
  }

  this->image = jas_image_create(numcmpts, cmptparms, clrspc);
  if (!this->image)
    return IM_ERR_DATA;

  if (this->image->metadata.count > 0) 
  {
    const void* data;
    int size;
    imAttribTable* attrib_table = AttribTable();

    // GeoTIFF first
    data = attrib_table->Get("GeoTIFFBox", NULL, &size);
    if (data)
    {
      jas_metadata_box_t *metabox = &image->metadata.boxes[JAS_IMAGE_BOX_GEO]; 
      jas_box_alloc(metabox, size);
      memcpy(metabox->buf, data, size);
      memcpy(metabox->id, msi_uuid, sizeof(msi_uuid));
    }
   
    // Adobe XMP
    data = attrib_table->Get("XMLPacket", NULL, &size);
    if (data)
    {
      jas_metadata_box_t *metabox = &image->metadata.boxes[JAS_IMAGE_BOX_XMP]; 
      jas_box_alloc(metabox, size);
      memcpy(metabox->buf, data, size);
      memcpy(metabox->id, xmp_uuid, sizeof(xmp_uuid));
    }
  }

  return IM_ERR_NONE;
}
int imFileFormatAVI::WriteImageInfo()
{
  if (dib)
  {
    if (dib->bmih->biWidth != width || dib->bmih->biHeight != height ||
        imColorModeSpace(file_color_mode) != imColorModeSpace(user_color_mode))
      return IM_ERR_DATA;

    return IM_ERR_NONE;  // parameters can be set only once
  }

  // force bottom up orientation
  this->file_data_type = IM_BYTE;
  this->file_color_mode = imColorModeSpace(this->user_color_mode);

  int bpp;
  if (this->file_color_mode == IM_RGB)
  {
    this->file_color_mode |= IM_PACKED;
    bpp = 24;

    if (imColorModeHasAlpha(this->user_color_mode))
    {
      this->file_color_mode |= IM_ALPHA;
      bpp = 32;

      this->rmask = 0x00FF0000;
      this->roff = 16;

      this->gmask = 0x0000FF00;
      this->goff = 8;

      this->bmask = 0x000000FF;
      this->boff = 0;
    }
  }
  else
    bpp = 8;

  this->line_buffer_extra = 4; // room enough for padding

  imAttribTable* attrib_table = AttribTable();

  const void* attrib_data = attrib_table->Get("FPS");
  if (attrib_data)
    fps = *(float*)attrib_data;
  else
    fps = 15;

  if (this->compression[0] == 0 || imStrEqual(this->compression, "NONE"))
    this->use_compressor = 0;
  else
    this->use_compressor = 1;

  dib = imDibCreate(width, height, bpp);

  if (use_compressor)
  {
    memset(&compvars, 0, sizeof(COMPVARS));
    compvars.cbSize = sizeof(COMPVARS);

    if (imStrEqual(this->compression, "CUSTOM"))
    {
      if (ICCompressorChoose(NULL, ICMF_CHOOSE_DATARATE | ICMF_CHOOSE_KEYFRAME, dib->dib, NULL, &compvars, "Choose Compression") == FALSE)
        return IM_ERR_COMPRESS;
    }
    else
    {
      compvars.dwFlags = ICMF_COMPVARS_VALID;
      compvars.fccType = ICTYPE_VIDEO;

      int* attrib = (int*)attrib_table->Get("KeyFrameRate");
      if (attrib)
        compvars.lKey = *attrib;
      else
        compvars.lKey = 15;        // same defaults of the dialog

      attrib = (int*)attrib_table->Get("DataRate");
      if (attrib)
        compvars.lDataRate = *attrib / 8;
      else
        compvars.lDataRate = 300;  // same defaults of the dialog

      attrib = (int*)attrib_table->Get("AVIQuality");
      if (attrib)
        compvars.lQ = *attrib;
      else
        compvars.lQ = (DWORD)ICQUALITY_DEFAULT;

      if (imStrEqual(this->compression, "RLE"))
        compvars.fccHandler = mmioFOURCC('M','R','L','E');
      else if (imStrEqual(this->compression, "CINEPACK"))
        compvars.fccHandler = mmioFOURCC('c','v','i','d');    
      else
        compvars.fccHandler = mmioFOURCC(compression[0],compression[1],compression[2],compression[3]);

      compvars.hic = ICOpen(ICTYPE_VIDEO, compvars.fccHandler, ICMODE_COMPRESS);
    }

    if (compvars.hic == NULL)
      use_compressor = 0;
  }

  AVISTREAMINFO streaminfo;
  memset(&streaminfo, 0, sizeof(AVISTREAMINFO));
  streaminfo.fccType = streamtypeVIDEO;
  streaminfo.dwScale = 1000;
  streaminfo.dwRate  = (DWORD)(fps*1000);
  SetRect(&streaminfo.rcFrame, 0, 0, width, height);

  if (use_compressor)
  {
    streaminfo.fccHandler = compvars.fccHandler;
    streaminfo.dwQuality = compvars.lQ;
  }
  else
  {
    streaminfo.fccHandler = mmioFOURCC('D','I','B',' ');
    streaminfo.dwQuality = (DWORD)ICQUALITY_DEFAULT;
  }

  /* creates a new stream in the new file */
  HRESULT hr = AVIFileCreateStream(file, &stream, &streaminfo);         
  if (hr != 0)
    return IM_ERR_ACCESS;

  /* set stream format */
  if (use_compressor)
  {
    if (!ICSeqCompressFrameStart(&compvars, dib->bmi))
      return IM_ERR_COMPRESS;

    hr = AVIStreamSetFormat(stream, 0, compvars.lpbiOut, dib->size - dib->bits_size); 
  }
  else
    hr = AVIStreamSetFormat(stream, 0, dib->dib, dib->size - dib->bits_size); 

  if (hr != 0)
    return IM_ERR_ACCESS;

  return IM_ERR_NONE;
}
Exemple #22
0
int imFileFormatRAS::WriteImageInfo()
{
  this->file_data_type = IM_BYTE;
  this->file_color_mode = imColorModeSpace(this->user_color_mode);

  if (imStrEqual(this->compression, "RLE"))
    this->comp_type = RAS_BYTE_ENCODED;
  else
    this->comp_type = RAS_STANDARD;

  // Force the palette, even for Binary and Gray.
  this->map_type = RAS_EQUAL_RGB;

  if (this->file_color_mode == IM_BINARY)
  {
    this->bpp = 1;
    this->convert_bpp = 1;
  }
  else if (this->file_color_mode == IM_RGB)
  {
    this->file_color_mode |= IM_PACKED;
    this->map_type = RAS_NONE;
    this->bpp = 24;

    if (imColorModeHasAlpha(this->user_color_mode))
    {
      this->file_color_mode |= IM_ALPHA;
      this->bpp = 32;
    }
  }
  else
    this->bpp = 8;

  this->file_color_mode |= IM_TOPDOWN;

  this->line_raw_size = imFileLineSizeAligned(this->width, this->bpp, 2);
  this->line_buffer_extra = 2; // room enough for padding

  if (this->comp_type == RAS_BYTE_ENCODED)
  {
    // allocates more than enough since compression algoritm can be ineficient
    this->line_buffer_extra += 2*this->line_raw_size;
  }

  /* writes the RAS file header */

  unsigned int dword_value = RAS_ID;
  imBinFileWrite(handle, &dword_value, 1, 4); /* identifier */
  dword_value = this->width;
  imBinFileWrite(handle, &dword_value, 1, 4); /* image width */
  dword_value = this->height;
  imBinFileWrite(handle, &dword_value, 1, 4); /* image height */
  dword_value = this->bpp;
  imBinFileWrite(handle, &dword_value, 1, 4); /* bits per pixel */
  dword_value = this->height * this->line_raw_size;
  imBinFileWrite(handle, &dword_value, 1, 4); /* image lenght */
  dword_value = this->comp_type;
  imBinFileWrite(handle, &dword_value, 1, 4); /* compression information */
  dword_value = this->map_type;
  imBinFileWrite(handle, &dword_value, 1, 4); /* palette information */
  dword_value = (this->map_type == RAS_NONE)? 0: this->palette_count * 3;
  imBinFileWrite(handle, &dword_value, 1, 4); /* palette lenght */

  /* tests if everything was ok */
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  if (this->map_type != RAS_NONE)
    return WritePalette();

  return IM_ERR_NONE;
}